17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2021 Bas Nieuwenhuizen
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#ifndef RADV_ACCELERATION_STRUCTURE_H
257ec681f3Smrg#define RADV_ACCELERATION_STRUCTURE_H
267ec681f3Smrg
277ec681f3Smrg#include <stdint.h>
287ec681f3Smrg#include <vulkan/vulkan.h>
297ec681f3Smrg
307ec681f3Smrgstruct radv_accel_struct_serialization_header {
317ec681f3Smrg   uint8_t driver_uuid[VK_UUID_SIZE];
327ec681f3Smrg   uint8_t accel_struct_compat[VK_UUID_SIZE];
337ec681f3Smrg   uint64_t serialization_size;
347ec681f3Smrg   uint64_t compacted_size;
357ec681f3Smrg   uint64_t instance_count;
367ec681f3Smrg   uint64_t instances[];
377ec681f3Smrg};
387ec681f3Smrg
397ec681f3Smrgstruct radv_accel_struct_header {
407ec681f3Smrg   uint32_t root_node_offset;
417ec681f3Smrg   uint32_t reserved;
427ec681f3Smrg   float aabb[2][3];
437ec681f3Smrg
447ec681f3Smrg   /* Everything after this gets updated/copied from the CPU. */
457ec681f3Smrg   uint64_t compacted_size;
467ec681f3Smrg   uint64_t serialization_size;
477ec681f3Smrg   uint32_t copy_dispatch_size[3];
487ec681f3Smrg   uint64_t instance_offset;
497ec681f3Smrg   uint64_t instance_count;
507ec681f3Smrg};
517ec681f3Smrg
527ec681f3Smrgstruct radv_bvh_triangle_node {
537ec681f3Smrg   float coords[3][3];
547ec681f3Smrg   uint32_t reserved[3];
557ec681f3Smrg   uint32_t triangle_id;
567ec681f3Smrg   /* flags in upper 4 bits */
577ec681f3Smrg   uint32_t geometry_id_and_flags;
587ec681f3Smrg   uint32_t reserved2;
597ec681f3Smrg   uint32_t id;
607ec681f3Smrg};
617ec681f3Smrg
627ec681f3Smrgstruct radv_bvh_aabb_node {
637ec681f3Smrg   float aabb[2][3];
647ec681f3Smrg   uint32_t primitive_id;
657ec681f3Smrg   /* flags in upper 4 bits */
667ec681f3Smrg   uint32_t geometry_id_and_flags;
677ec681f3Smrg   uint32_t reserved[8];
687ec681f3Smrg};
697ec681f3Smrg
707ec681f3Smrgstruct radv_bvh_instance_node {
717ec681f3Smrg   uint64_t base_ptr;
727ec681f3Smrg   /* lower 24 bits are the custom instance index, upper 8 bits are the visibility mask */
737ec681f3Smrg   uint32_t custom_instance_and_mask;
747ec681f3Smrg   /* lower 24 bits are the sbt offset, upper 8 bits are VkGeometryInstanceFlagsKHR */
757ec681f3Smrg   uint32_t sbt_offset_and_flags;
767ec681f3Smrg
777ec681f3Smrg   /* The translation component is actually a pre-translation instead of a post-translation. If you
787ec681f3Smrg    * want to get a proper matrix out of it you need to apply the directional component of the
797ec681f3Smrg    * matrix to it. The pre-translation of the world->object matrix is the same as the
807ec681f3Smrg    * post-translation of the object->world matrix so this way we can share data between both
817ec681f3Smrg    * matrices. */
827ec681f3Smrg   float wto_matrix[12];
837ec681f3Smrg   float aabb[2][3];
847ec681f3Smrg   uint32_t instance_id;
857ec681f3Smrg
867ec681f3Smrg   /* Object to world matrix transposed from the initial transform. Translate part is store in the
877ec681f3Smrg    * wto_matrix. */
887ec681f3Smrg   float otw_matrix[9];
897ec681f3Smrg};
907ec681f3Smrg
917ec681f3Smrgstruct radv_bvh_box16_node {
927ec681f3Smrg   uint32_t children[4];
937ec681f3Smrg   uint32_t coords[4][3];
947ec681f3Smrg};
957ec681f3Smrg
967ec681f3Smrgstruct radv_bvh_box32_node {
977ec681f3Smrg   uint32_t children[4];
987ec681f3Smrg   float coords[4][2][3];
997ec681f3Smrg   uint32_t reserved[4];
1007ec681f3Smrg};
1017ec681f3Smrg
1027ec681f3Smrg#endif