r.AOViewFadeDistanceScale

r.AOViewFadeDistanceScale

#Overview

name: r.AOViewFadeDistanceScale

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

It is referenced in 6 C++ source files.

#Summary

#Usage in the C++ source code

The purpose of r.AOViewFadeDistanceScale is to control the distance over which Ambient Occlusion (AO) fades out as it approaches the maximum AO view distance. It is used in the rendering system, specifically for the Ambient Occlusion post-processing effect.

This setting variable is primarily used in the Renderer module of Unreal Engine 5. It’s referenced in several files related to distance field ambient occlusion and lighting post-processing.

The value of this variable is set through the Unreal Engine console variable system. It’s initialized with a default value of 0.7 and can be modified at runtime.

The associated variable GAOViewFadeDistanceScale interacts directly with r.AOViewFadeDistanceScale. They share the same value, with GAOViewFadeDistanceScale being the actual variable used in the C++ code.

Developers should be aware that this variable affects the visual quality and performance of the ambient occlusion effect. A higher value will cause AO to fade out over a larger distance, potentially improving performance but reducing the visibility of AO at medium to long distances.

Best practices when using this variable include:

  1. Adjusting it in conjunction with r.AOMaxViewDistance for optimal visual results.
  2. Testing different values to find the right balance between visual quality and performance for your specific scene.
  3. Considering the scale of your environments when setting this value.

Regarding the associated variable GAOViewFadeDistanceScale:

The purpose of GAOViewFadeDistanceScale is the same as r.AOViewFadeDistanceScale, serving as the internal C++ variable that directly affects the AO calculations.

It’s used in various parts of the rendering pipeline, including setting up AO shader parameters, upsampling parameters, and in sky reflection calculations.

The value of GAOViewFadeDistanceScale is set by the console variable r.AOViewFadeDistanceScale.

It interacts with other variables in the AO system, such as the maximum AO view distance and the AO buffer size.

Developers should be aware that modifying GAOViewFadeDistanceScale directly in the code is not recommended, as it’s controlled by the console variable.

Best practices include using the console variable r.AOViewFadeDistanceScale to modify this value rather than changing the C++ code directly, ensuring thread-safety when accessing this variable, and considering its impact on the entire AO system when making changes.

#References in C++ code

#Callsites

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

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/DistanceFieldLightingPost.cpp:56

Scope: file

Source code excerpt:

float GAOViewFadeDistanceScale = .7f;
FAutoConsoleVariableRef CVarAOViewFadeDistanceScale(
	TEXT("r.AOViewFadeDistanceScale"),
	GAOViewFadeDistanceScale,
	TEXT("Distance over which AO will fade out as it approaches r.AOMaxViewDistance, as a fraction of r.AOMaxViewDistance."),
	ECVF_RenderThreadSafe
	);

bool UseAOHistoryStabilityPass()

#Associated Variable and Callsites

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

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/DistanceFieldAmbientOcclusion.cpp:261

Scope: file

Source code excerpt:

uint32 GAONumConeSteps = 10;

extern float GAOViewFadeDistanceScale;

FAOParameters DistanceField::SetupAOShaderParameters(const FDistanceFieldAOParameters& Parameters)
{
	const float AOLargestSampleOffset = Parameters.ObjectMaxOcclusionDistance / (1 + FMath::Tan(GAOConeHalfAngle));

	FAOParameters ShaderParameters;

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/DistanceFieldAmbientOcclusion.cpp:279

Scope (from outer to inner):

file
function     FDFAOUpsampleParameters DistanceField::SetupAOUpsampleParameters

Source code excerpt:

FDFAOUpsampleParameters DistanceField::SetupAOUpsampleParameters(const FViewInfo& View, FRDGTextureRef DistanceFieldAOBentNormal)
{
	const float DistanceFadeScaleValue = 1.0f / ((1.0f - GAOViewFadeDistanceScale) * GetMaxAOViewDistance());

	const FIntPoint AOBufferSize = GetBufferSizeForAO(View);
	const FIntPoint AOViewSize = View.ViewRect.Size() / GAODownsampleFactor;
	const FVector2f UVMax(
		(AOViewSize.X - 0.51f) / AOBufferSize.X, // 0.51 - so bilateral gather4 won't sample invalid texels
		(AOViewSize.Y - 0.51f) / AOBufferSize.Y);

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/DistanceFieldLightingPost.cpp:54

Scope: file

Source code excerpt:

	);

float GAOViewFadeDistanceScale = .7f;
FAutoConsoleVariableRef CVarAOViewFadeDistanceScale(
	TEXT("r.AOViewFadeDistanceScale"),
	GAOViewFadeDistanceScale,
	TEXT("Distance over which AO will fade out as it approaches r.AOMaxViewDistance, as a fraction of r.AOMaxViewDistance."),
	ECVF_RenderThreadSafe
	);

bool UseAOHistoryStabilityPass()
{

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/DistanceFieldLightingPost.cpp:98

Scope (from outer to inner):

file
function     FGeometryAwareUpsampleParameters SetupGeometryAwareUpsampleParameters

Source code excerpt:

	const FVector4f BentNormalBufferAndTexelSizeValue(ConeTracingBufferSize.X, ConeTracingBufferSize.Y, 1.0f / ConeTracingBufferSize.X, 1.0f / ConeTracingBufferSize.Y);

	extern float GAOViewFadeDistanceScale;
	const float DistanceFadeScaleValue = 1.0f / ((1.0f - GAOViewFadeDistanceScale) * GetMaxAOViewDistance());

	const FIntRect AOViewRect = FIntRect(FIntPoint::ZeroValue, FIntPoint::DivideAndRoundDown(View.ViewRect.Size(), GAODownsampleFactor));

	FGeometryAwareUpsampleParameters ShaderParameters;
	ShaderParameters.DistanceFieldNormalTexture = DistanceFieldNormal;
	ShaderParameters.DistanceFieldNormalSampler = TStaticSamplerState<SF_Point, AM_Clamp, AM_Clamp, AM_Clamp>::GetRHI();

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/IndirectLightRendering.cpp:1731

Scope (from outer to inner):

file
function     static void AddSkyReflectionPass

Source code excerpt:

				(AOViewSize.Y - 0.51f) / AOBufferSize.Y);

			extern float GAOViewFadeDistanceScale;
			PassParameters->PS.AOMaxViewDistance = GetMaxAOViewDistance();
			PassParameters->PS.DistanceFadeScale = 1.0f / ((1.0f - GAOViewFadeDistanceScale) * GetMaxAOViewDistance());

			PassParameters->PS.BentNormalAOTexture = DynamicBentNormalAOTexture;
			PassParameters->PS.BentNormalAOSampler = TStaticSamplerState<SF_Bilinear>::GetRHI();
		}

		PassParameters->PS.AmbientOcclusionTexture = AmbientOcclusionTexture;