From 40754478b11412b80a655f2db5fbd3405299917d Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Mon, 23 Nov 2026 21:40:00 +0100 Subject: [PATCH] Title memory the GPU uses in large pages goes to a sparse window; to_zero pruned once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GTA VI allocates its index and vertex data with VirtualAlloc(MEM_LARGE_PAGES | commit, protection 0x22004) – 1.2 GB, 0.6 GB, 0.6 GB – not MEM_GRAPHICS, not MEM_64K_PAGES. It got plain process memory; the GPU faulted on the index buffers there (Radeon GPU Detective: page fault in a DrawIndexed, driver timeout ~50 s in). Large-page allocations with protection bit 0x20000 now get a sparse window like MEM_64K_PAGES ones, from the highest window down so the title's physical pages (6.3 GB in one piece) keep the lower ones. (Without ivall/xcl#187's page homes out of GPU space they do not all fit.) sparse::decommit_tiles pruned `to_zero` – which grows until the next ExecuteCommandLists – at every decommit that unmapped a tile. While GTA VI maps gigabytes of physical pages during loading this walked the whole list per page and its watchdog ended the game. It is now pruned once, where the runs are taken. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/sparse.rs | 20 +++++++++++++++----- crates/xcl/src/hle/kernelx.rs | 17 ++++++++++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index ff20199be..e1e69fe85 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -181,12 +181,17 @@ fn decommit_tiles( } } } - if !unmapped.is_empty() { - to_zero.retain(|&(za, _)| mapped.contains_key(&(za & !(TILE - 1)))); - } unmapped } +/// Drop the runs of `to_zero` whose tile is no longer mapped – once, where the runs are taken. +/// (Pruning at every decommit that unmapped a tile walked the whole list each time: it grows +/// until the next ExecuteCommandLists, and a title mapping gigabytes of physical pages while it +/// loads – GTA VI – spent seconds there, its watchdog ended the game.) +fn prune_zero_runs(mapped: &FxHashMap, runs: &mut Vec<(usize, usize)>) { + runs.retain(|&(za, _)| mapped.contains_key(&(za & !(TILE - 1)))); +} + struct UploadSlot { alloc: ID3D12CommandAllocator, list: ID3D12GraphicsCommandList, @@ -1103,7 +1108,8 @@ pub unsafe fn readback_now(va: usize, len: usize) { if s.window_index(va).is_none() { return; } - let zeroed = std::mem::take(&mut s.to_zero); + let mut zeroed = std::mem::take(&mut s.to_zero); + prune_zero_runs(&s.mapped, &mut zeroed); if !zeroed.is_empty() { for &(a, n) in &zeroed { s.clear_sent_range(a, n); @@ -1246,7 +1252,8 @@ pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page: let Some(s) = g.as_mut() else { return }; 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); + let mut zero_runs = std::mem::take(&mut s.to_zero); + prune_zero_runs(&s.mapped, &mut zero_runs); for &(a, n) in &zero_runs { s.clear_sent_range(a, n); } @@ -1608,6 +1615,7 @@ mod tests { assert_eq!(unmapped, vec![t0]); assert!(!mapped.contains_key(&t0)); assert_eq!(free, vec![(0, 10)]); + prune_zero_runs(&mapped, &mut to_zero); assert!(to_zero.is_empty(), "stale to_zero entries for the unmapped tile must be pruned"); // 2. Sixteen individual 4 KiB decommits in reverse order. @@ -1623,6 +1631,7 @@ mod tests { assert_eq!(u, vec![t1]); assert!(!mapped.contains_key(&t1)); assert_eq!(free, vec![(0, 11)]); + prune_zero_runs(&mapped, &mut to_zero); assert!(to_zero.is_empty()); // 3. Pinned tile decommitted in two halves: unmapped from `mapped`, NOT returned to `free`. @@ -1635,6 +1644,7 @@ mod tests { assert_eq!(u, vec![t2]); assert!(!mapped.contains_key(&t2)); assert!(free.is_empty(), "pinned pool tile must not return to free list"); + prune_zero_runs(&mapped, &mut to_zero); assert!(to_zero.is_empty()); // 4. Single-committed-page tile decommit reclaims tile without dropping another tile's to_zero. diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 18a61ddc4..41362927e 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -158,6 +158,11 @@ const MEM_WRITE_WATCH: u32 = 0x20_0000; /// The console's MEM_64K_PAGES (MEM_LARGE_PAGES | MEM_PHYSICAL): title memory of 64 KiB /// pages – GPU-visible on the console like all title memory. const MEM_64K_PAGES: u32 = 0x2040_0000; +/// MEM_LARGE_PAGES: the console's 2 MiB pages of title memory (alone, or in MEM_64K_PAGES) +const MEM_LARGE_PAGES_X: u32 = 0x2000_0000; +/// a protection bit of the console's title memory the GPU uses (GTA VI's index and vertex data: +/// 0x22004; its 2.2 GB block of other data: 0x2404) +const PAGE_GPU_X: u32 = 0x2_0000; /// Commit `[a, a + n)` of a sparse window (the title's reservations there) and give it video /// memory (`d3d12x::sparse`). @@ -181,9 +186,15 @@ pub unsafe extern "system" fn VirtualAlloc(addr: *const c_void, size: usize, t: trace_mem("VirtualAlloc (sparse commit)", addr as usize, size, t as u64, p, q as usize); return q; } - // the title's own reservations of 64 KiB pages: in a sparse window, so the GPU sees them - if addr.is_null() && t & 0x2000 != 0 && t & MEM_64K_PAGES == MEM_64K_PAGES { - if let Some(q) = uma.and_then(|u| u.sparse_reserve(size)) { + // the title's own reservations of 64 KiB or large pages: in a sparse window, so the GPU sees + // them (GTA VI keeps index and vertex data in a 1.2 GB VirtualAlloc of large pages; outside + // the windows the GPU faulted on it – a driver timeout) + let large = t & MEM_64K_PAGES != MEM_64K_PAGES && t & MEM_LARGE_PAGES_X != 0 && p & PAGE_GPU_X != 0; + if addr.is_null() && t & 0x2000 != 0 && (t & MEM_64K_PAGES == MEM_64K_PAGES || large) { + // (large pages: from the highest window down – the title's physical pages, gigabytes + // in one piece, keep the lower ones) + let top = |u: &crate::gpu::uma::Uma| (1..crate::gpu::uma::MAX_WINDOWS).rev().find_map(|k| u.sparse_alloc_in(k, size, crate::gpu::uma::GRAIN).map(|(q, _)| q)); + if let Some(q) = uma.and_then(|u| if large { top(u).or_else(|| u.sparse_reserve(size)) } else { u.sparse_reserve(size) }) { if t & 0x1000 != 0 && commit_sparse(q, size, p).is_null() { uma.map(|u| u.sparse_release(q)); return std::ptr::null_mut();