UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
D3D12RayTracing.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "D3D12RHIPrivate.h"
6
7#if D3D12_RHI_RAYTRACING
8
10
11static_assert(sizeof(FD3D12_GPU_VIRTUAL_ADDRESS) == sizeof(D3D12_GPU_VIRTUAL_ADDRESS), "Size of FD3D12_GPU_VIRTUAL_ADDRESS must match D3D12_GPU_VIRTUAL_ADDRESS");
12
15
17
20{
21 virtual void RemoveListener(FD3D12RayTracingGeometry* InGeometry) = 0;
23};
24
26{
27 uint64 Data[4] = {~0ull, ~0ull, ~0ull, ~0ull};
28
29 // No shader is executed if a shader binding table record with null identifier is encountered.
30 static const FD3D12ShaderIdentifier Null;
31
32 bool operator == (const FD3D12ShaderIdentifier& Other) const
33 {
34 return Data[0] == Other.Data[0]
35 && Data[1] == Other.Data[1]
36 && Data[2] == Other.Data[2]
37 && Data[3] == Other.Data[3];
38 }
39
41 {
42 return !(*this == Other);
43 }
44
45 bool IsValid() const
46 {
47 return *this != FD3D12ShaderIdentifier();
48 }
49
50 void SetData(const void* InData)
51 {
52 FMemory::Memcpy(Data, InData, sizeof(Data));
53 }
54};
55
57{
58 void Reserve(uint32 NumShaders)
59 {
60 Shaders.Reserve(NumShaders);
61 Identifiers.Reserve(NumShaders);
62 }
63
64 int32 Find(FSHAHash Hash) const
65 {
66 for (int32 Index = 0; Index < Shaders.Num(); ++Index)
67 {
68 if (Hash == Shaders[Index]->GetHash())
69 {
70 return Index;
71 }
72 }
73
74 return INDEX_NONE;
75 }
76
79};
80
82{
83public:
85
87
88 FD3D12Device* Device;
89
94
96
99
100 // Maps raygen shader index to a specialized state object (may be -1 if no specialization is used for a shader)
102
103 // State objects with raygen shaders grouped by occupancy
105
108
110
112
113#if !NO_LOGGING
114 struct FShaderStats
115 {
116 const TCHAR* Name = nullptr;
117 float CompileTimeMS = 0;
118 uint32 StackSize = 0;
119 uint32 ShaderSize = 0;
120 };
122#endif // !NO_LOGGING
123
124 D3D12ResourceFrameCounter FrameCounter;
125};
126
128{
129public:
130
133
135 {
137 TEXT("Trying to get address of acceleration structure '%s' without allocated memory."), *DebugName.ToString());
138 return AccelerationStructureBuffers[GPUIndex]->ResourceLocation.GetGPUVirtualAddress();
139 }
140
141 void SetupHitGroupSystemParameters(uint32 InGPUIndex);
142 void UpdateResidency(FD3D12CommandContext& CommandContext);
146
147 // Implement FD3D12ShaderResourceRenameListener interface
149
151
154
156
158
160 void SetDirty(FRHIGPUMask GPUMask, bool bState)
161 {
162 for (uint32 GPUIndex : GPUMask)
163 {
165 }
166 }
167 bool IsDirty(uint32 GPUIndex) const
168 {
169 return bIsAccelerationStructureDirty[GPUIndex];
170 }
171 bool BuffersValid(uint32 GPUIndex) const;
172
175
176 static constexpr uint32 IndicesPerPrimitive = 3; // Triangle geometry only
177
178 static FBufferRHIRef NullTransformBuffer; // Null transform for hidden sections
179
181
183 bool bHasPendingCompactionRequests[MAX_NUM_GPUS];
184
185 // Hit shader parameters per geometry segment
187
188 // RAW SRVs to index and vertex buffers when using bindless hit group paramaters
191
192 FDebugName DebugName;
193 FName OwnerName; // Store the path name of the owner object for resource tracking
194
195 // Array of geometry descriptions, one per segment (single-segment geometry is a common case).
196 // Only references CPU-accessible structures (no GPU resources).
197 // Used as a template for BuildAccelerationStructure() later.
199
200 uint64 AccelerationStructureCompactedSize = 0;
201
202public:
203
204 void AddUpdateListener(ID3D12RayTracingGeometryUpdateListener* InUpdateListener) const
205 {
206 FScopeLock Lock(&UpdateListenersCS);
207 check(!UpdateListeners.Contains(InUpdateListener));
208 UpdateListeners.Add(InUpdateListener);
209 }
210
211 void RemoveUpdateListener(ID3D12RayTracingGeometryUpdateListener* InUpdateListener) const
212 {
213 FScopeLock Lock(&UpdateListenersCS);
214 uint32 Removed = UpdateListeners.Remove(InUpdateListener);
215
216 checkf(Removed == 1, TEXT("Should have exactly one registered listener during remove (same listener shouldn't registered twice and we shouldn't call this if not registered"));
217 }
218
219 bool HasListeners() const
220 {
221 FScopeLock Lock(&UpdateListenersCS);
222 return UpdateListeners.Num() != 0;
223 }
224
226 {
227 FScopeLock Lock(&UpdateListenersCS);
228 for (ID3D12RayTracingGeometryUpdateListener* UpdateListener : UpdateListeners)
229 {
230 UpdateListener->HitGroupParametersUpdated(this);
231 }
232 }
233
234private:
235 mutable FCriticalSection UpdateListenersCS;
237};
238
240{
241public:
242
245
246 const FRayTracingSceneInitializer& GetInitializer() const override final { return Initializer; }
247
248 void BindBuffer(FRHIBuffer* Buffer, uint32 BufferOffset);
249 void ReleaseBuffer();
250
252
253 uint32 NumInstances = 0;
254
256 uint32 BufferOffset = 0;
257
259
260 // Unique list of geometries referenced by all instances in this scene.
261 // Any referenced geometry is kept alive while the scene is alive.
263
264 // Scene keeps track of child acceleration structure buffers to ensure
265 // they are resident when any ray tracing work is dispatched.
266 // Resources that share residency handles are deduplicated.
268
269 void UpdateResidency(FD3D12CommandContext& CommandContext) const;
270
271 bool bBuilt = false;
272
273private:
275};
276
277// Manages all the pending BLAS compaction requests
279{
280public:
281
283
286 {
287 check(PendingRequests.IsEmpty());
288 }
289
290 void RequestCompact(FD3D12RayTracingGeometry* InRTGeometry);
291 bool ReleaseRequest(FD3D12RayTracingGeometry* InRTGeometry);
292
294
295private:
296
298 TArray<FD3D12RayTracingGeometry*> PendingRequests;
301
305};
306
307#endif // D3D12_RHI_RAYTRACING
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
@ INDEX_NONE
Definition CoreMiscDefines.h:150
#define UE_NONCOPYABLE(TypeName)
Definition CoreMiscDefines.h:457
#define TEXT(x)
Definition Platform.h:1272
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
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
uint64 FD3D12_GPU_VIRTUAL_ADDRESS
Definition D3D12RayTracingResources.h:12
UE_FORCEINLINE_HINT bool operator!=(const FIndexedPointer &Other) const
Definition LockFreeList.h:76
#define MAX_NUM_GPUS
Definition MultiGPU.h:25
EAccelerationStructureBuildMode
Definition RHIContext.h:579
uint64 FRayTracingAccelerationStructureAddress
Definition RHIResources.h:3725
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition D3D12RHICommon.h:54
Definition D3D12Adapter.h:136
Definition D3D12Resources.h:891
Definition D3D12CommandContext.h:513
Definition D3D12CommandContext.h:1193
Definition D3D12RHICommon.h:78
Definition D3D12Device.h:176
Definition D3D12Resources.h:641
Definition RHIDefinitions.h:95
Definition NameTypes.h:617
Definition UnrealTemplate.h:321
Definition RHIResources.h:1581
Definition RHICommandList.h:455
FRayTracingAccelerationStructureSize SizeInfo
Definition RHIResources.h:3722
Definition RHIResources.h:3729
FRayTracingGeometryInitializer Initializer
Definition RHIResources.h:3749
virtual FRayTracingAccelerationStructureAddress GetAccelerationStructureAddress(uint64 GPUIndex) const =0
Definition RHIResources.h:1115
Definition RHIResources.h:3755
virtual const FRayTracingSceneInitializer & GetInitializer() const =0
Definition RHIResources.h:5023
Definition SecureHash.h:226
Definition ScopeLock.h:141
Definition ArrayView.h:139
Definition Array.h:670
Definition RefCounting.h:454
Definition SharedPointer.h:692
Definition Mutex.h:18
GeometryCollection::Facades::FMuscleActivationData Data
Definition MuscleActivationConstraints.h:15
UE::FRecursiveMutex Mutex
Definition MeshPaintVirtualTexture.cpp:164
U16 Index
Definition radfft.cpp:71
Definition D3D12PipelineState.h:367
Definition D3D12Resources.h:884
virtual void ResourceRenamed(FD3D12ContextArray const &Contexts, FD3D12BaseShaderResource *InRenamedResource, FD3D12ResourceLocation *InNewResourceLocation)=0
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160
Definition MultiGPU.h:33
Definition RHIResources.h:3496
Definition RHIResources.h:3669
Definition ShaderCompiler.h:747