r.ShaderPipelineCache.BackgroundBatchSize

r.ShaderPipelineCache.BackgroundBatchSize

#Overview

name: r.ShaderPipelineCache.BackgroundBatchSize

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.ShaderPipelineCache.BackgroundBatchSize is to control the number of Pipeline State Objects (PSOs) to compile in a single batch operation when compiling in the background mode of the Shader Pipeline Cache system in Unreal Engine 5.

This setting variable is primarily used by the Rendering subsystem, specifically within the Shader Pipeline Cache module. It’s part of the optimization mechanism for shader compilation and loading.

The value of this variable is set through a console variable (CVarPSOFileCacheBackgroundBatchSize) in the engine’s configuration. It defaults to 1, meaning by default, it will compile a maximum of 1 PSO per frame when in background mode.

This variable interacts closely with other shader pipeline cache-related variables, such as r.ShaderPipelineCache.BatchSize and r.ShaderPipelineCache.PrecompileBatchSize, which control batch sizes for different compilation modes.

Developers should be aware that this variable specifically affects the background compilation mode. It’s designed to balance between compiling shaders and maintaining performance during less intensive game states, such as when the player is in menus.

Best practices for using this variable include:

  1. Adjust it based on the game’s performance requirements and the target hardware capabilities.
  2. Use in conjunction with FShaderPipelineCache::SetBatchMode to switch between background and fast compilation modes at appropriate times.
  3. Monitor the impact on frame rate and loading times when adjusting this value.

The associated variable CVarPSOFileCacheBackgroundBatchSize is the actual console variable that stores and controls the value of r.ShaderPipelineCache.BackgroundBatchSize. It’s defined in the engine’s C++ code and is used to retrieve the current value of the background batch size setting. This variable is used in various parts of the shader pipeline cache system to determine how many PSOs should be compiled in a single batch during background compilation.

#References in C++ code

#Callsites

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

#Loc: <Workspace>/Engine/Source/Runtime/RenderCore/Private/ShaderPipelineCache.cpp:58

Scope: file

Source code excerpt:


static TAutoConsoleVariable<int32> CVarPSOFileCacheBackgroundBatchSize(
														  TEXT("r.ShaderPipelineCache.BackgroundBatchSize"),
														  1,
														  TEXT("Set the number of PipelineStateObjects to compile in a single batch operation when compiling in the background. Defaults to a maximum of 1 per frame, due to async. file IO it is less in practice."),
														  ECVF_Default | ECVF_RenderThreadSafe
														  );
static TAutoConsoleVariable<int32> CVarPSOFileCacheBatchSize(
														   TEXT("r.ShaderPipelineCache.BatchSize"),

#Loc: <Workspace>/Engine/Source/Runtime/RenderCore/Public/ShaderPipelineCache.h:35

Scope: file

Source code excerpt:

 * - Enable the cache with r.ShaderPipelineCache.Enabled = 1, which allows the pipeline cache to load existing data from disk and precompile it.
 * - Set the default batch size with r.ShaderPipelineCache.BatchSize = X, where X is the maximum number of PSOs to compile in a single batch when precompiling in the default Fast BatchMode.
 * - Set the background batch size with r.ShaderPipelineCache.BackgroundBatchSize = X, where X is the maximum number of PSOs to compile when in the Background BatchMode.
 * - Instrument the game code to call FShaderPipelineCache::SetBatchMode to switch the batch mode between Fast & Background modes.
 * - BatchMode::Fast should be used when a loading screen or movie is being displayed to allow more PSOs to be compiled whereas Background should be used behind interactive menus.
 * - If required call NumPrecompilesRemaining to determine the total number of outstanding PSOs to compile and keep the loading screen or movie visible until complete.
 * - Depending on the game & target platform performance it may also be required to call PauseBatching to suspend precompilation during gameplay and then ResumeBatching when behind a loading screen, menu or movie to restart precompilation.
 *
 * Other Runtime Options:
 * - In the GGameIni (and thus also GGameUserSettingsIni) the Shader Pipeline Cache uses the [ShaderPipelineCache.CacheFile] section to store some settings.
 * - The LastOpened setting stores the name of the last opened cache file as specified with Open, which if present will be used within FShaderPipelineCache::Initialize to open the existing cache.
 *   If not specified this will default to the AppName.
 * - The SortMode settings stores the desired sort mode for the PSOs, which is one of:
 *		+ Default: Loaded in the order specified in the file.
 *		+ FirstToLatestUsed: Start with the PSOs with the lowest first-frame used and work toward those with the highest.
 *		+ MostToLeastUsed: Start with the most often used PSOs working toward the least.
 *   Will use "Default" within FShaderPipelineCache::Initialize & OpenPipelineFileCache if nothing is specified.
 * - The GameVersionKey is a read-only integer specified in the GGameIni that specifies the game content version to disambiguate incompatible versions of the game content. By default this is taken from the FEngineVersion changlist.
 *
 * Logging Usage:
 * - Enable the cache with r.ShaderPipelineCache.Enabled = 1 and also turn on runtime logging with r.ShaderPipelineCache.LogPSO = 1.
 * - Ensure that you have configured the game to open the appropriate cache on startup (see above) and allow the game to play.
 * - PSOs are logged as they are encountered as the Unreal Engine does not provide facility to cook them offline, so this system will only collect PSOs which are actually used to render.
 * - As such you must either manually play through the game to collect logs or automate the process which is beyond the scope of this code.
 * - The data can be saved at any time by calling FShaderPipelineCache::SavePipelineFileCache and this can happen automatically after a given number of PSOs by setting r.ShaderPipelineCache.SaveAfterPSOsLogged = X where X is the desired number of PSOs to log before saving (0 will disable auto-save).
 * - Log files are shader platform specific to reduce overheads.
 *
 * Notes:
 * - The open cache file can be changed by closing the existing file with ClosePipelineFileCache (which implicitly Fast saves) and then opening a new one with OpenPipelineFileCache.
 * - Different files can be used to minimise PSO compilation by having a file per-scalability bucket (i.e. one file for Low, one for Med, one for High).
 * - When logging if you switch files only new entries from after the switch will be logged, which means you will miss any PSOs that should have been logged prior to switching. This prevents polluting the cache with unexpected entries.
 * - The UnrealEd.MergeShaderPipelineCaches command-let can be used to merge cache files with the same file-version, shader platform and game-version.
 *
 * File Locations & Packaging:
 * - The writable cache file is always stored in the User Saved directory.
 * - The game can also provide an immutable copy within its Game Content directory which will be used as the initial or seed data.
 * - Having generated logs in development and merged them with UnrealEd.MergeShaderPipelineCaches they should be packaged inside the Gane Content directory for the relevant platform.
 *
 * Requirements:
 * - FShaderCodeLibrary must be enabled via Project Settings > Packaging > "Share Material Shader Code".
 * - Enabling "Native Shader Libraries" is optional, but strongly preferred for Metal.
 */
class FShaderPipelineCache : public FTickableObjectRenderThread
{
	// the FShaderPipelineCacheTask class represents a pipeline precompile of a single PSO file cache.
	friend class FShaderPipelineCacheTask;
public:
	/**
	 * Initializes the shader pipeline cache for the desired platform, called by the engine.
	 * The shader platform is used only to load the initial pipeline cache and can be changed by closing & reopening the cache if necessary.
	 */
	static RENDERCORE_API void Initialize(EShaderPlatform Platform);
	/** Terminates the shader pipeline cache, called by the engine. */
	static RENDERCORE_API void Shutdown();

	/** Pauses precompilation. */
	static RENDERCORE_API void PauseBatching();

#Loc: <Workspace>/Engine/Source/Runtime/RenderCore/Public/ShaderPipelineCache.h:93

Scope (from outer to inner):

file
class        class FShaderPipelineCache : public FTickableObjectRenderThread

Source code excerpt:

	enum class BatchMode
	{
		Background, // The maximum batch size is defined by r.ShaderPipelineCache.BackgroundBatchSize
		Fast, // The maximum batch size is defined by r.ShaderPipelineCache.BatchSize
		Precompile // The maximum batch size is defined by r.ShaderPipelineCache.PrecompileBatchSize
	};
	
	/** Sets the precompilation batching mode. */
	static RENDERCORE_API void SetBatchMode(BatchMode Mode);

#Associated Variable and Callsites

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

#Loc: <Workspace>/Engine/Source/Runtime/RenderCore/Private/ShaderPipelineCache.cpp:57

Scope: file

Source code excerpt:

														  );

static TAutoConsoleVariable<int32> CVarPSOFileCacheBackgroundBatchSize(
														  TEXT("r.ShaderPipelineCache.BackgroundBatchSize"),
														  1,
														  TEXT("Set the number of PipelineStateObjects to compile in a single batch operation when compiling in the background. Defaults to a maximum of 1 per frame, due to async. file IO it is less in practice."),
														  ECVF_Default | ECVF_RenderThreadSafe
														  );
static TAutoConsoleVariable<int32> CVarPSOFileCacheBatchSize(

#Loc: <Workspace>/Engine/Source/Runtime/RenderCore/Private/ShaderPipelineCache.cpp:787

Scope (from outer to inner):

file
function     void FShaderPipelineCache::SetBatchMode

Source code excerpt:

			default:
			{
				ShaderPipelineCache->BatchSize = CVarPSOFileCacheBackgroundBatchSize.GetValueOnAnyThread();
				ShaderPipelineCache->BatchTime = CVarPSOFileCacheBackgroundBatchTime.GetValueOnAnyThread();
				break;
			}
		}
		UE_CLOG(PreviousBatchSize != ShaderPipelineCache->BatchSize || PreviousBatchTime != ShaderPipelineCache->BatchTime, LogRHI, Log,
			TEXT("ShaderPipelineCache: Batch mode changed (%d): Size: %d -> %d, Time: %f -> %f"), (uint32)Mode, PreviousBatchSize, ShaderPipelineCache->BatchSize, PreviousBatchTime, ShaderPipelineCache->BatchTime);

#Loc: <Workspace>/Engine/Source/Runtime/RenderCore/Private/ShaderPipelineCache.cpp:1582

Scope (from outer to inner):

file
function     FShaderPipelineCache::FShaderPipelineCache

Source code excerpt:

			break;
		case 2:
			BatchSize = CVarPSOFileCacheBackgroundBatchSize.GetValueOnAnyThread();
			BatchTime = CVarPSOFileCacheBackgroundBatchTime.GetValueOnAnyThread();
			break;
		case 1:
		default:
			BatchSize = CVarPSOFileCacheBatchSize.GetValueOnAnyThread();
			BatchTime = CVarPSOFileCacheBatchTime.GetValueOnAnyThread();