Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions crates/viport-gpu/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand All @@ -135,14 +138,15 @@ 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));
// 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
} 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.
Expand Down Expand Up @@ -231,7 +235,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
Expand All @@ -251,4 +255,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);
}
}
Loading