UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
PipelineStateCache.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 PipelineStateCache.h: Pipeline state cache definition.
5=============================================================================*/
6
7#pragma once
8
9#include "CoreMinimal.h"
10#include "RHI.h"
11#include "Misc/Timeout.h"
12#include "Misc/EnumClassFlags.h"
13
18
19// Utility flags for modifying render target behavior on a PSO
21{
22 DoNothing = 0, // Just use the PSO from initializer's values, no checking and no modifying (used for PSO precompilation only)
23 CheckApply = 1 << 0, // Verify that the PSO's RT formats match the last Render Target formats set into the CmdList
24 ForceApply = CheckApply,// Deprecated. Do not use
25};
26
28
33{
34 Medium,
35 High,
36 Highest,
37};
38
40{
41 // Query the pipeline cache, create pipeline if necessary.
42 // Compilation may happen on a task, but RHIThread will block on it before translating the RHICmdList.
43 // Therefore the RHIThread may stall when creating large / complex pipelines.
44 Default = 0,
45
46 // Query the pipeline cache, create a background task to create the pipeline if necessary.
47 // GetAndOrCreateRayTracingPipelineState() may return NULL if pipeline is not ready.
48 // Caller must use an alternative fallback PSO to render current frame and may retry next frame.
49 // Pipeline creation task will not block RenderThread or RHIThread, allowing hitch-free rendering.
50 NonBlocking = 1 << 0,
51};
53
55{
56 Unknown, //< No known pre cache state
57 Active, //< PSO is currently precaching
58 Complete, //< PSO has been precached successfully
59 Missed, //< PSO precache miss, needs to be compiled at draw time
60 TooLate, //< PSO precache request still compiling when needed
61 NotSupported, //< PSO precache not supported (VertexFactory or MeshPassProcessor doesn't support/implement precaching)
62 Untracked, //< PSO is not tracked at all (Global shader or not coming from MeshDrawCommands)
63};
64
65extern RHI_API const TCHAR* LexToString(EPSOPrecacheResult Result);
66
67// Unique request ID of PSOPrecache which can be used to boost the priority of a PSO precache requests if it's needed for rendering
69{
70 enum class EType
71 {
72 Invalid = 0,
75 };
76
78
79 EType GetType() const { return (EType)Type; }
80 bool IsValid() const
81 {
82 return Type != (uint32)EType::Invalid;
83 }
85 {
86 return Type == Other.Type && RequestID == Other.RequestID;
87 }
89 {
90 return !(*this == rhs);
91 }
92
93 uint32 Type : 2; //< PSO request type
94 uint32 RequestID : 30; //< Unique request ID
95};
96
97// Result data of a precache pipeline state request
99{
100 bool IsValid() const { return RequestID.IsValid(); }
102 {
103 return RequestID == Other.RequestID && AsyncCompileEvent == Other.AsyncCompileEvent;
104 }
106 {
107 return !(*this == rhs);
108 }
109
112};
113
114// Statistics about PSOs created at runtime, including stats for PSOs which took over a pre-defined threshold to be created.
115// These only track PSOs requested by the renderer on the critical path (not from background precaching).
116// Note that while slow PSO creation times usually result in a hitch at runtime, it's not a 1-to-1 correlation.
117// There are cases where multiple PSOs are slow in a single frame and therefore cause a single hitch, and cases
118// where a PSO might not be slow enough to cause a visible hitch for a certain frame (depending on the threshold).
120{
121 // The total number of PSOs created after a request by the renderer. Does not include PSOs created from background precaching.
123
124 // The number of compute and graphics PSOs that took too long to create.
127
128 // How many of the PSOs that took too long to create were previously precached in the background.
130
131 // How many of the PSOs that too long to create were previously precached in the background and are suspected to have hit a full compilation again.
132 // These use a higher threshold and usually indicate the driver cache is being missed or is not working.
134
135 // Whether the driver cache is suspected to be unhealthy due to the number of very long creation times for precached PSO (controlled by a configurable threshold).
137};
138
142
143namespace PipelineStateCache
144{
146
148
150
152
154
156
158
160
162
163 // Retrieves RTPSO object from cache or adds a task to create it, which will be waited on by RHI thread.
164 // May return NULL in non-blocking mode if pipeline is not already in cache.
166 FRHICommandList& RHICmdList,
169
170 // Retrieves RTPSO object from cache or returns NULL if it's not found.
172
173 /* Evicts unused state entries based on r.pso.evictiontime time. Called in RHICommandList::BeginFrame */
174 extern RHI_API void FlushResources();
175
176 extern RHI_API void ReportFrameHitchToCSV();
177
178 // Waits for any pending tasks to complete.
179 extern RHI_API void WaitForAllTasks();
180
181 // Initializes any required component.
182 extern RHI_API void Init();
183
184 /* Clears all pipeline cached state. Called on shutdown, calling GetAndOrCreate after this will recreate state */
185 extern RHI_API void Shutdown();
186
187 /* Called when PSO precompile has completed */
188 extern RHI_API void PreCompileComplete();
189
190 /* Returns the number of PSO precompiles currently in progress */
192
193 /* Is precaching currently enabled - can help to skip certain time critical code when precaching is disabled */
194 extern RHI_API bool IsPSOPrecachingEnabled();
195
196 /* Precache the compute shader and return a request ID if precached async */
198
199 /* Precache the graphic PSO and return an optional graph event if precached async */
201
202 /* Retrieve the current PSO precache result state (slightly slower than IsPrecaching) */
204
205 /* Retrieve the current PSO precache result state (slightly slower than IsPrecaching) */
207
208 /* Is the given PSO still precaching? */
210
211 /* Is the given PSO initializer still precaching? */
212 extern RHI_API bool IsPrecaching(const FGraphicsPipelineStateInitializer& PipelineStateInitializer);
213
214 /* Is the given PSO initializer still precaching? */
216
217 /* Any async precaching operations still busy */
218 extern RHI_API bool IsPrecaching();
219
220 /* Boost the priority of the given PSO request ID */
222
223 /* Return number of active or pending PSO precache requests */
225
226 /* Set all subsequent high priority requests to highest priority, useful in non-interactive scenarios where maximum PSO throughput is preferable. */
228
229 /* Return stats on PSOs created at runtime, including slow PSO creation stats. */
231
232 /* Reset the PSO hitch tracking counters */
234}
235
236// Returns the shader index within the ray tracing pipeline or INDEX_NONE if given shader does not exist.
237// Asserts if shader is not found but bRequired is true.
241
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
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
RHI_API int32 FindRayTracingMissShaderIndex(FRayTracingPipelineState *Pipeline, FRHIRayTracingShader *MissShader, bool bRequired=true)
Definition PipelineStateCache.cpp:1413
ERayTracingPipelineCacheFlags
Definition PipelineStateCache.h:40
RHI_API int32 FindRayTracingHitGroupIndex(FRayTracingPipelineState *Pipeline, FRHIRayTracingShader *HitGroupShader, bool bRequired=true)
Definition PipelineStateCache.cpp:1387
RHI_API void SetGraphicsPipelineStateCheckApply(FRHICommandList &RHICmdList, const FGraphicsPipelineStateInitializer &Initializer, uint32 StencilRef, bool bApplyAdditionalState=true)
Definition PipelineStateCache.cpp:1492
RHI_API const TCHAR * LexToString(EPSOPrecacheResult Result)
Definition PipelineStateCache.cpp:414
EPSOPrecachePriority
Definition PipelineStateCache.h:33
EApplyRendertargetOption
Definition PipelineStateCache.h:21
RHI_API void SetGraphicsPipelineState(FRHICommandList &RHICmdList, const FGraphicsPipelineStateInitializer &Initializer, uint32 StencilRef, EApplyRendertargetOption ApplyFlags=EApplyRendertargetOption::CheckApply, bool bApplyAdditionalState=true)
Definition PipelineStateCache.cpp:1478
RHI_API int32 FindRayTracingCallableShaderIndex(FRayTracingPipelineState *Pipeline, FRHIRayTracingShader *CallableShader, bool bRequired=true)
Definition PipelineStateCache.cpp:1400
RHI_API void SetComputePipelineState(FRHIComputeCommandList &RHICmdList, FRHIComputeShader *ComputeShader)
Definition PipelineStateCache.cpp:1447
EPSOPrecacheResult
Definition PipelineStateCache.h:55
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition PipelineStateCache.cpp:1124
Definition RHIResources.h:4572
Definition PipelineStateCache.cpp:1230
Definition RHICommandList.h:3819
Definition RHICommandList.h:2735
Definition RHIResources.h:1018
Definition RHIResources.h:984
Definition RHIResources.h:725
Definition RHIResources.h:5023
Definition RHIResources.h:4982
Definition PipelineStateCache.cpp:1285
Definition RHIResources.h:4915
Definition PipelineStateCache.cpp:1174
Definition Array.h:670
Definition Timeout.h:21
static FTimeout Never()
Definition Timeout.h:49
Definition PipelineStateCache.cpp:26
RHI_API bool IsPrecaching()
Definition PipelineStateCache.cpp:4552
RHI_API EPSOPrecacheResult CheckPipelineStateInCache(const FGraphicsPipelineStateInitializer &PipelineStateInitializer)
Definition PipelineStateCache.cpp:4493
RHI_API void ReportFrameHitchToCSV()
Definition PipelineStateCache.cpp:3526
RHI_API void FlushResources()
Definition PipelineStateCache.cpp:3534
RHI_API FComputePipelineState * FindComputePipelineState(FRHIComputeShader *ComputeShader, bool bVerifyUse=true)
Definition PipelineStateCache.cpp:4337
RHI_API FGraphicsPipelineState * FindGraphicsPipelineState(const FGraphicsPipelineStateInitializer &Initializer, bool bVerifyUse=true)
Definition PipelineStateCache.cpp:4383
RHI_API void Shutdown()
Definition PipelineStateCache.cpp:4742
RHI_API void PrecachePSOsBoostToHighestPriority(bool bForceHighest)
Definition PipelineStateCache.cpp:4580
RHI_API FComputePipelineState * GetAndOrCreateComputePipelineState(FRHIComputeCommandList &RHICmdList, FRHIComputeShader *ComputeShader, bool bFromFileCache)
Definition PipelineStateCache.cpp:3704
RHI_API void WaitForAllTasks()
Definition PipelineStateCache.cpp:4720
RHI_API FRayTracingPipelineState * GetAndOrCreateRayTracingPipelineState(FRHICommandList &RHICmdList, const FRayTracingPipelineStateInitializer &Initializer, ERayTracingPipelineCacheFlags Flags=ERayTracingPipelineCacheFlags::Default)
Definition PipelineStateCache.cpp:4012
RHI_API FPSOPrecacheRequestResult PrecacheComputePipelineState(FRHIComputeShader *ComputeShader, const TCHAR *Name=nullptr, bool bForcePrecache=false)
Definition PipelineStateCache.cpp:4448
RHI_API bool IsPSOPrecachingEnabled()
Definition PipelineStateCache.cpp:4406
RHI_API void ResetPSOHitchTrackingStats()
Definition PipelineStateCache.cpp:4612
RHI_API void BoostPrecachePriority(EPSOPrecachePriority PSOPrecachePriority, const FPSOPrecacheRequestID &PSOPrecacheRequestID)
Definition PipelineStateCache.cpp:4562
RHI_API uint32 NumActivePrecacheRequests()
Definition PipelineStateCache.cpp:4590
RHI_API void Init()
Definition PipelineStateCache.cpp:4736
RHI_API int32 GetNumActivePipelinePrecompileTasks()
Definition PipelineStateCache.cpp:3133
RHI_API FPSOPrecacheRequestResult PrecacheGraphicsPipelineState(const FGraphicsPipelineStateInitializer &PipelineStateInitializer)
Definition PipelineStateCache.cpp:4488
RHI_API void GetPipelineStates(TArray< TRefCountPtr< FRHIResource > > &Out, bool bConsolidateCaches=true, UE::FTimeout ConsolidationTimeout=UE::FTimeout::Never())
Definition PipelineStateCache.cpp:4787
RHI_API uint64 RetrieveGraphicsPipelineStateSortKey(const FGraphicsPipelineState *GraphicsPipelineState)
Definition PipelineStateCache.cpp:3633
RHI_API FPSORuntimeCreationStats GetPSORuntimeCreationStats()
Definition PipelineStateCache.cpp:4600
RHI_API FRHIVertexDeclaration * GetOrCreateVertexDeclaration(const FVertexDeclarationElementList &Elements)
Definition PipelineStateCache.cpp:4768
RHI_API FWorkGraphPipelineState * GetAndOrCreateWorkGraphPipelineState(FRHIComputeCommandList &RHICmdList, const FWorkGraphPipelineStateInitializer &Initializer)
Definition PipelineStateCache.cpp:3823
RHI_API FWorkGraphPipelineState * FindWorkGraphPipelineState(const FWorkGraphPipelineStateInitializer &Initializer, bool bVerifyUse=true)
Definition PipelineStateCache.cpp:4360
RHI_API void PreCompileComplete()
Definition PipelineStateCache.cpp:494
RHI_API FRayTracingPipelineState * GetRayTracingPipelineState(const FRayTracingPipelineStateSignature &Signature)
Definition PipelineStateCache.cpp:4120
RHI_API FGraphicsPipelineState * GetAndOrCreateGraphicsPipelineState(FRHICommandList &RHICmdList, const FGraphicsPipelineStateInitializer &OriginalInitializer, EApplyRendertargetOption ApplyFlags)
Definition PipelineStateCache.cpp:4193
Definition PipelineStateCache.h:69
bool operator!=(const FPSOPrecacheRequestID &rhs) const
Definition PipelineStateCache.h:88
uint32 Type
Definition PipelineStateCache.h:93
bool operator==(const FPSOPrecacheRequestID &Other) const
Definition PipelineStateCache.h:84
bool IsValid() const
Definition PipelineStateCache.h:80
uint32 RequestID
Definition PipelineStateCache.h:94
EType GetType() const
Definition PipelineStateCache.h:79
EType
Definition PipelineStateCache.h:71
FPSOPrecacheRequestID()
Definition PipelineStateCache.h:77
Definition PipelineStateCache.h:99
bool operator!=(const FPSOPrecacheRequestResult &rhs) const
Definition PipelineStateCache.h:105
bool operator==(const FPSOPrecacheRequestResult &Other) const
Definition PipelineStateCache.h:101
FPSOPrecacheRequestID RequestID
Definition PipelineStateCache.h:110
bool IsValid() const
Definition PipelineStateCache.h:100
FGraphEventRef AsyncCompileEvent
Definition PipelineStateCache.h:111
Definition PipelineStateCache.h:120
uint32 GraphicsPSOHitches
Definition PipelineStateCache.h:126
uint32 SuspectedUnhealthyDriverCachePSOHitches
Definition PipelineStateCache.h:133
bool bDriverCacheSuspectedUnhealthy
Definition PipelineStateCache.h:136
uint32 TotalPSOCreations
Definition PipelineStateCache.h:122
uint32 PreviouslyPrecachedPSOHitches
Definition PipelineStateCache.h:129
uint32 ComputePSOHitches
Definition PipelineStateCache.h:125