fix(agc): wait for queued registration preparations before accepting a submission #5007

Merged
gravelas merged 1 commit from upstream-pr/5007 into main 2026-10-14 18:31:56 +00:00
gravelas commented 2026-10-13 08:00:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gravelas on 2026-10-13T08:00:21Z.


What

Goat Simulator 3 (PPSA04159) stops at startup with a write of 0 at a compiled-out debug hook (app.exe+0x2511b7b): the engine's garbage collector waits for a render command fence, gives up after 10 s and reaches a fatal path. With another program loading the CPU it happened on every launch (11 of 11), without load about one launch in four.

Since registered shaders are prepared in the background (6db5c1369), a draw whose shader the title registered just before its submission waits for that preparation inside the queue worker, in AwaitRegisteredPreparation. A GPU trace (APS5_TRACE_GPU=1) of a failing launch shows:

  • the third submission (1786 dwords) arrives at 1.2 s and is done at 5.1 s, after a 3.7 s wait for two registration preparations of about 1.4 s each;
  • both end-of-pipe interrupts of the submission are delivered;
  • the title never submits again; the timeout fires about 10 s later with the queue worker, the RHI thread and every task thread idle.

The title takes a submission of seconds for a hung GPU. On the console shaders are compiled ahead of time, so a submission never waits for one.

Driver::Submit now waits, on the submitting thread and before it accepts the commands, until the registration preparations queued before the call have run (RegistrationPreparePool::AwaitSubmitted: each queued task gets a sequence number, and the call returns once no task numbered at or before the newest one at the call is queued or running, so a later task finishing first does not end the wait). The queue worker then finds every such preparation settled, and the submission's GPU side stays short: the same submission is done in about 180 ms.

  • The wait covers every preparation queued before the call, not only those of the shaders the commands use. A TechnicalDebt entry records this: a submission behind a long queue of unrelated preparations waits for all of them, on the submitting thread.
  • Nothing changes with APS5_SYNC_SHADER_PREPARE=1 (nothing is queued) or when the pool has no threads (tasks run inline).
  • Related: #4404 moves a preparation that a caller waits for to the front of the queue, which shortens the wait when the shader is still queued behind others. This change keeps the wait out of the submission when the shader is already being prepared, which #4404 cannot shorten. The two touch the pool's Submit and would need a rebase against each other.
  • Related: #4883 keeps the front-end resource plan in the shader disk cache. The preparations in the trace above ran with a warm cache (1839 hits, 0 misses): the slow part was the front end that registration runs before the cache is consulted. With #4883 a warm launch would rarely leave a preparation for a submission to wait on, so this change would matter mostly when the cache is cold (a first launch, or any change to the recompiler sources, which invalidates the cache), for shaders registered for the first time while a title streams new areas, and where #4883 still runs the front end.

agc_driver_prepared_shaders gains cases: a task queued on the registration pool that sleeps 50 ms has run when AwaitRegistrationPreparation returns; and with a 300 ms task queued before the wait and a fast one queued after it began, the wait does not return before the slow task is done (a count of finished tasks fails this, because the fast task finishes first).

Tested

  • Windows 11, NVIDIA RTX 4080, AMD Ryzen 7 7800X3D, MinGW-w64 GCC 15.2.0, Release with BUILD_TESTING=ON: ctest -R "agc|shader" -j6 passes 473 of 473, as on main, and agc_driver_recorder_tests passes its default run.
  • tools/check_conventions.py --base origin/main passes.
  • Goat Simulator 3 (PPSA04159), on a local development branch with this change: with another game loading the CPU, 0 of 5 launches crashed (11 of 11 before), two 6-minute runs stayed up, and the main menu ran at the same 2.4-3.0 FPS as before. With APS5_SYNC_SHADER_PREPARE=1 instead, 0 of 3 crashed, which isolates the background wait as the cause. On current main the title stops earlier, at its first draw with clip distances (clip distances, layer, viewport or auxiliary vertex exports is unsupported (register 0x207 = 0x00400001), which #423 and #1077 cover), so these numbers are from that local branch: main of 2026-10-12 with #1077 merged.

Checklist

  • Based on current main; no other open PR implements the same functions (#4404 prioritizes queued preparations; see above)
  • 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:4840209661 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5007). Originally posted by [gravelas](https://github.com/gravelas) on 2026-10-13T08:00:21Z. --- ### What Goat Simulator 3 (PPSA04159) stops at startup with a write of 0 at a compiled-out debug hook (app.exe+0x2511b7b): the engine's garbage collector waits for a render command fence, gives up after 10 s and reaches a fatal path. With another program loading the CPU it happened on every launch (11 of 11), without load about one launch in four. Since registered shaders are prepared in the background (6db5c1369), a draw whose shader the title registered just before its submission waits for that preparation inside the queue worker, in `AwaitRegisteredPreparation`. A GPU trace (`APS5_TRACE_GPU=1`) of a failing launch shows: - the third submission (1786 dwords) arrives at 1.2 s and is done at 5.1 s, after a 3.7 s wait for two registration preparations of about 1.4 s each; - both end-of-pipe interrupts of the submission are delivered; - the title never submits again; the timeout fires about 10 s later with the queue worker, the RHI thread and every task thread idle. The title takes a submission of seconds for a hung GPU. On the console shaders are compiled ahead of time, so a submission never waits for one. `Driver::Submit` now waits, on the submitting thread and before it accepts the commands, until the registration preparations queued before the call have run (`RegistrationPreparePool::AwaitSubmitted`: each queued task gets a sequence number, and the call returns once no task numbered at or before the newest one at the call is queued or running, so a later task finishing first does not end the wait). The queue worker then finds every such preparation settled, and the submission's GPU side stays short: the same submission is done in about 180 ms. - The wait covers every preparation queued before the call, not only those of the shaders the commands use. A TechnicalDebt entry records this: a submission behind a long queue of unrelated preparations waits for all of them, on the submitting thread. - Nothing changes with `APS5_SYNC_SHADER_PREPARE=1` (nothing is queued) or when the pool has no threads (tasks run inline). - Related: #4404 moves a preparation that a caller waits for to the front of the queue, which shortens the wait when the shader is still queued behind others. This change keeps the wait out of the submission when the shader is already being prepared, which #4404 cannot shorten. The two touch the pool's `Submit` and would need a rebase against each other. - Related: #4883 keeps the front-end resource plan in the shader disk cache. The preparations in the trace above ran with a warm cache (1839 hits, 0 misses): the slow part was the front end that registration runs before the cache is consulted. With #4883 a warm launch would rarely leave a preparation for a submission to wait on, so this change would matter mostly when the cache is cold (a first launch, or any change to the recompiler sources, which invalidates the cache), for shaders registered for the first time while a title streams new areas, and where #4883 still runs the front end. `agc_driver_prepared_shaders` gains cases: a task queued on the registration pool that sleeps 50 ms has run when `AwaitRegistrationPreparation` returns; and with a 300 ms task queued before the wait and a fast one queued after it began, the wait does not return before the slow task is done (a count of finished tasks fails this, because the fast task finishes first). ### Tested - Windows 11, NVIDIA RTX 4080, AMD Ryzen 7 7800X3D, MinGW-w64 GCC 15.2.0, Release with `BUILD_TESTING=ON`: `ctest -R "agc|shader" -j6` passes 473 of 473, as on `main`, and `agc_driver_recorder_tests` passes its default run. - `tools/check_conventions.py --base origin/main` passes. - Goat Simulator 3 (PPSA04159), on a local development branch with this change: with another game loading the CPU, 0 of 5 launches crashed (11 of 11 before), two 6-minute runs stayed up, and the main menu ran at the same 2.4-3.0 FPS as before. With `APS5_SYNC_SHADER_PREPARE=1` instead, 0 of 3 crashed, which isolates the background wait as the cause. On current `main` the title stops earlier, at its first draw with clip distances (`clip distances, layer, viewport or auxiliary vertex exports is unsupported (register 0x207 = 0x00400001)`, which #423 and #1077 cover), so these numbers are from that local branch: `main` of 2026-10-12 with #1077 merged. ### 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 (#4404 prioritizes queued preparations; see above) - [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)
xXPBSXx commented 2026-10-13 09:41:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T09:41:45Z.


Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on main 2a2a3dc65 together with nine other open GPU PRs (their own commits applied, no merge commits; one further PR, #5024, conflicted with them and was left out), so only the tests that belong to this PR say something about it:

  • tests/execution/PreparedShaders.cpp, which ctest runs as agc_driver_prepared_shaders and its variants _indirect, _uncached, _debug, _debug_failure, _deferred, _deferred_undecodable, _registered_workgroup and _registered_workgroup_sync: 3 of 3 runs passed for each.

Not tested: no game run. The batch was built once, not per PR; nothing failed, so no bisect was needed.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6142753364 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5007#issuecomment-6142753364). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T09:41:45Z. --- Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on main `2a2a3dc65` together with nine other open GPU PRs (their own commits applied, no merge commits; one further PR, #5024, conflicted with them and was left out), so only the tests that belong to this PR say something about it: - `tests/execution/PreparedShaders.cpp`, which `ctest` runs as `agc_driver_prepared_shaders` and its variants `_indirect`, `_uncached`, `_debug`, `_debug_failure`, `_deferred`, `_deferred_undecodable`, `_registered_workgroup` and `_registered_workgroup_sync`: 3 of 3 runs passed for each. Not tested: no game run. The batch was built once, not per PR; nothing failed, so no bisect was needed. AI-assisted (Claude Code).
blawesom commented 2026-10-14 05:45:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-14T05:45:20Z.


Rebase handoff — rebased onto current main, authorship unchanged. This one was a real architectural merge of the preparation worker pool:

  • main's demanded-preparation prioritization (PreparedShaders* on tasks, Prioritize rotating the queue) and this PR's sequence-based await (AwaitSubmitted/AwaitRegistrationPreparation, the unfinished set) are both kept — the task struct carries sequence, function and prepared together, the demanded tasks still jump the queue with their sequence intact, and the await predicate is unchanged.
  • One subtlety worth knowing: the worker's post-run task reset must keep sequence alive for the unfinished.erase that follows — resetting the whole task would deadlock every await. That's how it's merged.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV):

  • builds; tools/check_conventions.py --base origin/main passes
  • full suite: 1099/1100 — the only failure is the known main regression (agc_driver_tessellation segfault, #4727); the prepared-shaders family (all 4 variants plus indirect) passes

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/5007
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is f1982baa3.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161803689 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5007#issuecomment-6161803689). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-14T05:45:20Z. --- **Rebase handoff** — rebased onto current `main`, authorship unchanged. This one was a real architectural merge of the preparation worker pool: - `main`'s demanded-preparation prioritization (`PreparedShaders*` on tasks, `Prioritize` rotating the queue) and this PR's sequence-based await (`AwaitSubmitted`/`AwaitRegistrationPreparation`, the `unfinished` set) are **both kept** — the task struct carries `sequence`, `function` and `prepared` together, the demanded tasks still jump the queue with their sequence intact, and the await predicate is unchanged. - One subtlety worth knowing: the worker's post-run task reset must keep `sequence` alive for the `unfinished.erase` that follows — resetting the whole task would deadlock every await. That's how it's merged. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): - builds; `tools/check_conventions.py --base origin/main` passes - **full suite: 1099/1100** — the only failure is the known `main` regression (`agc_driver_tessellation` segfault, #4727); the prepared-shaders family (all 4 variants plus indirect) passes **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/5007 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `f1982baa3`.
adriadam10 commented 2026-10-14 17:33:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T17:33:40Z.


Rebased onto current main (conflict in ShaderRegistry.cpp with the demanded-preparation work: both kept, sequence is read before the task is reset so unfinished.erase still gets it).

Checked on Linux (RADV, Release): scoped libSceAgcDriver build + ctest 550/551, the only failure is agc_driver_tessellation (segfault, also on main). All agc_driver_prepared_shaders* pass, 15 repeats without a hang. With AwaitSubmitted turned into a no-op, agc_driver_prepared_shaders fails on "a submission did not wait for the registration preparation queued before it". Game behaviour (Goat Simulator 3) not reproduced here; relying on the reported runs. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6174391650 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5007#issuecomment-6174391650). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T17:33:40Z. --- Rebased onto current `main` (conflict in `ShaderRegistry.cpp` with the demanded-preparation work: both kept, `sequence` is read before the task is reset so `unfinished.erase` still gets it). Checked on Linux (RADV, Release): scoped libSceAgcDriver build + ctest 550/551, the only failure is `agc_driver_tessellation` (segfault, also on `main`). All `agc_driver_prepared_shaders*` pass, 15 repeats without a hang. With `AwaitSubmitted` turned into a no-op, `agc_driver_prepared_shaders` fails on "a submission did not wait for the registration preparation queued before it". Game behaviour (Goat Simulator 3) not reproduced here; relying on the reported runs. Merging once the full Build is green.
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!5007
No description provided.