xref: /aosp_15_r20/external/mesa3d/src/amd/vulkan/bvh/bvh.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2021 Bas Nieuwenhuizen
3  *
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #ifndef BVH_BVH_H
8 #define BVH_BVH_H
9 
10 #define radv_bvh_node_triangle 0
11 #define radv_bvh_node_box16    4
12 #define radv_bvh_node_box32    5
13 #define radv_bvh_node_instance 6
14 #define radv_bvh_node_aabb     7
15 
16 #define radv_ir_node_triangle 0
17 #define radv_ir_node_internal 1
18 #define radv_ir_node_instance 2
19 #define radv_ir_node_aabb     3
20 
21 #define RADV_GEOMETRY_OPAQUE (1u << 31)
22 
23 #define RADV_INSTANCE_FORCE_OPAQUE                 (1u << 31)
24 #define RADV_INSTANCE_NO_FORCE_NOT_OPAQUE          (1u << 30)
25 #define RADV_INSTANCE_TRIANGLE_FACING_CULL_DISABLE (1u << 29)
26 #define RADV_INSTANCE_TRIANGLE_FLIP_FACING         (1u << 28)
27 
28 #ifdef VULKAN
29 #define VK_UUID_SIZE 16
30 #else
31 #include <vulkan/vulkan.h>
32 typedef struct radv_ir_node radv_ir_node;
33 typedef struct radv_global_sync_data radv_global_sync_data;
34 typedef struct radv_bvh_geometry_data radv_bvh_geometry_data;
35 
36 typedef uint16_t float16_t;
37 
38 typedef struct {
39    float values[3][4];
40 } mat3x4;
41 
42 typedef struct {
43    float x;
44    float y;
45    float z;
46 } vec3;
47 
48 typedef struct radv_aabb radv_aabb;
49 
50 #endif
51 
52 struct radv_aabb {
53    vec3 min;
54    vec3 max;
55 };
56 
57 struct radv_accel_struct_serialization_header {
58    uint8_t driver_uuid[VK_UUID_SIZE];
59    uint8_t accel_struct_compat[VK_UUID_SIZE];
60    uint64_t serialization_size;
61    uint64_t compacted_size;
62    uint64_t instance_count;
63 #ifndef VULKAN
64    uint64_t instances[];
65 #endif
66 };
67 
68 struct radv_accel_struct_geometry_info {
69    uint32_t primitive_count;
70    uint32_t flags;
71    uint32_t type;
72 };
73 
74 struct radv_accel_struct_header {
75    uint32_t bvh_offset;
76    uint32_t reserved;
77    radv_aabb aabb;
78 
79    /* Everything after this gets either updated/copied from the CPU or written by header.comp. */
80    uint64_t compacted_size;
81    uint64_t serialization_size;
82    uint32_t copy_dispatch_size[3];
83    uint64_t size;
84 
85    /* Everything after this gets updated/copied from the CPU. */
86    uint32_t geometry_count;
87    uint64_t instance_offset;
88    uint64_t instance_count;
89    uint32_t build_flags;
90 };
91 
92 struct radv_ir_node {
93    radv_aabb aabb;
94 };
95 
96 #define RADV_UNKNOWN_BVH_OFFSET 0xFFFFFFFF
97 #define RADV_NULL_BVH_OFFSET    0xFFFFFFFE
98 
99 struct radv_ir_box_node {
100    radv_ir_node base;
101    uint32_t children[2];
102    uint32_t bvh_offset;
103 };
104 
105 struct radv_global_sync_data {
106    uint32_t task_counts[2];
107    uint32_t task_started_counter;
108    uint32_t task_done_counter;
109    uint32_t current_phase_start_counter;
110    uint32_t current_phase_end_counter;
111    uint32_t phase_index;
112    /* If this flag is set, the shader should exit
113     * instead of executing another phase */
114    uint32_t next_phase_exit_flag;
115 };
116 
117 struct radv_ir_header {
118    int32_t min_bounds[3];
119    int32_t max_bounds[3];
120    uint32_t active_leaf_count;
121    /* Indirect dispatch dimensions for the encoder.
122     * ir_internal_node_count is the thread count in the X dimension,
123     * while Y and Z are always set to 1. */
124    uint32_t ir_internal_node_count;
125    uint32_t dispatch_size_y;
126    uint32_t dispatch_size_z;
127    radv_global_sync_data sync_data;
128    uint32_t dst_node_offset;
129 };
130 
131 struct radv_bvh_triangle_node {
132    float coords[3][3];
133    uint32_t reserved[3];
134    uint32_t triangle_id;
135    /* flags in upper 4 bits */
136    uint32_t geometry_id_and_flags;
137    uint32_t reserved2;
138    uint32_t id;
139 };
140 
141 struct radv_bvh_aabb_node {
142    uint32_t primitive_id;
143    /* flags in upper 4 bits */
144    uint32_t geometry_id_and_flags;
145    uint32_t reserved[14];
146 };
147 
148 struct radv_bvh_instance_node {
149    uint64_t bvh_ptr; /* pre-shifted/masked to serve as node base */
150 
151    /* lower 24 bits are the custom instance index, upper 8 bits are the visibility mask */
152    uint32_t custom_instance_and_mask;
153    /* lower 24 bits are the sbt offset, upper 8 bits are VkGeometryInstanceFlagsKHR */
154    uint32_t sbt_offset_and_flags;
155 
156    mat3x4 wto_matrix;
157 
158    uint32_t instance_id;
159    uint32_t bvh_offset;
160    uint32_t reserved[2];
161 
162    /* Object to world matrix transposed from the initial transform. */
163    mat3x4 otw_matrix;
164 };
165 
166 struct radv_bvh_box16_node {
167    uint32_t children[4];
168    float16_t coords[4][2][3];
169 };
170 
171 struct radv_bvh_box32_node {
172    uint32_t children[4];
173    radv_aabb coords[4];
174    uint32_t reserved[4];
175 };
176 
177 #define RADV_BVH_ROOT_NODE    radv_bvh_node_box32
178 #define RADV_BVH_INVALID_NODE 0xffffffffu
179 
180 /* If the task index is set to this value, there is no
181  * more work to do. */
182 #define TASK_INDEX_INVALID 0xFFFFFFFF
183 
184 struct radv_bvh_geometry_data {
185    uint64_t data;
186    uint64_t indices;
187    uint64_t transform;
188 
189    uint32_t geometry_id;
190    uint32_t geometry_type;
191    uint32_t first_id;
192    uint32_t stride;
193    uint32_t vertex_format;
194    uint32_t index_format;
195 };
196 
197 #endif /* BVH_H */
198