p.Chaos.BroadPhase.MidPhaseRedistributionEnabled

p.Chaos.BroadPhase.MidPhaseRedistributionEnabled

#Overview

name: p.Chaos.BroadPhase.MidPhaseRedistributionEnabled

This variable is created as a Console Variable (cvar).

It is referenced in 4 C++ source files.

#Summary

#Usage in the C++ source code

The purpose of p.Chaos.BroadPhase.MidPhaseRedistributionEnabled is to control the distribution of midphase collision detection tasks among worker threads in Unreal Engine’s Chaos physics system.

This setting variable is primarily used in the Chaos physics subsystem, specifically in the broadphase and midphase collision detection processes. It is part of the Experimental Chaos module in Unreal Engine 5.

The value of this variable is set using an FAutoConsoleVariableRef, which means it can be modified at runtime through console commands or configuration files. It is initialized to true by default.

The associated variable bChaosMidPhaseRedistributionEnabled directly interacts with p.Chaos.BroadPhase.MidPhaseRedistributionEnabled. They share the same value and are used interchangeably in the code.

Developers must be aware that enabling this variable (which is the default behavior) can improve the distribution of midphase collision detection tasks among worker threads. This can lead to better performance in multi-threaded environments, especially when dealing with complex scenes with many collision objects.

Best practices when using this variable include:

  1. Leaving it enabled (true) for most scenarios, as it can improve performance.
  2. Testing the performance impact in your specific use case, as the optimal setting may depend on the complexity of your scene and the number of available CPU cores.
  3. Consider disabling it only if you encounter specific issues or if you have a custom collision detection system that conflicts with this redistribution mechanism.

Regarding the associated variable bChaosMidPhaseRedistributionEnabled:

The purpose of bChaosMidPhaseRedistributionEnabled is the same as p.Chaos.BroadPhase.MidPhaseRedistributionEnabled. It is used internally within the Chaos physics system to control the redistribution of midphase collision detection tasks.

This variable is used in the Chaos physics subsystem, specifically in the SpatialAccelerationBroadPhase class, which is part of the collision detection system.

The value of bChaosMidPhaseRedistributionEnabled is set through the console variable p.Chaos.BroadPhase.MidPhaseRedistributionEnabled.

It interacts directly with p.Chaos.BroadPhase.MidPhaseRedistributionEnabled and is used in conditional statements to determine whether to redistribute midphase tasks among multiple contexts.

Developers should be aware that this variable is used in performance-critical code paths, and its value can significantly impact the distribution of work among threads.

Best practices for using bChaosMidPhaseRedistributionEnabled include:

  1. Generally, avoid modifying this variable directly in code. Instead, use the console variable p.Chaos.BroadPhase.MidPhaseRedistributionEnabled to control its behavior.
  2. When profiling physics performance, pay attention to how this setting affects the distribution of work among threads, especially in scenes with many collision objects.

#References in C++ code

#Callsites

This variable is referenced in the following C++ source code:

#Loc: <Workspace>/Engine/Source/Runtime/Experimental/Chaos/Private/Chaos/PBDRigidsEvolutionGBF.cpp:190

Scope (from outer to inner):

file
namespace    Chaos
namespace    CVars

Source code excerpt:

		// broadphas eoverlap was detected on which tends to give poor distribution.
		bool bChaosMidPhaseRedistributionEnabled = true;
		FAutoConsoleVariableRef CVarChaosSolverMidPhaseRedistributionEnabled(TEXT("p.Chaos.BroadPhase.MidPhaseRedistributionEnabled"), bChaosMidPhaseRedistributionEnabled, TEXT(""));
	}
	using namespace CVars;

	// We want to have largish blocks, but when we are running on multiple cores we don't want to waste too much memory
	int32 CalculateNumCollisionsPerBlock()
	{

#Associated Variable and Callsites

This variable is associated with another variable named bChaosMidPhaseRedistributionEnabled. They share the same value. See the following C++ source code.

#Loc: <Workspace>/Engine/Source/Runtime/Experimental/Chaos/Private/Chaos/PBDRigidsEvolutionGBF.cpp:189

Scope (from outer to inner):

file
namespace    Chaos
namespace    CVars

Source code excerpt:

		// Enable improved midphase distribution among worker threads. Without this the midphase and narrowphase run on whatever thread the
		// broadphas eoverlap was detected on which tends to give poor distribution.
		bool bChaosMidPhaseRedistributionEnabled = true;
		FAutoConsoleVariableRef CVarChaosSolverMidPhaseRedistributionEnabled(TEXT("p.Chaos.BroadPhase.MidPhaseRedistributionEnabled"), bChaosMidPhaseRedistributionEnabled, TEXT(""));
	}
	using namespace CVars;

	// We want to have largish blocks, but when we are running on multiple cores we don't want to waste too much memory
	int32 CalculateNumCollisionsPerBlock()
	{

#Loc: <Workspace>/Engine/Source/Runtime/Experimental/Chaos/Public/Chaos/Collision/SpatialAccelerationBroadPhase.h:22

Scope (from outer to inner):

file
namespace    Chaos::CVars

Source code excerpt:

namespace Chaos::CVars
{
	extern bool bChaosMidPhaseRedistributionEnabled;
	extern int32 ChaosOneWayInteractionPairCollisionMode;
}

namespace Chaos
{
	template <typename TPayloadType, typename T, int d>

#Loc: <Workspace>/Engine/Source/Runtime/Experimental/Chaos/Public/Chaos/Collision/SpatialAccelerationBroadPhase.h:614

Scope (from outer to inner):

file
namespace    Chaos
class        class FSpatialAccelerationBroadPhase
function     void RedistributeMidPhasesInContexts

Source code excerpt:

			QUICK_SCOPE_CYCLE_COUNTER(STAT_Collisions_RedistributeMidPhases);

			if ((NumActiveBroadphaseContexts > 1) && CVars::bChaosMidPhaseRedistributionEnabled)
			{
				// We want this many midphases per worker
				const int32 NumMidPhasesPerContext = FMath::DivideAndRoundUp(NumMidPhases, NumActiveBroadphaseContexts);

				// Reserve array space
				for (int32 ContextIndex = 0; ContextIndex < NumActiveBroadphaseContexts; ++ContextIndex)