vsg: include/vsg/raytracing/TopLevelAccelerationStructure.h Source File

vsg 1.1.13 VulkanSceneGraph library

Loading...
Searching...
No Matches
TopLevelAccelerationStructure.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2019 Thomas Hogarth
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/core/Array.h>
16#include <vsg/core/Value.h>
17#include <vsg/raytracing/AccelerationStructure.h>
18#include <vsg/raytracing/BottomLevelAccelerationStructure.h>
19
20namespace vsg
21{
22
23 // VkGeometryInstance encapsulates the VkAccelerationStructureInstanceKHR settings.
24 // This structure is required to populate the top level structures instance buffer and is essentially the same as VkAccelerationStructureInstanceKHR
25 // https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap33.html#acceleration-structure
26 struct VkGeometryInstance
27 {
28 float transform[12]; // transform matrix
29 uint32_t instanceId : 24; // id available in shader as gl_InstanceCustomIndexNV in shader
30 uint32_t mask : 8; // mask used with rayMask for culling
31 uint32_t instanceOffset : 24; // offset into shader binding table for shaders
32 uint32_t flags : 8; // VkGeometryInstanceFlagBitsNV
33 uint64_t accelerationStructureHandle; // handle to bottomlevel acceleration structure
34 };
35 VSG_value(VkGeometryInstanceValue, VkGeometryInstance);
36 VSG_array(VkGeometryInstanceArray, VkGeometryInstance);
37
39 class VSG_DECLSPEC GeometryInstance : public Inherit<Object, GeometryInstance>
40 {
41 public:
42 GeometryInstance();
43
44 operator VkGeometryInstance() const
45 {
46 VkGeometryInstance inst;
47 inst.instanceId = id;
48 inst.mask = mask;
49 inst.instanceOffset = shaderOffset;
50 inst.flags = flags;
51 inst.accelerationStructureHandle = accelerationStructure->handle();
52
53 inst.transform[0] = transform[0][0];
54 inst.transform[1] = transform[1][0];
55 inst.transform[2] = transform[2][0];
56 inst.transform[3] = transform[3][0];
57
58 inst.transform[4] = transform[0][1];
59 inst.transform[5] = transform[1][1];
60 inst.transform[6] = transform[2][1];
61 inst.transform[7] = transform[3][1];
62
63 inst.transform[8] = transform[0][2];
64 inst.transform[9] = transform[1][2];
65 inst.transform[10] = transform[2][2];
66 inst.transform[11] = transform[3][2];
67
68 return inst;
69 }
70
71 mat4 transform;
72 uint32_t id;
73 uint32_t mask;
74 uint32_t shaderOffset;
75 uint32_t flags;
76 ref_ptr<BottomLevelAccelerationStructure> accelerationStructure;
77 };
78 VSG_type_name(vsg::GeometryInstance);
79
80 using GeometryInstances = std::vector<ref_ptr<GeometryInstance>>;
81
83 class VSG_DECLSPEC TopLevelAccelerationStructure : public Inherit<AccelerationStructure, TopLevelAccelerationStructure>
84 {
85 public:
86 explicit TopLevelAccelerationStructure(Device* device);
87
88 void compile(Context& context) override;
89
90 GeometryInstances geometryInstances;
91
92 protected:
93 // compiled data
94 ref_ptr<VkGeometryInstanceArray> _instances;
95 ref_ptr<Buffer> _instanceBuffer;
96 };
97 VSG_type_name(vsg::TopLevelAccelerationStructure);
98
99} // namespace vsg
vsg::Context
Definition Context.h:67
vsg::Device
Device encapsulates VkDevice, a logical handle to the PhysicalDevice with capabilities specified duri...
Definition Device.h:39
vsg::GeometryInstance
GeometryInstance is an instance of a bottom level acceleration structure reference by a top level acc...
Definition TopLevelAccelerationStructure.h:40
vsg::TopLevelAccelerationStructure
TopLevelAccelerationStructure encapsulates the set up of the top level acceleration structure contain...
Definition TopLevelAccelerationStructure.h:84
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::VkGeometryInstance
Definition TopLevelAccelerationStructure.h:27