UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RayTracingMaterialHitShaders.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "RHIDefinitions.h"
6
7#if RHI_RAYTRACING
8
10#include "LightMapRendering.h"
11#include "MaterialDomain.h"
12#include "MeshMaterialShader.h"
13#include "MeshPassProcessor.inl"
16#include "RayTracingPayloadType.h"
19#include "SceneRendering.h"
20#include <type_traits>
21
25
27{
28public:
31
32 RENDERER_API void AddMeshBatch(const FMeshBatch& RESTRICT MeshBatch, uint64 BatchElementMask, const FPrimitiveSceneProxy* RESTRICT PrimitiveSceneProxy);
33
34private:
36 const FScene* Scene;
37 const FSceneView* ViewIfDynamicMeshCommand;
38 ERHIFeatureLevel::Type FeatureLevel;
40
41 bool Process(
44 const FPrimitiveSceneProxy* RESTRICT PrimitiveSceneProxy,
45 const FMaterialRenderProxy& RESTRICT MaterialRenderProxy,
46 const FMaterial& RESTRICT MaterialResource,
48
49 template<typename RayTracingShaderType, typename ShaderElementDataType>
53 const FPrimitiveSceneProxy* RESTRICT PrimitiveSceneProxy,
54 const FMaterialRenderProxy& RESTRICT MaterialRenderProxy,
55 const FMaterial& RESTRICT MaterialResource,
58 {
59 const FVertexFactory* RESTRICT VertexFactory = MeshBatch.VertexFactory;
60
61 checkf(MaterialRenderProxy.ImmutableSamplerState.ImmutableSamplers[0] == nullptr, TEXT("Immutable samplers not yet supported in Mesh Draw Command pipeline"));
62
64
65 SetupRayTracingMeshCommandMaskAndStatus(SharedCommand, MeshBatch, PrimitiveSceneProxy, MaterialResource, RayTracingType);
66
68 {
70 }
71
72 FVertexInputStreamArray VertexStreams;
73 VertexFactory->GetStreams(FeatureLevel, EVertexInputStreamType::Default, VertexStreams);
74
75 if (RayTracingShader.IsValid())
76 {
77 FMeshDrawSingleShaderBindings ShaderBindings = SharedCommand.ShaderBindings.GetSingleShaderBindings(SF_RayHitGroup);
78 RayTracingShader->GetShaderBindings(Scene, FeatureLevel, PrimitiveSceneProxy, MaterialRenderProxy, MaterialResource, ShaderElementData, ShaderBindings);
79 }
80
81 const int32 NumElements = MeshBatch.Elements.Num();
82
84 {
85 if ((1ull << BatchElementIndex) & BatchElementMask)
86 {
88 FRayTracingMeshCommand& RayTracingMeshCommand = CommandContext->AddCommand(SharedCommand);
89
90 if (RayTracingShader.IsValid())
91 {
93 FMeshMaterialShader::GetElementShaderBindings(RayTracingShader, Scene, ViewIfDynamicMeshCommand, VertexFactory, EVertexInputStreamType::Default, FeatureLevel, PrimitiveSceneProxy, MeshBatch, BatchElement, ShaderElementData, RayHitGroupShaderBindings, VertexStreams);
94
95 // Command can only be cached if no global/static uniform buffers are used - if all platforms use SBTLayout for all RT shaders then this could be a check
96 RayTracingMeshCommand.bCanBeCached = !RayTracingMeshCommand.HasGlobalUniformBufferBindings();
97 }
98
99 RayTracingMeshCommand.GeometrySegmentIndex = uint32(MeshBatch.SegmentIndex) + BatchElementIndex;
100 RayTracingMeshCommand.bIsTranslucent = MeshBatch.IsTranslucent(MaterialResource.GetFeatureLevel());
101 CommandContext->FinalizeCommand(RayTracingMeshCommand);
102 }
103 }
104 }
105
109 const FPrimitiveSceneProxy* RESTRICT PrimitiveSceneProxy,
110 const FMaterialRenderProxy& RESTRICT MaterialRenderProxy,
111 const FMaterial& RESTRICT MaterialResource);
112
113 bool TryAddMeshBatch(
116 const FPrimitiveSceneProxy* RESTRICT PrimitiveSceneProxy,
118 const FMaterialRenderProxy& MaterialRenderProxy,
119 const FMaterial& Material
120 );
121};
122
124{
127
129 {
131 }
132
133 static ERayTracingPayloadType GetRayTracingPayloadType(const int32 PermutationId)
134 {
135 return ERayTracingPayloadType::RayTracingMaterial;
136 }
137
139 {
140 return RayTracing::GetShaderBindingLayout(Parameters.Platform);
141 }
142
143 using FParameters = FEmptyShaderParameters;
144};
145
147{
150
152 {
154 }
155
156 static ERayTracingPayloadType GetRayTracingPayloadType(const int32 PermutationId)
157 {
158 return ERayTracingPayloadType::RayTracingMaterial;
159 }
160
162 {
163 return RayTracing::GetShaderBindingLayout(Parameters.Platform);
164 }
165
166 using FParameters = FEmptyShaderParameters;
167};
168
170{
173
175 {
177 }
178
179 static ERayTracingPayloadType GetRayTracingPayloadType(const int32 PermutationId)
180 {
181 return ERayTracingPayloadType::Decals;
182 }
183
185 {
186 return RayTracing::GetShaderBindingLayout(Parameters.Platform);
187 }
188
189 using FParameters = FEmptyShaderParameters;
190};
191
193{
194public:
195
197 {}
198
203
205
207 {
208 FRayTracingLocalShaderBindings* Result = AllocateInternal();
209
210 if (NumUniformBuffers)
211 {
212 uint32 AllocSize = sizeof(FRHIUniformBuffer*) * NumUniformBuffers;
213 Result->UniformBuffers = (FRHIUniformBuffer**)ParameterMemory.Alloc(AllocSize, alignof(FRHIUniformBuffer*));
214 FMemory::Memset(Result->UniformBuffers, 0, AllocSize);
215 }
216 Result->NumUniformBuffers = NumUniformBuffers;
217
218 if (LooseDataSize)
219 {
220 Result->LooseParameterData = (uint8*)ParameterMemory.Alloc(LooseDataSize, alignof(void*));
221 }
222 Result->LooseParameterDataSize = LooseDataSize;
223
224 return *Result;
225 }
226
228 {
229 return *AllocateInternal();
230 }
231
233 {
234 const FChunk* Chunk = FirstChunk;
235 while (Chunk)
236 {
238 Chunk = Chunk->Next;
239 }
240 }
241
242 struct FChunk
243 {
244 static constexpr uint32 MaxNum = 1024;
245
246 // Note: constructors for elements of this array are called explicitly in AllocateInternal(). Destructors are not called.
247 static_assert(std::is_trivially_destructible_v<FRayTracingLocalShaderBindings>, "FRayTracingLocalShaderBindings must be trivially destructible, as no destructor will be called.");
248 FRayTracingLocalShaderBindings Bindings[MaxNum];
249 FChunk* Next;
250 uint32 Num;
251 };
252
253 const FChunk* GetFirstChunk() const
254 {
255 return FirstChunk;
256 }
257
258private:
259
260 FChunk* FirstChunk = nullptr;
261 FChunk* CurrentChunk = nullptr;
262
264
265 friend class FRHICommandList;
266
267 FRayTracingLocalShaderBindings* AllocateInternal()
268 {
269 if (!CurrentChunk || CurrentChunk->Num == FChunk::MaxNum)
270 {
271 FChunk* OldChunk = CurrentChunk;
272
273 static_assert(std::is_trivially_destructible_v<FChunk>, "Chunk must be trivially destructible, as no destructor will be called.");
274 CurrentChunk = (FChunk*)ParameterMemory.Alloc(sizeof(FChunk), alignof(FChunk));
275 CurrentChunk->Next = nullptr;
276 CurrentChunk->Num = 0;
277
278 if (FirstChunk == nullptr)
279 {
280 FirstChunk = CurrentChunk;
281 }
282
283 if (OldChunk)
284 {
285 OldChunk->Next = CurrentChunk;
286 }
287 }
288
289 FRayTracingLocalShaderBindings* ResultMemory = &CurrentChunk->Bindings[CurrentChunk->Num++];
291 }
292};
293
294template <typename FunctionType>
296 FRDGBuilder& GraphBuilder,
299 FunctionType SetupBindingsFunction)
300{
302 const uint32 TargetBindingsPerTask = 1024;
304 const uint32 BindingsPerTask = FMath::DivideAndRoundUp(NumTotalDirtyBindings, NumTasks); // Evenly divide commands between tasks (avoiding potential short last task)
305
306 ShaderBindingWriters.SetNum(NumTasks);
307
308 for (uint32 TaskIndex = 0; TaskIndex < NumTasks; ++TaskIndex)
309 {
310 const uint32 FirstTaskBindingIndex = TaskIndex * BindingsPerTask;
313
316
317 GraphBuilder.AddSetupTask(
319 {
321
322 for (uint32 BindingIndex = 0; BindingIndex < NumBindings; ++BindingIndex)
323 {
326 }
327 });
328 }
329}
330
331void SetRayTracingShaderBindings(FRHICommandList& RHICmdList, FSceneRenderingBulkObjectAllocator& Allocator, FViewInfo::FRayTracingData& RayTracingData);
332
333#endif // RHI_RAYTRACING
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define RESTRICT
Definition Platform.h:706
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 TRACE_CPUPROFILER_EVENT_SCOPE(Name)
Definition CpuProfilerTrace.h:528
#define DECLARE_EXPORTED_GLOBAL_SHADER(ShaderClass, RequiredAPI)
Definition GlobalShader.h:409
@ Num
Definition MetalRHIPrivate.h:234
@ SF_RayHitGroup
Definition RHIDefinitions.h:211
#define GRHISupportsRayTracingShaders
Definition RHIGlobals.h:856
bool ShouldCompileRayTracingCallableShadersForProject(EShaderPlatform ShaderPlatform)
Definition RenderUtils.cpp:1770
bool ShouldCompileRayTracingShadersForProject(EShaderPlatform ShaderPlatform)
Definition RenderUtils.cpp:1758
#define SHADER_USE_ROOT_PARAMETER_STRUCT(ShaderClass, ShaderParentClass)
Definition ShaderParameterStruct.h:79
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition GlobalShader.h:173
Definition GlobalShader.h:269
static bool ShouldCompilePermutation(const FShaderPermutationParameters &)
Definition Shader.h:860
Definition MaterialRenderProxy.h:102
Definition MaterialShared.h:2058
Definition MemStack.h:78
FORCEINLINE FMeshDrawSingleShaderBindings GetSingleShaderBindings(EShaderFrequency Frequency, int32 &DataOffset)
Definition MeshPassProcessor.h:978
Definition MeshDrawShaderBindings.h:104
RENDERER_API void GetElementShaderBindings(const FShaderMapPointerTable &PointerTable, const FScene *Scene, const FSceneView *ViewIfDynamicMeshCommand, const FVertexFactory *VertexFactory, const EVertexInputStreamType InputStreamType, const FStaticFeatureLevel FeatureLevel, const FPrimitiveSceneProxy *PrimitiveSceneProxy, const FMeshBatch &MeshBatch, const FMeshBatchElement &BatchElement, const FMeshMaterialShaderElementData &ShaderElementData, FMeshDrawSingleShaderBindings &ShaderBindings, FVertexInputStreamArray &VertexStreams) const
Definition ShaderBaseClasses.cpp:471
Definition PrimitiveSceneProxy.h:296
Definition RenderGraphBuilder.h:49
UE::Tasks::FTask AddSetupTask(TaskLambda &&Task, bool bCondition=true, ERDGSetupTaskWaitPoint WaitPoint=ERDGSetupTaskWaitPoint::Compile)
Definition RHICommandList.h:3819
void SetRayTracingHitGroups(FRHIShaderBindingTable *SBT, FRayTracingPipelineState *Pipeline, uint32 NumBindings, const FRayTracingLocalShaderBindings *Bindings, bool bCopyDataToInlineStorage=true)
Definition RHICommandList.h:4449
Definition RHIResources.h:984
Definition RHIResources.h:3863
Definition RHIResources.h:1232
Definition RayTracingMeshDrawCommands.h:231
Definition RayTracingMeshDrawCommands.h:69
uint32 GeometrySegmentIndex
Definition RayTracingMeshDrawCommands.h:75
bool bCanBeCached
Definition RayTracingMeshDrawCommands.h:87
bool HasGlobalUniformBufferBindings() const
Definition RayTracingMeshDrawCommands.h:107
FMeshDrawShaderBindings ShaderBindings
Definition RayTracingMeshDrawCommands.h:71
bool bIsTranslucent
Definition RayTracingMeshDrawCommands.h:83
Definition PipelineStateCache.cpp:1285
Definition RayTracingMeshDrawCommands.h:165
Definition SceneView.h:1425
Definition ScenePrivate.h:2875
Definition ShaderCore.h:466
static ERayTracingPayloadType GetRayTracingPayloadType(const int32 PermutationId)
Definition Shader.h:873
static const FShaderBindingLayout * GetShaderBindingLayout(const FShaderPermutationParameters &)
Definition Shader.h:857
Definition LightMapRendering.h:346
Definition VertexFactory.h:661
Definition Shader.h:1021
Type
Definition RHIFeatureLevel.h:20
UE_STRING_CLASS Result(Forward< LhsType >(Lhs), RhsLen)
Definition String.cpp.inl:732
Definition GlobalShader.h:73
static constexpr UE_FORCEINLINE_HINT T DivideAndRoundUp(T Dividend, T Divisor)
Definition UnrealMathUtility.h:694
static UE_FORCEINLINE_HINT void * Memset(void *Dest, uint8 Char, SIZE_T Count)
Definition UnrealMemory.h:119
Definition MeshBatch.h:232
Definition MeshBatch.h:371
Definition RHICommandList.h:267
Definition ShaderPermutation.h:27
const EShaderPlatform Platform
Definition ShaderPermutation.h:29