From 7f94c6f5f0c93b3b369c22e4f5d647fd434f0eb5 Mon Sep 17 00:00:00 2001 From: Vax Date: Mon, 23 Nov 2026 19:56:36 -0500 Subject: [PATCH] Memory: decommitted sparse tiles stay mapped until the next submit A sparse window tile whose pages were all decommitted was unmapped at once (UpdateTileMappings with a NULL range, then a queue signal), and a commit there again mapped a pool tile anew. Each of these calls took about 0.12-0.15 ms in the NVIDIA driver on an RTX 4070 Ti. A retail title moves physical pages one at a time while it loads, on the thread that presents: thousands of tiles a second are decommitted and committed again, and timing probes and symbolized thread dumps put that thread in the driver under map_tiles / unmap_tiles most of the time. With presents stopped for more than 30 s the title's own stall watchdog ended it. A tile that goes is now parked: it stays mapped in its window and keeps its pool tile, and nothing is sent to the GPU. The parked tiles are unmapped together (one UpdateTileMappings per window) and their pool tiles freed (flush_parked) before the next title submit (before_execute), when the free list runs out (take_tile), or when 4096 are parked (256 MB of video memory). A commit at a parked address before that takes its pool tile back with no mapping call and queues it to be cleared (to_zero), as a reused tile is. Readers of `mapped` see a parked tile as unmapped, MAPPED_GEN changes when one is taken back, and release_if_unused does not free a pool tile that is parked. committed / decommitted keep their locking; their bodies are now State methods (commit_range / decommit_range) so the test can drive a state of its own. Assisted-by: Claude Code (Claude Opus 5.5) --- crates/xcl/src/hle/d3d12x/sparse.rs | 273 +++++++++++++++++++++++----- 1 file changed, 231 insertions(+), 42 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index 22c5a22fa..411ba0a58 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -34,6 +34,9 @@ use windows_sys::Win32::System::Memory::{ const TILE: usize = 0x1_0000; /// tiles of a video-memory pool heap (64 MiB) const POOL_TILES: u32 = 1024; +/// tiles decommitted while no title work was submitted that stay mapped (`State::parked`) before +/// they are unmapped together: 256 MB of video memory +const PARKED_MAX: usize = 4096; /// bytes of the ring-buffered upload staging buffer const STAGING: usize = 64 << 20; /// alignment of sub-allocations inside the upload staging ring (write-combined cacheline) @@ -216,6 +219,13 @@ struct State { mapped: FxHashMap, /// pool tiles also used by placed buffers (not freed while they are) pinned: FxHashMap<(u32, u32), u32>, + /// tile address → pool tile of tiles decommitted since the last title submit, still mapped + /// in their window: unmapped together (`flush_parked`) before the next submit, when the free + /// list runs out, or past `PARKED_MAX`; a commit there before that takes its tile back with + /// no new mapping. (A retail title moving physical pages one at a time while it loads + /// decommits and commits again thousands of tiles a second, and each `UpdateTileMappings` + /// took ~0.15 ms.) + parked: FxHashMap, /// placed buffers → the pool tiles they use buffers: FxHashMap>, /// reused tiles / decommitted partial pages to clear: (address, bytes) @@ -524,6 +534,7 @@ pub unsafe fn init(dev: &ID3D12Device) -> Vec<(usize, usize, u64)> { retired: Vec::new(), mapped: FxHashMap::default(), pinned: FxHashMap::default(), + parked: FxHashMap::default(), buffers: FxHashMap::default(), to_zero: Vec::new(), staging: None, @@ -566,6 +577,9 @@ impl State { } unsafe fn take_tile(&mut self) -> Option<((u32, u32), bool)> { + if self.free.is_empty() { + self.flush_parked(); + } if let Some(t) = self.free.pop() { return Some((t, true)); } @@ -718,16 +732,25 @@ impl State { let first = a & !(TILE - 1); let end = a.saturating_add(n); let mut new: Vec<(usize, (u32, u32))> = Vec::new(); + let mut unparked = false; let mut t = first; while t < end { if !self.mapped.contains_key(&t) && self.window_index(t).is_some() { - let Some((tile, reused)) = self.take_tile() else { break }; - self.mapped.insert(t, TileEntry::with_holes(tile, initial_holes(t))); - self.runs = None; - if reused { + // still mapped there: no new mapping, what it held is cleared + if let Some(tile) = self.parked.remove(&t) { + self.mapped.insert(t, TileEntry::with_holes(tile, initial_holes(t))); + self.runs = None; self.to_zero.push((t, TILE)); + unparked = true; + } else { + let Some((tile, reused)) = self.take_tile() else { break }; + self.mapped.insert(t, TileEntry::with_holes(tile, initial_holes(t))); + self.runs = None; + if reused { + self.to_zero.push((t, TILE)); + } + new.push((t, tile)); } - new.push((t, tile)); } match t.checked_add(TILE) { Some(next) => t = next, @@ -735,6 +758,9 @@ impl State { } } if new.is_empty() { + if unparked { + MAPPED_GEN.fetch_add(1, Ordering::Release); + } return; } MAPPED_GEN.fetch_add(1, Ordering::Release); @@ -1249,24 +1275,7 @@ pub unsafe fn committed(a: usize, n: usize) { early_add(&mut EARLY.lock().unwrap(), start, end); return; }; - s.ensure_mapped(a, n, |_| 0xFFFF); - let mut pg = a & !(PAGE - 1); - let end = a.saturating_add(n); - while pg < end { - let t = pg & !(TILE - 1); - let tile_end = t.saturating_add(TILE).min(end); - if let Some(entry) = s.mapped.get_mut(&t) { - let mut p = pg; - while p < tile_end { - entry.holes &= !(1u16 << ((p - t) / PAGE)); - p += PAGE; - } - } - if tile_end <= pg { - break; - } - pg = tile_end; - } + s.commit_range(a, n); } /// The title decommitted `[a, a + n)`: tiles whose pages are all uncommitted (whether in one @@ -1282,33 +1291,93 @@ pub unsafe fn decommitted(a: usize, n: usize) { early_remove(&mut EARLY.lock().unwrap(), start, end); return; }; - let tiles = decommit_tiles(&mut s.mapped, &s.pinned, &mut s.free, &mut s.to_zero, a, n); - if tiles.is_empty() { - return; - } - s.runs = None; - let mut by_window: FxHashMap> = FxHashMap::default(); - for &t in &tiles { - if let Some(wi) = s.window_index(t) { - by_window.entry(wi).or_default().push(t); + s.decommit_range(a, n); +} + +impl State { + /// `committed` with the state locked + unsafe fn commit_range(&mut self, a: usize, n: usize) { + self.ensure_mapped(a, n, |_| 0xFFFF); + let mut pg = a & !(PAGE - 1); + let end = a.saturating_add(n); + while pg < end { + let t = pg & !(TILE - 1); + let tile_end = t.saturating_add(TILE).min(end); + if let Some(entry) = self.mapped.get_mut(&t) { + let mut p = pg; + while p < tile_end { + entry.holes &= !(1u16 << ((p - t) / PAGE)); + p += PAGE; + } + } + if tile_end <= pg { + break; + } + pg = tile_end; } } - for (wi, ts) in by_window { - let (b, r) = (s.windows[wi].0, s.windows[wi].1.clone()); - s.unmap_tiles(&r, b, &ts); + + /// `decommitted` with the state locked: the tiles that go are parked (`flush_parked` unmaps + /// them and frees their pool tiles) + unsafe fn decommit_range(&mut self, a: usize, n: usize) { + let mut pools: Vec<(usize, (u32, u32))> = Vec::new(); + let mut t = a & !(TILE - 1); + let end = a.saturating_add(n); + while t < end { + if let Some(e) = self.mapped.get(&t) { + pools.push((t, e.pool)); + } + match t.checked_add(TILE) { + Some(next) => t = next, + None => break, + } + } + let tiles = decommit_tiles(&mut self.mapped, &self.pinned, &mut Vec::new(), &mut self.to_zero, a, n); + if tiles.is_empty() { + return; + } + self.runs = None; + for &t in &tiles { + if let Ok(i) = pools.binary_search_by_key(&t, |p| p.0) { + self.parked.insert(t, pools[i].1); + } + } + MAPPED_GEN.fetch_add(1, Ordering::Release); + if self.parked.len() >= PARKED_MAX && self.flush_parked() { + self.retire_free_pools(); + } } - MAPPED_GEN.fetch_add(1, Ordering::Release); - s.signal(); - s.retire_free_pools(); -} -impl State { - /// a pool tile no window maps and no buffer uses goes back to the free list + /// a pool tile no window maps, none is parked on and no buffer uses goes back to the free list fn release_if_unused(&mut self, tile: (u32, u32)) { - if !self.pinned.contains_key(&tile) && !self.mapped.values().any(|e| e.pool == tile) { + if !self.pinned.contains_key(&tile) && !self.mapped.values().any(|e| e.pool == tile) && !self.parked.values().any(|&p| p == tile) { self.free.push(tile); } } + + /// The parked tiles unmapped from their windows (one call per window) and their pool tiles + /// free (but those of placed buffers). Pools left all free are not retired here: callers do + /// that (`retire_free_pools`) unless they take a tile right away. + unsafe fn flush_parked(&mut self) -> bool { + if self.parked.is_empty() { + return false; + } + let mut by_window: FxHashMap> = FxHashMap::default(); + for (t, tile) in std::mem::take(&mut self.parked) { + if let Some(wi) = self.window_index(t) { + by_window.entry(wi).or_default().push(t); + } + if !self.pinned.contains_key(&tile) { + self.free.push(tile); + } + } + for (wi, ts) in by_window { + let (b, r) = (self.windows[wi].0, self.windows[wi].1.clone()); + self.unmap_tiles(&r, b, &ts); + } + self.signal(); + true + } } /// The sparse windows' tiles with video memory as sorted (start, end) runs: where a CPU write @@ -1348,6 +1417,10 @@ pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page: let t0 = std::time::Instant::now(); let mut g = STATE.lock().unwrap(); let Some(s) = g.as_mut() else { return }; + // the title's work sees what was decommitted unmapped, as it was + if s.flush_parked() { + s.retire_free_pools(); + } let t_locked = std::time::Instant::now(); PHASES[0].fetch_add((t_locked - t0).as_micros() as u64, Ordering::Relaxed); let zero_runs = std::mem::take(&mut s.to_zero); @@ -1658,6 +1731,7 @@ mod tests { retired: Vec::new(), mapped: FxHashMap::default(), pinned: FxHashMap::default(), + parked: FxHashMap::default(), buffers: FxHashMap::default(), to_zero: Vec::new(), staging: None, @@ -1706,6 +1780,121 @@ mod tests { } } + /// A tile decommitted and committed again before the next submit gets its pool tile back + /// with no new tile mapping (cleared instead). One that is not is unmapped and freed by + /// `flush_parked` (before a submit), or when the free list runs out; a placed buffer's + /// release does not free it while it is parked. + #[test] + fn a_tile_decommitted_and_committed_again_keeps_its_video_memory() { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + use windows_sys::Win32::System::Memory::{MEM_RELEASE, MEM_RESERVE}; + + unsafe { + let factory: IDXGIFactory4 = CreateDXGIFactory1().expect("DXGI factory"); + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + 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 base = VirtualAlloc(std::ptr::null(), 2 * TILE, MEM_RESERVE, PAGE_NOACCESS) as usize; + assert!(base != 0 && base % TILE == 0); + let mut reserved: Option = None; + dev.CreateReservedResource(&buffer_desc((2 * TILE) as u64), D3D12_RESOURCE_STATE_COMMON, None, &mut reserved) + .expect("CreateReservedResource"); + let reserved = reserved.expect("reserved buffer"); + 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![(base, reserved)], + pools: Vec::new(), + free: Vec::new(), + retired: Vec::new(), + mapped: FxHashMap::default(), + pinned: FxHashMap::default(), + parked: 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(), + }; + + s.commit_range(base, TILE); + let tile = s.mapped[&base].pool; + let v = s.value; + assert!(v > 0, "a new tile is mapped"); + + // decommitted: parked, still mapped, nothing sent to the GPU + s.decommit_range(base, TILE); + assert!(!s.mapped.contains_key(&base)); + assert_eq!(s.parked.get(&base), Some(&tile)); + assert!(!s.free.contains(&tile)); + assert_eq!(s.value, v, "no tile mapping changed"); + s.release_if_unused(tile); + assert!(!s.free.contains(&tile), "a parked tile is not free"); + + // committed again: the same tile, cleared, no new mapping + s.to_zero.clear(); + let gen = mapped_gen(); + s.commit_range(base, TILE); + assert_eq!(s.mapped[&base].pool, tile); + assert_eq!(s.mapped[&base].holes, 0); + assert!(s.parked.is_empty()); + assert_eq!(s.to_zero, vec![(base, TILE)]); + assert_eq!(s.value, v, "no tile mapping changed"); + assert!(mapped_gen() > gen, "readers of `mapped` see it changed"); + + // decommitted and flushed (as before a submit): unmapped and free + s.decommit_range(base, TILE); + assert!(s.flush_parked()); + assert!(s.parked.is_empty()); + assert!(s.free.contains(&tile)); + assert!(s.value > v, "the unmapping is sent"); + assert!(!s.flush_parked(), "nothing left to flush"); + + // no free tile left: the parked one is flushed and taken for the next commit + s.commit_range(base, TILE); + let tile = s.mapped[&base].pool; + s.decommit_range(base, TILE); + s.free.clear(); + s.commit_range(base + TILE, TILE); + assert!(s.parked.is_empty()); + assert_eq!(s.mapped[&(base + TILE)].pool, tile); + assert!(!s.mapped.contains_key(&base)); + + s.wait_for(s.value); + drop(s); + assert_ne!(VirtualFree(base as *mut c_void, 0, MEM_RELEASE), 0); + } + } + /// Sub-tile decommits (two 32 KiB halves, sixteen 4 KiB pages, or decommitting the last /// remaining committed page) must unmap the tile and return unpinned pool tiles to `free` /// once `holes` reaches `0xFFFF`, pruning stale `to_zero` entries for unmapped tiles.