r.Shaders.RemoveDeadCode

r.Shaders.RemoveDeadCode

#Overview

name: r.Shaders.RemoveDeadCode

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 r.Shaders.RemoveDeadCode is to enable or disable a preprocessing step that removes unreferenced code before compiling shaders. This setting is primarily used to improve shader compilation speed, especially for shaders that include many large utility headers.

This setting variable is mainly used by the shader compilation system in Unreal Engine 5. It affects how shaders are processed before they are sent to the graphics API for compilation. The main subsystems that rely on this variable are:

  1. Shader Compiler
  2. Rendering System

The value of this variable is set through the console variable system. It’s defined as a TAutoConsoleVariable with a default value of 1 (enabled).

The associated variable CVarShadersRemoveDeadCode interacts with r.Shaders.RemoveDeadCode. They share the same value and purpose.

Developers should be aware of the following when using this variable:

  1. Enabling this option can significantly improve shader compilation speed, especially for complex shaders.
  2. However, it may potentially remove code that was intended to be used in certain edge cases or debugging scenarios.
  3. Disabling this option might be necessary when debugging shader issues or when certain shader code is being unexpectedly removed.

Best practices when using this variable include:

  1. Keep it enabled (default setting) for normal development and production builds to benefit from faster shader compilation.
  2. Consider disabling it temporarily when investigating shader-related issues or unexpected behavior.
  3. Be aware that changing this setting might require recompilation of shaders to take effect.

Regarding the associated variable CVarShadersRemoveDeadCode:

It serves the same purpose as r.Shaders.RemoveDeadCode and is used internally by the engine to access the setting’s value. Developers typically don’t need to interact with this variable directly, as modifying r.Shaders.RemoveDeadCode through the console or configuration files will affect both variables.

#References in C++ code

#Callsites

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

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/ShaderCompiler/ShaderCompiler.cpp:2309

Scope: file

Source code excerpt:


static TAutoConsoleVariable<int32> CVarShadersRemoveDeadCode(
	TEXT("r.Shaders.RemoveDeadCode"),
	1,
	TEXT("Run a preprocessing step that removes unreferenced code before compiling shaders.\n")
	TEXT("This can improve the compilation speed for shaders which include many large utility headers.\n")
	TEXT("\t0: Keep all input source code.\n")
	TEXT("\t1: Remove unreferenced code before compilation (Default)\n"),
	ECVF_ReadOnly);

#Loc: <Workspace>/Engine/Source/Runtime/RenderCore/Private/Shader.cpp:2220

Scope (from outer to inner):

file
function     void ShaderMapAppendKeyString

Source code excerpt:


	{
		static const auto CVar = IConsoleManager::Get().FindTConsoleVariableDataInt(TEXT("r.Shaders.RemoveDeadCode"));
		if (CVar && CVar->GetValueOnAnyThread() != 0)
		{
			KeyString += TEXT("_MIN");
		}
	}

#Associated Variable and Callsites

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

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/ShaderCompiler/ShaderCompiler.cpp:2308

Scope: file

Source code excerpt:

	ECVF_ReadOnly);

static TAutoConsoleVariable<int32> CVarShadersRemoveDeadCode(
	TEXT("r.Shaders.RemoveDeadCode"),
	1,
	TEXT("Run a preprocessing step that removes unreferenced code before compiling shaders.\n")
	TEXT("This can improve the compilation speed for shaders which include many large utility headers.\n")
	TEXT("\t0: Keep all input source code.\n")
	TEXT("\t1: Remove unreferenced code before compilation (Default)\n"),

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/ShaderCompiler/ShaderCompiler.cpp:8647

Scope (from outer to inner):

file
function     void GlobalBeginCompileShader

Source code excerpt:

	SET_SHADER_DEFINE(Input.Environment, PLATFORM_SUPPORTS_BINDLESS, bEnableBindlessMacro);

	if (CVarShadersRemoveDeadCode.GetValueOnAnyThread())
	{
		Input.Environment.CompilerFlags.Add(CFLAG_RemoveDeadCode);
	}

	{
		static const auto CVar = IConsoleManager::Get().FindConsoleVariable(TEXT("r.VT.AnisotropicFiltering"));