• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_VK_BVH_H
25 #define BVH_VK_BVH_H
26 
27 #define vk_ir_node_triangle 0
28 #define vk_ir_node_internal 1
29 #define vk_ir_node_instance 2
30 #define vk_ir_node_aabb     3
31 
32 #define VK_GEOMETRY_OPAQUE (1u << 31)
33 
34 #ifdef VULKAN
35 #define VK_UUID_SIZE 16
36 #else
37 #include <vulkan/vulkan.h>
38 typedef struct vk_ir_node vk_ir_node;
39 typedef struct vk_global_sync_data vk_global_sync_data;
40 typedef struct vk_bvh_geometry_data vk_bvh_geometry_data;
41 
42 typedef struct {
43    float values[3][4];
44 } mat3x4;
45 
46 typedef struct {
47    float x;
48    float y;
49    float z;
50 } vec3;
51 
52 typedef struct vk_aabb vk_aabb;
53 #endif
54 
55 struct vk_aabb {
56    vec3 min;
57    vec3 max;
58 };
59 
60 /* This is the header structure for serialized acceleration structures, as
61  * defined by the Vulkan spec.
62  */
63 struct vk_accel_struct_serialization_header {
64    uint8_t driver_uuid[VK_UUID_SIZE];
65    uint8_t accel_struct_compat[VK_UUID_SIZE];
66    uint64_t serialization_size;
67    uint64_t deserialization_size;
68    uint64_t instance_count;
69 #ifndef VULKAN
70    uint64_t instances[];
71 #endif
72 };
73 
74 struct vk_global_sync_data {
75    uint32_t task_counts[2];
76    uint32_t task_started_counter;
77    uint32_t task_done_counter;
78    uint32_t current_phase_start_counter;
79    uint32_t current_phase_end_counter;
80    uint32_t phase_index;
81    /* If this flag is set, the shader should exit
82     * instead of executing another phase */
83    uint32_t next_phase_exit_flag;
84 };
85 
86 struct vk_ir_header {
87    int32_t min_bounds[3];
88    int32_t max_bounds[3];
89    uint32_t active_leaf_count;
90    /* Indirect dispatch dimensions for the encoder.
91     * ir_internal_node_count is the thread count in the X dimension,
92     * while Y and Z are always set to 1. */
93    uint32_t ir_internal_node_count;
94    uint32_t dispatch_size_y;
95    uint32_t dispatch_size_z;
96    vk_global_sync_data sync_data;
97    uint32_t dst_node_offset;
98 };
99 
100 struct vk_ir_node {
101    vk_aabb aabb;
102 };
103 
104 #define VK_UNKNOWN_BVH_OFFSET 0xFFFFFFFF
105 #define VK_NULL_BVH_OFFSET    0xFFFFFFFE
106 
107 struct vk_ir_box_node {
108    vk_ir_node base;
109    uint32_t children[2];
110    uint32_t bvh_offset;
111 };
112 
113 struct vk_ir_aabb_node {
114    vk_ir_node base;
115    uint32_t primitive_id;
116    uint32_t geometry_id_and_flags;
117 };
118 
119 struct vk_ir_triangle_node {
120    vk_ir_node base;
121    float coords[3][3];
122    uint32_t triangle_id;
123    uint32_t id;
124    uint32_t geometry_id_and_flags;
125 };
126 
127 struct vk_ir_instance_node {
128    vk_ir_node base;
129    /* See radv_bvh_instance_node */
130    uint64_t base_ptr;
131    uint32_t custom_instance_and_mask;
132    uint32_t sbt_offset_and_flags;
133    mat3x4 otw_matrix;
134    uint32_t instance_id;
135 };
136 
137 #define VK_BVH_INVALID_NODE 0xFFFFFFFF
138 
139 /* If the task index is set to this value, there is no
140  * more work to do. */
141 #define TASK_INDEX_INVALID 0xFFFFFFFF
142 
143 struct vk_bvh_geometry_data {
144    uint64_t data;
145    uint64_t indices;
146    uint64_t transform;
147 
148    uint32_t geometry_id;
149    uint32_t geometry_type;
150    uint32_t first_id;
151    uint32_t stride;
152    uint32_t vertex_format;
153    uint32_t index_format;
154 };
155 
156 #endif
157