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 RADV_ACCELERATION_STRUCTURE_H
25#define RADV_ACCELERATION_STRUCTURE_H
26
27#include <stdint.h>
28#include <vulkan/vulkan.h>
29
30enum radv_bvh_node_type {
31   radv_bvh_node_triangle = 0,
32   radv_bvh_node_internal = 5,
33   radv_bvh_node_instance = 6,
34   radv_bvh_node_aabb = 7,
35};
36
37struct radv_accel_struct_serialization_header {
38   uint8_t driver_uuid[VK_UUID_SIZE];
39   uint8_t accel_struct_compat[VK_UUID_SIZE];
40   uint64_t serialization_size;
41   uint64_t compacted_size;
42   uint64_t instance_count;
43   uint64_t instances[];
44};
45
46struct radv_accel_struct_header {
47   uint32_t root_node_offset;
48   uint32_t reserved;
49   float aabb[2][3];
50
51   /* Everything after this gets updated/copied from the CPU. */
52   uint64_t compacted_size;
53   uint64_t serialization_size;
54   uint32_t copy_dispatch_size[3];
55   uint64_t instance_offset;
56   uint64_t instance_count;
57   uint64_t size;
58};
59
60struct radv_bvh_triangle_node {
61   float coords[3][3];
62   uint32_t reserved[3];
63   uint32_t triangle_id;
64   /* flags in upper 4 bits */
65   uint32_t geometry_id_and_flags;
66   uint32_t reserved2;
67   uint32_t id;
68};
69
70struct radv_bvh_aabb_node {
71   float aabb[2][3];
72   uint32_t primitive_id;
73   /* flags in upper 4 bits */
74   uint32_t geometry_id_and_flags;
75   uint32_t reserved[8];
76};
77
78struct radv_bvh_instance_node {
79   uint64_t base_ptr;
80   /* lower 24 bits are the custom instance index, upper 8 bits are the visibility mask */
81   uint32_t custom_instance_and_mask;
82   /* lower 24 bits are the sbt offset, upper 8 bits are VkGeometryInstanceFlagsKHR */
83   uint32_t sbt_offset_and_flags;
84
85   /* The translation component is actually a pre-translation instead of a post-translation. If you
86    * want to get a proper matrix out of it you need to apply the directional component of the
87    * matrix to it. The pre-translation of the world->object matrix is the same as the
88    * post-translation of the object->world matrix so this way we can share data between both
89    * matrices. */
90   float wto_matrix[12];
91   float aabb[2][3];
92   uint32_t instance_id;
93
94   /* Object to world matrix transposed from the initial transform. Translate part is store in the
95    * wto_matrix. */
96   float otw_matrix[9];
97};
98
99struct radv_bvh_box16_node {
100   uint32_t children[4];
101   uint32_t coords[4][3];
102};
103
104struct radv_bvh_box32_node {
105   uint32_t children[4];
106   float coords[4][2][3];
107   uint32_t reserved[4];
108};
109
110#endif