Skip to content

Commit 43f6279

Browse files
committed
[d3d12] refactor buffer/texture creation functions to return resources instead of using out params
1 parent 85346df commit 43f6279

File tree

2 files changed

+28
-43
lines changed

2 files changed

+28
-43
lines changed

wgpu-hal/src/dx12/device.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ impl crate::Device for super::Device {
388388
&self,
389389
desc: &crate::BufferDescriptor,
390390
) -> Result<super::Buffer, crate::DeviceError> {
391-
let mut resource = None;
392391
let mut size = desc.size;
393392
if desc.usage.contains(crate::BufferUses::UNIFORM) {
394393
let align_mask = Direct3D12::D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT as u64 - 1;
@@ -411,10 +410,8 @@ impl crate::Device for super::Device {
411410
Flags: conv::map_buffer_usage_to_resource_flags(desc.usage),
412411
};
413412

414-
let allocation =
415-
super::suballocation::create_buffer_resource(self, desc, raw_desc, &mut resource)?;
416-
417-
let resource = resource.ok_or(crate::DeviceError::ResourceCreationFailed)?;
413+
let (resource, allocation) =
414+
super::suballocation::create_buffer_resource(self, desc, raw_desc)?;
418415

419416
if let Some(label) = desc.label {
420417
unsafe { resource.SetName(&windows::core::HSTRING::from(label)) }
@@ -471,10 +468,6 @@ impl crate::Device for super::Device {
471468
&self,
472469
desc: &crate::TextureDescriptor,
473470
) -> Result<super::Texture, crate::DeviceError> {
474-
use super::suballocation::create_texture_resource;
475-
476-
let mut resource = None;
477-
478471
let raw_desc = Direct3D12::D3D12_RESOURCE_DESC {
479472
Dimension: conv::map_texture_dimension(desc.dimension),
480473
Alignment: 0,
@@ -496,9 +489,9 @@ impl crate::Device for super::Device {
496489
Flags: conv::map_texture_usage_to_resource_flags(desc.usage),
497490
};
498491

499-
let allocation = create_texture_resource(self, desc, raw_desc, &mut resource)?;
492+
let (resource, allocation) =
493+
super::suballocation::create_texture_resource(self, desc, raw_desc)?;
500494

501-
let resource = resource.ok_or(crate::DeviceError::ResourceCreationFailed)?;
502495
if let Some(label) = desc.label {
503496
unsafe { resource.SetName(&windows::core::HSTRING::from(label)) }
504497
.into_device_result("SetName")?;

wgpu-hal/src/dx12/suballocation.rs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ pub(crate) fn create_buffer_resource(
5252
device: &crate::dx12::Device,
5353
desc: &crate::BufferDescriptor,
5454
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
55-
resource: &mut Option<Direct3D12::ID3D12Resource>,
56-
) -> Result<Option<AllocationWrapper>, crate::DeviceError> {
55+
) -> Result<(Direct3D12::ID3D12Resource, Option<AllocationWrapper>), crate::DeviceError> {
5756
let is_cpu_read = desc.usage.contains(crate::BufferUses::MAP_READ);
5857
let is_cpu_write = desc.usage.contains(crate::BufferUses::MAP_WRITE);
5958

6059
// Workaround for Intel Xe drivers
6160
if !device.private_caps.suballocation_supported {
62-
return create_committed_buffer_resource(device, desc, raw_desc, resource).map(|()| None);
61+
return create_committed_buffer_resource(device, desc, raw_desc)
62+
.map(|resource| (resource, None));
6363
}
6464

6565
let location = match (is_cpu_read, is_cpu_write) {
@@ -80,6 +80,7 @@ pub(crate) fn create_buffer_resource(
8080
location,
8181
);
8282
let allocation = allocator.allocator.allocate(&allocation_desc)?;
83+
let mut resource = None;
8384

8485
unsafe {
8586
device.raw.CreatePlacedResource(
@@ -88,32 +89,30 @@ pub(crate) fn create_buffer_resource(
8889
&raw_desc,
8990
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
9091
None,
91-
resource,
92+
&mut resource,
9293
)
9394
}
9495
.into_device_result("Placed buffer creation")?;
9596

96-
if resource.is_none() {
97-
return Err(crate::DeviceError::ResourceCreationFailed);
98-
}
97+
let resource = resource.ok_or(crate::DeviceError::ResourceCreationFailed)?;
9998

10099
device
101100
.counters
102101
.buffer_memory
103102
.add(allocation.size() as isize);
104103

105-
Ok(Some(AllocationWrapper { allocation }))
104+
Ok((resource, Some(AllocationWrapper { allocation })))
106105
}
107106

108107
pub(crate) fn create_texture_resource(
109108
device: &crate::dx12::Device,
110109
desc: &crate::TextureDescriptor,
111110
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
112-
resource: &mut Option<Direct3D12::ID3D12Resource>,
113-
) -> Result<Option<AllocationWrapper>, crate::DeviceError> {
111+
) -> Result<(Direct3D12::ID3D12Resource, Option<AllocationWrapper>), crate::DeviceError> {
114112
// Workaround for Intel Xe drivers
115113
if !device.private_caps.suballocation_supported {
116-
return create_committed_texture_resource(device, desc, raw_desc, resource).map(|()| None);
114+
return create_committed_texture_resource(device, desc, raw_desc)
115+
.map(|resource| (resource, None));
117116
}
118117

119118
let location = MemoryLocation::GpuOnly;
@@ -128,6 +127,7 @@ pub(crate) fn create_texture_resource(
128127
location,
129128
);
130129
let allocation = allocator.allocator.allocate(&allocation_desc)?;
130+
let mut resource = None;
131131

132132
unsafe {
133133
device.raw.CreatePlacedResource(
@@ -136,21 +136,19 @@ pub(crate) fn create_texture_resource(
136136
&raw_desc,
137137
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
138138
None, // clear value
139-
resource,
139+
&mut resource,
140140
)
141141
}
142142
.into_device_result("Placed texture creation")?;
143143

144-
if resource.is_none() {
145-
return Err(crate::DeviceError::ResourceCreationFailed);
146-
}
144+
let resource = resource.ok_or(crate::DeviceError::ResourceCreationFailed)?;
147145

148146
device
149147
.counters
150148
.texture_memory
151149
.add(allocation.size() as isize);
152150

153-
Ok(Some(AllocationWrapper { allocation }))
151+
Ok((resource, Some(AllocationWrapper { allocation })))
154152
}
155153

156154
pub(crate) fn free_buffer_allocation(
@@ -226,8 +224,7 @@ pub(crate) fn create_committed_buffer_resource(
226224
device: &crate::dx12::Device,
227225
desc: &crate::BufferDescriptor,
228226
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
229-
resource: &mut Option<Direct3D12::ID3D12Resource>,
230-
) -> Result<(), crate::DeviceError> {
227+
) -> Result<Direct3D12::ID3D12Resource, crate::DeviceError> {
231228
let is_cpu_read = desc.usage.contains(crate::BufferUses::MAP_READ);
232229
let is_cpu_write = desc.usage.contains(crate::BufferUses::MAP_WRITE);
233230

@@ -250,6 +247,8 @@ pub(crate) fn create_committed_buffer_resource(
250247
VisibleNodeMask: 0,
251248
};
252249

250+
let mut resource = None;
251+
253252
unsafe {
254253
device.raw.CreateCommittedResource(
255254
&heap_properties,
@@ -261,24 +260,19 @@ pub(crate) fn create_committed_buffer_resource(
261260
&raw_desc,
262261
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
263262
None,
264-
resource,
263+
&mut resource,
265264
)
266265
}
267266
.into_device_result("Committed buffer creation")?;
268267

269-
if resource.is_none() {
270-
return Err(crate::DeviceError::ResourceCreationFailed);
271-
}
272-
273-
Ok(())
268+
resource.ok_or(crate::DeviceError::ResourceCreationFailed)
274269
}
275270

276271
pub(crate) fn create_committed_texture_resource(
277272
device: &crate::dx12::Device,
278273
_desc: &crate::TextureDescriptor,
279274
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
280-
resource: &mut Option<Direct3D12::ID3D12Resource>,
281-
) -> Result<(), crate::DeviceError> {
275+
) -> Result<Direct3D12::ID3D12Resource, crate::DeviceError> {
282276
let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES {
283277
Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM,
284278
CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE,
@@ -290,6 +284,8 @@ pub(crate) fn create_committed_texture_resource(
290284
VisibleNodeMask: 0,
291285
};
292286

287+
let mut resource = None;
288+
293289
unsafe {
294290
device.raw.CreateCommittedResource(
295291
&heap_properties,
@@ -301,14 +297,10 @@ pub(crate) fn create_committed_texture_resource(
301297
&raw_desc,
302298
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
303299
None, // clear value
304-
resource,
300+
&mut resource,
305301
)
306302
}
307303
.into_device_result("Committed texture creation")?;
308304

309-
if resource.is_none() {
310-
return Err(crate::DeviceError::ResourceCreationFailed);
311-
}
312-
313-
Ok(())
305+
resource.ok_or(crate::DeviceError::ResourceCreationFailed)
314306
}

0 commit comments

Comments
 (0)