Skip to content

GPU: a frame wait takes its tick before it sleeps - #174

Merged
Marvisak merged 2 commits into
Marvisak:mainfrom
AndyLatimer:fix/gpu-frame-wait-reserves-tick
Nov 25, 2026
Merged

Marvisak merged 2 commits into
Marvisak:mainfrom
AndyLatimer:fix/gpu-frame-wait-reserves-tick

Conversation

@AndyLatimer

Copy link
Copy Markdown
Contributor

The frame.rs module doc says each event kind (origin, vblank, flip) happens once per tick, so two waits for the same kind never end on the same tick. wait picked the next tick in next_tick under the clock's lock, but marked it as taken (LAST[kind]) only after std::thread::sleep, with the lock released. Two threads waiting for one kind at the same time therefore both read the same LAST, both got tick T, and both returned S_OK on it. For the origin, that begins two frames (FRAME += 2) in one interval.

next_tick now takes a limit (the wait's time-out). When the tick comes within it, next_tick records the tick under the clock's lock, before anyone sleeps. A wait that times out takes no tick, as before. Sequential waits behave as before.

Testing

  • cargo test --workspace: 265 passed, 4 ignored (need external files). Includes 1 new test, waits_begun_together_take_different_ticks: two next_tick calls before any sleep get different ticks, and a timed-out call leaves LAST alone. It fails on the old behaviour ("1 after 1"), checked by putting it back.
  • cargo clippy --workspace --all-targets: no new warnings. The two that remain (xmem.rs and host.rs:267) are on main.
  • cargo xtask policy: pass. rustfmt --check on frame.rs: clean.
  • Tested with a synthetic unit test only (not run against a title).

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@DaviTostes

Copy link
Copy Markdown

Tested 810f442 under Wine 11.22 + vkd3d-proton 3.1 on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with cargo xwin ... --target x86_64-pc-windows-msvc and a native Microsoft d3dcompiler_47: pass, no regression.

No game data used.

@Sunkiller-qc

Copy link
Copy Markdown

The diagnostics build no longer compiles with this change. wait now drops the tick (let (_, wait) = next_tick(kind, limit); at crates/victory-gpu/src/frame.rs:133), but the #[cfg(feature = "diagnostics")] tracing::debug! further down still logs tick (frame.rs:156), so the diag build fails with E0425. Keeping the binding (let (tick, wait) = ...) fixes it. The logic change itself looks right to me.

Static review, not built or run.

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

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Review note: merged onto current main (d2a4bae), the default build is fine but the diagnostics build does not compile:

error[E0425]: cannot find value `tick` in this scope
   --> crates\victory-gpu\src\frame.rs:156:9

(cargo check -p victory-gpu --features diagnostics; main alone builds with that feature.) wait no longer binds tick, and the tracing call under diagnostics still names it. Testers use the diagnostics build for device-removal reports, so it is worth fixing before this goes in.

From building only; I have no copy of the title.

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 <noreply@anthropic.com>
@AndyLatimer

Copy link
Copy Markdown
Contributor Author

Thanks @Sunkiller-qc and @r4tedx, you are right. Fixed on the new head 882664e: wait binds the tick again for the diagnostics log (with unused_variables allowed when the feature is off). cargo check -p victory-gpu now passes with and without --features diagnostics, no warnings, and the frame tests pass.

@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 882664e on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (265 passed, 0 failed), including waits_begun_together_take_different_ticks.

The race is real. next_tick chose the tick under the clock lock, but LAST was only bumped after the sleep, with the lock released, so two concurrent waits for one kind could both end on tick T (two FRAME increments for the origin). Taking the tick inside next_tick when wait <= limit, and taking none on a time-out, fixes it.

Interaction with #319, which touches the same function. #319 adds an early return to next_tick for a late non-origin wait (return (passed, Duration::ZERO)) and relies on wait to mark that tick afterwards. This PR removes the mark in wait and adds its own mark only at the end of next_tick, so with both applied the early-return path never takes its tick. Merging this onto a branch with #319 confirms it: frame::tests::waits_end_on_ticks_and_count_frames fails at frame.rs:254 (the LAST == flipped + 1 check). Git reports no textual conflict, so this would slip through.

Fix for whichever lands second: in #319's early-return branch, take the tick before returning, i.e. last(kind).fetch_max(passed, Ordering::Relaxed); return (passed, Duration::ZERO);. Its limit check is trivially true, since the wait is zero.

Approving this PR's own change. I have not merged it into our fork, which already has #319.

AI-assisted (Claude Code).

nazmi pushed a commit to nazmi/VIctory that referenced this pull request Nov 24, 2026
Applied as the net diff of upstream PR Marvisak#174 (head 882664e), 2 commits: 882664e,810f4422.
yourmomsbasement pushed a commit to yourmomsbasement/VIctory_Dev that referenced this pull request Nov 24, 2026
…ied together

Merges Marvisak#72 Marvisak#347 Marvisak#1000 Marvisak#358 Marvisak#352 Marvisak#626 Marvisak#1152 Marvisak#1129 Marvisak#1203 Marvisak#24 Marvisak#642 Marvisak#427 Marvisak#485
Marvisak#550 Marvisak#174 Marvisak#1279 onto main (ffe310e), each head unchanged, no conflicts.

One change of mine: three of the PRs each add a `mod tests` at the end of
pipeline/host.rs, so the merge defines `tests` three times (E0428). The
second and third are named `library_tests` and `blend_tests`; nothing else
changes.

cargo clippy --workspace --all-targets --locked -- -D warnings, with and
without --all-features, is clean and cargo test --workspace --locked passes
(367 passed, 6 ignored; main has 342 passed, 6 ignored).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…ame wait bundle

Taken from the bundle: 64 MB stacks for pipeline workers (#1129), no
blending on integer render targets (#1152), root signature flag fallback
(#626), read-only depth read states (#485, on our stored rtvs/dsv and
bind_targets), enhanced texture barrier translation (#642, on our
recording_list so continuation segments get it), indirect mesh dispatch
(#24), the frame wait taking its tick before it sleeps (#174), and the
make.rs unit tests (#1203).

Kept from our side:
- pipeline/make.rs: early() starts every compute pipeline at creation
  (b4e8419); the per-pipeline wake pair, per-frame budget, wait_compute
  and copystat counters. The bundle's the_wait_budget_is_spent_once_per_frame
  test is dropped (written for its fixed BUDGET; our FrameBudget tests
  cover it); a_pipeline_is_made_once now holds the tests' serial lock.
- indirect.rs: our whole file (#40's overflow-safe clamp, DXC fallback for
  Wine, rectangle pass). #1279's structured-buffer clamp and its readback
  test (written for the old bound/done API) are left out.
- draw.rs: NOT_MADE counter, user_dword, typed table nulls
  (table_view_props, mirror_for_draw); the depth test calls readable()
  directly since our tables() needs a host pipeline.
- frame.rs: our `_tick` (the diagnostics log already names it).
- list.rs: execute_indirect's DISPATCH_SINGLE mapping in one place; our
  tests beside the bundle's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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.

@Marvisak
Marvisak merged commit 8a772ea into Marvisak:main Nov 25, 2026

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. In crates/victory-gpu/src/frame.rs, wait chose its tick in next_tick under the clock lock but recorded it in LAST only after sleeping, so two threads waiting for the same kind could both end on one tick (for the origin wait, two frames in one interval). next_tick now takes the time-out and records the tick under the lock when the wait will not time out; timed-out waits still take no tick, and single-thread behaviour is unchanged. The diagnostics build issue raised in review is fixed on this head, and the new test fails on the old order.

Note for whoever merges: #319 adds an early return in next_tick that would also need to record its tick, so whichever lands second needs that line.

CI: no check runs on the head commit.


Generated by Claude Code

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.

7 participants