From 0a962b42cc8efc961c16799952b8af296d511eeb Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sat, 9 Feb 2019 21:46:51 +0100 Subject: [PATCH 01/25] Implemented NV RayTracing wrapper, and wrapped create_acceleration_structure_nv --- ash/src/extensions/nv/mod.rs | 2 ++ ash/src/extensions/nv/ray_tracing.rs | 44 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 ash/src/extensions/nv/ray_tracing.rs diff --git a/ash/src/extensions/nv/mod.rs b/ash/src/extensions/nv/mod.rs index 4a8d1e5..ffc0c01 100644 --- a/ash/src/extensions/nv/mod.rs +++ b/ash/src/extensions/nv/mod.rs @@ -1,3 +1,5 @@ pub use self::mesh_shader::MeshShader; +pub use self::ray_tracing::RayTracing; mod mesh_shader; +mod ray_tracing; diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs new file mode 100644 index 0000000..03c630a --- /dev/null +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -0,0 +1,44 @@ +#![allow(dead_code)] +use prelude::*; +use std::ffi::CStr; +use std::mem; +use version::{DeviceV1_0, InstanceV1_0}; +use vk; +use RawPtr; + +#[derive(Clone)] +pub struct RayTracing { + ray_tracing_fn: vk::NvRayTracingFn, +} + +impl RayTracing { + pub fn new(instance: &I, device: &D) -> RayTracing { + let ray_tracing_fn = vk::NvRayTracingFn::load(|name| unsafe { + mem::transmute(instance.get_device_proc_addr(device.handle(), name.as_ptr())) + }); + RayTracing { ray_tracing_fn } + } + + pub unsafe fn create_acceleration_structure( + &self, + device: vk::Device, + create_info: &vk::AccelerationStructureCreateInfoNV, + allocation_callbacks: Option<&vk::AllocationCallbacks>, + ) -> VkResult { + let mut accel_struct = mem::uninitialized(); + let err_code = self.ray_tracing_fn.create_acceleration_structure_nv( + device, + create_info, + allocation_callbacks.as_raw_ptr(), + &mut accel_struct, + ); + match err_code { + vk::Result::SUCCESS => Ok(accel_struct), + _ => Err(err_code), + } + } + + pub fn name() -> &'static CStr { + CStr::from_bytes_with_nul(b"VK_NV_ray_tracing\0").expect("Wrong extension string") + } +} \ No newline at end of file From bff6f049eae0ed5ce843c89f5142708b077559ba Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 02:57:42 +0100 Subject: [PATCH 02/25] Implemented destroy_acceleration_structure --- ash/src/extensions/nv/ray_tracing.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index 03c630a..76c400e 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -38,6 +38,15 @@ impl RayTracing { } } + pub unsafe fn destroy_acceleration_structure( + &self, + device: vk::Device, + accel_struct: vk::AccelerationStructureNV, + allocation_callbacks: Option<&vk::AllocationCallbacks>, + ) { + self.ray_tracing_fn.destroy_acceleration_structure_nv(device, accel_struct, allocation_callbacks.as_raw_ptr()); + } + pub fn name() -> &'static CStr { CStr::from_bytes_with_nul(b"VK_NV_ray_tracing\0").expect("Wrong extension string") } From 24da3bb44661121a465303cb7b4467d9482134a0 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 03:03:25 +0100 Subject: [PATCH 03/25] Implemented get_acceleration_structure_memory_requirements --- ash/src/extensions/nv/ray_tracing.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index 76c400e..4e99b78 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -47,6 +47,16 @@ impl RayTracing { self.ray_tracing_fn.destroy_acceleration_structure_nv(device, accel_struct, allocation_callbacks.as_raw_ptr()); } + pub unsafe fn get_acceleration_structure_memory_requirements( + &self, + device: vk::Device, + info: &vk::AccelerationStructureMemoryRequirementsInfoNV, + ) -> vk::MemoryRequirements2KHR { + let mut requirements = mem::uninitialized(); + self.ray_tracing_fn.get_acceleration_structure_memory_requirements_nv(device, info, &mut requirements); + requirements + } + pub fn name() -> &'static CStr { CStr::from_bytes_with_nul(b"VK_NV_ray_tracing\0").expect("Wrong extension string") } From 1213e92687a0412d092a64c434e54f98a4b2f473 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 03:12:00 +0100 Subject: [PATCH 04/25] Rust format --- ash/src/extensions/nv/ray_tracing.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index 4e99b78..d7ef0ec 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -44,7 +44,11 @@ impl RayTracing { accel_struct: vk::AccelerationStructureNV, allocation_callbacks: Option<&vk::AllocationCallbacks>, ) { - self.ray_tracing_fn.destroy_acceleration_structure_nv(device, accel_struct, allocation_callbacks.as_raw_ptr()); + self.ray_tracing_fn.destroy_acceleration_structure_nv( + device, + accel_struct, + allocation_callbacks.as_raw_ptr(), + ); } pub unsafe fn get_acceleration_structure_memory_requirements( @@ -53,11 +57,12 @@ impl RayTracing { info: &vk::AccelerationStructureMemoryRequirementsInfoNV, ) -> vk::MemoryRequirements2KHR { let mut requirements = mem::uninitialized(); - self.ray_tracing_fn.get_acceleration_structure_memory_requirements_nv(device, info, &mut requirements); + self.ray_tracing_fn + .get_acceleration_structure_memory_requirements_nv(device, info, &mut requirements); requirements } pub fn name() -> &'static CStr { CStr::from_bytes_with_nul(b"VK_NV_ray_tracing\0").expect("Wrong extension string") } -} \ No newline at end of file +} From 4297ea9c61a994703aaa262ccf47cbd26d0ccd95 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 03:12:04 +0100 Subject: [PATCH 05/25] Implemented bind_acceleration_structure_memory --- ash/src/extensions/nv/ray_tracing.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index d7ef0ec..b063277 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -62,6 +62,22 @@ impl RayTracing { requirements } + pub unsafe fn bind_acceleration_structure_memory( + &self, + device: vk::Device, + bind_info: &[vk::BindAccelerationStructureMemoryInfoNV], + ) -> VkResult<()> { + let err_code = self.ray_tracing_fn.bind_acceleration_structure_memory_nv( + device, + bind_info.len() as u32, + bind_info.as_ptr(), + ); + match err_code { + vk::Result::SUCCESS => Ok(()), + _ => Err(err_code), + } + } + pub fn name() -> &'static CStr { CStr::from_bytes_with_nul(b"VK_NV_ray_tracing\0").expect("Wrong extension string") } From 04bb5de31eacf71d7933d146bed0b59a116b5894 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 03:19:37 +0100 Subject: [PATCH 06/25] Implemented compile_deferred --- ash/src/extensions/nv/ray_tracing.rs | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index b063277..cee2ed2 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -78,6 +78,35 @@ impl RayTracing { } } + // cmd_build_acceleration_structure_nv + + // cmd_copy_acceleration_structure_nv + + // cmd_trace_rays_nv + + // create_ray_tracing_pipelines_nv + + // get_ray_tracing_shader_group_handles_nv + + // get_acceleration_structure_handle_nv + + // cmd_write_acceleration_structures_properties_nv + + pub unsafe fn compile_deferred( + &self, + device: vk::Device, + pipeline: vk::Pipeline, + shader: u32, + ) -> VkResult<()> { + let err_code = self + .ray_tracing_fn + .compile_deferred_nv(device, pipeline, shader); + match err_code { + vk::Result::SUCCESS => Ok(()), + _ => Err(err_code), + } + } + pub fn name() -> &'static CStr { CStr::from_bytes_with_nul(b"VK_NV_ray_tracing\0").expect("Wrong extension string") } From 58ffae64de0d9fc4710061c3511d52843b1856d4 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 03:28:07 +0100 Subject: [PATCH 07/25] Implemented cmd_build_acceleration_structure --- ash/src/extensions/nv/ray_tracing.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index cee2ed2..4ddf1bb 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -78,7 +78,30 @@ impl RayTracing { } } - // cmd_build_acceleration_structure_nv + pub unsafe fn cmd_build_acceleration_structure( + &self, + command_buffer: vk::CommandBuffer, + info: &vk::AccelerationStructureInfoNV, + instance_data: vk::Buffer, + instance_offset: vk::DeviceSize, + update: bool, + dst: vk::AccelerationStructureNV, + src: vk::AccelerationStructureNV, + scratch: vk::Buffer, + scratch_offset: vk::DeviceSize, + ) { + self.ray_tracing_fn.cmd_build_acceleration_structure_nv( + command_buffer, + info, + instance_data, + instance_offset, + if update { vk::TRUE } else { vk::FALSE }, + dst, + src, + scratch, + scratch_offset, + ); + } // cmd_copy_acceleration_structure_nv From 5eb9f09c805bc378c1d09c458781b16da10e56eb Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 03:29:51 +0100 Subject: [PATCH 08/25] Implemented cmd_copy_acceleration_structure --- ash/src/extensions/nv/ray_tracing.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index 4ddf1bb..8fbe0e7 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -103,7 +103,16 @@ impl RayTracing { ); } - // cmd_copy_acceleration_structure_nv + pub unsafe fn cmd_copy_acceleration_structure( + &self, + command_buffer: vk::CommandBuffer, + dst: vk::AccelerationStructureNV, + src: vk::AccelerationStructureNV, + mode: vk::CopyAccelerationStructureModeNV, + ) { + self.ray_tracing_fn + .cmd_copy_acceleration_structure_nv(command_buffer, dst, src, mode); + } // cmd_trace_rays_nv From b0c554cbe58e3590d7a46c7907f529f5163e0421 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 03:33:16 +0100 Subject: [PATCH 09/25] Implemented cmd_trace_rays --- ash/src/extensions/nv/ray_tracing.rs | 37 +++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index 8fbe0e7..7a61005 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -114,7 +114,42 @@ impl RayTracing { .cmd_copy_acceleration_structure_nv(command_buffer, dst, src, mode); } - // cmd_trace_rays_nv + pub unsafe fn cmd_trace_rays( + &self, + command_buffer: vk::CommandBuffer, + raygen_shader_binding_table_buffer: vk::Buffer, + raygen_shader_binding_offset: vk::DeviceSize, + miss_shader_binding_table_buffer: vk::Buffer, + miss_shader_binding_offset: vk::DeviceSize, + miss_shader_binding_stride: vk::DeviceSize, + hit_shader_binding_table_buffer: vk::Buffer, + hit_shader_binding_offset: vk::DeviceSize, + hit_shader_binding_stride: vk::DeviceSize, + callable_shader_binding_table_buffer: vk::Buffer, + callable_shader_binding_offset: vk::DeviceSize, + callable_shader_binding_stride: vk::DeviceSize, + width: u32, + height: u32, + depth: u32, + ) { + self.ray_tracing_fn.cmd_trace_rays_nv( + command_buffer, + raygen_shader_binding_table_buffer, + raygen_shader_binding_offset, + miss_shader_binding_table_buffer, + miss_shader_binding_offset, + miss_shader_binding_stride, + hit_shader_binding_table_buffer, + hit_shader_binding_offset, + hit_shader_binding_stride, + callable_shader_binding_table_buffer, + callable_shader_binding_offset, + callable_shader_binding_stride, + width, + height, + depth, + ); + } // create_ray_tracing_pipelines_nv From ea66baff6290307adc9d9a9d19db80d7fc57e79e Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 03:38:01 +0100 Subject: [PATCH 10/25] Implemented cmd_write_acceleration_structures_properties --- ash/src/extensions/nv/ray_tracing.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index 7a61005..b9fab42 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -157,7 +157,24 @@ impl RayTracing { // get_acceleration_structure_handle_nv - // cmd_write_acceleration_structures_properties_nv + pub unsafe fn cmd_write_acceleration_structures_properties( + &self, + command_buffer: vk::CommandBuffer, + structures: &[vk::AccelerationStructureNV], + query_type: vk::QueryType, + query_pool: vk::QueryPool, + first_query: u32, + ) { + self.ray_tracing_fn + .cmd_write_acceleration_structures_properties_nv( + command_buffer, + structures.len() as u32, + structures.as_ptr(), + query_type, + query_pool, + first_query, + ); + } pub unsafe fn compile_deferred( &self, From 1c1fa85b91892a935546be9eeafc2069aea9985e Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 03:45:07 +0100 Subject: [PATCH 11/25] Implemented create_ray_tracing_pipelines --- ash/src/extensions/nv/ray_tracing.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index b9fab42..f26b38b 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -151,7 +151,27 @@ impl RayTracing { ); } - // create_ray_tracing_pipelines_nv + pub unsafe fn create_ray_tracing_pipelines( + &self, + device: vk::Device, + pipeline_cache: vk::PipelineCache, + create_info: &[vk::RayTracingPipelineCreateInfoNV], + allocation_callbacks: Option<&vk::AllocationCallbacks>, + ) -> VkResult> { + let mut pipelines = vec![mem::uninitialized(); create_info.len()]; + let err_code = self.ray_tracing_fn.create_ray_tracing_pipelines_nv( + device, + pipeline_cache, + create_info.len() as u32, + create_info.as_ptr(), + allocation_callbacks.as_raw_ptr(), + pipelines.as_mut_ptr(), + ); + match err_code { + vk::Result::SUCCESS => Ok(pipelines), + _ => Err(err_code), + } + } // get_ray_tracing_shader_group_handles_nv From 458a6fba2063ab6c27a86dd926d760a8af622490 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 03:51:32 +0100 Subject: [PATCH 12/25] Implemented get_ray_tracing_shader_group_handles --- ash/src/extensions/nv/ray_tracing.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index f26b38b..aabef17 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -173,7 +173,27 @@ impl RayTracing { } } - // get_ray_tracing_shader_group_handles_nv + pub unsafe fn get_ray_tracing_shader_group_handles( + &self, + device: vk::Device, + pipeline: vk::Pipeline, + first_group: u32, + group_count: u32, + data: &mut [u8], + ) -> VkResult<()> { + let err_code = self.ray_tracing_fn.get_ray_tracing_shader_group_handles_nv( + device, + pipeline, + first_group, + group_count, + data.len(), + data.as_mut_ptr() as *mut std::ffi::c_void, + ); + match err_code { + vk::Result::SUCCESS => Ok(()), + _ => Err(err_code), + } + } // get_acceleration_structure_handle_nv From c9495df12268d150be3ffff9d85538338d21a224 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 03:56:54 +0100 Subject: [PATCH 13/25] Implemented get_acceleration_structure_handle --- ash/src/extensions/nv/ray_tracing.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index aabef17..8645708 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -195,7 +195,23 @@ impl RayTracing { } } - // get_acceleration_structure_handle_nv + pub unsafe fn get_acceleration_structure_handle( + &self, + device: vk::Device, + accel_struct: vk::AccelerationStructureNV, + data: &mut [u8], + ) -> VkResult<()> { + let err_code = self.ray_tracing_fn.get_acceleration_structure_handle_nv( + device, + accel_struct, + data.len(), + data.as_mut_ptr() as *mut std::ffi::c_void, + ); + match err_code { + vk::Result::SUCCESS => Ok(()), + _ => Err(err_code), + } + } pub unsafe fn cmd_write_acceleration_structures_properties( &self, From e10bbb6298a9a508ab20897bddc819b9a460384e Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 04:33:55 +0100 Subject: [PATCH 14/25] Stub out nv_ray_tracing example --- examples/shader/nv_ray_tracing/frag.spv | Bin 0 -> 448 bytes examples/shader/nv_ray_tracing/triangle.frag | 10 + examples/shader/nv_ray_tracing/triangle.vert | 13 + examples/shader/nv_ray_tracing/vert.spv | Bin 0 -> 852 bytes examples/src/bin/nv_ray_tracing.rs | 476 +++++++++++++++++++ 5 files changed, 499 insertions(+) create mode 100644 examples/shader/nv_ray_tracing/frag.spv create mode 100644 examples/shader/nv_ray_tracing/triangle.frag create mode 100644 examples/shader/nv_ray_tracing/triangle.vert create mode 100644 examples/shader/nv_ray_tracing/vert.spv create mode 100644 examples/src/bin/nv_ray_tracing.rs diff --git a/examples/shader/nv_ray_tracing/frag.spv b/examples/shader/nv_ray_tracing/frag.spv new file mode 100644 index 0000000000000000000000000000000000000000..e1a850d82440c1021c6ff732fae6dd6eea16e628 GIT binary patch literal 448 zcmY*VO;5r=6dWl96cvbx-VDS$AqETykwi`Gr6>IXo3(5TiY;l&|M2H}F>z-72yFBE zcIM5#dAmXFstFwMYvBYxtA`pf&_=`OD1MHIWwjWNCnG6N5qP0A9W+sAR=nF)rothk z&FE4mo(9|n>Hg`|=rgR%y*vgr5X&0_A`OB+Is&Ze$(ke^M+Ggq2rp}D7 zM;n{GA!Mgr-;j*Fw~vKQAM$lxz-u&1ZW2#K-W|9krzNcVA3o!hnseYMhTora-yqj# zTu`t32+OhEDqH3C1@KM(H@sU7a2#$Ot#hs&F(8Zf*Wx^U{6@ikhsFrLmbdZ32KnlgUOMGs)UCpbJd zl=}+K!~?vkV4 = base + .present_image_views + .iter() + .map(|&present_image_view| { + let framebuffer_attachments = [present_image_view, base.depth_image_view]; + let frame_buffer_create_info = vk::FramebufferCreateInfo::builder() + .render_pass(renderpass) + .attachments(&framebuffer_attachments) + .width(base.surface_resolution.width) + .height(base.surface_resolution.height) + .layers(1); + + base.device + .create_framebuffer(&frame_buffer_create_info, None) + .unwrap() + }) + .collect(); + + let index_buffer_data = [0u32, 1, 2]; + let index_buffer_info = vk::BufferCreateInfo::builder() + .size(std::mem::size_of_val(&index_buffer_data) as u64) + .usage(vk::BufferUsageFlags::INDEX_BUFFER) + .sharing_mode(vk::SharingMode::EXCLUSIVE); + + let index_buffer = base.device.create_buffer(&index_buffer_info, None).unwrap(); + let index_buffer_memory_req = base.device.get_buffer_memory_requirements(index_buffer); + let index_buffer_memory_index = find_memorytype_index( + &index_buffer_memory_req, + &base.device_memory_properties, + vk::MemoryPropertyFlags::HOST_VISIBLE, + ) + .expect("Unable to find suitable memorytype for the index buffer."); + + let index_allocate_info = vk::MemoryAllocateInfo { + allocation_size: index_buffer_memory_req.size, + memory_type_index: index_buffer_memory_index, + ..Default::default() + }; + let index_buffer_memory = base + .device + .allocate_memory(&index_allocate_info, None) + .unwrap(); + let index_ptr = base + .device + .map_memory( + index_buffer_memory, + 0, + index_buffer_memory_req.size, + vk::MemoryMapFlags::empty(), + ) + .unwrap(); + let mut index_slice = Align::new( + index_ptr, + align_of::() as u64, + index_buffer_memory_req.size, + ); + index_slice.copy_from_slice(&index_buffer_data); + base.device.unmap_memory(index_buffer_memory); + base.device + .bind_buffer_memory(index_buffer, index_buffer_memory, 0) + .unwrap(); + + let vertex_input_buffer_info = vk::BufferCreateInfo { + size: 3 * std::mem::size_of::() as u64, + usage: vk::BufferUsageFlags::VERTEX_BUFFER, + sharing_mode: vk::SharingMode::EXCLUSIVE, + ..Default::default() + }; + + let vertex_input_buffer = base + .device + .create_buffer(&vertex_input_buffer_info, None) + .unwrap(); + + let vertex_input_buffer_memory_req = base + .device + .get_buffer_memory_requirements(vertex_input_buffer); + + let vertex_input_buffer_memory_index = find_memorytype_index( + &vertex_input_buffer_memory_req, + &base.device_memory_properties, + vk::MemoryPropertyFlags::HOST_VISIBLE, + ) + .expect("Unable to find suitable memorytype for the vertex buffer."); + + let vertex_buffer_allocate_info = vk::MemoryAllocateInfo { + allocation_size: vertex_input_buffer_memory_req.size, + memory_type_index: vertex_input_buffer_memory_index, + ..Default::default() + }; + + let vertex_input_buffer_memory = base + .device + .allocate_memory(&vertex_buffer_allocate_info, None) + .unwrap(); + + let vertices = [ + Vertex { + pos: [-1.0, 1.0, 0.0, 1.0], + color: [0.0, 1.0, 0.0, 1.0], + }, + Vertex { + pos: [1.0, 1.0, 0.0, 1.0], + color: [0.0, 0.0, 1.0, 1.0], + }, + Vertex { + pos: [0.0, -1.0, 0.0, 1.0], + color: [1.0, 0.0, 0.0, 1.0], + }, + ]; + + let vert_ptr = base + .device + .map_memory( + vertex_input_buffer_memory, + 0, + vertex_input_buffer_memory_req.size, + vk::MemoryMapFlags::empty(), + ) + .unwrap(); + + let mut vert_align = Align::new( + vert_ptr, + align_of::() as u64, + vertex_input_buffer_memory_req.size, + ); + vert_align.copy_from_slice(&vertices); + base.device.unmap_memory(vertex_input_buffer_memory); + base.device + .bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0) + .unwrap(); + let mut vertex_spv_file = + File::open(Path::new("shader/nv_ray_tracing/vert.spv")).expect("Could not find vert.spv."); + let mut frag_spv_file = + File::open(Path::new("shader/nv_ray_tracing/frag.spv")).expect("Could not find frag.spv."); + + let vertex_code = + read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file"); + let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(&vertex_code); + + let frag_code = + read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file"); + let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code); + + let vertex_shader_module = base + .device + .create_shader_module(&vertex_shader_info, None) + .expect("Vertex shader module error"); + + let fragment_shader_module = base + .device + .create_shader_module(&frag_shader_info, None) + .expect("Fragment shader module error"); + + let layout_create_info = vk::PipelineLayoutCreateInfo::default(); + + let pipeline_layout = base + .device + .create_pipeline_layout(&layout_create_info, None) + .unwrap(); + + let shader_entry_name = CString::new("main").unwrap(); + let shader_stage_create_infos = [ + vk::PipelineShaderStageCreateInfo { + module: vertex_shader_module, + p_name: shader_entry_name.as_ptr(), + stage: vk::ShaderStageFlags::VERTEX, + ..Default::default() + }, + vk::PipelineShaderStageCreateInfo { + s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO, + module: fragment_shader_module, + p_name: shader_entry_name.as_ptr(), + stage: vk::ShaderStageFlags::FRAGMENT, + ..Default::default() + }, + ]; + let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { + binding: 0, + stride: mem::size_of::() as u32, + input_rate: vk::VertexInputRate::VERTEX, + }]; + let vertex_input_attribute_descriptions = [ + vk::VertexInputAttributeDescription { + location: 0, + binding: 0, + format: vk::Format::R32G32B32A32_SFLOAT, + offset: offset_of!(Vertex, pos) as u32, + }, + vk::VertexInputAttributeDescription { + location: 1, + binding: 0, + format: vk::Format::R32G32B32A32_SFLOAT, + offset: offset_of!(Vertex, color) as u32, + }, + ]; + + let vertex_input_state_info = vk::PipelineVertexInputStateCreateInfo { + vertex_attribute_description_count: vertex_input_attribute_descriptions.len() as u32, + p_vertex_attribute_descriptions: vertex_input_attribute_descriptions.as_ptr(), + vertex_binding_description_count: vertex_input_binding_descriptions.len() as u32, + p_vertex_binding_descriptions: vertex_input_binding_descriptions.as_ptr(), + ..Default::default() + }; + let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo { + topology: vk::PrimitiveTopology::TRIANGLE_LIST, + ..Default::default() + }; + let viewports = [vk::Viewport { + x: 0.0, + y: 0.0, + width: base.surface_resolution.width as f32, + height: base.surface_resolution.height as f32, + min_depth: 0.0, + max_depth: 1.0, + }]; + let scissors = [vk::Rect2D { + offset: vk::Offset2D { x: 0, y: 0 }, + extent: base.surface_resolution.clone(), + }]; + let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder() + .scissors(&scissors) + .viewports(&viewports); + + let rasterization_info = vk::PipelineRasterizationStateCreateInfo { + front_face: vk::FrontFace::COUNTER_CLOCKWISE, + line_width: 1.0, + polygon_mode: vk::PolygonMode::FILL, + ..Default::default() + }; + let multisample_state_info = vk::PipelineMultisampleStateCreateInfo { + rasterization_samples: vk::SampleCountFlags::TYPE_1, + ..Default::default() + }; + let noop_stencil_state = vk::StencilOpState { + fail_op: vk::StencilOp::KEEP, + pass_op: vk::StencilOp::KEEP, + depth_fail_op: vk::StencilOp::KEEP, + compare_op: vk::CompareOp::ALWAYS, + ..Default::default() + }; + let depth_state_info = vk::PipelineDepthStencilStateCreateInfo { + depth_test_enable: 1, + depth_write_enable: 1, + depth_compare_op: vk::CompareOp::LESS_OR_EQUAL, + front: noop_stencil_state.clone(), + back: noop_stencil_state.clone(), + max_depth_bounds: 1.0, + ..Default::default() + }; + let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { + blend_enable: 0, + src_color_blend_factor: vk::BlendFactor::SRC_COLOR, + dst_color_blend_factor: vk::BlendFactor::ONE_MINUS_DST_COLOR, + color_blend_op: vk::BlendOp::ADD, + src_alpha_blend_factor: vk::BlendFactor::ZERO, + dst_alpha_blend_factor: vk::BlendFactor::ZERO, + alpha_blend_op: vk::BlendOp::ADD, + color_write_mask: vk::ColorComponentFlags::all(), + }]; + let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder() + .logic_op(vk::LogicOp::CLEAR) + .attachments(&color_blend_attachment_states); + + let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]; + let dynamic_state_info = + vk::PipelineDynamicStateCreateInfo::builder().dynamic_states(&dynamic_state); + + let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() + .stages(&shader_stage_create_infos) + .vertex_input_state(&vertex_input_state_info) + .input_assembly_state(&vertex_input_assembly_state_info) + .viewport_state(&viewport_state_info) + .rasterization_state(&rasterization_info) + .multisample_state(&multisample_state_info) + .depth_stencil_state(&depth_state_info) + .color_blend_state(&color_blend_state) + .dynamic_state(&dynamic_state_info) + .layout(pipeline_layout) + .render_pass(renderpass); + + let graphics_pipelines = base + .device + .create_graphics_pipelines( + vk::PipelineCache::null(), + &[graphic_pipeline_info.build()], + None, + ) + .expect("Unable to create graphics pipeline"); + + let graphic_pipeline = graphics_pipelines[0]; + + base.render_loop(|| { + let (present_index, _) = base + .swapchain_loader + .acquire_next_image( + base.swapchain, + std::u64::MAX, + base.present_complete_semaphore, + vk::Fence::null(), + ) + .unwrap(); + let clear_values = [ + vk::ClearValue { + color: vk::ClearColorValue { + float32: [0.0, 0.0, 0.0, 0.0], + }, + }, + vk::ClearValue { + depth_stencil: vk::ClearDepthStencilValue { + depth: 1.0, + stencil: 0, + }, + }, + ]; + + let render_pass_begin_info = vk::RenderPassBeginInfo::builder() + .render_pass(renderpass) + .framebuffer(framebuffers[present_index as usize]) + .render_area(vk::Rect2D { + offset: vk::Offset2D { x: 0, y: 0 }, + extent: base.surface_resolution.clone(), + }) + .clear_values(&clear_values); + + record_submit_commandbuffer( + &base.device, + base.draw_command_buffer, + base.present_queue, + &[vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT], + &[base.present_complete_semaphore], + &[base.rendering_complete_semaphore], + |device, draw_command_buffer| { + device.cmd_begin_render_pass( + draw_command_buffer, + &render_pass_begin_info, + vk::SubpassContents::INLINE, + ); + device.cmd_bind_pipeline( + draw_command_buffer, + vk::PipelineBindPoint::GRAPHICS, + graphic_pipeline, + ); + device.cmd_set_viewport(draw_command_buffer, 0, &viewports); + device.cmd_set_scissor(draw_command_buffer, 0, &scissors); + device.cmd_bind_vertex_buffers( + draw_command_buffer, + 0, + &[vertex_input_buffer], + &[0], + ); + device.cmd_bind_index_buffer( + draw_command_buffer, + index_buffer, + 0, + vk::IndexType::UINT32, + ); + device.cmd_draw_indexed( + draw_command_buffer, + index_buffer_data.len() as u32, + 1, + 0, + 0, + 1, + ); + // Or draw without the index buffer + // device.cmd_draw(draw_command_buffer, 3, 1, 0, 0); + device.cmd_end_render_pass(draw_command_buffer); + }, + ); + //let mut present_info_err = mem::uninitialized(); + let wait_semaphors = [base.rendering_complete_semaphore]; + let swapchains = [base.swapchain]; + let image_indices = [present_index]; + let present_info = vk::PresentInfoKHR::builder() + .wait_semaphores(&wait_semaphors) // &base.rendering_complete_semaphore) + .swapchains(&swapchains) + .image_indices(&image_indices); + + base.swapchain_loader + .queue_present(base.present_queue, &present_info) + .unwrap(); + }); + + base.device.device_wait_idle().unwrap(); + for pipeline in graphics_pipelines { + base.device.destroy_pipeline(pipeline, None); + } + base.device.destroy_pipeline_layout(pipeline_layout, None); + base.device + .destroy_shader_module(vertex_shader_module, None); + base.device + .destroy_shader_module(fragment_shader_module, None); + base.device.free_memory(index_buffer_memory, None); + base.device.destroy_buffer(index_buffer, None); + base.device.free_memory(vertex_input_buffer_memory, None); + base.device.destroy_buffer(vertex_input_buffer, None); + for framebuffer in framebuffers { + base.device.destroy_framebuffer(framebuffer, None); + } + base.device.destroy_render_pass(renderpass, None); + } +} From 9b6fa860c9def74381a552db6da31198ec56012b Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 13:37:53 +0100 Subject: [PATCH 15/25] Improved get_acceleration_structure_handle and keep local device handle in rt extension --- ash/src/extensions/nv/ray_tracing.rs | 41 +++++++++++++--------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index 8645708..95e9bf1 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -8,6 +8,7 @@ use RawPtr; #[derive(Clone)] pub struct RayTracing { + handle: vk::Device, ray_tracing_fn: vk::NvRayTracingFn, } @@ -16,18 +17,20 @@ impl RayTracing { let ray_tracing_fn = vk::NvRayTracingFn::load(|name| unsafe { mem::transmute(instance.get_device_proc_addr(device.handle(), name.as_ptr())) }); - RayTracing { ray_tracing_fn } + RayTracing { + handle: device.handle(), + ray_tracing_fn, + } } pub unsafe fn create_acceleration_structure( &self, - device: vk::Device, create_info: &vk::AccelerationStructureCreateInfoNV, allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult { let mut accel_struct = mem::uninitialized(); let err_code = self.ray_tracing_fn.create_acceleration_structure_nv( - device, + self.handle, create_info, allocation_callbacks.as_raw_ptr(), &mut accel_struct, @@ -40,12 +43,11 @@ impl RayTracing { pub unsafe fn destroy_acceleration_structure( &self, - device: vk::Device, accel_struct: vk::AccelerationStructureNV, allocation_callbacks: Option<&vk::AllocationCallbacks>, ) { self.ray_tracing_fn.destroy_acceleration_structure_nv( - device, + self.handle, accel_struct, allocation_callbacks.as_raw_ptr(), ); @@ -53,22 +55,20 @@ impl RayTracing { pub unsafe fn get_acceleration_structure_memory_requirements( &self, - device: vk::Device, info: &vk::AccelerationStructureMemoryRequirementsInfoNV, ) -> vk::MemoryRequirements2KHR { let mut requirements = mem::uninitialized(); self.ray_tracing_fn - .get_acceleration_structure_memory_requirements_nv(device, info, &mut requirements); + .get_acceleration_structure_memory_requirements_nv(self.handle, info, &mut requirements); requirements } pub unsafe fn bind_acceleration_structure_memory( &self, - device: vk::Device, bind_info: &[vk::BindAccelerationStructureMemoryInfoNV], ) -> VkResult<()> { let err_code = self.ray_tracing_fn.bind_acceleration_structure_memory_nv( - device, + self.handle, bind_info.len() as u32, bind_info.as_ptr(), ); @@ -153,14 +153,13 @@ impl RayTracing { pub unsafe fn create_ray_tracing_pipelines( &self, - device: vk::Device, pipeline_cache: vk::PipelineCache, create_info: &[vk::RayTracingPipelineCreateInfoNV], allocation_callbacks: Option<&vk::AllocationCallbacks>, ) -> VkResult> { let mut pipelines = vec![mem::uninitialized(); create_info.len()]; let err_code = self.ray_tracing_fn.create_ray_tracing_pipelines_nv( - device, + self.handle, pipeline_cache, create_info.len() as u32, create_info.as_ptr(), @@ -175,14 +174,13 @@ impl RayTracing { pub unsafe fn get_ray_tracing_shader_group_handles( &self, - device: vk::Device, pipeline: vk::Pipeline, first_group: u32, group_count: u32, data: &mut [u8], ) -> VkResult<()> { let err_code = self.ray_tracing_fn.get_ray_tracing_shader_group_handles_nv( - device, + self.handle, pipeline, first_group, group_count, @@ -197,18 +195,18 @@ impl RayTracing { pub unsafe fn get_acceleration_structure_handle( &self, - device: vk::Device, accel_struct: vk::AccelerationStructureNV, - data: &mut [u8], - ) -> VkResult<()> { + ) -> VkResult { + let mut handle: u64 = 0; + let handle_ptr: *mut u64 = &mut handle; let err_code = self.ray_tracing_fn.get_acceleration_structure_handle_nv( - device, + self.handle, accel_struct, - data.len(), - data.as_mut_ptr() as *mut std::ffi::c_void, + 8, // sizeof(u64) + handle_ptr as *mut std::ffi::c_void, ); match err_code { - vk::Result::SUCCESS => Ok(()), + vk::Result::SUCCESS => Ok(handle), _ => Err(err_code), } } @@ -234,13 +232,12 @@ impl RayTracing { pub unsafe fn compile_deferred( &self, - device: vk::Device, pipeline: vk::Pipeline, shader: u32, ) -> VkResult<()> { let err_code = self .ray_tracing_fn - .compile_deferred_nv(device, pipeline, shader); + .compile_deferred_nv(self.handle, pipeline, shader); match err_code { vk::Result::SUCCESS => Ok(()), _ => Err(err_code), From f348ddc33e281d540323f891c7463df8b8e59a0a Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 13:38:03 +0100 Subject: [PATCH 16/25] Progress on rt example --- examples/src/bin/nv_ray_tracing.rs | 99 ++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 4 deletions(-) diff --git a/examples/src/bin/nv_ray_tracing.rs b/examples/src/bin/nv_ray_tracing.rs index 3e16db1..61bd092 100644 --- a/examples/src/bin/nv_ray_tracing.rs +++ b/examples/src/bin/nv_ray_tracing.rs @@ -1,6 +1,7 @@ extern crate ash; extern crate examples; +use ash::extensions::nv::RayTracing; use ash::util::*; use ash::vk; use examples::*; @@ -17,9 +18,99 @@ struct Vertex { color: [f32; 4], } +struct AccelerationStructure { + memory: vk::DeviceMemory, + accel_struct_info: vk::AccelerationStructureInfoNV, + accel_struct: vk::AccelerationStructureNV, + handle: u64, +} + +fn create_acceleration_structure( + base: &ExampleBase, + ray_tracing: &RayTracing, + device: &ash::Device, + accel_type: vk::AccelerationStructureTypeNV, + geometry: &[vk::GeometryNV], + instance_count: u32, +) -> AccelerationStructure { + unsafe { + let accel_struct_info = vk::AccelerationStructureInfoNV::builder() + .ty(accel_type) + .flags(vk::BuildAccelerationStructureFlagsNV::PREFER_FAST_TRACE) + .geometries(geometry) + .instance_count(instance_count) + .build(); + let create_info = vk::AccelerationStructureCreateInfoNV::builder() + .info(accel_struct_info) + .compacted_size(0) + .build(); + let accel_struct = ray_tracing + .create_acceleration_structure(&create_info, None) + .unwrap(); + + let requirements_info = vk::AccelerationStructureMemoryRequirementsInfoNV::builder() + .acceleration_structure(accel_struct) + .ty(vk::AccelerationStructureMemoryRequirementsTypeNV::OBJECT) + .build(); + let requirements = + ray_tracing.get_acceleration_structure_memory_requirements(&requirements_info); + + let memory_type_index = find_memorytype_index( + &requirements.memory_requirements, + &base.device_memory_properties, + vk::MemoryPropertyFlags::DEVICE_LOCAL, + ) + .unwrap(); + + let allocate_info = vk::MemoryAllocateInfo { + allocation_size: requirements.memory_requirements.size, + memory_type_index, + ..Default::default() + }; + + let memory = device.allocate_memory(&allocate_info, None).unwrap(); + + ray_tracing + .bind_acceleration_structure_memory(&[ + vk::BindAccelerationStructureMemoryInfoNV::builder() + .acceleration_structure(accel_struct) + .memory(memory) + .memory_offset(0) + .build(), + ]) + .unwrap(); + + let handle = ray_tracing + .get_acceleration_structure_handle(accel_struct) + .unwrap(); + + AccelerationStructure { + memory, + accel_struct_info, + accel_struct, + handle, + } + } +} + +/* + + + + error = vkGetAccelerationStructureHandleNV(mDevice, _as.accelerationStructure, sizeof(uint64_t), &_as.handle); + if (VK_SUCCESS != error) { + CHECK_VK_ERROR(error, "vkGetAccelerationStructureHandleNVX"); + return false; + } + + return true; +} +*/ + fn main() { unsafe { let base = ExampleBase::new(1920, 1080); + let ray_tracing = RayTracing::new(&base.instance, &base.device); let renderpass_attachments = [ vk::AttachmentDescription { format: base.surface_format.format, @@ -202,10 +293,10 @@ fn main() { base.device .bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0) .unwrap(); - let mut vertex_spv_file = - File::open(Path::new("shader/nv_ray_tracing/vert.spv")).expect("Could not find vert.spv."); - let mut frag_spv_file = - File::open(Path::new("shader/nv_ray_tracing/frag.spv")).expect("Could not find frag.spv."); + let mut vertex_spv_file = File::open(Path::new("shader/nv_ray_tracing/vert.spv")) + .expect("Could not find vert.spv."); + let mut frag_spv_file = File::open(Path::new("shader/nv_ray_tracing/frag.spv")) + .expect("Could not find frag.spv."); let vertex_code = read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file"); From 011e9e611fa9ea79f23b9cb8a5b48c2fc1eb040d Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 23:54:44 +0100 Subject: [PATCH 17/25] Added DescriptorIndexing extension (just name for now), and also added names for VK_KHR_get_memory_requirements2 and VK_KHR_get_physical_device_properties2 (both are commonly used, and required for ray tracing) --- ash/src/extensions/ext/descriptor_indexing.rs | 10 ++++++++++ ash/src/extensions/ext/mod.rs | 12 ++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 ash/src/extensions/ext/descriptor_indexing.rs diff --git a/ash/src/extensions/ext/descriptor_indexing.rs b/ash/src/extensions/ext/descriptor_indexing.rs new file mode 100644 index 0000000..daa448b --- /dev/null +++ b/ash/src/extensions/ext/descriptor_indexing.rs @@ -0,0 +1,10 @@ +use std::ffi::CStr; + +#[derive(Clone)] +pub struct DescriptorIndexing {} + +impl DescriptorIndexing { + pub fn name() -> &'static CStr { + CStr::from_bytes_with_nul(b"VK_EXT_descriptor_indexing\0").expect("Wrong extension string") + } +} diff --git a/ash/src/extensions/ext/mod.rs b/ash/src/extensions/ext/mod.rs index eba690f..5d6bf89 100644 --- a/ash/src/extensions/ext/mod.rs +++ b/ash/src/extensions/ext/mod.rs @@ -1,7 +1,19 @@ pub use self::debug_marker::DebugMarker; pub use self::debug_report::DebugReport; pub use self::debug_utils::DebugUtils; +pub use self::descriptor_indexing::DescriptorIndexing; mod debug_marker; mod debug_report; mod debug_utils; +mod descriptor_indexing; + +pub fn memory_requirements2_name() -> &'static std::ffi::CStr { + std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_get_memory_requirements2\0") + .expect("Wrong extension string") +} + +pub fn physical_device_properties2_name() -> &'static std::ffi::CStr { + std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_get_physical_device_properties2\0") + .expect("Wrong extension string") +} From 0ed0a06634e86e77cbfd30b9e13d60ff03682071 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 23:55:31 +0100 Subject: [PATCH 18/25] Added helper on RayTracing to query physical device ray tracing properties info (such as shader group handle size) --- ash/src/extensions/nv/ray_tracing.rs | 29 ++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index 95e9bf1..48952cd 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -2,7 +2,7 @@ use prelude::*; use std::ffi::CStr; use std::mem; -use version::{DeviceV1_0, InstanceV1_0}; +use version::{DeviceV1_1, InstanceV1_1}; use vk; use RawPtr; @@ -13,7 +13,7 @@ pub struct RayTracing { } impl RayTracing { - pub fn new(instance: &I, device: &D) -> RayTracing { + pub fn new(instance: &I, device: &D) -> RayTracing { let ray_tracing_fn = vk::NvRayTracingFn::load(|name| unsafe { mem::transmute(instance.get_device_proc_addr(device.handle(), name.as_ptr())) }); @@ -23,6 +23,19 @@ impl RayTracing { } } + pub unsafe fn get_properties( + instance: &I, + pdevice: vk::PhysicalDevice, + ) -> vk::PhysicalDeviceRayTracingPropertiesNV { + let mut props_rt = vk::PhysicalDeviceRayTracingPropertiesNV::default(); + let mut props = vk::PhysicalDeviceProperties2::builder() + .next(&mut props_rt) + .build(); + + instance.get_physical_device_properties2(pdevice, &mut props); + props_rt + } + pub unsafe fn create_acceleration_structure( &self, create_info: &vk::AccelerationStructureCreateInfoNV, @@ -59,7 +72,11 @@ impl RayTracing { ) -> vk::MemoryRequirements2KHR { let mut requirements = mem::uninitialized(); self.ray_tracing_fn - .get_acceleration_structure_memory_requirements_nv(self.handle, info, &mut requirements); + .get_acceleration_structure_memory_requirements_nv( + self.handle, + info, + &mut requirements, + ); requirements } @@ -230,11 +247,7 @@ impl RayTracing { ); } - pub unsafe fn compile_deferred( - &self, - pipeline: vk::Pipeline, - shader: u32, - ) -> VkResult<()> { + pub unsafe fn compile_deferred(&self, pipeline: vk::Pipeline, shader: u32) -> VkResult<()> { let err_code = self .ray_tracing_fn .compile_deferred_nv(self.handle, pipeline, shader); From a348bec1ff8ca0e0256aca9ee17e851415d4cd7a Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 23:58:14 +0100 Subject: [PATCH 19/25] Deleted ray tracing example (keep this branch clean) --- examples/src/bin/nv_ray_tracing.rs | 567 ----------------------------- 1 file changed, 567 deletions(-) delete mode 100644 examples/src/bin/nv_ray_tracing.rs diff --git a/examples/src/bin/nv_ray_tracing.rs b/examples/src/bin/nv_ray_tracing.rs deleted file mode 100644 index 61bd092..0000000 --- a/examples/src/bin/nv_ray_tracing.rs +++ /dev/null @@ -1,567 +0,0 @@ -extern crate ash; -extern crate examples; - -use ash::extensions::nv::RayTracing; -use ash::util::*; -use ash::vk; -use examples::*; -use std::default::Default; -use std::ffi::CString; -use std::fs::File; -use std::mem; -use std::mem::align_of; -use std::path::Path; - -#[derive(Clone, Debug, Copy)] -struct Vertex { - pos: [f32; 4], - color: [f32; 4], -} - -struct AccelerationStructure { - memory: vk::DeviceMemory, - accel_struct_info: vk::AccelerationStructureInfoNV, - accel_struct: vk::AccelerationStructureNV, - handle: u64, -} - -fn create_acceleration_structure( - base: &ExampleBase, - ray_tracing: &RayTracing, - device: &ash::Device, - accel_type: vk::AccelerationStructureTypeNV, - geometry: &[vk::GeometryNV], - instance_count: u32, -) -> AccelerationStructure { - unsafe { - let accel_struct_info = vk::AccelerationStructureInfoNV::builder() - .ty(accel_type) - .flags(vk::BuildAccelerationStructureFlagsNV::PREFER_FAST_TRACE) - .geometries(geometry) - .instance_count(instance_count) - .build(); - let create_info = vk::AccelerationStructureCreateInfoNV::builder() - .info(accel_struct_info) - .compacted_size(0) - .build(); - let accel_struct = ray_tracing - .create_acceleration_structure(&create_info, None) - .unwrap(); - - let requirements_info = vk::AccelerationStructureMemoryRequirementsInfoNV::builder() - .acceleration_structure(accel_struct) - .ty(vk::AccelerationStructureMemoryRequirementsTypeNV::OBJECT) - .build(); - let requirements = - ray_tracing.get_acceleration_structure_memory_requirements(&requirements_info); - - let memory_type_index = find_memorytype_index( - &requirements.memory_requirements, - &base.device_memory_properties, - vk::MemoryPropertyFlags::DEVICE_LOCAL, - ) - .unwrap(); - - let allocate_info = vk::MemoryAllocateInfo { - allocation_size: requirements.memory_requirements.size, - memory_type_index, - ..Default::default() - }; - - let memory = device.allocate_memory(&allocate_info, None).unwrap(); - - ray_tracing - .bind_acceleration_structure_memory(&[ - vk::BindAccelerationStructureMemoryInfoNV::builder() - .acceleration_structure(accel_struct) - .memory(memory) - .memory_offset(0) - .build(), - ]) - .unwrap(); - - let handle = ray_tracing - .get_acceleration_structure_handle(accel_struct) - .unwrap(); - - AccelerationStructure { - memory, - accel_struct_info, - accel_struct, - handle, - } - } -} - -/* - - - - error = vkGetAccelerationStructureHandleNV(mDevice, _as.accelerationStructure, sizeof(uint64_t), &_as.handle); - if (VK_SUCCESS != error) { - CHECK_VK_ERROR(error, "vkGetAccelerationStructureHandleNVX"); - return false; - } - - return true; -} -*/ - -fn main() { - unsafe { - let base = ExampleBase::new(1920, 1080); - let ray_tracing = RayTracing::new(&base.instance, &base.device); - let renderpass_attachments = [ - vk::AttachmentDescription { - format: base.surface_format.format, - samples: vk::SampleCountFlags::TYPE_1, - load_op: vk::AttachmentLoadOp::CLEAR, - store_op: vk::AttachmentStoreOp::STORE, - final_layout: vk::ImageLayout::PRESENT_SRC_KHR, - ..Default::default() - }, - vk::AttachmentDescription { - format: vk::Format::D16_UNORM, - samples: vk::SampleCountFlags::TYPE_1, - load_op: vk::AttachmentLoadOp::CLEAR, - initial_layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, - final_layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, - ..Default::default() - }, - ]; - let color_attachment_refs = [vk::AttachmentReference { - attachment: 0, - layout: vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, - }]; - let depth_attachment_ref = vk::AttachmentReference { - attachment: 1, - layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, - }; - let dependencies = [vk::SubpassDependency { - src_subpass: vk::SUBPASS_EXTERNAL, - src_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, - dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ - | vk::AccessFlags::COLOR_ATTACHMENT_WRITE, - dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, - ..Default::default() - }]; - - let subpasses = [vk::SubpassDescription::builder() - .color_attachments(&color_attachment_refs) - .depth_stencil_attachment(&depth_attachment_ref) - .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) - .build()]; - - let renderpass_create_info = vk::RenderPassCreateInfo::builder() - .attachments(&renderpass_attachments) - .subpasses(&subpasses) - .dependencies(&dependencies); - - let renderpass = base - .device - .create_render_pass(&renderpass_create_info, None) - .unwrap(); - - let framebuffers: Vec = base - .present_image_views - .iter() - .map(|&present_image_view| { - let framebuffer_attachments = [present_image_view, base.depth_image_view]; - let frame_buffer_create_info = vk::FramebufferCreateInfo::builder() - .render_pass(renderpass) - .attachments(&framebuffer_attachments) - .width(base.surface_resolution.width) - .height(base.surface_resolution.height) - .layers(1); - - base.device - .create_framebuffer(&frame_buffer_create_info, None) - .unwrap() - }) - .collect(); - - let index_buffer_data = [0u32, 1, 2]; - let index_buffer_info = vk::BufferCreateInfo::builder() - .size(std::mem::size_of_val(&index_buffer_data) as u64) - .usage(vk::BufferUsageFlags::INDEX_BUFFER) - .sharing_mode(vk::SharingMode::EXCLUSIVE); - - let index_buffer = base.device.create_buffer(&index_buffer_info, None).unwrap(); - let index_buffer_memory_req = base.device.get_buffer_memory_requirements(index_buffer); - let index_buffer_memory_index = find_memorytype_index( - &index_buffer_memory_req, - &base.device_memory_properties, - vk::MemoryPropertyFlags::HOST_VISIBLE, - ) - .expect("Unable to find suitable memorytype for the index buffer."); - - let index_allocate_info = vk::MemoryAllocateInfo { - allocation_size: index_buffer_memory_req.size, - memory_type_index: index_buffer_memory_index, - ..Default::default() - }; - let index_buffer_memory = base - .device - .allocate_memory(&index_allocate_info, None) - .unwrap(); - let index_ptr = base - .device - .map_memory( - index_buffer_memory, - 0, - index_buffer_memory_req.size, - vk::MemoryMapFlags::empty(), - ) - .unwrap(); - let mut index_slice = Align::new( - index_ptr, - align_of::() as u64, - index_buffer_memory_req.size, - ); - index_slice.copy_from_slice(&index_buffer_data); - base.device.unmap_memory(index_buffer_memory); - base.device - .bind_buffer_memory(index_buffer, index_buffer_memory, 0) - .unwrap(); - - let vertex_input_buffer_info = vk::BufferCreateInfo { - size: 3 * std::mem::size_of::() as u64, - usage: vk::BufferUsageFlags::VERTEX_BUFFER, - sharing_mode: vk::SharingMode::EXCLUSIVE, - ..Default::default() - }; - - let vertex_input_buffer = base - .device - .create_buffer(&vertex_input_buffer_info, None) - .unwrap(); - - let vertex_input_buffer_memory_req = base - .device - .get_buffer_memory_requirements(vertex_input_buffer); - - let vertex_input_buffer_memory_index = find_memorytype_index( - &vertex_input_buffer_memory_req, - &base.device_memory_properties, - vk::MemoryPropertyFlags::HOST_VISIBLE, - ) - .expect("Unable to find suitable memorytype for the vertex buffer."); - - let vertex_buffer_allocate_info = vk::MemoryAllocateInfo { - allocation_size: vertex_input_buffer_memory_req.size, - memory_type_index: vertex_input_buffer_memory_index, - ..Default::default() - }; - - let vertex_input_buffer_memory = base - .device - .allocate_memory(&vertex_buffer_allocate_info, None) - .unwrap(); - - let vertices = [ - Vertex { - pos: [-1.0, 1.0, 0.0, 1.0], - color: [0.0, 1.0, 0.0, 1.0], - }, - Vertex { - pos: [1.0, 1.0, 0.0, 1.0], - color: [0.0, 0.0, 1.0, 1.0], - }, - Vertex { - pos: [0.0, -1.0, 0.0, 1.0], - color: [1.0, 0.0, 0.0, 1.0], - }, - ]; - - let vert_ptr = base - .device - .map_memory( - vertex_input_buffer_memory, - 0, - vertex_input_buffer_memory_req.size, - vk::MemoryMapFlags::empty(), - ) - .unwrap(); - - let mut vert_align = Align::new( - vert_ptr, - align_of::() as u64, - vertex_input_buffer_memory_req.size, - ); - vert_align.copy_from_slice(&vertices); - base.device.unmap_memory(vertex_input_buffer_memory); - base.device - .bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0) - .unwrap(); - let mut vertex_spv_file = File::open(Path::new("shader/nv_ray_tracing/vert.spv")) - .expect("Could not find vert.spv."); - let mut frag_spv_file = File::open(Path::new("shader/nv_ray_tracing/frag.spv")) - .expect("Could not find frag.spv."); - - let vertex_code = - read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file"); - let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(&vertex_code); - - let frag_code = - read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file"); - let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code); - - let vertex_shader_module = base - .device - .create_shader_module(&vertex_shader_info, None) - .expect("Vertex shader module error"); - - let fragment_shader_module = base - .device - .create_shader_module(&frag_shader_info, None) - .expect("Fragment shader module error"); - - let layout_create_info = vk::PipelineLayoutCreateInfo::default(); - - let pipeline_layout = base - .device - .create_pipeline_layout(&layout_create_info, None) - .unwrap(); - - let shader_entry_name = CString::new("main").unwrap(); - let shader_stage_create_infos = [ - vk::PipelineShaderStageCreateInfo { - module: vertex_shader_module, - p_name: shader_entry_name.as_ptr(), - stage: vk::ShaderStageFlags::VERTEX, - ..Default::default() - }, - vk::PipelineShaderStageCreateInfo { - s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO, - module: fragment_shader_module, - p_name: shader_entry_name.as_ptr(), - stage: vk::ShaderStageFlags::FRAGMENT, - ..Default::default() - }, - ]; - let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { - binding: 0, - stride: mem::size_of::() as u32, - input_rate: vk::VertexInputRate::VERTEX, - }]; - let vertex_input_attribute_descriptions = [ - vk::VertexInputAttributeDescription { - location: 0, - binding: 0, - format: vk::Format::R32G32B32A32_SFLOAT, - offset: offset_of!(Vertex, pos) as u32, - }, - vk::VertexInputAttributeDescription { - location: 1, - binding: 0, - format: vk::Format::R32G32B32A32_SFLOAT, - offset: offset_of!(Vertex, color) as u32, - }, - ]; - - let vertex_input_state_info = vk::PipelineVertexInputStateCreateInfo { - vertex_attribute_description_count: vertex_input_attribute_descriptions.len() as u32, - p_vertex_attribute_descriptions: vertex_input_attribute_descriptions.as_ptr(), - vertex_binding_description_count: vertex_input_binding_descriptions.len() as u32, - p_vertex_binding_descriptions: vertex_input_binding_descriptions.as_ptr(), - ..Default::default() - }; - let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo { - topology: vk::PrimitiveTopology::TRIANGLE_LIST, - ..Default::default() - }; - let viewports = [vk::Viewport { - x: 0.0, - y: 0.0, - width: base.surface_resolution.width as f32, - height: base.surface_resolution.height as f32, - min_depth: 0.0, - max_depth: 1.0, - }]; - let scissors = [vk::Rect2D { - offset: vk::Offset2D { x: 0, y: 0 }, - extent: base.surface_resolution.clone(), - }]; - let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder() - .scissors(&scissors) - .viewports(&viewports); - - let rasterization_info = vk::PipelineRasterizationStateCreateInfo { - front_face: vk::FrontFace::COUNTER_CLOCKWISE, - line_width: 1.0, - polygon_mode: vk::PolygonMode::FILL, - ..Default::default() - }; - let multisample_state_info = vk::PipelineMultisampleStateCreateInfo { - rasterization_samples: vk::SampleCountFlags::TYPE_1, - ..Default::default() - }; - let noop_stencil_state = vk::StencilOpState { - fail_op: vk::StencilOp::KEEP, - pass_op: vk::StencilOp::KEEP, - depth_fail_op: vk::StencilOp::KEEP, - compare_op: vk::CompareOp::ALWAYS, - ..Default::default() - }; - let depth_state_info = vk::PipelineDepthStencilStateCreateInfo { - depth_test_enable: 1, - depth_write_enable: 1, - depth_compare_op: vk::CompareOp::LESS_OR_EQUAL, - front: noop_stencil_state.clone(), - back: noop_stencil_state.clone(), - max_depth_bounds: 1.0, - ..Default::default() - }; - let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { - blend_enable: 0, - src_color_blend_factor: vk::BlendFactor::SRC_COLOR, - dst_color_blend_factor: vk::BlendFactor::ONE_MINUS_DST_COLOR, - color_blend_op: vk::BlendOp::ADD, - src_alpha_blend_factor: vk::BlendFactor::ZERO, - dst_alpha_blend_factor: vk::BlendFactor::ZERO, - alpha_blend_op: vk::BlendOp::ADD, - color_write_mask: vk::ColorComponentFlags::all(), - }]; - let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder() - .logic_op(vk::LogicOp::CLEAR) - .attachments(&color_blend_attachment_states); - - let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]; - let dynamic_state_info = - vk::PipelineDynamicStateCreateInfo::builder().dynamic_states(&dynamic_state); - - let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() - .stages(&shader_stage_create_infos) - .vertex_input_state(&vertex_input_state_info) - .input_assembly_state(&vertex_input_assembly_state_info) - .viewport_state(&viewport_state_info) - .rasterization_state(&rasterization_info) - .multisample_state(&multisample_state_info) - .depth_stencil_state(&depth_state_info) - .color_blend_state(&color_blend_state) - .dynamic_state(&dynamic_state_info) - .layout(pipeline_layout) - .render_pass(renderpass); - - let graphics_pipelines = base - .device - .create_graphics_pipelines( - vk::PipelineCache::null(), - &[graphic_pipeline_info.build()], - None, - ) - .expect("Unable to create graphics pipeline"); - - let graphic_pipeline = graphics_pipelines[0]; - - base.render_loop(|| { - let (present_index, _) = base - .swapchain_loader - .acquire_next_image( - base.swapchain, - std::u64::MAX, - base.present_complete_semaphore, - vk::Fence::null(), - ) - .unwrap(); - let clear_values = [ - vk::ClearValue { - color: vk::ClearColorValue { - float32: [0.0, 0.0, 0.0, 0.0], - }, - }, - vk::ClearValue { - depth_stencil: vk::ClearDepthStencilValue { - depth: 1.0, - stencil: 0, - }, - }, - ]; - - let render_pass_begin_info = vk::RenderPassBeginInfo::builder() - .render_pass(renderpass) - .framebuffer(framebuffers[present_index as usize]) - .render_area(vk::Rect2D { - offset: vk::Offset2D { x: 0, y: 0 }, - extent: base.surface_resolution.clone(), - }) - .clear_values(&clear_values); - - record_submit_commandbuffer( - &base.device, - base.draw_command_buffer, - base.present_queue, - &[vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT], - &[base.present_complete_semaphore], - &[base.rendering_complete_semaphore], - |device, draw_command_buffer| { - device.cmd_begin_render_pass( - draw_command_buffer, - &render_pass_begin_info, - vk::SubpassContents::INLINE, - ); - device.cmd_bind_pipeline( - draw_command_buffer, - vk::PipelineBindPoint::GRAPHICS, - graphic_pipeline, - ); - device.cmd_set_viewport(draw_command_buffer, 0, &viewports); - device.cmd_set_scissor(draw_command_buffer, 0, &scissors); - device.cmd_bind_vertex_buffers( - draw_command_buffer, - 0, - &[vertex_input_buffer], - &[0], - ); - device.cmd_bind_index_buffer( - draw_command_buffer, - index_buffer, - 0, - vk::IndexType::UINT32, - ); - device.cmd_draw_indexed( - draw_command_buffer, - index_buffer_data.len() as u32, - 1, - 0, - 0, - 1, - ); - // Or draw without the index buffer - // device.cmd_draw(draw_command_buffer, 3, 1, 0, 0); - device.cmd_end_render_pass(draw_command_buffer); - }, - ); - //let mut present_info_err = mem::uninitialized(); - let wait_semaphors = [base.rendering_complete_semaphore]; - let swapchains = [base.swapchain]; - let image_indices = [present_index]; - let present_info = vk::PresentInfoKHR::builder() - .wait_semaphores(&wait_semaphors) // &base.rendering_complete_semaphore) - .swapchains(&swapchains) - .image_indices(&image_indices); - - base.swapchain_loader - .queue_present(base.present_queue, &present_info) - .unwrap(); - }); - - base.device.device_wait_idle().unwrap(); - for pipeline in graphics_pipelines { - base.device.destroy_pipeline(pipeline, None); - } - base.device.destroy_pipeline_layout(pipeline_layout, None); - base.device - .destroy_shader_module(vertex_shader_module, None); - base.device - .destroy_shader_module(fragment_shader_module, None); - base.device.free_memory(index_buffer_memory, None); - base.device.destroy_buffer(index_buffer, None); - base.device.free_memory(vertex_input_buffer_memory, None); - base.device.destroy_buffer(vertex_input_buffer, None); - for framebuffer in framebuffers { - base.device.destroy_framebuffer(framebuffer, None); - } - base.device.destroy_render_pass(renderpass, None); - } -} From 3a76e81d3a3bc12fec7e981b95826ad6f9e2a0f4 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Sun, 10 Feb 2019 23:59:57 +0100 Subject: [PATCH 20/25] Delete old shader binaries --- examples/shader/nv_ray_tracing/frag.spv | Bin 448 -> 0 bytes examples/shader/nv_ray_tracing/triangle.frag | 10 ---------- examples/shader/nv_ray_tracing/triangle.vert | 13 ------------- examples/shader/nv_ray_tracing/vert.spv | Bin 852 -> 0 bytes 4 files changed, 23 deletions(-) delete mode 100644 examples/shader/nv_ray_tracing/frag.spv delete mode 100644 examples/shader/nv_ray_tracing/triangle.frag delete mode 100644 examples/shader/nv_ray_tracing/triangle.vert delete mode 100644 examples/shader/nv_ray_tracing/vert.spv diff --git a/examples/shader/nv_ray_tracing/frag.spv b/examples/shader/nv_ray_tracing/frag.spv deleted file mode 100644 index e1a850d82440c1021c6ff732fae6dd6eea16e628..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 448 zcmY*VO;5r=6dWl96cvbx-VDS$AqETykwi`Gr6>IXo3(5TiY;l&|M2H}F>z-72yFBE zcIM5#dAmXFstFwMYvBYxtA`pf&_=`OD1MHIWwjWNCnG6N5qP0A9W+sAR=nF)rothk z&FE4mo(9|n>Hg`|=rgR%y*vgr5X&0_A`OB+Is&Ze$(ke^M+Ggq2rp}D7 zM;n{GA!Mgr-;j*Fw~vKQAM$lxz-u&1ZW2#K-W|9krzNcVA3o!hnseYMhTora-yqj# zTu`t32+OhEDqH3C1@KM(H@sU7a2#$Ot#hs&F(8Zf*Wx^U{6@ikhsFrLmbdZ32KnlgUOMGs)UCpbJd zl=}+K!~?vkV4 Date: Mon, 11 Feb 2019 15:43:49 +0100 Subject: [PATCH 21/25] Added maintenance 1-3 extension names to khr/mod.rs and moved KHR extension names from EXT to proper place. --- ash/src/extensions/ext/mod.rs | 9 --------- ash/src/extensions/khr/mod.rs | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/ash/src/extensions/ext/mod.rs b/ash/src/extensions/ext/mod.rs index 5d6bf89..1c8376e 100644 --- a/ash/src/extensions/ext/mod.rs +++ b/ash/src/extensions/ext/mod.rs @@ -8,12 +8,3 @@ mod debug_report; mod debug_utils; mod descriptor_indexing; -pub fn memory_requirements2_name() -> &'static std::ffi::CStr { - std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_get_memory_requirements2\0") - .expect("Wrong extension string") -} - -pub fn physical_device_properties2_name() -> &'static std::ffi::CStr { - std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_get_physical_device_properties2\0") - .expect("Wrong extension string") -} diff --git a/ash/src/extensions/khr/mod.rs b/ash/src/extensions/khr/mod.rs index 8812674..e69ac8e 100644 --- a/ash/src/extensions/khr/mod.rs +++ b/ash/src/extensions/khr/mod.rs @@ -15,3 +15,28 @@ mod wayland_surface; mod win32_surface; mod xcb_surface; mod xlib_surface; + +pub fn memory_requirements2_name() -> &'static std::ffi::CStr { + std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_get_memory_requirements2\0") + .expect("Wrong extension string") +} + +pub fn physical_device_properties2_name() -> &'static std::ffi::CStr { + std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_get_physical_device_properties2\0") + .expect("Wrong extension string") +} + +pub fn maintenance1_name() -> &'static std::ffi::CStr { + std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance1\0") + .expect("Wrong extension string") +} + +pub fn maintenance2_name() -> &'static std::ffi::CStr { + std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance2\0") + .expect("Wrong extension string") +} + +pub fn maintenance3_name() -> &'static std::ffi::CStr { + std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance3\0") + .expect("Wrong extension string") +} From 69527c759a976dfd06fea08d4fd2aea0d9205fed Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Mon, 11 Feb 2019 16:21:43 +0100 Subject: [PATCH 22/25] Rustfmt --- ash/src/extensions/ext/mod.rs | 1 - ash/src/extensions/khr/mod.rs | 9 +++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/ash/src/extensions/ext/mod.rs b/ash/src/extensions/ext/mod.rs index 1c8376e..33a4f43 100644 --- a/ash/src/extensions/ext/mod.rs +++ b/ash/src/extensions/ext/mod.rs @@ -7,4 +7,3 @@ mod debug_marker; mod debug_report; mod debug_utils; mod descriptor_indexing; - diff --git a/ash/src/extensions/khr/mod.rs b/ash/src/extensions/khr/mod.rs index e69ac8e..75bdb38 100644 --- a/ash/src/extensions/khr/mod.rs +++ b/ash/src/extensions/khr/mod.rs @@ -27,16 +27,13 @@ pub fn physical_device_properties2_name() -> &'static std::ffi::CStr { } pub fn maintenance1_name() -> &'static std::ffi::CStr { - std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance1\0") - .expect("Wrong extension string") + std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance1\0").expect("Wrong extension string") } pub fn maintenance2_name() -> &'static std::ffi::CStr { - std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance2\0") - .expect("Wrong extension string") + std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance2\0").expect("Wrong extension string") } pub fn maintenance3_name() -> &'static std::ffi::CStr { - std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance3\0") - .expect("Wrong extension string") + std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance3\0").expect("Wrong extension string") } From ff44b2cb16044d649bea601dca828fec78fafc31 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Thu, 14 Feb 2019 12:03:41 +0100 Subject: [PATCH 23/25] Remove extension names from mod.rs and make all extensions use auto-generated names --- ash/src/extensions/ext/debug_marker.rs | 2 +- ash/src/extensions/ext/debug_report.rs | 2 +- ash/src/extensions/ext/debug_utils.rs | 2 +- ash/src/extensions/ext/descriptor_indexing.rs | 10 --------- ash/src/extensions/ext/mod.rs | 2 -- ash/src/extensions/khr/android_surface.rs | 2 +- ash/src/extensions/khr/display_swapchain.rs | 2 +- ash/src/extensions/khr/mod.rs | 22 ------------------- ash/src/extensions/khr/surface.rs | 2 +- ash/src/extensions/khr/swapchain.rs | 2 +- ash/src/extensions/khr/wayland_surface.rs | 2 +- ash/src/extensions/khr/win32_surface.rs | 2 +- ash/src/extensions/khr/xcb_surface.rs | 2 +- ash/src/extensions/khr/xlib_surface.rs | 2 +- ash/src/extensions/mvk/ios_surface.rs | 2 +- ash/src/extensions/mvk/macos_surface.rs | 2 +- ash/src/extensions/nv/mesh_shader.rs | 2 +- ash/src/extensions/nv/ray_tracing.rs | 2 +- 18 files changed, 15 insertions(+), 49 deletions(-) delete mode 100644 ash/src/extensions/ext/descriptor_indexing.rs diff --git a/ash/src/extensions/ext/debug_marker.rs b/ash/src/extensions/ext/debug_marker.rs index fe954e9..529c412 100644 --- a/ash/src/extensions/ext/debug_marker.rs +++ b/ash/src/extensions/ext/debug_marker.rs @@ -21,7 +21,7 @@ impl DebugMarker { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_EXT_debug_marker\0").expect("Wrong extension string") + vk::ExtDebugMarkerFn::name() } pub unsafe fn debug_marker_set_object_name( diff --git a/ash/src/extensions/ext/debug_report.rs b/ash/src/extensions/ext/debug_report.rs index ec8e147..5b44d1a 100644 --- a/ash/src/extensions/ext/debug_report.rs +++ b/ash/src/extensions/ext/debug_report.rs @@ -24,7 +24,7 @@ impl DebugReport { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_EXT_debug_report\0").expect("Wrong extension string") + vk::ExtDebugReportFn::name() } pub unsafe fn destroy_debug_report_callback( diff --git a/ash/src/extensions/ext/debug_utils.rs b/ash/src/extensions/ext/debug_utils.rs index 443e688..bbe5bc2 100644 --- a/ash/src/extensions/ext/debug_utils.rs +++ b/ash/src/extensions/ext/debug_utils.rs @@ -23,7 +23,7 @@ impl DebugUtils { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_EXT_debug_utils\0").expect("Wrong extension string") + vk::ExtDebugUtilsFn::name() } pub unsafe fn debug_utils_set_object_name( diff --git a/ash/src/extensions/ext/descriptor_indexing.rs b/ash/src/extensions/ext/descriptor_indexing.rs deleted file mode 100644 index daa448b..0000000 --- a/ash/src/extensions/ext/descriptor_indexing.rs +++ /dev/null @@ -1,10 +0,0 @@ -use std::ffi::CStr; - -#[derive(Clone)] -pub struct DescriptorIndexing {} - -impl DescriptorIndexing { - pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_EXT_descriptor_indexing\0").expect("Wrong extension string") - } -} diff --git a/ash/src/extensions/ext/mod.rs b/ash/src/extensions/ext/mod.rs index 33a4f43..eba690f 100644 --- a/ash/src/extensions/ext/mod.rs +++ b/ash/src/extensions/ext/mod.rs @@ -1,9 +1,7 @@ pub use self::debug_marker::DebugMarker; pub use self::debug_report::DebugReport; pub use self::debug_utils::DebugUtils; -pub use self::descriptor_indexing::DescriptorIndexing; mod debug_marker; mod debug_report; mod debug_utils; -mod descriptor_indexing; diff --git a/ash/src/extensions/khr/android_surface.rs b/ash/src/extensions/khr/android_surface.rs index 0ceade6..4fa6438 100644 --- a/ash/src/extensions/khr/android_surface.rs +++ b/ash/src/extensions/khr/android_surface.rs @@ -24,7 +24,7 @@ impl AndroidSurface { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_KHR_android_surface\0").expect("Wrong extension string") + vk::KhrAndroidSurfaceFn::name() } pub unsafe fn create_android_surface( diff --git a/ash/src/extensions/khr/display_swapchain.rs b/ash/src/extensions/khr/display_swapchain.rs index dfe1820..ca056d1 100644 --- a/ash/src/extensions/khr/display_swapchain.rs +++ b/ash/src/extensions/khr/display_swapchain.rs @@ -24,7 +24,7 @@ impl DisplaySwapchain { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_KHR_display_swapchain\0").expect("Wrong extension string") + vk::KhrDisplaySwapchainFn::name() } pub unsafe fn create_shared_swapchains( diff --git a/ash/src/extensions/khr/mod.rs b/ash/src/extensions/khr/mod.rs index 75bdb38..8812674 100644 --- a/ash/src/extensions/khr/mod.rs +++ b/ash/src/extensions/khr/mod.rs @@ -15,25 +15,3 @@ mod wayland_surface; mod win32_surface; mod xcb_surface; mod xlib_surface; - -pub fn memory_requirements2_name() -> &'static std::ffi::CStr { - std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_get_memory_requirements2\0") - .expect("Wrong extension string") -} - -pub fn physical_device_properties2_name() -> &'static std::ffi::CStr { - std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_get_physical_device_properties2\0") - .expect("Wrong extension string") -} - -pub fn maintenance1_name() -> &'static std::ffi::CStr { - std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance1\0").expect("Wrong extension string") -} - -pub fn maintenance2_name() -> &'static std::ffi::CStr { - std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance2\0").expect("Wrong extension string") -} - -pub fn maintenance3_name() -> &'static std::ffi::CStr { - std::ffi::CStr::from_bytes_with_nul(b"VK_KHR_maintenance3\0").expect("Wrong extension string") -} diff --git a/ash/src/extensions/khr/surface.rs b/ash/src/extensions/khr/surface.rs index bfcaca4..3e0648e 100644 --- a/ash/src/extensions/khr/surface.rs +++ b/ash/src/extensions/khr/surface.rs @@ -25,7 +25,7 @@ impl Surface { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_KHR_surface\0").expect("Wrong extension string") + vk::KhrSurfaceFn::name() } pub unsafe fn get_physical_device_surface_support( diff --git a/ash/src/extensions/khr/swapchain.rs b/ash/src/extensions/khr/swapchain.rs index fb24d59..aedcb8c 100644 --- a/ash/src/extensions/khr/swapchain.rs +++ b/ash/src/extensions/khr/swapchain.rs @@ -25,7 +25,7 @@ impl Swapchain { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_KHR_swapchain\0").expect("Wrong extension string") + vk::KhrSwapchainFn::name() } pub unsafe fn destroy_swapchain( diff --git a/ash/src/extensions/khr/wayland_surface.rs b/ash/src/extensions/khr/wayland_surface.rs index 7a1608c..5308706 100644 --- a/ash/src/extensions/khr/wayland_surface.rs +++ b/ash/src/extensions/khr/wayland_surface.rs @@ -24,7 +24,7 @@ impl WaylandSurface { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_KHR_wayland_surface\0").expect("Wrong extension string") + vk::KhrWaylandSurfaceFn::name() } pub unsafe fn create_wayland_surface( diff --git a/ash/src/extensions/khr/win32_surface.rs b/ash/src/extensions/khr/win32_surface.rs index 86daec6..e32f046 100644 --- a/ash/src/extensions/khr/win32_surface.rs +++ b/ash/src/extensions/khr/win32_surface.rs @@ -24,7 +24,7 @@ impl Win32Surface { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_KHR_win32_surface\0").expect("Wrong extension string") + vk::KhrWin32SurfaceFn::name() } pub unsafe fn create_win32_surface( diff --git a/ash/src/extensions/khr/xcb_surface.rs b/ash/src/extensions/khr/xcb_surface.rs index e3a662d..ede5859 100644 --- a/ash/src/extensions/khr/xcb_surface.rs +++ b/ash/src/extensions/khr/xcb_surface.rs @@ -24,7 +24,7 @@ impl XcbSurface { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_KHR_xcb_surface\0").expect("Wrong extension string") + vk::KhrXcbSurfaceFn::name() } pub unsafe fn create_xcb_surface( diff --git a/ash/src/extensions/khr/xlib_surface.rs b/ash/src/extensions/khr/xlib_surface.rs index ffd9ab1..32ab541 100644 --- a/ash/src/extensions/khr/xlib_surface.rs +++ b/ash/src/extensions/khr/xlib_surface.rs @@ -24,7 +24,7 @@ impl XlibSurface { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_KHR_xlib_surface\0").expect("Wrong extension string") + vk::KhrXlibSurfaceFn::name() } pub unsafe fn create_xlib_surface( diff --git a/ash/src/extensions/mvk/ios_surface.rs b/ash/src/extensions/mvk/ios_surface.rs index 1d7a234..a1ffd93 100644 --- a/ash/src/extensions/mvk/ios_surface.rs +++ b/ash/src/extensions/mvk/ios_surface.rs @@ -24,7 +24,7 @@ impl IOSSurface { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_MVK_IOS_surface\0").expect("Wrong extension string") + vk::MvkIosSurfaceFn::name() } pub unsafe fn create_ios_surface_mvk( diff --git a/ash/src/extensions/mvk/macos_surface.rs b/ash/src/extensions/mvk/macos_surface.rs index 8264c95..9fc1910 100644 --- a/ash/src/extensions/mvk/macos_surface.rs +++ b/ash/src/extensions/mvk/macos_surface.rs @@ -24,7 +24,7 @@ impl MacOSSurface { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_MVK_macos_surface\0").expect("Wrong extension string") + vk::MvkMacosSurfaceFn::name() } pub unsafe fn create_mac_os_surface_mvk( diff --git a/ash/src/extensions/nv/mesh_shader.rs b/ash/src/extensions/nv/mesh_shader.rs index db08f83..9e00e6b 100644 --- a/ash/src/extensions/nv/mesh_shader.rs +++ b/ash/src/extensions/nv/mesh_shader.rs @@ -62,6 +62,6 @@ impl MeshShader { ); } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_NV_mesh_shader\0").expect("Wrong extension string") + vk::NvMeshShaderFn::name() } } diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index 48952cd..f6fb8d1 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -258,6 +258,6 @@ impl RayTracing { } pub fn name() -> &'static CStr { - CStr::from_bytes_with_nul(b"VK_NV_ray_tracing\0").expect("Wrong extension string") + vk::NvRayTracingFn::name() } } From 658743f59b9b0d7af7372a02887d4a142aa26a48 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Thu, 14 Feb 2019 12:14:53 +0100 Subject: [PATCH 24/25] Use 1.0 in ray tracing extension except for get_properties helper function --- ash/src/extensions/nv/ray_tracing.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index f6fb8d1..013bd85 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -2,7 +2,7 @@ use prelude::*; use std::ffi::CStr; use std::mem; -use version::{DeviceV1_1, InstanceV1_1}; +use version::{DeviceV1_0, InstanceV1_0, InstanceV1_1}; use vk; use RawPtr; @@ -13,7 +13,7 @@ pub struct RayTracing { } impl RayTracing { - pub fn new(instance: &I, device: &D) -> RayTracing { + pub fn new(instance: &I, device: &D) -> RayTracing { let ray_tracing_fn = vk::NvRayTracingFn::load(|name| unsafe { mem::transmute(instance.get_device_proc_addr(device.handle(), name.as_ptr())) }); From 3941c762a7518c71ae6b6775a65a45ebfe0d67d2 Mon Sep 17 00:00:00 2001 From: Graham Wihlidal Date: Thu, 14 Feb 2019 12:28:29 +0100 Subject: [PATCH 25/25] Minor cleanup --- ash/src/extensions/nv/ray_tracing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ash/src/extensions/nv/ray_tracing.rs b/ash/src/extensions/nv/ray_tracing.rs index 013bd85..fc0deca 100644 --- a/ash/src/extensions/nv/ray_tracing.rs +++ b/ash/src/extensions/nv/ray_tracing.rs @@ -219,7 +219,7 @@ impl RayTracing { let err_code = self.ray_tracing_fn.get_acceleration_structure_handle_nv( self.handle, accel_struct, - 8, // sizeof(u64) + std::mem::size_of::(), handle_ptr as *mut std::ffi::c_void, ); match err_code {