From 6c8f9999aa8ba934d369c76373dea7a9c262690f Mon Sep 17 00:00:00 2001 From: bmjask <11672696+bmjask@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:17:27 -0500 Subject: [PATCH] PSO: xcl pso-precompile warms the driver-blob cache before a run Translates a title's compute programs from its shader package and has the driver compile their pipelines ahead of the run, writing the same driver blobs (same DXIL, root signature, flags) the runtime looks up. Pipelines an earlier run used go first, each group biggest first, on one driver compile a core (bounded by memory, --threads N). --records FILE / --used restrict it to given record hashes / pipeline keys; cached pipelines only load. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/real.rs | 2 +- crates/xcl/src/main.rs | 7 + crates/xcl/src/shaders.rs | 260 ++++++++++++++++++++++++++++-- 3 files changed, 251 insertions(+), 18 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index fed31e6224..5ef8fc4f17 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4324,7 +4324,7 @@ unsafe impl Send for OwnedDesc {} /// A hash of a pipeline's shaders in their order (never 0): what `pso_async` knows a used /// pipeline by in the next run. -fn shaders_key(blobs: &[Vec]) -> u64 { +pub(crate) fn shaders_key(blobs: &[Vec]) -> u64 { let mut h = 0xcbf2_9ce4_8422_2325u64; for b in blobs { h = (h ^ crate::gpu::shader::fnv64(b)).wrapping_mul(0x0000_0100_0000_01b3); diff --git a/crates/xcl/src/main.rs b/crates/xcl/src/main.rs index 6291b5dea0..6445612be0 100644 --- a/crates/xcl/src/main.rs +++ b/crates/xcl/src/main.rs @@ -41,6 +41,12 @@ Usage: --direct [--sample N] [--seed S] [--validate] [--compare]: direct DXIL translator coverage over a seeded sample (default 200 programs, 0 = all); also --timeout, --max-bytes, --threads, --json, --hash H, --dump DIR. + xcl pso-precompile [--threads N] [--records FILE] [--used] + Before a run: translates the game's compute programs (its shaders.rpf) and has the + driver compile their pipelines, the ones an earlier run used first, biggest first. + Fills the caches the run reads (DXIL, driver blobs); what is cached is skipped. + --records FILE / --used: only the programs of these record hashes / pipeline keys. + Run it with the run's XCL_DIRECT_DXIL / XCL_HEAP_WINDOWS: they change the keys. xcl rpf [--top N] [--find NAME] [--extract NAME --out DIR] Lists or extracts readable inline records. Archives larger than 1 GB use a bounded-memory header probe; --probe requests it explicitly and --full overrides it. @@ -72,6 +78,7 @@ fn main() { } Some("disasm") if args.len() >= 2 => xcl::disasm::run(&PathBuf::from(&args[1]), &args[2..]), Some("shaders") if args.len() >= 2 => xcl::shaders::run(&PathBuf::from(&args[1]), &args[2..]), + Some("pso-precompile") if args.len() >= 2 => xcl::shaders::pso_precompile(&PathBuf::from(&args[1]), &args[2..]), Some("rpf") if args.len() >= 2 => xcl::rpf::run(&PathBuf::from(&args[1]), &args[2..]), Some("--version") | Some("-V") => { println!("xcl {}", env!("CARGO_PKG_VERSION")); diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 591ca94cbb..9d5ea4cbac 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -678,6 +678,27 @@ fn coverage(agg: &Agg, order: &[String], ks: &[usize]) -> Vec<(usize, usize)> { ks.iter().map(|&k| (k, max_rank.iter().filter(|&&r| r <= k).count())).collect() } +/// `xcl pso-precompile [--threads N]`: `--precompile --pipelines` of the game's shader +/// package (`shaders.rpf` in its folder), so its run does not wait for the driver's compiles. +pub fn pso_precompile(game: &Path, args: &[String]) -> i32 { + let Some(pack) = shader_package(game) else { + eprintln!("xcl pso-precompile: no shaders.rpf in {}", game.display()); + return 2; + }; + let mut all = vec!["--precompile".to_string(), "--pipelines".to_string()]; + all.extend(args.iter().cloned()); + run(&pack, &all) +} + +/// A game's shader package: the path itself when it is a file, else `shaders.rpf` in the folder. +fn shader_package(game: &Path) -> Option { + if game.is_file() { + return Some(game.to_path_buf()); + } + let p = game.join("shaders.rpf"); + p.is_file().then_some(p) +} + pub fn run(path: &Path, args: &[String]) -> i32 { let opt = |name: &str| args.iter().position(|a| a == name).and_then(|i| args.get(i + 1)).and_then(|v| v.parse::().ok()); // a flag that needs a value: missing, or another flag in its place, is an error @@ -777,9 +798,27 @@ pub fn run(path: &Path, args: &[String]) -> i32 { if args.iter().any(|a| a == "--precompile") { let only = args.iter().position(|a| a == "--only").and_then(|i| args.get(i + 1)).map(|s| s.to_ascii_lowercase()); let pipelines = args.iter().any(|a| a == "--pipelines"); + if let Some(n) = opt("--threads") { + PIPELINE_THREADS.store(n.max(1), Ordering::Relaxed); + } // the runtime's key: heap windows with `XCL_HEAP_WINDOWS=1` (`pso::mem_windows`) let windows = runtime_windows(); - return precompile(&input, opt("--limit").unwrap_or(usize::MAX), windows, opt("--split").map(|v| v != 0), only.as_deref(), pipelines, t0); + // `--records FILE` (record hashes or pipeline keys, a hexadecimal one a line) and / or + // `--used` (the pipelines an earlier run used): only those programs + let mut select: Option> = None; + if let Some(f) = args.iter().position(|a| a == "--records").and_then(|i| args.get(i + 1)) { + match std::fs::read_to_string(f) { + Ok(t) => select.get_or_insert_with(Default::default).extend(parse_used(&t)), + Err(e) => { + eprintln!("xcl shaders --records {f}: {e}"); + return 2; + } + } + } + if args.iter().any(|a| a == "--used") { + select.get_or_insert_with(Default::default).extend(used_earlier()); + } + return precompile(&input, opt("--limit").unwrap_or(usize::MAX), windows, opt("--split").map(|v| v != 0), only.as_deref(), select.as_ref(), pipelines, t0); } if args.iter().any(|a| a == "--direct") { return direct_sweep(&input, args, top, json, t0); @@ -998,14 +1037,16 @@ fn measure_pso_result(input: &Input, hash: u64, repeat: usize, windows: u32, spl /// mesh pass-through, which the runtime builds for its pixel shader. /// `--pipelines`: each compute program's pipeline made on the GPU as a title's would be (XCL's /// ABI root signature with its static samplers). Save the driver's compiled blob in the same -/// cache the runtime reads at startup. Nothing runs on the GPU. +/// cache the runtime reads at startup. Nothing runs on the GPU. Made once all are translated: +/// the ones an earlier run used first, biggest first, on `pipeline_threads` (`--threads N`); +/// a pipeline whose blob is kept already is only loaded from it (resumable). struct Pipelines { dev: windows::Win32::Graphics::Direct3D12::ID3D12Device, signatures: Mutex, windows::Win32::Graphics::Direct3D12::ID3D12RootSignature>>, made: AtomicUsize, refused: AtomicUsize, reused: AtomicUsize, - /// driver compiles running and the most at once (`XCL_PIPELINE_THREADS`, 4): each of the + /// driver compiles running and the most at once (`pipeline_threads`): each of the /// biggest takes ~1.5 GB in the driver – fifteen at once took 24 GB running: (Mutex, std::sync::Condvar), most: usize, @@ -1023,13 +1064,14 @@ impl Pipelines { let sel = crate::gpu::adapter::Selector::from_env_or("auto"); let (adapter, _) = crate::gpu::adapter::candidates(&factory, &sel).into_iter().next().ok_or_else(|| crate::gpu::adapter::no_match(&factory, &sel))?; let dev = crate::gpu::agility::create_device(&adapter, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())?; - let most = std::env::var("XCL_PIPELINE_THREADS").ok().and_then(|v| v.parse().ok()).unwrap_or(4usize).max(1); + let most = pipeline_threads(); Ok(Pipelines { dev, signatures: Mutex::new(Default::default()), made: AtomicUsize::new(0), refused: AtomicUsize::new(0), reused: AtomicUsize::new(0), running: (Mutex::new(0), std::sync::Condvar::new()), most }) } } - /// The compute pipeline of `c` (made and released). - fn make(&self, c: &recomp::pso::Compiled) { + /// The compute pipeline of `c` (made and released): the driver's time and whether it came + /// from a kept blob, None when refused. + fn make(&self, c: &recomp::pso::Compiled) -> Option<(std::time::Duration, bool)> { use windows::Win32::Graphics::Direct3D12::*; let key: Vec = c.samplers.iter().flat_map(|(w, cmp)| w.iter().copied().chain([*cmp as u32])).collect(); let rs = { @@ -1043,7 +1085,7 @@ impl Pipelines { } Err(_) => { self.refused.fetch_add(1, Ordering::Relaxed); - return; + return None; } }, } @@ -1061,12 +1103,14 @@ impl Pipelines { *n += 1; } use crate::hle::d3d12x::real::driver_cache; + let started = std::time::Instant::now(); let path = driver_cache::path(&c.dxil, desc.Flags.0); let cached = path.as_deref().and_then(driver_cache::load); let from_cache = cached.as_ref().and_then(|bytes| { desc.CachedPSO = D3D12_CACHED_PIPELINE_STATE { pCachedBlob: bytes.as_ptr().cast(), CachedBlobSizeInBytes: bytes.len() }; unsafe { self.dev.CreateComputePipelineState::(&desc) }.ok() }); + let reused = from_cache.is_some(); let r = if let Some(pso) = from_cache { self.reused.fetch_add(1, Ordering::Relaxed); Ok(pso) @@ -1084,20 +1128,197 @@ impl Pipelines { } pso }; + let took = started.elapsed(); *self.running.0.lock().unwrap() -= 1; self.running.1.notify_one(); drop(std::mem::ManuallyDrop::into_inner(desc.pRootSignature)); - let done = match r { - Ok(_) => self.made.fetch_add(1, Ordering::Relaxed) + 1, - Err(_) => self.refused.fetch_add(1, Ordering::Relaxed) + 1, - }; - if done % 25 == 0 { - eprintln!(" pipelines: {} made, {} refused", self.made.load(Ordering::Relaxed), self.refused.load(Ordering::Relaxed)); + match r { + Ok(_) => { + self.made.fetch_add(1, Ordering::Relaxed); + Some((took, reused)) + } + Err(_) => { + self.refused.fetch_add(1, Ordering::Relaxed); + None + } + } + } +} + +/// `--threads N`: driver compiles at once for `--pipelines` (0: `pipeline_threads`' default). +static PIPELINE_THREADS: AtomicUsize = AtomicUsize::new(0); + +/// Driver compiles at once for `--pipelines`: `--threads`, `XCL_PIPELINE_THREADS`, or else one +/// a core but no more than one per 2 GB of physical memory. The biggest programs take the +/// driver ~1.5 GB each; a retail game's hundreds of 0.1 - 1 MB DXIL programs take it 1 - 120 s +/// each, so the warm-up is bound by how many run at once (4 before). +fn pipeline_threads() -> usize { + let set = PIPELINE_THREADS.load(Ordering::Relaxed); + if set != 0 { + return set; + } + if let Some(n) = std::env::var("XCL_PIPELINE_THREADS").ok().and_then(|v| v.trim().parse::().ok()) { + return n.max(1); + } + let cores = std::thread::available_parallelism().map_or(4, |n| n.get()); + default_pipeline_threads(cores, physical_memory_gb()) +} + +fn default_pipeline_threads(cores: usize, memory_gb: Option) -> usize { + let by_memory = memory_gb.map_or(4, |gb| (gb / 2) as usize); + cores.min(by_memory).clamp(1, 32) +} + +fn physical_memory_gb() -> Option { + use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX}; + let mut m: MEMORYSTATUSEX = unsafe { std::mem::zeroed() }; + m.dwLength = std::mem::size_of::() as u32; + (unsafe { GlobalMemoryStatusEx(&mut m) } != 0).then_some(m.ullTotalPhys >> 30) +} + +/// The keys of the pipelines an earlier run used (`used-pipelines.txt` beside the DXIL cache, +/// written by the runtime's `pso_async::used`); a compute pipeline's key is +/// `real::shaders_key` of its console program. +fn used_earlier() -> std::collections::HashSet { + parse_used(&std::fs::read_to_string(recomp::dxc::cache_dir().join("used-pipelines.txt")).unwrap_or_default()) +} + +fn parse_used(text: &str) -> std::collections::HashSet { + text.lines().filter_map(|l| u64::from_str_radix(l.trim(), 16).ok()).filter(|&k| k != 0).collect() +} + +/// A compute pipeline waiting for the driver. +struct PipelineJob { + /// an earlier run of the title used it (`used_earlier`) + used: bool, + compiled: recomp::pso::Compiled, +} + +/// The ones an earlier run used first (what the title's loading asks for), each group biggest +/// first: the longest compiles do not start last and leave the other threads idle at the end. +fn order_pipeline_jobs(jobs: &mut [PipelineJob]) { + jobs.sort_by(|a, b| b.used.cmp(&a.used).then(b.compiled.dxil.len().cmp(&a.compiled.dxil.len()))); +} + +/// Build `jobs` on `p`'s threads in `order_pipeline_jobs`' order: progress, the driver's time by +/// DXIL size and the slowest at the end. +fn make_pipelines(p: &Pipelines, mut jobs: Vec, used_keys: usize, t0: std::time::Instant) { + order_pipeline_jobs(&mut jobs); + let used = jobs.iter().filter(|j| j.used).count(); + println!("pipelines: {} compute programs ({used} of the {used_keys} pipelines an earlier run used: first), {} driver compiles at once", jobs.len(), p.most); + let next = AtomicUsize::new(0); + let done = AtomicUsize::new(0); + // (driver ms, DXIL bytes, cache file name, from the cache) + let times: Mutex> = Mutex::new(Vec::new()); + let started = std::time::Instant::now(); + std::thread::scope(|s| { + for _ in 0..p.most { + s.spawn(|| loop { + let i = next.fetch_add(1, Ordering::Relaxed); + let Some(j) = jobs.get(i) else { break }; + let r = p.make(&j.compiled); + if let Some((took, reused)) = r { + let name = crate::hle::d3d12x::real::driver_cache::name(&j.compiled.dxil, 0); + times.lock().unwrap().push((took.as_millis(), j.compiled.dxil.len(), name, reused)); + } + let d = done.fetch_add(1, Ordering::Relaxed) + 1; + let slow = r.filter(|(t, reused)| !reused && t.as_secs() >= 10); + if d % 50 == 0 || d == jobs.len() || slow.is_some() { + eprintln!( + " pipelines {d}/{} ({} from the driver cache, {} refused; {:.0} s){}", + jobs.len(), + p.reused.load(Ordering::Relaxed), + p.refused.load(Ordering::Relaxed), + t0.elapsed().as_secs_f64(), + slow.map_or(String::new(), |(t, _)| format!(" – {} B DXIL took the driver {:.1} s", j.compiled.dxil.len(), t.as_secs_f64())) + ); + } + }); } + }); + let mut times = times.into_inner().unwrap(); + times.retain(|t| !t.3); + let total: u128 = times.iter().map(|t| t.0).sum(); + println!("pipelines: {} compiled by the driver ({:.0} s of driver time) in {:.1} s", times.len(), total as f64 / 1000.0, started.elapsed().as_secs_f64()); + for (lo, hi) in [(0usize, 64usize << 10), (64 << 10, 256 << 10), (256 << 10, 512 << 10), (512 << 10, usize::MAX)] { + let b: Vec<_> = times.iter().filter(|t| t.1 >= lo && t.1 < hi).collect(); + if !b.is_empty() { + let ms: u128 = b.iter().map(|t| t.0).sum(); + let longest = b.iter().map(|t| t.0).max().unwrap_or(0); + println!(" DXIL from {:>4} KB: {:>5} pipelines, driver {:>7.1} s, longest {:.1} s", lo >> 10, b.len(), ms as f64 / 1000.0, longest as f64 / 1000.0); + } + } + times.sort_by(|a, b| b.0.cmp(&a.0)); + for (ms, len, name, _) in times.iter().take(10) { + println!(" slowest: {name} ({len} B DXIL) {:.1} s", *ms as f64 / 1000.0); + } +} + +#[cfg(test)] +mod pipeline_tests { + use super::*; + + fn job(used: bool, len: usize) -> PipelineJob { + PipelineJob { used, compiled: recomp::pso::Compiled { dxil: vec![0; len], samplers: Vec::new() } } } + + #[test] + fn pipelines_an_earlier_run_used_go_first_and_each_group_biggest_first() { + let mut jobs = vec![job(false, 10), job(true, 5), job(false, 900), job(true, 50)]; + order_pipeline_jobs(&mut jobs); + let order: Vec<_> = jobs.iter().map(|j| (j.used, j.compiled.dxil.len())).collect(); + assert_eq!(order, [(true, 50), (true, 5), (false, 900), (false, 10)]); + } + + #[test] + fn the_shader_package_is_found_in_the_game_folder() { + let dir = std::env::temp_dir().join(format!("xcl-pso-pre-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + assert_eq!(shader_package(&dir), None); + let pack = dir.join("shaders.rpf"); + std::fs::write(&pack, b"RPF9").unwrap(); + assert_eq!(shader_package(&dir).as_deref(), Some(pack.as_path())); + assert_eq!(shader_package(&pack).as_deref(), Some(pack.as_path())); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn records_select_programs_by_record_hash_or_pipeline_key() { + let (a, b, c) = (vec![1u8, 2, 3], vec![4u8, 5], vec![6u8]); + let set: std::collections::HashSet = [crate::gpu::shader::fnv64(&a), crate::hle::d3d12x::real::shaders_key(std::slice::from_ref(&b))].into(); + assert!(selected(Some(&set), &a)); + assert!(selected(Some(&set), &b)); + assert!(!selected(Some(&set), &c)); + assert!(selected(None, &c)); + } + + #[test] + fn driver_compiles_at_once_follow_the_cores_and_the_memory() { + assert_eq!(default_pipeline_threads(24, Some(128)), 24); + assert_eq!(default_pipeline_threads(24, Some(16)), 8); + assert_eq!(default_pipeline_threads(8, None), 4); + assert_eq!(default_pipeline_threads(64, Some(512)), 32); + assert_eq!(default_pipeline_threads(4, Some(1)), 1); + } + + #[test] + fn used_pipeline_keys_are_read_as_the_runtime_writes_them() { + let keys = parse_used("00000000000000ab\nnot a key\n\n0000000000000000\n ff00000000000001 \n"); + assert_eq!(keys.len(), 2); + assert!(keys.contains(&0xab) && keys.contains(&0xff00_0000_0000_0001)); + // a compute pipeline's key is that of its one console program + let blob = vec![1u8, 2, 3]; + assert_ne!(crate::hle::d3d12x::real::shaders_key(std::slice::from_ref(&blob)), 0); + } +} + +/// Whether `--records` / `--used` (`select`) takes the console program `blob`: by its record +/// hash (`fnv64`, as the runtime's compile log names it) or its pipeline key (`used-pipelines.txt`). +fn selected(select: Option<&std::collections::HashSet>, blob: &[u8]) -> bool { + select.is_none_or(|s| s.contains(&crate::gpu::shader::fnv64(blob)) || s.contains(&crate::hle::d3d12x::real::shaders_key(&[blob.to_vec()]))) } -fn precompile(input: &Input, limit: usize, windows: u32, split: Option, only: Option<&str>, pipelines: bool, t0: std::time::Instant) -> i32 { +fn precompile(input: &Input, limit: usize, windows: u32, split: Option, only: Option<&str>, select: Option<&std::collections::HashSet>, pipelines: bool, t0: std::time::Instant) -> i32 { let pipelines = if pipelines { match Pipelines::new() { Ok(p) => Some(p), @@ -1114,6 +1335,8 @@ 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()); + let jobs: Mutex> = Mutex::new(Vec::new()); + let used = if pipelines.is_some() { used_earlier() } else { Default::default() }; // 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 @@ -1140,7 +1363,7 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on let (name, blob) = input.get(i); let Some(blob) = blob else { continue }; let Some(sh) = xbox::parse(&blob) else { continue }; - if only.is_some_and(|o| !sh.stage.eq_ignore_ascii_case(o)) { + if only.is_some_and(|o| !sh.stage.eq_ignore_ascii_case(o)) || !selected(select, &blob) { done.fetch_add(1, Ordering::Relaxed); continue; } @@ -1149,8 +1372,10 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on if sh.fssm.iter().any(|f| matches!(recomp::interface(&blob, &sh, f), Some(recomp::Mode::Compute(_)))) { match recomp::pso::console_to_dxil_in(&blob, recomp::pso::Stage::Compute, 0, split, windows) { Ok(c) => { - if let Some(p) = pipelines { - p.make(&c); + // made on the GPU once all are translated, in their order + if pipelines.is_some() { + let used = used.contains(&crate::hle::d3d12x::real::shaders_key(std::slice::from_ref(&blob))); + jobs.lock().unwrap().push(PipelineJob { used, compiled: c }); } compiled.fetch_add(1, Ordering::Relaxed) } @@ -1221,6 +1446,7 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on t0.elapsed().as_secs_f64() ); if let Some(p) = pipelines { + make_pipelines(p, jobs.into_inner().unwrap(), used.len(), t0); println!("pipelines made on the GPU {}, refused {}", p.made.load(Ordering::Relaxed), p.refused.load(Ordering::Relaxed)); println!("pipelines loaded from the driver cache {}", p.reused.load(Ordering::Relaxed)); }