r.Shaders.FastMath

r.Shaders.FastMath

#Overview

name: r.Shaders.FastMath

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.FastMath is to control whether fast-math optimizations are used in shaders. This setting variable is used to enable or disable fast math operations in shader compilation.

The Unreal Engine shader compiler system relies on this setting variable. It’s referenced in the ShaderCompiler module, specifically in the shader compilation process.

The value of this variable is set through a console variable (CVar) named “r.Shaders.FastMath”. By default, it’s set to 1, meaning fast math optimizations are enabled.

This variable interacts with the compiler flags for shader compilation. When r.Shaders.FastMath is set to 0, the CFLAG_NoFastMath flag is added to the shader compiler environment, disabling fast math optimizations.

Developers should be aware that changing this setting can affect shader performance and precision. Fast math optimizations can improve performance but may slightly reduce precision in some calculations.

Best practices when using this variable:

  1. Keep it enabled (default value of 1) for better performance in most cases.
  2. If you notice precision issues in your shaders, consider disabling it for debugging.
  3. Be consistent across your project to avoid unexpected behavior.

Regarding the associated variable CVarShaderFastMath:

The purpose of CVarShaderFastMath is to provide programmatic access to the r.Shaders.FastMath setting within the C++ code.

It’s part of the console variable system in Unreal Engine, allowing runtime modification and querying of the fast math setting.

The value of CVarShaderFastMath is set when the console variable is registered, mirroring the value of r.Shaders.FastMath.

This variable is used in the shader compilation process to determine whether to add the CFLAG_NoFastMath flag to the compiler environment.

Developers should use CVarShaderFastMath when they need to programmatically check or modify the fast math setting in C++ code.

Best practices for CVarShaderFastMath:

  1. Use it for runtime queries of the fast math setting.
  2. If modifying, ensure you understand the implications on shader compilation and performance.
  3. Consider using it in conjunction with other shader-related settings for comprehensive shader control.

#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:2215

Scope: file

Source code excerpt:


static TAutoConsoleVariable<int32> CVarShaderFastMath(
	TEXT("r.Shaders.FastMath"),
	1,
	TEXT("Whether to use fast-math optimisations in shaders."),
	ECVF_ReadOnly);

static TAutoConsoleVariable<int32> CVarShaderZeroInitialise(
	TEXT("r.Shaders.ZeroInitialise"),

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

Scope (from outer to inner):

file
function     void ShaderMapAppendKeyString

Source code excerpt:

	{
		// Always default to fast math unless specified
		static const auto CVar = IConsoleManager::Get().FindConsoleVariable(TEXT("r.Shaders.FastMath"));
		KeyString += (CVar && CVar->GetInt() == 0) ? TEXT("_NoFastMath") : TEXT("");
	}

	{
		static FShaderPlatformCachedIniValue<int32> CVarWarningsAsErrorsPerPlatform(TEXT("r.Shaders.WarningsAsErrors"));
		if (const int32 Level = CVarWarningsAsErrorsPerPlatform.Get(Platform); Level != 0)

#Associated Variable and Callsites

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

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

Scope: file

Source code excerpt:

	ECVF_ReadOnly);

static TAutoConsoleVariable<int32> CVarShaderFastMath(
	TEXT("r.Shaders.FastMath"),
	1,
	TEXT("Whether to use fast-math optimisations in shaders."),
	ECVF_ReadOnly);

static TAutoConsoleVariable<int32> CVarShaderZeroInitialise(

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

Scope (from outer to inner):

file
function     void GlobalBeginCompileShader

Source code excerpt:

	}

	if (CVarShaderFastMath.GetValueOnAnyThread() == 0)
	{
		Input.Environment.CompilerFlags.Add(CFLAG_NoFastMath);
	}
    
	if (bUsingMobileRenderer)
    {