1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2021 Bas Nieuwenhuizen
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef RADV_ACCELERATION_STRUCTURE_H
25bf215546Sopenharmony_ci#define RADV_ACCELERATION_STRUCTURE_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <stdint.h>
28bf215546Sopenharmony_ci#include <vulkan/vulkan.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_cienum radv_bvh_node_type {
31bf215546Sopenharmony_ci   radv_bvh_node_triangle = 0,
32bf215546Sopenharmony_ci   radv_bvh_node_internal = 5,
33bf215546Sopenharmony_ci   radv_bvh_node_instance = 6,
34bf215546Sopenharmony_ci   radv_bvh_node_aabb = 7,
35bf215546Sopenharmony_ci};
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistruct radv_accel_struct_serialization_header {
38bf215546Sopenharmony_ci   uint8_t driver_uuid[VK_UUID_SIZE];
39bf215546Sopenharmony_ci   uint8_t accel_struct_compat[VK_UUID_SIZE];
40bf215546Sopenharmony_ci   uint64_t serialization_size;
41bf215546Sopenharmony_ci   uint64_t compacted_size;
42bf215546Sopenharmony_ci   uint64_t instance_count;
43bf215546Sopenharmony_ci   uint64_t instances[];
44bf215546Sopenharmony_ci};
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_cistruct radv_accel_struct_header {
47bf215546Sopenharmony_ci   uint32_t root_node_offset;
48bf215546Sopenharmony_ci   uint32_t reserved;
49bf215546Sopenharmony_ci   float aabb[2][3];
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   /* Everything after this gets updated/copied from the CPU. */
52bf215546Sopenharmony_ci   uint64_t compacted_size;
53bf215546Sopenharmony_ci   uint64_t serialization_size;
54bf215546Sopenharmony_ci   uint32_t copy_dispatch_size[3];
55bf215546Sopenharmony_ci   uint64_t instance_offset;
56bf215546Sopenharmony_ci   uint64_t instance_count;
57bf215546Sopenharmony_ci   uint64_t size;
58bf215546Sopenharmony_ci};
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_cistruct radv_bvh_triangle_node {
61bf215546Sopenharmony_ci   float coords[3][3];
62bf215546Sopenharmony_ci   uint32_t reserved[3];
63bf215546Sopenharmony_ci   uint32_t triangle_id;
64bf215546Sopenharmony_ci   /* flags in upper 4 bits */
65bf215546Sopenharmony_ci   uint32_t geometry_id_and_flags;
66bf215546Sopenharmony_ci   uint32_t reserved2;
67bf215546Sopenharmony_ci   uint32_t id;
68bf215546Sopenharmony_ci};
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_cistruct radv_bvh_aabb_node {
71bf215546Sopenharmony_ci   float aabb[2][3];
72bf215546Sopenharmony_ci   uint32_t primitive_id;
73bf215546Sopenharmony_ci   /* flags in upper 4 bits */
74bf215546Sopenharmony_ci   uint32_t geometry_id_and_flags;
75bf215546Sopenharmony_ci   uint32_t reserved[8];
76bf215546Sopenharmony_ci};
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_cistruct radv_bvh_instance_node {
79bf215546Sopenharmony_ci   uint64_t base_ptr;
80bf215546Sopenharmony_ci   /* lower 24 bits are the custom instance index, upper 8 bits are the visibility mask */
81bf215546Sopenharmony_ci   uint32_t custom_instance_and_mask;
82bf215546Sopenharmony_ci   /* lower 24 bits are the sbt offset, upper 8 bits are VkGeometryInstanceFlagsKHR */
83bf215546Sopenharmony_ci   uint32_t sbt_offset_and_flags;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   /* The translation component is actually a pre-translation instead of a post-translation. If you
86bf215546Sopenharmony_ci    * want to get a proper matrix out of it you need to apply the directional component of the
87bf215546Sopenharmony_ci    * matrix to it. The pre-translation of the world->object matrix is the same as the
88bf215546Sopenharmony_ci    * post-translation of the object->world matrix so this way we can share data between both
89bf215546Sopenharmony_ci    * matrices. */
90bf215546Sopenharmony_ci   float wto_matrix[12];
91bf215546Sopenharmony_ci   float aabb[2][3];
92bf215546Sopenharmony_ci   uint32_t instance_id;
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   /* Object to world matrix transposed from the initial transform. Translate part is store in the
95bf215546Sopenharmony_ci    * wto_matrix. */
96bf215546Sopenharmony_ci   float otw_matrix[9];
97bf215546Sopenharmony_ci};
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_cistruct radv_bvh_box16_node {
100bf215546Sopenharmony_ci   uint32_t children[4];
101bf215546Sopenharmony_ci   uint32_t coords[4][3];
102bf215546Sopenharmony_ci};
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_cistruct radv_bvh_box32_node {
105bf215546Sopenharmony_ci   uint32_t children[4];
106bf215546Sopenharmony_ci   float coords[4][2][3];
107bf215546Sopenharmony_ci   uint32_t reserved[4];
108bf215546Sopenharmony_ci};
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci#endif