r.OptimizedWPO.AffectNonNaniteShaderSelection

r.OptimizedWPO.AffectNonNaniteShaderSelection

#Overview

name: r.OptimizedWPO.AffectNonNaniteShaderSelection

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

It is referenced in 3 C++ source files.

#Summary

#Usage in the C++ source code

The purpose of r.OptimizedWPO.AffectNonNaniteShaderSelection is to control whether the per-primitive World Position Offset (WPO) flag should influence shader selection for non-Nanite primitives in Unreal Engine’s rendering system.

This setting variable is primarily used by the Engine’s rendering subsystem, specifically in the context of primitive scene proxies and shader selection.

The value of this variable is set through a console variable (CVar) system, initialized as false by default. It can be changed at runtime through console commands or programmatically.

The associated variable CVarOptimizedWPOAffectNonNaniteShaderSelection directly interacts with r.OptimizedWPO.AffectNonNaniteShaderSelection. They share the same value and purpose.

Developers must be aware that enabling this variable can increase the chances of selecting the position-only depth vertex shader for non-Nanite primitives. However, this comes at the cost of updating cached draw commands whenever the WPO flag changes, which could impact performance.

Best practices when using this variable include:

  1. Carefully consider the trade-off between potentially improved shader selection and the performance cost of updating cached draw commands.
  2. Monitor performance metrics when enabling this feature, especially in scenes with many non-Nanite primitives using World Position Offset.
  3. Use this setting in conjunction with other WPO-related optimizations for a comprehensive approach to managing World Position Offset in your project.

Regarding the associated variable CVarOptimizedWPOAffectNonNaniteShaderSelection:

This is the actual C++ variable that controls the behavior described for r.OptimizedWPO.AffectNonNaniteShaderSelection. It’s implemented as a TAutoConsoleVariable, which allows it to be changed at runtime through the console system.

The value of this variable is accessed in the Engine code through the GetValueOnAnyThread() method, as seen in the ShouldOptimizedWPOAffectNonNaniteShaderSelection() function. This function serves as a convenient way for other parts of the engine to check if this optimization should be applied.

When working with this variable, developers should be aware that changes to its value will trigger a global component recreate render state context, which can be performance-intensive. Therefore, it’s advisable to change this setting only when necessary and not frequently during runtime.

#References in C++ code

#Callsites

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

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/PrimitiveSceneProxy.cpp:42

Scope: file

Source code excerpt:


static TAutoConsoleVariable<bool> CVarOptimizedWPOAffectNonNaniteShaderSelection(
	TEXT("r.OptimizedWPO.AffectNonNaniteShaderSelection"),
	false,
	TEXT("Whether the per primitive WPO flag should affect shader selection for non-nanite primitives. It increase the chance of selecting the position only depth VS ")
	TEXT("at the cost of updating cached draw commands whenever the WPO flag changes."),
	FConsoleVariableDelegate::CreateLambda([](IConsoleVariable* InVariable)
	{
		FGlobalComponentRecreateRenderStateContext Context;

#Associated Variable and Callsites

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

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/PrimitiveSceneProxy.cpp:41

Scope: file

Source code excerpt:

	);

static TAutoConsoleVariable<bool> CVarOptimizedWPOAffectNonNaniteShaderSelection(
	TEXT("r.OptimizedWPO.AffectNonNaniteShaderSelection"),
	false,
	TEXT("Whether the per primitive WPO flag should affect shader selection for non-nanite primitives. It increase the chance of selecting the position only depth VS ")
	TEXT("at the cost of updating cached draw commands whenever the WPO flag changes."),
	FConsoleVariableDelegate::CreateLambda([](IConsoleVariable* InVariable)
	{

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/PrimitiveSceneProxy.cpp:121

Scope (from outer to inner):

file
function     bool ShouldOptimizedWPOAffectNonNaniteShaderSelection

Source code excerpt:

bool ShouldOptimizedWPOAffectNonNaniteShaderSelection()
{
	return CVarOptimizedWPOAffectNonNaniteShaderSelection.GetValueOnAnyThread() != 0;
}

bool CacheShadowDepthsFromPrimitivesUsingWPO()
{
	return CVarCacheWPOPrimitives.GetValueOnAnyThread(true) != 0;
}