1 /* 2 * Copyright © 2021 Bas Nieuwenhuizen 3 * Copyright © 2024 Valve Corporation 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 * IN THE SOFTWARE. 23 */ 24 25 #ifndef TU_BVH_H 26 #define TU_BVH_H 27 28 #ifdef VULKAN 29 #define VK_UUID_SIZE 16 30 #else 31 #include <vulkan/vulkan.h> 32 #endif 33 34 #include "vk_bvh.h" 35 36 /* The size in bytes of each record in the D3D-style UAV descriptor for 37 * acceleration structures. The first record is the acceleration struct header 38 * and the rest are the instances. 39 */ 40 #define AS_RECORD_SIZE 128 41 42 /* The size of a BVH node as defined by the HW. */ 43 #define AS_NODE_SIZE 64 44 45 struct tu_accel_struct_header { 46 vk_aabb aabb; 47 48 uint64_t bvh_ptr; 49 50 /* This word contains flags that should be set in the leaf nodes for 51 * instances pointing to this BLAS. ALL_NODES_{OPAQUE_NONOPAQUE} may be 52 * modified by the FORCE_OPAQUE and FORCE_NON_OPAQUE instance flags. 53 */ 54 uint32_t instance_flags; 55 56 /* Everything after this gets either updated/copied from the CPU or written by header.comp. */ 57 uint32_t copy_dispatch_size[3]; 58 59 uint64_t compacted_size; 60 uint64_t serialization_size; 61 uint64_t size; 62 63 /* Everything after this gets updated/copied from the CPU. */ 64 uint64_t instance_count; 65 66 uint64_t self_ptr; 67 68 uint32_t padding[10]; 69 }; 70 71 /* See 72 * https://gitlab.freedesktop.org/freedreno/freedreno/-/wikis/a7xx-ray-tracing 73 * for details of the encoding. 74 */ 75 76 #define TU_NODE_TYPE_TLAS (1u << 24) 77 #define TU_NODE_TYPE_LEAF (1u << 25) 78 #define TU_NODE_TYPE_NONOPAQUE (1u << 26) 79 #define TU_NODE_TYPE_AABB (1u << 27) 80 81 #define TU_INTERSECTION_TYPE_TLAS (1u << 8) 82 #define TU_INTERSECTION_TYPE_LEAF (1u << 9) 83 #define TU_INTERSECTION_TYPE_NONOPAQUE (1u << 10) 84 #define TU_INTERSECTION_TYPE_AABB (1u << 11) 85 #define TU_INTERSECTION_BACK_FACE (1u << 12) 86 87 #define TU_INSTANCE_ALL_OPAQUE (1u << 2) 88 #define TU_INSTANCE_ALL_NONOPAQUE (1u << 3) 89 #define TU_INSTANCE_ALL_AABB (1u << 6) 90 91 struct tu_leaf_node { 92 uint32_t id; 93 float coords[3][3]; 94 uint32_t geometry_id; /* Ignored by HW, we use it to stash the geometry ID */ 95 uint32_t padding[4]; 96 uint32_t type_flags; 97 }; 98 99 struct tu_internal_node { 100 uint32_t id; 101 uint16_t bases[3]; 102 uint8_t mantissas[8][2][3]; 103 uint8_t exponents[3]; 104 uint8_t child_count; 105 uint16_t type_flags; 106 }; 107 108 struct tu_compressed_node { 109 uint32_t id; 110 uint32_t bases[3]; 111 uint32_t data[12]; 112 }; 113 114 struct tu_instance_descriptor { 115 uint64_t bvh_ptr; 116 117 uint32_t custom_instance_index; 118 119 /* lower 24 bits are the sbt offset, upper 8 bits are the 120 * VkGeometryInstanceFlagsKHR 121 */ 122 uint32_t sbt_offset_and_flags; 123 124 mat3x4 wto_matrix; 125 126 uint32_t bvh_offset; 127 128 /* Pad to make the size a power of 2 so that addressing math is 129 * simplified. 130 */ 131 uint32_t reserved[3]; 132 133 /* Object to world matrix inverted from the initial transform. */ 134 mat3x4 otw_matrix; 135 }; 136 137 #endif 138 139