UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RayTracingShaderBindingTable.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "RHIDefinitions.h"
7#include "SpanAllocator.h"
9#include "RenderGraph.h"
10
11#if RHI_RAYTRACING
12
14{
15 Base = 0,
16 Decals,
17
18 NUM
19};
20
22{
23 None = 0,
24 Base = 1 << (uint32)ERayTracingShaderBindingLayer::Base,
25 Decals = 1 << (uint32)ERayTracingShaderBindingLayer::Decals,
26 All = Base | Decals,
27};
28
30{
31public:
32
33 FRayTracingSBTAllocation() = default;
34
35 bool IsValid() const
36 {
37 return NumRecords > 0;
38 }
39
44 {
45 // InstanceContributionToHitGroupIndex is stored at the first segment index because all other segments are directly allocated after this one
46 return GetRecordIndex(Layer, 0);
47 }
48
53 RENDERER_API int32 GetSegmentCount() const;
54 RENDERER_API int32 GetUniqueId() const;
56
57private:
58
60 {
61 check(InAllocatedLayers != ERayTracingShaderBindingLayerMask::None);
65 NumRecords = InNumRecords;
67 Flags = InFlags;
68 }
69
71 {
72 check(InAllocatedLayers != ERayTracingShaderBindingLayerMask::None);
76 NumRecords = InNumRecords;
77 }
78
80
83 uint32 NumRecords = 0;
85
86 // Store the original geometry and flags in the allocation object so it can be used to build the lookup key again used for deduplication
87 const FRHIRayTracingGeometry* Geometry = nullptr;
89};
90
92
97{
98public:
99
102
107
112
117 {
118 // Just release the RHI object so the slot can be reused for the next persistent SBT
120 }
121
124
125 uint32 GetNumShaderSlotsPerSegment() const { return NumShaderSlotsPerGeometrySegment; }
126
131
136 FRHICommandListBase& RHICmdList,
137 ERayTracingShaderBindingMode ShaderBindingMode,
138 ERayTracingHitGroupIndexingMode HitGroupIndexingMode,
139 uint32 LocalBindingDataSize) const;
140
145
150 {
153 return AllocateStaticRangeInternal(AllocatedLayers, SegmentCount, nullptr, DefaultFlags);
154 }
155
161
166
171
176
180 void EndFrame();
181
186 {
188 }
189
194
198 bool IsDirty(uint32 RecordIndex) const;
199
203 bool IsPersistent() const;
204
205public:
206
207 uint32 NumMissShaderSlots = 1; // we must have a default miss shader, so always include it from the start
208 uint32 NumCallableShaderSlots = 0;
210
211 // Helper array to hold references to single frame uniform buffers used in SBTs
213
214private:
215
220
225
230
234 void MarkDirty(FRayTracingSBTAllocation* SBTAllocation);
235 void MarkSet(FRayTracingSBTAllocation* SBTAllocation, bool bValue);
236
237 struct FAllocationKey
238 {
241
242 bool operator==(const FAllocationKey& Other) const
243 {
244 return Geometry == Other.Geometry &&
245 Flags == Other.Flags;
246 }
247
248 bool operator!=(const FAllocationKey& Other) const
249 {
250 return !(*this == Other);
251 }
252
253 friend uint32 GetTypeHash(const FAllocationKey& Key)
254 {
255 return HashCombine(GetTypeHash(Key.Geometry), GetTypeHash(Key.Flags));
256 }
257 };
258
260 {
261 FRayTracingSBTAllocation* Allocation;
262 uint32 RefCount = 0;
263 };
264
265 struct FPersistentSBTData
266 {
267 void Reset()
268 {
269 ShaderBindingMode = ERayTracingShaderBindingMode::Disabled;
270 ShaderBindingTable = nullptr;
272 }
273
275 FShaderBindingTableRHIRef ShaderBindingTable; //< Actual persistent RHI shader binding table
276 TRefCountPtr<FRDGPooledBuffer> InlineBindingDataPooledBuffer; //< Optional inline binding data buffer - size is retrieved from the RHI SBT after creation
277 };
278
279 uint32 NumShaderSlotsPerGeometrySegment = 0; //< Number of slots per geometry segment (engine wide fixed)
280
281 FRayTracingShaderBindingTableInitializer PersistentSBTInitializer; //< Shared initializer used for all persistent SBTs - so they can all be versioned together
282 TArray<FPersistentSBTData> PersistentSBTs; //< All currently allocated persistent SBTs (FRayTracingPersistentShaderBindingTableID contains index into this array so can be sparse)
283 TBitArray<> SetPersistentRecords; //< BitArray containing which bits are valid in the cached persistent SBTs (use for validation)
284 TBitArray<> DirtyPersistentRecords; //< BitArray containing which bits are dirty in the cached persistent SBTs (used during dirty binding collection from currently visible bindings)
285 TBitArray<> PersistentRecordsToClear; //< BitArray containing which bits need to be cleared in the persisten SBTs
286
287 TArray<FRayTracingSBTAllocation*> PendingStaticAllocationsToFree; //< All pending allocations to free during next flush
288
289 FCriticalSection StaticAllocationCS; //< Critical section used to access all static allocation data
290 bool bStaticAllocationsLocked = false; //< Static allocations are not allowed when this bool is set (used for validation)
291 FSpanAllocator StaticRangeAllocator; //< Range allocator to find free static record ranges
292 TMap<FAllocationKey, FRefCountedAllocation> TrackedAllocationMap; //< All static allocation with refcount tracking
293
294 TArray<FRayTracingSBTAllocation*> ActiveDynamicAllocations; //< All current active dynamic allocations
295 TArray<FRayTracingSBTAllocation*> FreeDynamicAllocationPool; //< Free dynamic allocation pool (for faster allocations)
296
297 uint32 TotalStaticAllocationCount = 0; //< Total amount of static allocations (without deduplications)
298 uint32 AllocatedStaticSegmentCount = 0; //< Total amount of allocated static segments (with deduplication)
299
300 uint32 MaxNumDynamicGeometrySegments = 0; //< Maximum number of allocated dynamic segments required (peek number)
301 uint32 NumDynamicGeometrySegments = 0; //< Current number of allocated dynamic segments
302 uint32 StartDynamicRangeOffset = 0; //< Start SBT record offset for the first dynamic allocation
303 uint32 CurrentDynamicRangeOffset = 0; //< Current working SBT record offset for the next dynamic allocation
304};
305
306#endif // RHI_RAYTRACING
307
#define check(expr)
Definition AssertionMacros.h:314
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
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
constexpr bool EnumHasAnyFlags(Enum Flags, Enum Contains)
Definition EnumClassFlags.h:35
UE_FORCEINLINE_HINT bool operator!=(const FIndexedPointer &Other) const
Definition LockFreeList.h:76
ERayTracingHitGroupIndexingMode
Definition RHIResources.h:3628
ERayTracingShaderBindingMode
Definition RHIResources.h:3620
constexpr uint32 HashCombine(uint32 A, uint32 C)
Definition TypeHash.h:36
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition RenderGraphResources.h:1321
Definition RenderGraphBuilder.h:49
RENDERCORE_API FRDGBufferRef RegisterExternalBuffer(const TRefCountPtr< FRDGPooledBuffer > &ExternalPooledBuffer, ERDGBufferFlags Flags=ERDGBufferFlags::None)
Definition RenderGraphBuilder.cpp:1111
Definition RHICommandList.h:455
Definition RHICommandList.h:3819
Definition RHIResources.h:3729
Definition RHIResources.h:3863
Definition ScopeLock.h:141
Definition SpanAllocator.h:15
Definition Array.h:670
Definition UnrealString.h.inl:34
Definition RefCounting.h:454
uint32 GetTypeHash(const FKey &Key)
Definition BlackboardKey.h:35
bool operator==(const FCachedAssetKey &A, const FCachedAssetKey &B)
Definition AssetDataMap.h:501
void MarkDirty(UE::Net::FReplicationStateHeader &InternalState, FNetBitArrayView &MemberChangeMask, const FReplicationStateMemberChangeMaskDescriptor &ChangeMaskInfo)
Definition ReplicationStateUtil.h:76
Definition RayTracingMeshDrawCommands.h:16
Definition RHIResources.h:3635