diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index 3bc4ad2d6..a2fa4d76d 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -206,7 +206,7 @@ pub fn submit(standin: usize, compute: bool, job: Job) { /// Lazy pipeline creation (`XCL_LAZY_PSO=0`: off): a title's pipelines with console shaders get /// their stand-in when made and are built when first used – the first bind of a graphics one -/// sends it to the workers, a compute one is built at its first bind. A retail game makes +/// sends it to the workers, a compute one is built ahead (`Preload`, `XCL_PRELOAD_COMPUTE`) or else at its first bind. A retail game makes /// ~51 000 pipelines in its first 30 s and uses few; each one built stays in the driver /// (~400 KB), and the process ran out of commit. pub fn lazy() -> bool { @@ -215,11 +215,20 @@ pub fn lazy() -> bool { } /// `submit`, sent to the workers only on its first `resolve` (`lazy`) – or now, at a low -/// priority, when an earlier run used a pipeline of these shaders (`key`, 0 = none; `used`). +/// priority, when an earlier run used a pipeline of these shaders (`key`, 0 = none; `used`), +/// and for every compute pipeline (after those: `Preload`; `XCL_PRELOAD_COMPUTE=0`: not). pub fn submit_lazy(standin: usize, compute: bool, job: Job, key: u64) { submit_as(standin, compute, job, false, key); } +/// Whether a compute pipeline no earlier run used is built ahead (`Preload`; +/// `XCL_PRELOAD_COMPUTE=0`: off, it is then built at its first bind). `XCL_PSO_PRELOAD=0` turns +/// off this too. +fn preload_compute() -> bool { + static C: OnceLock = OnceLock::new(); + *C.get_or_init(|| std::env::var("XCL_PRELOAD_COMPUTE").map_or(true, |v| v.trim() != "0")) +} + fn submit_as(standin: usize, compute: bool, job: Job, send: bool, key: u64) { let p = Arc::new(Pending { job: Mutex::new(Some(job)), @@ -241,9 +250,15 @@ fn submit_as(standin: usize, compute: bool, job: Job, send: bool, key: u64) { }); if send { let _ = queue().lock().unwrap().send(p); - } else if used::known(key) { + } else if used::known(key) || (compute && preload_compute()) { if let Some(q) = preload() { - q.list.lock().unwrap().push_back(p); + let mut g = q.list.lock().unwrap(); + if used::known(key) { + g.0.push_back(p); + } else { + g.1.push_back(p); + } + drop(g); q.cv.notify_one(); } } @@ -327,8 +342,18 @@ mod used { /// (`XCL_PSO_PRELOAD=0`: off). Apart from the ones a bind sends to (`queue`) and at the lowest /// priority: a pipeline that is bound is not held up by the ones made ahead (whoever takes /// `job` first builds it), and the title's own threads are not either. +/// +/// After those, every compute pipeline is built ahead too, as it is made: its first bind waits +/// for it on the title's thread (a skipped dispatch would corrupt what it computes), and the +/// driver takes seconds for a big one – on an NVIDIA GPU a retail title's binds waited 3 – 23 s +/// each, 16 of them within 10 s on five of its threads, and its hang watchdog ended it. Compute +/// pipelines are few (2 734 of the title's ~58 000), so building them all costs little; graphics +/// pipelines stay lazy. Each build is kept on disk (`real::driver_cache`) for later runs. +/// The driver keeps each one built (an AMD GPU under Wine: 7 – 10 GB more host memory), so +/// `XCL_PRELOAD_COMPUTE=0` turns off only this part (`preload_compute`). struct Preload { - list: Mutex>>, + /// (of shaders an earlier run used, the other compute pipelines): the first goes first + list: Mutex<(std::collections::VecDeque>, std::collections::VecDeque>)>, cv: Condvar, } @@ -356,7 +381,7 @@ fn preload() -> Option<&'static Preload> { let p = { let mut g = q.list.lock().unwrap(); loop { - if let Some(p) = g.pop_front() { + if let Some(p) = g.0.pop_front().or_else(|| g.1.pop_front()) { break p; } g = q.cv.wait(g).unwrap(); @@ -507,6 +532,72 @@ mod used_tests { released(known); released(unknown); } + + /// A compute pipeline is built without a bind even when no earlier run used it: its first + /// bind would otherwise build it on the title's thread. A graphics one made with it is not. + #[test] + fn a_compute_pipeline_is_built_ahead_of_its_first_bind() { + let (compute, graphics) = (0x7E57_7003usize, 0x7E57_7004usize); + submit_lazy(compute, true, Box::new(|| 0x4323), 0x5EED_0003); + submit_lazy(graphics, false, Box::new(|| 0x4324), 0x5EED_0004); + let start = std::time::Instant::now(); + while is_pending(compute) && start.elapsed() < std::time::Duration::from_secs(10) { + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert!(!is_pending(compute), "built by the workers that build ahead, with no bind"); + assert_eq!(resolve(compute), 0x4323); + assert!(is_pending(graphics), "graphics pipelines stay lazy"); + assert_eq!(resolve_wait(graphics), 0x4324); + released(compute); + released(graphics); + } + + /// `XCL_PRELOAD_COMPUTE=0` keeps a compute pipeline no earlier run used lazy: it is built at + /// its first bind. Unset, it is built ahead. Each case runs in its own process, which reads + /// the switch once. + #[test] + fn xcl_preload_compute_0_leaves_compute_pipelines_to_their_first_bind() { + const CHILD: &str = "XCL_TEST_PRELOAD_COMPUTE_CHILD"; + let Some(case) = std::env::var_os(CHILD) else { + for (case, switch) in [("ahead", None), ("lazy", Some("0"))] { + let mut child = std::process::Command::new(std::env::current_exe().unwrap()); + child + .args(["--exact", "hle::d3d12x::pso_async::used_tests::xcl_preload_compute_0_leaves_compute_pipelines_to_their_first_bind", "--nocapture"]) + .env(CHILD, case) + .env_remove("XCL_PSO_PRELOAD") + .env_remove("XCL_PRELOAD_COMPUTE"); + if let Some(v) = switch { + child.env("XCL_PRELOAD_COMPUTE", v); + } + let output = child.output().unwrap(); + assert!(output.status.success(), "{case}: {}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)); + assert!(String::from_utf8_lossy(&output.stdout).contains("1 passed"), "{case}: the child ran the test"); + } + return; + }; + use std::sync::atomic::{AtomicBool, Ordering}; + static BUILT: AtomicBool = AtomicBool::new(false); + let compute = 0x7E57_7005usize; + submit_lazy(compute, true, Box::new(|| { + BUILT.store(true, Ordering::SeqCst); + 0x4325 + }), 0x5EED_0005); + if case == "lazy" { + assert!(!preload_compute()); + assert!(preload().unwrap().list.lock().unwrap().1.is_empty(), "not sent to the workers that build ahead"); + std::thread::sleep(std::time::Duration::from_millis(200)); + assert!(!BUILT.load(Ordering::SeqCst) && is_pending(compute), "built at its first bind"); + } else { + let start = std::time::Instant::now(); + while is_pending(compute) && start.elapsed() < std::time::Duration::from_secs(10) { + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert!(!is_pending(compute), "built ahead with the switch unset"); + } + assert_eq!(resolve_wait(compute), 0x4325); + assert!(BUILT.load(Ordering::SeqCst)); + released(compute); + } } fn entry(pso: usize) -> Option> {