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 
234  void destroy(RV_Instance* inst);
236 
237  void setShaderInfoIndex(uint32_t index)
238  { myShaderInfoIndex = index; }
239  uint32_t getShaderInfoIndex() const
240  { return myShaderInfoIndex; }
241  void setTag(int tag)
242  { myShaderInfo.tag = tag; }
243  void setLightId(int light_id)
244  { myShaderInfo.lightId = light_id; }
246  { return myShaderInfo; }
247  VkAccelerationStructureKHR getVkAccelStruct()
248  { return myVkAccelStruct; }
250  { return myInfo->getScratchSize(); }
251  bool isBuilt() const
252  { return myIsBuilt; }
253  const RV_ResourceID &getID() const
254  { return myId; }
255  const UT_StringHolder &getName() const
256  { return myName; }
257  exint getSize() const;
258  VkDeviceAddress getDeviceAddress();
259 
260  void addInstance(RV_VKAccelerationStructureInstance as_inst);
261  void clearInstances();
262  void getVkInstances(UT_Array<VkAccelerationStructureInstanceKHR> &as_insts);
263 
264  // Only call this when the latest instances have been included
265  // in a top-level acceleration structure.
267  { myIsInstDirty = false; }
268  bool isInstDirty() const
269  { return myIsInstDirty; }
270 
271  void build(RV_VKCommandBuffer* cb, RV_VKBuffer* scratch_buffer);
272  void buildCompact(
273  RV_VKCommandBuffer* cb,
274  exint compact_size,
275  VkAccelerationStructureKHR& compact_vk_as,
276  RV_VKBufferPtr& compact_buffer);
277  void reset();
278  void update(
279  VkAccelerationStructureKHR compact_vk_as,
280  RV_VKBufferPtr compact_buffer);
281 
282  // return the size of the struct in GPU memory bytes
283  exint gpuMemoryUsage() const;
284 
285  // Debug print out of acceleration structure properties
286  void print() const;
287 
288 private:
289  RV_Instance* myInst;
290  RV_ResourceID myId;
291  UT_StringHolder myName;
293  VkAccelerationStructureKHR myVkAccelStruct;
294  RV_VKBufferPtr myBuffer;
296 
297  bool myIsBuilt = false;
298  bool myIsInstDirty = true;
299  uint32_t myShaderInfoIndex = 0;
300 
301  RV_AccelStructShaderInfo myShaderInfo;
302 
303  exint getVisibilityMask() const;
304 };
305 
306 // --------------------------------------------------
307 // RV_BuildAccelerationStructures
308 //
309 // Helper class to build acceleration structures
311 {
312 public:
314 
315  void addAccelerationStructure(RV_VKAccelerationStructure* accel_struct);
316 
317  void build(RV_VKCommandBuffer* cb);
318  void buildAndQueryCompactSize(RV_VKCommandBuffer* cb);
319  void buildCompact(RV_VKCommandBuffer* cb);
320  void updateCompact();
321 
322  bool isBuilt() const
323  { return myIsBuilt; }
324  bool isBuiltCompact() const
325  { return myIsBuiltCompact; }
326 
328  { return myTotalAccelStructSize; }
330  { return myTotalScratchSize; }
331  exint getTotalCompactSize();
332 
333 private:
334  RV_Instance* myInst;
335  UT_UniquePtr<RV_VKQuery> myQuery;
337  UT_Array<RV_VKBufferPtr> myScratchBuffers;
338 
339  UT_Array<VkAccelerationStructureKHR> myCompactVkAccelStructs;
340  UT_Array<RV_VKBufferPtr> myCompactBuffers;
341 
342  bool myIsBuilt = false;
343  bool myIsBuiltCompact = false;
344 
345  exint myTotalAccelStructSize = 0;
346  exint myTotalScratchSize = 0;
347  exint myTotalCompactSize = 0;
348 };
349 
351 {
352 public:
354 
356  {
357  myAccelStruct->reset();
358  }
359 
361  RV_VKAccelerationStructure* accel_struct)
362  : myAccelStruct(accel_struct)
363  {}
364 };
365 
367  RV_VKAccelerationStructure* accel_struct);
368 #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:454
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:48
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:75
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