From 354ce0c7189d0820ddf7d05bc5c662013ff7a5f4 Mon Sep 17 00:00:00 2001 From: Eterst Date: Tue, 24 Nov 2026 17:58:03 -0600 Subject: [PATCH] Shaders: --precompile --used-only builds only the pipelines a title used A retail pack's ~2 700 compute programs take a 32-lane NVIDIA card's driver hours to build, most of them programs the title never binds in the part it reached. The runtime already notes every pipeline a title uses by a hash of its shaders (`used-pipelines.txt` beside the DXIL cache, read by `pso_async` to build them ahead next run). `--used-only` reads that file and passes over every compute program whose pipeline key (`real::shaders_key` of the one shader, as the runtime makes it) is not in it. Measured on a retail pack with the list of a few runs (1 221 keys): 824 compute programs built, 1 865 passed over. Test: used_only_keeps_the_compute_programs_an_earlier_run_used. Co-Authored-By: Claude Opus 5.5 --- README.md | 1 + crates/xcl/src/hle/d3d12x/pso_async.rs | 6 ++++ crates/xcl/src/hle/d3d12x/real.rs | 2 +- crates/xcl/src/shaders.rs | 39 ++++++++++++++++++++++++-- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e35903a1c..2a24f92d6 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,7 @@ xcl run D:\Game [-- game arguments] # run xcl disasm [--hex] # RDNA2 disassembler (console containers too) xcl shaders D:\data\shaders.rpf --top 50 # pack statistics; --recomp / --compile N xcl shaders D:\data\shaders.rpf --precompile --only cs --pipelines # build compute pipelines and save driver blobs for the next run +xcl shaders D:\data\shaders.rpf --precompile --only cs --pipelines --used-only # only the compute pipelines an earlier run of the title used xcl shaders D:\data\shaders.rpf --export-hlsl out --record-hash HEX # exact compute HLSL for investigation xcl shaders D:\data\shaders.rpf --measure-pso HEX --repeat 3 # cold driver PSO timing (cache disabled) xcl rpf D:\data\shaders.rpf --extract NAME --out folder diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index 3bc4ad2d6..39f338143 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -249,6 +249,12 @@ fn submit_as(standin: usize, compute: bool, job: Job, send: bool, key: u64) { } } +/// The keys of the pipelines earlier runs used (`used-pipelines.txt` beside the DXIL cache): +/// what `xcl shaders --precompile --used-only` builds. +pub fn earlier_used_keys() -> std::collections::HashSet { + used::parse(&std::fs::read_to_string(crate::gpu::recomp::dxc::cache_dir().join("used-pipelines.txt")).unwrap_or_default()) +} + /// The pipelines a title used, by a hash of their shaders, kept between runs /// (`used-pipelines.txt` beside the DXIL cache, a key a line). /// diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index fed31e622..5ef8fc4f1 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/shaders.rs b/crates/xcl/src/shaders.rs index 92c04341d..e087c1a7e 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -894,9 +894,11 @@ 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"); + // `--used-only`: only the compute programs whose pipelines an earlier run of a title used + let used = args.iter().any(|a| a == "--used-only").then(crate::hle::d3d12x::pso_async::earlier_used_keys); // 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); + return precompile(&input, opt("--limit").unwrap_or(usize::MAX), windows, opt("--split").map(|v| v != 0), only.as_deref(), pipelines, used.as_ref(), t0); } if args.iter().any(|a| a == "--direct") { return direct_sweep(&input, args, top, json, t0); @@ -1214,7 +1216,13 @@ impl Pipelines { } } -fn precompile(input: &Input, limit: usize, windows: u32, split: Option, only: Option<&str>, pipelines: bool, t0: std::time::Instant) -> i32 { +/// A compute program a title used in an earlier run (`--used-only`): its pipeline's key, as the +/// runtime notes it (`real::shaders_key` of the one shader), is among `used`. +fn used_compute(blob: &[u8], used: &std::collections::HashSet) -> bool { + used.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, used: Option<&std::collections::HashSet>, t0: std::time::Instant) -> i32 { let pipelines = if pipelines { match Pipelines::new() { Ok(p) => Some(p), @@ -1231,6 +1239,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()); + let unused = AtomicUsize::new(0); + if let Some(u) = used { + println!("--used-only: {} pipelines earlier runs used (used-pipelines.txt); compute programs of other pipelines are passed over", u.len()); + } // 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 @@ -1264,6 +1276,11 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on // a compute program as a title's pipeline translates it (`pso::console_to_dxil_in`): // into the translation cache too, so the title does not translate it again if sh.fssm.iter().any(|f| matches!(recomp::interface(&blob, &sh, f), Some(recomp::Mode::Compute(_)))) { + if used.is_some_and(|u| !used_compute(&blob, u)) { + unused.fetch_add(1, Ordering::Relaxed); + done.fetch_add(1, Ordering::Relaxed); + continue; + } match recomp::pso::console_to_dxil_in(&blob, recomp::pso::Stage::Compute, 0, split, windows) { Ok(c) => { if let Some(p) = pipelines { @@ -1337,6 +1354,9 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on skipped.into_inner(), t0.elapsed().as_secs_f64() ); + if used.is_some() { + println!("compute programs passed over (no earlier run used their pipeline): {}", unused.into_inner()); + } if let Some(p) = pipelines { 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)); @@ -1674,6 +1694,21 @@ fn print(path: &Path, what: &str, agg: Agg, top: usize, json: bool, t0: std::tim #[cfg(test)] mod tests { + /// `--used-only` keeps a compute program exactly when the runtime noted its pipeline: the + /// key of a pipeline of that one shader, as `real::shaders_key` makes it. + #[test] + fn used_only_keeps_the_compute_programs_an_earlier_run_used() { + let (a, b) = (vec![1u8, 2, 3, 4], vec![5u8, 6, 7, 8]); + let used: std::collections::HashSet = [crate::hle::d3d12x::real::shaders_key(&[a.clone()])].into_iter().collect(); + assert!(super::used_compute(&a, &used)); + assert!(!super::used_compute(&b, &used)); + // the keys as the runtime writes them, one hexadecimal key a line + let text = format!("{:016x} +", crate::hle::d3d12x::real::shaders_key(&[b.clone()])); + let keys: std::collections::HashSet = text.lines().filter_map(|l| u64::from_str_radix(l.trim(), 16).ok()).collect(); + assert!(super::used_compute(&b, &keys)); + } + use super::*; use crate::gpu::xbox::tests::{fssm, sample, CODE};