From ffd3681b2965296c98340e4b55413da5efabe22c Mon Sep 17 00:00:00 2001 From: TheGeogeo <57634077+TheGeogeo@users.noreply.github.com> Date: Sun, 29 Nov 2026 15:53:54 +0100 Subject: [PATCH 1/2] d3d12: a pipeline a frame waits for goes before those asked for earlier (D3D12-PIPELINE-WAIT-QUEUE) A draw whose host pipeline is not made queues it at the back of the requested list, and its frame then waits for it up to the frame's budget. On the first visit to the world about 1,700 pipelines were queued at once, 562 of them took over a second each, and with the launcher's 10 s budget every frame waited the whole budget for a pipeline a thousand others stood before, then dropped its draws anyway: 0.35 fps for three and a half minutes (RTX 3080, 8 workers). A new queue state, AWAITED, sits between REQUESTED and MAKING. When a frame starts waiting for a pipeline that is still in the queue, the entry moves to the front of its list (passes over the screen in theirs), after those awaited before it, so the next free worker makes it. It moves at most once, is never claimed twice, and WAITING does not count it again; makes already running are not interrupted. LEONIDA_PIPELINE_AWAIT_FIRST=0 keeps the order asked. Frames on a cold cache still wait up to the frame's budget: the waits now succeed, so a frame may spend its budget on several pipelines it then draws instead of one it drops. The budget setting bounds that, as before. --- d3d12/crates/leonida-gpu/src/pipeline/make.rs | 119 +++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) diff --git a/d3d12/crates/leonida-gpu/src/pipeline/make.rs b/d3d12/crates/leonida-gpu/src/pipeline/make.rs index fbe1577f4c..dfac0a092f 100644 --- a/d3d12/crates/leonida-gpu/src/pipeline/make.rs +++ b/d3d12/crates/leonida-gpu/src/pipeline/make.rs @@ -505,6 +505,9 @@ fn wait(p: &Pending, frame: u64) { if left.is_zero() { return; } + if await_first() { + promote(p); + } let started = Instant::now(); let (lock, done) = &DONE; let mut held = lock.lock().unwrap_or_else(|e| e.into_inner()); @@ -553,10 +556,35 @@ fn wait(p: &Pending, frame: u64) { ); } +/// Whether a pipeline a frame waits for goes before the others asked for +/// ([`AWAITED`]). A draw's pipeline went to the back of the queue: on the +/// first visit to the world about 1,700 were queued at once (562 of them took +/// over a second each), and with the launcher's 10 s budget every frame waited +/// its whole budget for one that a thousand others stood before, then dropped +/// its draws anyway: 0.35 fps for three and a half minutes (RTX 3080, 8 +/// workers). On by default; `LEONIDA_PIPELINE_AWAIT_FIRST=0`: in the order +/// asked, as before. +fn await_first() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("LEONIDA_PIPELINE_AWAIT_FIRST").is_none_or(|v| v != "0")) +} + +/// Moves `p`, asked for and not taken by a worker yet, to the front of its +/// list as a frame begins to wait for it. (A frame waiting again for one +/// already moved or being made does not take the lock.) +fn promote(p: &Pending) { + if p.queue_state.load(Ordering::Relaxed) == REQUESTED { + QUEUE.lock().unwrap_or_else(|e| e.into_inner()).promote(p); + } +} + const NOT_QUEUED: u8 = 0; const EARLY: u8 = 1; const REQUESTED: u8 = 2; -const MAKING: u8 = 3; +/// Requested, and a frame waits for it: at the front of its list, after +/// those awaited before it ([`Queue::promote`]). +const AWAITED: u8 = 3; +const MAKING: u8 = 4; /// How many workers may make pipelines at once that are known to take long /// and that no draw has asked for: a quarter of them. On a fresh cache (a @@ -624,6 +652,36 @@ impl Queue { Some(prev == NOT_QUEUED) } + /// Moves `p` from its place among those asked for to the front, after + /// those awaited before it; whether it moved (not if a worker has it, it + /// is not asked for, or it was moved already). Behind the earlier ones, + /// not before them: with the shared budget their frames' waits have run + /// out, but taking them in turn keeps any from waiting forever. It does + /// not stop a make a worker has begun: the next free worker takes it. + fn promote(&mut self, p: &Pending) -> bool { + if p.queue_state.load(Ordering::Relaxed) != REQUESTED { + return false; + } + let list = if p.screen && screens_first() { + &mut self.screen + } else { + &mut self.requested + }; + let Some(at) = list.iter().position(|q| std::ptr::eq(Arc::as_ptr(q), p)) else { + return false; + }; + let Some(q) = list.remove(at) else { + return false; + }; + q.queue_state.store(AWAITED, Ordering::Relaxed); + let front = list + .iter() + .position(|q| q.queue_state.load(Ordering::Relaxed) != AWAITED) + .unwrap_or(list.len()); + list.insert(front, q); + true + } + fn pop(&mut self, screens_only: bool, slow_limit: usize) -> Option> { // An early entry can have been promoted and claimed already. let unclaimed = |list: &mut VecDeque>| { @@ -896,4 +954,63 @@ mod tests { assert!(queue.pop(true, 1).is_none()); assert!(Arc::ptr_eq(&queue.pop(false, 1).unwrap(), &world)); } + + fn in_order(queue: &mut Queue, screens_only: bool, expected: &[&Arc]) { + let made: Vec<_> = std::iter::from_fn(|| queue.pop(screens_only, 1)).collect(); + assert_eq!(made.len(), expected.len()); + for (i, (got, want)) in made.iter().zip(expected).enumerate() { + assert!(Arc::ptr_eq(got, want), "pipeline {i} out of order"); + } + } + + /// A pipeline a frame waits for is made next, before those asked for + /// earlier; those awaited keep their order among themselves. + #[test] + fn an_awaited_pipeline_is_made_before_those_asked_earlier() { + let new = || Arc::new(Pending::new(Vec::new(), None)); + let (a, b, c, d, e) = (new(), new(), new(), new(), new()); + let mut queue = Queue::new(); + for p in [&a, &b, &c, &d] { + assert_eq!(queue.push_as(p, REQUESTED, false), Some(true)); + } + // c, then a, are waited for: they go first, c before a, once each. + assert!(queue.promote(&c)); + assert!(queue.promote(&a)); + assert!(!queue.promote(&a)); + // One a draw has not asked for, or that a worker has, stays where it is. + assert!(!queue.promote(&e)); + assert!(Arc::ptr_eq(&queue.pop(false, 1).unwrap(), &c)); + assert!(!queue.promote(&c)); + in_order(&mut queue, false, &[&a, &b, &d]); + } + + #[test] + fn an_awaited_early_pipeline_is_claimed_once() { + let new = || Arc::new(Pending::new(Vec::new(), None)); + let (early, asked) = (new(), new()); + let mut queue = Queue::new(); + assert_eq!(queue.push_as(&early, EARLY, false), Some(true)); + assert_eq!(queue.push_as(&asked, REQUESTED, false), Some(true)); + assert_eq!(queue.push_as(&early, REQUESTED, false), Some(false)); + assert!(queue.promote(&early)); + // Already past asked for: a draw asking again changes nothing. + assert_eq!(queue.push_as(&early, REQUESTED, false), None); + // (The early list's stale entry is passed over.) + in_order(&mut queue, false, &[&early, &asked]); + } + + #[test] + fn an_awaited_pass_over_the_screen_goes_first_among_them() { + let pass = || { + let mut p = Pending::new(Vec::new(), None); + p.screen = true; + Arc::new(p) + }; + let (first, second) = (pass(), pass()); + let mut queue = Queue::new(); + assert_eq!(queue.push(&first, REQUESTED), Some(true)); + assert_eq!(queue.push(&second, REQUESTED), Some(true)); + assert!(queue.promote(&second)); + in_order(&mut queue, true, &[&second, &first]); + } } From 61c5b18d85631c47129bbc761b707ecb829f0e7c Mon Sep 17 00:00:00 2001 From: TheGeogeo <57634077+TheGeogeo@users.noreply.github.com> Date: Mon, 30 Nov 2026 04:14:48 +0100 Subject: [PATCH 2/2] d3d12: a frame's pipeline wait counts the queue lock its promotion waits for (review) wait() started its timer after promote() took the queue's lock, so time spent waiting for that lock came on top of the frame's budget: in a synthetic contention test (queue held 240 ms, 80 ms budget) a frame waited 320 ms. The timer now starts before the promotion, so the lock delay is part of the budget (240 ms in the same test, the lock holder's time itself). Reported with the test by merzleckeier. --- d3d12/crates/leonida-gpu/src/pipeline/make.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/d3d12/crates/leonida-gpu/src/pipeline/make.rs b/d3d12/crates/leonida-gpu/src/pipeline/make.rs index dfac0a092f..a475ffd9b3 100644 --- a/d3d12/crates/leonida-gpu/src/pipeline/make.rs +++ b/d3d12/crates/leonida-gpu/src/pipeline/make.rs @@ -505,10 +505,12 @@ fn wait(p: &Pending, frame: u64) { if left.is_zero() { return; } + // Before the promotion: the queue's lock it may wait for is part of this + // frame's wait, not on top of it. + let started = Instant::now(); if await_first() { promote(p); } - let started = Instant::now(); let (lock, done) = &DONE; let mut held = lock.lock().unwrap_or_else(|e| e.into_inner()); while p.made.get().is_none() {