diff --git a/crates/hyper_render/src/renderer.rs b/crates/hyper_render/src/renderer.rs index b576fa57..9f6dbb2e 100644 --- a/crates/hyper_render/src/renderer.rs +++ b/crates/hyper_render/src/renderer.rs @@ -38,23 +38,27 @@ impl Renderer { pub fn new(graphics_device: &Arc) -> Self { let object_pipeline_layout = graphics_device.create_pipeline_layout(&PipelineLayoutDescriptor { + label: Some("Object Pipeline Layout"), push_constants_size: size_of::(), }); let opaque_pipeline = { let vertex_shader = graphics_device.create_shader_module(&ShaderModuleDescriptor { + label: Some(""), path: "./assets/shaders/opaque_shader.hlsl", entry_point: "vs_main", stage: ShaderStage::Vertex, }); let fragment_shader = graphics_device.create_shader_module(&ShaderModuleDescriptor { + label: Some(""), path: "./assets/shaders/opaque_shader.hlsl", entry_point: "fs_main", stage: ShaderStage::Fragment, }); graphics_device.create_graphics_pipeline(&GraphicsPipelineDescriptor { + label: Some("Opaque Graphics Pipeline"), layout: &object_pipeline_layout, vertex_shader: &vertex_shader, fragment_shader: &fragment_shader, @@ -62,6 +66,7 @@ impl Renderer { }; let material = graphics_device.create_buffer(&BufferDescriptor { + label: Some("Material Buffer"), data: bytemuck::cast_slice(&[Material { base_color: Vec4::new(1.0, 0.0, 0.0, 1.0), }]), @@ -69,6 +74,7 @@ impl Renderer { }); let positions = graphics_device.create_buffer(&BufferDescriptor { + label: Some("Positions Buffer"), data: bytemuck::cast_slice(&[ Vec4::new(-0.5, -0.5, 0.0, 1.0), Vec4::new(0.5, -0.5, 0.0, 1.0), @@ -79,11 +85,13 @@ impl Renderer { }); let normals = graphics_device.create_buffer(&BufferDescriptor { + label: Some("Normals Buffer"), data: bytemuck::cast_slice(&[Vec4::ZERO, Vec4::ZERO, Vec4::ZERO, Vec4::ZERO]), usage: BufferUsage::STORAGE, }); let mesh = graphics_device.create_buffer(&BufferDescriptor { + label: Some("Mesh Buffer"), data: bytemuck::cast_slice(&[Mesh { positions: positions.handle(), normals: normals.handle(), @@ -93,6 +101,7 @@ impl Renderer { }); let indices = graphics_device.create_buffer(&BufferDescriptor { + label: Some("Index Buffer"), data: bytemuck::cast_slice(&[0_u32, 1_u32, 2_u32, 2_u32, 3_u32, 0_u32, 0, 0]), usage: BufferUsage::INDEX, }); @@ -123,6 +132,7 @@ impl Renderer { { let mut render_pass = command_encoder.begin_render_pass(&RenderPassDescriptor { + label: Some("Main Render Pass"), texture: &swapchain_texture, }); diff --git a/crates/hyper_rhi/src/buffer.rs b/crates/hyper_rhi/src/buffer.rs index d329dedc..bd51c630 100644 --- a/crates/hyper_rhi/src/buffer.rs +++ b/crates/hyper_rhi/src/buffer.rs @@ -23,6 +23,7 @@ pub(crate) const ALIGNMENT: usize = 64 * 1024; #[derive(Clone, Debug)] pub struct BufferDescriptor<'a> { + pub label: Option<&'a str>, pub data: &'a [u8], pub usage: BufferUsage, } diff --git a/crates/hyper_rhi/src/commands/render_pass.rs b/crates/hyper_rhi/src/commands/render_pass.rs index 61e3deab..54f0282c 100644 --- a/crates/hyper_rhi/src/commands/render_pass.rs +++ b/crates/hyper_rhi/src/commands/render_pass.rs @@ -15,6 +15,7 @@ use crate::{ #[derive(Clone, Debug)] pub struct RenderPassDescriptor<'a> { + pub label: Option<&'a str>, pub texture: &'a Arc, } diff --git a/crates/hyper_rhi/src/d3d12/buffer.rs b/crates/hyper_rhi/src/d3d12/buffer.rs index 61bcb837..68d6fc1f 100644 --- a/crates/hyper_rhi/src/d3d12/buffer.rs +++ b/crates/hyper_rhi/src/d3d12/buffer.rs @@ -63,6 +63,10 @@ impl Buffer { let resource_handle_pair = graphics_device.allocate_buffer_handle(resource.resource(), size); + if let Some(label) = descriptor.label { + graphics_device.set_debug_name(resource.resource(), label); + } + tracing::debug!( size, aligned_size, diff --git a/crates/hyper_rhi/src/d3d12/graphics_device.rs b/crates/hyper_rhi/src/d3d12/graphics_device.rs index 0a9ae8e0..2d2a378b 100644 --- a/crates/hyper_rhi/src/d3d12/graphics_device.rs +++ b/crates/hyper_rhi/src/d3d12/graphics_device.rs @@ -15,7 +15,7 @@ use gpu_allocator::{ AllocatorDebugSettings, }; use windows::{ - core::Interface, + core::{Interface, HSTRING}, Win32::{ Foundation::{CloseHandle, HANDLE}, Graphics::{ @@ -29,6 +29,7 @@ use windows::{ ID3D12Device, ID3D12Fence, ID3D12GraphicsCommandList, + ID3D12Object, ID3D12Resource, D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_QUEUE_DESC, @@ -293,6 +294,12 @@ impl GraphicsDevice { command_list } + pub(crate) fn set_debug_name(&self, object: &ID3D12Object, label: &str) { + unsafe { + object.SetName(&HSTRING::from(label)).unwrap(); + } + } + pub(crate) fn allocate_buffer_handle( &self, resource: &ID3D12Resource, diff --git a/crates/hyper_rhi/src/d3d12/graphics_pipeline.rs b/crates/hyper_rhi/src/d3d12/graphics_pipeline.rs index 354fe4d1..c219219b 100644 --- a/crates/hyper_rhi/src/d3d12/graphics_pipeline.rs +++ b/crates/hyper_rhi/src/d3d12/graphics_pipeline.rs @@ -113,13 +113,17 @@ impl GraphicsPipeline { }; state_descriptor.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; - let pipeline_state = unsafe { + let pipeline_state: ID3D12PipelineState = unsafe { graphics_device .device() .CreateGraphicsPipelineState(&state_descriptor) } .unwrap(); + if let Some(label) = descriptor.label { + graphics_device.set_debug_name(&pipeline_state, label); + } + Self { pipeline_state, diff --git a/crates/hyper_rhi/src/d3d12/pipeline_layout.rs b/crates/hyper_rhi/src/d3d12/pipeline_layout.rs index 1336d211..efeba292 100644 --- a/crates/hyper_rhi/src/d3d12/pipeline_layout.rs +++ b/crates/hyper_rhi/src/d3d12/pipeline_layout.rs @@ -67,7 +67,7 @@ impl PipelineLayout { let signature = signature.unwrap(); - let root_signature = unsafe { + let root_signature: ID3D12RootSignature = unsafe { graphics_device.device().CreateRootSignature( 0, slice::from_raw_parts(signature.GetBufferPointer() as _, signature.GetBufferSize()), @@ -75,6 +75,10 @@ impl PipelineLayout { } .unwrap(); + if let Some(label) = descriptor.label { + graphics_device.set_debug_name(&root_signature, label); + } + Self { push_constants_size: descriptor.push_constants_size, root_signature, diff --git a/crates/hyper_rhi/src/d3d12/shader_module.rs b/crates/hyper_rhi/src/d3d12/shader_module.rs index ab55e103..e4715fe5 100644 --- a/crates/hyper_rhi/src/d3d12/shader_module.rs +++ b/crates/hyper_rhi/src/d3d12/shader_module.rs @@ -25,6 +25,8 @@ impl ShaderModule { OutputApi::D3D12, ); + // TODO: Add label + Self { entry_point: descriptor.entry_point.to_owned(), stage: descriptor.stage, diff --git a/crates/hyper_rhi/src/d3d12/upload_manager.rs b/crates/hyper_rhi/src/d3d12/upload_manager.rs index ecb1927c..cb4c4370 100644 --- a/crates/hyper_rhi/src/d3d12/upload_manager.rs +++ b/crates/hyper_rhi/src/d3d12/upload_manager.rs @@ -126,6 +126,7 @@ impl UploadManager { let buffer = Buffer::new_staging( graphics_device, &BufferDescriptor { + label: Some("Staging Buffer"), data: source, usage: BufferUsage::STORAGE, }, diff --git a/crates/hyper_rhi/src/graphics_pipeline.rs b/crates/hyper_rhi/src/graphics_pipeline.rs index f58790ae..ce397a02 100644 --- a/crates/hyper_rhi/src/graphics_pipeline.rs +++ b/crates/hyper_rhi/src/graphics_pipeline.rs @@ -12,6 +12,7 @@ use crate::{pipeline_layout::PipelineLayout, shader_module::ShaderModule}; #[derive(Clone, Debug)] pub struct GraphicsPipelineDescriptor<'a> { + pub label: Option<&'a str>, pub layout: &'a Arc, pub vertex_shader: &'a Arc, pub fragment_shader: &'a Arc, diff --git a/crates/hyper_rhi/src/pipeline_layout.rs b/crates/hyper_rhi/src/pipeline_layout.rs index 3c26e48a..a7f31764 100644 --- a/crates/hyper_rhi/src/pipeline_layout.rs +++ b/crates/hyper_rhi/src/pipeline_layout.rs @@ -9,7 +9,8 @@ use std::fmt::Debug; use downcast_rs::Downcast; #[derive(Clone, Debug)] -pub struct PipelineLayoutDescriptor { +pub struct PipelineLayoutDescriptor<'a> { + pub label: Option<&'a str>, pub push_constants_size: usize, } diff --git a/crates/hyper_rhi/src/shader_module.rs b/crates/hyper_rhi/src/shader_module.rs index 863ab7b4..5944c2ce 100644 --- a/crates/hyper_rhi/src/shader_module.rs +++ b/crates/hyper_rhi/src/shader_module.rs @@ -17,6 +17,7 @@ pub enum ShaderStage { #[derive(Clone, Debug)] pub struct ShaderModuleDescriptor<'a> { + pub label: Option<&'a str>, pub path: &'a str, pub entry_point: &'a str, pub stage: ShaderStage, diff --git a/crates/hyper_rhi/src/texture.rs b/crates/hyper_rhi/src/texture.rs index 739e481e..010533be 100644 --- a/crates/hyper_rhi/src/texture.rs +++ b/crates/hyper_rhi/src/texture.rs @@ -11,7 +11,9 @@ use downcast_rs::Downcast; use crate::resource::Resource; #[derive(Clone, Debug)] -pub struct TextureDescriptor {} +pub struct TextureDescriptor<'a> { + pub label: Option<&'a str>, +} pub trait Texture: Debug + Downcast + Resource { fn width(&self) -> u32; diff --git a/crates/hyper_rhi/src/vulkan/buffer.rs b/crates/hyper_rhi/src/vulkan/buffer.rs index 871dc296..18427eb4 100644 --- a/crates/hyper_rhi/src/vulkan/buffer.rs +++ b/crates/hyper_rhi/src/vulkan/buffer.rs @@ -43,6 +43,12 @@ impl Buffer { let resource_handle = graphics_device.allocate_buffer_handle(buffer); + if let Some(label) = descriptor.label { + graphics_device.set_debug_name(buffer, label); + graphics_device + .set_debug_name(unsafe { allocation.memory() }, &format!("{} Memory", label)); + } + tracing::debug!( size, aligned_size, diff --git a/crates/hyper_rhi/src/vulkan/graphics_device.rs b/crates/hyper_rhi/src/vulkan/graphics_device.rs index 166126c3..5aaefb6e 100644 --- a/crates/hyper_rhi/src/vulkan/graphics_device.rs +++ b/crates/hyper_rhi/src/vulkan/graphics_device.rs @@ -6,7 +6,7 @@ use std::{ collections::HashSet, - ffi::{c_void, CStr}, + ffi::{c_void, CStr, CString}, sync::{Arc, Mutex}, }; @@ -66,6 +66,7 @@ pub(crate) struct FrameData { struct DebugUtils { debug_messenger: vk::DebugUtilsMessengerEXT, + device: Option, loader: debug_utils::Instance, } @@ -107,7 +108,7 @@ impl GraphicsDevice { let instance = Self::create_instance(descriptor.display_handle, &entry, validation_layers_enabled); - let debug_utils = if validation_layers_enabled { + let mut debug_utils = if validation_layers_enabled { Some(Self::create_debug_utils(&entry, &instance)) } else { None @@ -126,6 +127,10 @@ impl GraphicsDevice { let device = Self::create_device(&instance, physical_device, queue_family_index); let queue = unsafe { device.get_device_queue(queue_family_index, 0) }; + if let Some(debug_utils) = debug_utils.as_mut() { + debug_utils.device = Some(debug_utils::Device::new(&instance, &device)); + } + let allocator = Arc::new(Mutex::new( Allocator::new(&AllocatorCreateDesc { instance: instance.clone(), @@ -274,6 +279,7 @@ impl GraphicsDevice { DebugUtils { debug_messenger, + device: None, loader, } } @@ -563,6 +569,30 @@ impl GraphicsDevice { texture_views.clear(); } + pub(crate) fn set_debug_name(&self, object: T, label: &str) + where + T: vk::Handle, + { + // TODO: Ensure debug is enabled + + let label = CString::new(label).unwrap(); + let label_str = label.as_c_str(); + let name_info = vk::DebugUtilsObjectNameInfoEXT::default() + .object_handle(object) + .object_name(label_str); + + unsafe { + self.debug_utils + .as_ref() + .unwrap() + .device + .as_ref() + .unwrap() + .set_debug_utils_object_name(&name_info) + .unwrap(); + } + } + pub(crate) fn allocate_buffer_handle(&self, buffer: vk::Buffer) -> ResourceHandle { self.descriptor_manager.allocate_buffer_handle(self, buffer) } diff --git a/crates/hyper_rhi/src/vulkan/graphics_pipeline.rs b/crates/hyper_rhi/src/vulkan/graphics_pipeline.rs index e93edb39..f154cdcd 100644 --- a/crates/hyper_rhi/src/vulkan/graphics_pipeline.rs +++ b/crates/hyper_rhi/src/vulkan/graphics_pipeline.rs @@ -162,6 +162,10 @@ impl GraphicsPipeline { .unwrap()[0] }; + if let Some(label) = descriptor.label { + graphics_device.set_debug_name(pipeline, label); + } + Self { pipeline, diff --git a/crates/hyper_rhi/src/vulkan/pipeline_layout.rs b/crates/hyper_rhi/src/vulkan/pipeline_layout.rs index a6ecd445..a1c2894b 100644 --- a/crates/hyper_rhi/src/vulkan/pipeline_layout.rs +++ b/crates/hyper_rhi/src/vulkan/pipeline_layout.rs @@ -36,6 +36,10 @@ impl PipelineLayout { } .unwrap(); + if let Some(label) = descriptor.label { + graphics_device.set_debug_name(layout, label); + } + Self { push_constants_size: descriptor.push_constants_size, layout, diff --git a/crates/hyper_rhi/src/vulkan/shader_module.rs b/crates/hyper_rhi/src/vulkan/shader_module.rs index 37fe7119..58056289 100644 --- a/crates/hyper_rhi/src/vulkan/shader_module.rs +++ b/crates/hyper_rhi/src/vulkan/shader_module.rs @@ -49,6 +49,10 @@ impl ShaderModule { } .unwrap(); + if let Some(label) = descriptor.label { + graphics_device.set_debug_name(shader_module, label); + } + Self { entry_point: descriptor.entry_point.to_owned(), stage: descriptor.stage, diff --git a/crates/hyper_rhi/src/vulkan/upload_manager.rs b/crates/hyper_rhi/src/vulkan/upload_manager.rs index 17479e16..2880e484 100644 --- a/crates/hyper_rhi/src/vulkan/upload_manager.rs +++ b/crates/hyper_rhi/src/vulkan/upload_manager.rs @@ -155,6 +155,7 @@ impl UploadManager { let buffer = Buffer::new_staging( graphics_device, &BufferDescriptor { + label: Some("Staging Buffer"), data: source, usage: BufferUsage::STORAGE, },