Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ xcl run D:\Game [-- game arguments] # run
xcl disasm <shader.bin> [--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
Expand Down
6 changes: 6 additions & 0 deletions crates/xcl/src/hle/d3d12x/pso_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> {
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).
///
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>]) -> u64 {
pub(crate) fn shaders_key(blobs: &[Vec<u8>]) -> 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);
Expand Down
39 changes: 37 additions & 2 deletions crates/xcl/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1214,7 +1216,13 @@ impl Pipelines {
}
}

fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, 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<u64>) -> bool {
used.contains(&crate::hle::d3d12x::real::shaders_key(&[blob.to_vec()]))
}

fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, only: Option<&str>, pipelines: bool, used: Option<&std::collections::HashSet<u64>>, t0: std::time::Instant) -> i32 {
let pipelines = if pipelines {
match Pipelines::new() {
Ok(p) => Some(p),
Expand All @@ -1231,6 +1239,10 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, 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<Vec<String>> = 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
Expand Down Expand Up @@ -1264,6 +1276,11 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, 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 {
Expand Down Expand Up @@ -1337,6 +1354,9 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, 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));
Expand Down Expand Up @@ -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<u64> = [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<u64> = 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};

Expand Down
Loading