diff --git a/README.md b/README.md index a5edccbb69..99e75896fb 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,10 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder - For compute shaders that XCL can split, the runtime first tries the saved driver blob for the whole shader. A usable blob lets the GPU run it in one dispatch. If the blob is missing or the driver rejects it, XCL builds the split pieces. +- A split program first seen through `ExecuteIndirect` is also kept in + `used-indirect-split.txt` beside the DXIL cache. A later `--pipelines --used-only` pass still + builds its direct-dispatch pieces and additionally saves both its pieces and whole fallback under + the indirect root; other split programs do not pay for those potentially long driver compiles. - **`--pipelines` size cap**: a compute program whose DXIL is over 8 MiB and that has no saved blob is left to the runtime instead of being compiled in the pass — those compiles take tens of minutes and gigabytes and stopped the pass on the biggest programs. `XCL_PIPELINE_MAX_KB` diff --git a/crates/xcl/src/eh.rs b/crates/xcl/src/eh.rs index d2a7674fee..2a94cd36e0 100644 --- a/crates/xcl/src/eh.rs +++ b/crates/xcl/src/eh.rs @@ -137,6 +137,7 @@ unsafe extern "system" fn hk_nt_terminate_process(h: isize, status: i32) -> i32 } crate::log::flush(); crate::hle::d3d12x::pso_async::flush_used_bounded(std::time::Duration::from_millis(100)); + crate::hle::d3d12x::real::flush_indirect_split_records(std::time::Duration::from_millis(100)); } let orig: unsafe extern "system" fn(isize, i32) -> i32 = std::mem::transmute(ORIG_NT_TERMINATE.load(Ordering::Acquire)); orig(h, status) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index ed76e406ff..3800992107 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4875,6 +4875,9 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const pub(super) mod dispatch_chain; pub(crate) use dispatch_chain::log_counts as log_dispatch_chain_counts; +pub(crate) use dispatch_chain::earlier_indirect_used as earlier_indirect_split_records; +pub(crate) use dispatch_chain::flush_indirect_used as flush_indirect_split_records; +pub(crate) use dispatch_chain::shutdown_indirect_used as shutdown_indirect_split_records; pub(super) mod remap_fallback { use super::*; diff --git a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs index 9986c135c2..29db8ef012 100644 --- a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs +++ b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs @@ -1,7 +1,9 @@ //! PSOs for a split compute entry. The first piece is the real PSO returned //! to the caller. Other pieces and the lazily built whole form belong to it. use super::*; +use super::super::used_journal::Journal; use crate::gpu::{dxil::kernel::DispatchChain, recomp::pso}; +use std::collections::HashSet; use std::sync::{Arc, LazyLock, OnceLock, Weak}; static PROGRAMS: AtomicU64 = AtomicU64::new(0); @@ -14,6 +16,55 @@ static LIVE: AtomicU64 = AtomicU64::new(0); static PEAK: AtomicU64 = AtomicU64::new(0); static WANTED: AtomicU64 = AtomicU64::new(0); +struct IndirectUsed { + now: Journal, +} + +static INDIRECT_USED: OnceLock = OnceLock::new(); + +fn parse_used(text: &str) -> HashSet { + text.lines() + .filter_map(|line| u64::from_str_radix(line.trim(), 16).ok()) + .filter(|&record| record != 0) + .collect() +} + +fn indirect_used_path() -> std::path::PathBuf { + crate::gpu::recomp::dxc::cache_dir().join("used-indirect-split.txt") +} + +fn indirect_used() -> &'static IndirectUsed { + INDIRECT_USED.get_or_init(|| { + if cfg!(test) { + return IndirectUsed { now: Journal::memory(HashSet::new()) }; + } + let path = indirect_used_path(); + let earlier = parse_used(&std::fs::read_to_string(&path).unwrap_or_default()); + let now = Journal::file(path, earlier.clone()); + IndirectUsed { now } + }) +} + +fn note_indirect_use(record: u64) { + indirect_used().now.note(record); +} + +pub(crate) fn earlier_indirect_used() -> HashSet { + parse_used(&std::fs::read_to_string(indirect_used_path()).unwrap_or_default()) +} + +pub(crate) fn shutdown_indirect_used(timeout: std::time::Duration) { + if let Some(used) = INDIRECT_USED.get() { + let _ = used.now.shutdown_bounded(timeout); + } +} + +pub(crate) fn flush_indirect_used(timeout: std::time::Duration) { + if let Some(used) = INDIRECT_USED.get() { + let _ = used.now.flush_bounded(timeout); + } +} + pub(crate) fn log_counts() { let mib = |a: &AtomicU64| a.load(Ordering::Relaxed) >> 20; crate::xlog_info!("compute split: {} programs split, {} pieces built, {} chained dispatches issued in {} slabs; scratch {} MiB (most {}, largest dispatch {} in one slab)", @@ -457,6 +508,7 @@ pub(in crate::hle::d3d12x) unsafe fn execute_indirect( bind: impl FnOnce(usize) -> Result<(), String>, ) -> Result { let Some(chain) = CHAINS.lock().unwrap().get(&pso).cloned() else { return Ok(false) }; + note_indirect_use(chain.builder.record); match chain.whole.ready() { None => return Ok(false), Some(Ok(_)) => {} @@ -503,6 +555,11 @@ pub(in crate::hle::d3d12x) unsafe fn execute_indirect( mod tests { use super::*; + #[test] + fn indirect_used_records_parse_unique_nonzero_hex() { + assert_eq!(parse_used("1\n0000000000000002\ninvalid\n0\n1\n"), [1, 2].into()); + } + const MB: u64 = 1 << 20; #[test] diff --git a/crates/xcl/src/hle/d3d12x/used_journal.rs b/crates/xcl/src/hle/d3d12x/used_journal.rs index 11f6083082..685b0a0e28 100644 --- a/crates/xcl/src/hle/d3d12x/used_journal.rs +++ b/crates/xcl/src/hle/d3d12x/used_journal.rs @@ -160,8 +160,7 @@ impl Journal { #[cfg(test)] pub(super) fn contains(&self, key: u64) -> bool { self.shared.state.lock().unwrap_or_else(|e| e.into_inner()).seen.contains(&key) } - #[cfg(test)] - fn flush_bounded(&self, timeout: Duration) -> bool { self.wait_bounded(timeout, false) } + pub(super) fn flush_bounded(&self, timeout: Duration) -> bool { self.wait_bounded(timeout, false) } pub(super) fn shutdown_bounded(&self, timeout: Duration) -> bool { self.wait_bounded(timeout, true) } diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index 5209dff67c..c4730abac6 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -215,6 +215,7 @@ pub fn host_main(boot_path: &Path) -> i32 { crate::report::write_final(None); crate::log::flush(); crate::hle::d3d12x::pso_async::shutdown_used(); + crate::hle::d3d12x::real::shutdown_indirect_split_records(std::time::Duration::from_millis(500)); crate::hle::d3d12x::real::end_title(0); }); } @@ -234,6 +235,7 @@ pub fn host_main(boot_path: &Path) -> i32 { crate::report::write_final(None); crate::log::flush(); crate::hle::d3d12x::pso_async::shutdown_used(); + crate::hle::d3d12x::real::shutdown_indirect_split_records(std::time::Duration::from_millis(500)); code as i32 } diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 506e50c12b..c98e2ddae1 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -1092,7 +1092,10 @@ pub fn run(path: &Path, args: &[String]) -> i32 { Ok(coverage) => coverage, Err(e) => { eprintln!("xcl shaders: {e}"); return 2; } }; - return match precompile(&input, opts.limit, windows, opts.split, opts.only.as_deref(), opts.pipelines, selected.as_ref(), opts.threads, t0, None, coverage.as_ref()) { + let indirect = (opts.used_only && opts.pipelines) + .then(crate::hle::d3d12x::real::earlier_indirect_split_records); + return match precompile(&input, opts.limit, windows, opts.split, opts.only.as_deref(), opts.pipelines, + selected.as_ref(), indirect.as_ref(), opts.threads, t0, None, coverage.as_ref()) { Ok(()) => 0, Err(e) => { eprintln!("xcl shaders: {e}"); 2 } }; @@ -1322,7 +1325,7 @@ fn measure_pso_result(input: &Input, hash: u64, repeat: usize, windows: u32, spl /// (the > 8 MB HLSL programs DXC compiles at `-O0`, #1269). struct Pipelines { dev: windows::Win32::Graphics::Direct3D12::ID3D12Device, - signatures: Mutex), windows::Win32::Graphics::Direct3D12::ID3D12RootSignature>>, + signatures: Mutex), windows::Win32::Graphics::Direct3D12::ID3D12RootSignature>>, warm_blobs: Option, verified: AtomicUsize, made: AtomicUsize, @@ -1334,6 +1337,7 @@ struct Pipelines { elsewhere: AtomicUsize, split_programs: AtomicUsize, split_pieces: AtomicUsize, + indirect_programs: AtomicUsize, /// programs over `max_dxil` with no blob yet, and their DXIL bytes capped: AtomicUsize, capped_bytes: AtomicUsize, @@ -1369,6 +1373,21 @@ mod pipeline_write_tests { } } +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +enum PipelineRoot { + Whole, + DirectChain, + IndirectChain, +} + +fn split_piece_roots(prewarm_indirect: bool) -> &'static [PipelineRoot] { + if prewarm_indirect { + &[PipelineRoot::DirectChain, PipelineRoot::IndirectChain] + } else { + &[PipelineRoot::DirectChain] + } +} + #[derive(Clone, Debug, Eq, Hash, PartialEq)] struct PipelineCacheIdentity { dxil_len: usize, @@ -1376,7 +1395,7 @@ struct PipelineCacheIdentity { dxil_hash_b: u64, flags: i32, node_mask: u32, - chained: bool, + root: PipelineRoot, sampler_words: Vec, cache_path: Option, } @@ -1386,7 +1405,7 @@ impl PipelineCacheIdentity { dxil: &[u8], flags: i32, node_mask: u32, - chained: bool, + root: PipelineRoot, sampler_words: Vec, cache_path: Option, ) -> Self { @@ -1401,7 +1420,7 @@ impl PipelineCacheIdentity { dxil_hash_b: alt, flags, node_mask, - chained, + root, sampler_words, cache_path, } @@ -1439,33 +1458,40 @@ impl Drop for InflightGuard<'_> { #[cfg(test)] mod inflight_build_tests { - use super::{InflightBuilds, InflightGuard, PipelineCacheIdentity}; + use super::{InflightBuilds, InflightGuard, PipelineCacheIdentity, PipelineRoot}; use std::path::PathBuf; use std::sync::{mpsc, Condvar, Mutex}; use std::time::Duration; - fn identity(dxil: &[u8], flags: i32, node: u32, chained: bool, samplers: &[u32], path: &str) -> PipelineCacheIdentity { - PipelineCacheIdentity::new(dxil, flags, node, chained, samplers.to_vec(), Some(PathBuf::from(path))) + fn identity(dxil: &[u8], flags: i32, node: u32, root: PipelineRoot, samplers: &[u32], path: &str) -> PipelineCacheIdentity { + PipelineCacheIdentity::new(dxil, flags, node, root, samplers.to_vec(), Some(PathBuf::from(path))) } #[test] fn identity_covers_every_pipeline_input() { - let base = identity(b"abc", 0, 0, false, &[1, 2], "a.pso"); - assert_eq!(base, identity(b"abc", 0, 0, false, &[1, 2], "a.pso")); - assert_ne!(base, identity(b"abd", 0, 0, false, &[1, 2], "a.pso")); - assert_ne!(base, identity(b"abc", 1, 0, false, &[1, 2], "a.pso")); - assert_ne!(base, identity(b"abc", 0, 2, false, &[1, 2], "a.pso")); - assert_ne!(base, identity(b"abc", 0, 0, true, &[1, 2], "a.pso")); - assert_ne!(base, identity(b"abc", 0, 0, false, &[1, 3], "a.pso")); - assert_ne!(base, identity(b"abc", 0, 0, false, &[1, 2], "b.pso")); - assert_ne!(base, PipelineCacheIdentity::new(b"abc", 0, 0, false, vec![1, 2], None)); + let base = identity(b"abc", 0, 0, PipelineRoot::Whole, &[1, 2], "a.pso"); + assert_eq!(base, identity(b"abc", 0, 0, PipelineRoot::Whole, &[1, 2], "a.pso")); + assert_ne!(base, identity(b"abd", 0, 0, PipelineRoot::Whole, &[1, 2], "a.pso")); + assert_ne!(base, identity(b"abc", 1, 0, PipelineRoot::Whole, &[1, 2], "a.pso")); + assert_ne!(base, identity(b"abc", 0, 2, PipelineRoot::Whole, &[1, 2], "a.pso")); + assert_ne!(base, identity(b"abc", 0, 0, PipelineRoot::DirectChain, &[1, 2], "a.pso")); + assert_ne!(base, identity(b"abc", 0, 0, PipelineRoot::IndirectChain, &[1, 2], "a.pso")); + assert_ne!(base, identity(b"abc", 0, 0, PipelineRoot::Whole, &[1, 3], "a.pso")); + assert_ne!(base, identity(b"abc", 0, 0, PipelineRoot::Whole, &[1, 2], "b.pso")); + assert_ne!(base, PipelineCacheIdentity::new(b"abc", 0, 0, PipelineRoot::Whole, vec![1, 2], None)); + } + + #[test] + fn indirect_selection_adds_indirect_pieces_without_changing_direct_pieces() { + assert_eq!(super::split_piece_roots(false), &[PipelineRoot::DirectChain]); + assert_eq!(super::split_piece_roots(true), &[PipelineRoot::DirectChain, PipelineRoot::IndirectChain]); } #[test] fn same_build_waits_while_distinct_builds_and_failures_can_proceed() { let state = (Mutex::new(InflightBuilds::default()), Condvar::new()); - let base = identity(b"program", 0, 0, false, &[], "same.pso"); - let other = identity(b"other", 0, 0, false, &[], "other.pso"); + let base = identity(b"program", 0, 0, PipelineRoot::Whole, &[], "same.pso"); + let other = identity(b"other", 0, 0, PipelineRoot::Whole, &[], "other.pso"); let first = InflightGuard::enter(&state, base.clone()); drop(InflightGuard::enter(&state, other)); @@ -1483,7 +1509,7 @@ mod inflight_build_tests { done_rx.recv_timeout(Duration::from_secs(1)).expect("waiter stayed blocked"); }); - let retry = identity(b"retry", 0, 0, false, &[], "retry.pso"); + let retry = identity(b"retry", 0, 0, PipelineRoot::Whole, &[], "retry.pso"); let panicked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { let _failed = InflightGuard::enter(&state, retry.clone()); panic!("simulated build failure"); @@ -1951,6 +1977,7 @@ impl Pipelines { elsewhere: AtomicUsize::new(0), split_programs: AtomicUsize::new(0), split_pieces: AtomicUsize::new(0), + indirect_programs: AtomicUsize::new(0), capped: AtomicUsize::new(0), capped_bytes: AtomicUsize::new(0), inflight: (Mutex::new(InflightBuilds::default()), std::sync::Condvar::new()), @@ -1968,35 +1995,30 @@ impl Pipelines { /// dispatch-chain root signature (`.chain.pso`) as the runtime does (`dispatch_chain::build`), /// instead of the unsplit whole container. fn make(&self, c: &recomp::pso::Compiled) { - self.make_with_cache_path(c, None); + self.make_selected(c, false, None); } + #[cfg(test)] fn make_with_cache_path(&self, c: &recomp::pso::Compiled, path_override: Option) { + self.make_selected(c, false, path_override); + } + + fn make_selected(&self, c: &recomp::pso::Compiled, prewarm_indirect: bool, path_override: Option) { if path_override.is_none() { if let Some(chain) = c.dispatch_chain.as_deref().filter(|chain| chain.pieces.len() >= 2) { self.split_programs.fetch_add(1, Ordering::Relaxed); self.split_pieces.fetch_add(chain.pieces.len(), Ordering::Relaxed); - let workers = chain.pieces.len().min(self.most); - if workers <= 1 { - for piece in &chain.pieces { - self.make_one(piece, &c.samplers, true, None); - } - } else { - let next = AtomicUsize::new(0); - std::thread::scope(|s| { - for _ in 0..workers { - s.spawn(|| loop { - let i = next.fetch_add(1, Ordering::Relaxed); - let Some(piece) = chain.pieces.get(i) else { break }; - self.make_one(piece, &c.samplers, true, None); - }); - } - }); + for &root in split_piece_roots(prewarm_indirect) { + self.make_chain_pieces(&chain.pieces, &c.samplers, root); + } + if prewarm_indirect { + self.indirect_programs.fetch_add(1, Ordering::Relaxed); + self.make_one(&c.dxil, &c.samplers, PipelineRoot::IndirectChain, None); } return; } } - self.make_one(&c.dxil, &c.samplers, false, path_override); + self.make_one(&c.dxil, &c.samplers, PipelineRoot::Whole, path_override); } fn prepare_compute(&self, blob: &[u8], windows: u32) -> Result<(), String> { @@ -2006,39 +2028,59 @@ impl Pipelines { } fn prepare_compiled_compute(&self, program: &recomp::pso::Compiled) -> Result<(), String> { - let make = |dxil: &[u8], chained| { - self.make_required(dxil, &program.samplers, chained, None, true) + let make = |dxil: &[u8], root| { + self.make_required(dxil, &program.samplers, root, None, true) }; - make(&program.dxil, false)?; + make(&program.dxil, PipelineRoot::Whole)?; if let Some(chain) = program.dispatch_chain.as_deref().filter(|c| c.pieces.len() >= 2) { - for piece in &chain.pieces { make(piece, true)?; } - make(&program.dxil, true)?; + for piece in &chain.pieces { make(piece, PipelineRoot::DirectChain)?; } + make(&program.dxil, PipelineRoot::DirectChain)?; } Ok(()) } - fn make_one(&self, dxil: &[u8], samplers: &[recomp::pso::StaticSampler], chained: bool, path_override: Option) { - let _ = self.make_required(dxil, samplers, chained, path_override, false); + fn make_chain_pieces(&self, pieces: &[Vec], samplers: &[recomp::pso::StaticSampler], root: PipelineRoot) { + let workers = pieces.len().min(self.most); + if workers <= 1 { + for piece in pieces { + self.make_one(piece, samplers, root, None); + } + } else { + let next = AtomicUsize::new(0); + std::thread::scope(|s| { + for _ in 0..workers { + s.spawn(|| loop { + let i = next.fetch_add(1, Ordering::Relaxed); + let Some(piece) = pieces.get(i) else { break }; + self.make_one(piece, samplers, root, None); + }); + } + }); + } + } + + fn make_one(&self, dxil: &[u8], samplers: &[recomp::pso::StaticSampler], root: PipelineRoot, path_override: Option) { + let _ = self.make_required(dxil, samplers, root, path_override, false); } - fn make_required(&self, dxil: &[u8], samplers: &[recomp::pso::StaticSampler], chained: bool, + fn make_required(&self, dxil: &[u8], samplers: &[recomp::pso::StaticSampler], root: PipelineRoot, path_override: Option, required: bool) -> Result<(), String> { use windows::Win32::Graphics::Direct3D12::*; let sampler_words: Vec = samplers .iter() .flat_map(|(w, cmp)| w.iter().copied().chain([*cmp as u32])) .collect(); - let key = (chained, sampler_words.clone()); + let key = (root, sampler_words.clone()); let layout = self.warm_blobs.as_ref().map(|_| warm_blobs::digest(&key)); let rs = { let mut g = self.signatures.lock().unwrap(); match g.get(&key) { Some(rs) => rs.clone(), None => { - let created = if chained { - recomp::pso::dispatch_chain_root_signature(&self.dev, samplers) - } else { - recomp::pso::abi_root_signature_with(&self.dev, true, samplers) + let created = match root { + PipelineRoot::Whole => recomp::pso::abi_root_signature_with(&self.dev, true, samplers), + PipelineRoot::DirectChain => recomp::pso::dispatch_chain_root_signature(&self.dev, samplers), + PipelineRoot::IndirectChain => recomp::pso::dispatch_chain_indirect_root_signature(&self.dev, samplers), }; match created { Ok(rs) => { @@ -2062,9 +2104,25 @@ impl Pipelines { let path = path_override.or_else(|| { #[cfg(test)] if let Some(dir) = self.cache_dir.as_deref() { - return Some(dir.join(driver_cache::name_for_root(dxil, desc.Flags.0, chained, samplers))); + return Some(dir.join(match root { + PipelineRoot::Whole => driver_cache::name_for_root(dxil, desc.Flags.0, false, samplers), + PipelineRoot::DirectChain => driver_cache::name_for_chain_root_node( + dxil, desc.Flags.0, driver_cache::ChainRoot::DirectConstants, samplers, desc.NodeMask, + ), + PipelineRoot::IndirectChain => driver_cache::name_for_chain_root_node( + dxil, desc.Flags.0, driver_cache::ChainRoot::IndirectCbv, samplers, desc.NodeMask, + ), + })); + } + match root { + PipelineRoot::Whole => driver_cache::path_for_root(dxil, desc.Flags.0, false, samplers), + PipelineRoot::DirectChain => driver_cache::path_for_chain_root_node( + dxil, desc.Flags.0, driver_cache::ChainRoot::DirectConstants, samplers, desc.NodeMask, + ), + PipelineRoot::IndirectChain => driver_cache::path_for_chain_root_node( + dxil, desc.Flags.0, driver_cache::ChainRoot::IndirectCbv, samplers, desc.NodeMask, + ), } - driver_cache::path_for_root(dxil, desc.Flags.0, chained, samplers) }); let mut loaded = path.as_deref().and_then(driver_cache::load); let mut cached = loaded.as_ref().map(driver_cache::LoadedBlob::shared); @@ -2086,7 +2144,7 @@ impl Pipelines { drop(std::mem::ManuallyDrop::into_inner(desc.pRootSignature)); return Err(format!("Compute DXIL exceeds the native pipeline size limit ({} bytes)", dxil.len())); } - let identity = PipelineCacheIdentity::new(dxil, desc.Flags.0, desc.NodeMask, chained, sampler_words, path.clone()); + let identity = PipelineCacheIdentity::new(dxil, desc.Flags.0, desc.NodeMask, root, sampler_words, path.clone()); let _inflight = InflightGuard::enter(&self.inflight, identity); // A valid blob may be used by many processes, but validation itself can fall through to // a full driver compile when the blob is stale. Claim before either path so only one @@ -2183,7 +2241,15 @@ impl Pipelines { pso }; let library = match &r { - Ok(pso) => pipeline_library::store(driver_cache::name_for_root(dxil, desc.Flags.0, chained, samplers), pso), + Ok(pso) => pipeline_library::store(match root { + PipelineRoot::Whole => driver_cache::name_for_root(dxil, desc.Flags.0, false, samplers), + PipelineRoot::DirectChain => driver_cache::name_for_chain_root_node( + dxil, desc.Flags.0, driver_cache::ChainRoot::DirectConstants, samplers, desc.NodeMask, + ), + PipelineRoot::IndirectChain => driver_cache::name_for_chain_root_node( + dxil, desc.Flags.0, driver_cache::ChainRoot::IndirectCbv, samplers, desc.NodeMask, + ), + }, pso), Err(_) => Ok(()), }; drop(std::mem::ManuallyDrop::into_inner(desc.pRootSignature)); @@ -2222,6 +2288,7 @@ fn used_compute(blob: &[u8], used: &std::collections::HashSet) -> bool { struct PipelineJob { used: bool, + prewarm_indirect: bool, compiled: recomp::pso::Compiled, } @@ -2233,7 +2300,7 @@ fn make_pipelines(pipelines: &Pipelines, mut jobs: Vec) { order_pipeline_jobs(&mut jobs); let used = jobs.iter().filter(|job| job.used).count(); println!("pipelines: {} compute programs ({used} used earlier: first), {} driver compiles at once", jobs.len(), pipelines.most); - run_pipeline_jobs(jobs, pipelines.most, |job| pipelines.make(&job.compiled)); + run_pipeline_jobs(jobs, pipelines.most, |job| pipelines.make_selected(&job.compiled, job.prewarm_indirect, None)); } fn run_pipeline_jobs(jobs: Vec, workers: usize, build: impl Fn(&T) + Sync) { @@ -2258,6 +2325,7 @@ mod pipeline_selection_tests { fn job(used: bool, bytes: usize) -> PipelineJob { PipelineJob { used, + prewarm_indirect: false, compiled: recomp::pso::Compiled { dispatch_chain: None, dxil: vec![0; bytes], samplers: Vec::new() }, } } @@ -2413,13 +2481,15 @@ fn prepare_title_inputs(root: &Path, progress: &LaunchPreparation, corpus: Optio // about two pipelines at a time for one process, whatever the processors) if workers > 1 && matches!(input, Input::Pack { .. }) { let n = input.len(); - precompile(input, usize::MAX, windows, None, None, false, None, None, std::time::Instant::now(), Some(progress), coverage.as_ref())?; + precompile(input, usize::MAX, windows, None, None, false, None, None, None, + std::time::Instant::now(), Some(progress), coverage.as_ref())?; if progress.cancel.load(Ordering::Relaxed) { return Err("Shader preparation cancelled".into()); } if coverage.as_ref().is_some_and(ComputeCoverage::all_staged) { continue; } pipelines_in_workers(&pack, windows, workers, n, progress, coverage.as_ref())?; continue; } - precompile(input, usize::MAX, windows, None, None, true, None, None, std::time::Instant::now(), Some(progress), coverage.as_ref())?; + precompile(input, usize::MAX, windows, None, None, true, None, None, None, + std::time::Instant::now(), Some(progress), coverage.as_ref())?; } if progress.cancel.load(Ordering::Relaxed) { return Err("Shader preparation cancelled".into()); } if std::env::var("XCL_PREPARE_GRAPHICS").is_ok_and(|v| v.trim() == "0") { return Ok(()); } @@ -3018,6 +3088,10 @@ fn precompile_slice(startup_owner: bool, value: Option<&str>) -> Option<(usize, Some((i.trim().parse().ok()?, n.trim().parse::().ok().filter(|n| *n > 0)?)) } +fn used_indirect_split(blob: &[u8], used: &std::collections::HashSet) -> bool { + used.contains(&crate::gpu::shader::fnv64(blob)) +} + /// Standalone graphics variants whose runtime cache keys can be known without a pipeline pairing. /// Runtime selection also uses the first hardware stage of the first matching FSSM (4: VS, 5: PS). fn standalone_graphics_variants(blob: &[u8], sh: &xbox::XboxShader, split: bool, windows: u32) -> Vec<(recomp::pso::Stage, u32, bool, u32)> { @@ -3031,7 +3105,10 @@ fn standalone_graphics_variants(blob: &[u8], sh: &xbox::XboxShader, split: bool, .collect() } -fn precompile(input: &Input, limit: usize, windows: u32, split: Option, only: Option<&str>, pipelines: bool, selected: Option<&std::collections::HashSet>, pipeline_threads: Option, t0: std::time::Instant, startup: Option<&LaunchPreparation>, coverage: Option<&ComputeCoverage>) -> Result<(), String> { +fn precompile(input: &Input, limit: usize, windows: u32, split: Option, only: Option<&str>, pipelines: bool, + selected: Option<&std::collections::HashSet>, indirect: Option<&std::collections::HashSet>, + pipeline_threads: Option, t0: std::time::Instant, startup: Option<&LaunchPreparation>, + coverage: Option<&ComputeCoverage>) -> Result<(), String> { if startup.is_none() { end_with_parent(); } let mandatory = startup.is_some() || std::env::var_os("XCL_PRECOMPILE_PARENT").is_some(); let pipelines = if pipelines { @@ -3069,6 +3146,9 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on if let Some(selected) = selected { println!("selection: {} record hashes or pipeline keys requested", selected.len()); } + if let Some(i) = indirect { + println!("--used-only: {} split programs earlier runs dispatched indirectly get indirect pieces and a whole fallback", i.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 @@ -3175,7 +3255,8 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on } } else { let used = used_earlier.contains(&pipeline_key(&blob)); - pipeline_jobs.lock().unwrap().push(PipelineJob { used, compiled: c }); + let prewarm_indirect = indirect.is_some_and(|i| used_indirect_split(&blob, i)); + pipeline_jobs.lock().unwrap().push(PipelineJob { used, prewarm_indirect, compiled: c }); } } compiled.fetch_add(1, Ordering::Relaxed) @@ -3306,6 +3387,10 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on if split_programs != 0 { println!("split compute programs: {} ({} pieces)", split_programs, p.split_pieces.load(Ordering::Relaxed)); } + let indirect_programs = p.indirect_programs.load(Ordering::Relaxed); + if indirect_programs != 0 { + println!("indirect split programs (pieces and whole fallback): {indirect_programs}"); + } let capped = p.capped.load(Ordering::Relaxed); if capped != 0 { println!("pipelines over the {} MiB DXIL cap, left to the runtime: {} ({} MiB of DXIL; XCL_PIPELINE_MAX_KB)", p.max_dxil >> 20, capped, p.capped_bytes.load(Ordering::Relaxed) >> 20); @@ -3824,6 +3909,9 @@ mod tests { ", 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)); + let indirect: std::collections::HashSet = [crate::gpu::shader::fnv64(&a)].into(); + assert!(super::used_indirect_split(&a, &indirect)); + assert!(!super::used_indirect_split(&b, &indirect)); } use super::*;