UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RayTracingScene.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
8#if RHI_RAYTRACING
9
11#include "Math/DoubleFloat.h"
12#include "RHI.h"
13#include "RHIUtilities.h"
14#include "RHIGPUReadback.h"
16#include "Misc/MemStack.h"
18#include "MeshPassProcessor.h"
21#include "RayTracingDebugTypes.h"
22
23class FGPUScene;
27class FRDGBuilder;
29
30namespace Nanite
31{
33}
34
36{
37 Base = 0,
38 Decals,
40
41 NUM
42};
43
49{
50public:
51
52 struct FInstanceHandle
53 {
54 FInstanceHandle()
57 {}
58
59 bool IsValid() const
60 {
61 return Layer < ERayTracingSceneLayer::NUM && Index != UINT32_MAX;
62 }
63
64 // We currently need to store these handles in FPrimitiveSceneInfo but since that's a public header we can't use FRayTracingScene::FInstanceHandle directly.
65 // For now we provide a way to cast FInstanceHandle to uint32 and then FRayTracingScene methods also accept uint32 "PackedHandle".
66 // TODO: Consider moving this handle type to a public header to avoid this.
67 uint32 AsUint32() const
68 {
69 return Index | (int32(Layer) << 24);
70 }
71
72 private:
74 : Layer(InLayer)
75 , Index(InIndex)
76 {}
77
78 explicit FInstanceHandle(uint32 InPackedHandle)
80 , Index(InPackedHandle & 0xFFFFFF)
81 {}
82
85
86 friend class FRayTracingScene;
87 };
88
89 static const FInstanceHandle INVALID_INSTANCE_HANDLE;
90
91 struct FViewHandle
92 {
95 {}
96
97 bool IsValid() const
98 {
99 return Index != UINT32_MAX;
100 }
101
102 private:
103 explicit FViewHandle(uint32 InIndex)
104 : Index(InIndex)
105 {}
106
107 operator uint32()
108 {
109 return Index;
110 }
111
113
114 friend class FRayTracingScene;
115 };
116
117 static const FViewHandle INVALID_VIEW_HANDLE;
118
121
122 FInstanceHandle AddCachedInstance(FRayTracingGeometryInstance Instance, ERayTracingSceneLayer Layer, const FPrimitiveSceneProxy* Proxy = nullptr, bool bDynamic = false, int32 GeometryHandle = INDEX_NONE);
123 void FreeCachedInstance(FInstanceHandle Handle);
125
126 void UpdateCachedInstanceGeometry(FInstanceHandle Handle, FRHIRayTracingGeometry* GeometryRHI, int32 InstanceContributionToHitGroupIndex);
127 void UpdateCachedInstanceGeometry(uint32 Handle, FRHIRayTracingGeometry* GeometryRHI, int32 InstanceContributionToHitGroupIndex);
128
131
133
134 void MarkInstanceVisible(FInstanceHandle Handle, FViewHandle ViewHandle);
136
137 // Builds various metadata required to create the final scene.
138 // Must be done before calling Create(...).
139 void BuildInitializationData(bool bUseLightingChannels, bool bForceOpaque, bool bDisableTriangleCull);
140
143
144 // Allocates GPU memory to fit at least the current number of instances.
145 // Kicks off instance buffer build to parallel thread along with RDG pass.
146 void Update(FRDGBuilder& GraphBuilder, FSceneUniformBuffer& SceneUniformBuffer, const FGPUScene* GPUScene, ERDGPassFlags ComputePassFlags);
147
148 void Build(FRDGBuilder& GraphBuilder, ERDGPassFlags ComputePassFlags, FRDGBufferRef DynamicGeometryScratchBuffer);
149
150 void PostRender(FRDGBuilder& GraphBuilder, ERDGPassFlags ComputePassFlags = ERDGPassFlags::Compute);
151
152 // Reset transient state/resources
153 void Reset();
154
155 void EndFrame();
156
157 // Prevent cached instances being added/freed (via validation checks) until Reset() or EndFrame()
158 // Must be done before adding transient instances.
160 {
162 }
163
164 bool SetInstanceExtraDataBufferEnabled(bool bEnabled);
165 bool SetTracingFeedbackEnabled(bool bEnabled);
166 bool SetInstanceDebugDataEnabled(bool bEnabled);
167
169
170 // Allocates temporary memory that will be valid until the next Reset().
171 // Can be used to store temporary instance transforms, user data, etc.
172 template <typename T>
173 TArrayView<T> Allocate(int32 Count)
174 {
175 return MakeArrayView(new(Allocator) T[Count], Count);
176 }
177
178 // Returns true if RHI ray tracing scene has been created.
179 // i.e. returns true after BeginCreate() and before Reset().
180 RENDERER_API bool IsCreated() const;
181
182 // Returns RayTracingSceneRHI object (may return null).
184
185 // Similar to GetRayTracingScene, but checks that ray tracing scene RHI object is valid.
187
188 // Creates new RHI view of a layer. Can only be used on valid ray tracing scene.
190
191 // Returns RDG view of a layer. Can only be used on valid ray tracing scene.
193
194 // Feedback
196
197 FRDGBufferRef GetInstanceBuffer(ERayTracingSceneLayer Layer, FViewHandle ViewHandle) const { return Layers[uint8(Layer)].Views[ViewHandle].InstanceBuffer; }
198
199 TConstArrayView<FRayTracingGeometryInstance> GetInstances(ERayTracingSceneLayer Layer) const { return MakeArrayView(Layers[uint8(Layer)].Instances); }
200
201 FRayTracingGeometryInstance& GetInstance(FInstanceHandle Handle) { return Layers[uint8(Handle.Layer)].Instances[Handle.Index]; }
202
204
205 FRDGBufferRef GetInstanceDebugBuffer(ERayTracingSceneLayer Layer) const { return Layers[uint8(Layer)].InstanceDebugBuffer; }
206
207 FRDGBufferRef GetInstanceExtraDataBuffer(ERayTracingSceneLayer Layer, FViewHandle ViewHandle) const { return Layers[uint8(Layer)].Views[ViewHandle].InstanceExtraDataBuffer; }
208
209 void SetViewParams(FViewHandle ViewHandle, const FViewMatrices& ViewMatrices, const FRayTracingCullingParameters& CullingParameters);
210
211 FVector GetPreViewTranslation(FViewHandle ViewHandle) const { return ViewParameters[ViewHandle].PreViewTranslation; }
212
213public:
214
215 // Public members for initial refactoring step (previously were public members of FViewInfo).
216
217 // Geometries which still have a pending build request but are used this frame and require a force build.
219
220 bool bUsesLightingChannels = false;
221
222 UE::Tasks::FTask InitTask; // Task to asynchronously call BuildInitializationData()
223
224private:
225
226 void FinishTracingFeedback(FRDGBuilder& GraphBuilder, ERDGPassFlags ComputePassFlags);
227 void FinishStats(FRDGBuilder& GraphBuilder, ERDGPassFlags ComputePassFlags);
228
231
232 struct FLayerView
233 {
235
237
238 FRDGBufferRef InstanceBuffer = nullptr;
239 FRDGBufferRef HitGroupContributionsBuffer = nullptr;
241
242 // Feedback
246
250
252
253 TBitArray<> VisibleInstances;
254
256 uint32 MaxNumInstances = 0;
257 };
258
259 struct FLayer
260 {
262
263 // Feedback
266
267 // Special data for debugging purposes
269
270 // Persistent storage for ray tracing instance descriptors.
271 // The array is divided into two sections [Cached instances | Transient Instances]
272 // Transient instances are cleared every frame.
274
276
278
280
281 TArray<FLayerView> Views;
282
283 FName Name;
284 };
285
286 TArray<FLayer> Layers;
287
288 // Transient memory allocator
290
291 struct FViewParameters
292 {
293 const FRayTracingCullingParameters* CullingParameters;
294
295 // Used for transforming to translated world space in which TLAS was built.
296 FVector PreViewTranslation;
297 };
298
300
301 TSparseArray<int32> ActiveViews;
304
306 bool bTracingFeedbackEnabled = false;
307 bool bInstanceDebugDataEnabled = false;
308
309 bool bInitializationDataBuilt = false;
310 bool bUsedThisFrame = false;
311
312 // Adding/freeing cached instances is not allowed when this bool is set (used for validation)
313 bool bCachedInstancesLocked = false;
314
316 {
319 };
320
321 const uint32 MaxReadbackBuffers = 4;
322
324
328
329 struct FStatsReadbackData
330 {
331 FRHIGPUBufferReadback* ReadbackBuffer = nullptr;
333 };
334
338};
339
340#endif // RHI_RAYTRACING
constexpr auto MakeArrayView(OtherRangeType &&Other)
Definition ArrayView.h:873
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::int16 int16
A 16-bit signed integer.
Definition Platform.h:1123
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
FORCEINLINE bool IsRayTracingFeedbackEnabled(const FSceneViewFamily &ViewFamily)
Definition RaytracingOptions.h:195
ERDGPassFlags
Definition RenderGraphDefinitions.h:128
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition GPUScene.h:217
Definition MemStack.h:78
Definition NameTypes.h:617
Definition PrimitiveSceneProxy.h:296
Definition RenderGraphResources.h:1426
Definition RenderGraphResources.h:1452
Definition RenderGraphResources.h:1321
Definition RenderGraphBuilder.h:49
Definition RHICommandList.h:455
Definition RHIGPUReadback.h:116
Definition RHIResources.h:3729
Definition RHIResources.h:3755
Definition RHIResources.h:3304
Definition RayTracingGeometry.h:31
Definition SceneUniformBuffer.h:137
Definition ArrayView.h:139
Definition Array.h:670
Definition UnrealString.h.inl:34
Definition SparseArray.h:524
void PostRender(FScene &Scene)
Definition HairStrandsRendering.cpp:372
Definition SkinnedMeshComponent.h:50
int16 CoarseMeshStreamingHandle
Definition PrimitiveSceneProxy.h:55
U16 Index
Definition radfft.cpp:71
Definition RHIResources.h:3357
Definition SceneView.h:317