[d3d12] Simplify set_name calls with a helper

This commit is contained in:
Connor Fitzgerald
2025-04-07 18:00:21 -04:00
committed by Erich Gubler
parent 6262462979
commit a1e96cc179
4 changed files with 43 additions and 22 deletions

View File

@@ -3,5 +3,6 @@
pub mod conv;
pub mod exception;
pub mod factory;
pub mod name;
pub mod result;
pub mod time;

View File

@@ -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<T> 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") }
}
}

View File

@@ -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);

View File

@@ -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::<ffi::c_void>();
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