• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2021 Bas Nieuwenhuizen
3  * Copyright © 2023 Valve Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #ifndef VK_ACCELERATION_STRUCTURE_H
26 #define VK_ACCELERATION_STRUCTURE_H
27 
28 #include "vk_object.h"
29 #include "radix_sort/radix_sort_vk.h"
30 
31 #include "bvh/vk_bvh.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 enum vk_acceleration_structure_build_step {
38    VK_ACCELERATION_STRUCTURE_BUILD_STEP_TOP,
39    VK_ACCELERATION_STRUCTURE_BUILD_STEP_BUILD_LEAVES,
40    VK_ACCELERATION_STRUCTURE_BUILD_STEP_MORTON_GENERATE,
41    VK_ACCELERATION_STRUCTURE_BUILD_STEP_MORTON_SORT,
42    VK_ACCELERATION_STRUCTURE_BUILD_STEP_LBVH_BUILD_INTERNAL,
43    VK_ACCELERATION_STRUCTURE_BUILD_STEP_PLOC_BUILD_INTERNAL,
44    VK_ACCELERATION_STRUCTURE_BUILD_STEP_ENCODE,
45 };
46 
47 struct vk_acceleration_structure {
48    struct vk_object_base base;
49 
50    VkBuffer buffer;
51    uint64_t offset;
52    uint64_t size;
53 };
54 
55 VkDeviceAddress vk_acceleration_structure_get_va(struct vk_acceleration_structure *accel_struct);
56 
57 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_acceleration_structure, base, VkAccelerationStructureKHR,
58                                VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR)
59 
60 #define MAX_ENCODE_PASSES 2
61 #define MAX_UPDATE_PASSES 2
62 
63 struct vk_acceleration_structure_build_ops {
64    void (*begin_debug_marker)(VkCommandBuffer commandBuffer,
65                               enum vk_acceleration_structure_build_step step,
66                               const char *format, ...);
67    void (*end_debug_marker)(VkCommandBuffer commandBuffer);
68    VkDeviceSize (*get_as_size)(VkDevice device,
69                                const VkAccelerationStructureBuildGeometryInfoKHR *pBuildInfo,
70                                uint32_t leaf_count);
71    VkDeviceSize (*get_update_scratch_size)(struct vk_device *device, uint32_t leaf_count);
72    uint32_t (*get_encode_key[MAX_ENCODE_PASSES])(VkAccelerationStructureTypeKHR type,
73                                                  VkBuildAccelerationStructureFlagBitsKHR flags);
74    VkResult (*encode_bind_pipeline[MAX_ENCODE_PASSES])(VkCommandBuffer cmd_buffer,
75                                                        uint32_t key);
76    void (*encode_as[MAX_ENCODE_PASSES])(VkCommandBuffer cmd_buffer,
77                                         const VkAccelerationStructureBuildGeometryInfoKHR *build_info,
78                                         const VkAccelerationStructureBuildRangeInfoKHR *build_range_infos,
79                                         VkDeviceAddress intermediate_as_addr,
80                                         VkDeviceAddress intermediate_header_addr,
81                                         uint32_t leaf_count,
82                                         uint32_t key,
83                                         struct vk_acceleration_structure *dst);
84    void (*init_update_scratch)(VkCommandBuffer cmd_buffer,
85                                VkDeviceAddress scratch,
86                                uint32_t leaf_count,
87                                struct vk_acceleration_structure *src_as,
88                                struct vk_acceleration_structure *dst_as);
89    void (*update_bind_pipeline[MAX_ENCODE_PASSES])(VkCommandBuffer cmd_buffer);
90    void (*update_as[MAX_ENCODE_PASSES])(VkCommandBuffer cmd_buffer,
91                                         const VkAccelerationStructureBuildGeometryInfoKHR *build_info,
92                                         const VkAccelerationStructureBuildRangeInfoKHR *build_range_infos,
93                                         uint32_t leaf_count,
94                                         struct vk_acceleration_structure *src,
95                                         struct vk_acceleration_structure *dst);
96 };
97 
98 struct vk_acceleration_structure_build_args {
99    uint32_t subgroup_size;
100    uint32_t bvh_bounds_offset;
101    bool emit_markers;
102    const radix_sort_vk_t *radix_sort;
103 };
104 
105 struct vk_meta_device;
106 
107 void vk_cmd_build_acceleration_structures(VkCommandBuffer cmdbuf,
108                                           struct vk_device *device,
109                                           struct vk_meta_device *meta,
110                                           uint32_t info_count,
111                                           const VkAccelerationStructureBuildGeometryInfoKHR *pInfos,
112                                           const VkAccelerationStructureBuildRangeInfoKHR *const *ppBuildRangeInfos,
113                                           const struct vk_acceleration_structure_build_args *args);
114 
115 void vk_get_as_build_sizes(VkDevice _device, VkAccelerationStructureBuildTypeKHR buildType,
116                            const VkAccelerationStructureBuildGeometryInfoKHR *pBuildInfo,
117                            const uint32_t *pMaxPrimitiveCounts,
118                            VkAccelerationStructureBuildSizesInfoKHR *pSizeInfo,
119                            const struct vk_acceleration_structure_build_args *args);
120 
121 bool vk_acceleration_struct_vtx_format_supported(VkFormat format);
122 
123 static inline VkGeometryTypeKHR
vk_get_as_geometry_type(const VkAccelerationStructureBuildGeometryInfoKHR * build_info)124 vk_get_as_geometry_type(const VkAccelerationStructureBuildGeometryInfoKHR *build_info)
125 {
126    if (build_info->geometryCount) {
127       if (build_info->pGeometries)
128          return build_info->pGeometries[0].geometryType;
129       else
130          return build_info->ppGeometries[0]->geometryType;
131    }
132 
133    /* If there are no geometries, the geometry type shouldn't matter, but
134     * return something.
135     */
136    return VK_GEOMETRY_TYPE_TRIANGLES_KHR;
137 }
138 
139 struct vk_bvh_geometry_data
140 vk_fill_geometry_data(VkAccelerationStructureTypeKHR type, uint32_t first_id, uint32_t geom_index,
141                       const VkAccelerationStructureGeometryKHR *geometry,
142                       const VkAccelerationStructureBuildRangeInfoKHR *build_range_info);
143 
144 void vk_accel_struct_cmd_begin_debug_marker(VkCommandBuffer commandBuffer,
145                                             enum vk_acceleration_structure_build_step step,
146                                             const char *format, ...);
147 
148 void vk_accel_struct_cmd_end_debug_marker(VkCommandBuffer commandBuffer);
149 
150 #ifdef __cplusplus
151 }
152 #endif
153 
154 #endif
155