Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement VK_EXT_debug_utils naming #58

Merged
merged 9 commits into from Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/01_basic/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32> = 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,
Expand All @@ -97,6 +98,7 @@ impl ExampleApp for Basic {
vk::BufferUsageFlags::VERTEX_BUFFER,
)?,
};
ctx.device.set_name(&resources.vertex_buffer, "Vertex Buffer")?;

Ok(Self {
resources,
Expand Down
14 changes: 9 additions & 5 deletions examples/03_raytracing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn make_input_buffer<T: Copy>(
data: &[T],
usage: vk::BufferUsageFlags,
alignment: Option<u64>,
name: &str,
) -> Result<Buffer> {
let buffer = match alignment {
None => Buffer::new(
Expand All @@ -70,6 +71,7 @@ fn make_input_buffer<T: Copy>(
.view_full()
.mapped_slice::<T>()?
.copy_from_slice(data);
ctx.device.set_name(&buffer, name)?;
Ok(buffer)
}

Expand All @@ -78,12 +80,12 @@ fn make_vertex_buffer(ctx: &mut Context) -> Result<Buffer> {
-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<Buffer> {
let indices = (0..=5).collect::<Vec<u32>>();
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<Buffer> {
Expand All @@ -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> {
Expand Down Expand Up @@ -138,6 +140,7 @@ fn make_acceleration_structure(
ctx: &mut Context,
build_info: &AccelerationStructureBuildInfo,
prim_counts: &[u32],
name: &str,
) -> Result<BackedAccelerationStructure> {
let sizes = query_build_size(
&ctx.device,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());
Expand Down
41 changes: 39 additions & 2 deletions src/core/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ 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};
use crate::core::traits::Nameable;
#[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
Expand Down Expand Up @@ -66,6 +68,8 @@ struct DeviceInner {
acceleration_structure: Option<khr::AccelerationStructure>,
#[derivative(Debug = "ignore")]
rt_pipeline: Option<khr::RayTracingPipeline>,
#[derivative(Debug = "ignore")]
debug_utils: Option<ext::DebugUtils>,
}

/// Wrapper around a `VkDevice`. The device provides access to almost the entire
Expand Down Expand Up @@ -318,6 +322,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) => {
Expand Down Expand Up @@ -375,6 +385,7 @@ impl Device {
dynamic_state3,
acceleration_structure,
rt_pipeline,
debug_utils,
#[cfg(feature = "fsr2")]
fsr2_context: Mutex::new(fsr2),
};
Expand Down Expand Up @@ -467,6 +478,16 @@ impl Device {
Ok(self.inner.rt_properties.as_ref().unwrap())
}

/// Get access to the functions of VK_EXT_debug_utils
/// # Errors
/// - 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.
/// # Example
/// ```
Expand Down Expand Up @@ -541,6 +562,22 @@ impl Device {
pub fn fsr2_context(&self) -> MutexGuard<ManuallyDrop<Fsr2Context>> {
self.inner.fsr2_context.lock().unwrap()
}

/// Set the name of any given compatible object for debugging purposes
pub fn set_name<T: Nameable>(&self, object: &T, name: &str) -> Result<()> {
let object_name = CString::new(name)?;
let name_info = vk::DebugUtilsObjectNameInfoEXT::builder()
.object_type(<T as Nameable>::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 {
Expand Down
1 change: 1 addition & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pub mod init;
pub mod instance;
pub mod physical_device;
pub mod queue;
pub mod traits;
15 changes: 15 additions & 0 deletions src/core/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Defines traits for core

use ash::vk;

/// 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: AsRaw {
/// Change the name of the given object
const OBJECT_TYPE: vk::ObjectType;
}
12 changes: 12 additions & 0 deletions src/resource/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ use std::ptr::NonNull;

use anyhow::Result;
use ash::vk;
use ash::vk::Handle;
NotAPenguin0 marked this conversation as resolved.
Show resolved Hide resolved

use crate::core::traits::{AsRaw, Nameable};
use crate::util::align::align;
use crate::{Allocation, Allocator, DefaultAllocator, Device, Error, MemoryType};

Expand Down Expand Up @@ -277,6 +279,16 @@ impl<A: Allocator> Buffer<A> {
}
}

unsafe impl AsRaw for Buffer {
unsafe fn as_raw(&self) -> u64 {
self.handle().as_raw()
}
}

impl Nameable for Buffer {
const OBJECT_TYPE: vk::ObjectType = vk::ObjectType::BUFFER;
}

impl<A: Allocator> Drop for Buffer<A> {
fn drop(&mut self) {
#[cfg(feature = "log-objects")]
Expand Down
12 changes: 12 additions & 0 deletions src/resource/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use std::sync::Arc;

use anyhow::Result;
use ash::vk;
use ash::vk::Handle;
NotAPenguin0 marked this conversation as resolved.
Show resolved Hide resolved

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
Expand Down Expand Up @@ -303,6 +305,16 @@ impl<A: Allocator> Image<A> {
}
}

unsafe impl AsRaw for Image {
unsafe fn as_raw(&self) -> u64 {
self.handle().as_raw()
}
}

impl Nameable for Image {
const OBJECT_TYPE: vk::ObjectType = vk::ObjectType::IMAGE;
}

impl<A: Allocator> Drop for Image<A> {
fn drop(&mut self) {
#[cfg(feature = "log-objects")]
Expand Down
12 changes: 12 additions & 0 deletions src/resource/raytracing/acceleration_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

use anyhow::Result;
use ash::vk;
use ash::vk::Handle;
NotAPenguin0 marked this conversation as resolved.
Show resolved Hide resolved

use crate::core::device::ExtensionID;
use crate::core::traits::{AsRaw, Nameable};
use crate::util::to_vk::IntoVulkanType;
use crate::{AccelerationStructureType, BufferView, Device};

Expand Down Expand Up @@ -92,6 +94,16 @@ impl AccelerationStructure {
}
}

unsafe impl AsRaw for AccelerationStructure {
unsafe fn as_raw(&self) -> u64 {
self.handle().as_raw()
}
}

impl 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")]
Expand Down