1 /*
2  * Copyright © 2021 Bas Nieuwenhuizen
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifndef RADV_ACCELERATION_STRUCTURE_H
25 #define RADV_ACCELERATION_STRUCTURE_H
26 
27 #include <stdint.h>
28 #include <vulkan/vulkan.h>
29 
30 struct radv_accel_struct_serialization_header {
31    uint8_t driver_uuid[VK_UUID_SIZE];
32    uint8_t accel_struct_compat[VK_UUID_SIZE];
33    uint64_t serialization_size;
34    uint64_t compacted_size;
35    uint64_t instance_count;
36    uint64_t instances[];
37 };
38 
39 struct radv_accel_struct_header {
40    uint32_t root_node_offset;
41    uint32_t reserved;
42    float aabb[2][3];
43 
44    /* Everything after this gets updated/copied from the CPU. */
45    uint64_t compacted_size;
46    uint64_t serialization_size;
47    uint32_t copy_dispatch_size[3];
48    uint64_t instance_offset;
49    uint64_t instance_count;
50 };
51 
52 struct radv_bvh_triangle_node {
53    float coords[3][3];
54    uint32_t reserved[3];
55    uint32_t triangle_id;
56    /* flags in upper 4 bits */
57    uint32_t geometry_id_and_flags;
58    uint32_t reserved2;
59    uint32_t id;
60 };
61 
62 struct radv_bvh_aabb_node {
63    float aabb[2][3];
64    uint32_t primitive_id;
65    /* flags in upper 4 bits */
66    uint32_t geometry_id_and_flags;
67    uint32_t reserved[8];
68 };
69 
70 struct radv_bvh_instance_node {
71    uint64_t base_ptr;
72    /* lower 24 bits are the custom instance index, upper 8 bits are the visibility mask */
73    uint32_t custom_instance_and_mask;
74    /* lower 24 bits are the sbt offset, upper 8 bits are VkGeometryInstanceFlagsKHR */
75    uint32_t sbt_offset_and_flags;
76 
77    /* The translation component is actually a pre-translation instead of a post-translation. If you
78     * want to get a proper matrix out of it you need to apply the directional component of the
79     * matrix to it. The pre-translation of the world->object matrix is the same as the
80     * post-translation of the object->world matrix so this way we can share data between both
81     * matrices. */
82    float wto_matrix[12];
83    float aabb[2][3];
84    uint32_t instance_id;
85 
86    /* Object to world matrix transposed from the initial transform. Translate part is store in the
87     * wto_matrix. */
88    float otw_matrix[9];
89 };
90 
91 struct radv_bvh_box16_node {
92    uint32_t children[4];
93    uint32_t coords[4][3];
94 };
95 
96 struct radv_bvh_box32_node {
97    uint32_t children[4];
98    float coords[4][2][3];
99    uint32_t reserved[4];
100 };
101 
102 #endif