fix(videoout): pace vblanks with a high-resolution timer #1472

Merged
popcoder565 merged 1 commit from upstream-pr/1472 into main 2026-10-08 14:13:50 +00:00
popcoder565 commented 2026-10-08 08:30:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T08:30:57Z.


What

On Windows, vblanks did not come every 16.68 ms. Over 3 × 240 vblanks, intervals ran from 2 ms to 34 ms, and about three quarters were more than 1 ms off the period.

The vblank thread waited for each deadline with condition_variable::wait_until. With MinGW, that goes through winpthreads, which ends a timed wait only once GetTickCount64() has advanced by the timeout. GetTickCount64 moves in 15.6 ms steps even with the 0.5 ms tick Time.cpp sets, so a wait ended on the first tick step after its deadline. When one step passed two deadlines, two vblanks fired back to back. Details and the other host waits this affects are in #1471.

The thread now sleeps to each deadline with PreciseSleepUs (libc/include/PreciseWait.hpp). That uses a high-resolution waitable timer on Windows and this_thread::sleep_for elsewhere. Deadlines are still counted from the start, so the rate stays 59.94 Hz.

A stop request or a presentation failure is now seen when the current sleep ends, at most one vblank later, instead of waking the thread at once. Shutdown can therefore take up to 16.7 ms longer.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Intel Core i7-12700H, RTX 3060 Laptop.

  • I added a mode to video_out_flip_tests locally (not in this PR). It opens a port, times 240 returns of sceVideoOutWaitVblank with steady_clock, and was run 3 times on each build:

    min p10 median p90 max > 1 ms off 16.68 ms
    main d549f768 2.0 to 3.8 ms 11.0 to 11.4 ms 16.1 to 16.5 ms 21.8 to 25.0 ms 32.5 to 34.0 ms 171 to 186 of 240
    This change 15.5 ms 15.9 to 16.0 ms 16.7 ms 17.2 to 17.4 ms 17.6 to 17.8 ms 1 to 7 of 240
  • video_out_flip_tests (not in ctest, #1040), run from a directory with an app0/sce_sys/param.json, gives the same results with and without this change:

    • decode, controls, present, backtoback, dcc, pacing and aftergpu pass.
    • unavailable fails ("failed presentation was marked complete or retained its buffer").
    • lifetime and reopen terminate ("port is closed").
  • Full ctest: 419 of 421 pass. agc_driver_dcc_keys_first_write and agc_driver_srgb8_color_target fail intermittently without this change too (#1434). Neither loads libSceVideoOut. Run on their own afterwards, the first passed 3 of 3 times and the second passed 3 of 7.

Not run on Linux, where PreciseSleepUs is this_thread::sleep_for. No title tested.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4784049115 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1472). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-08T08:30:57Z. --- ### What On Windows, vblanks did not come every 16.68 ms. Over 3 × 240 vblanks, intervals ran from 2 ms to 34 ms, and about three quarters were more than 1 ms off the period. The vblank thread waited for each deadline with `condition_variable::wait_until`. With MinGW, that goes through winpthreads, which ends a timed wait only once `GetTickCount64()` has advanced by the timeout. `GetTickCount64` moves in 15.6 ms steps even with the 0.5 ms tick `Time.cpp` sets, so a wait ended on the first tick step after its deadline. When one step passed two deadlines, two vblanks fired back to back. Details and the other host waits this affects are in #1471. The thread now sleeps to each deadline with `PreciseSleepUs` (`libc/include/PreciseWait.hpp`). That uses a high-resolution waitable timer on Windows and `this_thread::sleep_for` elsewhere. Deadlines are still counted from the start, so the rate stays 59.94 Hz. A stop request or a presentation failure is now seen when the current sleep ends, at most one vblank later, instead of waking the thread at once. `Shutdown` can therefore take up to 16.7 ms longer. ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Intel Core i7-12700H, RTX 3060 Laptop. - I added a mode to `video_out_flip_tests` locally (not in this PR). It opens a port, times 240 returns of `sceVideoOutWaitVblank` with `steady_clock`, and was run 3 times on each build: | | min | p10 | median | p90 | max | > 1 ms off 16.68 ms | |---|---|---|---|---|---|---| | `main` d549f768 | 2.0 to 3.8 ms | 11.0 to 11.4 ms | 16.1 to 16.5 ms | 21.8 to 25.0 ms | 32.5 to 34.0 ms | 171 to 186 of 240 | | This change | 15.5 ms | 15.9 to 16.0 ms | 16.7 ms | 17.2 to 17.4 ms | 17.6 to 17.8 ms | 1 to 7 of 240 | - `video_out_flip_tests` (not in ctest, #1040), run from a directory with an `app0/sce_sys/param.json`, gives the same results with and without this change: - `decode`, `controls`, `present`, `backtoback`, `dcc`, `pacing` and `aftergpu` pass. - `unavailable` fails ("failed presentation was marked complete or retained its buffer"). - `lifetime` and `reopen` terminate ("port is closed"). - Full ctest: 419 of 421 pass. `agc_driver_dcc_keys_first_write` and `agc_driver_srgb8_color_target` fail intermittently without this change too (#1434). Neither loads `libSceVideoOut`. Run on their own afterwards, the first passed 3 of 3 times and the second passed 3 of 7. Not run on Linux, where `PreciseSleepUs` is `this_thread::sleep_for`. No title tested. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-08 11:57:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T11:57:52Z.

Review: APPROVED


Reviewed the complete diff, PR description, and current Linux, Windows, Conventions, and Progress report checks. The deadline-based high-resolution wait preserves the 59.94 Hz schedule, and the shutdown/failure wakeup delay is clearly documented. I found no issues.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5456194371 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1472#pullrequestreview-5456194371). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T11:57:52Z. **Review:** APPROVED --- Reviewed the complete diff, PR description, and current Linux, Windows, Conventions, and Progress report checks. The deadline-based high-resolution wait preserves the 59.94 Hz schedule, and the shutdown/failure wakeup delay is clearly documented. I found no issues.
adriadam10 commented 2026-10-08 14:13:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T14:13:46Z.


Checked the diff against the description, build + full ctest (462/462) on top of current main, and CI is green on this head (Linux, Windows, conventions). Deadlines are still counted from start, so the rate stays 59.94 Hz, and the extra shutdown latency (≤ 1 vblank) is documented. The Windows pacing numbers are yours (not reproducible here); merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6061792579 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1472#issuecomment-6061792579). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T14:13:46Z. --- Checked the diff against the description, build + full ctest (462/462) on top of current main, and CI is green on this head (Linux, Windows, conventions). Deadlines are still counted from `start`, so the rate stays 59.94 Hz, and the extra shutdown latency (≤ 1 vblank) is documented. The Windows pacing numbers are yours (not reproducible here); merging.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
boykopovar/AnyPS5!1472
No description provided.