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