GPU: with VICTORY_FPS set, a present waits for the next frame's deadline #2746

Open
github-bot wants to merge 2 commits from github-bot/gh-2746 into main AGit
Owner

@⁠kais7654 on GitHub · 2026-11-25 19:29 UTC · original
kais7654:display/present-pacing → main · +199 −0 · 4 files · 2 commits · open

What

Adds an opt-in present pacer. When the environment variable VICTORY_FPS is set to a value between 1 and 1000, the host's present() waits until the next frame's deadline before calling Present(0, ...). Without the variable, nothing changes.

Why

The title currently drives presentation with no way to cap the frame rate, so an uncapped run burns GPU/power for frames that are discarded by the compositor anyway. A present-side cap is the one place every frame goes through, it needs no title cooperation, and it helps with thermals, fan noise and frame-time consistency on laptops.

How

  • New crates/victory-gpu/src/pace.rs (#![cfg(windows)]): Pacer::from_env() reads VICTORY_FPS and validates the range (1–1000; an invalid value falls back to no pacing).
  • Waiting uses a high-resolution waitable timer (CreateWaitableTimerExW with CREATE_WAITABLE_TIMER_HIGH_RESOLUTION), then spins over the last ~2 ms so the present lands close to the deadline without busy-waiting the whole interval.
  • The first present of a chain starts it without waiting; each later present waits for its deadline. If a present is late, the chain restarts from that present — no catch-up burst of frames.
  • The wait happens on the title's thread inside host.present(), which is where the frame boundary already is, so no new threads and no change to the frame pipeline.
  • README documents VICTORY_FPS next to the other environment variables.

Testing

  • 3 new unit tests in pace.rs (period parsing/validation, deadline chain, late-present restart) pass.
  • cargo clippy --workspace --all-targets --locked -- -D warnings clean (standard and --all-features variants).
  • cargo test -p victory-gpu: 196 passed; the 2 remaining failures are the known heap-cap pair from #1943 (NVIDIA-specific on this machine, unrelated to this diff).

AI-assisted: implemented with Mistral Vibe.

**@⁠kais7654** on GitHub · 2026-11-25 19:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/2746) `kais7654:display/present-pacing` → `main` · +199 −0 · 4 files · 2 commits · open ## What Adds an opt-in present pacer. When the environment variable `VICTORY_FPS` is set to a value between 1 and 1000, the host's `present()` waits until the next frame's deadline before calling `Present(0, ...)`. Without the variable, nothing changes. ## Why The title currently drives presentation with no way to cap the frame rate, so an uncapped run burns GPU/power for frames that are discarded by the compositor anyway. A present-side cap is the one place every frame goes through, it needs no title cooperation, and it helps with thermals, fan noise and frame-time consistency on laptops. ## How - New `crates/victory-gpu/src/pace.rs` (`#![cfg(windows)]`): `Pacer::from_env()` reads `VICTORY_FPS` and validates the range (1–1000; an invalid value falls back to no pacing). - Waiting uses a high-resolution waitable timer (`CreateWaitableTimerExW` with `CREATE_WAITABLE_TIMER_HIGH_RESOLUTION`), then spins over the last ~2 ms so the present lands close to the deadline without busy-waiting the whole interval. - The first present of a chain starts it without waiting; each later present waits for its deadline. If a present is late, the chain restarts from that present — no catch-up burst of frames. - The wait happens on the title's thread inside `host.present()`, which is where the frame boundary already is, so no new threads and no change to the frame pipeline. - README documents `VICTORY_FPS` next to the other environment variables. ## Testing - 3 new unit tests in `pace.rs` (period parsing/validation, deadline chain, late-present restart) pass. - `cargo clippy --workspace --all-targets --locked -- -D warnings` clean (standard and `--all-features` variants). - `cargo test -p victory-gpu`: 196 passed; the 2 remaining failures are the known heap-cap pair from #1943 (NVIDIA-specific on this machine, unrelated to this diff). AI-assisted: implemented with Mistral Vibe.
Every present is shown at once (Present(0), so no sync interval waits),
and the title's frame loop then makes frames as fast as the GPU allows:
a strong card runs flat out the whole play, a laptop throttles on its
thermals mid-scene, and the frame times jitter while it does. An option
is one wait on the title's presenting thread, which the flip wait holds
anyway: `VICTORY_FPS=30` (the console's own cadence, say) holds the
present until the frame's deadline it starts a cadence chain from, a
late present restarts the chain instead of bursting to catch up, and a
high-resolution waitable timer makes the wake exact (a plain sleep plus
a spin makes up for one the OS does not make). Unset, nothing waits
that did not before.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
A failed `SetWaitableTimer` left the fallback sleep skipped, and waits
then span the frame's whole rest. `pace::sleep_until`'s fallback
condition takes the disjunction: a failed `SetWaitableTimer` (or
`WaitForSingleObject`) sleeps the interval, as no timer does. Found in
review on #2746 by fuzeofficial (verified on RTX 5090 and WARP), on
`pace.rs`:64.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:13:17 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 20:07 UTC · original

Solid pacing: the deadline chain restarts from now on a late frame (next.max(now)) so presents never burst to catch up, the first present doesn't wait, and the 2 ms spin tail after a high-res timer wait hits the deadline tightly. Clamping to 1..=1000 fps and warning on bad input is tidy. Waiting before Present(0) on the title's thread is exactly where this belongs.

**@⁠kotekompotek** on GitHub · 2026-11-25 20:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/2746#issuecomment-7173220682) Solid pacing: the deadline chain restarts from now on a late frame (next.max(now)) so presents never burst to catch up, the first present doesn't wait, and the 2 ms spin tail after a high-res timer wait hits the deadline tightly. Clamping to 1..=1000 fps and warning on bad input is tidy. Waiting before Present(0) on the title's thread is exactly where this belongs.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 20:14 UTC · original

Reviewed and verified 1f32381f (merged cleanly onto main 13f91ca) on Windows 11 (RTX 5090 + WARP).

Code review

  • Fast path & pacing math look clean: when VICTORY_FPS is unset or invalid, Pacer stores period: None and wait() returns immediately without calling Instant::now() or creating a timer. period_of rejects 0, negative, non-integer, and > 1000 values with a warning; next.max(Instant::now()) prevents catch-up bursts after long stalls while keeping deadline spacing steady when frames arrive on time; Host::present holds the presenter mutex so concurrent presents are serialized without blocking render queues.
  • One bug in pace.rs:64-75 fallback condition:
    let on = SetWaitableTimer(...) != 0;
    if on && WaitForSingleObject(...) == WAIT_FAILED {
        std::thread::sleep(left - SPIN);
    }
    
    When SetWaitableTimer fails (on == false), on && ... is false, so the fallback std::thread::sleep(left - SPIN) is skipped and wait() busy-spins through the entire remaining frame time. This should be if !on || WaitForSingleObject(...) == WAIT_FAILED.

Verification (Windows 11, RTX 5090 + WARP)

  • RTX 5090 (--features diagnostics): pace::tests 3/3 passed, draw::tests --test-threads=1 11/11 passed.
  • WARP (VICTORY_D3D12_WARP=1, --features diagnostics --lib -- --test-threads=1): 201 passed, 0 failed, 1 ignored.
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings (default & --features diagnostics): 0 warnings; cargo xtask policy: 185 files ok; clean git merge-tree against 13f91ca and #2752.
**@⁠fuzeofficlal** on GitHub · 2026-11-25 20:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/2746#issuecomment-7173381202) Reviewed and verified `1f32381f` (merged cleanly onto `main` `13f91ca`) on Windows 11 (RTX 5090 + WARP). ### Code review - **Fast path & pacing math look clean**: when `VICTORY_FPS` is unset or invalid, `Pacer` stores `period: None` and `wait()` returns immediately without calling `Instant::now()` or creating a timer. `period_of` rejects `0`, negative, non-integer, and `> 1000` values with a warning; `next.max(Instant::now())` prevents catch-up bursts after long stalls while keeping deadline spacing steady when frames arrive on time; `Host::present` holds the `presenter` mutex so concurrent presents are serialized without blocking render queues. - **One bug in `pace.rs:64-75` fallback condition**: ```rust let on = SetWaitableTimer(...) != 0; if on && WaitForSingleObject(...) == WAIT_FAILED { std::thread::sleep(left - SPIN); } ``` When `SetWaitableTimer` fails (`on == false`), `on && ...` is `false`, so the fallback `std::thread::sleep(left - SPIN)` is skipped and `wait()` busy-spins through the entire remaining frame time. This should be `if !on || WaitForSingleObject(...) == WAIT_FAILED`. ### Verification (Windows 11, RTX 5090 + WARP) - **RTX 5090 (`--features diagnostics`)**: `pace::tests` **3/3 passed**, `draw::tests --test-threads=1` **11/11 passed**. - **WARP (`VICTORY_D3D12_WARP=1`, `--features diagnostics --lib -- --test-threads=1`)**: **201 passed, 0 failed, 1 ignored**. - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` (default & `--features diagnostics`): **0 warnings**; `cargo xtask policy`: **185 files ok**; clean `git merge-tree` against `13f91ca` and `#2752`.
Author
Owner

@⁠kais7654 on GitHub · 2026-11-25 20:27 UTC · original

Reviewed and verified 1f32381f (merged cleanly onto main 13f91ca) on Windows 11 (RTX 5090 + WARP).

Code review

  • Fast path & pacing math look clean: when VICTORY_FPS is unset or invalid, Pacer stores period: None and wait() returns immediately without calling Instant::now() or creating a timer. period_of rejects 0, negative, non-integer, and > 1000 values with a warning; next.max(Instant::now()) prevents catch-up bursts after long stalls while keeping deadline spacing steady when frames arrive on time; Host::present holds the presenter mutex so concurrent presents are serialized without blocking render queues.

  • One bug in pace.rs:64-75 fallback condition:

    let on = SetWaitableTimer(...) != 0;
    if on && WaitForSingleObject(...) == WAIT_FAILED {
        std::thread::sleep(left - SPIN);
    }
    

    When SetWaitableTimer fails (on == false), on && ... is false, so the fallback std::thread::sleep(left - SPIN) is skipped and wait() busy-spins through the entire remaining frame time. This should be if !on || WaitForSingleObject(...) == WAIT_FAILED.

Verification (Windows 11, RTX 5090 + WARP)

  • RTX 5090 (--features diagnostics): pace::tests 3/3 passed, draw::tests --test-threads=1 11/11 passed.
  • WARP (VICTORY_D3D12_WARP=1, --features diagnostics --lib -- --test-threads=1): 201 passed, 0 failed, 1 ignored.
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings (default & --features diagnostics): 0 warnings; cargo xtask policy: 185 files ok; clean git merge-tree against 13f91ca and #2752.

@⁠fuzeofficlal Fixed in 9d70ef7: the fallback condition now takes the disjunction — if !on || WaitForSingleObject(...) == WAIT_FAILED — so a failed arm of SetWaitableTimer sleeps the remaining interval instead of leaving the spin to cover the whole frame's rest. cargo clippy -p victory-gpu --all-targets --locked -- -D warnings clean (default and diagnostics), the pace tests pass. Thanks for the catch and the verification.

AI-assisted (Mistral Vibe).

**@⁠kais7654** on GitHub · 2026-11-25 20:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/2746#issuecomment-7173692996) > Reviewed and verified `1f32381f` (merged cleanly onto `main` `13f91ca`) on Windows 11 (RTX 5090 + WARP). > > ### Code review > * **Fast path & pacing math look clean**: when `VICTORY_FPS` is unset or invalid, `Pacer` stores `period: None` and `wait()` returns immediately without calling `Instant::now()` or creating a timer. `period_of` rejects `0`, negative, non-integer, and `> 1000` values with a warning; `next.max(Instant::now())` prevents catch-up bursts after long stalls while keeping deadline spacing steady when frames arrive on time; `Host::present` holds the `presenter` mutex so concurrent presents are serialized without blocking render queues. > * **One bug in `pace.rs:64-75` fallback condition**: > ```rust > let on = SetWaitableTimer(...) != 0; > if on && WaitForSingleObject(...) == WAIT_FAILED { > std::thread::sleep(left - SPIN); > } > ``` > > > > > > > > > > > > When `SetWaitableTimer` fails (`on == false`), `on && ...` is `false`, so the fallback `std::thread::sleep(left - SPIN)` is skipped and `wait()` busy-spins through the entire remaining frame time. This should be `if !on || WaitForSingleObject(...) == WAIT_FAILED`. > > ### Verification (Windows 11, RTX 5090 + WARP) > * **RTX 5090 (`--features diagnostics`)**: `pace::tests` **3/3 passed**, `draw::tests --test-threads=1` **11/11 passed**. > * **WARP (`VICTORY_D3D12_WARP=1`, `--features diagnostics --lib -- --test-threads=1`)**: **201 passed, 0 failed, 1 ignored**. > * `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` (default & `--features diagnostics`): **0 warnings**; `cargo xtask policy`: **185 files ok**; clean `git merge-tree` against `13f91ca` and `#2752`. @⁠fuzeofficlal Fixed in 9d70ef7: the fallback condition now takes the disjunction — `if !on || WaitForSingleObject(...) == WAIT_FAILED` — so a failed arm of `SetWaitableTimer` sleeps the remaining interval instead of leaving the spin to cover the whole frame's rest. `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` clean (default and diagnostics), the pace tests pass. Thanks for the catch and the verification. AI-assisted (Mistral Vibe).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 20:28 UTC · original

Checked 9d70ef7a — if !on || WaitForSingleObject(...) == WAIT_FAILED resolves the fallback condition cleanly. Looks good to me.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 20:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/2746#issuecomment-7173732728) Checked `9d70ef7a` — `if !on || WaitForSingleObject(...) == WAIT_FAILED` resolves the fallback condition cleanly. Looks good to me.
Author
Owner

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

Automated triage for the maintainers. This PR conflicts with current main in crates/victory-gpu/src/host.rs. It will be reviewed once it is rebased.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 23:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/2746#issuecomment-7177538018) <!-- victory-triage v1 head=9d70ef7a565830125dab57562a68422bb60ee14b verdict=needs-rebase at=2026-11-25T23:25:13Z --> Automated triage for the maintainers. This PR conflicts with current `main` in `crates/victory-gpu/src/host.rs`. It will be reviewed once it is rebased. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠steven-seagull27 on GitHub · 2026-11-26 02:04 UTC · original

@⁠kais7654 I resolved the two Presenter conflicts against current main. Commit 55b0a185 has your exact 9d70ef7a as first parent and 63bcca43 as second parent. It keeps both the pacer and the compositor, including the timer-arm fallback fix. To take it on display/present-pacing:

git fetch https://github.com/steven-seagull27/VIctory.git codex/pr2746-present-pacing-63bcca43
git merge --ff-only FETCH_HEAD

I tested the Windows present paths with a hidden swap chain on an RTX 5080, driver 621.40. A local fixture counted Pacer::wait calls and timestamped the start of each DXGI Present. All 188 calls returned S_OK, with exactly one pacer call each. This included single-plane copy, forced compositor failure with primary-plane fallback, and successful two-plane blending. The existing pixel checks passed on both compositor paths. Empty input returned before pacing.

VICTORY_FPS Median interval between DXGI calls First interval after the late frame
unset 6.058 ms 5.812 ms
30 33.333 ms 33.334 ms
60 16.667 ms 16.667 ms
120 8.333 ms 8.333 ms

Each median uses 40 steady-state intervals. I inserted a 120 ms sleep between frames; the late call proceeded and the next interval resumed at target. These are CPU call timestamps on Windows 26300.10075, independent of display scanout.

One timing distinction for the PR description: a late wake inside an already-started wait can shorten the next interval. The 120 FPS run included one 12.886 ms / 3.780 ms pair. In a separate controlled 60 FPS test I held the thread just after sleep_until returned. A 9 ms hold produced 25.85 ms then 7.48 ms; a 40 ms hold produced 56.81 ms then 0.04 ms. The pacer schedules from the saved deadline, and the next call returns immediately if that deadline has already passed. This is separate from the between-frame stall above.

The three stock pacing tests and warning-denied Clippy for victory-gpu --all-targets passed (default and diagnostics). The timing instrumentation stayed local. Tested with Codex assistance.

**@⁠steven-seagull27** on GitHub · 2026-11-26 02:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/2746#issuecomment-7180440386) @⁠kais7654 I resolved the two `Presenter` conflicts against current main. Commit [`55b0a185`](https://github.com/steven-seagull27/VIctory/commit/55b0a185c1d563e639c5a7241dd7828dc23be34e) has your exact `9d70ef7a` as first parent and `63bcca43` as second parent. It keeps both the pacer and the compositor, including the timer-arm fallback fix. To take it on `display/present-pacing`: ```sh git fetch https://github.com/steven-seagull27/VIctory.git codex/pr2746-present-pacing-63bcca43 git merge --ff-only FETCH_HEAD ``` I tested the Windows present paths with a hidden swap chain on an RTX 5080, driver 621.40. A local fixture counted `Pacer::wait` calls and timestamped the start of each DXGI `Present`. All 188 calls returned `S_OK`, with exactly one pacer call each. This included single-plane copy, forced compositor failure with primary-plane fallback, and successful two-plane blending. The existing pixel checks passed on both compositor paths. Empty input returned before pacing. | `VICTORY_FPS` | Median interval between DXGI calls | First interval after the late frame | |---|---:|---:| | unset | 6.058 ms | 5.812 ms | | 30 | 33.333 ms | 33.334 ms | | 60 | 16.667 ms | 16.667 ms | | 120 | 8.333 ms | 8.333 ms | Each median uses 40 steady-state intervals. I inserted a 120 ms sleep **between** frames; the late call proceeded and the next interval resumed at target. These are CPU call timestamps on Windows 26300.10075, independent of display scanout. One timing distinction for the PR description: a **late wake inside an already-started wait** can shorten the next interval. The 120 FPS run included one 12.886 ms / 3.780 ms pair. In a separate controlled 60 FPS test I held the thread just after `sleep_until` returned. A 9 ms hold produced 25.85 ms then 7.48 ms; a 40 ms hold produced 56.81 ms then 0.04 ms. The pacer schedules from the saved deadline, and the next call returns immediately if that deadline has already passed. This is separate from the between-frame stall above. The three stock pacing tests and warning-denied Clippy for `victory-gpu --all-targets` passed (default and diagnostics). The timing instrumentation stayed local. Tested with Codex assistance.
Author
Owner

@⁠steven-seagull27 on GitHub · 2026-11-26 02:10 UTC · original

The PR specifies that a late present restarts the chain from that present. I put the late-wake correction in a separate optional commit, 09ccea0, directly on top of the conflict resolution. It schedules the next deadline one period after the wait actually returns. On-time wakes keep their cadence, and the first call still returns immediately.

The new virtual-clock test fails with the original calculation: after a 9 ms overshoot at a 20 ms period, the following frame starts at 60 ms instead of 69 ms. It passes with the fix, including a 40 ms overshoot.

In the Windows 60 FPS timer fixture, the interval immediately following 9 ms / 40 ms injected wake delays was 16.67 ms in both cases, up from 7.48 ms / 0.04 ms. The RTX 5080 hidden swap chain still measured median intervals of 33.3335, 16.6668 and 8.3335 ms at 30, 60 and 120 FPS. All four pacing tests and Clippy (default and diagnostics) passed. The conflict-resolution commit alone is available separately.

To take both commits from display/present-pacing:

git fetch https://github.com/steven-seagull27/VIctory.git codex/pr2746-late-wake
git merge --ff-only FETCH_HEAD

Tested locally with Codex assistance.

**@⁠steven-seagull27** on GitHub · 2026-11-26 02:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/2746#issuecomment-7180528350) The PR specifies that a late present restarts the chain from that present. I put the late-wake correction in a [separate optional commit, `09ccea0`](https://github.com/steven-seagull27/VIctory/commit/09ccea0ba684b54ab5abe4825287f2ba9314d773), directly on top of the conflict resolution. It schedules the next deadline one period after the wait actually returns. On-time wakes keep their cadence, and the first call still returns immediately. The new virtual-clock test fails with the original calculation: after a 9 ms overshoot at a 20 ms period, the following frame starts at 60 ms instead of 69 ms. It passes with the fix, including a 40 ms overshoot. In the Windows 60 FPS timer fixture, the interval immediately following 9 ms / 40 ms injected wake delays was **16.67 ms in both cases**, up from **7.48 ms / 0.04 ms**. The RTX 5080 hidden swap chain still measured median intervals of 33.3335, 16.6668 and 8.3335 ms at 30, 60 and 120 FPS. All four pacing tests and Clippy (default and diagnostics) passed. The [conflict-resolution commit alone](https://github.com/steven-seagull27/VIctory/commit/55b0a185c1d563e639c5a7241dd7828dc23be34e) is available separately. To take both commits from `display/present-pacing`: ```sh git fetch https://github.com/steven-seagull27/VIctory.git codex/pr2746-late-wake git merge --ff-only FETCH_HEAD ``` Tested locally with Codex assistance.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/host.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2746/head:github-bot/gh-2746'
git switch 'github-bot/gh-2746'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2746'
git switch 'github-bot/gh-2746'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2746'
git switch 'github-bot/gh-2746'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2746'
git switch 'main'
git merge --squash 'github-bot/gh-2746'
git switch 'main'
git merge --ff-only 'github-bot/gh-2746'
git switch 'main'
git merge 'github-bot/gh-2746'
git push origin 'main'
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!2746
No description provided.