1 use crate::prelude::*; 2 use crate::vk; 3 use crate::RawPtr; 4 use crate::{Device, Instance}; 5 use std::ffi::CStr; 6 use std::mem; 7 8 #[derive(Clone)] 9 pub struct AccelerationStructure { 10 handle: vk::Device, 11 acceleration_structure_fn: vk::KhrAccelerationStructureFn, 12 } 13 14 impl AccelerationStructure { new(instance: &Instance, device: &Device) -> Self15 pub fn new(instance: &Instance, device: &Device) -> Self { 16 let acceleration_structure_fn = vk::KhrAccelerationStructureFn::load(|name| unsafe { 17 mem::transmute(instance.get_device_proc_addr(device.handle(), name.as_ptr())) 18 }); 19 Self { 20 handle: device.handle(), 21 acceleration_structure_fn, 22 } 23 } 24 get_properties( instance: &Instance, pdevice: vk::PhysicalDevice, ) -> vk::PhysicalDeviceAccelerationStructurePropertiesKHR25 pub unsafe fn get_properties( 26 instance: &Instance, 27 pdevice: vk::PhysicalDevice, 28 ) -> vk::PhysicalDeviceAccelerationStructurePropertiesKHR { 29 let mut props_rt = vk::PhysicalDeviceAccelerationStructurePropertiesKHR::default(); 30 { 31 let mut props = vk::PhysicalDeviceProperties2::builder().push_next(&mut props_rt); 32 instance.get_physical_device_properties2(pdevice, &mut props); 33 } 34 props_rt 35 } 36 37 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCreateAccelerationStructureKHR.html>"] create_acceleration_structure( &self, create_info: &vk::AccelerationStructureCreateInfoKHR, allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult<vk::AccelerationStructureKHR>38 pub unsafe fn create_acceleration_structure( 39 &self, 40 create_info: &vk::AccelerationStructureCreateInfoKHR, 41 allocation_callbacks: Option<&vk::AllocationCallbacks>, 42 ) -> VkResult<vk::AccelerationStructureKHR> { 43 let mut accel_struct = mem::zeroed(); 44 self.acceleration_structure_fn 45 .create_acceleration_structure_khr( 46 self.handle, 47 create_info, 48 allocation_callbacks.as_raw_ptr(), 49 &mut accel_struct, 50 ) 51 .result_with_success(accel_struct) 52 } 53 54 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkDestroyAccelerationStructureKHR.html>"] destroy_acceleration_structure( &self, accel_struct: vk::AccelerationStructureKHR, allocation_callbacks: Option<&vk::AllocationCallbacks>, )55 pub unsafe fn destroy_acceleration_structure( 56 &self, 57 accel_struct: vk::AccelerationStructureKHR, 58 allocation_callbacks: Option<&vk::AllocationCallbacks>, 59 ) { 60 self.acceleration_structure_fn 61 .destroy_acceleration_structure_khr( 62 self.handle, 63 accel_struct, 64 allocation_callbacks.as_raw_ptr(), 65 ); 66 } 67 68 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCmdBuildAccelerationStructuresKHR.html>"] cmd_build_acceleration_structures( &self, command_buffer: vk::CommandBuffer, infos: &[vk::AccelerationStructureBuildGeometryInfoKHR], build_range_infos: &[&[vk::AccelerationStructureBuildRangeInfoKHR]], )69 pub unsafe fn cmd_build_acceleration_structures( 70 &self, 71 command_buffer: vk::CommandBuffer, 72 infos: &[vk::AccelerationStructureBuildGeometryInfoKHR], 73 build_range_infos: &[&[vk::AccelerationStructureBuildRangeInfoKHR]], 74 ) { 75 assert_eq!(infos.len(), build_range_infos.len()); 76 77 let build_range_infos = build_range_infos 78 .iter() 79 .zip(infos.iter()) 80 .map(|(range_info, info)| { 81 assert_eq!(range_info.len(), info.geometry_count as usize); 82 range_info.as_ptr() 83 }) 84 .collect::<Vec<_>>(); 85 86 self.acceleration_structure_fn 87 .cmd_build_acceleration_structures_khr( 88 command_buffer, 89 infos.len() as _, 90 infos.as_ptr(), 91 build_range_infos.as_ptr(), 92 ); 93 } 94 95 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCmdBuildAccelerationStructuresIndirectKHR.html>"] cmd_build_acceleration_structures_indirect( &self, command_buffer: vk::CommandBuffer, infos: &[vk::AccelerationStructureBuildGeometryInfoKHR], indirect_device_addresses: &[vk::DeviceAddress], indirect_strides: &[u32], max_primitive_counts: &[&[u32]], )96 pub unsafe fn cmd_build_acceleration_structures_indirect( 97 &self, 98 command_buffer: vk::CommandBuffer, 99 infos: &[vk::AccelerationStructureBuildGeometryInfoKHR], 100 indirect_device_addresses: &[vk::DeviceAddress], 101 indirect_strides: &[u32], 102 max_primitive_counts: &[&[u32]], 103 ) { 104 assert_eq!(infos.len(), indirect_device_addresses.len()); 105 assert_eq!(infos.len(), indirect_strides.len()); 106 assert_eq!(infos.len(), max_primitive_counts.len()); 107 108 let max_primitive_counts = max_primitive_counts 109 .iter() 110 .zip(infos.iter()) 111 .map(|(cnt, info)| { 112 assert_eq!(cnt.len(), info.geometry_count as usize); 113 cnt.as_ptr() 114 }) 115 .collect::<Vec<_>>(); 116 117 self.acceleration_structure_fn 118 .cmd_build_acceleration_structures_indirect_khr( 119 command_buffer, 120 infos.len() as _, 121 infos.as_ptr(), 122 indirect_device_addresses.as_ptr(), 123 indirect_strides.as_ptr(), 124 max_primitive_counts.as_ptr(), 125 ); 126 } 127 128 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkBuildAccelerationStructuresKHR.html>"] build_acceleration_structures( &self, deferred_operation: vk::DeferredOperationKHR, infos: &[vk::AccelerationStructureBuildGeometryInfoKHR], build_range_infos: &[&[vk::AccelerationStructureBuildRangeInfoKHR]], ) -> VkResult<()>129 pub unsafe fn build_acceleration_structures( 130 &self, 131 deferred_operation: vk::DeferredOperationKHR, 132 infos: &[vk::AccelerationStructureBuildGeometryInfoKHR], 133 build_range_infos: &[&[vk::AccelerationStructureBuildRangeInfoKHR]], 134 ) -> VkResult<()> { 135 assert_eq!(infos.len(), build_range_infos.len()); 136 137 let build_range_infos = build_range_infos 138 .iter() 139 .zip(infos.iter()) 140 .map(|(range_info, info)| { 141 assert_eq!(range_info.len(), info.geometry_count as usize); 142 range_info.as_ptr() 143 }) 144 .collect::<Vec<_>>(); 145 146 self.acceleration_structure_fn 147 .build_acceleration_structures_khr( 148 self.handle, 149 deferred_operation, 150 infos.len() as _, 151 infos.as_ptr(), 152 build_range_infos.as_ptr(), 153 ) 154 .into() 155 } 156 157 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCopyAccelerationStructureKHR.html>"] copy_acceleration_structure( &self, deferred_operation: vk::DeferredOperationKHR, info: &vk::CopyAccelerationStructureInfoKHR, ) -> VkResult<()>158 pub unsafe fn copy_acceleration_structure( 159 &self, 160 deferred_operation: vk::DeferredOperationKHR, 161 info: &vk::CopyAccelerationStructureInfoKHR, 162 ) -> VkResult<()> { 163 self.acceleration_structure_fn 164 .copy_acceleration_structure_khr(self.handle, deferred_operation, info as *const _) 165 .into() 166 } 167 168 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCopyAccelerationStructureToMemoryKHR.html>"] copy_acceleration_structure_to_memory( &self, deferred_operation: vk::DeferredOperationKHR, info: &vk::CopyAccelerationStructureToMemoryInfoKHR, ) -> VkResult<()>169 pub unsafe fn copy_acceleration_structure_to_memory( 170 &self, 171 deferred_operation: vk::DeferredOperationKHR, 172 info: &vk::CopyAccelerationStructureToMemoryInfoKHR, 173 ) -> VkResult<()> { 174 self.acceleration_structure_fn 175 .copy_acceleration_structure_to_memory_khr( 176 self.handle, 177 deferred_operation, 178 info as *const _, 179 ) 180 .into() 181 } 182 183 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCopyMemoryToAccelerationStructureKHR.html>"] copy_memory_to_acceleration_structure( &self, deferred_operation: vk::DeferredOperationKHR, info: &vk::CopyMemoryToAccelerationStructureInfoKHR, ) -> VkResult<()>184 pub unsafe fn copy_memory_to_acceleration_structure( 185 &self, 186 deferred_operation: vk::DeferredOperationKHR, 187 info: &vk::CopyMemoryToAccelerationStructureInfoKHR, 188 ) -> VkResult<()> { 189 self.acceleration_structure_fn 190 .copy_memory_to_acceleration_structure_khr( 191 self.handle, 192 deferred_operation, 193 info as *const _, 194 ) 195 .into() 196 } 197 198 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkWriteAccelerationStructuresPropertiesKHR.html>"] write_acceleration_structures_properties( &self, acceleration_structures: &[vk::AccelerationStructureKHR], query_type: vk::QueryType, data: &mut [u8], stride: usize, ) -> VkResult<()>199 pub unsafe fn write_acceleration_structures_properties( 200 &self, 201 acceleration_structures: &[vk::AccelerationStructureKHR], 202 query_type: vk::QueryType, 203 data: &mut [u8], 204 stride: usize, 205 ) -> VkResult<()> { 206 self.acceleration_structure_fn 207 .write_acceleration_structures_properties_khr( 208 self.handle, 209 acceleration_structures.len() as _, 210 acceleration_structures.as_ptr(), 211 query_type, 212 data.len(), 213 data.as_mut_ptr() as *mut std::ffi::c_void, 214 stride, 215 ) 216 .into() 217 } 218 219 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCmdCopyAccelerationStructureKHR.html>"] cmd_copy_acceleration_structure( &self, command_buffer: vk::CommandBuffer, info: &vk::CopyAccelerationStructureInfoKHR, )220 pub unsafe fn cmd_copy_acceleration_structure( 221 &self, 222 command_buffer: vk::CommandBuffer, 223 info: &vk::CopyAccelerationStructureInfoKHR, 224 ) { 225 self.acceleration_structure_fn 226 .cmd_copy_acceleration_structure_khr(command_buffer, info); 227 } 228 229 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCmdCopyAccelerationStructureToMemoryKHR.html>"] cmd_copy_acceleration_structure_to_memory( &self, command_buffer: vk::CommandBuffer, info: &vk::CopyAccelerationStructureToMemoryInfoKHR, )230 pub unsafe fn cmd_copy_acceleration_structure_to_memory( 231 &self, 232 command_buffer: vk::CommandBuffer, 233 info: &vk::CopyAccelerationStructureToMemoryInfoKHR, 234 ) { 235 self.acceleration_structure_fn 236 .cmd_copy_acceleration_structure_to_memory_khr(command_buffer, info as *const _); 237 } 238 239 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCmdCopyMemoryToAccelerationStructureKHR.html>"] cmd_copy_memory_to_acceleration_structure( &self, command_buffer: vk::CommandBuffer, info: &vk::CopyMemoryToAccelerationStructureInfoKHR, )240 pub unsafe fn cmd_copy_memory_to_acceleration_structure( 241 &self, 242 command_buffer: vk::CommandBuffer, 243 info: &vk::CopyMemoryToAccelerationStructureInfoKHR, 244 ) { 245 self.acceleration_structure_fn 246 .cmd_copy_memory_to_acceleration_structure_khr(command_buffer, info as *const _); 247 } 248 249 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetAccelerationStructureHandleKHR.html>"] get_acceleration_structure_device_address( &self, info: &vk::AccelerationStructureDeviceAddressInfoKHR, ) -> vk::DeviceAddress250 pub unsafe fn get_acceleration_structure_device_address( 251 &self, 252 info: &vk::AccelerationStructureDeviceAddressInfoKHR, 253 ) -> vk::DeviceAddress { 254 self.acceleration_structure_fn 255 .get_acceleration_structure_device_address_khr(self.handle, info as *const _) 256 } 257 258 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCmdWriteAccelerationStructuresPropertiesKHR.html>"] cmd_write_acceleration_structures_properties( &self, command_buffer: vk::CommandBuffer, structures: &[vk::AccelerationStructureKHR], query_type: vk::QueryType, query_pool: vk::QueryPool, first_query: u32, )259 pub unsafe fn cmd_write_acceleration_structures_properties( 260 &self, 261 command_buffer: vk::CommandBuffer, 262 structures: &[vk::AccelerationStructureKHR], 263 query_type: vk::QueryType, 264 query_pool: vk::QueryPool, 265 first_query: u32, 266 ) { 267 self.acceleration_structure_fn 268 .cmd_write_acceleration_structures_properties_khr( 269 command_buffer, 270 structures.len() as _, 271 structures.as_ptr(), 272 query_type, 273 query_pool, 274 first_query, 275 ); 276 } 277 278 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetDeviceAccelerationStructureCompatibilityKHR.html>"] get_device_acceleration_structure_compatibility( &self, version: &vk::AccelerationStructureVersionInfoKHR, ) -> vk::AccelerationStructureCompatibilityKHR279 pub unsafe fn get_device_acceleration_structure_compatibility( 280 &self, 281 version: &vk::AccelerationStructureVersionInfoKHR, 282 ) -> vk::AccelerationStructureCompatibilityKHR { 283 let mut compatibility = vk::AccelerationStructureCompatibilityKHR::default(); 284 285 self.acceleration_structure_fn 286 .get_device_acceleration_structure_compatibility_khr( 287 self.handle, 288 version, 289 &mut compatibility as *mut _, 290 ); 291 292 compatibility 293 } 294 295 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetAccelerationStructureBuildSizesKHR.html>"] get_acceleration_structure_build_sizes( &self, build_type: vk::AccelerationStructureBuildTypeKHR, build_info: &vk::AccelerationStructureBuildGeometryInfoKHR, max_primitive_counts: &[u32], ) -> vk::AccelerationStructureBuildSizesInfoKHR296 pub unsafe fn get_acceleration_structure_build_sizes( 297 &self, 298 build_type: vk::AccelerationStructureBuildTypeKHR, 299 build_info: &vk::AccelerationStructureBuildGeometryInfoKHR, 300 max_primitive_counts: &[u32], 301 ) -> vk::AccelerationStructureBuildSizesInfoKHR { 302 assert_eq!(max_primitive_counts.len(), build_info.geometry_count as _); 303 304 let mut size_info = vk::AccelerationStructureBuildSizesInfoKHR::default(); 305 306 self.acceleration_structure_fn 307 .get_acceleration_structure_build_sizes_khr( 308 self.handle, 309 build_type, 310 build_info as *const _, 311 max_primitive_counts.as_ptr(), 312 &mut size_info as *mut _, 313 ); 314 315 size_info 316 } 317 name() -> &'static CStr318 pub fn name() -> &'static CStr { 319 vk::KhrAccelerationStructureFn::name() 320 } 321 fp(&self) -> &vk::KhrAccelerationStructureFn322 pub fn fp(&self) -> &vk::KhrAccelerationStructureFn { 323 &self.acceleration_structure_fn 324 } 325 device(&self) -> vk::Device326 pub fn device(&self) -> vk::Device { 327 self.handle 328 } 329 } 330