perf(agc-driver): prepare shader ABI variants before taking the preparation lock #3159

Merged
XairRrR merged 1 commit from upstream-pr/3159 into main 2026-10-11 21:23:53 +00:00
XairRrR commented 2026-10-10 23:32:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-10T23:32:08Z.


What

A shader preparation transaction took the global writers lock when constructed and kept it to the end. ResolveShaderAbi, reached from sceAgcCreateInterpolantMapping and sceAgcLinkShaders, compiled its variants inside such a transaction, so every thread creating or linking shaders waited behind one compile.

PPSA05684 creates shaders while it streams its first level. Stack samples had a game thread blocked on this lock in most samples. The frames over 150 ms spent about 515 ms each in Shader.Register, against 9 ms in the other frames (frame-timing.log).

Changes:

  • The root transaction takes the lock on its first Read, Edit or Hold.
  • ResolveShaderAbi prepares the variants before reading the prepared state when its ABI was not committed yet.
  • Registry publication keeps the lock: PublishRegisteredShader holds it, and RegisterShader holds it before the driver mutex, so the lock order is unchanged.
  • Commit takes no lock: a transaction with changes already holds it.

Test in tests/execution/PreparedPublication.cpp (TransactionLock): an open transaction that has read nothing does not block another one, and one that has read does. It fails on main ("an open transaction that read nothing blocked another one"). The existing Registration test (a replacement must not interleave with an unfinished preparation) passes 32/32 parallel runs.

Tested

  • Arch Linux (kernel 7.2.6), GCC 16.2.1, NVIDIA RTX 4070 Ti SUPER (driver 615.71.09), Release with BUILD_TESTING=ON.
  • Branch built alone, rebased on main a11c8f7f. Full ctest: 831/832. The one failure, agc_driver_emulated_color_compare, fails the same way on plain main. Conventions check passed.
  • After numbers on PPSA05684: see this comment.
  • Title: Uncharted: Legacy of Thieves Collection (PPSA05684), on its own bring-up branch.

Checklist

  • Based on current main; no other open PR implements the same functions
    • Overlaps #2363: see the comments. Independent of it; if #2363 lands first, this PR moves onto its defer_lock constructor and drops Hold.
  • 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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4815201830 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3159). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-10T23:32:08Z. --- ### What A shader preparation transaction took the global writers lock when constructed and kept it to the end. `ResolveShaderAbi`, reached from `sceAgcCreateInterpolantMapping` and `sceAgcLinkShaders`, compiled its variants inside such a transaction, so every thread creating or linking shaders waited behind one compile. PPSA05684 creates shaders while it streams its first level. Stack samples had a game thread blocked on this lock in most samples. The frames over 150 ms spent about 515 ms each in `Shader.Register`, against 9 ms in the other frames (`frame-timing.log`). Changes: - The root transaction takes the lock on its first `Read`, `Edit` or `Hold`. - `ResolveShaderAbi` prepares the variants before reading the prepared state when its ABI was not committed yet. - Registry publication keeps the lock: `PublishRegisteredShader` holds it, and `RegisterShader` holds it before the driver mutex, so the lock order is unchanged. - `Commit` takes no lock: a transaction with changes already holds it. Test in `tests/execution/PreparedPublication.cpp` (`TransactionLock`): an open transaction that has read nothing does not block another one, and one that has read does. It fails on main ("an open transaction that read nothing blocked another one"). The existing `Registration` test (a replacement must not interleave with an unfinished preparation) passes 32/32 parallel runs. ### Tested - Arch Linux (kernel 7.2.6), GCC 16.2.1, NVIDIA RTX 4070 Ti SUPER (driver 615.71.09), Release with `BUILD_TESTING=ON`. - Branch built alone, rebased on main a11c8f7f. Full ctest: 831/832. The one failure, `agc_driver_emulated_color_compare`, fails the same way on plain main. Conventions check passed. - After numbers on PPSA05684: see [this comment](https://github.com/boykopovar/AnyPS5/pull/3159#issuecomment-6109220598). - Title: Uncharted: Legacy of Thieves Collection (PPSA05684), on its own bring-up branch. ### 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 - Overlaps #2363: see the comments. Independent of it; if #2363 lands first, this PR moves onto its `defer_lock` constructor and drops `Hold`. - [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
Dextroid17 commented 2026-10-11 00:16:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-11T00:16:18Z.

Review: COMMENTED


The review of #3158 covers both of these — it is written as a pair, because the premise and the measurements belong together. The short version here, so this one is not left without a note of its own: it is the sharper of the two, because it attaches its cost to something a player feels. Frames over 150 ms spent about 515 ms in Shader.Register against 9 ms in the others, and the samples are what rule out the simpler explanation that the lock was merely contended. Taking the lock on first use and preparing the variants before reading prepared state is the standard shape.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481301198 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3159#pullrequestreview-5481301198). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-11T00:16:18Z. **Review:** COMMENTED --- The review of #3158 covers both of these — it is written as a pair, because the premise and the measurements belong together. The short version here, so this one is not left without a note of its own: it is the sharper of the two, because it attaches its cost to something a player feels. Frames over 150 ms spent about 515 ms in `Shader.Register` against 9 ms in the others, and the samples are what rule out the simpler explanation that the lock was merely contended. Taking the lock on first use and preparing the variants before reading prepared state is the standard shape. Written with AI assistance.
XairRrR commented 2026-10-11 01:00:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-11T01:00:25Z.


Correction to the checklist: #2363 (open since Oct 9) touches the same code. Both defer taking the preparation writers lock, for different callers:

  • #2363 defers it in RegisterShader, keeping eager locking as the default and adding a std::defer_lock constructor.
  • This PR defers it for every root transaction, so that ResolveShaderAbi (reached from sceAgcCreateInterpolantMapping and sceAgcLinkShaders) prepares its variants before the first Read.

They conflict in ShaderRegistry.cpp and can't both merge as they are. Whichever lands second needs a rebase. If #2363 goes first, I'll rebase this one on its defer_lock constructor and keep only the ResolveShaderAbi part, plus deferring the transaction that sceAgcCreateInterpolantMapping opens.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103994669 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3159#issuecomment-6103994669). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-11T01:00:25Z. --- Correction to the checklist: #2363 (open since Oct 9) touches the same code. Both defer taking the preparation writers lock, for different callers: - #2363 defers it in `RegisterShader`, keeping eager locking as the default and adding a `std::defer_lock` constructor. - This PR defers it for every root transaction, so that `ResolveShaderAbi` (reached from `sceAgcCreateInterpolantMapping` and `sceAgcLinkShaders`) prepares its variants before the first `Read`. They conflict in `ShaderRegistry.cpp` and can't both merge as they are. Whichever lands second needs a rebase. If #2363 goes first, I'll rebase this one on its `defer_lock` constructor and keep only the `ResolveShaderAbi` part, plus deferring the transaction that `sceAgcCreateInterpolantMapping` opens.
oneandonlydean commented 2026-10-11 01:08:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T01:08:28Z.


Could you add the after numbers for this change alone on PPSA05684: the >150 ms frames and the Shader.Register time with it, against the 515 ms you measured before? Perf PRs need both sides (#218).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104053702 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3159#issuecomment-6104053702). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T01:08:28Z. --- Could you add the after numbers for this change alone on PPSA05684: the >150 ms frames and the `Shader.Register` time with it, against the 515 ms you measured before? Perf PRs need both sides (#218).
Silverash-i8 commented 2026-10-11 01:51:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-11T01:51:16Z.


Windows run, since the PR has no Windows results yet: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto main d31b57a4 in one build with #3213, #3159, #3158, #3142, #3141 and #2977, on an RTX 5060 Laptop and the Arrow Lake iGPU (ANYPS5_GPU=Intel).

  • agc_driver_prepared_publication (with the new TransactionLock case) passes on both GPUs.

In the full ctest of that build, the only failure not also on plain main is agc_driver_dcc_keys_first_write, which I bisected to #3158 alone (reported there).

AI-assisted (Claude Code); I ran this on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104356325 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3159#issuecomment-6104356325). Originally posted by [Silverash-i8](https://github.com/Silverash-i8) on 2026-10-11T01:51:16Z. --- Windows run, since the PR has no Windows results yet: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto `main` d31b57a4 in one build with #3213, #3159, #3158, #3142, #3141 and #2977, on an RTX 5060 Laptop and the Arrow Lake iGPU (`ANYPS5_GPU=Intel`). - `agc_driver_prepared_publication` (with the new `TransactionLock` case) passes on both GPUs. In the full `ctest` of that build, the only failure not also on plain `main` is `agc_driver_dcc_keys_first_write`, which I bisected to #3158 alone (reported there). AI-assisted (Claude Code); I ran this on my machine.
adriadam10 commented 2026-10-11 07:05:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T07:05:59Z.

Review: CHANGES_REQUESTED


Checked: build and ctest of libSceAgcDriver are green (349/349), and agc_driver_prepared_publication fails without the change, so the new test does pin it. The lock logic reads right.

Two things before this can merge:

  • After numbers. As @oneandonlydean asked, perf PRs need both sides (CONTRIBUTING). Please post the >150 ms frames and the Shader.Register time on PPSA05684 with this change alone, against the 515 ms / 9 ms you measured before.
  • Overlap with #2363. Both change when ShaderPreparationTransaction takes writers and both edit ShaderRegistry.cpp, so they can't both land as they are. The checklist says Depends on: none; please settle the order with #2363 and list it under Depends on. If #2363 goes first, rebase and reuse its deferred-lock constructor rather than adding a second mechanism (Hold) for the same thing.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5482275091 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3159#pullrequestreview-5482275091). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T07:05:59Z. **Review:** CHANGES_REQUESTED --- Checked: build and ctest of libSceAgcDriver are green (349/349), and `agc_driver_prepared_publication` fails without the change, so the new test does pin it. The lock logic reads right. Two things before this can merge: - **After numbers.** As @oneandonlydean asked, perf PRs need both sides (CONTRIBUTING). Please post the >150 ms frames and the `Shader.Register` time on PPSA05684 with this change alone, against the 515 ms / 9 ms you measured before. - **Overlap with #2363.** Both change when `ShaderPreparationTransaction` takes `writers` and both edit `ShaderRegistry.cpp`, so they can't both land as they are. The checklist says `Depends on: none`; please settle the order with #2363 and list it under Depends on. If #2363 goes first, rebase and reuse its deferred-lock constructor rather than adding a second mechanism (`Hold`) for the same thing.
XairRrR commented 2026-10-11 12:52:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-11T12:52:40Z.


After numbers on PPSA05684, as asked. Same bring-up branch, one 15-minute run each from boot to the chapter 1 boat intro, with and without this change (the PR's exact code). Plain main does not run this title yet.

without with
Shader.Register calls 4838 4838
Shader.Register total 169.3 s 15.5 s
Shader.Register per frame over 150 ms 118 ms 8.9 ms
Shader.Register per other frame 9.1 ms 1.6 ms
Frames over 150 ms 1338 1331
Shader.ResolveAbi per call (15,374 calls) 10.6 ms 18.0 ms

The wait in Shader.Register is gone. The count of frames over 150 ms does not change, though, so those frames have another cause. ResolveAbi costs more per call, likely because threads now prepare the same variants concurrently outside the lock. My earlier 515 ms figure came from an older state of the branch. One run per side, so treat these as indicative.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6109220598 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3159#issuecomment-6109220598). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-11T12:52:40Z. --- After numbers on PPSA05684, as asked. Same bring-up branch, one 15-minute run each from boot to the chapter 1 boat intro, with and without this change (the PR's exact code). Plain `main` does not run this title yet. | | without | with | |---|---|---| | `Shader.Register` calls | 4838 | 4838 | | `Shader.Register` total | 169.3 s | 15.5 s | | `Shader.Register` per frame over 150 ms | 118 ms | 8.9 ms | | `Shader.Register` per other frame | 9.1 ms | 1.6 ms | | Frames over 150 ms | 1338 | 1331 | | `Shader.ResolveAbi` per call (15,374 calls) | 10.6 ms | 18.0 ms | The wait in `Shader.Register` is gone. The count of frames over 150 ms does not change, though, so those frames have another cause. `ResolveAbi` costs more per call, likely because threads now prepare the same variants concurrently outside the lock. My earlier 515 ms figure came from an older state of the branch. One run per side, so treat these as indicative.
XairRrR commented 2026-10-11 13:13:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-11T13:13:33Z.


On the order with #2363: #2363 is still waiting on start-up numbers from its own title, so I've left this PR independent of it and rebased it onto current main (the only conflict was the test list in PreparedPublication.cpp).

If #2363 lands first, I'll rebase this one onto its defer_lock constructor and drop Hold, keeping only the ResolveShaderAbi part, as @adriadam10 asked. If this one lands first, #2363 can reuse Hold (or the lazy root lock) instead of a second constructor. The description now says so under Depends on.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6109396152 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3159#issuecomment-6109396152). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-11T13:13:33Z. --- On the order with #2363: #2363 is still waiting on start-up numbers from its own title, so I've left this PR independent of it and rebased it onto current `main` (the only conflict was the test list in `PreparedPublication.cpp`). If #2363 lands first, I'll rebase this one onto its `defer_lock` constructor and drop `Hold`, keeping only the `ResolveShaderAbi` part, as @adriadam10 asked. If this one lands first, #2363 can reuse `Hold` (or the lazy root lock) instead of a second constructor. The description now says so under Depends on.
github-actions[bot] commented 2026-10-11 13:27:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T13:27:15Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#2363 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp L160, L162, L182, L195, L1096
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6109515085 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3159#issuecomment-6109515085). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T13:27:15Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #2363 | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp` [L160](https://github.com/boykopovar/AnyPS5/blob/b3ae112012d84ac519525b39c65a91e4d7e87430/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp#L160), [L162](https://github.com/boykopovar/AnyPS5/blob/b3ae112012d84ac519525b39c65a91e4d7e87430/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp#L162), [L182](https://github.com/boykopovar/AnyPS5/blob/b3ae112012d84ac519525b39c65a91e4d7e87430/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp#L182), [L195](https://github.com/boykopovar/AnyPS5/blob/b3ae112012d84ac519525b39c65a91e4d7e87430/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp#L195), [L1096](https://github.com/boykopovar/AnyPS5/blob/b3ae112012d84ac519525b39c65a91e4d7e87430/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp#L1096) | | |
adriadam10 commented 2026-10-11 21:23:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:23:49Z.

Review: APPROVED


Checked: libSceAgcDriver build and ctest are green; agc_driver_prepared_publication passes and fails without the change; Commit still takes each destination mutex and the lock order (writers before the driver mutex) is unchanged. The after numbers address the earlier request. Merging; whichever of this and #2363 lands second rebases onto the other.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5485021303 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3159#pullrequestreview-5485021303). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:23:49Z. **Review:** APPROVED --- Checked: libSceAgcDriver build and ctest are green; `agc_driver_prepared_publication` passes and fails without the change; `Commit` still takes each destination mutex and the lock order (`writers` before the driver mutex) is unchanged. The after numbers address the earlier request. Merging; whichever of this and #2363 lands second rebases onto the other.
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!3159
No description provided.