RuntimeMeshRendering.h
Go to the documentation of this file.
1 // Copyright 2016-2018 Chris Conway (Koderz). All Rights Reserved.
2 
3 #pragma once
4 
5 #include "Engine/Engine.h"
6 #include "RuntimeMeshCore.h"
8 
10 using FRuntimeMeshSectionProxyPtr = TSharedPtr<FRuntimeMeshSectionProxy, ESPMode::NotThreadSafe>;
11 using FRuntimeMeshSectionProxyWeakPtr = TWeakPtr<FRuntimeMeshSectionProxy, ESPMode::NotThreadSafe>;
12 
13 
15 class FRuntimeMeshVertexBuffer : public FVertexBuffer
16 {
17 protected:
19  const EBufferUsageFlags UsageFlags;
20 
22  const int32 VertexSize;
23 
25  int32 NumVertices;
26 
28  FShaderResourceViewRHIRef ShaderResourceView;
29 
30 public:
31 
32  FRuntimeMeshVertexBuffer(EUpdateFrequency InUpdateFrequency, int32 InVertexSize);
33 
35 
36  void Reset(int32 InNumVertices);
37 
38  virtual void InitRHI() override;
39 
41  int32 Num() { return NumVertices; }
42 
44  int32 GetBufferSize() const { return NumVertices * VertexSize; }
45 
46  /* Set the size of the vertex buffer */
47  void SetNum(int32 NewVertexCount);
48 
49  /* Set the data for the vertex buffer */
50  void SetData(const TArray<uint8>& Data);
51 
52  virtual void Bind(FLocalVertexFactory::FDataType& DataType) = 0;
53 
54 protected:
55 #if ENGINE_MAJOR_VERSION >= 4 && ENGINE_MINOR_VERSION >= 19
56  virtual void CreateSRV() = 0;
57 #endif
58 };
59 
60 
62 {
63 public:
65  : FRuntimeMeshVertexBuffer(InUpdateFrequency, sizeof(FVector))
66  {
67 
68  }
69 
70  virtual void Bind(FLocalVertexFactory::FDataType& DataType) override
71  {
72  DataType.PositionComponent = FVertexStreamComponent(this, 0, 12, VET_Float3);
73 #if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 19
74  DataType.PositionComponentSRV = ShaderResourceView;
75 #endif
76  }
77 
78 protected:
79 #if ENGINE_MAJOR_VERSION >= 4 && ENGINE_MINOR_VERSION >= 19
80  virtual void CreateSRV() override
81  {
82  ShaderResourceView = RHICreateShaderResourceView(VertexBufferRHI, 4, PF_R32_FLOAT);
83  }
84 #endif
85 };
86 
88 {
91 
92 public:
93  FRuntimeMeshTangentsVertexBuffer(EUpdateFrequency InUpdateFrequency, bool bInUseHighPrecision)
94  : FRuntimeMeshVertexBuffer(InUpdateFrequency, (bInUseHighPrecision ? sizeof(FPackedRGBA16N) : sizeof(FPackedNormal)) * 2)
95  , bUseHighPrecision(bInUseHighPrecision)
96  {
97 
98  }
99 
100  virtual void Bind(FLocalVertexFactory::FDataType& DataType) override
101  {
102  uint32 TangentSizeInBytes = 0;
103  uint32 TangentXOffset = 0;
104  uint32 TangentZOffset = 0;
105  EVertexElementType TangentElementType = VET_None;
106 
107  if (bUseHighPrecision)
108  {
109  TangentElementType = VET_UShort4N;
110  TangentSizeInBytes = sizeof(FRuntimeMeshTangentsHighPrecision);
111  TangentXOffset = offsetof(FRuntimeMeshTangentsHighPrecision, Tangent);
112  TangentZOffset = offsetof(FRuntimeMeshTangentsHighPrecision, Normal);
113  }
114  else
115  {
116  TangentElementType = VET_PackedNormal;
117  TangentSizeInBytes = sizeof(FRuntimeMeshTangents);
118  TangentXOffset = offsetof(FRuntimeMeshTangents, Tangent);
119  TangentZOffset = offsetof(FRuntimeMeshTangents, Normal);
120  }
121 
122 #if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 19
123  DataType.TangentBasisComponents[0] = FVertexStreamComponent(this, TangentXOffset, TangentSizeInBytes, TangentElementType, EVertexStreamUsage::ManualFetch);
124  DataType.TangentBasisComponents[1] = FVertexStreamComponent(this, TangentZOffset, TangentSizeInBytes, TangentElementType, EVertexStreamUsage::ManualFetch);
125  DataType.TangentsSRV = ShaderResourceView;
126 #else
127  DataType.TangentBasisComponents[0] = FVertexStreamComponent(this, TangentXOffset, TangentSizeInBytes, TangentElementType);
128  DataType.TangentBasisComponents[1] = FVertexStreamComponent(this, TangentZOffset, TangentSizeInBytes, TangentElementType);
129 #endif
130  }
131 
132 protected:
133 #if ENGINE_MAJOR_VERSION >= 4 && ENGINE_MINOR_VERSION >= 20
134  virtual void CreateSRV() override
135  {
136  ShaderResourceView = RHICreateShaderResourceView(VertexBufferRHI, bUseHighPrecision ? 8 : 4, bUseHighPrecision ? PF_R16G16B16A16_SNORM : PF_R8G8B8A8_SNORM);
137  }
138 #elif ENGINE_MAJOR_VERSION >= 4 && ENGINE_MINOR_VERSION >= 19
139  virtual void CreateSRV() override
140  {
141  ShaderResourceView = RHICreateShaderResourceView(VertexBufferRHI, bUseHighPrecision? 8 : 4, bUseHighPrecision? PF_A16B16G16R16 : PF_R8G8B8A8);
142  }
143 #endif
144 };
145 
147 {
150 
152  int32 NumUVs;
153 
154 public:
155  FRuntimeMeshUVsVertexBuffer(EUpdateFrequency InUpdateFrequency, bool bInUseHighPrecision, int32 InNumUVs)
156  : FRuntimeMeshVertexBuffer(InUpdateFrequency, (bInUseHighPrecision ? sizeof(FVector2D) : sizeof(FVector2DHalf)) * InNumUVs)
157  , bUseHighPrecision(bInUseHighPrecision), NumUVs(InNumUVs)
158  {
159 
160  }
161 
162  virtual void Bind(FLocalVertexFactory::FDataType& DataType) override
163  {
164  DataType.TextureCoordinates.Empty();
165 
166 #if ENGINE_MAJOR_VERSION >= 4 && ENGINE_MINOR_VERSION >= 19
167  DataType.NumTexCoords = NumUVs;
168 #endif
169 
170 
171  EVertexElementType UVDoubleWideVertexElementType = VET_None;
172  EVertexElementType UVVertexElementType = VET_None;
173  uint32 UVSizeInBytes = 0;
174  if (bUseHighPrecision)
175  {
176  UVSizeInBytes = sizeof(FVector2D);
177  UVDoubleWideVertexElementType = VET_Float4;
178  UVVertexElementType = VET_Float2;
179  }
180  else
181  {
182  UVSizeInBytes = sizeof(FVector2DHalf);
183  UVDoubleWideVertexElementType = VET_Half4;
184  UVVertexElementType = VET_Half2;
185  }
186 
187  uint32 UVStride = UVSizeInBytes * NumUVs;
188 
189  int32 UVIndex;
190  for (UVIndex = 0; UVIndex < NumUVs - 1; UVIndex += 2)
191  {
192 #if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 19
193  DataType.TextureCoordinates.Add(FVertexStreamComponent(this, UVSizeInBytes * UVIndex, UVStride, UVDoubleWideVertexElementType, EVertexStreamUsage::ManualFetch));
194 #else
195  DataType.TextureCoordinates.Add(FVertexStreamComponent(this, UVSizeInBytes * UVIndex, UVStride, UVDoubleWideVertexElementType));
196 #endif
197  }
198 
199  // possible last UV channel if we have an odd number
200  if (UVIndex < NumUVs)
201  {
202 #if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 19
203  DataType.TextureCoordinates.Add(FVertexStreamComponent(this, UVSizeInBytes * UVIndex, UVStride, UVDoubleWideVertexElementType, EVertexStreamUsage::ManualFetch));
204 #else
205  DataType.TextureCoordinates.Add(FVertexStreamComponent(this, UVSizeInBytes * UVIndex, UVStride, UVDoubleWideVertexElementType));
206 #endif
207  }
208 
209 #if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 19
210  DataType.TextureCoordinatesSRV = ShaderResourceView;
211 #endif
212  }
213 
214 protected:
215 #if ENGINE_MAJOR_VERSION >= 4 && ENGINE_MINOR_VERSION >= 19
216  virtual void CreateSRV() override
217  {
218  ShaderResourceView = RHICreateShaderResourceView(VertexBufferRHI, bUseHighPrecision ? 8 : 4, bUseHighPrecision ? PF_G32R32F : PF_G16R16F);
219  }
220 #endif
221 };
222 
224 {
225 public:
227  : FRuntimeMeshVertexBuffer(InUpdateFrequency, sizeof(FColor))
228  {
229 
230  }
231 
232  virtual void Bind(FLocalVertexFactory::FDataType& DataType) override
233  {
234 #if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 19
235  DataType.ColorComponent = FVertexStreamComponent(this, 0, 4, EVertexElementType::VET_Color, EVertexStreamUsage::ManualFetch);
236  DataType.ColorComponentsSRV = ShaderResourceView;
237 #else
238  DataType.ColorComponent = FVertexStreamComponent(this, 0, 4, EVertexElementType::VET_Color);
239 #endif
240  }
241 
242 protected:
243 #if ENGINE_MAJOR_VERSION >= 4 && ENGINE_MINOR_VERSION >= 19
244  virtual void CreateSRV() override
245  {
246  ShaderResourceView = RHICreateShaderResourceView(VertexBufferRHI, 4, PF_R8G8B8A8);
247  }
248 #endif
249 };
250 
251 
252 
253 
254 
256 class FRuntimeMeshIndexBuffer : public FIndexBuffer
257 {
258 private:
259  /* The number of indices this buffer is currently allocated to hold */
260  int32 NumIndices;
261 
262  /* The size of a single index*/
263  int32 IndexSize;
264 
265  /* The buffer configuration to use */
266  EBufferUsageFlags UsageFlags;
267 
268 public:
269 
271 
273 
274  void Reset(int32 InIndexSize, int32 InNumIndices, EUpdateFrequency InUpdateFrequency);
275 
276  virtual void InitRHI() override;
277 
278  /* Get the size of the index buffer */
279  int32 Num() { return NumIndices; }
280 
282  int32 GetBufferSize() const { return NumIndices * IndexSize; }
283 
284  /* Set the size of the index buffer */
285  void SetNum(int32 NewIndexCount);
286 
287  /* Set the data for the index buffer */
288  void SetData(const TArray<uint8>& Data);
289 };
290 
292 class FRuntimeMeshVertexFactory : public FLocalVertexFactory
293 {
294 public:
295 
297 
299  void Init(FLocalVertexFactory::FDataType VertexStructure);
300 
301  /* Gets the section visibility for static sections */
302 #if ENGINE_MAJOR_VERSION >= 4 && ENGINE_MINOR_VERSION >= 19
303  virtual uint64 GetStaticBatchElementVisibility(const class FSceneView& View, const struct FMeshBatch* Batch, const void* InViewCustomData = nullptr) const;
304 #else
305  virtual uint64 GetStaticBatchElementVisibility(const class FSceneView& View, const struct FMeshBatch* Batch) const;
306 #endif
307 
308 private:
309  /* Interface to the parent section for checking visibility.*/
311 };
virtual void Bind(FLocalVertexFactory::FDataType &DataType) override
const EBufferUsageFlags UsageFlags
FShaderResourceViewRHIRef ShaderResourceView
virtual void Bind(FLocalVertexFactory::FDataType &DataType) override
EUpdateFrequency
virtual void Bind(FLocalVertexFactory::FDataType &DataType) override
TSharedPtr< FRuntimeMeshSectionProxy, ESPMode::NotThreadSafe > FRuntimeMeshSectionProxyPtr
FRuntimeMeshTangentsVertexBuffer(EUpdateFrequency InUpdateFrequency, bool bInUseHighPrecision)
virtual void Bind(FLocalVertexFactory::FDataType &DataType) override
void Reset(int32 InNumVertices)
FRuntimeMeshVertexBuffer(EUpdateFrequency InUpdateFrequency, int32 InVertexSize)
virtual void Bind(FLocalVertexFactory::FDataType &DataType)=0
FRuntimeMeshSectionProxy * SectionParent
TWeakPtr< FRuntimeMeshSectionProxy, ESPMode::NotThreadSafe > FRuntimeMeshSectionProxyWeakPtr
FRuntimeMeshPositionVertexBuffer(EUpdateFrequency InUpdateFrequency)
#define offsetof(STRUCTURE, FIELD)
Definition: sqlite3.c:11372
void SetData(const TArray< uint8 > &Data)
virtual void InitRHI() override
FRuntimeMeshColorVertexBuffer(EUpdateFrequency InUpdateFrequency)
void SetNum(int32 NewVertexCount)
FRuntimeMeshUVsVertexBuffer(EUpdateFrequency InUpdateFrequency, bool bInUseHighPrecision, int32 InNumUVs)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:41