Skip to content

Commit a9ac317

Browse files
committed
Port dx12 backend to new suballocator
1 parent 594d4bc commit a9ac317

6 files changed

Lines changed: 1991 additions & 400 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wgpu-hal/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ dx12 = [
154154
"dep:parking_lot",
155155
"dep:profiling",
156156
"dep:range-alloc",
157+
"dep:wgpu-block-pool",
158+
"dep:wgpu-offset-allocator",
157159
"dep:windows-core",
158-
"gpu-allocator/d3d12",
159160
"naga/hlsl-out",
160161
"once_cell/std",
161162
"windows/Win32_Devices_DeviceAndDriverInstallation",
@@ -218,7 +219,7 @@ naga.workspace = true
218219
wgpu-naga-bridge.workspace = true
219220
naga-types = { workspace = true }
220221
wgpu-types = { workspace = true, default-features = false }
221-
# Backend: Vulkan (in-repo suballocator)
222+
# Backend: Vulkan and Dx12 (in-repo suballocator)
222223
wgpu-block-pool = { workspace = true, optional = true }
223224
wgpu-offset-allocator = { workspace = true, optional = true }
224225

wgpu-hal/src/dx12/command.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ impl super::CommandEncoder {
305305
&crate::dx12::QuerySet {
306306
raw: query_set_raw,
307307
raw_ty: Direct3D12::D3D12_QUERY_TYPE_TIMESTAMP,
308+
tracked_query_bytes: 0,
308309
},
309310
index,
310311
);

wgpu-hal/src/dx12/device.rs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ impl super::Device {
6060
auxil::dxgi::exception::register_exception_handler();
6161
}
6262

63-
let mem_allocator =
64-
suballocation::Allocator::new(&raw, memory_hints, memory_budget_thresholds)?;
63+
let mem_allocator = suballocation::Allocator::new(
64+
&raw,
65+
memory_hints,
66+
&private_caps,
67+
memory_budget_thresholds,
68+
)?;
6569

6670
let idle_fence: Direct3D12::ID3D12Fence = unsafe {
6771
profiling::scope!("ID3D12Device::CreateFence");
@@ -2293,26 +2297,17 @@ impl crate::Device for super::Device {
22932297
),
22942298
};
22952299

2296-
if let Some(threshold) = self
2300+
// Query heaps allocate device memory outside the pool machinery, so we
2301+
// gate them through the allocator's budget probe. Assume each query is
2302+
// 256 bytes. On an AMD W6800 with driver version 32.0.12030.9, occlusion
2303+
// and pipeline statistics are 256 bytes, timestamp is 8.
2304+
let query_bytes = (desc.count as u64).saturating_mul(256);
2305+
let tracked_query_bytes = self
22972306
.mem_allocator
2298-
.memory_budget_thresholds
2299-
.for_resource_creation
2300-
{
2301-
let info = self
2302-
.shared
2303-
.adapter
2304-
.query_video_memory_info(Dxgi::DXGI_MEMORY_SEGMENT_GROUP_LOCAL)?;
2305-
2306-
// Assume each query is 256 bytes.
2307-
// On an AMD W6800 with driver version 32.0.12030.9, occlusion and pipeline statistics are 256, timestamp is 8.
2308-
2309-
if info.CurrentUsage + desc.count as u64 * 256 >= info.Budget / 100 * threshold as u64 {
2310-
return Err(crate::DeviceError::OutOfMemory);
2311-
}
2312-
}
2307+
.add_external_allocation(&self.shared.adapter, query_bytes)?;
23132308

23142309
let mut raw = None::<Direct3D12::ID3D12QueryHeap>;
2315-
unsafe {
2310+
let create_result = unsafe {
23162311
self.raw.CreateQueryHeap(
23172312
&Direct3D12::D3D12_QUERY_HEAP_DESC {
23182313
Type: heap_ty,
@@ -2322,20 +2317,39 @@ impl crate::Device for super::Device {
23222317
&mut raw,
23232318
)
23242319
}
2325-
.into_device_result("Query heap creation")?;
2320+
.into_device_result("Query heap creation");
2321+
if let Err(error) = create_result {
2322+
self.mem_allocator
2323+
.remove_external_allocation(tracked_query_bytes);
2324+
return Err(error);
2325+
}
23262326

2327-
let raw = raw.ok_or(crate::DeviceError::Unexpected)?;
2327+
let Some(raw) = raw else {
2328+
self.mem_allocator
2329+
.remove_external_allocation(tracked_query_bytes);
2330+
return Err(crate::DeviceError::Unexpected);
2331+
};
23282332

23292333
if let Some(label) = desc.label {
2330-
raw.set_name(label)?;
2334+
if let Err(error) = raw.set_name(label) {
2335+
self.mem_allocator
2336+
.remove_external_allocation(tracked_query_bytes);
2337+
return Err(error);
2338+
}
23312339
}
23322340

23332341
self.counters.query_sets.add(1);
23342342

2335-
Ok(super::QuerySet { raw, raw_ty })
2343+
Ok(super::QuerySet {
2344+
raw,
2345+
raw_ty,
2346+
tracked_query_bytes,
2347+
})
23362348
}
23372349

2338-
unsafe fn destroy_query_set(&self, _set: super::QuerySet) {
2350+
unsafe fn destroy_query_set(&self, set: super::QuerySet) {
2351+
self.mem_allocator
2352+
.remove_external_allocation(set.tracked_query_bytes);
23392353
self.counters.query_sets.sub(1);
23402354
}
23412355

wgpu-hal/src/dx12/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,6 @@ enum MemoryArchitecture {
559559
struct PrivateCapabilities {
560560
instance_flags: wgt::InstanceFlags,
561561
workarounds: Workarounds,
562-
#[allow(unused)]
563562
heterogeneous_resource_heaps: bool,
564563
memory_architecture: MemoryArchitecture,
565564
heap_create_not_zeroed: bool,
@@ -1087,6 +1086,7 @@ static_assertions::assert_impl_all!(Sampler: Send, Sync);
10871086
pub struct QuerySet {
10881087
raw: Direct3D12::ID3D12QueryHeap,
10891088
raw_ty: Direct3D12::D3D12_QUERY_TYPE,
1089+
tracked_query_bytes: u64,
10901090
}
10911091

10921092
impl crate::DynQuerySet for QuerySet {}

0 commit comments

Comments
 (0)