Skip to content

GPU: end a late flip wait on the tick it missed, not the next - #319

Open
xmvo wants to merge 1 commit into
Marvisak:mainfrom
xmvo:gpu/late-flip-waits
Open

xmvo wants to merge 1 commit into
Marvisak:mainfrom
xmvo:gpu/late-flip-waits

Conversation

@xmvo

@xmvo xmvo commented Nov 24, 2026 •

Copy link
Copy Markdown

What

frame::next_tick always returned the next tick after now. When a frame took a little longer than one interval, its flip wait started just after the tick it should have flipped on, and so waited for the whole following interval. Frame times rounded up to whole intervals: at a 33.3 ms interval, a 40 ms frame was shown at 66.7 ms (30 fps falling straight to 15, then 10).

Now a non-origin wait (flip, vblank) that begins after a tick nobody has taken yet ends immediately, on that tick. A late frame costs only its own lateness:

  • 40 ms of work: about 25 fps instead of 15,
  • on-time frames still wait for their tick, so the rate never exceeds the interval,
  • each tick is still taken once (last(kind) advances to it),
  • origin waits are unchanged, so the title's "token on time-out" origin poll behaves as before.

Why

A user on slower hardware saw menus at 15 fps and frame times clustered at exactly 66/100 ms, which matches this rounding.

Testing

  • cargo test -p victory-gpu frame: passes, with a new case added to waits_end_on_ticks_and_count_frames: a flip wait begun 25 ms after a flip at a 20 ms interval returns at once, on the next tick.
  • Not yet measured in the title. A before/after fps reading on a machine whose menus run below 30 would confirm the gain.

Open to gating this behind a flag if you'd rather keep strict tick pacing by default.

A flip (or vblank) wait always ended on the next tick after now. A frame
that took a little longer than an interval therefore waited for the
whole following interval: at 30 fps, a 40 ms frame was shown after
66 ms, and frame times rounded up to whole intervals (30 fps falling
straight to 15, then 10). A non-origin wait that begins after a tick
nobody has taken now ends at once on that tick; on-time frames are
paced as before. Origin waits are unchanged.
@xmvo
xmvo force-pushed the gpu/late-flip-waits branch from bc05ba0 to b36bd4c Compare November 24, 2026 02:17
muhammed-aksoy pushed a commit to muhammed-aksoy/VIctory that referenced this pull request Nov 24, 2026
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…he next

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…d, not the next"

Its test fails together with 810f442 (a frame wait takes its tick before it sleeps) from another merged PR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
… the #319 revert)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremygold02

Copy link
Copy Markdown
Contributor

Tested PR #319 at head b36bd4c1d655f4d2f2069e4369a402a2828091e2 (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at b36bd4c1d655f4d2f2069e4369a402a2828091e2.

  • cargo test --locked -p victory-gpu frame::tests — passed (ok. 1 passed; 0 failed; 0 ignored; 0 measured; 43 filtered out; finished in 0.07s ); cases: frame::tests::waits_end_on_ticks_and_count_frames.

Behavior covered: Checks the frame timing tests for late flip waits ending on the missed tick instead of waiting another interval.
Code: crates/victory-gpu/src/frame.rs

@etcetera-yt

Copy link
Copy Markdown

Each PR passes on its own; merged together on main (d2a4bae), the frame-wait test fails:

main + #319:        frame::tests::waits_end_on_ticks_and_count_frames ... ok
main + #174 + #319: frame::tests::waits_end_on_ticks_and_count_frames ... FAILED
  crates\victory-gpu\src\frame.rs:252:9  left: 10  right: 11

Both change when a frame wait ends: #174 takes the wait's tick before it sleeps, #319 ends a late flip wait on
the tick it missed rather than the next. Together a frame is counted one tick short. Whichever lands second
probably needs to rebase its tick accounting (and the test's expected count) on the other.

Merged with plain git merge, no text conflict. Windows 10 19045, RTX 5060 Ti, Rust 1.98.1,
cargo test -p victory-gpu -- --test-threads=4 waits_end.

AI use: Claude Code, Claude Opus 5.5.

On a title (added after a local run)

With #174 and #319 both merged (plus #175 for Windows 10), the title never left its first black screen and the
host presented about 1,100 frames per second at 3 draws per frame. The same build with only #319 reverted runs at
30.0 fps and reaches the start screen. (RTX 5060 Ti, driver 621.14, Windows 10 19045, release build.)

@ghost ghost mentioned this pull request Nov 24, 2026
@KevinPratap

Copy link
Copy Markdown
Contributor

A fix for the clash with #174 reported above. With #174, a wait takes its tick inside next_tick (before it sleeps) instead of after, so this PR's late-flip early return has to take its tick there too; otherwise nobody records it and the next flip ends on the same tick again:

     if kind != ORIGIN && passed > last_tick && passed > c.first {
+        // Taken now, as any wait takes its tick before it would sleep.
+        last(kind).fetch_max(passed, Ordering::Relaxed);
         return (passed, Duration::ZERO);
     }

Verified on main (d2a4bae) + #174 (810f442) + #319 (b36bd4c), Windows 11:

before:  cargo test -p victory-gpu --lib frame::   1 passed; 1 failed (waits_end_on_ticks_and_count_frames)
after:   cargo test -p victory-gpu --lib frame::   2 passed; 0 failed

On this branch alone the line is harmless (the caller's later fetch_max takes the same tick), so it can go into whichever of the two lands second. AI-assisted.

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested b36bd4c on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (264 passed, 0 failed). I ran frame:: 5 more times on its own and it passed every time, so the sleep-based timing in the new test looks stable here. Not tested in game.

  • The rounding bug is real. elapsed / interval + 1 always picks the tick after now, so a frame that misses its tick by a little waits a whole extra interval.
  • The new early return only applies to non-origin waits (flip and vblank), only when the most recently passed tick hasn't been taken (passed > last_tick), and never for the first tick. Each tick is still consumed once, and an on-time frame still waits, so the rate cap holds.
  • This changes pacing from strict tick-aligned to "present late frames on the missed tick". That matches how a console flip queue behaves when a frame misses vsync by less than an interval (no tearing here, since we present on our own schedule). It looks like the right trade.

No issues found.

AI-assisted (Claude Code).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
… it missed, not the next

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mercuryVM

Copy link
Copy Markdown

Heads-up: #174 changes next_tick so the tick is taken inside it rather than in wait. With both PRs applied, this PR's early-return branch never takes its tick, and frame::tests::waits_end_on_ticks_and_count_frames fails at the LAST == flipped + 1 check (verified by merging both). Whichever lands second should add last(kind).fetch_max(passed, Ordering::Relaxed); before the early return (passed, Duration::ZERO). Details are in my review on #174.

AI-assisted (Claude Code).

@alexdeliu

Copy link
Copy Markdown
Contributor

Heads-up: #319 (b36bd4c1) and #174 (882664e1) each pass on main, but merged together frame::tests::waits_end_on_ticks_and_count_frames fails on every run (left: 10, right: 11 at frame.rs:254).

#174 moves taking the tick (last(kind).fetch_max) into next_tick. #319 adds an early return (passed, Duration::ZERO) for a late flip wait, before that point, so the late wait's tick is never taken. Taking it in the early return too fixes it. The tests pass (710 in the merge train):

if kind != ORIGIN && passed > last_tick && passed > c.first {
    last(kind).fetch_max(passed, Ordering::Relaxed);
    return (passed, Duration::ZERO);
}

Whichever of the two lands second needs this. It is in e3f72c57 on the train branch.

@fiestorra

Copy link
Copy Markdown
Contributor

The clash is real on this head, and here is a patch that fixes it

Chasing TEST-FRAME-WAITS in #1633 (find which pull request this test combines badly with) landed on this pair, and the answer is #174 — as @etcetera-yt, @mercuryVM, @KevinPratap and @alexdeliu already reported above. What I can add is the measured form and a fix that I ran.

Reproduction, on the forge train (KevinPratap/VIctory forge/main, which merges #174 as 5c8275a and this pull request as 07a6fc0, so its tree is ffe310e + #174 + #319):

$ cargo test -p victory-gpu --locked waits_end_on_ticks_and_count_frames
panicked at crates\victory-gpu\src\frame.rs:254:
assertion `left == right` failed
  left: 8
 right: 9

Ten runs out of ten, so it is not the test's timing being flaky. The same command at ffe310e passes five out of five, and at 07a6fc0 fails five out of five: this pull request's merge in forge already carries #174, and the clash appears there.

Why. #174 moves the "take the tick" from wait() into next_tick(). This pull request's late-flip early return

    if kind != ORIGIN && passed > last_tick && passed > c.first {
        return (passed, Duration::ZERO);
    }

returns before that, so a wait that ends on the tick it missed never records it, LAST[2] stays where it was, and the assertion this pull request adds (LAST[2] == flipped + 1) fails with 8 against 9. The pull request's own doc comment already says what should happen: "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."

Patch (against b36bd4c1, the current head, in crates/victory-gpu/src/frame.rs):

     if kind != ORIGIN && passed > last_tick && passed > c.first {
+        // Taken here as the blocking path does: the wait that ends on this
+        // tick is the one that counted it.
+        last(kind).fetch_max(passed, Ordering::Relaxed);
         return (passed, Duration::ZERO);
     }

Verified with it applied:

  • waits_end_on_ticks_and_count_frames → 5 passes out of 5 (0 out of 5 without it).
  • waits_begun_together_take_different_ticks, the other test this pull request adds → still passes.
  • cargo test -p victory-gpu --locked on the patched forge tree → 88 passed, 0 failed, 3 ignored.

The two waits that begin together still take different ticks, because the blocking path takes its tick under the same lock; only the late path, which returned without taking one, changes.

No game data is involved; this is a unit test of the frame clock.

Written with an AI assistant.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This pull request no longer merges cleanly into main (conflicts in crates/victory-gpu/src/frame.rs). It will be reviewed once it is rebased onto the current main. The earlier verdict (needs-work) was for an older state of main.


Generated by Claude Code

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo clippy -p victory-gpu --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)
  • Test failure: cargo test -p victory-gpu --lib frame:: fails in frame::tests::waits_end_on_ticks_and_count_frames at crates/victory-gpu/src/frame.rs:256:9:
assertion `left == right` failed
  left: 8
 right: 9

Holding off approval until waits_end_on_ticks_and_count_frames passes cleanly on current main.

AI-assisted (Gemini 4 Neon).

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
@oddmaniv

Copy link
Copy Markdown

Additional synthetic-clock regression coverage tested against this PR at b36bd4c1d655f4d2f2069e4369a402a2828091e2 checks multi-tick late flip/vblank waits, future origin waits, and that consumed ticks are not reused. The added test runs in an isolated process and leaves this PR's production code unchanged.

The new regression fails on unchanged main (tick 106 instead of 105) and passes with this PR. Both frame tests passed together twice; workspace Clippy with and without all features, changed-file formatting and asset policy passed on the test-instrumented PR checkout, on Windows with Rust/Cargo 1.98.1.

This is additional regression coverage, not evidence of console late-flip behaviour, gameplay performance, or resolution of the #174 interaction. No full workspace suite, game or GPU execution was performed. AI-assisted.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I checked the late-flip/vblank case again on main at 63bcca4. The standalone Rust test compiled the original frame module from each revision and set the clock to the same position: tick 105 had passed, tick 106 was still in the future, and the last consumed tick was 103.

On 2a4938e (before 9c179ea), both zero-timeout polls chose tick 106 and consumed nothing. On 63bcca4, the first poll takes tick 105 without waiting. The second poll chooses tick 106 and leaves the consumed tick at 105. The result is the same for flip and vblank. This also checks the reservation mentioned in the #174 discussion above.

The late flip/vblank fix is already present in main through 9c179ea. The current implementation also allows origin polls to take a passed tick. #2877 has separate virtual-clock coverage of that origin behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.