UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ShaderCodeLibrary.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 ShaderCodeLibrary.h:
5=============================================================================*/
6
7#pragma once
8
9#include "Containers/Array.h"
12#include "Containers/Map.h"
13#include "Containers/Set.h"
17#include "Delegates/Delegate.h"
18#include "HAL/Platform.h"
19#include "HAL/PlatformCrt.h"
20#include "Logging/LogMacros.h"
21#include "Misc/Crc.h"
22#include "Misc/SecureHash.h"
23#include "RHI.h"
24#include "RHIDefinitions.h"
27#include "UObject/NameTypes.h"
28
30
33class FShaderPipeline;
34
36class FIoBuffer;
37class FIoChunkId;
38class IPlugin;
39class UObject;
40
41#if WITH_EDITOR
42class FCbFieldView;
43class FCbWriter;
45#endif
46
48{
50 mutable uint32 Hash;
51
54
56
58 {
60 {
61 if (A.Shaders[Frequency] != B.Shaders[Frequency])
62 {
63 return false;
64 }
65 }
66 return true;
67 }
68
70 {
71 if(!Key.Hash)
72 {
74 {
75 Key.Hash = FCrc::MemCrc32(Key.Shaders[Frequency].Hash, sizeof(FSHAHash), Key.Hash);
76 }
77 }
78 return Key.Hash;
79 }
80
83
85 {
87 {
88 Ar << Info.Shaders[Frequency];
89 }
90 return Ar << Info.Hash;
91 }
92};
93
95{
97
99 {
100 return ObjectClassAndPath == Other.ObjectClassAndPath;
101 }
102
103 RENDERCORE_API FString ToString() const;
104 RENDERCORE_API FString ToStringPathOnly() const;
109
110#if WITH_EDITOR
113#endif
114};
115
116
118{
130
132
134
136 : KeyHash(0)
137 {
138 }
139
143 RENDERCORE_API FString ToString() const;
144 RENDERCORE_API void ToString(FString& OutResult) const;
146 static RENDERCORE_API FString HeaderLine();
147
150
152 {
153 return
154 A.ClassNameAndObjectPath == B.ClassNameAndObjectPath &&
155 A.ShaderType == B.ShaderType &&
156 A.ShaderClass == B.ShaderClass &&
157 A.MaterialDomain == B.MaterialDomain &&
158 A.FeatureLevel == B.FeatureLevel &&
159 A.QualityLevel == B.QualityLevel &&
160 A.TargetFrequency == B.TargetFrequency &&
161 A.TargetPlatform == B.TargetPlatform &&
162 A.VFType == B.VFType &&
163 A.PermutationId == B.PermutationId &&
164 A.PipelineHash == B.PipelineHash;
165 }
166
168 {
169 return Key.KeyHash;
170 }
171};
172
173struct FStableShaderKeyFuncs : BaseKeyFuncs<TUniquePtr<FStableShaderKeyAndValue>, FStableShaderKeyAndValue>
174{
176 {
177 return *Element;
178 }
179
181 {
182 return A == B;
183 }
184
187 {
188 return GetTypeHash(Key);
189 }
190};
191
193
194#if WITH_EDITOR
195RENDERCORE_API void WriteToCompactBinary(FCbWriter& Writer, const FStableShaderKeyAndValue& Key,
199#endif
200
201
204
205using FSharedShaderCodeRequest = TTSMulticastDelegate<void(const FSHAHash&, FArchive*)>; // thread-safe because it's "broadcasted" concurrently
207
208// Collection of unique shader code
209// Populated at cook time
211{
213 static RENDERCORE_API void PreInit();
214
216 static RENDERCORE_API void InitForRuntime(EShaderPlatform ShaderPlatform);
217 static RENDERCORE_API void Shutdown();
218
219 static RENDERCORE_API bool IsEnabled();
220
222
231 static RENDERCORE_API void AddKnownChunkIDs(const int32* IDs, const int32 NumChunkIDs);
232
242 static RENDERCORE_API bool OpenLibrary(FString const& Name, FString const& Directory, bool bMonolithicOnly = false);
243
249 static RENDERCORE_API void CloseLibrary(FString const& Name);
250
252 static RENDERCORE_API bool ContainsShaderCode(const FSHAHash& Hash, const FString& LogicalLibraryName);
253
255
256 static RENDERCORE_API bool PreloadShader(const FSHAHash& Hash, FArchive* Ar);
258
266
267 // Total number of shader entries in the library
269
270 // The shader platform that the library manages - at runtime this will only be one
272
273 // Safely assign the hash to a shader object
275
276 // Delegate called whenever shader code is requested.
277 static RENDERCORE_API FDelegateHandle RegisterSharedShaderCodeRequestDelegate_Handle(const FSharedShaderCodeRequest::FDelegate& Delegate);
279
280 // Disables opening the specified plugin's shader library on mount
281 static RENDERCORE_API void DontOpenPluginShaderLibraryOnMount(const FString& PluginName);
282
283 // Open the plugin's shader library
284 // @param bMonolithicOnly If true, only attempt to open a monolithic library (no chunks) - which is a default behavior for DLC plugins, see FShaderLibraryChunkDataGenerator.
286};
287
288#if WITH_EDITOR
289class ITargetPlatform;
290
292{
293 // Initialize the library cooker
295 // Shutdown the library cooker
296 static RENDERCORE_API void Shutdown();
297
298 // Clean the cook directories
300
302 {
303 FName ShaderFormat;
304 bool bNeedsStableKeys;
306 };
307
315 static RENDERCORE_API bool BeginCookingLibrary(FString const& Name);
316
321 static RENDERCORE_API void EndCookingLibrary(FString const& Name);
322
327
328 // Specify the shader formats to cook and which ones needs stable keys. Provide an array of FShaderFormatDescriptors
330
331 // At cook time, add shader code to collection
333
334#if WITH_EDITOR
337
340#endif
341
342 // We check this early in the callstack to avoid creating a bunch of FName and keys and things we will never save anyway.
343 // Pass the shader platform to check or EShaderPlatform::SP_NumPlatforms to check if any of the registered types require
344 // stable keys.
345 static RENDERCORE_API bool NeedsShaderStableKeys(EShaderPlatform ShaderPlatform);
346
347 // At cook time, add the human readable key value information
349
351 static RENDERCORE_API void FinishPopulateShaderLibrary(const ITargetPlatform* TargetPlatform, FString const& Name, FString const& SandboxDestinationPath,
352 FString const& SandboxMetadataPath);
353
364
378 static RENDERCORE_API bool SaveShaderLibraryWithoutChunking(const ITargetPlatform* TargetPlatform, FString const& Name, FString const& SandboxDestinationPath,
380
394 static RENDERCORE_API bool SaveShaderLibraryChunk(int32 ChunkId, const TSet<FName>& InPackagesInChunk, const ITargetPlatform* TargetPlatform,
396
397 // Dump collected stats for each shader platform
398 UE_DEPRECATED(5.6, "DumpShaderCodeStats is deprecated due to being incorrect w.r.t shader duplication due to chunking.")
400
401 static RENDERCORE_API void DumpShaderTypeStats(const FString& DebugInfoPath, const FString& MetaDataPath);
402
403 // Create a smaller 'patch' library that only contains data from 'NewMetaDataDir' not contained in any of 'OldMetaDataDirs'
404 static RENDERCORE_API bool CreatePatchLibrary(TArray<FString> const& OldMetaDataDirs, FString const& NewMetaDataDir, FString const& OutDir, bool bNativeFormat, bool bNeedsDeterministicOrder);
405};
406#endif
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
#define FORCEINLINE
Definition AndroidPlatform.h:140
bool LoadFromCompactBinary(FCbFieldView Field, FAssetDependency &Dependency)
Definition AssetRegistry.cpp:10420
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define DECLARE_DELEGATE_OneParam(DelegateName, Param1Type)
Definition DelegateCombinations.h:48
#define DECLARE_LOG_CATEGORY_EXTERN(CategoryName, DefaultVerbosity, CompileTimeVerbosity)
Definition LogMacros.h:361
EShaderFrequency
Definition RHIDefinitions.h:202
@ SF_NumGraphicsFrequencies
Definition RHIDefinitions.h:219
EShaderPlatform
Definition RHIShaderPlatform.h:11
RENDERCORE_API FSharedShaderMapResourceExplicitRelease OnSharedShaderMapResourceExplicitRelease
Definition ShaderCodeLibrary.cpp:301
TSet< TUniquePtr< FStableShaderKeyAndValue >, FStableShaderKeyFuncs > FStableShaderSet
Definition ShaderCodeLibrary.h:192
TSet< FName > FShaderMapAssetPaths
Definition Shader.h:88
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Archive.h:1208
Definition CompactBinary.h:610
Definition CompactBinaryWriter.h:68
Definition IDelegateInstance.h:14
Definition IoBuffer.h:15
Definition IoChunkId.h:64
Definition NameTypes.h:617
Definition RHIResources.h:854
Definition SecureHash.h:226
Definition Shader.h:521
Definition Shader.h:336
Definition Shader.h:2182
Definition IPluginManager.h:111
Definition Array.h:670
Definition UnrealString.h.inl:34
Definition DelegateSignatureImpl.inl:1029
Definition RefCounting.h:454
Definition UniquePtr.h:107
Definition Object.h:95
Definition FieldSystemNoiseAlgo.cpp:6
Definition SetUtilities.h:23
Definition ShaderCodeLibrary.h:95
RENDERCORE_API FString ToStringPathOnly() const
Definition ShaderCodeLibrary.cpp:449
RENDERCORE_API void ParseFromString(const FStringView &Src)
Definition ShaderCodeLibrary.cpp:532
bool operator==(const FCompactFullName &Other) const
Definition ShaderCodeLibrary.h:98
RENDERCORE_API void AppendString(FStringBuilderBase &Out) const
Definition ShaderCodeLibrary.cpp:470
RENDERCORE_API FString ToString() const
Definition ShaderCodeLibrary.cpp:442
TArray< FName, TInlineAllocator< 16 > > ObjectClassAndPath
Definition ShaderCodeLibrary.h:96
friend RENDERCORE_API uint32 GetTypeHash(const FCompactFullName &A)
Definition ShaderCodeLibrary.cpp:584
static UE_FORCEINLINE_HINT uint32 MemCrc32(const void *Data, int32 Length, uint32 CRC=0)
Definition Crc.h:31
Definition ShaderCodeLibrary.h:48
uint32 Hash
Definition ShaderCodeLibrary.h:50
FSHAHash Shaders[SF_NumGraphicsFrequencies]
Definition ShaderCodeLibrary.h:49
friend bool operator==(const FShaderCodeLibraryPipeline &A, const FShaderCodeLibraryPipeline &B)
Definition ShaderCodeLibrary.h:57
friend FArchive & operator<<(FArchive &Ar, FShaderCodeLibraryPipeline &Info)
Definition ShaderCodeLibrary.h:84
FShaderCodeLibraryPipeline()
Definition ShaderCodeLibrary.h:55
friend uint32 GetTypeHash(const FShaderCodeLibraryPipeline &Key)
Definition ShaderCodeLibrary.h:69
RENDERCORE_API void GetPipelineHash(FSHAHash &Output)
Definition ShaderCodeLibrary.cpp:944
Definition ShaderCodeLibrary.h:211
static RENDERCORE_API FDelegateHandle RegisterSharedShaderCodeRequestDelegate_Handle(const FSharedShaderCodeRequest::FDelegate &Delegate)
Definition ShaderCodeLibrary.cpp:4703
static RENDERCORE_API FRayTracingShaderRHIRef CreateRayTracingShader(EShaderPlatform Platform, const FSHAHash &Hash, EShaderFrequency Frequency)
Definition ShaderCodeLibrary.cpp:4282
static RENDERCORE_API FPixelShaderRHIRef CreatePixelShader(EShaderPlatform Platform, const FSHAHash &Hash)
Definition ShaderCodeLibrary.cpp:4237
static RENDERCORE_API bool ContainsShaderCode(const FSHAHash &Hash)
Definition ShaderCodeLibrary.cpp:4180
static RENDERCORE_API bool ReleasePreloadedShader(const FSHAHash &Hash)
Definition ShaderCodeLibrary.cpp:4219
static RENDERCORE_API FVertexShaderRHIRef CreateVertexShader(EShaderPlatform Platform, const FSHAHash &Hash)
Definition ShaderCodeLibrary.cpp:4228
static RENDERCORE_API FComputeShaderRHIRef CreateComputeShader(EShaderPlatform Platform, const FSHAHash &Hash)
Definition ShaderCodeLibrary.cpp:4255
static RENDERCORE_API FAmplificationShaderRHIRef CreateAmplificationShader(EShaderPlatform Platform, const FSHAHash &Hash)
Definition ShaderCodeLibrary.cpp:4273
static RENDERCORE_API EShaderPlatform GetRuntimeShaderPlatform(void)
Definition ShaderCodeLibrary.cpp:4302
static RENDERCORE_API void UnregisterSharedShaderCodeRequestDelegate_Handle(FDelegateHandle Handle)
Definition ShaderCodeLibrary.cpp:4708
static RENDERCORE_API bool OpenLibrary(FString const &Name, FString const &Directory, bool bMonolithicOnly=false)
Definition ShaderCodeLibrary.cpp:4339
static RENDERCORE_API void Shutdown()
Definition ShaderCodeLibrary.cpp:4140
static RENDERCORE_API bool AreShaderMapsPreloadedAtLoadTime()
Definition ShaderCodeLibrary.cpp:4175
static RENDERCORE_API FMeshShaderRHIRef CreateMeshShader(EShaderPlatform Platform, const FSHAHash &Hash)
Definition ShaderCodeLibrary.cpp:4264
static RENDERCORE_API void DontOpenPluginShaderLibraryOnMount(const FString &PluginName)
Definition ShaderCodeLibrary.cpp:4713
static RENDERCORE_API void OpenPluginShaderLibrary(IPlugin &Plugin, bool bMonolithicOnly=true)
Definition ShaderCodeLibrary.cpp:4722
static RENDERCORE_API void SafeAssignHash(FRHIShader *InShader, const FSHAHash &Hash)
Definition ShaderCodeLibrary.cpp:4695
static RENDERCORE_API void CloseLibrary(FString const &Name)
Definition ShaderCodeLibrary.cpp:4349
static RENDERCORE_API FGeometryShaderRHIRef CreateGeometryShader(EShaderPlatform Platform, const FSHAHash &Hash)
Definition ShaderCodeLibrary.cpp:4246
static RENDERCORE_API void AddKnownChunkIDs(const int32 *IDs, const int32 NumChunkIDs)
Definition ShaderCodeLibrary.cpp:4312
static RENDERCORE_API bool IsEnabled()
Definition ShaderCodeLibrary.cpp:4170
static RENDERCORE_API uint32 GetShaderCount(void)
Definition ShaderCodeLibrary.cpp:4292
static RENDERCORE_API TRefCountPtr< FShaderMapResource > LoadResource(const FSHAHash &Hash, FArchive *Ar)
Definition ShaderCodeLibrary.cpp:4198
static RENDERCORE_API void InitForRuntime(EShaderPlatform ShaderPlatform)
Definition ShaderCodeLibrary.cpp:4076
static RENDERCORE_API void PreInit()
Definition ShaderCodeLibrary.cpp:4070
Definition ShaderCodeLibrary.h:118
FName MaterialDomain
Definition ShaderCodeLibrary.h:122
FName QualityLevel
Definition ShaderCodeLibrary.h:124
friend uint32 GetTypeHash(const FStableShaderKeyAndValue &Key)
Definition ShaderCodeLibrary.h:167
FName ShaderClass
Definition ShaderCodeLibrary.h:121
FSHAHash PipelineHash
Definition ShaderCodeLibrary.h:129
FName TargetFrequency
Definition ShaderCodeLibrary.h:125
RENDERCORE_API void ParseFromString(const FStringView &Src)
Definition ShaderCodeLibrary.cpp:635
FName PermutationId
Definition ShaderCodeLibrary.h:128
FName ShaderType
Definition ShaderCodeLibrary.h:120
uint32 KeyHash
Definition ShaderCodeLibrary.h:131
FName VFType
Definition ShaderCodeLibrary.h:127
static RENDERCORE_API FString HeaderLine()
Definition ShaderCodeLibrary.cpp:802
FStableShaderKeyAndValue()
Definition ShaderCodeLibrary.h:135
FSHAHash OutputHash
Definition ShaderCodeLibrary.h:133
RENDERCORE_API void SetPipelineHash(const FShaderPipeline *Pipeline)
Definition ShaderCodeLibrary.cpp:840
FName FeatureLevel
Definition ShaderCodeLibrary.h:123
RENDERCORE_API void AppendString(FAnsiStringBuilderBase &Out) const
Definition ShaderCodeLibrary.cpp:768
friend bool operator==(const FStableShaderKeyAndValue &A, const FStableShaderKeyAndValue &B)
Definition ShaderCodeLibrary.h:151
FName TargetPlatform
Definition ShaderCodeLibrary.h:126
RENDERCORE_API void ParseFromStringCached(const FStringView &Src, class TMap< uint32, FName > &NameCache)
Definition ShaderCodeLibrary.cpp:683
FCompactFullName ClassNameAndObjectPath
Definition ShaderCodeLibrary.h:119
RENDERCORE_API FString ToString() const
Definition ShaderCodeLibrary.cpp:732
RENDERCORE_API void ComputeKeyHash()
Definition ShaderCodeLibrary.cpp:617
Definition ShaderCodeLibrary.h:174
static FORCEINLINE const FStableShaderKeyAndValue & GetSetKey(const TUniquePtr< FStableShaderKeyAndValue > &Element)
Definition ShaderCodeLibrary.h:175
static FORCEINLINE uint32 GetKeyHash(const FStableShaderKeyAndValue &Key)
Definition ShaderCodeLibrary.h:186
static FORCEINLINE bool Matches(const FStableShaderKeyAndValue &A, const FStableShaderKeyAndValue &B)
Definition ShaderCodeLibrary.h:180