Skip to content
Open
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
84 changes: 70 additions & 14 deletions crates/victory-gpu/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//! with "token on time-out" (starting its next frame at once) and is paced
//! by a blocking wait for the flip before each present.
//!
//! shortcut: event offsets are ignored (every event is at the tick); tie
//! the clock to presentation if a title paces by them.
//! Events are scheduled at an offset from the tick, which waits honour;
//! the clock is still free-running rather than tied to presentation.
#![allow(clippy::not_unsafe_ptr_arg_deref)]

use std::sync::Mutex;
Expand Down Expand Up @@ -68,14 +68,20 @@ pub(crate) extern "system" fn set_interval(
S_OK
}

/// Device method: events are scheduled at every tick.
/// Per event kind: the offset from the tick, in nanoseconds, last scheduled.
static OFFSETS: [AtomicU64; 3] = [const { AtomicU64::new(0) }; 3];

/// Device method: events are scheduled at every tick, `offset` microseconds
/// into it. The list is the event's wait list, which our clock does not use.
pub(crate) extern "system" fn schedule(
_: This,
_kind: u32,
_offset: u32,
kind: u32,
offset: u32,
_list: *const u8,
_flags: u32,
) -> HRESULT {
OFFSETS[(kind as usize).min(OFFSETS.len() - 1)]
.store(u64::from(offset) * 1_000, Ordering::Relaxed);
S_OK
}

Expand All @@ -92,12 +98,21 @@ fn last(kind: u32) -> &'static AtomicU64 {
&LAST[(kind as usize).min(LAST.len() - 1)]
}

/// The scheduled tick and delay, in nanoseconds. Whole intervals in the
/// offset advance the tick. Wait for its remaining offset if the event is
/// still pending; otherwise take it at once, unless a wait already took it.
fn event_after(elapsed: u128, interval: u128, first: u64, last: u64, offset: u128) -> (u64, u128) {
let interval = interval.max(1);
let tick = (first + (elapsed / interval + offset / interval) as u64).max(last + 1);
let at = u128::from(tick - first) * interval + offset % interval;
(tick, at.saturating_sub(elapsed))
}

/// The tick a wait for `kind` ends on, and how long until it: the first
/// one after the last 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), the latest one passed if that is later. A wait begun after
/// its tick passed (a frame slower than the interval) so ends at once, as
/// an event signalled at the tick and left signalled until a wait takes it
/// tick), or the current scheduled tick if that is later. A wait begun
/// after that tick's event passed ends at once, as an event left signalled
/// until a wait takes it
/// would (assumed: nothing we have says what a late wait does); waiting for
/// the next tick instead cost the cutscene's frames 0-33 ms each (4 % of
/// the frame thread, run perf3). A wait that
Expand All @@ -110,11 +125,14 @@ fn next_tick(kind: u32, limit: Duration) -> (u64, Duration) {
interval: SIXTIETH,
first: 0,
});
let elapsed = c.start.elapsed().as_nanos();
let interval = c.interval.as_nanos().max(1);
let tick = (c.first + (elapsed / interval) as u64).max(last(kind).load(Ordering::Relaxed) + 1);
let at = u128::from(tick - c.first) * interval;
let wait = Duration::from_nanos(at.saturating_sub(elapsed) as u64);
let (tick, delay) = event_after(
c.start.elapsed().as_nanos(),
c.interval.as_nanos(),
c.first,
last(kind).load(Ordering::Relaxed),
u128::from(OFFSETS[(kind as usize).min(OFFSETS.len() - 1)].load(Ordering::Relaxed)),
);
let wait = Duration::from_nanos(delay.min(u64::MAX as u128) as u64);
if wait <= limit {
last(kind).fetch_max(tick, Ordering::Relaxed);
}
Expand Down Expand Up @@ -217,6 +235,41 @@ pub(crate) extern "system" fn statistics(
mod tests {
use super::*;

#[test]
fn events_happen_at_their_offset_within_the_tick() {
const MS: u128 = 1_000_000;
// A quarter into the first millisecond after now.
assert_eq!(event_after(250_000, MS, 0, 0, 0), (1, 750_000));
assert_eq!(event_after(250_000, MS, 0, 0, 300_000), (1, 1_050_000));
// An offset past a tick moves the event into the next one; that
// event is still ahead and must not be skipped.
assert_eq!(event_after(250_000, MS, 0, 0, MS + 200_000), (1, 950_000));
// Review note on #321: between a tick and its offset, that tick's
// event is still pending.
assert_eq!(event_after(1_100_000, MS, 0, 0, 300_000), (1, 200_000));
// A late wait takes the latest tick without waiting again.
assert_eq!(event_after(2_400_000, MS, 0, 0, 300_000), (2, 0));
// A kind already woken on a tick cannot wake on it again, offset or
// no offset.
assert_eq!(event_after(250_000, MS, 0, 1, 0), (2, 1_750_000));
assert_eq!(event_after(250_000, MS, 0, 1, 300_000), (2, 2_050_000));
// The clock's first tick is carried through interval changes.
assert_eq!(event_after(250_000, MS, 100, 100, 0), (101, 750_000));
// An offset behind now never makes the wait negative.
assert_eq!(event_after(900_000, MS, 0, 0, 0).1, 100_000);
}

fn scheduling_stores_the_offset_for_a_kind() {
let null = std::ptr::null();
let list = std::ptr::null::<u8>();
assert_eq!(schedule(null, 2, 700, list, 0), S_OK);
assert_eq!(OFFSETS[2].load(Ordering::Relaxed), 700_000);
// An unknown kind shares the last slot but still accepts the call.
assert_eq!(schedule(null, 99, 12, list, 0), S_OK);
assert_eq!(OFFSETS[2].load(Ordering::Relaxed), 12_000);
schedule(null, 2, 0, list, 0);
}

#[test]
fn waits_end_on_ticks_and_count_frames() {
let null = std::ptr::null();
Expand Down Expand Up @@ -273,6 +326,9 @@ mod tests {
assert!(late >= flips[3] + 2, "{late} after {}", flips[3]);
assert_eq!(wait(null, FLIP, u32::MAX, std::ptr::null(), 0, &mut token), S_OK);
assert_eq!(LAST[2].load(Ordering::Relaxed), late + 1);
// The scheduled offset is global too. Do not change it during the
// flip waits above.
scheduling_stores_the_offset_for_a_kind();
}

/// Two waits for one kind that begin together (two threads) take two
Expand Down