UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
GlobalShader.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 GlobalShader.h: Shader manager definitions.
5=============================================================================*/
6
7#pragma once
8
9#include "Containers/Array.h"
10#include "Containers/Map.h"
12#include "CoreMinimal.h"
13#include "HAL/Platform.h"
15#include "Misc/Paths.h"
16#include "RHI.h"
17#include "RHIDefinitions.h"
18#include "RHIResources.h"
21#include "Shader.h"
23#include "Templates/Tuple.h"
24#include "Templates/UniquePtr.h"
25#include "UObject/NameTypes.h"
26#include "UObject/UnrealNames.h"
27
28class FArchive;
34class ITargetPlatform;
36#if WITH_EDITOR
38#endif
39
42
45{
46public:
47
50
52 RENDERCORE_API void AppendKeyString(FString& KeyString, const TArray<FShaderTypeDependency>& Dependencies) const;
53
54 const TMap<FString, TArray<FShaderTypeDependency>>& GetShaderFilenameToDependeciesMap() const { return ShaderFilenameToDependenciesMap; }
55
56 RENDERCORE_API bool WithEditorOnly() const;
57
58 inline EShaderPermutationFlags GetShaderPermutationFlags() const { return ::GetShaderPermutationFlags(LayoutParams); }
59
60private:
62 EShaderPlatform ShaderPlatform;
63 FName IniPlatformName;
64
66 TMap<FString, TArray<FShaderTypeDependency>> ShaderFilenameToDependenciesMap;
67
69 TArray<FShaderPipelineTypeDependency> ShaderPipelineTypeDependencies;
70};
71
81
121
123{
124 using Super = FShaderMapContent;
125 friend class FGlobalShaderMap;
128public:
129 const FHashedName& GetHashedSourceFilename() const { return HashedSourceFilename; }
130
131private:
133 : Super(InPlatform)
134 , HashedSourceFilename(InHashedSourceFilename)
135 {}
136
137 LAYOUT_FIELD(FHashedName, HashedSourceFilename);
138};
139
140class FGlobalShaderMapSection : public TShaderMap<FGlobalShaderMapContent, FShaderMapPointerTable>
141{
143 friend class FGlobalShaderMap;
144public:
146
147#if WITH_EDITOR
149#endif
150
154private:
156 {
157#if WITH_EDITOR
158 // associate with a non-empty asset name as to not have an exception in the library
159 FShaderMapBase::AssociateWithAsset(NAME_None);
160#endif
161 }
162
164 {
166 }
167
168 TShaderRef<FShader> GetShader(FShaderType* ShaderType, int32 PermutationId = 0) const;
169 FShaderPipelineRef GetShaderPipeline(const FShaderPipelineType* PipelineType) const;
170};
171
173{
174public:
177
178 RENDERCORE_API TShaderRef<FShader> GetShader(FShaderType* ShaderType, int32 PermutationId = 0) const;
180
181 template<typename ShaderType>
183 {
184 ensureMsgf(ShaderType::GetStaticType().GetPermutationCount() == 1, TEXT("Failed to provide PermutationId for shader type %s with %u permutations"), ShaderType::GetStaticType().GetName(), ShaderType::GetStaticType().GetPermutationCount());
185 return GetShader<ShaderType>(0);
186 }
187
188 template<typename ShaderType>
190 {
191 TShaderRef<FShader> Shader = GetShader(&ShaderType::GetStaticType(), PermutationId);
192 checkf(Shader.IsValid(), TEXT("Failed to find shader type %s in Platform %s"), ShaderType::GetStaticType().GetName(), *LegacyShaderPlatformToShaderFormat(Platform).ToString());
193
194 // Validate that the permutation has been precached
195 ensureMsgf(ShaderType::ShouldPrecachePermutation(FGlobalShaderPermutationParameters(ShaderType::GetStaticType().GetFName(), Platform, PermutationId)) != EShaderPermutationPrecacheRequest::NotUsed,
196 TEXT("Using a global shader permutation of %s which hasn't been requested for precaching at runtime. Check the implementation of ShouldPrecachePermutation on the global shader and make sure all required or development only permutations are requested for PSO precaching"), ShaderType::GetStaticType().GetName());
197
199 }
200
202 template<typename ShaderType>
203 TShaderRef<ShaderType> GetShader(const typename ShaderType::FPermutationDomain& PermutationVector) const
204 {
205 return GetShader<ShaderType>(PermutationVector.ToDimensionValueId());
206 }
207
208 bool HasShader(FShaderType* Type, int32 PermutationId) const
209 {
210 return GetShader(Type, PermutationId).IsValid();
211 }
212
217
218 RENDERCORE_API bool IsEmpty() const;
219
221 RENDERCORE_API bool IsComplete(const ITargetPlatform* TargetPlatform) const;
222
223 RENDERCORE_API void Empty();
225
226 RENDERCORE_API FShader* FindOrAddShader(const FShaderType* ShaderType, int32 PermutationId, FShader* Shader);
228
229 RENDERCORE_API void RemoveShaderTypePermutaion(const FShaderType* Type, int32 PermutationId);
231
235
238
240
241#if WITH_EDITOR
243 RENDERCORE_API void SaveShaderStableKeys(EShaderPlatform TargetShaderPlatform);
244#endif // WITH_EDITOR
245
247 {
248 if (const auto It = SectionMap.CreateConstIterator())
249 {
250 return It.Value();
251 }
252 return nullptr;
253 }
254
255private:
257 EShaderPlatform Platform;
258};
259
261
262
268class FGlobalShader : public FShader
269{
271public:
274
275 FGlobalShader() = default;
276
278
279 template<typename TViewUniformShaderParameters>
281 {
282 const auto& ViewUniformBufferParameter = static_cast<const FShaderUniformBufferParameter&>(GetUniformBufferParameter<TViewUniformShaderParameters>());
283 SetUniformBufferParameter(BatchedParameters, ViewUniformBufferParameter, ViewUniformBuffer);
284 }
285
288
290};
291
311
320
333
334
363/*#define DECLARE_GLOBAL_SHADER(ShaderClass) \
364 public: \
365 \
366 using ShaderMetaType = FGlobalShaderType; \
367 \
368 using ShaderMapType = FGlobalShaderMap; \
369 \
370 static ShaderMetaType StaticType; \
371 \
372 static FShader* ConstructSerializedInstance() { return new ShaderClass(); } \
373 static FShader* ConstructCompiledInstance(const ShaderMetaType::CompiledShaderInitializerType& Initializer) \
374 { return new ShaderClass(Initializer); } \
375 \
376 static void ModifyCompilationEnvironmentImpl( \
377 const FShaderPermutationParameters& Parameters, \
378 FShaderCompilerEnvironment& OutEnvironment) \
379 { \
380 FPermutationDomain PermutationVector(Parameters.PermutationId); \
381 PermutationVector.ModifyCompilationEnvironment(OutEnvironment); \
382 ShaderClass::ModifyCompilationEnvironment(Parameters, OutEnvironment); \
383 }
384
385#define IMPLEMENT_GLOBAL_SHADER(ShaderClass,SourceFilename,FunctionName,Frequency) \
386 ShaderClass::ShaderMetaType ShaderClass::StaticType( \
387 TEXT(#ShaderClass), \
388 TEXT(SourceFilename), \
389 TEXT(FunctionName), \
390 Frequency, \
391 ShaderClass::FPermutationDomain::PermutationCount, \
392 ShaderClass::ConstructSerializedInstance, \
393 ShaderClass::ConstructCompiledInstance, \
394 ShaderClass::ConstructEditorContent, \
395 ShaderClass::DestroyInstance, \
396 ShaderClass::DestroyEditorContent, \
397 ShaderClass::ModifyCompilationEnvironmentImpl, \
398 ShaderClass::ShouldCompilePermutation, \
399 ShaderClass::ValidateCompiledResult, \
400 ShaderClass::FreezeMemoryImageImpl, \
401 ShaderClass::FreezeMemoryImageEditorImpl, \
402 sizeof(ShaderClass), \
403 sizeof(ShaderClass::FEditorContent), \
404 ShaderClass::GetRootParametersMetadata() \
405 )
406 */
407
408#define DECLARE_GLOBAL_SHADER(ShaderClass) DECLARE_SHADER_TYPE(ShaderClass, Global)
409#define DECLARE_EXPORTED_GLOBAL_SHADER(ShaderClass, RequiredAPI) DECLARE_EXPORTED_SHADER_TYPE(ShaderClass, Global, RequiredAPI)
410#define IMPLEMENT_GLOBAL_SHADER(ShaderClass,SourceFilename,FunctionName,Frequency) IMPLEMENT_SHADER_TYPE(,ShaderClass,TEXT(SourceFilename),TEXT(FunctionName),Frequency)
411
417{
418 None = 0,
419
420 // Shaders used for resolving MSAA textures (defined in ResolveShader.h)
421 Resolve = 1 << 0,
422
423 // Shaders used for clearing textures and buffers (defined in ClearReplacementShaders.h)
424 Clear = 1 << 1,
425
426 // A null pixel shader (defined above)
427 Null = 1 << 2
428};
430
431/*
432* Set by the platform RHI implementations to indicate which groups of global shaders will be used recursively.
433*/
435
436/*
437* Called on the render thread when the global shader map is available to force-init certain global shaders as specified by GRequiredRecursiveShaders.
438*/
440
441void ForceInitGlobalShaderType(FShaderType& ShaderType);
442
443template <typename TShaderType>
445{
446 ForceInitGlobalShaderType(TShaderType::GetStaticType());
447}
#define ensureMsgf( InExpression, InFormat,...)
Definition AssertionMacros.h:465
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
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 ENUM_CLASS_FLAGS(Enum)
Definition EnumClassFlags.h:6
void ForceInitGlobalShaderType()
Definition GlobalShader.h:444
RENDERCORE_API const int32 GlobalShaderMapId
Definition GlobalShader.cpp:377
RENDERCORE_API FGlobalShaderMap * GetGlobalShaderMap(EShaderPlatform Platform)
Definition GlobalShader.cpp:539
RENDERCORE_API void CreateRecursiveShaders()
Definition GlobalShader.cpp:837
RENDERCORE_API FGlobalShaderMap * GGlobalShaderMap[SP_NumPlatforms]
Definition GlobalShader.cpp:20
RENDERCORE_API ERecursiveShader GRequiredRecursiveShaders
Definition GlobalShader.cpp:817
ERecursiveShader
Definition GlobalShader.h:417
#define LAYOUT_FIELD(T, Name,...)
Definition MemoryLayout.h:471
#define DECLARE_EXPORTED_TYPE_LAYOUT(T, RequiredAPI, Interface)
Definition MemoryLayout.h:559
#define DECLARE_TYPE_LAYOUT(T, Interface)
Definition MemoryLayout.h:557
#define GShaderPlatformForFeatureLevel
Definition RHIGlobals.h:915
EShaderPlatform
Definition RHIShaderPlatform.h:11
@ SP_NumPlatforms
Definition RHIShaderPlatform.h:55
FName LegacyShaderPlatformToShaderFormat(EShaderPlatform Platform)
Definition RHIStrings.cpp:237
void SetUniformBufferParameter(FRHIBatchedShaderParameters &BatchedParameters, const FShaderUniformBufferParameter &Parameter, FRHIUniformBuffer *UniformBufferRHI)
Definition ShaderParameterUtils.h:207
EShaderPermutationFlags
Definition ShaderPermutation.h:19
EShaderPermutationPrecacheRequest
Definition Shader.h:807
#define DECLARE_EXPORTED_SHADER_TYPE(ShaderClass, ShaderMetaTypeShortcut, RequiredAPI,...)
Definition Shader.h:1683
Definition Archive.h:1208
Definition GlobalShader.h:123
const FHashedName & GetHashedSourceFilename() const
Definition GlobalShader.h:129
Definition GlobalShader.h:45
const TMap< FString, TArray< FShaderTypeDependency > > & GetShaderFilenameToDependeciesMap() const
Definition GlobalShader.h:54
RENDERCORE_API void AppendKeyString(FString &KeyString, const TArray< FShaderTypeDependency > &Dependencies) const
Definition GlobalShader.cpp:470
EShaderPermutationFlags GetShaderPermutationFlags() const
Definition GlobalShader.h:58
RENDERCORE_API bool WithEditorOnly() const
Definition GlobalShader.cpp:503
Definition GlobalShader.h:141
virtual RENDERCORE_API void GetShaderList(TMap< FHashedName, TShaderRef< FShader > > &OutShaders) const override
Definition GlobalShader.cpp:588
virtual RENDERCORE_API void GetShaderPipelineList(TArray< FShaderPipelineRef > &OutShaderPipelines) const override
Definition GlobalShader.cpp:593
static RENDERCORE_API FGlobalShaderMapSection * CreateFromArchive(FArchive &Ar)
Definition GlobalShader.cpp:546
Definition GlobalShader.h:173
RENDERCORE_API ~FGlobalShaderMap()
Definition GlobalShader.cpp:603
RENDERCORE_API bool IsComplete(const ITargetPlatform *TargetPlatform) const
Definition GlobalShader.cpp:660
RENDERCORE_API FShaderPipeline * FindOrAddShaderPipeline(const FShaderPipelineType *ShaderPipelineType, FShaderPipeline *ShaderPipeline)
Definition GlobalShader.cpp:731
RENDERCORE_API FGlobalShaderMapSection * FindSection(const FHashedName &HashedShaderFilename)
Definition GlobalShader.cpp:768
bool HasShader(FShaderType *Type, int32 PermutationId) const
Definition GlobalShader.h:208
TShaderRef< ShaderType > GetShader(int32 PermutationId) const
Definition GlobalShader.h:189
TShaderRef< ShaderType > GetShader() const
Definition GlobalShader.h:182
RENDERCORE_API void SaveToGlobalArchive(FArchive &Ar)
Definition GlobalShader.cpp:786
RENDERCORE_API void RemoveShaderPipelineType(const FShaderPipelineType *ShaderPipelineType)
Definition GlobalShader.cpp:750
RENDERCORE_API void LoadFromGlobalArchive(FArchive &Ar)
Definition GlobalShader.cpp:798
RENDERCORE_API void Empty()
Definition GlobalShader.cpp:703
const FGlobalShaderMapSection * GetFirstSection()
Definition GlobalShader.h:246
RENDERCORE_API void BeginCreateAllShaders()
Definition GlobalShader.cpp:620
RENDERCORE_API FGlobalShaderMapSection * FindOrAddSection(const FShaderType *ShaderType)
Definition GlobalShader.cpp:774
RENDERCORE_API bool IsEmpty() const
Definition GlobalShader.cpp:648
RENDERCORE_API void ReleaseAllSections()
Definition GlobalShader.cpp:711
RENDERCORE_API FShaderPipelineRef GetShaderPipeline(const FShaderPipelineType *PipelineType) const
Definition GlobalShader.cpp:614
TShaderRef< ShaderType > GetShader(const typename ShaderType::FPermutationDomain &PermutationVector) const
Definition GlobalShader.h:203
RENDERCORE_API void RemoveShaderTypePermutaion(const FShaderType *Type, int32 PermutationId)
Definition GlobalShader.cpp:741
bool HasShaderPipeline(const FShaderPipelineType *ShaderPipelineType) const
Definition GlobalShader.h:213
RENDERCORE_API FShader * FindOrAddShader(const FShaderType *ShaderType, int32 PermutationId, FShader *Shader)
Definition GlobalShader.cpp:720
RENDERCORE_API void AddSection(FGlobalShaderMapSection *InSection)
Definition GlobalShader.cpp:759
Definition GlobalShader.h:87
friend class FGlobalShaderTypeCompiler
Definition GlobalShader.h:88
FShader::CompiledShaderInitializerType CompiledShaderInitializerType
Definition GlobalShader.h:91
EShaderPermutationPrecacheRequest ShouldPrecachePermutation(EShaderPlatform Platform, int32 PermutationId, EShaderPermutationFlags Flags) const
Definition GlobalShader.h:105
bool ShouldCompilePermutation(EShaderPlatform Platform, int32 PermutationId, EShaderPermutationFlags Flags) const
Definition GlobalShader.h:100
static RENDERCORE_API bool ShouldCompilePipeline(const FShaderPipelineType *ShaderPipelineType, EShaderPlatform Platform, EShaderPermutationFlags Flags)
Definition GlobalShader.cpp:508
IMPLEMENT_SHADER_TYPE_CONSTRUCTOR(FGlobalShaderType, EShaderTypeForDynamicCast::Global, TEXT("global shader"))
Definition GlobalShader.h:269
static void ModifyCompilationEnvironment(const FGlobalShaderPermutationParameters &Parameters, FShaderCompilerEnvironment &Environment)
Definition GlobalShader.h:289
void SetParameters(FRHIBatchedShaderParameters &BatchedParameters, FRHIUniformBuffer *ViewUniformBuffer)
Definition GlobalShader.h:280
FGlobalShader()=default
Definition MemoryImage.h:858
Definition GlobalShader.h:296
FNULLPS(const ShaderMetaType::CompiledShaderInitializerType &Initializer)
Definition GlobalShader.h:306
static bool ShouldCompilePermutation(const FGlobalShaderPermutationParameters &Parameters)
Definition GlobalShader.h:300
FNULLPS()
Definition GlobalShader.h:305
Definition NameTypes.h:617
Definition RHIResources.h:1232
Definition ShaderCompilerJobTypes.h:152
Definition ShaderCompilerJobTypes.h:331
RENDERCORE_API void AssignContent(TMemoryImageObject< FShaderMapContent > InContent)
Definition ShaderMap.cpp:184
Definition Shader.h:2312
Definition ShaderParameterMetadata.h:136
Definition Shader.h:2273
bool IsValid() const
Definition Shader.h:2278
Definition Shader.h:1931
Definition Shader.h:2182
Definition Shader.h:1238
const FName & GetFName() const
Definition Shader.h:1468
RENDERCORE_API bool ShouldCompilePermutation(const FShaderPermutationParameters &Parameters) const
Definition Shader.cpp:462
RENDERCORE_API EShaderPermutationPrecacheRequest ShouldPrecachePermutation(const FShaderPermutationParameters &Parameters) const
Definition Shader.cpp:467
Definition ShaderParameters.h:109
Definition Shader.h:829
static EShaderPermutationPrecacheRequest ShouldPrecachePermutation(const FShaderPermutationParameters &Parameters)
Definition Shader.h:863
static bool ShouldCompilePermutation(const FShaderPermutationParameters &)
Definition Shader.h:860
Definition VertexFactory.h:314
Definition Array.h:670
Definition UnrealString.h.inl:34
Definition Shader.h:2580
Definition Shader.h:1021
static TShaderRefBase< ShaderType, PointerTableType > Cast(const TShaderRefBase< OtherShaderType, OtherPointerTableType > &Rhs)
Definition Shader.h:1041
Type
Definition RHIFeatureLevel.h:20
Definition GlobalShader.h:73
const FName GlobalShaderName
Definition GlobalShader.h:74
FGlobalShaderPermutationParameters(FName InGlobalShaderName, EShaderPlatform InShaderPlatform, int32 InPermutationId=0, EShaderPermutationFlags InFlags=EShaderPermutationFlags::HasEditorOnlyData)
Definition GlobalShader.h:76
Definition MemoryLayout.h:799
Definition RHIShaderParameters.h:241
Definition ShaderSerialization.h:174
Definition Shader.h:1610
Definition ShaderCore.h:544
Definition ShaderPermutation.h:27
Definition ShaderSerialization.h:26