From 9dd8715e440251601ad7a65469e6e9106988e547 Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:52:01 -0500 Subject: [PATCH] D3D12.X: build a split program's whole form as a background pipeline A split compute program keeps its whole form for what the pieces cannot run: an ExecuteIndirect, and a dispatch with no allocator to keep its scratch, in a bundle, or whose scratch allocation fails. That form was built in `Chain::whole` on the calling thread, the title's, the first time it was needed, ungated (a title thread's huge compile skips the huge-build slots) and outside `XCL_PSO_WAIT_MS`. A retail title's ExecuteIndirect of split programs then held its thread for one 20-35 s whole build after another, with no present (reported on #2080). The whole form is now a lazy pipeline in `pso_async`, behind the chain's address as a stand-in's: it is sent to the PSO workers on first need, where the huge-build slots and the driver blob cache apply as before (the build itself, `compute_container` with the chain's root signature and its `.chain.pso` blob, is unchanged). Its callers wait for it as a bind waits for an unsplit pipeline: with `XCL_PSO_WAIT_MS` up to the budget, then the dispatch is skipped and counted until a worker has built it; without a budget, until it is built, built on the calling thread if no worker has started it, as before. `XCL_PSO_IMAGE_SKIP` spares it as it would spare the unsplit program. `forget` releases it, or marks it orphaned while it is being built. The `compute split:` log line counts the whole forms asked for and built and the dispatches skipped while one was being built. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/bind.rs | 15 +- crates/xcl/src/hle/d3d12x/console_list.rs | 7 +- crates/xcl/src/hle/d3d12x/pso_async.rs | 4 +- crates/xcl/src/hle/d3d12x/real.rs | 4 +- .../xcl/src/hle/d3d12x/real/dispatch_chain.rs | 202 +++++++++++++++--- 5 files changed, 198 insertions(+), 34 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index fad934addc..733c098648 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -315,9 +315,14 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us match bind_abi(this, list, pso, gfx) { Ok(()) => { if compute { + use super::real::dispatch_chain::Whole; match super::real::dispatch_chain::whole(pso) { - Ok(Some(whole)) => list.SetPipelineState(&whole), - Ok(None) => {} + Ok(Whole::Built(whole)) => list.SetPipelineState(&whole), + Ok(Whole::NotSplit) => {} + Ok(Whole::Building) => { + count(DISPATCHES_SKIPPED); + return; + } Err(e) => { crate::xlog_warn!("D3D12.X: ExecuteIndirect whole compute PSO: {e}"); count(DISPATCHES_SKIPPED); @@ -860,7 +865,9 @@ pub(super) unsafe extern "system" fn dispatch(this: *mut c_void, x: u32, y: u32, return; } match translated_dispatch(this, list, pso, x, y, z) { - Ok(()) => count(DISPATCHES), + Ok(true) => count(DISPATCHES), + // the whole form of a split program, still being built (`dispatch_chain::dispatch`) + Ok(false) => count(DISPATCHES_SKIPPED), Err(e) => { count(DISPATCHES_SKIPPED); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); @@ -976,7 +983,7 @@ pub(super) unsafe extern "system" fn allocate_upload_memory(this: *mut c_void, s } } -unsafe fn translated_dispatch(this: *mut c_void, list: &ID3D12GraphicsCommandList, pso: usize, x: u32, y: u32, z: u32) -> Result<(), String> { +unsafe fn translated_dispatch(this: *mut c_void, list: &ID3D12GraphicsCommandList, pso: usize, x: u32, y: u32, z: u32) -> Result { if super::present::trace_frame() { trace_fullscreen(this, pso, [x, y, z]); } diff --git a/crates/xcl/src/hle/d3d12x/console_list.rs b/crates/xcl/src/hle/d3d12x/console_list.rs index 71206c010a..6005cea5ed 100644 --- a/crates/xcl/src/hle/d3d12x/console_list.rs +++ b/crates/xcl/src/hle/d3d12x/console_list.rs @@ -855,8 +855,11 @@ pub unsafe fn close(obj: *mut c_void) -> Result { let raw = real as *mut c_void; let pc = ID3D12PipelineState::from_raw_borrowed(&raw).ok_or("PSO")?; list.SetPipelineState(pc); - super::real::dispatch_chain::dispatch(&list, real, st.allocator, [x, y, z])?; - out.dispatches += 1; + if super::real::dispatch_chain::dispatch(&list, real, st.allocator, [x, y, z])? { + out.dispatches += 1; + } else { + out.skipped_dispatches += 1; + } } _ => out.skipped_dispatches += 1, } diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index 9db4ae2eb9..8c3f62e1bf 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -857,7 +857,7 @@ pub fn resolve_wait(pso: usize) -> usize { } /// `resolve_wait`, waiting at most `budget` (None: until the pipeline is built). -fn resolve_wait_for(pso: usize, budget: Option) -> usize { +pub(super) fn resolve_wait_for(pso: usize, budget: Option) -> usize { let Some(p) = entry(pso) else { return pso }; p.note_use(); if p.spared.load(Ordering::Acquire) { @@ -887,7 +887,7 @@ fn resolve_wait_for(pso: usize, budget: Option) -> usize { /// it is built). A retail game builds compute programs on its own threads: the driver takes up to /// 30 s for the biggest translated ones, and the title's watchdog ends it when its main thread /// has been held 30 s. -fn wait_budget() -> Option { +pub(super) fn wait_budget() -> Option { static B: OnceLock> = OnceLock::new(); *B.get_or_init(|| std::env::var("XCL_PSO_WAIT_MS").ok().and_then(|v| v.trim().parse::().ok()).filter(|&ms| ms > 0).map(std::time::Duration::from_millis)) } diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 574f582d86..811b627390 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4320,7 +4320,9 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const } let record = crate::gpu::shader::fnv64(cs); if let Some(plan) = compiled.dispatch_chain { - if let Some(rs) = dispatch_chain::build(this, f, d, riid, ppv, record, &dxil, &compiled.samplers, plan) { + // (its whole form is waited for as the unsplit pipeline would be: `go_background`) + let spared = super::pso_async::image_skip() && crate::gpu::effects::big_and_image_only(cs); + if let Some(rs) = dispatch_chain::build(this, f, d, riid, ppv, record, &dxil, &compiled.samplers, plan, spared) { LAST_COMPUTE_RS.with(|c| c.set(rs)); note_pso_rs(*ppv, rs as *mut c_void); super::dred::label(*ppv as usize, &[("cs", Some(cs))]); diff --git a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs index ddce2bfa9a..2ec9538cea 100644 --- a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs +++ b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs @@ -1,12 +1,16 @@ //! 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::pso_async; use crate::gpu::{dxil::kernel::DispatchChain, recomp::pso}; use std::sync::{Arc, LazyLock, OnceLock}; static PROGRAMS: AtomicU64 = AtomicU64::new(0); static PIECES: AtomicU64 = AtomicU64::new(0); static DISPATCHES: AtomicU64 = AtomicU64::new(0); +static WHOLES_ASKED: AtomicU64 = AtomicU64::new(0); +static WHOLES_BUILT: AtomicU64 = AtomicU64::new(0); +static WHOLE_SKIPS: AtomicU64 = AtomicU64::new(0); /// slabs dispatched (`pso::dispatch_slabs`) static SLABS: AtomicU64 = AtomicU64::new(0); /// scratch alive now, the most there was, and the most a dispatch would have needed at once (bytes) @@ -16,9 +20,10 @@ static WANTED: AtomicU64 = AtomicU64::new(0); 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)", + crate::xlog_info!("compute split: {} programs split, {} pieces built, {} chained dispatches issued in {} slabs; scratch {} MiB (most {}, largest dispatch {} in one slab); whole form: {} asked for, {} built, {} dispatches skipped while it was being built", PROGRAMS.load(Ordering::Relaxed), PIECES.load(Ordering::Relaxed), DISPATCHES.load(Ordering::Relaxed), SLABS.load(Ordering::Relaxed), - mib(&LIVE), mib(&PEAK), mib(&WANTED)); + mib(&LIVE), mib(&PEAK), mib(&WANTED), + WHOLES_ASKED.load(Ordering::Relaxed), WHOLES_BUILT.load(Ordering::Relaxed), WHOLE_SKIPS.load(Ordering::Relaxed)); } /// The scratch a dispatch uses at once, `XCL_SPLIT_SCRATCH_MB` (256): more groups than fit run in @@ -48,31 +53,70 @@ impl PipelineBuilder { } } +/// The whole program's pipeline, for what the pieces cannot run (`Chain::whole`). +pub(in crate::hle::d3d12x) enum Whole { + /// not a split program: its own pipeline runs it + NotSplit, + Built(ID3D12PipelineState), + /// still being built and not waited for (`XCL_PSO_WAIT_MS`): the dispatch is skipped, as + /// one of an unsplit pipeline still being built + Building, +} + struct Chain { builder: Arc, tail: Vec, words: u32, - whole_code: Vec, - whole: OnceLock, - build_whole: Mutex<()>, + /// the whole form was asked for (`WHOLES_ASKED`) + asked: std::sync::atomic::AtomicBool, } impl Chain { - unsafe fn whole(&self) -> Result { - if let Some(p) = self.whole.get() { return Ok(p.clone()); } - let _guard = self.build_whole.lock().unwrap(); - if let Some(p) = self.whole.get() { return Ok(p.clone()); } - let p = self.builder.pipeline(&self.whole_code, &pc_iid(Kind::PipelineState)).ok_or("whole compute pipeline")?; - let _ = self.whole.set(p.clone()); - Ok(p) + /// The whole form is built as a pipeline behind a stand-in is (`pso_async`), with this + /// address as its stand-in's: sent to the workers when first needed, released with the + /// chain (`forget`). + fn key(&self) -> usize { + self as *const Chain as usize + } + + /// The whole form, waited for as a bind waits for an unsplit pipeline (`budget`: as + /// `pso_async::resolve_wait`'s). + fn whole(&self, budget: Option) -> Result { + if !self.asked.swap(true, Ordering::Relaxed) { + WHOLES_ASKED.fetch_add(1, Ordering::Relaxed); + } + let key = self.key(); + let mut p = pso_async::resolve_wait_for(key, budget); + if p == key && pso_async::is_pending(key) { + WHOLE_SKIPS.fetch_add(1, Ordering::Relaxed); + return Ok(Whole::Building); + } + if p == key { + // (built since the wait gave up on it, or failed) + p = pso_async::resolve(key); + } + if p == key { + return Err("whole compute pipeline".into()); + } + unsafe { Ok(Whole::Built(ID3D12PipelineState::from_raw_borrowed(&(p as *mut c_void)).ok_or("whole compute pipeline")?.clone())) } + } + + /// Hand the whole form's build to `pso_async`, not sent before it is needed (`spared`: + /// `pso_async::image_only`). + fn queue_whole(&self, job: pso_async::Job, spared: bool) { + pso_async::submit_lazy(self.key(), true, job, 0); + if spared { + pso_async::image_only(self.key()); + } } } static CHAINS: LazyLock>>> = LazyLock::new(Default::default); +/// `spared`: binds need not wait for the whole form (`pso_async::image_only`). pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUTE_PIPELINE_STATE_DESC, iid: *const GUID, result: *mut *mut c_void, record: u64, whole: &[u8], samplers: &[pso::StaticSampler], - plan: Arc) -> Option { + plan: Arc, spared: bool) -> Option { if result.is_null() || iid.is_null() || plan.pieces.len() < 2 { return None; } let dev = ID3D12Device::from_raw_borrowed(&this)?; let signature = pso::dispatch_chain_root_signature(dev, samplers).ok()?; @@ -83,15 +127,21 @@ pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUT let plan = plan.clone(); let builder = builder.clone(); let iid = if i == 0 { *iid } else { pc_iid(Kind::PipelineState) }; - Box::new(move || builder.pipeline(&plan.pieces[i], &iid).map_or(0, |p| p.into_raw() as usize)) as super::super::pso_async::Job + Box::new(move || builder.pipeline(&plan.pieces[i], &iid).map_or(0, |p| p.into_raw() as usize)) as pso_async::Job }).collect(); // Take ownership of every success, including when a different piece failed. - let built: Vec<_> = super::super::pso_async::build_pieces(jobs).into_iter() + let built: Vec<_> = pso_async::build_pieces(jobs).into_iter() .map(|p| (p != 0).then(|| ID3D12PipelineState::from_raw(p as *mut c_void))).collect(); let mut pieces = built.into_iter().collect::>>()?.into_iter(); let first = pieces.next()?; - let chain = Arc::new(Chain { builder, tail: pieces.collect(), words: plan.dwords_per_group, - whole_code: whole.to_vec(), whole: OnceLock::new(), build_whole: Mutex::new(()) }); + let whole_code = whole.to_vec(); + let whole_builder = builder.clone(); + let chain = Arc::new(Chain { builder, tail: pieces.collect(), words: plan.dwords_per_group, asked: Default::default() }); + chain.queue_whole(Box::new(move || { + let p = whole_builder.pipeline(&whole_code, &pc_iid(Kind::PipelineState)).map_or(0, |p| p.into_raw() as usize); + if p != 0 { WHOLES_BUILT.fetch_add(1, Ordering::Relaxed); } + p + }), spared); PROGRAMS.fetch_add(1, Ordering::Relaxed); PIECES.fetch_add(plan.pieces.len() as u64, Ordering::Relaxed); *result = first.into_raw(); @@ -101,12 +151,15 @@ pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUT pub(super) fn forget(pso: usize) { let gone = CHAINS.lock().unwrap().remove(&pso); + // the whole form now, or when its build ends (`pso_async::released`) + if let Some(c) = &gone { pso_async::release(pso_async::released(c.key())); } drop(gone); // releasing a tail PSO also goes through forget_pso } -pub(in crate::hle::d3d12x) unsafe fn whole(pso: usize) -> Result, String> { +/// The whole form of `pso` for an ExecuteIndirect (`Chain::whole`). +pub(in crate::hle::d3d12x) fn whole(pso: usize) -> Result { let chain = CHAINS.lock().unwrap().get(&pso).cloned(); - chain.map(|c| c.whole()).transpose() + chain.map_or(Ok(Whole::NotSplit), |c| c.whole(pso_async::wait_budget())) } struct Scratch { @@ -194,11 +247,12 @@ unsafe fn scratch_buffer(dev: &ID3D12Device, bytes: u64) -> Option Result<(), String> { +/// Ok(false): skipped, the whole form it needs is still being built (`Whole::Building`). +pub(in crate::hle::d3d12x) unsafe fn dispatch(list: &ID3D12GraphicsCommandList, pso: usize, allocator: usize, grid: [u32; 3]) -> Result { let [x, y, z] = grid; let chain = CHAINS.lock().unwrap().get(&pso).cloned(); - let Some(chain) = chain else { list.Dispatch(x, y, z); return Ok(()) }; - if grid.contains(&0) { return Ok(()); } + let Some(chain) = chain else { list.Dispatch(x, y, z); return Ok(true) }; + if grid.contains(&0) { return Ok(true); } // A title Reset can switch to a different PC allocator while this // recording is still submitted on the old one. Retain by backing. let allocator = if super::super::alloc_pool::enabled() { @@ -212,9 +266,10 @@ pub(in crate::hle::d3d12x) unsafe fn dispatch(list: &ID3D12GraphicsCommandList, let most = scratch.as_ref().map_or(1, |(s, _)| (s.bytes / group).max(1)); let slabs = pso::dispatch_slabs(grid, most); let Some((scratch, used)) = scratch else { - list.SetPipelineState(&chain.whole()?); + let Whole::Built(whole) = chain.whole(pso_async::wait_budget())? else { return Ok(false) }; + list.SetPipelineState(&whole); list.Dispatch(x, y, z); - return Ok(()); + return Ok(true); }; let first = ID3D12PipelineState::from_raw_borrowed(&(pso as *mut c_void)).ok_or("first compute piece")?.clone(); let held = Arc::new(Recording { scratch, first, chain }); @@ -222,7 +277,7 @@ pub(in crate::hle::d3d12x) unsafe fn dispatch(list: &ID3D12GraphicsCommandList, DISPATCHES.fetch_add(1, Ordering::Relaxed); SLABS.fetch_add(slabs.len() as u64, Ordering::Relaxed); pso::record_dispatch_chain(list, grid, most, &held.scratch.buffer, used, std::iter::once(&held.first).chain(&held.chain.tail)); - Ok(()) + Ok(true) } #[cfg(test)] @@ -250,3 +305,100 @@ mod tests { assert_eq!(scratch_bytes(9 * MB, 9 * MB, 4 * MB, Some(MB), 400 * MB, 384 * MB), (9 * MB, true)); } } + +#[cfg(test)] +mod whole_tests { + use super::*; + use std::sync::mpsc; + use std::time::{Duration, Instant}; + + unsafe extern "system" fn no_create(_: *mut c_void, _: *const c_void, _: *const GUID, _: *mut *mut c_void) -> HRESULT { + E_INVALIDARG + } + + /// The references held on `pso` (by calling AddRef and Release on it). + unsafe fn refs(pso: &ID3D12PipelineState) -> u32 { + let raw = pso.as_raw(); + let vt = *(raw as *const *const usize); + let add_ref: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*vt.add(1)); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*vt.add(2)); + add_ref(raw); + release(raw) + } + + /// A split program's whole form asked for while it is still being built (an ExecuteIndirect + /// of it, `bind::execute_indirect`) is not built on the asking thread and not waited for + /// past the bind's budget: the dispatch is skipped, a worker builds it, a later ask gets + /// it, and `forget` releases it. + #[test] + fn a_whole_form_still_being_built_is_not_waited_for_past_the_budget() { + unsafe { + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + let warp = CreateDXGIFactory1::().and_then(|f| f.EnumWarpAdapter::()); + if let Ok(warp) = warp { + let _ = D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_11_0, &mut dev); + } + } + let Some(dev) = dev else { return crate::gpu::recomp::runner::skip_test("no D3D12 device") }; + let mut ser = None; + D3D12SerializeRootSignature(&D3D12_ROOT_SIGNATURE_DESC::default(), D3D_ROOT_SIGNATURE_VERSION_1, &mut ser, None).expect("serialize RS"); + let ser = ser.unwrap(); + let rs: ID3D12RootSignature = dev + .CreateRootSignature(0, std::slice::from_raw_parts(ser.GetBufferPointer() as *const u8, ser.GetBufferSize())) + .expect("CreateRootSignature"); + let cs = dummy_cs_bytecode(); + let mut desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: std::mem::ManuallyDrop::new(Some(rs.clone())), + CS: D3D12_SHADER_BYTECODE { pShaderBytecode: cs.as_ptr() as *const c_void, BytecodeLength: cs.len() }, + ..Default::default() + }; + let whole: ID3D12PipelineState = dev.CreateComputePipelineState(&desc).expect("CreateComputePipelineState"); + std::mem::ManuallyDrop::drop(&mut desc.pRootSignature); + + let builder = Arc::new(PipelineBuilder { device: dev.clone(), create: no_create, signature: rs, flags: Default::default(), node: 0, record: 0 }); + let chain = Arc::new(Chain { builder, tail: Vec::new(), words: 1, asked: Default::default() }); + let (started_tx, started) = mpsc::channel(); + let (go, finish) = mpsc::channel::<()>(); + let built = whole.clone().into_raw() as usize; + chain.queue_whole(Box::new(move || { + let _ = started_tx.send(std::thread::current().name().map(str::to_owned)); + let _ = finish.recv(); + built + }), false); + let first = 0x7E57_C4A1usize; + CHAINS.lock().unwrap().insert(first, chain.clone()); + assert!(pso_async::is_pending(chain.key()), "queued, not built before it is needed"); + + let budget = Some(Duration::from_millis(30)); + let t = Instant::now(); + let asked = chain.whole(budget); + if asked.is_err() || t.elapsed() >= Duration::from_millis(400) { + let _ = go.send(()); + } + assert!(matches!(asked, Ok(Whole::Building)), "still being built: the dispatch is skipped"); + assert!(t.elapsed() < Duration::from_millis(400), "waited {:?}", t.elapsed()); + let builder_thread = started.recv_timeout(Duration::from_secs(10)).expect("a worker builds the whole form"); + assert!(builder_thread.as_deref().is_some_and(|n| n.starts_with("xcl-pso")), "built on {builder_thread:?}, not on a PSO worker"); + let t = Instant::now(); + assert!(matches!(chain.whole(budget), Ok(Whole::Building))); + assert!(t.elapsed() < Duration::from_millis(20), "a late whole form is not waited for again ({:?})", t.elapsed()); + + go.send(()).unwrap(); + let t = Instant::now(); + while pso_async::is_pending(chain.key()) && t.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(5)); + } + match chain.whole(budget) { + Ok(Whole::Built(p)) => assert_eq!(p.as_raw(), whole.as_raw()), + _ => panic!("the whole form was built, but not handed out"), + } + assert!(matches!(super::whole(first), Ok(Whole::Built(_))), "built: an ExecuteIndirect gets it"); + assert!(matches!(super::whole(0x7E57_C4A3), Ok(Whole::NotSplit))); + assert_eq!(refs(&whole), 2, "this test's and the build's"); + forget(first); + assert_eq!(refs(&whole), 1, "forget released the whole form"); + assert_eq!(pso_async::resolve(chain.key()), chain.key(), "forgotten by pso_async too"); + } + } +}