fix(agc): complete a flip only once the frame's GPU work has finished #417

Merged
oneandonlydean merged 1 commit from upstream-pr/417 into main 2026-10-03 22:30:23 +00:00
oneandonlydean commented 2026-10-02 22:34:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-02T22:34:49Z.


What

A flip could complete while the GPU was still executing the frame it shows.

With APS5_FLIP_INFLIGHT at its default of 1, Driver::Present reported the flip (gpuReady, so flipPendingNum drops and the flip event fires) as soon as the blit was queued behind the frame's batches. A title reads that completion as "the GPU is done with this frame" and starts reusing the frame's memory: per-draw constants, vertex data, command buffers. The recorded batches read guest memory in place when they finally execute, so draws that had not run yet read the next frame's data.

In PPSA21564 on my integration branch this showed up as follows (that branch also delivers end-of-pipe interrupts without draining, as #409 proposes, so the window there is wider than on main, see below):

  • runaway shader loops: a vertex program reads an integer sample count from its material constants and steps a loop by its reciprocal. When the constant ring had already been rewritten, it read float 1.0 (0x3f800000) from another draw as the count and looped about 1e9 times, stalling the GPU;
  • exploded geometry and flat coloured frames, from other draws' matrices and constants.

With APS5_FLIP_INFLIGHT=0 (the render fence wait before the flip completes) none of this happened. In my test runs of the affected level (300 s each), the hits of that loop went from 435 to 0 with this change.

The presenter now queues the present as before and then waits for the frame's own fence (FinishPresent), outside the GPU mutex, before reporting the flip. The swapchain present still goes out at once, so the display does not wait for the CPU; only the completion the title observes moves to the point where the GPU no longer reads the frame. APS5_SYNC_FLIP already waited and is unchanged. The comment in FlipRequest::GpuReady that described the old completion point is updated.

On main a RELEASE_MEM that raises an interrupt drains the device, so the window is the frame's work recorded after its last such packet, plus anything ordered only by plain labels or waits. That is still enough for a title to see the flip before its last draws have run.

When no blit is submitted (the window is not presentable, e.g. minimised), the flip still completes without waiting, as before.

The flip now completes later, so a present is more likely to end past the next vblank. That moves the next flip's target out by one vblank on main; #416 fixes that and should go in with or before this one.

Tested

  • Linux (CachyOS, RTX 4090): full build with BUILD_TESTING=ON and ctest pass.
  • New by-hand mode video_out_flip_tests aftergpu (needs a GPU and a display, like the file's other presentation modes): submits a busy compute dispatch followed by an end-of-pipe label and a flip, and checks that the flip does not complete before the label lands. The label has no interrupt, so main does not drain at it. Three runs each: on main the flip completed at 2.1, 9.3 and 10.8 ms while the label landed at about 243 ms, and the test failed; with this change the flip completed 0.1 to 0.2 ms after the label (about 243 to 257 ms) and the test passed.
  • video_out_flip_tests present and backtoback pass.
  • PPSA21564: the change has been on my integration branch through repeated passes of my visual regression check (checkpoint frames, flicker, shimmer, video tearing, FPS), with FPS at the reference.

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: #416 (should land with or before this one). With #409 the window this closes gets wider, so this one matters more once #409 is in.
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4722169901 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/417). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-02T22:34:49Z. --- ### What A flip could complete while the GPU was still executing the frame it shows. With `APS5_FLIP_INFLIGHT` at its default of 1, `Driver::Present` reported the flip (`gpuReady`, so `flipPendingNum` drops and the flip event fires) as soon as the blit was queued behind the frame's batches. A title reads that completion as "the GPU is done with this frame" and starts reusing the frame's memory: per-draw constants, vertex data, command buffers. The recorded batches read guest memory in place when they finally execute, so draws that had not run yet read the next frame's data. In PPSA21564 on my integration branch this showed up as follows (that branch also delivers end-of-pipe interrupts without draining, as #409 proposes, so the window there is wider than on main, see below): - runaway shader loops: a vertex program reads an integer sample count from its material constants and steps a loop by its reciprocal. When the constant ring had already been rewritten, it read float 1.0 (`0x3f800000`) from another draw as the count and looped about 1e9 times, stalling the GPU; - exploded geometry and flat coloured frames, from other draws' matrices and constants. With `APS5_FLIP_INFLIGHT=0` (the render fence wait before the flip completes) none of this happened. In my test runs of the affected level (300 s each), the hits of that loop went from 435 to 0 with this change. The presenter now queues the present as before and then waits for the frame's own fence (`FinishPresent`), outside the GPU mutex, before reporting the flip. The swapchain present still goes out at once, so the display does not wait for the CPU; only the completion the title observes moves to the point where the GPU no longer reads the frame. `APS5_SYNC_FLIP` already waited and is unchanged. The comment in `FlipRequest::GpuReady` that described the old completion point is updated. On main a RELEASE_MEM that raises an interrupt drains the device, so the window is the frame's work recorded after its last such packet, plus anything ordered only by plain labels or waits. That is still enough for a title to see the flip before its last draws have run. When no blit is submitted (the window is not presentable, e.g. minimised), the flip still completes without waiting, as before. The flip now completes later, so a present is more likely to end past the next vblank. That moves the next flip's target out by one vblank on main; #416 fixes that and should go in with or before this one. ### Tested - Linux (CachyOS, RTX 4090): full build with `BUILD_TESTING=ON` and `ctest` pass. - New by-hand mode `video_out_flip_tests aftergpu` (needs a GPU and a display, like the file's other presentation modes): submits a busy compute dispatch followed by an end-of-pipe label and a flip, and checks that the flip does not complete before the label lands. The label has no interrupt, so main does not drain at it. Three runs each: on main the flip completed at 2.1, 9.3 and 10.8 ms while the label landed at about 243 ms, and the test failed; with this change the flip completed 0.1 to 0.2 ms after the label (about 243 to 257 ms) and the test passed. - `video_out_flip_tests present` and `backtoback` pass. - PPSA21564: the change has been on my integration branch through repeated passes of my visual regression check (checkpoint frames, flicker, shimmer, video tearing, FPS), with FPS at the reference. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: #416 (should land with or before this one). With #409 the window this closes gets wider, so this one matters more once #409 is in. - [x] AI-assisted: yes
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!417
No description provided.