From 70ad0a5dc718af6e6b72e18155ee32f0e2b8d229 Mon Sep 17 00:00:00 2001 From: CryingLaughing <62956736+CryingLaughing@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:32:48 +0800 Subject: [PATCH] Shaders: precompile FSPX-only compute shaders from folders Recognize ACCC/FSPX compute inputs when the metadata parser finds no XDAT. Keep the existing stage filter, runtime translation, and native pipeline cache path. Part of #2453. --- README.md | 2 + crates/xcl/src/shaders.rs | 21 ++++++--- crates/xcl/tests/gpu_compute_e2e.rs | 69 +++++++++++++++++++++++++++-- 3 files changed, 82 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1bd0c54868..e3dfb962be 100644 --- a/README.md +++ b/README.md @@ -230,6 +230,8 @@ xcl shaders D:\data\shaders.rpf --measure-pso HEX --repeat 3 # cold driv xcl rpf D:\data\shaders.rpf --extract NAME --out folder ``` +`xcl shaders --precompile --only cs` also accepts ACCC/FSPX compute containers. +
Switches, caches, logs and the files XCL writes diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 4c5a014974..c69b157ef1 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -1708,20 +1708,26 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on record_done(); continue; }; - let Some(sh) = xbox::parse(&blob) else { - if startup.is_some() && !matches!(shader::classify(&blob).0, shader::Kind::Dxil | shader::Kind::Dxbc) { - failed.fetch_add(1, Ordering::Relaxed); + let shader = xbox::parse(&blob); + let stage = match &shader { + Some(sh) => sh.stage.as_str(), + None if recomp::pso::fspx(&blob).is_some() => "CS", + None => { + if startup.is_some() && !matches!(shader::classify(&blob).0, shader::Kind::Dxil | shader::Kind::Dxbc) { + failed.fetch_add(1, Ordering::Relaxed); + } + record_done(); + continue; } - record_done(); - continue; }; - if only.is_some_and(|o| !sh.stage.eq_ignore_ascii_case(o)) { + if only.is_some_and(|o| !stage.eq_ignore_ascii_case(o)) { record_done(); continue; } // 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(_)))) { + // An admitted shader without XDAT is the FSPX compute case above. + if shader.as_ref().is_none_or(|sh| 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); record_done(); @@ -1748,6 +1754,7 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on record_done(); continue; } + let Some(sh) = shader else { continue }; for f in &sh.fssm { let Some(mode) = recomp::interface(&blob, &sh, f) else { continue }; let jobs: Vec<(&str, Option)> = match &mode { diff --git a/crates/xcl/tests/gpu_compute_e2e.rs b/crates/xcl/tests/gpu_compute_e2e.rs index 5ffa519a96..6edf81b2d7 100644 --- a/crates/xcl/tests/gpu_compute_e2e.rs +++ b/crates/xcl/tests/gpu_compute_e2e.rs @@ -72,19 +72,82 @@ fn console_cs_flags(code: &[u8], rsrc2: u32, threads: [u32; 3], flags: u32) -> V let dwords = (d.len() / 4) as u32; d[4..8].copy_from_slice(&dwords.to_le_bytes()); d[16..20].copy_from_slice(&((nobc - 8) as u32).to_le_bytes()); + shader_container(b"DXIL", &d) +} + +fn shader_container(tag: &[u8; 4], data: &[u8]) -> Vec { let mut v = vec![0u8; 36]; v[0..4].copy_from_slice(b"DXBC"); v[20..24].copy_from_slice(&1u32.to_le_bytes()); v[28..32].copy_from_slice(&1u32.to_le_bytes()); v[32..36].copy_from_slice(&36u32.to_le_bytes()); - v.extend_from_slice(b"DXIL"); - le(&mut v, d.len() as u32); - v.extend_from_slice(&d); + v.extend_from_slice(tag); + le(&mut v, data.len() as u32); + v.extend_from_slice(data); let total = v.len() as u32; v[24..28].copy_from_slice(&total.to_le_bytes()); v } +#[test] +fn folder_precompile_includes_fspx_only_compute() { + use std::{fs, path::PathBuf, process::Command}; + if xcl::gpu::recomp::dxc::location().is_err() { + return runner::skip_test("no DXC"); + } + let mut part = Vec::from(*b"FSPX"); + // Two directory entries: type 0 is the compute header, type 6 is the code. + for word in [1, 2, 0x74, 0, 0x30, 0x1c, 0, 6, 0x4c, 0x28, 0] { + le(&mut part, word); + } + part.resize(0x74, 0); + part[0x3c..0x42].copy_from_slice(&[1, 0, 1, 0, 1, 0]); // numthreads(1, 1, 1) + part[0x70..].copy_from_slice(&0xbf81_0000u32.to_le_bytes()); // s_endpgm + let blob = shader_container(b"ACCC", &part); + assert!(xcl::gpu::xbox::parse(&blob).is_none()); + assert!(xcl::gpu::recomp::pso::fspx(&blob).is_some()); + + struct Directory(PathBuf); + impl Drop for Directory { + fn drop(&mut self) { let _ = fs::remove_dir_all(&self.0); } + } + let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); + let root = std::env::temp_dir().join(format!("xcl-fspx-precompile-{}-{nonce}", std::process::id())); + fs::create_dir(&root).unwrap(); + let dir = Directory(root); + let input = dir.0.join("input"); + fs::create_dir(&input).unwrap(); + fs::write(input.join("compute.bin"), blob).unwrap(); + let precompile = |only, cache, extra: &[&str]| { + let output = Command::new(env!("CARGO_BIN_EXE_xcl")) + .arg("shaders").arg(&input) + .args(["--precompile", "--only", only, "--split", "1"]) + .args(extra) + .env("XCL_DXIL_CACHE", dir.0.join(cache)) + .env("XCL_DXC_THREADS", "1") + .env("XCL_DIRECT_DXIL", "0") + .env("XCL_PSO_DISK_CACHE", "1") + .output().unwrap(); + let text = String::from_utf8_lossy(&output.stdout).into_owned(); + assert!(output.status.success(), "{text}\n{}", String::from_utf8_lossy(&output.stderr)); + println!("FSPX precompile --only {only} {extra:?}\n{text}"); + text + }; + let ps = precompile("ps", "pixel-cache", &[]); + assert!(ps.contains("compiled 0, already cached 0, errors 0"), "{ps}"); + let cs = precompile("cs", "compute-cache", &[]); + assert!(cs.contains("compiled 1, already cached 0, errors 0"), "{cs}"); + + if !runner::wave32_supported().unwrap_or(false) { + return runner::skip_test("no wave32 GPU for the native pipeline check"); + } + let native = precompile("cs", "native-cache", &["--pipelines"]); + assert!(native.contains("pipelines made on the GPU 1, refused 0"), "{native}"); + let mut files = fs::read_dir(dir.0.join("native-cache/driver")).unwrap(); + let blob = files.next().unwrap().unwrap().path(); + assert!(files.next().is_none() && blob.metadata().unwrap().len() > 0); +} + const V: u64 = 256; // VALU source code of v0 /// Make several guarded arithmetic blocks so the same shader has both whole and split forms.