From b085df77627c964e4a4f09dbb9752f978d650a8f Mon Sep 17 00:00:00 2001 From: sentaxic Date: Fri, 27 Nov 2026 13:09:58 +0530 Subject: [PATCH] feat(gpu): alias title memory outside buffers instead of copying it `VICTORY_ALIAS_LOOSE=1` (off without it): a window of title memory outside any buffer object is remapped onto its host buffer, zero copy, where every page of the window is committed. Such memory is Grand Theft Auto VI's interface vertices, from its own allocator. It is not a buffer the title asks the device for, so nothing aliases it at allocation the way `XMemVirtualAlloc` does, and each window is copied again on every submission that reaches it. The whole window must be committed because `alias_region` preserves the contents by reading every page of it first; the check already there only vouches for the address asked about. It pays only with small loose windows, which are now the default on a Mac with 32 GB or less: a 256 KiB window is committed whole far more often than a 4 MiB one. In the world with 4 MiB windows 19 regions were aliased; with 256 KiB windows, 2,793. Measured on an M5 MacBook Air (16 GB), macOS 27.2, Series build 1.0.2.1 at 1280x720, with `VICTORY_FAULT_TRACKING=1` and 256 KiB windows: aliased regions 9 -> 1,902 loose window memory 3,527 MB -> 541 MB copy time, heaviest frame 7,248 ms -> 2,033 ms copy time, 5 heaviest 4,965 ms -> 1,771 ms (mean) No `mach_vm_remap` failures, no host GPU failure and no exception across the runs. The runs were compared by elapsed time, not frame for frame, so take the ratios as approximate. Off by default. A window is not an allocation of the title's: it is cut around whatever address a draw referenced, so nothing tells us when the title frees or reuses the memory under it, as `XMemFree` does for the buffers aliased at allocation. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/shadow.rs | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crates/victory-gpu/src/shadow.rs b/crates/victory-gpu/src/shadow.rs index 33f82cbb3..7882f3535 100644 --- a/crates/victory-gpu/src/shadow.rs +++ b/crates/victory-gpu/src/shadow.rs @@ -2428,6 +2428,21 @@ fn loose_window_size(asked: Option, rosetta: bool, total_ram: Option) if small { SMALL_WINDOW } else { LARGE_WINDOW } } +/// Opt-in (`VICTORY_ALIAS_LOOSE=1`): a window of title memory outside any +/// buffer is aliased onto its host buffer (zero copy, `crate::alias`) +/// instead of being copied every submission, where every page of it is +/// committed. +/// +/// Off by default. A window is not an allocation of the title's: it is cut +/// around whatever address a draw referenced, so nothing tells us when the +/// title frees or reuses the memory under it, as `XMemFree` does for the +/// buffers aliased at allocation. The remap also takes the window whole, +/// so it claims the title's other data sharing those pages. +fn alias_loose() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| crate::switch::on("VICTORY_ALIAS_LOOSE")) +} + /// Copies of title memory outside buffers, by (start, length). static LOOSE: Mutex>> = Mutex::new(BTreeMap::new()); @@ -2473,6 +2488,31 @@ fn loose(address: u64, end: u64) -> Option> { if !crate::texture::committed(address as *const u8, 1) { return None; } + // Zero-copy instead of a copy per submission, where the whole window is + // committed: the remap preserves the window's contents by reading every + // page of it first, so one uncommitted page would fault. The check above + // only vouches for the address asked about. + if alias_loose() && crate::texture::committed(start as *const u8, len as usize) { + drop(map); + if crate::alias::alias_region(start as usize, len as usize, true) { + tracing::debug!( + start = format_args!("{start:#x}"), + len, + "loose memory aliased zero-copy" + ); + return find_aliased(address, end); + } + // Refused: another window aliased over it first, or the remap failed. + if let Some(shadow) = find_aliased(address, end) { + return Some(shadow); + } + map = LOOSE.lock().unwrap_or_else(|e| e.into_inner()); + if let Some(shadow) = + enclosing_window(&map, LOOSE_SPAN.load(Ordering::Relaxed), address, end) + { + return Some(shadow.clone()); + } + } let (buffer, data) = host()?.shared_buffer(len)?; crate::host::name(&buffer, || format!("title memory {start:#x}+{len:#x}")); // SAFETY: a live buffer.