ReloadGlobalShaders

ReloadGlobalShaders

#Overview

name: ReloadGlobalShaders

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 ReloadGlobalShaders is to provide a mechanism for reloading global shaders in Unreal Engine 5. Global shaders are shader programs that are used across the entire rendering pipeline and are not specific to any particular material or object.

This setting variable is primarily used by the Unreal Engine’s shader compiler and rendering subsystem. Based on the callsites, it appears to be utilized in the Engine module, specifically within the ShaderCompiler and ODSC (On-Demand Shader Compilation) components.

The value of this variable is not directly set as a traditional variable. Instead, it’s implemented as a console command that can be triggered to reload global shaders. This is evident from the FAutoConsoleCommand declaration in ShaderCompiler.cpp.

ReloadGlobalShaders interacts with other parts of the shader compilation system, particularly the global shader cache. It’s likely that when this command is executed, it triggers a recompilation or reloading of all global shaders from their source files.

Developers must be aware that calling ReloadGlobalShaders can have significant performance implications, as it forces a recompilation of all global shaders. This operation can be time-consuming and should not be performed frequently during runtime.

Best practices when using this variable include:

  1. Use it primarily during development and debugging, not in production builds.
  2. Call it when you’ve made changes to global shader code and need to see the effects without restarting the engine.
  3. Be prepared for a temporary performance hit when calling this command, as shader compilation can be resource-intensive.
  4. Ensure all rendering commands are flushed before reloading global shaders to prevent potential crashes or undefined behavior.
  5. Use it in conjunction with proper error handling and logging to catch and diagnose any issues that may arise during the reload process.

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

Scope: file

Source code excerpt:


static FAutoConsoleCommand CCmdReloadGlobalShaders = FAutoConsoleCommand(
	TEXT("ReloadGlobalShaders"),
	TEXT("Reloads the global shaders file"),
	FConsoleCommandDelegate::CreateStatic(ReloadGlobalShaders)
);

void SetGlobalShaderCacheOverrideDirectory(const TArray<FString>& Args)
{

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/ODSC/ODSCThread.cpp:95

Scope (from outer to inner):

file
function     bool FODSCMessageHandler::ReloadGlobalShaders

Source code excerpt:

}

bool FODSCMessageHandler::ReloadGlobalShaders() const
{
	return RecompileCommandType == ODSCRecompileCommand::Global;
}

FODSCThread::FODSCThread(const FString& HostIP)
	: Thread(nullptr),

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/ODSC/ODSCThread.h:38

Scope (from outer to inner):

file
class        class FODSCMessageHandler : public IPlatformFile::IFileServerMessageHandler

Source code excerpt:

	const TArray<uint8>& GetMeshMaterialMaps() const;
	const TArray<uint8>& GetGlobalShaderMap() const;
	bool ReloadGlobalShaders() const;

private:
	/** The time when this command was issued.  This isn't serialized to the cooking server. */
	double RequestStartTime = 0.0;

	/** The materials we send over the network and expect maps for on the return */

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

Scope (from outer to inner):

file
function     void ReloadGlobalShaders

Source code excerpt:

}

void ReloadGlobalShaders()
{
	UE_LOG(LogShaders, Display, TEXT("Reloading global shaders..."));

	// Flush pending accesses to the existing global shaders.
	FlushRenderingCommands();