From 56ec2afb0c3d6ba9dd75dae0e23acd2871443890 Mon Sep 17 00:00:00 2001 From: fxceless <36782933+fxceless@users.noreply.github.com> Date: Fri, 27 Nov 2026 23:04:45 +0300 Subject: [PATCH 1/2] GPU: a dispatch waits for its compute pipeline until it is made A draw whose pipeline is not ready within the frame's budget is dropped and drawn again next frame. A dispatch may not be drawn again: the title runs some compute passes once, when a scene changes, and what they leave is read for as long as the scene lasts. A compute program met for the first time took 4.4 s to make on a 16 GB machine; its one dispatch was dropped after the half second of budget, and the windows of the room before stayed blended over the picture for minutes (run 195). A compute pipeline now waits for its own making, whatever the budget; a draw still waits within the budget. The pipeline library has it on the next run. `VICTORY_PIPELINE_COMPUTE_WAIT=0` restores the old behaviour. Ported from fxceless/victory-fork a42b627 (pull request #1312), GPL-3.0-or-later. --- crates/victory-gpu/src/pipeline/make.rs | 27 ++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/victory-gpu/src/pipeline/make.rs b/crates/victory-gpu/src/pipeline/make.rs index 9b7b09584..dbfddb625 100644 --- a/crates/victory-gpu/src/pipeline/make.rs +++ b/crates/victory-gpu/src/pipeline/make.rs @@ -289,11 +289,36 @@ pub(crate) fn ready(p: &Arc) -> Option<&HostPipeline> { } if p.made.get().is_none() { start(p, REQUESTED); - wait(p, crate::frame::current()); + if p.graphics.is_none() && compute_waits() { + // Made here, or waited for while a worker makes it. + p.make(); + } else { + wait(p, crate::frame::current()); + } } p.made.get()?.as_ref() } +/// Whether a dispatch waits for its compute pipeline until it is made, +/// whatever the frame's budget ([`wait_budget`]); a draw still waits within +/// the budget and is dropped while its pipeline is not there. +/// +/// A dropped draw is drawn again in the next frame. A dropped dispatch may +/// not be: the title runs some compute passes once, when a scene changes +/// (a buffer reset or cleared), and what they leave is read for as long as +/// the scene lasts. In the prison corridor a compute program met for the +/// first time took 4.4 s to make on a 16 GB machine; its one dispatch was +/// dropped after the half second of budget, and the windows of the room +/// before stayed blended over the whole picture for minutes (run 195). +/// A wait of seconds once (the pipeline library has it on the next run) +/// against a picture wrong until the scene ends. +/// On by default; `VICTORY_PIPELINE_COMPUTE_WAIT=0`: within the budget, as +/// before. +fn compute_waits() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("VICTORY_PIPELINE_COMPUTE_WAIT").is_none_or(|v| v != "0")) +} + /// Makes `p` on this thread if it is not made yet (waiting for a worker /// that is making it); whether it has a host pipeline. pub(crate) fn now(p: &Arc) -> bool { From 74113b0933a8bbce5de8abde4efd83280b2e2079 Mon Sep 17 00:00:00 2001 From: armin Date: Fri, 27 Nov 2026 01:29:26 -0800 Subject: [PATCH 2/2] GPU: cap the time a frame waits for compute pipelines made at their dispatch With VICTORY_PIPELINE_COMPUTE_WAIT on (the default) a compute dispatch makes its pipeline on the spot, however long that takes. With an empty cache the first cutscene met 457 compute programs in one frame at about a second each: the frame did not end for over 30 s and the title's own hang watchdog stored to address 0 (+0x23cf683, #1420). A frame now waits for such pipelines up to VICTORY_PIPELINE_COMPUTE_WAIT_MS (8,000 by default, summed over threads); after that its dispatches wait within the frame's budget as with VICTORY_PIPELINE_COMPUTE_WAIT=0. The first 600 frames (loading) still make theirs whatever it costs. --- crates/victory-gpu/src/pipeline/make.rs | 66 +++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/crates/victory-gpu/src/pipeline/make.rs b/crates/victory-gpu/src/pipeline/make.rs index dbfddb625..5a8261cda 100644 --- a/crates/victory-gpu/src/pipeline/make.rs +++ b/crates/victory-gpu/src/pipeline/make.rs @@ -289,11 +289,19 @@ pub(crate) fn ready(p: &Arc) -> Option<&HostPipeline> { } if p.made.get().is_none() { start(p, REQUESTED); - if p.graphics.is_none() && compute_waits() { - // Made here, or waited for while a worker makes it. + // A compute pipeline with `compute_waits`: made here, or waited for + // while a worker makes it. The first 600 frames (loading) make + // theirs whatever it costs; later frames only while under `compute_cap`. + let frame = crate::frame::current(); + let early = frame < 600; + if p.graphics.is_none() && compute_waits() && (early || compute_room(frame)) { + let began = std::time::Instant::now(); p.make(); + if !early { + compute_spent(frame, began.elapsed()); + } } else { - wait(p, crate::frame::current()); + wait(p, frame); } } p.made.get()?.as_ref() @@ -319,6 +327,58 @@ fn compute_waits() -> bool { *ON.get_or_init(|| std::env::var_os("VICTORY_PIPELINE_COMPUTE_WAIT").is_none_or(|v| v != "0")) } +/// The most a frame waits for compute pipelines made at their dispatch +/// ([`compute_waits`]), over all threads, before its later dispatches go +/// back to the frame's budget ([`wait`]). With a new cache the first +/// cutscene met 457 compute programs in one frame at about a second each: +/// the frame did not end for over 30 s and the title's own hang watchdog +/// stored to address 0. Capped, the frame ends and the rest are made by +/// the workers and waited for within the budget, as before +/// [`compute_waits`]. The check comes before a make, so a frame can run +/// over by the makes already started on other threads (seconds each): +/// `VICTORY_PIPELINE_COMPUTE_WAIT_MS`, 8,000 by default, leaves room for +/// that under the watchdog's 30 s. +fn compute_cap() -> std::time::Duration { + static MS: OnceLock = OnceLock::new(); + std::time::Duration::from_millis(*MS.get_or_init(|| { + std::env::var("VICTORY_PIPELINE_COMPUTE_WAIT_MS") + .ok() + .and_then(|v| v.trim().parse().ok()) + .unwrap_or(8_000) + })) +} + +/// The frame whose compute waits are counted, and the microseconds counted. +static COMPUTE_WAITED: std::sync::Mutex<(u64, u64)> = std::sync::Mutex::new((0, 0)); + +/// Whether frame `frame` may still wait for a compute pipeline ([`compute_cap`]). +fn compute_room(frame: u64) -> bool { + let mut w = COMPUTE_WAITED.lock().unwrap_or_else(|e| e.into_inner()); + if w.0 != frame { + *w = (frame, 0); + } + let room = u128::from(w.1) < compute_cap().as_micros(); + if !room { + static SAID: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + if !SAID.swap(true, Ordering::Relaxed) { + tracing::warn!( + frame, + cap_ms = u64::try_from(compute_cap().as_millis()).unwrap_or(u64::MAX), + "a frame waited its cap for compute pipelines; the rest wait within its budget (said once)" + ); + } + } + room +} + +/// Counts a compute wait to `frame`, if that is still the frame counted. +fn compute_spent(frame: u64, took: std::time::Duration) { + let mut w = COMPUTE_WAITED.lock().unwrap_or_else(|e| e.into_inner()); + if w.0 == frame { + w.1 = w.1.saturating_add(u64::try_from(took.as_micros()).unwrap_or(u64::MAX)); + } +} + /// Makes `p` on this thread if it is not made yet (waiting for a worker /// that is making it); whether it has a host pipeline. pub(crate) fn now(p: &Arc) -> bool {