From 8617577b67c3b2c67542ff3d47d6e631967c16c6 Mon Sep 17 00:00:00 2001 From: Prime <215761102+Prime1997@users.noreply.github.com> Date: Thu, 26 Nov 2026 21:41:30 -0800 Subject: [PATCH 1/2] xcl run: XCL_PREPARE=0 starts the title without the shader preparation Since #2555 every start on main runs the shader preparation before the title's code, and it has no way out: on a retail title with a shader pack of 26,503 records and a cold cache that is hours on an RTX 3060 Ti, for a look at a change that needs a minute of play, and a regression run of main or of a PR's head sits through it first. nvidia-dev has the switch (present/preparation.rs, the same place): with XCL_PREPARE=0 the preparation is left out and the title builds the shaders it uses as it goes, standing still for those builds, as before #2555. Unset, or any other value, prepares as now. Test: only_xcl_prepare_0_leaves_the_preparation_out. README: the switch, next to the preparation's description. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 ++ .../xcl/src/hle/d3d12x/present/preparation.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 080f8a9b3e..0f7b14c29e 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,8 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder title are created during play. Later launches reuse matching cache entries. Closing the window cancels the launch. A compiler or driver failure stops it and reports the cause. Null-renderer runs skip shader preparation. With presentation disabled, the gate runs without a window. + `XCL_PREPARE=0` leaves the preparation out: the game starts at once and builds the shaders it + uses as it goes (it stands still while they build). - For compute shaders that XCL can split, the runtime first tries the saved driver blob for the whole shader. A usable blob lets the GPU run it in one dispatch. If the blob is missing or the driver rejects it, XCL builds the split pieces. diff --git a/crates/xcl/src/hle/d3d12x/present/preparation.rs b/crates/xcl/src/hle/d3d12x/present/preparation.rs index 1d4e614b8f..ba9f22257d 100644 --- a/crates/xcl/src/hle/d3d12x/present/preparation.rs +++ b/crates/xcl/src/hle/d3d12x/present/preparation.rs @@ -45,6 +45,10 @@ pub(crate) fn before_guest(boot: &Boot) -> Result<(), String> { (!root.join("shaders.rpf").try_exists().map_err(|e| e.to_string())? && !root.join("shaders").is_dir()) { return Ok(()); } + if left_out(std::env::var("XCL_PREPARE").ok().as_deref()) { + crate::xlog_info!("Shader preparation left out (XCL_PREPARE=0): the title builds what it uses as it goes"); + return Ok(()); + } let progress = Arc::new(LaunchPreparation { adapter: boot.config.d3d12_adapter.clone(), ..Default::default() }); let hwnd = if super::enabled() { *SCREEN.lock().unwrap() = Some(Screen { progress: progress.clone(), start: Instant::now(), clock: StallClock::default(), ready: false }); @@ -194,10 +198,23 @@ fn percent(done: usize, total: usize, ready: bool) -> usize { if ready { 100 } else { done.saturating_mul(100).checked_div(total).unwrap_or(0).min(99) } } +/// `XCL_PREPARE=0` (`value`): the title starts at once and builds what it uses as it goes, +/// standing still for those builds, as before the preparation (the switch nvidia-dev has). +fn left_out(value: Option<&str>) -> bool { + value.is_some_and(|v| v.trim() == "0") +} + #[cfg(test)] mod tests { use super::*; + /// Only `XCL_PREPARE=0` leaves the preparation out; unset or any other value prepares. + #[test] + fn only_xcl_prepare_0_leaves_the_preparation_out() { + assert!(left_out(Some("0")) && left_out(Some(" 0 "))); + assert!(!left_out(None) && !left_out(Some("1")) && !left_out(Some("")) && !left_out(Some("no"))); + } + #[test] fn clock_advances_during_a_stall_and_remembers_recovery() { let mut clock = StallClock::default(); From f78e05bcc2803becade7e5368b2f0008e5d7c74b Mon Sep 17 00:00:00 2001 From: Prime <215761102+Prime1997@users.noreply.github.com> Date: Thu, 26 Nov 2026 20:37:50 -0800 Subject: [PATCH 2/2] D3D12.X: the driver heap's grooming reserves stretches a smaller window fits in The driver-heap remake of the coherent window (#2844) reserves every free stretch of 1 GiB or more so that the driver's mapping of the new heap can only land in the window's hole. A window under 1 GiB (XCL's 512 MB before a title's size is known, and the 16-window layout's) fits in a stretch between its size and 1 GiB that stays free: in a retail title on an RTX 3060 Ti the driver mapped the heap at 0x4a720000, not the window's 0x1300000000, and the window stayed imported ("Map gave ..., not the window's ..."). Stretches the window fits in are reserved now, with a margin under half the window left free for the driver's own mappings. With the fix the window was remade at its address ("a driver CUSTOM write-back L0 heap mapped at its address"). Test: grooming_leaves_a_window_under_1_gib_only_its_hole (in a process of its own): with a 768 MB free stretch between two reservations below the hole, a 512 MB reservation after grooming lands in the hole; with the old 1 GiB rule it landed in the stretch. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/uma_driver.rs | 54 +++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/uma_driver.rs b/crates/xcl/src/hle/d3d12x/uma_driver.rs index e68ac4d994..e4c4b8b7ee 100644 --- a/crates/xcl/src/hle/d3d12x/uma_driver.rs +++ b/crates/xcl/src/hle/d3d12x/uma_driver.rs @@ -77,10 +77,13 @@ unsafe fn written_pages(base: usize, size: usize) -> Result) Ok(out) } -/// Reserves every free stretch of 1 GiB or more (but its first 256 MiB, room for the driver's own -/// small mappings) outside `[hole, hole + len)`. +/// Reserves every free stretch the window's `len` fits in (1 GiB or more for a larger window; but +/// its first 256 MiB, or less than half the window, room for the driver's own small mappings) +/// outside `[hole, hole + len)`. A window under 1 GiB (512 MB with the 16-window layout) found a +/// stretch between its size and 1 GiB unreserved, and the driver mapped its heap there. unsafe fn groom(hole: usize, len: usize) -> Vec { let (top, mut a, mut got) = (0x7FFF_FFFE_0000usize, 0x1_0000usize, Vec::new()); + let (least, margin) = (len.min(1 << 30), (256usize << 20).min(len / 2) & !0xFFFF); while a < top { let mut m: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); if VirtualQuery(a as *const c_void, &mut m, std::mem::size_of::()) == 0 { @@ -88,10 +91,10 @@ unsafe fn groom(hole: usize, len: usize) -> Vec { } let (base, end) = (m.BaseAddress as usize, (m.BaseAddress as usize + m.RegionSize).min(top)); a = end; - if m.State != MEM_FREE || m.RegionSize < 1 << 30 { + if m.State != MEM_FREE || m.RegionSize < least { continue; } - let s = ((base + 0xFFFF) & !0xFFFF) + (256 << 20); + let s = ((base + 0xFFFF) & !0xFFFF) + margin; for (s, e) in [(s, end.min(hole)), (s.max(hole + len), end)] { let mut s = s; while s < e { @@ -302,4 +305,47 @@ mod tests { assert!(transcript.contains("coherent window could not be restored"), "{transcript}"); assert!(!transcript.contains("continued after failed restore"), "{transcript}"); } + + /// Grooming for a window under 1 GiB (512 MB, as XCL makes the coherent window before a title's + /// size is known) leaves it no free stretch but its own hole: a window-sized reservation lands + /// there, not in a 768 MB stretch between two other reservations below it. Run in a process of + /// its own: while grooming, no other thread could reserve more than the margins. + #[test] + fn grooming_leaves_a_window_under_1_gib_only_its_hole() { + use super::*; + const MEM_TOP_DOWN: u32 = 0x0010_0000; + const LEN: usize = 512 << 20; + if std::env::var_os("XCL_TEST_GROOM_SMALL").is_some() { + unsafe { + let reserve = |at: usize, n: usize, flags: u32| VirtualAlloc(at as *const c_void, n, MEM_RESERVE | flags, PAGE_NOACCESS) as usize; + // the window's place, high up; held while the stretch below is made + let hole = reserve(0, LEN, MEM_TOP_DOWN); + assert_ne!(hole, 0); + // a free stretch of 768 MB between two reservations, below the hole + let probe = reserve(0, 2 << 30, 0); + assert!(probe != 0 && probe < hole); + VirtualFree(probe as *mut c_void, 0, MEM_RELEASE); + let (low, high) = (reserve(probe, 256 << 20, 0), reserve(probe + (1024 << 20), 256 << 20, 0)); + assert!(low == probe && high == probe + (1024 << 20)); + VirtualFree(hole as *mut c_void, 0, MEM_RELEASE); + let got = groom(hole, LEN); + let p = reserve(0, LEN, 0); + ungroom(got); + for a in [p, low, high] { + if a != 0 { + VirtualFree(a as *mut c_void, 0, MEM_RELEASE); + } + } + println!("window reservation at {p:#x}, hole {hole:#x}, stretch {:#x}..{:#x}", probe + (256 << 20), probe + (1024 << 20)); + std::process::exit(if p == hole { 0 } else { 1 }); + } + } + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "hle::d3d12x::uma_driver::tests::grooming_leaves_a_window_under_1_gib_only_its_hole", "--nocapture"]) + .env("XCL_TEST_GROOM_SMALL", "1") + .output() + .unwrap(); + let transcript = format!("{}{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)); + assert_eq!(output.status.code(), Some(0), "{transcript}"); + } }