From 7c69bc8b191f4db3dc967872c3784253c155b2ff Mon Sep 17 00:00:00 2001 From: enlight3d <19186686+enlight3d@users.noreply.github.com> Date: Tue, 1 Dec 2026 21:46:50 +0100 Subject: [PATCH] d3d12: the threads of a frame share its pipeline wait budget and none is left with nothing (D3D12-COLD-START-SKIPS) The shared frame budget was a reservation: the first recording thread to wait in a frame took all that was left of it, and every other thread of that frame (and any thread still on an older frame) got nothing, so its draw went out without a wait and was dropped as not made. On first starts (no pipeline book) 400 to 3,600 draws, dispatches and indirect executions a frame were dropped at the arrival in the world, and the character's head, drawn by a pass the title runs once, stayed missing for the run. - The frame's waits are counted once over the threads that wait at the same time: what passed while at least one thread waited. A thread that begins to wait while others do is left what is left at that count, so waits that overlap still cost the frame its budget once; the time a thread records between two waits is not counted; a thread on an older frame joins the current frame's waits and opens no budget of its own (#2601). On by default; LEONIDA_PIPELINE_WAIT_DEADLINE=0 reserves the frame's budget as before (no effect with LEONIDA_PIPELINE_WAIT_SHARED=0); LEONIDA_AB can turn it while the title runs. - A pipeline's own budget (LEONIDA_PIPELINE_WAIT_ONCE) is charged the same way, the real time once, not the threads' times added up, whatever the sharing of the frame's budget (LEONIDA_PIPELINE_WAIT_ONCE=0 turns that budget off). - The queue a wait moves its pipeline forward in and the budget a pipeline's waits count against are passed in, so that tests cover them; one line tells the rules in use at the first wait. AI-assisted: Claude Code (Claude Opus 5.5) Co-Authored-By: Claude Opus 5.5 --- d3d12/crates/leonida-gpu/src/pipeline/make.rs | 577 +++++++++++++++--- d3d12/docs/switches.md | 9 +- 2 files changed, 494 insertions(+), 92 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/pipeline/make.rs b/d3d12/crates/leonida-gpu/src/pipeline/make.rs index fac23dab8a..4b823b252e 100644 --- a/d3d12/crates/leonida-gpu/src/pipeline/make.rs +++ b/d3d12/crates/leonida-gpu/src/pipeline/make.rs @@ -10,7 +10,9 @@ //! A first use queues the pipeline and waits for it within a budget per //! frame, so the cheap ones (menus, one-off passes such as a colour-grading //! bake) are there at once as before; past the budget a draw or dispatch -//! whose pipeline is not ready is dropped, as an untranslated one is. +//! whose pipeline is not ready is dropped, as an untranslated one is. The +//! budget is one for all the title's recording threads, and waits that +//! overlap count once ([`deadline_on`]). //! //! The default budget is half a second per title frame. A pass the title //! runs only once and whose pipeline takes longer is lost. Passes over the @@ -48,9 +50,9 @@ pub(crate) struct Pending { scene_input: bool, /// Why this pipeline is intentionally skipped (e.g. RT resolve without BVH). left_out: Option<&'static str>, - /// LOCAL: how long draws have waited for it so far, in microseconds - /// ([`wait_once`]). - waited_us: AtomicU64, + /// LOCAL: how long draws have waited for it so far, counted once over + /// the threads that waited for it together ([`wait_once`]). + waits: Mutex, /// LOCAL (fxceless a35c557393): the formats its state declares (zeros /// without a graphics state), and the same shaders and state for other /// formats bound at a draw ([`for_formats`]): a chain, one link per set. @@ -183,7 +185,7 @@ impl Pending { made: OnceLock::new(), key: key.finalize().to_hex()[..32].to_owned(), used: AtomicBool::new(false), - waited_us: AtomicU64::new(0), + waits: Mutex::new(Waited::new()), } } @@ -441,12 +443,41 @@ pub(crate) fn early(p: &Arc) { /// frame with nothing counted yet and waited the whole half second, 2.5 s /// of waiting in every frame in which pipelines were being made (runs /// 34-49: "2504 ms waiting for pipelines" in each such frame). -/// On by default; `LEONIDA_PIPELINE_WAIT_SHARED=0` counts as before. +/// On by default; `LEONIDA_PIPELINE_WAIT_SHARED=0` counts the frame's budget +/// as before (a pipeline's own budget is counted in real time in every mode, +/// [`wait_once`]). How the threads share it: [`deadline_on`]. fn shared_budget() -> bool { static ON: OnceLock = OnceLock::new(); *ON.get_or_init(|| std::env::var_os("LEONIDA_PIPELINE_WAIT_SHARED").is_none_or(|v| v != "0")) } +/// Whether the shared budget counts the frame's waits once over the threads +/// that wait at the same time ([`Waited`]): what passed while at least one +/// recording thread waited. A thread that begins to wait while others do is +/// left what is left of the budget at that count, so that waits that overlap +/// end together and cost the frame its budget once (the reason for +/// [`shared_budget`]); the time a thread records between two waits is not +/// counted. Before, the budget was reserved: the first thread to wait in a +/// frame took all that was left of it, and every other thread recording in +/// that frame, as well as a thread still on an older frame, was left +/// nothing, so that its draw went out without a wait and was dropped as not +/// made. On first starts (no pipeline book) 400 to 3,600 draws, dispatches +/// and indirect executions a frame were dropped as not made at the arrival +/// in the world (RTX 3090, NVIDIA 621.40; reproduced on an RTX 4070 Laptop), +/// hundreds a frame even with a budget of ten minutes, and the character's +/// head, drawn by a pass the title runs once, was missing for the whole run +/// (8 runs of 8). With the frame's waits ending at one deadline (an earlier +/// form of this count, from the frame's first wait) the head was drawn on 2 +/// first starts of 2, and missing on the one with this switch off (RTX +/// 3090). Only with the shared budget: no effect with +/// `LEONIDA_PIPELINE_WAIT_SHARED=0`. On by default; +/// `LEONIDA_PIPELINE_WAIT_DEADLINE=0` reserves the frame's budget as before +/// (only the frame's: a pipeline's own budget is counted in real time in every +/// mode, [`wait_once`]; with `LEONIDA_AB`, on and off while the title runs). +fn deadline_on() -> bool { + crate::toggled!("LEONIDA_PIPELINE_WAIT_DEADLINE") +} + /// LOCAL (the problem of fxceless issue #1181): whether one pipeline is /// waited for at most one budget over the whole run, not one budget every /// frame. A pipeline whose make takes longer than the budget (a driver @@ -455,83 +486,248 @@ fn shared_budget() -> bool { /// `LEONIDA_PIPELINE_WAIT_MS=10000` the picture stood still while the thing /// drawn with it was in view, and moved again when the camera turned away. /// After its budget its draws are dropped until it is made, as with any -/// pipeline past the frame's budget. On by default; +/// pipeline past the frame's budget. The time is counted once over the +/// threads that wait for it at the same time ([`Waited`]): counted per +/// thread, a pass the title runs once that several threads waited for +/// together ran out of its budget early (13 such pipelines on a first start, +/// some reported two or three times at the same millisecond). So it is in +/// every sharing of the frame's budget ([`Sharing`]). On by default; /// `LEONIDA_PIPELINE_WAIT_ONCE=0` waits as before. fn wait_once() -> bool { static ON: OnceLock = OnceLock::new(); *ON.get_or_init(|| std::env::var_os("LEONIDA_PIPELINE_WAIT_ONCE").is_none_or(|v| v != "0")) } -fn reserve_frame_wait( - spent: &mut (u64, Duration), +/// How the threads recording a frame share its budget ([`wait`]). Only the +/// frame's budget: a pipeline's own is counted in real time in every case +/// ([`wait_once`]). +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Sharing { + /// The frame's waits counted once ([`deadline_on`]). + Deadline, + /// A thread takes what is left and gives back what it did not use + /// (`LEONIDA_PIPELINE_WAIT_DEADLINE=0`). + Reserved, + /// A budget per thread, counted as each wait ends + /// (`LEONIDA_PIPELINE_WAIT_SHARED=0`). + PerThread, +} + +/// The sharing that the switches give: `shared` ([`shared_budget`]), and +/// `deadline` ([`deadline_on`]), which counts only with the shared budget. +fn sharing_from(shared: bool, deadline: bool) -> Sharing { + match (shared, deadline) { + (false, _) => Sharing::PerThread, + (true, true) => Sharing::Deadline, + (true, false) => Sharing::Reserved, + } +} + +/// The time waited by threads that may wait at the same time, counted once: +/// what passed while at least one of them waited. +struct Waited { + /// Over the waits that have ended. + spent: Duration, + /// Waits under way, and since when one has been. + waiting: u32, + since: Option, +} + +impl Waited { + const fn new() -> Self { + Self { + spent: Duration::ZERO, + waiting: 0, + since: None, + } + } + + /// What has been waited by `now`. + fn at(&self, now: Instant) -> Duration { + self.spent + self.since.map_or(Duration::ZERO, |since| now.saturating_duration_since(since)) + } + + /// A wait begins at `now`. + fn begin(&mut self, now: Instant) { + if self.waiting == 0 { + self.since = Some(now); + } + self.waiting += 1; + } + + /// A wait ends at `now`. + fn end(&mut self, now: Instant) { + debug_assert!(self.waiting > 0, "a wait ended that had not begun"); + self.waiting = self.waiting.saturating_sub(1); + if self.waiting == 0 + && let Some(since) = self.since.take() + { + self.spent += now.saturating_duration_since(since); + } + } +} + +/// What a frame has waited for pipelines, of one [`Class`]. +struct FrameWait { + /// The frame it is of. frame: u64, - budget: Duration, - own: Duration, - shared: bool, -) -> Duration { - // Recording threads sample the frame before taking the budget lock. - if frame < spent.0 { - return Duration::ZERO; + /// What threads took or were charged ([`Sharing::Reserved`], + /// [`Sharing::PerThread`]). + spent: Duration, + /// Its waits, counted once ([`Sharing::Deadline`]). + waited: Waited, +} + +impl FrameWait { + const fn new(frame: u64) -> Self { + Self { + frame, + spent: Duration::ZERO, + waited: Waited::new(), + } + } + + /// How long a thread recording frame `frame` may wait from `now` (at most + /// `own`, and within the frame's `budget` as `sharing` counts it), and + /// the frame whose waits it joins ([`FrameWait::give_back`]). + fn take( + &mut self, + frame: u64, + now: Instant, + budget: Duration, + own: Duration, + sharing: Sharing, + ) -> (Duration, u64) { + if frame > self.frame { + *self = Self::new(frame); + } + let left = match sharing { + // Recording threads sample the frame before taking the lock: one + // still on an older frame waits now, in the newer frame's time, so + // it joins that frame's waits (and opens no budget of its own). + Sharing::Deadline => budget.saturating_sub(self.waited.at(now)), + _ if frame < self.frame => return (Duration::ZERO, frame), + _ => budget.saturating_sub(self.spent), + } + .min(own); + match sharing { + Sharing::Deadline if !left.is_zero() => self.waited.begin(now), + Sharing::Reserved => self.spent += left, + _ => {} + } + (left, self.frame) } - if frame > spent.0 { - *spent = (frame, Duration::ZERO); + + /// A wait in the waits of frame `joined` that [`FrameWait::take`] gave + /// `left` ended at `now`, after `waited`. + fn give_back( + &mut self, + joined: u64, + now: Instant, + left: Duration, + waited: Duration, + sharing: Sharing, + ) { + // A frame that began meanwhile has its own budget. + if self.frame != joined { + return; + } + match sharing { + Sharing::Deadline => self.waited.end(now), + Sharing::Reserved => { + self.spent = self.spent.saturating_sub(left.saturating_sub(waited)); + } + Sharing::PerThread => self.spent += waited, + } } - let left = budget.saturating_sub(spent.1).min(own); - if shared { - spent.1 += left; +} + +/// Which budget of the frame a pipeline's waits count against. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Class { + World = 0, + /// Passes over the screen, while they go first ([`screens_first`]). + Screen = 1, +} + +fn class_of(p: &Pending, screens_first: bool) -> Class { + if p.screen && screens_first { + Class::Screen + } else { + Class::World } - left } /// Waits for `p` while frame `frame` has budget left. fn wait(p: &Pending, frame: u64) { - let screen = p.screen && screens_first(); - let budget = if screen { screen_budget() } else { wait_budget() }; + // The frame each budget belongs to, and what was waited of it. + static WAITS: [Mutex; 2] = [const { Mutex::new(FrameWait::new(0)) }; 2]; + static TOLD: AtomicBool = AtomicBool::new(false); + let class = class_of(p, screens_first()); + let budget = match class { + Class::Screen => screen_budget(), + Class::World => wait_budget(), + }; + let sharing = sharing_from(shared_budget(), deadline_on()); + if !TOLD.load(Ordering::Relaxed) && !TOLD.swap(true, Ordering::Relaxed) { + tracing::info!( + ?sharing, + ms = wait_budget().as_millis() as u64, + screen_ms = screen_budget().as_millis() as u64, + once = wait_once(), + "how frames wait for host pipelines not made yet, as at the first wait (LEONIDA_AB can change it later; Deadline: the waits of a frame counted once; Reserved: LEONIDA_PIPELINE_WAIT_DEADLINE=0; PerThread: LEONIDA_PIPELINE_WAIT_SHARED=0)" + ); + } + wait_in(p, frame, &WAITS[class as usize], &QUEUE, budget, sharing, class == Class::Screen); +} + +/// [`wait`], with the waits `waits` of the frame's budget `budget` (that of +/// the passes over the screen if `screen`), and the `queue` in which `p` is +/// moved forward. +fn wait_in( + p: &Pending, + frame: u64, + waits: &Mutex, + queue: &Mutex, + budget: Duration, + sharing: Sharing, + screen: bool, +) { if budget.is_zero() { return; } + let once = wait_once(); // LOCAL: what is left of this pipeline's own budget ([`wait_once`]). - let own = if wait_once() { - let own = budget.saturating_sub(Duration::from_micros(p.waited_us.load(Ordering::Relaxed))); - if own.is_zero() { - return; - } - own + let own = if once { + budget.saturating_sub(billed(p, Instant::now())) } else { Duration::MAX }; - // The frame the budget belongs to, and what was waited of it (or is - // taken by a thread waiting now); of the passes over the screen too. - static SPENT: [Mutex<(u64, Duration)>; 2] = [const { Mutex::new((0, Duration::ZERO)) }; 2]; - let spent_of = &SPENT[usize::from(screen)]; - let shared = shared_budget(); - let left = { - let mut spent = spent_of.lock().unwrap_or_else(|e| e.into_inner()); - reserve_frame_wait(&mut spent, frame, budget, own, shared) - }; + if own.is_zero() { + return; + } + let (left, joined) = waits + .lock() + .unwrap_or_else(|e| e.into_inner()) + .take(frame, Instant::now(), budget, own, sharing); 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); + if once { + p.waits.lock().unwrap_or_else(|e| e.into_inner()).begin(Instant::now()); } - let (lock, done) = &DONE; - let mut held = lock.lock().unwrap_or_else(|e| e.into_inner()); - while p.made.get().is_none() { - let rest = left.saturating_sub(started.elapsed()); - if rest.is_zero() { - break; - } - held = done.wait_timeout(held, rest).unwrap_or_else(|e| e.into_inner()).0; - } - drop(held); - let waited = started.elapsed().min(left); - if wait_once() && p.made.get().is_none() { - let before = p.waited_us.fetch_add(waited.as_micros() as u64, Ordering::Relaxed); - if Duration::from_micros(before) + waited >= budget { + let took = until_made(p, left, queue); + let waited = took.min(left); + let now = Instant::now(); + if once { + // Told by the wait that ends the last one under way, once past the budget. + let past = { + let mut charged = p.waits.lock().unwrap_or_else(|e| e.into_inner()); + let before = charged.spent; + charged.end(now); + before < budget && charged.spent >= budget + }; + if past && p.made.get().is_none() { tracing::info!( shaders = ?p.shaders, ms = budget.as_millis() as u64, @@ -547,22 +743,39 @@ fn wait(p: &Pending, frame: u64) { "a pass over the screen was waited for (LEONIDA_SCREEN_WAIT_MS=0: dropped past half a second a frame)" ); } - { - let mut spent = spent_of.lock().unwrap_or_else(|e| e.into_inner()); - // A frame that began meanwhile has its own budget. - if spent.0 == frame { - if shared { - spent.1 = spent.1.saturating_sub(left - waited); - } else { - spent.1 += waited; - } + waits + .lock() + .unwrap_or_else(|e| e.into_inner()) + .give_back(joined, now, left, waited, sharing); + #[cfg(feature = "diagnostics")] + crate::copystat::add(crate::copystat::C::PipelineWaitUs, took.as_micros() as u64); +} + +/// What `p` has been waited for by `now` ([`wait_once`]). +fn billed(p: &Pending, now: Instant) -> Duration { + p.waits.lock().unwrap_or_else(|e| e.into_inner()).at(now) +} + +/// Waits up to `left` for `p` to be made, moved in `queue` before the others +/// asked for first ([`await_first`]); how long it took. +fn until_made(p: &Pending, left: Duration, queue: &Mutex) -> Duration { + // Before the promotion: the queue's lock it may wait for is part of this + // wait, not on top of it. + let started = Instant::now(); + if await_first() { + promote_in(queue, p); + } + let (lock, done) = &DONE; + let mut held = lock.lock().unwrap_or_else(|e| e.into_inner()); + while p.made.get().is_none() { + let rest = left.saturating_sub(started.elapsed()); + if rest.is_zero() { + break; } + held = done.wait_timeout(held, rest).unwrap_or_else(|e| e.into_inner()).0; } - #[cfg(feature = "diagnostics")] - crate::copystat::add( - crate::copystat::C::PipelineWaitUs, - started.elapsed().as_micros() as u64, - ); + drop(held); + started.elapsed() } /// Whether a pipeline a frame waits for goes before the others asked for @@ -579,11 +792,11 @@ fn await_first() -> bool { } /// 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) { +/// list in `queue` 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_in(queue: &Mutex, p: &Pending) { if p.queue_state.load(Ordering::Relaxed) == REQUESTED { - QUEUE.lock().unwrap_or_else(|e| e.into_inner()).promote(p); + queue.lock().unwrap_or_else(|e| e.into_inner()).promote(p); } } @@ -843,24 +1056,212 @@ fn drained() { mod tests { use super::*; + fn ms(n: u64) -> Duration { + Duration::from_millis(n) + } + + /// A pipeline no worker will make (taken, as by a worker that does not + /// finish): a wait for it runs to its end. + fn never_made() -> Arc { + let p = Pending::new(Vec::new(), None); + p.queue_state.store(MAKING, Ordering::Relaxed); + Arc::new(p) + } + + /// Waits until a thread waits in `waits` (failing past ten seconds). + fn until_waiting(waits: &Mutex) { + let began = Instant::now(); + while waits.lock().unwrap().waited.waiting == 0 { + assert!(began.elapsed() < Duration::from_secs(10), "no thread began to wait"); + std::thread::yield_now(); + } + } + + /// With both switches at their default (on) the waits of a frame count + /// once; `LEONIDA_PIPELINE_WAIT_DEADLINE=0` reserves, and + /// `LEONIDA_PIPELINE_WAIT_SHARED=0` counts per thread whatever the other. + #[test] + fn the_switches_choose_how_a_frame_shares_its_budget() { + assert_eq!(sharing_from(true, true), Sharing::Deadline); + assert_eq!(sharing_from(true, false), Sharing::Reserved); + assert_eq!(sharing_from(false, true), Sharing::PerThread); + assert_eq!(sharing_from(false, false), Sharing::PerThread); + } + + /// Waits at the same time count once: what passed while one at least + /// was under way. + #[test] + fn waits_at_the_same_time_count_once() { + let t0 = Instant::now(); + let at = |n| t0 + ms(n); + let mut w = Waited::new(); + w.begin(at(0)); + w.begin(at(50)); + assert_eq!(w.at(at(100)), ms(100)); + w.end(at(200)); + assert_eq!(w.at(at(250)), ms(250), "one still waits"); + w.end(at(300)); + assert_eq!(w.at(at(900)), ms(300), "none waits: the time does not count"); + w.begin(at(1000)); + w.end(at(1100)); + assert_eq!((w.at(at(2000)), w.waiting), (ms(400), 0)); + } + + /// Threads recording one frame wait to one deadline while their waits + /// overlap: the second to wait is left what is left of the first's, not + /// nothing (the reservation gave the first all of the budget) and not a + /// budget of its own. + #[test] + fn threads_of_a_frame_wait_to_one_deadline() { + let (budget, t0) = (ms(500), Instant::now()); + let mut w = FrameWait::new(0); + let take = + |w: &mut FrameWait, at, own| w.take(42, t0 + ms(at), budget, own, Sharing::Deadline); + assert_eq!(take(&mut w, 0, budget), (budget, 42)); + assert_eq!(take(&mut w, 100, budget), (ms(400), 42)); + // Its own budget ([`wait_once`]) still holds a thread back. + assert_eq!(take(&mut w, 100, ms(50)), (ms(50), 42)); + // The first ends early: the others wait on, and so does the count. + w.give_back(42, t0 + ms(150), budget, ms(150), Sharing::Deadline); + assert_eq!(take(&mut w, 200, budget), (ms(300), 42)); + assert_eq!(take(&mut w, 600, budget), (Duration::ZERO, 42)); + } + + /// The time a thread records between two waits is not counted: only + /// that during which one thread at least waited. + #[test] + fn the_time_between_waits_is_not_counted() { + let (budget, t0) = (ms(500), Instant::now()); + let mut w = FrameWait::new(0); + let at = |n| t0 + ms(n); + assert_eq!(w.take(42, at(0), budget, budget, Sharing::Deadline), (budget, 42)); + w.give_back(42, at(100), budget, ms(100), Sharing::Deadline); + assert_eq!(w.take(42, at(1100), budget, budget, Sharing::Deadline), (ms(400), 42)); + w.give_back(42, at(1500), ms(400), ms(400), Sharing::Deadline); + assert_eq!(w.take(42, at(1600), budget, budget, Sharing::Deadline), (Duration::ZERO, 42)); + } + + /// A thread that sampled an older frame before taking the lock does not + /// open that frame's budget again (pull request 2601), and joins the + /// newer frame's waits instead of not waiting at all: it waits now, in + /// the newer frame's time. #[test] fn older_frames_cannot_reopen_a_pipeline_wait_budget() { - let budget = Duration::from_millis(500); - for shared in [true, false] { - let mut spent = (0, Duration::ZERO); - let reserve = |spent: &mut _, frame| reserve_frame_wait(spent, frame, budget, budget, shared); - assert_eq!(reserve(&mut spent, 0), budget); - assert_eq!(reserve(&mut spent, 42), budget); - if !shared { + let (budget, t0) = (ms(500), Instant::now()); + let mut w = FrameWait::new(0); + let take = |w: &mut FrameWait, frame, at| { + w.take(frame, t0 + ms(at), budget, budget, Sharing::Deadline) + }; + assert_eq!(take(&mut w, 42, 0), (budget, 42)); + assert_eq!(take(&mut w, 41, 100), (ms(400), 42)); + assert_eq!(take(&mut w, 42, 200), (ms(300), 42)); + // The next frame has a budget of its own; a thread still on the one + // before joins it, and what ends in the frame before leaves it be. + assert_eq!(take(&mut w, 43, 300), (budget, 43)); + assert_eq!(take(&mut w, 42, 400), (ms(400), 43)); + w.give_back(42, t0 + ms(450), ms(400), ms(350), Sharing::Deadline); + assert_eq!(w.waited.waiting, 2); + } + + /// `LEONIDA_PIPELINE_WAIT_DEADLINE=0` and `LEONIDA_PIPELINE_WAIT_SHARED=0` + /// count the frame's budget as before. + #[test] + fn the_switches_off_count_the_budget_as_before() { + let (budget, t0) = (ms(500), Instant::now()); + for sharing in [Sharing::Reserved, Sharing::PerThread] { + let mut w = FrameWait::new(0); + assert_eq!(w.take(42, t0, budget, budget, sharing).0, budget); + if sharing == Sharing::PerThread { // Without a reservation, the wait is charged when it finishes. - spent.1 += budget; + w.give_back(42, t0, budget, budget, sharing); } + assert_eq!(w.take(41, t0, budget, budget, sharing).0, Duration::ZERO); + assert_eq!(w.take(42, t0, budget, budget, sharing).0, Duration::ZERO); + assert_eq!(w.take(43, t0, budget, budget, sharing).0, budget); + } + // The reservation gives back what a wait did not use. + let mut w = FrameWait::new(0); + assert_eq!(w.take(7, t0, budget, budget, Sharing::Reserved).0, budget); + w.give_back(7, t0, budget, ms(100), Sharing::Reserved); + assert_eq!(w.take(7, t0, budget, budget, Sharing::Reserved).0, ms(400)); + } - let delayed = reserve(&mut spent, 41); - let same_frame = reserve(&mut spent, 42); - assert_eq!((delayed, same_frame), (Duration::ZERO, Duration::ZERO)); - assert_eq!(reserve(&mut spent, 43), budget); + /// Two threads of one frame wait at once for pipelines not made: both + /// wait, the second to the first's deadline, so that the frame waits its + /// budget once. + #[test] + fn overlapping_waits_cost_the_frame_its_budget_once() { + let budget = ms(600); + let (waits, queue) = (Mutex::new(FrameWait::new(0)), Mutex::new(Queue::new())); + let (a, b) = (never_made(), never_made()); + let began = Instant::now(); + let (first, second) = std::thread::scope(|s| { + let first = s.spawn(|| { + let t = Instant::now(); + wait_in(&a, 9, &waits, &queue, budget, Sharing::Deadline, false); + t.elapsed() + }); + until_waiting(&waits); + std::thread::sleep(ms(200)); + let t = Instant::now(); + wait_in(&b, 9, &waits, &queue, budget, Sharing::Deadline, false); + let second = t.elapsed(); + (first.join().unwrap(), second) + }); + let together = began.elapsed(); + assert!(first >= budget, "the first waits the budget: {first:?}"); + assert!(second >= ms(100), "the second is not left with nothing: {second:?}"); + assert!(second < ms(500), "the second waits to the first's deadline: {second:?}"); + assert!(together < budget + ms(300), "the frame waits its budget once: {together:?}"); + } + + /// A pipeline that two threads wait for together is charged the time it + /// was waited for once ([`wait_once`]), not the threads' time added up. + #[test] + fn a_pipeline_is_charged_the_time_it_was_waited_for_once() { + let budget = ms(300); + let (waits, queue) = (Mutex::new(FrameWait::new(0)), Mutex::new(Queue::new())); + let p = never_made(); + std::thread::scope(|s| { + let first = + s.spawn(|| wait_in(&p, 9, &waits, &queue, budget, Sharing::Deadline, false)); + until_waiting(&waits); + wait_in(&p, 9, &waits, &queue, budget, Sharing::Deadline, false); + first.join().unwrap(); + }); + let charged = billed(&p, Instant::now()); + let once = budget..budget + ms(150); + assert!(once.contains(&charged), "charged {charged:?} for {budget:?}"); + } + + /// A frame that begins to wait for a pipeline moves it before those + /// asked for earlier ([`await_first`]). + #[test] + fn a_frame_wait_moves_the_pipeline_forward() { + let new = || Arc::new(Pending::new(Vec::new(), None)); + let (earlier, awaited) = (new(), new()); + let (waits, queue) = (Mutex::new(FrameWait::new(0)), Mutex::new(Queue::new())); + for p in [&earlier, &awaited] { + assert_eq!(queue.lock().unwrap().push_as(p, REQUESTED, false), Some(true)); } + wait_in(&awaited, 9, &waits, &queue, ms(20), Sharing::Deadline, false); + assert_eq!(awaited.queue_state.load(Ordering::Relaxed), AWAITED); + in_order(&mut queue.lock().unwrap(), false, &[&awaited, &earlier]); + } + + /// Passes over the screen count against a budget of their own while + /// they go first; other pipelines, and all with screens not first, + /// against the world's. + #[test] + fn passes_over_the_screen_have_a_budget_of_their_own() { + let of = |screen| { + let mut p = Pending::new(Vec::new(), None); + p.screen = screen; + p + }; + assert_eq!(class_of(&of(true), true), Class::Screen); + assert_eq!(class_of(&of(true), false), Class::World); + assert_eq!(class_of(&of(false), true), Class::World); } #[test] diff --git a/d3d12/docs/switches.md b/d3d12/docs/switches.md index 33bc9b3cc9..5ea08c136c 100644 --- a/d3d12/docs/switches.md +++ b/d3d12/docs/switches.md @@ -6,7 +6,7 @@ It says which names exist and what the code's own comments say of them. It is ** settings and says nothing of what a switch gains: for that read [findings.md](findings.md) first, and [how to measure frame rates](../../docs/measuring_fps.md) before trying one. -674 names; 634 have a sentence of a comment below, the others no comment or only a bare list. +675 names; 635 have a sentence of a comment below, the others no comment or only a bare list. - **Kind**: how the code reads it, where that can be told. "see the code" is a number, a path, a list or a switch read some other way; its default is not given here. @@ -21,7 +21,7 @@ and [how to measure frame rates](../../docs/measuring_fps.md) before trying one. | Name | Kind | A/B | L | F | Named in | In the code's words | |---|---|---|---|---|---|---| -| `LEONIDA_AB` | see the code | | | yes | `leonida/leonida.rs` +40 | `LEONIDA_AB=<seconds>,<switch>[,<switch>..]`: every so many seconds, at a present, the next phase begins; switch number n is on in the phases whose number has bit n, so that all combinations come one after another, again... | +| `LEONIDA_AB` | see the code | | | yes | `leonida/leonida.rs` +41 | `LEONIDA_AB=<seconds>,<switch>[,<switch>..]`: every so many seconds, at a present, the next phase begins; switch number n is on in the phases whose number has bit n, so that all combinations come one after another, again... | | `LEONIDA_AUDIO_CAPTURE` | see the code | | | | `leonida/main.rs` +1 | `LEONIDA_AUDIO_CAPTURE=<file.wav>` records what the title plays, 48 kHz stereo 16-bit, through process loopback: our own process tree only, never the rest of the host's mix. | | `LEONIDA_CAPTURE` | see the code | | yes | | `leonida/leonida.rs` +4 | Presented frames written to PNG files, to see what reaches the screen: with `LEONIDA_CAPTURE=<folder>`, every frame whose number is a power of two (the frames the log reports) is saved as `frame-<n>.png`, at half size. | | `LEONIDA_CARD_AWAKE` | see the code | yes | yes | yes | `leonida/replay.rs` +4 | Keeps the card's clocks up between submissions (`LEONIDA_CARD_AWAKE=1`; off by default). | @@ -409,9 +409,10 @@ and [how to measure frame rates](../../docs/measuring_fps.md) before trying one. | `LEONIDA_PIPELINE_LOWEST` | see the code | | | | `leonida-gpu/pipeline/make.rs` | Below the title's own threads (loading, recording), and the streaming one, which is below normal itself (`LEONIDA_PIPELINE_LOWEST=0`: level with it, as before). | | `LEONIDA_PIPELINE_PANIC_CATCH` | see the code | | | | `leonida-gpu/pipeline/make.rs` | `LEONIDA_PIPELINE_PANIC_CATCH=0`: the panic goes on, as before. | | `LEONIDA_PIPELINE_SLOW` | see the code | | | | `leonida-gpu/pipeline/make.rs` | `LEONIDA_PIPELINE_SLOW=0`: in the order the title creates them, as before. | +| `LEONIDA_PIPELINE_WAIT_DEADLINE` | on, `0` turns it off | yes | | | `leonida-gpu/pipeline/make.rs` | `LEONIDA_PIPELINE_WAIT_DEADLINE=0` reserves the frame's budget as before (only the frame's: a pipeline's own budget is counted in real time in every mode, [`wait_once`]; with `LEONIDA_AB`, on and off while the title runs... | | `LEONIDA_PIPELINE_WAIT_MS` | see the code | | yes | yes | `leonida-gpu/host/menu.rs` +2 | ...e budget in every frame in which a draw asked for it: with `LEONIDA_PIPELINE_WAIT_MS=10000` the picture stood still while the thing drawn with it was in view, and moved again when the camera turned away.... | | `LEONIDA_PIPELINE_WAIT_ONCE` | see the code | | | | `leonida-gpu/pipeline/make.rs` | `LEONIDA_PIPELINE_WAIT_ONCE=0` waits as before. | -| `LEONIDA_PIPELINE_WAIT_SHARED` | see the code | | | | `leonida-gpu/pipeline/make.rs` | `LEONIDA_PIPELINE_WAIT_SHARED=0` counts as before. | +| `LEONIDA_PIPELINE_WAIT_SHARED` | see the code | | | | `leonida-gpu/pipeline/make.rs` | `LEONIDA_PIPELINE_WAIT_SHARED=0` counts the frame's budget as before (a pipeline's own budget is counted in real time in every mode, [`wait_once`]). | | `LEONIDA_PIPELINE_WORKERS` | see the code | | | | `leonida-gpu/pipeline/make.rs` | `asked` (`LEONIDA_PIPELINE_WORKERS`, 1 to 64), else two thirds of the logical processors, 2 to 16. | | `LEONIDA_PLACED_FILLER` | see the code | | | | `leonida-gpu/resource.rs` | `LEONIDA_PLACED_FILLER=1`: kept in the key, as before. | | `LEONIDA_PLACED_LEFT_READERS` | see the code | yes | | | `leonida-gpu/host/card_steps.rs` | ...ne of its lists waits for them and the CPU still needs it (`LEONIDA_PLACED_LEFT_READERS=1`; off by default; `LEONIDA_AB` can name it). [`Host::card_steps`] is asked when something keeps a submission from being prepared.... | @@ -471,6 +472,7 @@ and [how to measure frame rates](../../docs/measuring_fps.md) before trying one. | `LEONIDA_REMAP_BOOST` | on, `0` turns it off | yes | | | `leonida-gpu/watch.rs` | Whether the thread that changes a mapping runs at the highest priority while it holds title memory still (`LEONIDA_REMAP_BOOST=0`: at its own). | | `LEONIDA_REMAP_HISTORY` | on, `0` turns it off | yes | | | `leonida-gpu/descriptor/host.rs` | The cursor of the single shared pass with `LEONIDA_REMAP_HISTORY=0` (as before: the heap whose pass runs first takes the ranges). | | `LEONIDA_REMAP_SPANS` | on, `0` turns it off | yes | | yes | `leonida-gpu/texture.rs` +1 | Whether a read in place that names its range ([`title_memory_reading_range`]) waits only for the changes of mapping that touch that range (`LEONIDA_REMAP_SPANS=0`: for every change, as before; `LEONIDA_AB` can name it).... | +| `LEONIDA_RENDER_SCALE_MIN` | see the code | | yes | | `leonida-gpu/host/menu.rs` +2 | | | `LEONIDA_REPLAY_GBV` | see the code | | | | `leonida-gpu/replay/play.rs` | | | `LEONIDA_REPLAY_PEEK` | see the code | | | | `leonida-gpu/replay/play/diverge.rs` | | | `LEONIDA_RETIRE_QUEUES` | on, `0` turns it off | yes | | yes | `leonida-gpu/host.rs` | `LEONIDA_RETIRE_QUEUES=0`: every thread takes the first, as before. | @@ -678,7 +680,6 @@ and [how to measure frame rates](../../docs/measuring_fps.md) before trying one. | `LEONIDA_PATCH_CLZERO` | see the code | | | | `leonida-loader/console_insn.rs` | `LEONIDA_PATCH_CLZERO=0` emulates each time, as before. | | `LEONIDA_PATCH_WAITS` | see the code | | | | `leonida-loader/console_insn.rs` | `LEONIDA_PATCH_WAITS=0` emulates each time, as before. | | `LEONIDA_PROFILE_ONLY` | see the code | | | yes | `leonida-loader/profile.rs` | `LEONIDA_PROFILE_ONLY=<name>[,<name>..]` samples only the threads whose name or start (`module+rva`) holds one of them, which makes the rounds short. | -| `LEONIDA_RENDER_SCALE_MIN` | see the code | | yes | | `leonida-loader/hooks.rs` +1 | | | `LEONIDA_SIM_MAX_STEP_MS` | see the code | | | | `leonida-loader/hooks.rs` +1 | The ceiling `LEONIDA_SIM_MAX_STEP_MS` asks for: `Some(None)` for the title's own, `None` if not one of them. | | `LEONIDA_STACKS` | see the code | | | | `leonida-loader/profile.rs` +1 | `LEONIDA_STACKS=<seconds>[,<seconds>...]` prints each thread's unwound stack as `module+rva` that many seconds after [`dump_stacks_on_schedule`], to find what a title waits on. |