From 5fab11264ee651312d063a465f752f53b6bd6be8 Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Tue, 24 Nov 2026 08:42:33 +0100 Subject: [PATCH 1/2] D3D12.X: XCL_PSO_WAIT_MS bounds how long a bind waits for a pipeline being built A retail game creates compute programs on its own threads and binds them at once. The driver takes up to 30 s for the biggest translated ones, and the bind waited for the build (`resolve_wait`). Once its world starts loading, the title met programs not built yet, its main thread was held, and its watchdog ended it 30 s later ("GPU Loc Sig", issue #505). With `XCL_PSO_WAIT_MS=` a bind waits at most that long. The build then goes on on the workers, not on the title's thread. Until it is done, the pipeline is late: its binds get the stand-in, and their dispatches are skipped, as for a pipeline without a translation. Unset, a bind waits for the build as before. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SKtJseucqXR6iuAE6HBtiA --- crates/xcl/src/hle/d3d12x/pso_async.rs | 75 ++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index b303cf55e4..ab910ae017 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -27,6 +27,8 @@ struct Pending { used: std::sync::atomic::AtomicBool, /// the stand-in was released before the real pipeline was built orphan: std::sync::atomic::AtomicBool, + /// a bind waited for it longer than `wait_budget`: binds go on without it until it is built + late: std::sync::atomic::AtomicBool, } impl Pending { @@ -134,6 +136,7 @@ fn submit_as(standin: usize, compute: bool, job: Job, send: bool, key: u64) { key, used: Default::default(), orphan: Default::default(), + late: Default::default(), }); PENDING.lock().unwrap().get_or_insert_with(HashMap::new).insert(standin, p.clone()); static ONCE: std::sync::Once = std::sync::Once::new(); @@ -288,6 +291,30 @@ fn preload() -> Option<&'static Preload> { mod used_tests { use super::*; + /// A bind waits for a pipeline still being built no longer than its budget and goes on with + /// the stand-in; later binds do not wait for it again; once built it is the real one. + #[test] + fn a_bind_waits_for_a_slow_pipeline_no_longer_than_its_budget() { + let standin = 0x7E57_B001usize; + submit(standin, true, Box::new(|| { + std::thread::sleep(std::time::Duration::from_millis(600)); + 0x5151 + })); + let budget = Some(std::time::Duration::from_millis(30)); + let t = std::time::Instant::now(); + assert_eq!(resolve_wait_for(standin, budget), standin, "not built within the budget: the stand-in"); + assert!(t.elapsed() < std::time::Duration::from_millis(400), "waited {:?}", t.elapsed()); + let t = std::time::Instant::now(); + assert_eq!(resolve_wait_for(standin, budget), standin); + assert!(t.elapsed() < std::time::Duration::from_millis(20), "a late pipeline is not waited for again ({:?})", t.elapsed()); + let start = std::time::Instant::now(); + while is_pending(standin) && start.elapsed() < std::time::Duration::from_secs(10) { + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert_eq!(resolve_wait_for(standin, budget), 0x5151, "built by a worker"); + let _ = released(standin); + } + #[test] fn the_keys_of_a_file_are_read_and_other_lines_passed_over() { let keys = used::parse("00000000000000ff\n\nnot a key\n 1a2b \n0\n"); @@ -342,8 +369,16 @@ pub fn resolve(pso: usize) -> usize { /// `resolve`, waiting for a pipeline still being built – built here if no worker has started /// it yet (a queue of a title's hundreds of pipelines must not hold up the one it binds now). pub fn resolve_wait(pso: usize) -> usize { + resolve_wait_for(pso, wait_budget()) +} + +/// `resolve_wait`, waiting at most `budget` (None: until the pipeline is built). +fn resolve_wait_for(pso: usize, budget: Option) -> usize { let Some(p) = entry(pso) else { return pso }; p.note_use(); + if let Some(budget) = budget { + return resolve_within(&p, pso, budget); + } let job = p.job.lock().unwrap().take(); if let Some(job) = job { complete(&p, job()); @@ -358,6 +393,46 @@ pub fn resolve_wait(pso: usize) -> usize { } } +/// How long a bind waits for a pipeline still being built (`XCL_PSO_WAIT_MS`; unset: until 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 { + static B: OnceLock> = OnceLock::new(); + *B.get_or_init(|| std::env::var("XCL_PSO_WAIT_MS").ok().and_then(|v| v.trim().parse::().ok()).map(std::time::Duration::from_millis)) +} + +/// `resolve_wait` for at most `budget`: the build goes to the workers (not this thread), and a +/// pipeline not built by then is `late` – it and later binds of it get the stand-in (their +/// dispatches are skipped) until a worker has built it. +fn resolve_within(p: &Arc, pso: usize, budget: std::time::Duration) -> usize { + let built = |p: &Pending| match p.real.load(Ordering::Acquire) { + 0 => pso, + r => r, + }; + if *p.done.lock().unwrap() { + return built(p); + } + if !p.sent.swap(true, Ordering::AcqRel) { + let _ = queue().lock().unwrap().send(p.clone()); + } + if p.late.load(Ordering::Acquire) { + return pso; + } + let (done, _) = p.cv.wait_timeout_while(p.done.lock().unwrap(), budget, |d| !*d).unwrap(); + if *done { + return built(p); + } + drop(done); + p.late.store(true, Ordering::Release); + static N: AtomicUsize = AtomicUsize::new(0); + let n = N.fetch_add(1, Ordering::Relaxed); + if n < 16 || n.is_power_of_two() { + crate::xlog_warn!("D3D12.X: a pipeline still being built after {} ms: its binds go on without it until it is built ({} so far)", budget.as_millis(), n + 1); + } + pso +} + /// For binding: compute pipelines are waited for, graphics ones are not. pub fn resolve_for_bind(pso: usize) -> usize { match entry(pso) { From 8cada26f71aa9616843d3cd3ee1dd46ef8662404 Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Tue, 24 Nov 2026 09:11:55 +0100 Subject: [PATCH 2/2] D3D12.X: XCL_PSO_WAIT_MS=0 waits until the pipeline is built, as unset Reported in review: another build gives 0 that meaning, and 0 here skipped every dispatch of a pipeline still being built. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SKtJseucqXR6iuAE6HBtiA --- crates/xcl/src/hle/d3d12x/pso_async.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index ab910ae017..33a85cf128 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -393,13 +393,13 @@ fn resolve_wait_for(pso: usize, budget: Option) -> usize { } } -/// How long a bind waits for a pipeline still being built (`XCL_PSO_WAIT_MS`; unset: until it -/// is built). A retail game builds compute programs on its own threads: the driver takes up to +/// How long a bind waits for a pipeline still being built (`XCL_PSO_WAIT_MS`; unset or 0: until +/// 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 { static B: OnceLock> = OnceLock::new(); - *B.get_or_init(|| std::env::var("XCL_PSO_WAIT_MS").ok().and_then(|v| v.trim().parse::().ok()).map(std::time::Duration::from_millis)) + *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)) } /// `resolve_wait` for at most `budget`: the build goes to the workers (not this thread), and a