UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
DynamicPrimitiveDrawing.inl
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 DynamicPrimitiveDrawing.inl: Dynamic primitive drawing implementation.
5=============================================================================*/
6
7#pragma once
8
9#include "CoreTypes.h"
10#include "CoreFwd.h"
12
16class FHitProxyId;
19class FRenderTask;
20class FRHICommandList;
21class FTexture;
22class FViewElementPDI;
23class FViewInfo;
24class HHitProxy;
25struct FMath;
26struct FMeshBatch;
28struct TStatId;
29template<typename TTask> class TGraphTask;
30
37
39{
40 return HitProxyConsumer != NULL;
41}
43{
44 // Change the current hit proxy.
45 CurrentHitProxy = HitProxy;
46
47 if(HitProxyConsumer && HitProxy)
48 {
49 // Notify the hit proxy consumer of the new hit proxy.
50 HitProxyConsumer->AddHitProxy(HitProxy);
51 }
52}
53
55{
56 if (IsInGameThread())
57 {
58 // Render thread might be reading the array while we are adding in the game thread
59 FViewInfo* InViewInfo = ViewInfo;
62 {
63 InViewInfo->DynamicResources.Add(DynamicResource);
64 DynamicResource->InitPrimitiveResource(RHICmdList);
65 });
66 }
67 else
68 {
69 ViewInfo->DynamicResources.Add(DynamicResource);
70 DynamicResource->InitPrimitiveResource(FRHICommandListImmediate::Get());
71 }
72}
73
74inline FBatchedElements& FViewElementPDI::GetElements(uint8 DepthPriorityGroup) const
75{
76 return DepthPriorityGroup ? ViewInfo->TopBatchedViewElements : ViewInfo->BatchedViewElements;
77}
78
80 const FVector& Position,
81 float SizeX,
82 float SizeY,
83 const FTexture* Sprite,
84 const FLinearColor& Color,
85 uint8 DepthPriorityGroup,
86 float U,
87 float UL,
88 float V,
89 float VL,
90 uint8 BlendMode,
91 float OpacityMaskRefVal
92 )
93{
94 FBatchedElements &Elements = GetElements(DepthPriorityGroup);
95
96 Elements.AddSprite(
98 SizeX,
99 SizeY,
100 Sprite,
101 Color,
102 CurrentHitProxy ? CurrentHitProxy->Id : FHitProxyId(),
103 U, UL, V, VL,
104 BlendMode,
105 OpacityMaskRefVal
106 );
107}
108
110{
111 FBatchedElements& Elements = GetElements(DepthPriorityGroup);
112
114}
115
117 const FVector& Start,
118 const FVector& End,
119 const FLinearColor& Color,
120 uint8 DepthPriorityGroup,
121 float Thickness,
122 float DepthBias,
123 bool bScreenSpace
124 )
125{
126 FBatchedElements &Elements = GetElements(DepthPriorityGroup);
127
128 Elements.AddLine(
129 Start,
130 End,
131 Color,
132 CurrentHitProxy ? CurrentHitProxy->Id : FHitProxyId(),
133 Thickness,
134 DepthBias,
135 bScreenSpace
136 );
137}
138
140 const FVector& Start,
141 const FVector& End,
142 const FLinearColor& Color,
143 uint8 DepthPriorityGroup,
144 float Thickness,
145 float DepthBias,
146 bool bScreenSpace
147)
148{
149 FBatchedElements& Elements = GetElements(DepthPriorityGroup);
150
151 Elements.AddTranslucentLine(
152 Start,
153 End,
154 Color,
155 CurrentHitProxy ? CurrentHitProxy->Id : FHitProxyId(),
156 Thickness,
157 DepthBias,
158 bScreenSpace
159 );
160}
161
163 const FVector& Position,
164 const FLinearColor& Color,
165 float PointSize,
166 uint8 DepthPriorityGroup
167 )
168{
169 float ScaledPointSize = PointSize;
170
171 bool bIsPerspective = (ViewInfo->ViewMatrices.GetProjectionMatrix().M[3][3] < 1.0f) ? true : false;
172 if( !bIsPerspective )
173 {
174 const float ZoomFactor = FMath::Min<float>(View->ViewMatrices.GetProjectionMatrix().M[0][0], View->ViewMatrices.GetProjectionMatrix().M[1][1]);
176 }
177
178 FBatchedElements &Elements = GetElements(DepthPriorityGroup);
179
180 Elements.AddPoint(
181 Position,
183 Color,
184 CurrentHitProxy ? CurrentHitProxy->Id : FHitProxyId()
185 );
186}
187
189{
190 bool bHasPrimitives = true;
191 const int32 NumElements = Mesh.Elements.Num();
192 for (int32 ElementIndex = 0; ElementIndex < NumElements; ++ElementIndex)
193 {
194 const FMeshBatchElement& Element = Mesh.Elements[ElementIndex];
195 bHasPrimitives = bHasPrimitives && Element.NumPrimitives > 0 && Element.NumInstances > 0;
196 }
197 return bHasPrimitives;
198}
199
201{
202 // Warning: can be called from Game Thread or Rendering Thread. Be careful what you access.
203 check(DynamicPrimitiveCollector);
204
206 {
207 // Keep track of view mesh elements whether that have translucency.
208 ViewInfo->bHasTranslucentViewMeshElements |= true;//Mesh.IsTranslucent() ? 1 : 0;
209
210 uint8 DPGIndex = Mesh.DepthPriorityGroup;
211 // Get the correct element list based on dpg index
212 // Translucent view mesh elements in the foreground dpg are not supported yet
214
217 if( CurrentHitProxy != nullptr )
218 {
219 NewMesh->BatchHitProxyId = CurrentHitProxy->Id;
220 }
221
222 {
223 ERHIFeatureLevel::Type FeatureLevel = ViewInfo->GetFeatureLevel();
224
226 [NewMesh, DynamicPrimitiveCollectorForRT = DynamicPrimitiveCollector, FeatureLevel](FRHICommandListImmediate& RHICmdList)
227 {
228 const bool bPrimitiveShaderDataComesFromSceneBuffer = NewMesh->VertexFactory->GetPrimitiveIdStreamIndex(FeatureLevel, EVertexInputStreamType::Default) >= 0;
229
230 for (int32 ElementIndex = 0; ElementIndex < NewMesh->Elements.Num(); ElementIndex++)
231 {
232 FMeshBatchElement& MeshElement = NewMesh->Elements[ElementIndex];
233
235 {
236 checkf(!NewMesh->Elements[ElementIndex].PrimitiveUniformBuffer,
237 TEXT("FMeshBatch was assigned a PrimitiveUniformBuffer even though Vertex Factory %s fetches primitive shader data through a Scene buffer. The assigned PrimitiveUniformBuffer cannot be respected. Use PrimitiveUniformBufferResource instead for dynamic primitive data, or leave both null to get FPrimitiveSceneProxy->UniformBuffer."), NewMesh->VertexFactory->GetType()->GetName());
238 }
239
240 checkf(bPrimitiveShaderDataComesFromSceneBuffer || NewMesh->Elements[ElementIndex].PrimitiveUniformBufferResource != NULL,
241 TEXT("FMeshBatch was not properly setup. The primitive uniform buffer must be specified."));
242 }
243
244 // If we are maintaining primitive scene data on the GPU, copy the primitive uniform buffer data to a unified array so it can be uploaded later
246 {
247 for (int32 Index = 0; Index < NewMesh->Elements.Num(); ++Index)
248 {
249 FMeshBatchElement& Element = NewMesh->Elements[Index];
250 const TUniformBuffer<FPrimitiveUniformShaderParameters>* PrimitiveUniformBufferResource = Element.PrimitiveUniformBufferResource;
251
252 if (PrimitiveUniformBufferResource)
253 {
254 Element.PrimitiveIdMode = PrimID_DynamicPrimitiveShaderData;
256 Element.DynamicPrimitiveData,
257 *reinterpret_cast<const FPrimitiveUniformShaderParameters*>(PrimitiveUniformBufferResource->GetContents()),
258 Element.NumInstances,
259 Element.DynamicPrimitiveIndex,
260 Element.DynamicPrimitiveInstanceSceneDataOffset);
261 }
262 }
263 }
264
265 NewMesh->MaterialRenderProxy->UpdateUniformExpressionCacheIfNeeded(FeatureLevel);
266 });
267 }
268
269 return 1;
270 }
271 return 0;
272}
273
#define NULL
Definition oodle2base.h:134
#define check(expr)
Definition AssertionMacros.h:314
#define ensure( InExpression)
Definition AssertionMacros.h:464
#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
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
bool MeshBatchHasPrimitives(const FMeshBatch &Mesh)
Definition DynamicPrimitiveDrawing.inl:188
@ SDPG_Foreground
Definition EngineTypes.h:191
@ PrimID_DynamicPrimitiveShaderData
Definition MeshBatch.h:83
EShaderPlatform GMaxRHIShaderPlatform
Definition RHI.cpp:1335
bool UseGPUScene(const FStaticShaderPlatform Platform, const FStaticFeatureLevel FeatureLevel)
Definition RenderUtils.cpp:1636
#define ENQUEUE_RENDER_COMMAND(Type)
Definition RenderingThread.h:1167
CORE_API bool IsInGameThread()
Definition ThreadingBase.cpp:185
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition BatchedElements.h:102
ENGINE_API void AddPoint(const FVector &Position, float Size, const FLinearColor &Color, FHitProxyId HitProxyId)
Definition BatchedElements.cpp:198
ENGINE_API void AddLine(const FVector &Start, const FVector &End, const FLinearColor &Color, FHitProxyId HitProxyId, float Thickness=0.0f, float DepthBias=0.0f, bool bScreenSpace=false)
Definition BatchedElements.cpp:129
ENGINE_API void AddSprite(const FVector &Position, float SizeX, float SizeY, const FTexture *Texture, const FLinearColor &Color, FHitProxyId HitProxyId, float U, float UL, float V, float VL, uint8 BlendMode, float OpacityMaskRefVal)
Definition BatchedElements.cpp:412
ENGINE_API void AddReserveLines(int32 NumLines, bool bDepthBiased=false, bool bThickLines=false)
Definition BatchedElements.cpp:353
ENGINE_API void AddTranslucentLine(const FVector &Start, const FVector &End, const FLinearColor &Color, FHitProxyId HitProxyId, float Thickness=0.0f, float DepthBias=0.0f, bool bScreenSpace=false)
Definition BatchedElements.cpp:165
Definition SceneManagement.h:1267
Definition GPUScene.h:80
Definition HitProxies.h:218
virtual void AddHitProxy(HHitProxy *HitProxy)=0
Definition HitProxies.h:86
Definition PrimitiveDrawInterface.h:19
const FSceneView * View
Definition PrimitiveDrawInterface.h:22
Definition RHICommandList.h:4626
static FRHICommandListImmediate & Get()
Definition RHICommandList.h:5522
Definition RHICommandList.h:3819
Definition RHICommandList.h:5514
ERHIFeatureLevel::Type GetFeatureLevel() const
Definition SceneView.h:2004
FViewMatrices ViewMatrices
Definition SceneView.h:1464
Definition RenderResource.h:354
Definition DynamicPrimitiveDrawing.h:17
virtual void AddReserveLines(uint8 DepthPriorityGroup, int32 NumLines, bool bDepthBiased=false, bool bThickLines=false) override
Definition DynamicPrimitiveDrawing.inl:109
virtual void DrawLine(const FVector &Start, const FVector &End, const FLinearColor &Color, uint8 DepthPriorityGroup, float Thickness=0.0f, float DepthBias=0.0f, bool bScreenSpace=false) override
Definition DynamicPrimitiveDrawing.inl:116
virtual void DrawTranslucentLine(const FVector &Start, const FVector &End, const FLinearColor &Color, uint8 DepthPriorityGroup, float Thickness=0.0f, float DepthBias=0.0f, bool bScreenSpace=false) override
Definition DynamicPrimitiveDrawing.inl:139
FViewElementPDI(FViewInfo *InViewInfo, FHitProxyConsumer *InHitProxyConsumer, FGPUScenePrimitiveCollector *InDynamicPrimitiveCollector)
Definition DynamicPrimitiveDrawing.inl:31
virtual void DrawPoint(const FVector &Position, const FLinearColor &Color, float PointSize, uint8 DepthPriorityGroup) override
Definition DynamicPrimitiveDrawing.inl:162
virtual void SetHitProxy(HHitProxy *HitProxy) override
Definition DynamicPrimitiveDrawing.inl:42
virtual void RegisterDynamicResource(FDynamicPrimitiveResource *DynamicResource) override
Definition DynamicPrimitiveDrawing.inl:54
virtual bool IsHitTesting() override
Definition DynamicPrimitiveDrawing.inl:38
virtual void DrawSprite(const FVector &Position, float SizeX, float SizeY, const FTexture *Sprite, const FLinearColor &Color, uint8 DepthPriorityGroup, float U, float UL, float V, float VL, uint8 BlendMode=SE_BLEND_Masked, float OpacityMaskRefVal=.5f) override
Definition DynamicPrimitiveDrawing.inl:79
virtual int32 DrawMesh(const FMeshBatch &Mesh) override
Definition DynamicPrimitiveDrawing.inl:200
Definition SceneRendering.h:1132
TIndirectArray< FMeshBatch, SceneRenderingAllocator > TopViewMeshElements
Definition SceneRendering.h:1249
TIndirectArray< FMeshBatch, SceneRenderingAllocator > ViewMeshElements
Definition SceneRendering.h:1246
FBatchedElements & TopBatchedViewElements
Definition SceneRendering.h:1243
FBatchedElements & BatchedViewElements
Definition SceneRendering.h:1240
uint32 bHasTranslucentViewMeshElements
Definition SceneRendering.h:1357
TArray< FDynamicPrimitiveResource *, SceneRenderingAllocator > DynamicResources
Definition SceneRendering.h:1252
Definition HitProxies.h:135
FHitProxyId Id
Definition HitProxies.h:153
Definition TaskGraphInterfaces.h:598
Definition IndirectArray.h:20
Definition UniformBuffer.h:29
const uint8 * GetContents() const
Definition UniformBuffer.h:78
Type
Definition RHIFeatureLevel.h:20
U16 Index
Definition radfft.cpp:71
Definition Color.h:48
Definition UnrealMathUtility.h:270
Definition MeshBatch.h:232
Definition MeshBatch.h:371
const FMatrix & GetProjectionMatrix() const
Definition SceneView.h:433
Definition LightweightStats.h:416
T M[4][4]
Definition Matrix.h:49