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

Manually merged
github merged 2 commits from gh-pr/174/head into gh-pr/174/base 2026-11-26 03:53:23 +00:00
Owner

@⁠AndyLatimer on GitHub · 2026-11-24 01:06 UTC · original
AndyLatimer:fix/gpu-frame-wait-reserves-tick → main · +29 −11 · 1 files · 2 commits · merged 2026-11-25 00:18 UTC

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

**@⁠AndyLatimer** on GitHub · 2026-11-24 01:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/174) `AndyLatimer:fix/gpu-frame-wait-reserves-tick` → `main` · +29 −11 · 1 files · 2 commits · merged 2026-11-25 00:18 UTC 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](https://claude.com/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 <[email protected]>
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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:53:21 +00:00
github manually merged commit 882664e13c into gh-pr/174/base 2026-11-26 03:53:23 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:21 UTC · original

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.

  • victory_gpu: 42 passed, 2 failed, 1 ignored, including the 1 test this PR adds.
  • The two failures are the environment-only a_rejected_blob_gives_an_empty_library (vkd3d needs a non-empty blob) and #128.
  • The ignored test is host::batch::texture_checks_per_submission (benchmark).

No game data used.

**@⁠DaviTostes** on GitHub · 2026-11-24 01:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7119023211) 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**. - `victory_gpu`: **42 passed, 2 failed, 1 ignored**, including the 1 test this PR adds. - The two failures are the environment-only `a_rejected_blob_gives_an_empty_library` (vkd3d needs a non-empty blob) and #128. - The ignored test is `host::batch::texture_checks_per_submission` (benchmark). No game data used.
Author
Owner

@⁠Sunkiller-qc on GitHub · 2026-11-24 01:29 UTC · original

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.

**@⁠Sunkiller-qc** on GitHub · 2026-11-24 01:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7119149839) 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.
Author
Owner

@⁠r4tedx on GitHub · 2026-11-24 03:29 UTC · original

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.

**@⁠r4tedx** on GitHub · 2026-11-24 03:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7121151889) 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.
Author
Owner

@⁠AndyLatimer on GitHub · 2026-11-24 07:46 UTC · original

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.

**@⁠AndyLatimer** on GitHub · 2026-11-24 07:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7125350518) 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.
Author
Owner

@⁠alexdeliu on GitHub · 2026-11-24 18:45 UTC · original

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.

**@⁠alexdeliu** on GitHub · 2026-11-24 18:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7141117307) 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): ```rust 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`](https://github.com/alexdeliu/VIctory/commit/e3f72c5768173be7a1cf44ceaecffc3a685d6b56) on the train branch.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:21 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 00:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/174#issuecomment-7148416699) <!-- victory-triage v1 head=882664e13cfa557fb46d41e182c3267b01097485 verdict=merge-candidate at=2026-11-25T00:18:15Z --> 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](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!174
No description provided.