HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RV_VKAccelerationStructure.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: RV_VKAccelerationStructure.h ( RV Library, C++)
7  *
8  * COMMENTS:
9  * Class to manage Vulkan Acceleration Structure object
10  */
11 
12 #ifndef RV_VKAccelerationStructure_h
13 #define RV_VKAccelerationStructure_h
14 
15 #include "RV_API.h"
16 #include "RV_TypePtrs.h"
17 #include "RV_VKQuery.h"
18 
19 #include <VE/VE_VK.h>
20 
21 #include <UT/UT_Array.h>
22 #include <UT/UT_Matrix4.h>
23 #include <UT/UT_UniquePtr.h>
24 
25 class RV_Instance;
26 class RV_VKBuffer;
27 
28 
30 {
31  // Defines the ray hit behavior.
33  // Light ID > -1 if the accel struct is a light.
34  int lightId = -1;
35 };
36 
37 // -----------------
38 // RV_VKAccelerationStructureGeometry
39 //
40 // Specifies a single geometry within an acceleration structure
42 {
43 public:
44  void setOpaque(bool opaque)
45  { myIsOpaque = opaque; }
46  bool isOpaque() const
47  { return myIsOpaque; }
48  void setSingleHit(bool singleHit)
49  { myIsSingleHit = singleHit; }
50  bool isSingleHit() const
51  { return myIsSingleHit; }
52 
53  void setTriangles(
54  VkFormat vertex_format,
55  VkDeviceAddress vertex_buffer,
56  exint vertex_stride,
57  uint32_t num_vertices,
58  VkDeviceAddress index_buffer,
59  uint32_t num_indices);
60 
61  void setAABBs(
62  VkDeviceAddress aabb_buffer,
63  exint aabb_stride,
64  uint32_t num_boxes);
65 
66  void setInstances(
67  VkDeviceAddress instance_buffer,
68  uint32_t num_instances);
69 
70  void fillGeometryInfo();
71 
72 protected:
75  nullptr,
76  VK_GEOMETRY_TYPE_TRIANGLES_KHR, /* geometryType */
77  {}, /* geometry */
78  0 /* flags */
79  };
80 
84 
85  bool myIsOpaque = true;
86  bool myIsSingleHit = false;
87 
89 };
90 
91 // -----------------
92 // RV_VKAccelerationStructureBuildInfo
93 //
94 // Specifies the info for creating and building an acceleration structure
96 {
97 public:
98  void setUpdate(bool update)
99  { myIsUpdate = update; }
100  bool isUpdate() const
101  { return myIsUpdate; }
102  void setTopLevel(bool top_level)
103  { myIsTopLevel = top_level; }
104  bool isTopLevel() const
105  { return myIsTopLevel; }
107  { myPreference = preference; }
109  { return myPreference; }
110 
111  exint getSize() const
112  { return myBuildSizeInfo.accelerationStructureSize; }
114  { return !myIsUpdate ? myBuildSizeInfo.buildScratchSize :
115  myBuildSizeInfo.updateScratchSize; }
117  { return myBuildRangeInfos.data(); }
118 
119  void addGeometry(RV_VKAccelerationStructureGeometry& geometry);
120 
123  {
126  }
127 
128 protected:
131  nullptr,
132  0, /* createFlags */
133  VK_NULL_HANDLE, /* buffer */
134  0, /* offset */
135  0, /* size */
137  0 /* deviceAddress */
138  };
139 
142  nullptr,
146  VK_NULL_HANDLE, /* srcAccelerationStructure */
147  VK_NULL_HANDLE, /* dstAccelerationStructure */
148  0, /* geometryCount */
149  nullptr, /* pGeometries */
150  nullptr, /* ppGeometries */
151  {} /* scratchData */
152  };
153 
156  nullptr,
157  0,
158  0,
159  0
160  };
161 
164 
165  void querySizeInfo(RV_Instance* inst);
166  void fillCreateInfo(RV_VKBuffer* buffer);
167  void fillBuildInfo(
168  RV_VKBuffer* scratch_buffer,
169  VkAccelerationStructureKHR accel_struct);
170 
172 
173 private:
176 
177  bool myIsUpdate = false;
178  bool myIsTopLevel = false;
180 };
181 
183 {
184 public:
186  { myTransform = transform; }
188  { myVkAccelStructInst.instanceCustomIndex = index; }
190  { myVkAccelStructInst.mask = mask; }
192  { myVkAccelStructInst.instanceShaderBindingTableRecordOffset = offset; }
193  void setFaceCullingEnabled(bool enable)
194  { myFaceCullingEnabled = enable; }
195 
196  void fillInstanceInfo();
197 
198 protected:
199  VkAccelerationStructureInstanceKHR myVkAccelStructInst = {};
200 
202  { myVkAccelStructInst.accelerationStructureReference = ref; }
203 
205 
206 private:
208  bool myFaceCullingEnabled = true;
209 };
210 
211 // -----------------
212 // RV_VKAccelerationStructure
213 //
214 // Class holding VkAcceleratrionStructureKHR handle and bound
215 /// memory allocation
217 {
218 public:
219  // Build the compact version of an acceleration structure if the
220  // its compact size is less than %90 of its worst-case size
221  static constexpr float theCompactSizeThreshold = 0.9f;
222 
223  static RV_VKAccelerationStructurePtr create(
224  RV_Instance* inst,
227 
229  RV_Instance* inst,
231  VkAccelerationStructureKHR vk_as,
233 
235 
236  void setShaderInfoIndex(uint32_t index)
237  { myShaderInfoIndex = index; }
238  uint32_t getShaderInfoIndex() const
239  { return myShaderInfoIndex; }
240  void setTag(int tag)
241  { myShaderInfo.tag = tag; }
242  void setLightId(int light_id)
243  { myShaderInfo.lightId = light_id; }
245  { return myShaderInfo; }
246  VkAccelerationStructureKHR getVkAccelStruct()
247  { return myVkAccelStruct; }
249  { return myInfo->getScratchSize(); }
250  bool isBuilt() const
251  { return myIsBuilt; }
252  const RV_ResourceID &getID() const
253  { return myId; }
254  const UT_StringHolder &getName() const
255  { return myName; }
256  exint getSize() const;
257  VkDeviceAddress getDeviceAddress();
258 
259  void addInstance(RV_VKAccelerationStructureInstance as_inst);
260  void clearInstances();
261  void getVkInstances(UT_Array<VkAccelerationStructureInstanceKHR> &as_insts);
262 
263  // Only call this when the latest instances have been included
264  // in a top-level acceleration structure.
266  { myIsInstDirty = false; }
267  bool isInstDirty() const
268  { return myIsInstDirty; }
269 
270  void build(RV_VKCommandBuffer* cb, RV_VKBuffer* scratch_buffer);
271  void buildCompact(
272  RV_VKCommandBuffer* cb,
273  exint compact_size,
274  VkAccelerationStructureKHR& compact_vk_as,
275  RV_VKBufferPtr& compact_buffer);
276  void reset();
277  void update(
278  VkAccelerationStructureKHR compact_vk_as,
279  RV_VKBufferPtr compact_buffer);
280 
281  // return the size of the struct in GPU memory bytes
282  exint gpuMemoryUsage() const;
283 
284  // Debug print out of acceleration structure properties
285  void print() const;
286 
287 private:
288  RV_Instance* myInst;
289  RV_ResourceID myId;
290  UT_StringHolder myName;
292  VkAccelerationStructureKHR myVkAccelStruct;
293  RV_VKBufferPtr myBuffer;
295 
296  bool myIsBuilt = false;
297  bool myIsInstDirty = true;
298  uint32_t myShaderInfoIndex = 0;
299 
300  RV_AccelStructShaderInfo myShaderInfo;
301 
302  exint getVisibilityMask() const;
303 };
304 
305 // --------------------------------------------------
306 // RV_BuildAccelerationStructures
307 //
308 // Helper class to build acceleration structures
310 {
311 public:
313 
314  void addAccelerationStructure(RV_VKAccelerationStructure* accel_struct);
315 
316  void build(RV_VKCommandBuffer* cb);
317  void buildAndQueryCompactSize(RV_VKCommandBuffer* cb);
318  void buildCompact(RV_VKCommandBuffer* cb);
319  void updateCompact();
320 
321  bool isBuilt() const
322  { return myIsBuilt; }
323  bool isBuiltCompact() const
324  { return myIsBuiltCompact; }
325 
327  { return myTotalAccelStructSize; }
329  { return myTotalScratchSize; }
330  exint getTotalCompactSize();
331 
332 private:
333  RV_Instance* myInst;
334  UT_UniquePtr<RV_VKQuery> myQuery;
336  UT_Array<RV_VKBufferPtr> myScratchBuffers;
337 
338  UT_Array<VkAccelerationStructureKHR> myCompactVkAccelStructs;
339  UT_Array<RV_VKBufferPtr> myCompactBuffers;
340 
341  bool myIsBuilt = false;
342  bool myIsBuiltCompact = false;
343 
344  exint myTotalAccelStructSize = 0;
345  exint myTotalScratchSize = 0;
346  exint myTotalCompactSize = 0;
347 };
348 
350 {
351 public:
353 
355  {
356  myAccelStruct->reset();
357  }
358 
360  RV_VKAccelerationStructure* accel_struct)
361  : myAccelStruct(accel_struct)
362  {}
363 };
364 
366  RV_VKAccelerationStructure* accel_struct);
367 #endif
#define VK_NULL_HANDLE
Definition: vulkan_core.h:45
UT_UniquePtr< RV_VKBuffer > RV_VKBufferPtr
Definition: RV_TypePtrs.h:63
int64 exint
Definition: SYS_Types.h:125
VkAccelerationStructureKHR getVkAccelStruct()
RV_VKAccelerationStructure * myAccelStruct
GLuint buffer
Definition: glcorearb.h:660
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
UT_UniquePtr< RV_VKAccelerationStructureBuildInfo > clone() const
GLintptr offset
Definition: glcorearb.h:665
GLboolean reset
Definition: glad.h:5138
UT_UniquePtr< RV_VKAccelerationStructure > RV_VKAccelerationStructurePtr
Definition: RV_TypePtrs.h:84
GLint ref
Definition: glcorearb.h:124
void setAccelStructReference(VkDeviceAddress ref)
static const UT_Matrix4T< fpreal32 > & getIdentityMatrix()
GLint GLuint mask
Definition: glcorearb.h:124
#define RV_API
Definition: RV_API.h:10
RV_AccelStructBuildPref
Definition: RV_Type.h:520
const VkAccelerationStructureBuildRangeInfoKHR * getBuildRangeInfos()
RV_ResetAccelStructTask(RV_VKAccelerationStructure *accel_struct)
GLuint const GLchar * name
Definition: glcorearb.h:786
GA_API const UT_StringHolder transform
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:44
RV_AccelStructBuildPref getPreference() const
RV_AccelStructShaderInfo getShaderInfo() const
RV_VKCommandBuffer::Callback RVmakeResetAccelStructTask(RV_VKAccelerationStructure *accel_struct)
LeafData & operator=(const LeafData &)=delete
GLuint index
Definition: glcorearb.h:786
void setPreference(RV_AccelStructBuildPref preference)
const UT_StringHolder & getName() const
VkFormat
Definition: vulkan_core.h:1386
VkGeometryTypeKHR
A vulkan buffer object.
Definition: RV_VKBuffer.h:81
FMT_INLINE void print(format_string< T...> fmt, T &&...args)
Definition: core.h:2903
uint64_t VkDeviceAddress
Definition: vulkan_core.h:94
UT_Function< void(RV_Instance *)> Callback
const RV_ResourceID & getID() const