From 357c4aab876397c902162cfe7ab79e34bd03057a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9rez?= Date: Tue, 24 Nov 2026 02:06:00 +0100 Subject: [PATCH 1/2] GPU: a frame wait takes its tick before it sleeps frame::wait marked the tick it waited for as taken (LAST) only after the sleep, outside the clock's lock, so two threads waiting for one event kind at once both got the same tick and both returned on it, against the module's once-per-tick rule (an origin wait then begins two frames in one interval). next_tick now takes the tick under the clock's lock when the wait comes within its time-out; a time-out takes none, as before. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 810f4422c763fe2904495919eed248d972b3f9cb) --- crates/viport-gpu/src/frame.rs | 38 ++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/crates/viport-gpu/src/frame.rs b/crates/viport-gpu/src/frame.rs index ac058bfa..3c4ad41d 100644 --- a/crates/viport-gpu/src/frame.rs +++ b/crates/viport-gpu/src/frame.rs @@ -92,10 +92,12 @@ fn last(kind: u32) -> &'static AtomicU64 { &LAST[(kind as usize).min(LAST.len() - 1)] } -/// The next tick after now and after the last one a wait for `kind` ended -/// on (an event happens once per tick: a wait woken a little early must not -/// let the next one end on the same tick), and how long until it. -fn next_tick(kind: u32) -> (u64, Duration) { +/// The next tick after now and after the last one a wait for `kind` took +/// (an event happens once per tick: a wait woken a little early must not +/// let the next one end on the same tick), and how long until it. A wait +/// that comes within `limit` takes the tick here, under the clock's lock, +/// so two threads waiting for one kind never take the same tick. +fn next_tick(kind: u32, limit: Duration) -> (u64, Duration) { let mut clock = clock(); let c = clock.get_or_insert_with(|| Clock { start: Instant::now(), @@ -116,10 +118,11 @@ fn next_tick(kind: u32) -> (u64, Duration) { } let tick = (c.first + (elapsed / interval + 1) as u64).max(last_tick + 1); let at = u128::from(tick - c.first) * interval; - ( - tick, - Duration::from_nanos(at.saturating_sub(elapsed) as u64), - ) + let wait = Duration::from_nanos(at.saturating_sub(elapsed) as u64); + if wait <= limit { + last(kind).fetch_max(tick, Ordering::Relaxed); + } + (tick, wait) } /// Device method: blocks until the next tick (or the time-out); an origin @@ -135,14 +138,13 @@ pub(crate) extern "system" fn wait( token: *mut u64, ) -> HRESULT { let origin = kind == ORIGIN; - let (tick, wait) = next_tick(kind); let limit = Duration::from_millis(u64::from(timeout_ms)); + let (_, wait) = next_tick(kind, limit); let hr = if wait > limit { std::thread::sleep(limit); TIMEOUT } else { std::thread::sleep(wait); - last(kind).fetch_max(tick, Ordering::Relaxed); S_OK }; // A frame begun on a time-out takes no tick: its flip still does. @@ -231,7 +233,7 @@ mod tests { .collect(); assert!(flips.windows(2).all(|w| w[1] == w[0] + 1), "{flips:?}"); // Even when a flip wait begins on the tick another just ended on. - let (tick, _) = next_tick(FLIP); + let (tick, _) = next_tick(FLIP, Duration::ZERO); assert!(tick > flips[3]); // A frame over budget: its flip wait begins after a tick nobody @@ -251,4 +253,18 @@ mod tests { assert!(late.elapsed() < Duration::from_millis(10)); assert_eq!(LAST[2].load(Ordering::Relaxed), flipped + 1); } + + /// Two waits for one kind that begin together (two threads) take two + /// ticks: the first takes its tick before it sleeps. A time-out takes + /// none. The old code marked the tick after the sleep, so both got it. + #[test] + fn waits_begun_together_take_different_ticks() { + const VBLANK: u32 = 1; + let long = Duration::from_secs(1); + let (first, _) = next_tick(VBLANK, long); + let (second, _) = next_tick(VBLANK, long); + assert!(second > first, "{second} after {first}"); + next_tick(VBLANK, Duration::ZERO); + assert_eq!(last(VBLANK).load(Ordering::Relaxed), second); + } } From 29b7051db8f09d46876939516d5d187dde2ccda5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9rez?= Date: Tue, 24 Nov 2026 08:45:52 +0100 Subject: [PATCH 2/2] GPU: keep the frame wait's tick for the diagnostics log The previous commit dropped the tick next_tick returns, but the diagnostics build's tracing::debug! still names it (E0425). Bind it again; it is unused without the feature, so that lint is allowed there. Reported by Sunkiller-qc and r4tedx. cargo check -p victory-gpu passes with and without --features diagnostics. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 882664e13cfa557fb46d41e182c3267b01097485) --- crates/viport-gpu/src/frame.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/viport-gpu/src/frame.rs b/crates/viport-gpu/src/frame.rs index 3c4ad41d..0920159d 100644 --- a/crates/viport-gpu/src/frame.rs +++ b/crates/viport-gpu/src/frame.rs @@ -139,7 +139,9 @@ pub(crate) extern "system" fn wait( ) -> HRESULT { let origin = kind == ORIGIN; let limit = Duration::from_millis(u64::from(timeout_ms)); - let (_, wait) = next_tick(kind, limit); + // The tick is only logged (diagnostics); taking it is what counts. + #[cfg_attr(not(feature = "diagnostics"), allow(unused_variables))] + let (tick, wait) = next_tick(kind, limit); let hr = if wait > limit { std::thread::sleep(limit); TIMEOUT