Skip to content

Fix/agc hold flip behind worker - #1745

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
AymanFE:fix/agc-hold-flip-behind-worker
Oct 10, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
AymanFE:fix/agc-hold-flip-behind-worker

Conversation

@AymanFE

@AymanFE AymanFE commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

What

A title that paces itself on pending flips can get further ahead of the queue 0 worker than its per-frame memory allows, and the worker then records a frame from memory the title has already rewritten.

Astro Bot (PPSA21564) keeps per-frame data in a ring of three slots, polls sceVideoOutIsFlipPending, and submits the next flip as soon as fewer than three are pending. On hardware a pending flip is a frame whose commands have been read. Here submissions waited 98 to 110 ms in queue 0, so the worker was still recording frame N while the title wrote frame N+3 into the same slot. A block of per-frame constants that holds the title's frame counter read 0x17e in the frame right after one that read 0x17a: the worker recorded frame 0x17b from the slot of frame 0x17e. When the slot was half rewritten the draw bound the wrong block, and the frame came out black, without the movie quad, or with the quad at half size: 8 to 16 of 48 window captures in each of five runs. APS5_FLIP_READ_CHECK=1 counted 141,478 of 158,640 in-place reads as overwritten before execution. The same overrun produced guest snapshot differs from registered memory in compute shader 0x5005e7b00.

A submission on queue 0 that carries a flip now waits until the worker has reached the flip of every earlier one. The wait ends early when queue 0 is blocked on memory that no running, held or pending work writes, the criterion orderReleased uses, since only the title can end that wait. The presenter stays asynchronous: the title still has three flips pending when the display is the limit. APS5_NO_FLIP_HOLD=1 submits as before.

tests/Flip.cpp gains a case: a flip behind a closed rendering wait holds the next flip submission until the wait opens, and a flip behind a WAIT_REG_MEM on a label only the test stores does not hold it.

Not established: what the console returns from sceVideoOutIsFlipPending while its GPU is behind. The change does not depend on it.

Tested

  • Windows 11, RTX 5070 Ti, Astro Bot (PPSA21564, 01.007.000), built from b3fcd6d6 (an older main) with this change. 45 s runs through the intro, 48 window captures each in bursts 70 ms apart:
    Build | Odd captures | Frames presented -- | -- | -- Without the change (five runs) | 8 to 16 | 730 to 795 With the change (two runs) | 0 | 868, 999 With the change and APS5_NO_FLIP_HOLD=1 | 12 | 795

    With the change the snapshot check passes through the whole intro without APS5_NO_SNAPSHOT_CHECK. The title still needs a local workaround for an unrelated failure in the same shader at the first frame (guest memory is not readable at 0x30) and APS5_HOST_IMPORT_MIB=16384.

  • Linux, on 4ddbf049 (this branch's base): agc_driver_flip, agc_driver_pm4 and agc_driver_pm4_failure pass; agc_driver is skipped there because the machine has no Vulkan device. The new case fails with APS5_NO_FLIP_HOLD=1 and passes without it. tools/check_conventions.py --base origin/main passes. The rest of the test suite was not built or run.
  • Not tested: the title on 4ddbf049 itself (the Windows runs used the older base; rebasing needed two textual conflict resolutions next to the new timing fields), any title on Linux, any title other than Astro Bot.

Checklist

  • Based on current main; no other open PR implements the same functions (unmerged pull requests from fix(agc): turn off dma-buf import on the NVIDIA proprietary driver #1380 to ci: warn about tests that take more than 30 s #1744 were checked through their refs; older ones were not)
  • 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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes. The investigation, the change, the test and this description were written with Claude; the Windows runs were made on my PC with my own copy of the game.
What

A title that paces itself on pending flips can get further ahead of the queue 0 worker than its per-frame memory allows, and the worker then records a frame from memory the title has already rewritten.

Astro Bot (PPSA21564) keeps per-frame data in a ring of three slots, polls sceVideoOutIsFlipPending, and submits the next flip as soon as fewer than three are pending. On hardware a pending flip is a frame whose commands have been read. Here submissions waited 98 to 110 ms in queue 0, so the worker was still recording frame N while the title wrote frame N+3 into the same slot. A block of per-frame constants that holds the title's frame counter read 0x17e in the frame right after one that read 0x17a: the worker recorded frame 0x17b from the slot of frame 0x17e. When the slot was half rewritten the draw bound the wrong block, and the frame came out black, without the movie quad, or with the quad at half size: 8 to 16 of 48 window captures in each of five runs. APS5_FLIP_READ_CHECK=1 counted 141,478 of 158,640 in-place reads as overwritten before execution. The same overrun produced guest snapshot differs from registered memory in compute shader 0x5005e7b00.

A submission on queue 0 that carries a flip now waits until the worker has reached the flip of every earlier one. The wait ends early when queue 0 is blocked on memory that no running, held or pending work writes, the criterion orderReleased uses, since only the title can end that wait. The presenter stays asynchronous: the title still has three flips pending when the display is the limit. APS5_NO_FLIP_HOLD=1 submits as before.

tests/Flip.cpp gains a case: a flip behind a closed rendering wait holds the next flip submission until the wait opens, and a flip behind a WAIT_REG_MEM on a label only the test stores does not hold it.

Not established: what the console returns from sceVideoOutIsFlipPending while its GPU is behind. The change does not depend on it.

Tested
Windows 11, RTX 5070 Ti, Astro Bot (PPSA21564, 01.007.000), built from b3fcd6d (an older main) with this change. 45 s runs through the intro, 48 window captures each in bursts 70 ms apart:
Build Odd captures Frames presented
Without the change (five runs) 8 to 16 730 to 795
With the change (two runs) 0 868, 999
With the change and APS5_NO_FLIP_HOLD=1 12 795
With the change the snapshot check passes through the whole intro without APS5_NO_SNAPSHOT_CHECK. The title still needs a local workaround for an unrelated failure in the same shader at the first frame (guest memory is not readable at 0x30) and APS5_HOST_IMPORT_MIB=16384.
Linux, on 4ddbf04 (this branch's base): agc_driver_flip, agc_driver_pm4 and agc_driver_pm4_failure pass; agc_driver is skipped there because the machine has no Vulkan device. The new case fails with APS5_NO_FLIP_HOLD=1 and passes without it. tools/check_conventions.py --base origin/main passes. The rest of the test suite was not built or run.
Not tested: the title on 4ddbf04 itself (the Windows runs used the older base; rebasing needed two textual conflict resolutions next to the new timing fields), any title on Linux, any title other than Astro Bot.
Checklist
Based on current main; no other open PR implements the same functions (unmerged pull requests from #1380 to #1744 were checked through their refs; older ones were not)
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 (none added)
Generic behaviour, not specific to one title
Depends on: none
AI-assisted: yes. The investigation, the change, the test and this description were written with Claude; the Windows runs were made on my PC with my own copy of the game.

@AymanFE
AymanFE force-pushed the fix/agc-hold-flip-behind-worker branch from 7e5ad0a to 878d7ca Compare October 8, 2026 18:38
@MarcZX10

MarcZX10 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

I checked the failed conventions run (37825811506). It reports that What, Tested, and Checklist are missing, plus a non-Conventional Commit title. The submitted body starts with a pasted HTML document and then repeats the same content as plain text, so the checker is not recognizing the sections. Please replace it with the repository template in plain Markdown (one copy), use a title such as fix(agc): hold flips behind the worker, then rerun conventions. The Linux build is still running; the latest Windows build passed. CONTRIBUTING also asks for a branch based on current main, so please refresh the branch before treating the reported older-base results as final.

@github-actions github-actions Bot added the agc libSceAgc* label Oct 8, 2026
@fisherjoey

Copy link
Copy Markdown
Contributor

Linux results, since this was tested on Windows only. Everything passes here.

Nobara 44 (Linux 7.2), GCC 16.2.1, Release, BUILD_TESTING=ON. This PR merged onto main f145139 cleanly.

main f145139 main + this PR
RTX 5070 Ti, driver 595.91.07, ctest -R agc_driver -j1 207 / 207 207 / 207
RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same 207 / 207 207 / 207
CPU tests (-E agc_driver) 286 / 287 286 / 287

The CPU failure is guest_dynamic_loader, which fails the same way on main.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Three things before this can go in:

  • Drop APS5_NO_FLIP_HOLD (Submission.cpp:141). Env switches that turn a fix off aren't wanted here; the hold should just be right.
  • Wake the hold on worker progress instead of polling: Submission.cpp:148 waits in 1 ms wait_for steps until flipHoldOver() is true. Notify changed wherever runningWorkers, orderHolders and the pending completions drop, and wait on the predicate. Polling costs a wake-up every millisecond while held and can release up to 1 ms late.
  • Hold writers on the other queues too: submission.queue != 0 returns early (Submission.cpp:142), so a title that writes the next frame's data through a compute or second graphics queue still races the worker. The flip being held is on queue 0, but the overwrite can come from any queue.

@popcoder565

Copy link
Copy Markdown
Contributor

Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head 878d7ca1, merged onto main d70b899 in one build with 33 other AGC/shader PRs that had no Windows run.

No title was run.

🤖 Addressed by Claude Code

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the investigation and the measurements. Still open before this can go in:

  • Dean's three points from the previous comment are not addressed yet: drop APS5_NO_FLIP_HOLD (Submission.cpp:141), wake the hold by notifying changed where runningWorkers, orderHolders and the pending completions drop instead of the 1 ms wait_for poll (Submission.cpp:148), and hold writers coming from queues other than 0 (Submission.cpp:142).
  • conventions is red: the PR body is a pasted HTML document followed by a plain-text copy, so the What / Tested / Checklist sections are not recognized. Please replace it with one plain-Markdown copy of the template, and retitle as fix(agc): hold flips behind the worker (Conventional Commits).
  • The new test in Flip.cpp should also cover the notify path (hold released by worker progress with no extra submission) once the polling is gone.

Rebase on current main when you push; the Linux (fisherjoey) and Windows (popcoder565) runs on this head were clean.

AymanFE and others added 2 commits October 10, 2026 22:58
…evious one

A title that paces itself on pending flips assumes what hardware gives it:
the command processor consumes a submitted buffer at once, so a pending flip
is a frame whose commands have been read and that only waits for the display.
The queue worker is slower than that. With three flips pending it can still be
recording the oldest frame while the title rewrites that frame's slot of a
three-deep ring of per-frame memory for the frame after next, and the draws
are then recorded with the later frame's (or half-written) constants.

A submission on queue 0 that carries a flip now waits until the worker has
reached the flip of every earlier one. The wait ends early when queue 0 is
blocked on memory that no running or pending work writes, since only the
title can end that wait. APS5_NO_FLIP_HOLD=1 submits as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017MPYE7P9ob12aAE1H4JtHF
The hold no longer polls every millisecond and has no switch to turn it
off. A held submission waits on the driver's condition variable until no
flip is ahead of it, or until queue 0 waits for memory that only the
title can store. The queue 0 worker reports the second case from its
memory and rewind waits, and the worker that reaches a flip wakes the
holders.

agc_driver_flip_tests: a flip held behind a rendering wait is released
when the worker goes on to wait for a label only the title stores, with
no further submission.
@oneandonlydean
oneandonlydean force-pushed the fix/agc-hold-flip-behind-worker branch from 878d7ca to 5d888ab Compare October 10, 2026 10:06
@oneandonlydean

Copy link
Copy Markdown
Collaborator

I rebased this on current main and added one commit on top for the review points (5d888ab):

  • APS5_NO_FLIP_HOLD is gone.
  • The hold waits on changed with no timeout. Two things wake it: the worker that reaches a flip, and the queue 0 worker once its memory or rewind wait can only be ended by a store from the title. That second check is the same one flipHoldOver made, now done by the worker in its wait loop.
  • New case in agc_driver_flip_tests: a flip held behind a rendering wait is released when the worker moves on to a label wait that only the title can satisfy, without another submission. Without the wake it fails.

I left the other-queue point out. The hold blocks the title's thread at its flip submission, so that thread's later submissions on any queue come after the held one anyway.

Tested: Linux, RTX 4090, Release, full ctest. Everything passes except agc_driver_dcc_keys_first_write, which fails on main too. agc_driver_flip passes 20 of 20 runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants