From a1e96cc17982c6ed5d443e8acebe51343285ff60 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Mon, 7 Apr 2025 18:00:21 -0400 Subject: [PATCH] [d3d12] Simplify `set_name` calls with a helper --- wgpu-hal/src/auxil/dxgi/mod.rs | 1 + wgpu-hal/src/auxil/dxgi/name.rs | 24 ++++++++++++++++++++++++ wgpu-hal/src/dx12/command.rs | 8 +++++--- wgpu-hal/src/dx12/device.rs | 32 +++++++++++++------------------- 4 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 wgpu-hal/src/auxil/dxgi/name.rs diff --git a/wgpu-hal/src/auxil/dxgi/mod.rs b/wgpu-hal/src/auxil/dxgi/mod.rs index 9b945923b8..1ab2e85003 100644 --- a/wgpu-hal/src/auxil/dxgi/mod.rs +++ b/wgpu-hal/src/auxil/dxgi/mod.rs @@ -3,5 +3,6 @@ pub mod conv; pub mod exception; pub mod factory; +pub mod name; pub mod result; pub mod time; diff --git a/wgpu-hal/src/auxil/dxgi/name.rs b/wgpu-hal/src/auxil/dxgi/name.rs new file mode 100644 index 0000000000..d120e4ed15 --- /dev/null +++ b/wgpu-hal/src/auxil/dxgi/name.rs @@ -0,0 +1,24 @@ +use windows::Win32::Graphics::Direct3D12::ID3D12Object; + +use crate::auxil::dxgi::result::HResult; + +/// Helper trait for setting the name of a D3D12 object. +/// +/// This is implemented on all types that can be converted to an [`ID3D12Object`]. +pub trait ObjectExt { + fn set_name(&self, name: &str) -> Result<(), crate::DeviceError>; +} + +impl ObjectExt for T +where + // Windows impls `From` for all parent interfaces, so we can use that to convert to ID3D12Object. + // + // This includes implementations for references. + for<'a> &'a ID3D12Object: From<&'a T>, +{ + fn set_name(&self, name: &str) -> Result<(), crate::DeviceError> { + let name = windows::core::HSTRING::from(name); + let object: &ID3D12Object = self.into(); + unsafe { object.SetName(&name).into_device_result("SetName") } + } +} diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 5ca6b87cdc..a14cada4f0 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -8,7 +8,10 @@ use windows_core::Interface; use super::conv; use crate::{ - auxil::{self, dxgi::result::HResult as _}, + auxil::{ + self, + dxgi::{name::ObjectExt, result::HResult as _}, + }, dx12::borrow_interface_temporarily, AccelerationStructureEntries, }; @@ -328,8 +331,7 @@ impl crate::CommandEncoder for super::CommandEncoder { }; if let Some(label) = label { - unsafe { list.SetName(&windows::core::HSTRING::from(label)) } - .into_device_result("SetName")?; + list.set_name(label)?; } self.list = Some(list); diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index acd56a79a8..61b33fa309 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -22,7 +22,10 @@ use windows::{ use super::{conv, descriptor, D3D12Lib}; use crate::{ - auxil::{self, dxgi::result::HResult}, + auxil::{ + self, + dxgi::{name::ObjectExt, result::HResult}, + }, dx12::{ borrow_optional_interface_temporarily, shader_compilation, suballocation, DynamicStorageBufferOffsets, Event, @@ -424,8 +427,7 @@ impl crate::Device for super::Device { suballocation::DeviceAllocationContext::from(self).create_buffer(&desc)?; if let Some(label) = desc.label { - unsafe { resource.SetName(&windows::core::HSTRING::from(label)) } - .into_device_result("SetName")?; + resource.set_name(label)?; } self.counters.buffers.add(1); @@ -504,8 +506,7 @@ impl crate::Device for super::Device { suballocation::DeviceAllocationContext::from(self).create_texture(desc, raw_desc)?; if let Some(label) = desc.label { - unsafe { resource.SetName(&windows::core::HSTRING::from(label)) } - .into_device_result("SetName")?; + resource.set_name(label)?; } self.counters.textures.add(1); @@ -720,8 +721,7 @@ impl crate::Device for super::Device { .into_device_result("Command allocator creation")?; if let Some(label) = desc.label { - unsafe { allocator.SetName(&windows::core::HSTRING::from(label)) } - .into_device_result("SetName")?; + allocator.set_name(label)?; } self.counters.command_encoders.add(1); @@ -1315,8 +1315,7 @@ impl crate::Device for super::Device { }; if let Some(label) = desc.label { - unsafe { raw.SetName(&windows::core::HSTRING::from(label)) } - .into_device_result("SetName")?; + raw.set_name(label)?; } self.counters.pipeline_layouts.add(1); @@ -1549,8 +1548,7 @@ impl crate::Device for super::Device { let (buffer, allocation) = suballocation::DeviceAllocationContext::from(self).create_buffer(&buffer_desc)?; - unsafe { buffer.SetName(&windows::core::HSTRING::from(&*label)) } - .into_device_result("SetName")?; + buffer.set_name(&*label)?; let mut mapping = ptr::null_mut::(); unsafe { buffer.Map(0, None, Some(&mut mapping)) }.into_device_result("Map")?; @@ -1835,8 +1833,7 @@ impl crate::Device for super::Device { }; if let Some(label) = desc.label { - unsafe { raw.SetName(&windows::core::HSTRING::from(label)) } - .into_device_result("SetName")?; + raw.set_name(label)?; } self.counters.render_pipelines.add(1); @@ -1899,8 +1896,7 @@ impl crate::Device for super::Device { })?; if let Some(label) = desc.label { - unsafe { raw.SetName(&windows::core::HSTRING::from(label)) } - .into_device_result("SetName")?; + raw.set_name(label)?; } self.counters.compute_pipelines.add(1); @@ -1958,8 +1954,7 @@ impl crate::Device for super::Device { let raw = raw.ok_or(crate::DeviceError::Unexpected)?; if let Some(label) = desc.label { - unsafe { raw.SetName(&windows::core::HSTRING::from(label)) } - .into_device_result("SetName")?; + raw.set_name(label)?; } self.counters.query_sets.add(1); @@ -2253,8 +2248,7 @@ impl crate::Device for super::Device { .create_acceleration_structure(desc, raw_desc)?; if let Some(label) = desc.label { - unsafe { resource.SetName(&windows::core::HSTRING::from(label)) } - .into_device_result("SetName")?; + resource.set_name(label)?; } // for some reason there is no counter for acceleration structures