Skip to content

Commit

Permalink
feat: add debug labels
Browse files Browse the repository at this point in the history
  • Loading branch information
SkillerRaptor committed Sep 18, 2024
1 parent 70fa9d9 commit 6b2538e
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 7 deletions.
10 changes: 10 additions & 0 deletions crates/hyper_render/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,43 @@ impl Renderer {
pub fn new(graphics_device: &Arc<dyn GraphicsDevice>) -> Self {
let object_pipeline_layout =
graphics_device.create_pipeline_layout(&PipelineLayoutDescriptor {
label: Some("Object Pipeline Layout"),
push_constants_size: size_of::<ObjectPushConstants>(),
});

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,
})
};

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),
}]),
usage: BufferUsage::STORAGE,
});

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),
Expand All @@ -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(),
Expand All @@ -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,
});
Expand Down Expand Up @@ -123,6 +132,7 @@ impl Renderer {

{
let mut render_pass = command_encoder.begin_render_pass(&RenderPassDescriptor {
label: Some("Main Render Pass"),
texture: &swapchain_texture,
});

Expand Down
1 change: 1 addition & 0 deletions crates/hyper_rhi/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions crates/hyper_rhi/src/commands/render_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{

#[derive(Clone, Debug)]
pub struct RenderPassDescriptor<'a> {
pub label: Option<&'a str>,
pub texture: &'a Arc<dyn Texture>,
}

Expand Down
4 changes: 4 additions & 0 deletions crates/hyper_rhi/src/d3d12/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion crates/hyper_rhi/src/d3d12/graphics_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use gpu_allocator::{
AllocatorDebugSettings,
};
use windows::{
core::Interface,
core::{Interface, HSTRING},
Win32::{
Foundation::{CloseHandle, HANDLE},
Graphics::{
Expand All @@ -29,6 +29,7 @@ use windows::{
ID3D12Device,
ID3D12Fence,
ID3D12GraphicsCommandList,
ID3D12Object,
ID3D12Resource,
D3D12_COMMAND_LIST_TYPE_DIRECT,
D3D12_COMMAND_QUEUE_DESC,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion crates/hyper_rhi/src/d3d12/graphics_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
6 changes: 5 additions & 1 deletion crates/hyper_rhi/src/d3d12/pipeline_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,18 @@ 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()),
)
}
.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,
Expand Down
2 changes: 2 additions & 0 deletions crates/hyper_rhi/src/d3d12/shader_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ impl ShaderModule {
OutputApi::D3D12,
);

// TODO: Add label

Self {
entry_point: descriptor.entry_point.to_owned(),
stage: descriptor.stage,
Expand Down
1 change: 1 addition & 0 deletions crates/hyper_rhi/src/d3d12/upload_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl UploadManager {
let buffer = Buffer::new_staging(
graphics_device,
&BufferDescriptor {
label: Some("Staging Buffer"),
data: source,
usage: BufferUsage::STORAGE,
},
Expand Down
1 change: 1 addition & 0 deletions crates/hyper_rhi/src/graphics_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn PipelineLayout>,
pub vertex_shader: &'a Arc<dyn ShaderModule>,
pub fragment_shader: &'a Arc<dyn ShaderModule>,
Expand Down
3 changes: 2 additions & 1 deletion crates/hyper_rhi/src/pipeline_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
1 change: 1 addition & 0 deletions crates/hyper_rhi/src/shader_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion crates/hyper_rhi/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions crates/hyper_rhi/src/vulkan/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 32 additions & 2 deletions crates/hyper_rhi/src/vulkan/graphics_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use std::{
collections::HashSet,
ffi::{c_void, CStr},
ffi::{c_void, CStr, CString},
sync::{Arc, Mutex},
};

Expand Down Expand Up @@ -66,6 +66,7 @@ pub(crate) struct FrameData {

struct DebugUtils {
debug_messenger: vk::DebugUtilsMessengerEXT,
device: Option<debug_utils::Device>,
loader: debug_utils::Instance,
}

Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -274,6 +279,7 @@ impl GraphicsDevice {

DebugUtils {
debug_messenger,
device: None,
loader,
}
}
Expand Down Expand Up @@ -563,6 +569,30 @@ impl GraphicsDevice {
texture_views.clear();
}

pub(crate) fn set_debug_name<T>(&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)
}
Expand Down
4 changes: 4 additions & 0 deletions crates/hyper_rhi/src/vulkan/graphics_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ impl GraphicsPipeline {
.unwrap()[0]
};

if let Some(label) = descriptor.label {
graphics_device.set_debug_name(pipeline, label);
}

Self {
pipeline,

Expand Down
4 changes: 4 additions & 0 deletions crates/hyper_rhi/src/vulkan/pipeline_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions crates/hyper_rhi/src/vulkan/shader_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/hyper_rhi/src/vulkan/upload_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ impl UploadManager {
let buffer = Buffer::new_staging(
graphics_device,
&BufferDescriptor {
label: Some("Staging Buffer"),
data: source,
usage: BufferUsage::STORAGE,
},
Expand Down

0 comments on commit 6b2538e

Please sign in to comment.