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 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 enum vk_acceleration_structure_build_step {
36 VK_ACCELERATION_STRUCTURE_BUILD_STEP_TOP,
37 VK_ACCELERATION_STRUCTURE_BUILD_STEP_BUILD_LEAVES,
38 VK_ACCELERATION_STRUCTURE_BUILD_STEP_MORTON_GENERATE,
39 VK_ACCELERATION_STRUCTURE_BUILD_STEP_MORTON_SORT,
40 VK_ACCELERATION_STRUCTURE_BUILD_STEP_LBVH_BUILD_INTERNAL,
41 VK_ACCELERATION_STRUCTURE_BUILD_STEP_PLOC_BUILD_INTERNAL,
42 VK_ACCELERATION_STRUCTURE_BUILD_STEP_ENCODE,
43 };
44
45 struct vk_acceleration_structure {
46 struct vk_object_base base;
47
48 VkBuffer buffer;
49 uint64_t offset;
50 uint64_t size;
51 };
52
53 VkDeviceAddress vk_acceleration_structure_get_va(struct vk_acceleration_structure *accel_struct);
54
55 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_acceleration_structure, base, VkAccelerationStructureKHR,
56 VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR)
57
58 #define MAX_ENCODE_PASSES 2
59 #define MAX_UPDATE_PASSES 2
60
61 struct vk_acceleration_structure_build_ops {
62 void (*begin_debug_marker)(VkCommandBuffer commandBuffer,
63 enum vk_acceleration_structure_build_step step,
64 const char *format, ...);
65 void (*end_debug_marker)(VkCommandBuffer commandBuffer);
66 VkDeviceSize (*get_as_size)(VkDevice device,
67 const VkAccelerationStructureBuildGeometryInfoKHR *pBuildInfo,
68 uint32_t leaf_count);
69 VkDeviceSize (*get_update_scratch_size)(struct vk_device *device, uint32_t leaf_count);
70 uint32_t (*get_encode_key[MAX_ENCODE_PASSES])(VkAccelerationStructureTypeKHR type,
71 VkBuildAccelerationStructureFlagBitsKHR flags);
72 VkResult (*encode_bind_pipeline[MAX_ENCODE_PASSES])(VkCommandBuffer cmd_buffer,
73 uint32_t key);
74 void (*encode_as[MAX_ENCODE_PASSES])(VkCommandBuffer cmd_buffer,
75 const VkAccelerationStructureBuildGeometryInfoKHR *build_info,
76 const VkAccelerationStructureBuildRangeInfoKHR *build_range_infos,
77 VkDeviceAddress intermediate_as_addr,
78 VkDeviceAddress intermediate_header_addr,
79 uint32_t leaf_count,
80 uint32_t key,
81 struct vk_acceleration_structure *dst);
82 void (*init_update_scratch)(VkCommandBuffer cmd_buffer,
83 VkDeviceAddress scratch,
84 uint32_t leaf_count,
85 struct vk_acceleration_structure *src_as,
86 struct vk_acceleration_structure *dst_as);
87 void (*update_bind_pipeline[MAX_ENCODE_PASSES])(VkCommandBuffer cmd_buffer);
88 void (*update_as[MAX_ENCODE_PASSES])(VkCommandBuffer cmd_buffer,
89 const VkAccelerationStructureBuildGeometryInfoKHR *build_info,
90 const VkAccelerationStructureBuildRangeInfoKHR *build_range_infos,
91 uint32_t leaf_count,
92 struct vk_acceleration_structure *src,
93 struct vk_acceleration_structure *dst);
94 };
95
96 struct vk_acceleration_structure_build_args {
97 uint32_t subgroup_size;
98 uint32_t bvh_bounds_offset;
99 bool emit_markers;
100 const radix_sort_vk_t *radix_sort;
101 };
102
103 struct vk_meta_device;
104
105 void vk_cmd_build_acceleration_structures(VkCommandBuffer cmdbuf,
106 struct vk_device *device,
107 struct vk_meta_device *meta,
108 uint32_t info_count,
109 const VkAccelerationStructureBuildGeometryInfoKHR *pInfos,
110 const VkAccelerationStructureBuildRangeInfoKHR *const *ppBuildRangeInfos,
111 const struct vk_acceleration_structure_build_args *args);
112
113 void vk_get_as_build_sizes(VkDevice _device, VkAccelerationStructureBuildTypeKHR buildType,
114 const VkAccelerationStructureBuildGeometryInfoKHR *pBuildInfo,
115 const uint32_t *pMaxPrimitiveCounts,
116 VkAccelerationStructureBuildSizesInfoKHR *pSizeInfo,
117 const struct vk_acceleration_structure_build_args *args);
118
119 bool vk_acceleration_struct_vtx_format_supported(VkFormat format);
120
121 static inline VkGeometryTypeKHR
vk_get_as_geometry_type(const VkAccelerationStructureBuildGeometryInfoKHR * build_info)122 vk_get_as_geometry_type(const VkAccelerationStructureBuildGeometryInfoKHR *build_info)
123 {
124 if (build_info->geometryCount) {
125 if (build_info->pGeometries)
126 return build_info->pGeometries[0].geometryType;
127 else
128 return build_info->ppGeometries[0]->geometryType;
129 }
130
131 /* If there are no geometries, the geometry type shouldn't matter, but
132 * return something.
133 */
134 return VK_GEOMETRY_TYPE_TRIANGLES_KHR;
135 }
136
137 struct vk_bvh_geometry_data
138 vk_fill_geometry_data(VkAccelerationStructureTypeKHR type, uint32_t first_id, uint32_t geom_index,
139 const VkAccelerationStructureGeometryKHR *geometry,
140 const VkAccelerationStructureBuildRangeInfoKHR *build_range_info);
141
142 void vk_accel_struct_cmd_begin_debug_marker(VkCommandBuffer commandBuffer,
143 enum vk_acceleration_structure_build_step step,
144 const char *format, ...);
145
146 void vk_accel_struct_cmd_end_debug_marker(VkCommandBuffer commandBuffer);
147
148 #ifdef __cplusplus
149 }
150 #endif
151
152 #endif
153