r.Shadow.SpotLightDepthBias

r.Shadow.SpotLightDepthBias

#Overview

name: r.Shadow.SpotLightDepthBias

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.Shadow.SpotLightDepthBias is to control the depth bias applied in the depth pass for whole-scene projected shadows from spot lights. This setting variable is part of Unreal Engine 5’s rendering system, specifically the shadow rendering subsystem.

The Unreal Engine subsystem that relies on this setting variable is the Renderer module, particularly the shadow rendering component. This can be seen from the file location where the variable is defined and used: ‘Engine/Source/Runtime/Renderer/Private/ShadowRendering.cpp’.

The value of this variable is set as a console variable with a default value of 3.0f. It can be changed at runtime through console commands or programmatically.

This variable interacts closely with another variable named CVarSpotLightShadowSlopeScaleDepthBias, which controls the slope scale depth bias for spot light shadows. Together, these variables help fine-tune the shadow rendering for spot lights.

Developers must be aware that this variable directly affects the quality and accuracy of shadows cast by spot lights. Adjusting this value can help reduce shadow acne (self-shadowing artifacts) but may also introduce peter-panning (shadows detaching from objects) if set too high.

Best practices when using this variable include:

  1. Start with the default value and adjust incrementally.
  2. Test in various lighting conditions and environments to ensure optimal shadow quality.
  3. Consider the interaction with CVarSpotLightShadowSlopeScaleDepthBias when making adjustments.
  4. Be mindful of performance implications when increasing the bias significantly.

Regarding the associated variable CVarSpotLightShadowDepthBias:

The purpose of CVarSpotLightShadowDepthBias is the same as r.Shadow.SpotLightDepthBias, as they share the same value. It’s an internal representation of the console variable within the C++ code.

This variable is used directly in the FProjectedShadowInfo::UpdateShaderDepthBias function to calculate the actual depth bias applied to spot light shadows. The calculation takes into account the scene’s depth range and resolution, as well as any user-defined shadow bias.

Developers should be aware that modifying CVarSpotLightShadowDepthBias directly in code will have the same effect as changing r.Shadow.SpotLightDepthBias through console commands. It’s generally recommended to use the console variable (r.Shadow.SpotLightDepthBias) for adjustments rather than modifying the C++ variable directly, as this allows for easier runtime tweaking and debugging.

#References in C++ code

#Callsites

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

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/ShadowRendering.cpp:113

Scope: file

Source code excerpt:

// Spot light
static TAutoConsoleVariable<float> CVarSpotLightShadowDepthBias(
	TEXT("r.Shadow.SpotLightDepthBias"),
	3.0f,
	TEXT("Depth bias that is applied in the depth pass for whole-scene projected shadows from spot lights"),
	ECVF_RenderThreadSafe);

static TAutoConsoleVariable<float> CVarSpotLightShadowSlopeScaleDepthBias(
	TEXT("r.Shadow.SpotLightSlopeDepthBias"),

#Associated Variable and Callsites

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

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/ShadowRendering.cpp:112

Scope: file

Source code excerpt:

///////////////////////////////////////////////////////////////////////////////////////////////////
// Spot light
static TAutoConsoleVariable<float> CVarSpotLightShadowDepthBias(
	TEXT("r.Shadow.SpotLightDepthBias"),
	3.0f,
	TEXT("Depth bias that is applied in the depth pass for whole-scene projected shadows from spot lights"),
	ECVF_RenderThreadSafe);

static TAutoConsoleVariable<float> CVarSpotLightShadowSlopeScaleDepthBias(

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/ShadowRendering.cpp:1891

Scope (from outer to inner):

file
function     void FProjectedShadowInfo::UpdateShaderDepthBias

Source code excerpt:

		{
			// spot lights (old code, might need to be improved)
			const float LightTypeDepthBias = CVarSpotLightShadowDepthBias.GetValueOnRenderThread();
			DepthBias = LightTypeDepthBias * 512.0f / ((MaxSubjectZ - MinSubjectZ) * FMath::Max(ResolutionX, ResolutionY));
			// * 2.0f to be compatible with the system we had before ShadowBias
			DepthBias *= 2.0f * LightSceneInfo->Proxy->GetUserShadowBias();

			SlopeScaleDepthBias = CVarSpotLightShadowSlopeScaleDepthBias.GetValueOnRenderThread();
			SlopeScaleDepthBias *= LightSceneInfo->Proxy->GetUserShadowSlopeBias();