UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
GPUSortManager.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 GPUSortManager.h: Interface manage sorting between different FXSystemInterface
5=============================================================================*/
6
7#pragma once
8
9#include "CoreMinimal.h"
11#include "RHI.h"
12#include "RenderResource.h"
13
34
35struct FGPUSortBuffers;
36
39{
40public:
41
43 {
44 BufferSize = InBufferSize;
45 }
46
48 virtual void InitRHI(FRHICommandListBase& RHICmdList) override;
49
51 virtual void ReleaseRHI() override;
52
55 {
56 check((BufferIndex & 0xFFFFFFFE) == 0);
57 return KeyBufferUAVs[BufferIndex];
58 }
59
62
65 {
66 check((BufferIndex & 0xFFFFFFFE) == 0);
67 return VertexBuffers[BufferIndex];
68 }
69
71 int32 GetSize() { return BufferSize; }
72
73private:
74
76 FBufferRHIRef KeyBuffers[2];
78 FShaderResourceViewRHIRef KeyBufferSRVs[2];
80 FUnorderedAccessViewRHIRef KeyBufferUAVs[2];
81
83 FBufferRHIRef VertexBuffers[2];
85 FShaderResourceViewRHIRef VertexBufferSortSRVs[2];
87 FUnorderedAccessViewRHIRef VertexBufferSortUAVs[2];
88
90 int32 BufferSize = 0;
91};
92
95{
96 None = 0x00,
104 LowPrecisionKeys = 0x01, // Low precision keys ((EleIdx << 16) | (16 bits key))
105 HighPrecisionKeys = 0x02, // High precision keys ((EleIdx << (32 - IdxBitCount)) | (32 bits key >> IdxBitCount))
106
107 AnyKeyPrecision = 0x03,
108
117 KeyGenAfterPreRender = 0x04, // Typically Cascade emitters simulated in PreRender() or any Niagara emitters.
118 KeyGenAfterPostRenderOpaque = 0x08, // Typically any Cascade emitters, or Niagara emitters simulated in PreInitViews().
119
120 AnyKeyGenLocation = 0x0C,
121
129 SortAfterPreRender = 0x10, // Any Emitter since good for both opaque or translucent
130 SortAfterPostRenderOpaque = 0x20, // Emitter using translucent material.
131
132 AnySortLocation = 0x30,
133
138 ValuesAsG16R16F = 0x40,
139 ValuesAsInt32 = 0x80,
140
141 AnyValueFormat = 0xC0,
142};
143
145
146
159
167{
168public:
169
172 {
176 uint32 BufferOffset = INDEX_NONE;
178 int32 SortBatchId = INDEX_NONE;
180 int32 ElementIndex = INDEX_NONE;
181 };
182
198
199private:
200
202 struct FCallbackInfo
203 {
205 FGPUSortKeyGenDelegate Delegate;
209 FName Name;
210 };
211
212 typedef TArray<FCallbackInfo> FCallbackArray;
213
215 struct FSettings
216 {
218 EGPUSortFlags AllowedFlags = EGPUSortFlags::None;
220 float BufferSlack = 2.f;
222 int32 FrameCountBeforeShrinking = 100;
224 int32 MinBufferSize = 8192;
225 };
226
228 class FValueBuffer : public FVertexBuffer
229 {
230 public:
231
239 FValueBuffer(FRHICommandListBase& RHICmdList, int32 InAllocatedCount, int32 InUsedCount, const FSettings& InSettings);
240
241 ~FValueBuffer() { ReleaseRHI(); }
242
244 void ReleaseRHI() override final;
245
255
257 int32 AllocatedCount = 0;
259 int32 UsedCount = 0;
260
268 inline void Allocate(FAllocationInfo& OutInfo, int32 ValueCount, EGPUSortFlags Flags);
269 };
270
271 typedef TIndirectArray<FValueBuffer, TInlineAllocator<4>> FValueBufferArray;
272
278 struct FDynamicValueBuffer
279 {
280 ~FDynamicValueBuffer() { ReleaseRHI(); }
281
283 FValueBufferArray ValueBuffers;
284
286 int32 NumFramesRequiringShrinking = 0;
287
289 EGPUSortFlags LastSortBatchFlags = EGPUSortFlags::None;
290
292 int32 CurrentSortBatchId = INDEX_NONE;
293
302 void Allocate(FRHICommandListBase& RHICmdList, FAllocationInfo& OutInfo, const FSettings& InSettings, int32 ValueCount, EGPUSortFlags Flags);
303
312 void SkrinkAndReset(FRHICommandListBase& RHICmdList, const FSettings& InSettings);
313
315 void ReleaseRHI();
317 inline int32 GetUsedCount() const { return ValueBuffers.Num() ? ValueBuffers.Last().UsedCount : 0; }
319 inline int32 GetAllocatedCount() const { return ValueBuffers.Num() ? ValueBuffers.Last().AllocatedCount : 0; }
320 };
321
322 typedef TIndirectArray<FDynamicValueBuffer, TInlineAllocator<4>> FDynamicValueBufferArray;
323
332 enum class ESortBatchProcessingOrder : uint32
333 {
334 Undefined = 0,
335 KeyGenAndSortAfterPreRender = 1,
336 KeyGenAfterPreRenderAndSortAfterPostRenderOpaque = 2,
337 KeyGenAndSortAfterPostRenderOpaque = 3,
338 };
339
345 struct FSortBatch
346 {
350 int32 NumElements = 0;
351
358
360 FDynamicValueBuffer* DynamicValueBuffer = nullptr;
361
363 FParticleSortBuffers* SortBuffers = nullptr;
364
366 ESortBatchProcessingOrder ProcessingOrder = ESortBatchProcessingOrder::Undefined;
367
369 void UpdateProcessingOrder();
370
378 void GenerateKeys(FRHICommandListImmediate& RHICmdList, const FCallbackArray& InCallbacks, EGPUSortFlags KeyGenLocation);
379
386 void SortAndResolve(FRHICommandListImmediate& RHICmdList, ERHIFeatureLevel::Type InFeatureLevel);
387
389 inline int32 GetUsedValueCount() const { return DynamicValueBuffer ? DynamicValueBuffer->GetUsedCount() : 0; }
391 inline int32 GetAllocatedValueCount() const { return DynamicValueBuffer ? DynamicValueBuffer->GetAllocatedCount() : 0; }
392 };
393
395 typedef TArray<FSortBatch, TInlineAllocator<4>> FSortBatchArray;
396
397public:
398
401
403
415
425 ENGINE_API bool AddTask(FRHICommandListBase& RHICmdList, FAllocationInfo& OutInfo, int32 ValueCount, EGPUSortFlags TaskFlags);
426
436 ENGINE_API void OnPreRender(class FRDGBuilder& GraphBuilder);
437
445 ENGINE_API void OnPostRenderOpaque(class FRDGBuilder& GraphBuilder);
446
452
458
459private:
460
462 static inline bool HasEither(EGPUSortFlags Flags, EGPUSortFlags A, EGPUSortFlags B);
464 static inline EGPUSortFlags GetBatchFlags(EGPUSortFlags TaskFlags);
466 static inline EGPUSortFlags CombineBatchFlags(EGPUSortFlags BatchFlags, EGPUSortFlags TaskFlags);
468 static inline bool TestBatchFlags(EGPUSortFlags BatchFlags, EGPUSortFlags TaskFlags);
470 static inline auto GetPrecisionString(EGPUSortFlags BatchFlags) -> TCHAR const(&)[1];
471
488 FDynamicValueBuffer* GetDynamicValueBufferFromPool(EGPUSortFlags TaskFlags, int32 SortBatchId);
489
491 void FinalizeSortBatches();
493 void UpdateSortBuffersPool(FRHICommandListBase& RHICmdList);
495 void ResetDynamicValuesBuffers(FRHICommandListBase& RHICmdList);
497 void ReleaseSortBuffers();
498
500 UE::FMutex AddTaskMutex;
501
507 FSortBatchArray SortBatches;
508
510 FSettings Settings;
511
513 FDynamicValueBufferArray DynamicValueBufferPool;
514
517
519 FCallbackArray Callbacks;
520
522 ERHIFeatureLevel::Type FeatureLevel;
523};
#define check(expr)
Definition AssertionMacros.h:314
@ INDEX_NONE
Definition CoreMiscDefines.h:150
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
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define DECLARE_DELEGATE_SixParams(DelegateName, Param1Type, Param2Type, Param3Type, Param4Type, Param5Type, Param6Type)
Definition DelegateCombinations.h:93
#define ENUM_CLASS_FLAGS(Enum)
Definition EnumClassFlags.h:6
ENGINE_API void CopyUIntBufferToTargets(FRHICommandList &RHICmdList, ERHIFeatureLevel::Type FeatureLevel, FRHIShaderResourceView *SourceSRV, FRHIUnorderedAccessView *const *TargetUAVs, int32 *TargetSizes, int32 StartingOffset, int32 NumTargets)
Definition GPUSortManager.cpp:205
EGPUSortFlags
Definition GPUSortManager.h:95
@ SortAfterPostRenderOpaque
@ KeyGenAfterPostRenderOpaque
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition GPUSortManager.h:167
FPostPreRenderEvent PostPreRenderEvent
Definition GPUSortManager.h:451
FPostPreRenderEvent PostPostRenderEvent
Definition GPUSortManager.h:457
DECLARE_EVENT_OneParam(FGPUSortManager, FPostPreRenderEvent, FRHICommandListImmediate &)
DECLARE_EVENT_OneParam(FGPUSortManager, FPostPostRenderEvent, FRHICommandListImmediate &)
Definition NameTypes.h:617
Definition GPUSortManager.h:39
FGPUSortBuffers GetSortBuffers()
Definition ParticleSortingGPU.cpp:251
virtual void InitRHI(FRHICommandListBase &RHICmdList) override
Definition ParticleSortingGPU.cpp:187
virtual void ReleaseRHI() override
Definition ParticleSortingGPU.cpp:234
void SetBufferSize(int32 InBufferSize)
Definition GPUSortManager.h:42
int32 GetSize()
Definition GPUSortManager.h:71
FRHIUnorderedAccessView * GetKeyBufferUAV(int32 BufferIndex)
Definition GPUSortManager.h:54
FRHIBuffer * GetSortedVertexBufferRHI(int32 BufferIndex)
Definition GPUSortManager.h:64
Definition RenderGraphBuilder.h:49
Definition RHIResources.h:1581
Definition RHICommandList.h:455
Definition RHICommandList.h:4626
Definition RHICommandList.h:3819
Definition RHIResources.h:3304
Definition RHIResources.h:3294
Definition RefCounting.h:252
Definition RenderResource.h:37
Definition RenderResource.h:474
virtual RENDERCORE_API void ReleaseRHI() override
Definition RenderResource.cpp:556
Definition Array.h:670
Definition IndirectArray.h:20
Definition ContainerAllocationPolicies.h:894
Definition Mutex.h:18
Type
Definition RHIFeatureLevel.h:20
Definition GPUSort.h:16
Definition GPUSortManager.h:172
FShaderResourceViewRHIRef BufferSRV
Definition GPUSortManager.h:174
Definition GPUSortManager.h:185
uint32 SortKeyMask
Definition GPUSortManager.h:194
FUintVector4 SortKeyParams
Definition GPUSortManager.h:196
uint32 ElementKeyMask
Definition GPUSortManager.h:192
uint32 ElementKeyShift
Definition GPUSortManager.h:193