xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/lavapipe/lvp_acceleration_structure.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 
2 /*
3  * Copyright © 2021 Google
4  * Copyright © 2023 Valve Corporation
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #ifndef LVP_ACCELERATION_STRUCTURE_H
9 #define LVP_ACCELERATION_STRUCTURE_H
10 
11 #include "lvp_private.h"
12 
13 #define LVP_GEOMETRY_OPAQUE (1u << 31)
14 
15 #define LVP_INSTANCE_FORCE_OPAQUE                 (1u << 31)
16 #define LVP_INSTANCE_NO_FORCE_NOT_OPAQUE          (1u << 30)
17 #define LVP_INSTANCE_TRIANGLE_FACING_CULL_DISABLE (1u << 29)
18 #define LVP_INSTANCE_TRIANGLE_FLIP_FACING         (1u << 28)
19 
20 #define lvp_bvh_node_triangle 0
21 #define lvp_bvh_node_internal 1
22 #define lvp_bvh_node_instance 2
23 #define lvp_bvh_node_aabb     3
24 
25 typedef struct {
26    float values[3][4];
27 } lvp_mat3x4;
28 
29 typedef struct {
30    float x;
31    float y;
32    float z;
33 } lvp_vec3;
34 
35 typedef struct lvp_aabb {
36    lvp_vec3 min;
37    lvp_vec3 max;
38 } lvp_aabb;
39 
40 struct lvp_bvh_triangle_node {
41    float coords[3][3];
42 
43    uint32_t padding;
44 
45    uint32_t primitive_id;
46    /* flags in upper 4 bits */
47    uint32_t geometry_id_and_flags;
48 };
49 
50 struct lvp_bvh_aabb_node {
51    lvp_aabb bounds;
52 
53    uint32_t primitive_id;
54    /* flags in upper 4 bits */
55    uint32_t geometry_id_and_flags;
56 };
57 
58 struct lvp_bvh_instance_node {
59    uint64_t bvh_ptr;
60 
61    /* lower 24 bits are the custom instance index, upper 8 bits are the visibility mask */
62    uint32_t custom_instance_and_mask;
63    /* lower 24 bits are the sbt offset, upper 8 bits are VkGeometryInstanceFlagsKHR */
64    uint32_t sbt_offset_and_flags;
65 
66    lvp_mat3x4 wto_matrix;
67    uint32_t padding;
68 
69    uint32_t instance_id;
70 
71    /* Object to world matrix transposed from the initial transform. */
72    lvp_mat3x4 otw_matrix;
73 };
74 
75 struct lvp_bvh_box_node {
76    lvp_aabb bounds[2];
77    uint32_t children[2];
78 };
79 
80 struct lvp_bvh_header {
81    lvp_aabb bounds;
82 
83    uint32_t serialization_size;
84    uint32_t instance_count;
85    uint32_t leaf_nodes_offset;
86 
87    uint32_t padding;
88 };
89 
90 struct lvp_accel_struct_serialization_header {
91    uint8_t driver_uuid[VK_UUID_SIZE];
92    uint8_t accel_struct_compat[VK_UUID_SIZE];
93    uint64_t serialization_size;
94    uint64_t compacted_size;
95    uint64_t instance_count;
96    uint64_t instances[];
97 };
98 
99 /* The root node is the first node after the header. */
100 #define LVP_BVH_ROOT_NODE_OFFSET (sizeof(struct lvp_bvh_header))
101 #define LVP_BVH_ROOT_NODE        (LVP_BVH_ROOT_NODE_OFFSET | lvp_bvh_node_internal)
102 #define LVP_BVH_INVALID_NODE     0xFFFFFFFF
103 
104 void
105 lvp_build_acceleration_structure(VkAccelerationStructureBuildGeometryInfoKHR *info,
106                                  const VkAccelerationStructureBuildRangeInfoKHR *ranges);
107 
108 #endif
109