From 14d638a3ab3cf8d52f8eb6e6a88f0b559a530d5e Mon Sep 17 00:00:00 2001 From: WebIdentity1 <212448219+WebIdentity1@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:56:15 -0500 Subject: [PATCH] Shaders: bound default compile workers by available memory Budget one GiB per worker after the existing title or offline reserve, using the smaller of free physical memory and available commit. Preserve explicit XCL_DXC_THREADS overrides and set the offline reserve before choosing the worker count. Part of #542. Native Windows tests: policy 3 passed; old-decision control 1 passed and 2 failed. Workspace 682 passed, 1 pre-existing integer CLAMP failure, 41 ignored, four explicit skips. Retail regression not run; draft only. --- README.md | 4 +++ crates/xcl/src/gpu/recomp/dxc.rs | 60 ++++++++++++++++++++++++++++---- crates/xcl/src/shaders.rs | 4 +-- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f55cad72ec..921fb41d97 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,10 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder - XCL works on **unpacked** game files you own; it contains nothing for decrypting or circumventing DRM. - **The game folder is never modified** – writes go to the overlay. +- Shader compilation leaves one CPU thread free and caps the default worker count by available + RAM and commit (1 GiB per worker after a 6 GiB reserve, or 2 GiB for offline precompilation). + The count is chosen on first use; `XCL_DXC_THREADS=N` overrides it (1–64). This limits the + default concurrency, not the title's total memory use or an individual shader's allocation. - Every run writes `report.json` (called stubs, unknown D3D12.X slots and interfaces, XGameRuntime classes asked for, shader statistics, audio formats, crash). `d3d12.presents` counts presentations, not visible content. With `XCL_FRAME_DUMP=N` (`N > 1`), diff --git a/crates/xcl/src/gpu/recomp/dxc.rs b/crates/xcl/src/gpu/recomp/dxc.rs index b769d80087..cbb45c7b87 100644 --- a/crates/xcl/src/gpu/recomp/dxc.rs +++ b/crates/xcl/src/gpu/recomp/dxc.rs @@ -372,20 +372,66 @@ struct Job { reply: std::sync::mpsc::Sender, String>>, } -/// Compile threads (DXC workers, and the PSO workers of `pso_async` that feed them): every core -/// but one, `XCL_DXC_THREADS` to choose. Memory is what `wait_for_memory` watches: a retail -/// pack's biggest translations take ~0.7 GB in DXC (a typical one 0.1–0.3 GB). The cap used -/// to be 6, which left most of a 16-thread CPU idle while a title with ~24 000 shaders -/// compiled for half an hour. (Normal priority: a title thread can wait for a pipeline – -/// `resolve_wait`, a compute pipeline's first bind – and must not wait behind its own threads.) +/// DXC workers and the PSO workers that feed them: leave a CPU thread free and budget +/// 1 GiB per worker from available physical memory / commit after the title's reserve. +/// This startup cap complements the live commit gate; `XCL_DXC_THREADS` overrides it. pub fn compile_threads() -> usize { static N: OnceLock = OnceLock::new(); *N.get_or_init(|| { + use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX}; let cores = std::thread::available_parallelism().map_or(4, |n| n.get()); - std::env::var("XCL_DXC_THREADS").ok().and_then(|v| v.parse().ok()).unwrap_or(cores.saturating_sub(1)).clamp(1, 64) + let requested = std::env::var("XCL_DXC_THREADS").ok().and_then(|v| v.parse().ok()); + let mut m: MEMORYSTATUSEX = unsafe { std::mem::zeroed() }; + m.dwLength = std::mem::size_of::() as u32; + let available = (unsafe { GlobalMemoryStatusEx(&mut m) } != 0).then_some((m.ullAvailPhys, m.ullAvailPageFile)); + compile_thread_limit(cores, requested, available, RESERVE_GB.load(std::sync::atomic::Ordering::Relaxed)) }) } +fn compile_thread_limit(cores: usize, requested: Option, available: Option<(u64, u64)>, reserve_gb: u64) -> usize { + if let Some(n) = requested { + return n.clamp(1, 64); + } + // A big translation can grow to ~0.7 GiB in DXC. Do not treat pagefile-backed commit + // as free RAM; without a memory reading, start one worker instead of guessing. + let memory_limit = available.map_or(1, |(physical, commit)| { + (physical.min(commit).saturating_sub(reserve_gb.saturating_mul(1 << 30)) >> 30).clamp(1, 64) as usize + }); + cores.saturating_sub(1).clamp(1, 64).min(memory_limit) +} + +#[cfg(test)] +mod worker_tests { + use super::compile_thread_limit; + + #[test] + fn default_workers_fit_physical_memory_and_commit() { + for (physical, commit, want) in [(20, 40, 14), (12, 40, 6), (8, 40, 2), (40, 8, 2), (6, 40, 1), (0, 0, 1)] { + assert_eq!(compile_thread_limit(32, None, Some((physical << 30, commit << 30)), 6), want); + } + // Less than a full GiB of headroom must not admit another worker. + assert_eq!(compile_thread_limit(32, None, Some(((8 << 30) - 1, 40 << 30)), 6), 1); + assert_eq!(compile_thread_limit(32, None, Some((20 << 30, 40 << 30)), 2), 18); + } + + #[test] + fn default_workers_leave_a_cpu_free_and_handle_missing_memory() { + let plenty = Some((128 << 30, 128 << 30)); + assert_eq!(compile_thread_limit(16, None, plenty, 6), 15); + assert_eq!(compile_thread_limit(1, None, plenty, 6), 1); + assert_eq!(compile_thread_limit(128, None, plenty, 6), 64); + assert_eq!(compile_thread_limit(32, None, None, 6), 1); + } + + #[test] + fn explicit_workers_keep_the_existing_override_and_bounds() { + for (requested, want) in [(0, 1), (4, 4), (64, 64), (128, 64)] { + assert_eq!(compile_thread_limit(1, Some(requested), Some((0, 0)), 6), want); + assert_eq!(compile_thread_limit(1, Some(requested), None, 6), want); + } + } +} + /// GB of commit `wait_for_memory` keeps free besides the compiles: 6 for a running title (its /// own allocations), less without one (`set_memory_reserve`: `xcl shaders --precompile`). static RESERVE_GB: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(6); diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 5c3027d7b0..19fda556d4 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -828,10 +828,10 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on let next = AtomicUsize::new(0); let (done, compiled, cached, failed, skipped) = (AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0)); let errors: Mutex> = Mutex::new(Vec::new()); + // Set the offline reserve before the worker count takes its memory snapshot. + recomp::dxc::set_memory_reserve(2); // the compiles run on the DXC pool; these threads translate and keep it fed let threads = recomp::dxc::compile_threads() + 2; - // no title runs: only a little commit is kept free besides the compiles - recomp::dxc::set_memory_reserve(2); // VS / PS: the 32-lane form wherever the GPU can run 32-lane waves (`pso::graphics_wave32`); // `--split` sets both let graphics_split = split.unwrap_or_else(recomp::runner::graphics_wave32);