From d7de63734bd8a50cd730970af46416a04dde807f Mon Sep 17 00:00:00 2001 From: Danny Le Date: Wed, 28 Jun 2023 16:16:33 -0400 Subject: [PATCH 1/9] Add DebugUtils to device --- src/core/device.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/core/device.rs b/src/core/device.rs index 39d8379..2c78d1d 100644 --- a/src/core/device.rs +++ b/src/core/device.rs @@ -66,6 +66,8 @@ struct DeviceInner { acceleration_structure: Option, #[derivative(Debug = "ignore")] rt_pipeline: Option, + #[derivative(Debug = "ignore")] + debug_utils: Option } /// Wrapper around a `VkDevice`. The device provides access to almost the entire @@ -318,6 +320,12 @@ impl Device { None }; + let debug_utils = if settings.enable_validation { + Some( ext::DebugUtils::new( unsafe { instance.loader() }, &instance )) + } else { + None + }; + match &mut accel_properties { None => {} Some(properties) => { @@ -375,6 +383,7 @@ impl Device { dynamic_state3, acceleration_structure, rt_pipeline, + debug_utils, #[cfg(feature = "fsr2")] fsr2_context: Mutex::new(fsr2), }; @@ -467,6 +476,15 @@ impl Device { Ok(self.inner.rt_properties.as_ref().unwrap()) } + /// Get the debug_utils object + /// # Errors + /// - Fails if validation layer is disabled + pub fn debug_utils( + &self + ) -> Result<&ext::DebugUtils> { + Ok(self.inner.debug_utils.as_ref().unwrap()) + } + /// Check if a device extension is enabled. /// # Example /// ``` From 55958773fe9ae2c8bab4501d7c766d544f979262 Mon Sep 17 00:00:00 2001 From: Danny Le Date: Wed, 28 Jun 2023 16:19:48 -0400 Subject: [PATCH 2/9] Add traits.rs for set_name --- src/core/traits.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/core/traits.rs diff --git a/src/core/traits.rs b/src/core/traits.rs new file mode 100644 index 0000000..f96f169 --- /dev/null +++ b/src/core/traits.rs @@ -0,0 +1,13 @@ +//! Defines traits for core + +use anyhow::Result; + +pub unsafe trait AsRawHandle { + unsafe fn handle() -> u64; +} + +/// Represents an abstraction for naming objects in Vulkan for debuggers +pub trait Nameable { + /// Change the name of the given object + fn set_name(&self, device: &crate::core::device::Device, name: &str) -> Result<()>; +} \ No newline at end of file From d0380cc5d3c681b90aff5b8f3866f156b155aa4e Mon Sep 17 00:00:00 2001 From: Danny Le Date: Wed, 28 Jun 2023 16:31:48 -0400 Subject: [PATCH 3/9] Add traits.rs to module --- src/core/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/mod.rs b/src/core/mod.rs index 0b47e28..5356ac0 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -8,3 +8,4 @@ pub mod init; pub mod instance; pub mod physical_device; pub mod queue; +pub mod traits; \ No newline at end of file From 18803cc505e4b9d819f1615d329a871dc64d315e Mon Sep 17 00:00:00 2001 From: Danny Le Date: Wed, 28 Jun 2023 17:13:40 -0400 Subject: [PATCH 4/9] Reduce code repetition in implementations --- src/resource/buffer.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/resource/buffer.rs b/src/resource/buffer.rs index b4214c4..2aa32cb 100644 --- a/src/resource/buffer.rs +++ b/src/resource/buffer.rs @@ -35,6 +35,7 @@ use std::ptr::NonNull; use anyhow::Result; use ash::vk; +use ash::vk::Handle; use crate::util::align::align; use crate::{Allocation, Allocator, DefaultAllocator, Device, Error, MemoryType}; @@ -277,6 +278,16 @@ impl Buffer { } } +unsafe impl crate::core::traits::AsRaw for Buffer { + unsafe fn as_raw(&self) -> u64 { + self.handle().as_raw() + } +} + +impl crate::core::traits::Nameable for Buffer { + const OBJECT_TYPE: vk::ObjectType = vk::ObjectType::BUFFER; +} + impl Drop for Buffer { fn drop(&mut self) { #[cfg(feature = "log-objects")] From 57417b9eb9fdce704c2e9b597a71c6d31ed76d86 Mon Sep 17 00:00:00 2001 From: Danny Le Date: Wed, 28 Jun 2023 17:22:29 -0400 Subject: [PATCH 5/9] Documentation + Fix const access error --- src/core/traits.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/core/traits.rs b/src/core/traits.rs index f96f169..e47fcc6 100644 --- a/src/core/traits.rs +++ b/src/core/traits.rs @@ -1,13 +1,35 @@ //! Defines traits for core use anyhow::Result; +use crate::Device; -pub unsafe trait AsRawHandle { - unsafe fn handle() -> u64; +/// Direct access to the object's handle's as_raw representation +pub unsafe trait AsRaw { + /// Get the as_raw u64 value of the handle underlying the object + unsafe fn as_raw(&self) -> u64; } /// Represents an abstraction for naming objects in Vulkan for debuggers -pub trait Nameable { +pub trait Nameable: AsRaw { /// Change the name of the given object - fn set_name(&self, device: &crate::core::device::Device, name: &str) -> Result<()>; + const OBJECT_TYPE: crate::vk::ObjectType; +} + +impl Device { + /// Set the name of any given compatible object for debugging purposes + pub fn set_name(&self, object: &T, name: &str) -> Result<()> { + let object_name = std::ffi::CString::new(name)?; + let name_info = crate::vk::DebugUtilsObjectNameInfoEXT::builder() + .object_type(::OBJECT_TYPE) + .object_handle( unsafe {object.as_raw()} ) + .object_name(&object_name) + .build(); + + unsafe { + Ok(self.debug_utils()?.set_debug_utils_object_name( + self.handle().handle(), + &name_info + )?) + } + } } \ No newline at end of file From cd06fe1b7d28372e107a9f97317fad6c2c93fd9b Mon Sep 17 00:00:00 2001 From: Danny Le Date: Wed, 28 Jun 2023 17:39:21 -0400 Subject: [PATCH 6/9] Add Nameable implementation --- src/resource/image.rs | 11 +++++++++++ src/resource/raytracing/acceleration_structure.rs | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/resource/image.rs b/src/resource/image.rs index 2a16a9b..73bc003 100644 --- a/src/resource/image.rs +++ b/src/resource/image.rs @@ -18,6 +18,7 @@ use std::sync::Arc; use anyhow::Result; use ash::vk; +use ash::vk::Handle; use crate::{Allocation, Allocator, DefaultAllocator, Device, MemoryType}; @@ -303,6 +304,16 @@ impl Image { } } +unsafe impl crate::core::traits::AsRaw for Image { + unsafe fn as_raw(&self) -> u64 { + self.handle().as_raw() + } +} + +impl crate::core::traits::Nameable for Image { + const OBJECT_TYPE: vk::ObjectType = vk::ObjectType::IMAGE; +} + impl Drop for Image { fn drop(&mut self) { #[cfg(feature = "log-objects")] diff --git a/src/resource/raytracing/acceleration_structure.rs b/src/resource/raytracing/acceleration_structure.rs index 97cb6f1..5fc707a 100644 --- a/src/resource/raytracing/acceleration_structure.rs +++ b/src/resource/raytracing/acceleration_structure.rs @@ -2,6 +2,7 @@ use anyhow::Result; use ash::vk; +use ash::vk::Handle; use crate::core::device::ExtensionID; use crate::util::to_vk::IntoVulkanType; @@ -92,6 +93,16 @@ impl AccelerationStructure { } } +unsafe impl crate::core::traits::AsRaw for AccelerationStructure { + unsafe fn as_raw(&self) -> u64 { + self.handle().as_raw() + } +} + +impl crate::core::traits::Nameable for AccelerationStructure { + const OBJECT_TYPE: vk::ObjectType = vk::ObjectType::ACCELERATION_STRUCTURE_KHR; +} + impl Drop for AccelerationStructure { fn drop(&mut self) { #[cfg(feature = "log-objects")] From 0d1ff438523dc9248dff46b863aa4775ccaa51ad Mon Sep 17 00:00:00 2001 From: Danny Le Date: Wed, 28 Jun 2023 17:39:39 -0400 Subject: [PATCH 7/9] Show off basic usage of set_name in examples --- examples/01_basic/main.rs | 2 ++ examples/03_raytracing/main.rs | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/01_basic/main.rs b/examples/01_basic/main.rs index a87395b..084148f 100644 --- a/examples/01_basic/main.rs +++ b/examples/01_basic/main.rs @@ -82,6 +82,7 @@ impl ExampleApp for Basic { vk::Format::R8G8B8A8_SRGB, vk::SampleCountFlags::TYPE_1, )?; + ctx.device.set_name(&image, "Render Image")?; let data: Vec = vec![ -1.0, 1.0, 0.0, 1.0, -1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, @@ -97,6 +98,7 @@ impl ExampleApp for Basic { vk::BufferUsageFlags::VERTEX_BUFFER, )?, }; + ctx.device.set_name(&resources.vertex_buffer, "Vertex Buffer")?; Ok(Self { resources, diff --git a/examples/03_raytracing/main.rs b/examples/03_raytracing/main.rs index 9ecd468..aaa4d56 100644 --- a/examples/03_raytracing/main.rs +++ b/examples/03_raytracing/main.rs @@ -44,6 +44,7 @@ fn make_input_buffer( data: &[T], usage: vk::BufferUsageFlags, alignment: Option, + name: &str, ) -> Result { let buffer = match alignment { None => Buffer::new( @@ -70,6 +71,7 @@ fn make_input_buffer( .view_full() .mapped_slice::()? .copy_from_slice(data); + ctx.device.set_name(&buffer, name)?; Ok(buffer) } @@ -78,12 +80,12 @@ fn make_vertex_buffer(ctx: &mut Context) -> Result { -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, ]; - make_input_buffer(ctx, &vertices, vk::BufferUsageFlags::VERTEX_BUFFER, None) + make_input_buffer(ctx, &vertices, vk::BufferUsageFlags::VERTEX_BUFFER, None, "Vertex Buffer") } fn make_index_buffer(ctx: &mut Context) -> Result { let indices = (0..=5).collect::>(); - make_input_buffer(ctx, indices.as_slice(), vk::BufferUsageFlags::INDEX_BUFFER, None) + make_input_buffer(ctx, indices.as_slice(), vk::BufferUsageFlags::INDEX_BUFFER, None, "Index Buffer") } fn make_instance_buffer(ctx: &mut Context, blas: &AccelerationStructure) -> Result { @@ -97,7 +99,7 @@ fn make_instance_buffer(ctx: &mut Context, blas: &AccelerationStructure) -> Resu .acceleration_structure(&blas, AccelerationStructureBuildType::Device)?; // The Vulkan spec states: For any element of pInfos[i].pGeometries or pInfos[i].ppGeometries with a geometryType of VK_GEOMETRY_TYPE_INSTANCES_KHR, // if geometry.arrayOfPointers is VK_FALSE, geometry.instances.data.deviceAddress must be aligned to 16 bytes - make_input_buffer(ctx, std::slice::from_ref(&instance), Default::default(), Some(16)) + make_input_buffer(ctx, std::slice::from_ref(&instance), Default::default(), Some(16), "Instance Buffer") } fn blas_build_info<'a>(vertices: &Buffer, indices: &Buffer) -> AccelerationStructureBuildInfo<'a> { @@ -138,6 +140,7 @@ fn make_acceleration_structure( ctx: &mut Context, build_info: &AccelerationStructureBuildInfo, prim_counts: &[u32], + name: &str, ) -> Result { let sizes = query_build_size( &ctx.device, @@ -164,6 +167,7 @@ fn make_acceleration_structure( buffer.view_full(), vk::AccelerationStructureCreateFlagsKHR::default(), )?; + ctx.device.set_name(&acceleration_structure, name)?; Ok(BackedAccelerationStructure { accel: acceleration_structure, buffer, @@ -202,7 +206,7 @@ impl ExampleApp for RaytracingSample { // We only need to set the build mode, flags and all geometry. // src and dst acceleration structures can be left empty let mut blas_build_info = blas_build_info(&vtx_buffer, &idx_buffer); - let blas = make_acceleration_structure(&mut ctx, &blas_build_info, &[2])?; + let blas = make_acceleration_structure(&mut ctx, &blas_build_info, &[2], "BLAS")?; // We can now fill the rest of the build info (source and destination acceleration structures, and the scratch data). blas_build_info = blas_build_info .dst(&blas.accel) @@ -246,7 +250,7 @@ impl ExampleApp for RaytracingSample { let instance_buffer = make_instance_buffer(&mut ctx, &compact_as)?; let mut tlas_build_info = tlas_build_info(&instance_buffer); - let tlas = make_acceleration_structure(&mut ctx, &tlas_build_info, &[1])?; + let tlas = make_acceleration_structure(&mut ctx, &tlas_build_info, &[1], "TLAS")?; tlas_build_info = tlas_build_info .dst(&tlas.accel) .scratch_data(tlas.scratch.address()); From abdf71c2f7ad1d8640caccb23e42733c4009b114 Mon Sep 17 00:00:00 2001 From: Danny Le Date: Wed, 28 Jun 2023 19:42:21 -0400 Subject: [PATCH 8/9] Fix formatting --- src/core/device.rs | 22 +++++----- src/core/traits.rs | 41 ++++++++++--------- src/resource/buffer.rs | 5 ++- src/resource/image.rs | 5 ++- .../raytracing/acceleration_structure.rs | 5 ++- 5 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/core/device.rs b/src/core/device.rs index 2c78d1d..3d8511b 100644 --- a/src/core/device.rs +++ b/src/core/device.rs @@ -9,18 +9,19 @@ use std::ops::Deref; #[allow(unused_imports)] use std::sync::{Arc, Mutex, MutexGuard}; -use anyhow::Result; +use anyhow::{anyhow, Result}; use ash::extensions::{ext, khr}; use ash::vk; #[cfg(feature = "fsr2")] use fsr2_sys::FfxDimensions2D; +use futures::future::err; -use crate::{AppSettings, Error, Instance, PhysicalDevice, WindowInterface}; #[cfg(feature = "fsr2")] use crate::fsr2::Fsr2Context; #[cfg(feature = "fsr2")] use crate::fsr2::Fsr2ContextCreateInfo; use crate::util::string::unwrap_to_raw_strings; +use crate::{AppSettings, Error, Instance, PhysicalDevice, WindowInterface}; /// Device extensions that phobos requests but might not be available. /// # Example @@ -67,7 +68,7 @@ struct DeviceInner { #[derivative(Debug = "ignore")] rt_pipeline: Option, #[derivative(Debug = "ignore")] - debug_utils: Option + debug_utils: Option, } /// Wrapper around a `VkDevice`. The device provides access to almost the entire @@ -321,7 +322,7 @@ impl Device { }; let debug_utils = if settings.enable_validation { - Some( ext::DebugUtils::new( unsafe { instance.loader() }, &instance )) + Some(ext::DebugUtils::new(unsafe { instance.loader() }, &instance)) } else { None }; @@ -476,13 +477,14 @@ impl Device { Ok(self.inner.rt_properties.as_ref().unwrap()) } - /// Get the debug_utils object + /// Get access to the functions of VK_EXT_debug_utils /// # Errors - /// - Fails if validation layer is disabled - pub fn debug_utils( - &self - ) -> Result<&ext::DebugUtils> { - Ok(self.inner.debug_utils.as_ref().unwrap()) + /// - Fails if validation layers are disabled + pub fn debug_utils(&self) -> Result<&ext::DebugUtils> { + self.inner + .debug_utils + .as_ref() + .ok_or_else(|| anyhow::anyhow!("DebugUtils does not exist")) } /// Check if a device extension is enabled. diff --git a/src/core/traits.rs b/src/core/traits.rs index e47fcc6..4a1de14 100644 --- a/src/core/traits.rs +++ b/src/core/traits.rs @@ -1,35 +1,36 @@ //! Defines traits for core use anyhow::Result; +use ash::vk; + use crate::Device; /// Direct access to the object's handle's as_raw representation pub unsafe trait AsRaw { - /// Get the as_raw u64 value of the handle underlying the object - unsafe fn as_raw(&self) -> u64; + /// Get the as_raw u64 value of the handle underlying the object + unsafe fn as_raw(&self) -> u64; } /// Represents an abstraction for naming objects in Vulkan for debuggers pub trait Nameable: AsRaw { - /// Change the name of the given object - const OBJECT_TYPE: crate::vk::ObjectType; + /// Change the name of the given object + const OBJECT_TYPE: vk::ObjectType; } impl Device { - /// Set the name of any given compatible object for debugging purposes - pub fn set_name(&self, object: &T, name: &str) -> Result<()> { - let object_name = std::ffi::CString::new(name)?; - let name_info = crate::vk::DebugUtilsObjectNameInfoEXT::builder() - .object_type(::OBJECT_TYPE) - .object_handle( unsafe {object.as_raw()} ) - .object_name(&object_name) - .build(); + /// Set the name of any given compatible object for debugging purposes + pub fn set_name(&self, object: &T, name: &str) -> Result<()> { + let object_name = std::ffi::CString::new(name)?; + let name_info = vk::DebugUtilsObjectNameInfoEXT::builder() + .object_type(::OBJECT_TYPE) + .object_handle(unsafe { object.as_raw() }) + .object_name(&object_name) + .build(); - unsafe { - Ok(self.debug_utils()?.set_debug_utils_object_name( - self.handle().handle(), - &name_info - )?) - } - } -} \ No newline at end of file + unsafe { + Ok(self + .debug_utils()? + .set_debug_utils_object_name(self.handle().handle(), &name_info)?) + } + } +} diff --git a/src/resource/buffer.rs b/src/resource/buffer.rs index 2aa32cb..136769e 100644 --- a/src/resource/buffer.rs +++ b/src/resource/buffer.rs @@ -37,6 +37,7 @@ use anyhow::Result; use ash::vk; use ash::vk::Handle; +use crate::core::traits::{AsRaw, Nameable}; use crate::util::align::align; use crate::{Allocation, Allocator, DefaultAllocator, Device, Error, MemoryType}; @@ -278,13 +279,13 @@ impl Buffer { } } -unsafe impl crate::core::traits::AsRaw for Buffer { +unsafe impl AsRaw for Buffer { unsafe fn as_raw(&self) -> u64 { self.handle().as_raw() } } -impl crate::core::traits::Nameable for Buffer { +impl Nameable for Buffer { const OBJECT_TYPE: vk::ObjectType = vk::ObjectType::BUFFER; } diff --git a/src/resource/image.rs b/src/resource/image.rs index 73bc003..9884347 100644 --- a/src/resource/image.rs +++ b/src/resource/image.rs @@ -20,6 +20,7 @@ use anyhow::Result; use ash::vk; use ash::vk::Handle; +use crate::core::traits::{AsRaw, Nameable}; use crate::{Allocation, Allocator, DefaultAllocator, Device, MemoryType}; /// Abstraction over a [`VkImage`](vk::Image). Stores information about size, format, etc. Additionally couples the image data together @@ -304,13 +305,13 @@ impl Image { } } -unsafe impl crate::core::traits::AsRaw for Image { +unsafe impl AsRaw for Image { unsafe fn as_raw(&self) -> u64 { self.handle().as_raw() } } -impl crate::core::traits::Nameable for Image { +impl Nameable for Image { const OBJECT_TYPE: vk::ObjectType = vk::ObjectType::IMAGE; } diff --git a/src/resource/raytracing/acceleration_structure.rs b/src/resource/raytracing/acceleration_structure.rs index 5fc707a..2187119 100644 --- a/src/resource/raytracing/acceleration_structure.rs +++ b/src/resource/raytracing/acceleration_structure.rs @@ -5,6 +5,7 @@ use ash::vk; use ash::vk::Handle; use crate::core::device::ExtensionID; +use crate::core::traits::{AsRaw, Nameable}; use crate::util::to_vk::IntoVulkanType; use crate::{AccelerationStructureType, BufferView, Device}; @@ -93,13 +94,13 @@ impl AccelerationStructure { } } -unsafe impl crate::core::traits::AsRaw for AccelerationStructure { +unsafe impl AsRaw for AccelerationStructure { unsafe fn as_raw(&self) -> u64 { self.handle().as_raw() } } -impl crate::core::traits::Nameable for AccelerationStructure { +impl Nameable for AccelerationStructure { const OBJECT_TYPE: vk::ObjectType = vk::ObjectType::ACCELERATION_STRUCTURE_KHR; } From a708595716b18e60fac5f09e099aadf760fc4c8c Mon Sep 17 00:00:00 2001 From: Danny Le Date: Thu, 29 Jun 2023 07:26:52 -0400 Subject: [PATCH 9/9] Move set_name into device --- src/core/device.rs | 17 +++++++++++++++++ src/core/traits.rs | 21 --------------------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/core/device.rs b/src/core/device.rs index 3d8511b..1f48312 100644 --- a/src/core/device.rs +++ b/src/core/device.rs @@ -16,6 +16,7 @@ use ash::vk; use fsr2_sys::FfxDimensions2D; use futures::future::err; +use crate::core::traits::Nameable; #[cfg(feature = "fsr2")] use crate::fsr2::Fsr2Context; #[cfg(feature = "fsr2")] @@ -561,6 +562,22 @@ impl Device { pub fn fsr2_context(&self) -> MutexGuard> { self.inner.fsr2_context.lock().unwrap() } + + /// Set the name of any given compatible object for debugging purposes + pub fn set_name(&self, object: &T, name: &str) -> Result<()> { + let object_name = CString::new(name)?; + let name_info = vk::DebugUtilsObjectNameInfoEXT::builder() + .object_type(::OBJECT_TYPE) + .object_handle(unsafe { object.as_raw() }) + .object_name(&object_name) + .build(); + + unsafe { + Ok(self + .debug_utils()? + .set_debug_utils_object_name(self.handle().handle(), &name_info)?) + } + } } impl Deref for Device { diff --git a/src/core/traits.rs b/src/core/traits.rs index 4a1de14..50f3ad6 100644 --- a/src/core/traits.rs +++ b/src/core/traits.rs @@ -1,10 +1,7 @@ //! Defines traits for core -use anyhow::Result; use ash::vk; -use crate::Device; - /// Direct access to the object's handle's as_raw representation pub unsafe trait AsRaw { /// Get the as_raw u64 value of the handle underlying the object @@ -16,21 +13,3 @@ pub trait Nameable: AsRaw { /// Change the name of the given object const OBJECT_TYPE: vk::ObjectType; } - -impl Device { - /// Set the name of any given compatible object for debugging purposes - pub fn set_name(&self, object: &T, name: &str) -> Result<()> { - let object_name = std::ffi::CString::new(name)?; - let name_info = vk::DebugUtilsObjectNameInfoEXT::builder() - .object_type(::OBJECT_TYPE) - .object_handle(unsafe { object.as_raw() }) - .object_name(&object_name) - .build(); - - unsafe { - Ok(self - .debug_utils()? - .set_debug_utils_object_name(self.handle().handle(), &name_info)?) - } - } -}