[d3d12] refactor buffer/texture creation functions to return resources instead of using out params

This commit is contained in:
teoxoy 2024-08-21 12:24:39 +02:00 committed by Teodor Tanasoaia
parent 85346dfd20
commit 43f6279605
2 changed files with 28 additions and 43 deletions

View File

@ -388,7 +388,6 @@ impl crate::Device for super::Device {
&self,
desc: &crate::BufferDescriptor,
) -> Result<super::Buffer, crate::DeviceError> {
let mut resource = None;
let mut size = desc.size;
if desc.usage.contains(crate::BufferUses::UNIFORM) {
let align_mask = Direct3D12::D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT as u64 - 1;
@ -411,10 +410,8 @@ impl crate::Device for super::Device {
Flags: conv::map_buffer_usage_to_resource_flags(desc.usage),
};
let allocation =
super::suballocation::create_buffer_resource(self, desc, raw_desc, &mut resource)?;
let resource = resource.ok_or(crate::DeviceError::ResourceCreationFailed)?;
let (resource, allocation) =
super::suballocation::create_buffer_resource(self, desc, raw_desc)?;
if let Some(label) = desc.label {
unsafe { resource.SetName(&windows::core::HSTRING::from(label)) }
@ -471,10 +468,6 @@ impl crate::Device for super::Device {
&self,
desc: &crate::TextureDescriptor,
) -> Result<super::Texture, crate::DeviceError> {
use super::suballocation::create_texture_resource;
let mut resource = None;
let raw_desc = Direct3D12::D3D12_RESOURCE_DESC {
Dimension: conv::map_texture_dimension(desc.dimension),
Alignment: 0,
@ -496,9 +489,9 @@ impl crate::Device for super::Device {
Flags: conv::map_texture_usage_to_resource_flags(desc.usage),
};
let allocation = create_texture_resource(self, desc, raw_desc, &mut resource)?;
let (resource, allocation) =
super::suballocation::create_texture_resource(self, desc, raw_desc)?;
let resource = resource.ok_or(crate::DeviceError::ResourceCreationFailed)?;
if let Some(label) = desc.label {
unsafe { resource.SetName(&windows::core::HSTRING::from(label)) }
.into_device_result("SetName")?;

View File

@ -52,14 +52,14 @@ pub(crate) fn create_buffer_resource(
device: &crate::dx12::Device,
desc: &crate::BufferDescriptor,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
resource: &mut Option<Direct3D12::ID3D12Resource>,
) -> Result<Option<AllocationWrapper>, crate::DeviceError> {
) -> Result<(Direct3D12::ID3D12Resource, Option<AllocationWrapper>), crate::DeviceError> {
let is_cpu_read = desc.usage.contains(crate::BufferUses::MAP_READ);
let is_cpu_write = desc.usage.contains(crate::BufferUses::MAP_WRITE);
// Workaround for Intel Xe drivers
if !device.private_caps.suballocation_supported {
return create_committed_buffer_resource(device, desc, raw_desc, resource).map(|()| None);
return create_committed_buffer_resource(device, desc, raw_desc)
.map(|resource| (resource, None));
}
let location = match (is_cpu_read, is_cpu_write) {
@ -80,6 +80,7 @@ pub(crate) fn create_buffer_resource(
location,
);
let allocation = allocator.allocator.allocate(&allocation_desc)?;
let mut resource = None;
unsafe {
device.raw.CreatePlacedResource(
@ -88,32 +89,30 @@ pub(crate) fn create_buffer_resource(
&raw_desc,
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
None,
resource,
&mut resource,
)
}
.into_device_result("Placed buffer creation")?;
if resource.is_none() {
return Err(crate::DeviceError::ResourceCreationFailed);
}
let resource = resource.ok_or(crate::DeviceError::ResourceCreationFailed)?;
device
.counters
.buffer_memory
.add(allocation.size() as isize);
Ok(Some(AllocationWrapper { allocation }))
Ok((resource, Some(AllocationWrapper { allocation })))
}
pub(crate) fn create_texture_resource(
device: &crate::dx12::Device,
desc: &crate::TextureDescriptor,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
resource: &mut Option<Direct3D12::ID3D12Resource>,
) -> Result<Option<AllocationWrapper>, crate::DeviceError> {
) -> Result<(Direct3D12::ID3D12Resource, Option<AllocationWrapper>), crate::DeviceError> {
// Workaround for Intel Xe drivers
if !device.private_caps.suballocation_supported {
return create_committed_texture_resource(device, desc, raw_desc, resource).map(|()| None);
return create_committed_texture_resource(device, desc, raw_desc)
.map(|resource| (resource, None));
}
let location = MemoryLocation::GpuOnly;
@ -128,6 +127,7 @@ pub(crate) fn create_texture_resource(
location,
);
let allocation = allocator.allocator.allocate(&allocation_desc)?;
let mut resource = None;
unsafe {
device.raw.CreatePlacedResource(
@ -136,21 +136,19 @@ pub(crate) fn create_texture_resource(
&raw_desc,
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
None, // clear value
resource,
&mut resource,
)
}
.into_device_result("Placed texture creation")?;
if resource.is_none() {
return Err(crate::DeviceError::ResourceCreationFailed);
}
let resource = resource.ok_or(crate::DeviceError::ResourceCreationFailed)?;
device
.counters
.texture_memory
.add(allocation.size() as isize);
Ok(Some(AllocationWrapper { allocation }))
Ok((resource, Some(AllocationWrapper { allocation })))
}
pub(crate) fn free_buffer_allocation(
@ -226,8 +224,7 @@ pub(crate) fn create_committed_buffer_resource(
device: &crate::dx12::Device,
desc: &crate::BufferDescriptor,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
resource: &mut Option<Direct3D12::ID3D12Resource>,
) -> Result<(), crate::DeviceError> {
) -> Result<Direct3D12::ID3D12Resource, crate::DeviceError> {
let is_cpu_read = desc.usage.contains(crate::BufferUses::MAP_READ);
let is_cpu_write = desc.usage.contains(crate::BufferUses::MAP_WRITE);
@ -250,6 +247,8 @@ pub(crate) fn create_committed_buffer_resource(
VisibleNodeMask: 0,
};
let mut resource = None;
unsafe {
device.raw.CreateCommittedResource(
&heap_properties,
@ -261,24 +260,19 @@ pub(crate) fn create_committed_buffer_resource(
&raw_desc,
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
None,
resource,
&mut resource,
)
}
.into_device_result("Committed buffer creation")?;
if resource.is_none() {
return Err(crate::DeviceError::ResourceCreationFailed);
}
Ok(())
resource.ok_or(crate::DeviceError::ResourceCreationFailed)
}
pub(crate) fn create_committed_texture_resource(
device: &crate::dx12::Device,
_desc: &crate::TextureDescriptor,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
resource: &mut Option<Direct3D12::ID3D12Resource>,
) -> Result<(), crate::DeviceError> {
) -> Result<Direct3D12::ID3D12Resource, crate::DeviceError> {
let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES {
Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM,
CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE,
@ -290,6 +284,8 @@ pub(crate) fn create_committed_texture_resource(
VisibleNodeMask: 0,
};
let mut resource = None;
unsafe {
device.raw.CreateCommittedResource(
&heap_properties,
@ -301,14 +297,10 @@ pub(crate) fn create_committed_texture_resource(
&raw_desc,
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
None, // clear value
resource,
&mut resource,
)
}
.into_device_result("Committed texture creation")?;
if resource.is_none() {
return Err(crate::DeviceError::ResourceCreationFailed);
}
Ok(())
resource.ok_or(crate::DeviceError::ResourceCreationFailed)
}