From 71bda9339a3364b1b804a1bf47f8d66dbb286280 Mon Sep 17 00:00:00 2001 From: Maxime Cambou Date: Tue, 24 Nov 2026 09:22:24 +0100 Subject: [PATCH] D3D12.X: a sparse pool retired a moment ago is taken back (#913) Freeing the last tile in use of a 64 MiB pool retires the pool for at least 2 s; the next tile allocation found the free list empty and made a new heap. A free / alloc loop thus made a new pool each time: #913 counted 288 pools and about 18 GB asked of a 12 GB card in one test. take_tile now takes back the most recently retired pool before creating a heap. Its tiles are flagged for clearing, as freed tiles of a pool in use already are when they are reused at once. Measured on an RTX 5080 16 GB, peak video memory during `cargo test -p xcl --lib -- --test-threads=4` (one run each): main 09940bb 15816 MiB with 3 failed tests (the #718 cubemap check and two poisoned-lock follow-ons); with this change 3488 MiB, 0 failed. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/sparse.rs | 88 ++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index 891fc19466..c6b600b942 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -577,6 +577,14 @@ impl State { if let Some(t) = self.free.pop() { return Some((t, true)); } + // a pool retired a moment ago (its last tile came free) is taken back before a new + // 64 MB heap is made: freeing and allocating a tile in turn made a new pool each time, + // up to 18 GB of retired heaps in 2 s (#913). Its tiles are reused as freed tiles are + // (zeroed), so this is no less safe than reusing a tile of a pool in use. + if let Some((heap, _, _)) = self.retired.pop() { + let h = self.add_pool(heap); + return Some(((h, 0), true)); + } let desc = D3D12_HEAP_DESC { SizeInBytes: POOL_TILES as u64 * TILE as u64, Properties: D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, @@ -589,6 +597,13 @@ impl State { return None; } let heap = heap?; + let h = self.add_pool(heap); + Some(((h, 0), false)) + } + + /// `heap` becomes a pool (in a given-back pool's index if there is one); its tiles but the + /// first, which the caller takes, go to the free list. + fn add_pool(&mut self, heap: ID3D12Heap) -> u32 { let h = match self.pools.iter().position(|p| p.is_none()) { Some(i) => { self.pools[i] = Some(heap); @@ -600,7 +615,7 @@ impl State { } }; self.free.extend((1..POOL_TILES).rev().map(|t| (h, t))); - Some(((h, 0), false)) + h } /// Pools whose every tile is free again go (64 MB of video memory each – and as much of the @@ -1532,6 +1547,77 @@ mod tests { } } + /// Freeing the last tile in use of a pool and taking a tile again takes the retired pool + /// back, not a new 64 MB heap each time (#913: 288 pools, 18 GB asked of a 12 GB card). + #[test] + fn a_pool_retired_a_moment_ago_is_taken_back() { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + + unsafe { + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + let factory: IDXGIFactory4 = CreateDXGIFactory1().expect("DXGI factory"); + let warp: IDXGIAdapter = factory.EnumWarpAdapter().expect("WARP"); + D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_11_0, &mut dev).expect("WARP D3D12 device"); + } + let dev = dev.expect("D3D12 device"); + let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC::default()).expect("queue"); + let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).expect("fence"); + let rb_fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).expect("rb_fence"); + let event = windows::Win32::System::Threading::CreateEventW(None, false, false, None).expect("event"); + let rb_alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).expect("rb_alloc"); + let rb_list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &rb_alloc, None).expect("rb_list"); + rb_list.Close().expect("rb_list close"); + let mut s = State { + dev, + queue, + fence, + value: 0, + event, + rb_fence, + rb_value: 0, + windows: Vec::new(), + pools: Vec::new(), + free: Vec::new(), + retired: Vec::new(), + mapped: FxHashMap::default(), + pinned: FxHashMap::default(), + buffers: FxHashMap::default(), + to_zero: Vec::new(), + staging: None, + ring_write: 0, + ring_submitted: 0, + ring_free: 0, + inflight: VecDeque::new(), + zero_buf: None, + slots: Vec::new(), + next_slot: 0, + readback: None, + rb_alloc, + rb_list, + rb_last_fence: 0, + waited: FxHashMap::default(), + runs: None, + scratch_runs: Vec::new(), + }; + + let (first, reused) = s.take_tile().expect("a tile from a new pool"); + assert!(!reused, "a new pool's tile needs no clearing"); + s.free.push(first); + s.retire_free_pools(); + assert_eq!(s.retired.len(), 1, "an all-free pool is retired"); + for round in 0..8 { + let (tile, reused) = s.take_tile().expect("a tile"); + assert!(reused, "round {round}: a tile of a retired pool is cleared before use"); + s.free.push(tile); + s.retire_free_pools(); + assert_eq!(s.retired.len(), 1, "round {round}: the retired pool was taken back, not a new one made"); + assert_eq!(s.pools.iter().filter(|p| p.is_some()).count(), 0, "round {round}"); + } + } + } + #[test] fn aligned_tile_addresses_spread_across_hash_buckets() { use std::hash::BuildHasher;