Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions crates/xcl/src/hle/d3d12x/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize, TileEntry>, runs: &mut Vec<(usize, usize)>) {
runs.retain(|&(za, _)| mapped.contains_key(&(za & !(TILE - 1))));
}

struct UploadSlot {
alloc: ID3D12CommandAllocator,
list: ID3D12GraphicsCommandList,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
Expand All @@ -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`.
Expand All @@ -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.
Expand Down
17 changes: 14 additions & 3 deletions crates/xcl/src/hle/kernelx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand All @@ -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();
Expand Down
Loading