UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
LightRendering.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 LightRendering.h: Light rendering declarations.
5=============================================================================*/
6
7#pragma once
8
9#include "CoreMinimal.h"
10#include "RHI.h"
11#include "RHIResourceUtils.h"
12#include "RenderResource.h"
13#include "UniformBuffer.h"
14#include "ShaderParameters.h"
16#include "Shader.h"
17#include "GlobalShader.h"
18#include "SceneRendering.h"
20#include "LightSceneInfo.h"
21
26 SHADER_PARAMETER(float, ContactShadowLength)
27 SHADER_PARAMETER(float, ContactShadowCastingIntensity)
28 SHADER_PARAMETER(float, ContactShadowNonCastingIntensity)
29 SHADER_PARAMETER(float, VolumetricScatteringIntensity)
31 SHADER_PARAMETER(uint32,LightingChannelMask)
34
36
37extern float GetLightFadeFactor(const FSceneView& View, const FLightSceneProxy* Proxy);
38
39extern uint32 GetShadowedBits(const FSceneView& View, const FLightSceneInfo& LightSceneInfo, bool bUseLightFunctionAtlas = false);
40extern FDeferredLightUniformStruct GetDeferredLightParameters(const FSceneView& View, const FLightSceneInfo& LightSceneInfo, bool bUseLightFunctionAtlas=false, uint32 LightFlags=0);
41
51
53 const FSceneView& View,
56
67
70{
72public:
73 void Bind(const FShaderParameterMap& ParameterMap);
74
76
81
84 {
85 Ar << P.LightFunctionParameters;
86 return Ar;
87 }
88
89private:
91};
92
95{
100 extern void DrawSphere(FRHICommandList& RHICmdList);
106 extern void DrawSphere(FRHICommandList& RHICmdList, uint32 InstanceCount);
108 extern void DrawVectorSphere(FRHICommandList& RHICmdList);
110 extern void DrawCone(FRHICommandList& RHICmdList);
111
115 template<int32 NumSphereSides, int32 NumSphereRings, typename VectorType>
116 class TStencilSphereVertexBuffer : public FVertexBuffer
117 {
118 public:
119 static_assert(std::is_same_v<typename VectorType::FReal, float>, "Must be a float vector type");
120
121 int32 GetNumRings() const
122 {
123 return NumSphereRings;
124 }
125
129 void InitRHI(FRHICommandListBase& RHICmdList) override
130 {
131 const int32 NumSides = NumSphereSides;
133 const int32 NumVerts = (NumSides + 1) * (NumRings + 1);
134
135 const float RadiansPerRingSegment = UE_PI / (float)NumRings;
136 float Radius = 1;
137
139 ArcVerts.Empty(NumRings + 1);
140 // Calculate verts for one arc
141 for (int32 i = 0; i < NumRings + 1; i++)
142 {
143 const float Angle = i * RadiansPerRingSegment;
144 ArcVerts.Add(FVector3f(0.0f, FMath::Sin(Angle), FMath::Cos(Angle)));
145 }
146
147 TArray<VectorType> Verts;
148 Verts.Empty(NumVerts);
149 // Then rotate this arc NumSides + 1 times.
150 const FVector3f Center = FVector3f(0,0,0);
151 for (int32 s = 0; s < NumSides + 1; s++)
152 {
153 FRotator3f ArcRotator(0, 360.f * ((float)s / NumSides), 0);
155
156 for (int32 v = 0; v < NumRings + 1; v++)
157 {
158 const int32 VIx = (NumRings + 1) * s + v;
159 Verts.Add(Center + Radius * ArcRot.TransformPosition(ArcVerts[v]));
160 }
161 }
162
163 NumSphereVerts = Verts.Num();
164
165 // Create vertex buffer. Fill buffer with initial data upon creation
166 VertexBufferRHI = UE::RHIResourceUtils::CreateVertexBufferFromArray(RHICmdList, TEXT("TStencilSphereVertexBuffer"), EBufferUsageFlags::Static, MakeConstArrayView(Verts));
167 }
168
169 int32 GetVertexCount() const { return NumSphereVerts; }
170
179 void CalcTransform(FVector4f& OutPosAndScale, const FSphere& Sphere, const FVector& PreViewTranslation, bool bConservativelyBoundSphere = true)
180 {
181 float Radius = Sphere.W; // LWC_TODO: Precision loss
183 {
185 const float RadiansPerRingSegment = UE_PI / (float)NumRings;
186
187 // Boost the effective radius so that the edges of the sphere approximation lie on the sphere, instead of the vertices
188 Radius /= FMath::Cos(RadiansPerRingSegment);
189 }
190
191 const FVector3f Translate(Sphere.Center + PreViewTranslation);
193 }
194
195 private:
196 int32 NumSphereVerts;
197 };
198
202 template<int32 NumSphereSides, int32 NumSphereRings>
203 class TStencilSphereIndexBuffer : public FIndexBuffer
204 {
205 public:
209 void InitRHI(FRHICommandListBase& RHICmdList) override
210 {
211 const int32 NumSides = NumSphereSides;
213 TArray<uint16> Indices;
214 Indices.Reserve(NumSides * NumRings * 6);
215
216 // Add triangles for all the vertices generated
217 for (int32 s = 0; s < NumSides; s++)
218 {
219 const int32 a0start = (s + 0) * (NumRings + 1);
220 const int32 a1start = (s + 1) * (NumRings + 1);
221
222 for (int32 r = 0; r < NumRings; r++)
223 {
224 Indices.Add(a0start + r + 0);
225 Indices.Add(a1start + r + 0);
226 Indices.Add(a0start + r + 1);
227 Indices.Add(a1start + r + 0);
228 Indices.Add(a1start + r + 1);
229 Indices.Add(a0start + r + 1);
230 }
231 }
232
233 NumIndices = Indices.Num();
234
235 // Create index buffer. Fill buffer with initial data upon creation
236 IndexBufferRHI = UE::RHIResourceUtils::CreateIndexBufferFromArray(RHICmdList, TEXT("TStencilSphereIndexBuffer"), EBufferUsageFlags::Static, MakeConstArrayView(Indices));
237 }
238
239 int32 GetIndexCount() const { return NumIndices; };
240
241 private:
242 int32 NumIndices;
243 };
244
245 extern TGlobalResource<TStencilSphereVertexBuffer<18, 12, FVector4f> > GStencilSphereVertexBuffer;
246 extern TGlobalResource<TStencilSphereVertexBuffer<18, 12, FVector3f> > GStencilSphereVectorBuffer;
247 extern TGlobalResource<TStencilSphereIndexBuffer<18, 12> > GStencilSphereIndexBuffer;
248 extern TGlobalResource<TStencilSphereVertexBuffer<4, 4, FVector4f> > GLowPolyStencilSphereVertexBuffer;
249 extern TGlobalResource<TStencilSphereIndexBuffer<4, 4> > GLowPolyStencilSphereIndexBuffer;
250
251}; //End StencilingGeometry
252
257{
259
260public:
261
262 BEGIN_SHADER_PARAMETER_STRUCT(FParameters, )
267
268 void Bind(const FShaderParameterMap& ParameterMap);
270 void Set(FRHIBatchedShaderParameters& BatchedParameters, const FSceneView& View, const FLightSceneInfo* LightSceneInfo) const;
271
273 static FParameters GetParameters(const FSceneView& View, const FLightSceneInfo* LightSceneInfo);
274
277 {
278 Ar << P.StencilGeometryPosAndScale;
279 Ar << P.StencilConeParameters;
280 Ar << P.StencilConeTransform;
281 return Ar;
282 }
283
284private:
285
289};
290
291
294 SHADER_PARAMETER(FVector4f, UVScaleBias)
297
300{
303
304 class FRadialLight : SHADER_PERMUTATION_BOOL("SHADER_RADIAL_LIGHT");
305 using FPermutationDomain = TShaderPermutationDomain<FRadialLight>;
306
307 BEGIN_SHADER_PARAMETER_STRUCT(FParameters, )
308 SHADER_PARAMETER_STRUCT_REF(FViewUniformShaderParameters, View)
309 SHADER_PARAMETER_STRUCT_INCLUDE(FDrawFullScreenRectangleParameters, FullScreenRect)
310 SHADER_PARAMETER_STRUCT_INCLUDE(FStencilingGeometryShaderParameters::FParameters, Geometry)
312
313public:
314 static bool ShouldCompilePermutation(const FGlobalShaderPermutationParameters& Parameters);
315 static void ModifyCompilationEnvironment(const FGlobalShaderPermutationParameters& Parameters, FShaderCompilerEnvironment& OutEnvironment);
316
317 static FDrawFullScreenRectangleParameters GetFullScreenRectParameters(
318 float X, float Y,
319 float SizeX, float SizeY,
320 float U, float V,
321 float SizeU, float SizeV,
322 FIntPoint InTargetSize,
323 FIntPoint InTextureSize);
324
325 static FParameters GetParameters(const FViewInfo& View,
326 float X, float Y,
327 float SizeX, float SizeY,
328 float U, float V,
329 float SizeU, float SizeV,
330 FIntPoint TargetSize,
331 FIntPoint TextureSize,
332 bool bBindViewUniform = true);
333
334 static FParameters GetParameters(const FViewInfo& View, bool bBindViewUniform = true);
335
336 static FParameters GetParameters(const FViewInfo& View, const FSphere& LightBounds, bool bBindViewUniform = true);
337
338 static FParameters GetParameters(const FViewInfo& View, const FLightSceneInfo* LightSceneInfo, bool bBindViewUniform = true);
339};
340
342{
343 Shadowmap,
344 Raytraced,
345 MegaLights, // Light handled through MegaLights raytracing
346 MegaLightsVSM, // Light projection handled through MegaLights using a VSM source data
347};
348
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
constexpr auto MakeConstArrayView(OtherRangeType &&Other)
Definition ArrayView.h:904
#define TEXT(x)
Definition Platform.h:1272
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
D3D12_DESCRIPTOR_HEAP_TYPE Translate(ERHIDescriptorHeapType InHeapType)
Definition D3D12Descriptors.h:19
ELightOcclusionType GetLightOcclusionType(const FLightSceneProxy &Proxy, const FSceneViewFamily &ViewFamily)
Definition LightRendering.cpp:747
FDeferredLightUniformStruct GetDeferredLightParameters(const FSceneView &View, const FLightSceneInfo &LightSceneInfo, bool bUseLightFunctionAtlas=false, uint32 LightFlags=0)
Definition LightRendering.cpp:623
void SetDeferredLightParameters(FRHIBatchedShaderParameters &BatchedParameters, const TShaderUniformBufferParameter< FDeferredLightUniformStruct > &DeferredLightUniformBufferParameter, const FLightSceneInfo *LightSceneInfo, const FSceneView &View, bool bUseLightFunctionAtlas)
Definition LightRendering.h:42
void SetSimpleDeferredLightParameters(FRHIBatchedShaderParameters &BatchedParameters, const TShaderUniformBufferParameter< FDeferredLightUniformStruct > &DeferredLightUniformBufferParameter, const FSimpleLightEntry &SimpleLight, const FSimpleLightPerViewEntry &SimpleLightPerViewData, const FSceneView &View)
Definition LightRendering.h:57
FDeferredLightUniformStruct GetSimpleDeferredLightParameters(const FSceneView &View, const FSimpleLightEntry &SimpleLight, const FSimpleLightPerViewEntry &SimpleLightPerViewData)
Definition LightRendering.cpp:739
uint32 GetShadowedBits(const FSceneView &View, const FLightSceneInfo &LightSceneInfo, bool bUseLightFunctionAtlas=false)
Definition LightRendering.cpp:613
uint32 GetShadowQuality()
Definition ShadowRendering.cpp:256
float GetLightFadeFactor(const FSceneView &View, const FLightSceneProxy *Proxy)
Definition LightRendering.cpp:799
ELightOcclusionType
Definition LightRendering.h:342
UE::Math::TVector< float > FVector3f
Definition MathFwd.h:73
UE::Math::TVector4< float > FVector4f
Definition MathFwd.h:75
#define LAYOUT_FIELD(T, Name,...)
Definition MemoryLayout.h:471
#define DECLARE_TYPE_LAYOUT(T, Interface)
Definition MemoryLayout.h:557
const bool
Definition NetworkReplayStreaming.h:178
void DrawCone(FPrimitiveDrawInterface *PDI, const FMatrix &ConeToWorld, float Angle1, float Angle2, uint32 NumSides, bool bDrawSideLines, const FLinearColor &SideLineColor, const FMaterialRenderProxy *MaterialRenderProxy, uint8 DepthPriority)
Definition PrimitiveDrawingUtils.cpp:481
void DrawSphere(FPrimitiveDrawInterface *PDI, const FVector &Center, const FRotator &Orientation, const FVector &Radii, int32 NumSides, int32 NumRings, const FMaterialRenderProxy *MaterialRenderProxy, uint8 DepthPriority, bool bDisableBackfaceCulling)
Definition PrimitiveDrawingUtils.cpp:302
#define END_GLOBAL_SHADER_PARAMETER_STRUCT
Definition ShaderParameterMacros.h:1669
#define BEGIN_SHADER_PARAMETER_STRUCT(StructTypeName, DllStorage)
Definition ShaderParameterMacros.h:1482
#define SHADER_PARAMETER_STRUCT_REF(StructType, MemberName)
Definition ShaderParameterMacros.h:1909
#define BEGIN_GLOBAL_SHADER_PARAMETER_STRUCT
Definition ShaderParameterMacros.h:1663
#define SHADER_PARAMETER_STRUCT_INCLUDE(StructType, MemberName)
Definition ShaderParameterMacros.h:1895
#define END_SHADER_PARAMETER_STRUCT()
Definition ShaderParameterMacros.h:1485
#define SHADER_PARAMETER(MemberType, MemberName)
Definition ShaderParameterMacros.h:1684
#define SHADER_USE_PARAMETER_STRUCT(ShaderClass, ShaderParentClass)
Definition ShaderParameterStruct.h:62
void SetShaderValue(FRHIBatchedShaderParameters &BatchedParameters, const FShaderParameter &Parameter, const ParameterType &Value, uint32 ElementIndex=0)
Definition ShaderParameterUtils.h:24
void SetUniformBufferParameterImmediate(FRHIBatchedShaderParameters &BatchedParameters, const TShaderUniformBufferParameter< TBufferStruct > &Parameter, const TBufferStruct &UniformBufferValue)
Definition ShaderParameterUtils.h:246
#define SHADER_PERMUTATION_BOOL(InDefineName)
Definition ShaderPermutation.h:482
#define DECLARE_SHADER_TYPE(ShaderClass, ShaderMetaTypeShortcut,...)
Definition Shader.h:1688
USkinnedMeshComponent float
Definition SkinnedMeshComponent.h:60
#define UE_PI
Definition UnrealMathUtility.h:129
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Archive.h:1208
Definition LightRendering.h:300
Definition GlobalShader.h:269
Definition RenderResource.h:509
FBufferRHIRef IndexBufferRHI
Definition RenderResource.h:524
Definition LightRendering.h:70
void Set(FRHIBatchedShaderParameters &BatchedParameters, const FLightSceneInfo *LightSceneInfo, float ShadowFadeFraction) const
Definition LightRendering.h:77
static FVector4f GetLightFunctionSharedParameters(const FLightSceneInfo *LightSceneInfo, float ShadowFadeFraction)
Definition LightRendering.cpp:310
void Bind(const FShaderParameterMap &ParameterMap)
Definition LightRendering.cpp:305
friend FArchive & operator<<(FArchive &Ar, FLightFunctionSharedParameters &P)
Definition LightRendering.h:83
Definition LightSceneInfo.h:36
Definition LightSceneInfo.h:208
Definition LightSceneProxy.h:43
Definition RHICommandList.h:455
Definition RHICommandList.h:3819
Definition SceneView.h:2212
Definition SceneView.h:1425
Definition ShaderCore.h:323
Definition ShaderParameters.h:56
Definition PrimitiveSceneProxy.h:136
Definition PrimitiveSceneProxy.h:153
Definition LightRendering.h:257
void Bind(const FShaderParameterMap &ParameterMap)
Definition LightRendering.cpp:434
friend FArchive & operator<<(FArchive &Ar, FStencilingGeometryShaderParameters &P)
Definition LightRendering.h:276
static FParameters GetParameters(const FVector4f &InStencilingGeometryPosAndScale)
Definition LightRendering.cpp:464
Definition RenderResource.h:474
FBufferRHIRef VertexBufferRHI
Definition RenderResource.h:489
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
void Empty(SizeType Slack=0)
Definition Array.h:2273
UE_FORCEINLINE_HINT void Reserve(SizeType Number)
Definition Array.h:3016
Definition RenderResource.h:543
Definition ShaderParameters.h:148
Definition MegaLights.cpp:462
Definition LightRendering.h:95
@ false
Definition radaudio_common.h:23
float v
Definition radaudio_mdct.cpp:62
Definition RHIShaderParameters.h:241
Definition ShaderPermutation.h:229