perf(agc): resolve a committed shader ABI without the preparation lock #8928
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8928"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T06:35:11Z.
What
Every shader-ABI resolution opens a
ShaderPreparationTransaction, and its firstReadtakesShaderPreparationTransaction::State::writers, a process-wide mutex held until the transaction ends. That is also true when the ABI is already prepared, so the call changes nothing. AReadalso copies the shader's whole prepared state into the transaction: every recorded ABI key, graphics ABI and entry.Some titles resolve ABIs for every draw from several threads. In CoD BO2's (PPSA34502) war-room menu, five job threads call
sceAgcCreateInterpolantMappingandsceAgcCreatePrimState. Per frame, the trace records about 2,000Shader.ResolveAbiand 1,500Shader.ResolveGraphicsStagesAbicalls of 20 us or more, averaging 46 us each, and nearly every one is for an ABI the title already resolved. Those threads queue onwriters: in context-switch samples of the menu, 116,000 of the 142,000 times a title thread blocked inside an ABI resolution were inShaderPreparationTransaction::Read, inpthread_mutex_lock(3 s, five threads). Half of the queue worker's idle time per frame overlaps title threads insideShader.ResolveAbi, so the waits delay the submissions the worker is waiting for.Now the three resolutions first check the shader's published state under the shader's own mutex, as
ResolveShaderAbialready did withCommittedAbibefore preparing:ResolveShaderAbi: the ABI key is already recorded.ResolveGraphicsStagesAbi: the owner already has a graphics ABI with this key and registry. Its stages are then resolved as before.ResolvePreparedGraphics(underResolveGraphicsAbiand the stages above): the call would add no fragment link and request no rectangle, and no link has expired.When the check holds, the call returns without
Read, so it neither copies the state nor takeswriters. It applies only when the transaction holds no view of that shader yet (ShaderPreparationTransaction::Tracks). Inside a group that already read or changed the shader, the call keeps using the group's view. A transaction that reads nothing still blocks no other one, asTransactionLockchecks, and any call that would change the state still takeswritersbefore it reads. A transaction copies a shader's state only while it holdswriters, and its commit only adds ABI keys, graphics ABIs and links. The exceptions are graphics ABIs of a registry that no longer exists and expired links. A check that held stays true for as long as the call needs it: it compares against the live registry, and it doesn't apply while a link has expired.Tested
Ubuntu 24.04 (Linux 6.8), GCC 13.3, NVIDIA RTX 4070 Ti, driver 595.99.02, on
maineb9f904d32, then rebased ontomain3bd7173b77(no conflicts, none of the 35 commits between touch these files).Tests.
CommittedLinksintests/execution/PreparedPublication.cpp:main).CommittedAbisintests/execution/PreparedShaders.cpp(default run, also under--indirectand the uncached and debug modes) registers a vertex and a pixel shader and resolves them the waysceAgcLinkShadersdoes:ResolveShaderAbi,ResolveGraphicsStagesAbiandResolveGraphicsAbiin oneShaderPreparationScope. While another thread's transaction holds the lock, repeating the same resolution returns, with no front-end build. The same pixel shader under another interpolant state still waits.b68c03b4e4(test only) makes the "still waits" half ofCommittedAbisindependent of the other thread's front-end build time. It waits until that build has started, then 500 ms, and also requires the pixel shader's committed ABI count to be unchanged. It reads the build counter after background registration preparation has settled. The five prepared-shader tests pass 3 of 3 on it.ShaderRegistry.cppfrommain, both tests fail at the first check (agc_driver_prepared_publicationafter 10.4 s,agc_driver_prepared_shadersafter 11.1 s). With theTrackscheck removed fromResolvePreparedGraphicsonly,CommittedLinksfails at the in-transaction check. With this PR both pass, as doagc_driver_prepared_shaders_indirect,_uncachedand_debug.Suites.
48549d1a9c:agc_driver_prepared_publication,agc_driver_prepared_shadersand its_indirect,_uncachedand_debugmodes pass 3 of 3.1bee200fa3, the same commit oneb9f904d32:ctest -R '^agc_'passes 850 of 850.maineb9f904d32and on1bee200fa3:agc_driver_prepared_publicationandagc_driver_prepared_shaderspass 2 of 2 on both, with no VUID.CoD BO2 (PPSA34502), war-room menu. The title needs #5499 to reach it. The base is #5499 rebased on
maineea640a5bfplus #8756 and #8888 (77df0e16de); the other build adds this commit (f21f3f3d94). Release libs, warm caches, three rounds with the order swapped each round, 100 s in the menu each. The figures are the ten 10 s intervals in the menu (APS5_TRACE_SUMMARY_EVERY=10):Shader.ResolveAbiper frameA 5 s trace window in each run (
APS5_TRACE_SECONDS=220:5):Shader.ResolveAbislices of 20 us or more per frameDriver.Drawon the queue worker per frameWith this PR the queue worker processes more draws per second, and its trace buffer fills 4.5 to 4.8 s into the 5 s window (16,000 to 36,000 events lost, totals kept). The base windows dropped nothing.
Files, round 2 (rounds 1 and 3 are in the same release):
How I found it: a CPU profile of the menu showed little time in the resolution functions, while the trace showed about 46 us per call. Context-switch samples (
perf record -e context-switches, 3 s) put 116,000 of the 142,000 blocks of title threads inside an ABI resolution inShaderPreparationTransaction::Read, all inpthread_mutex_lock.@ycbuzi on Windows 11 (RTX 4070 SUPER, driver 616.92, MinGW-w64 GCC 15.2.0, Release):
agc_driver_prepared_publicationand the 19agc_driver_prepared_shaders*tests pass on two repeats, first on48549d1a9cmerged ontomainb77bd58806, then on the current headb68c03b4e4merged ontomaind6eafa54d0(clean), none skipped.Not tested: AMD, Intel, other titles. #2292 reports the same lock from shader registration while loading; this PR doesn't change registration. #7644 adds its lazy branch to
ResolveShaderAbiabove the check added here; the two change different lines.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T07:46:26Z.
Windows run of
48549d1a9cmerged ontomainb77bd58806(c146deb862, clean), one of the hosts the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release,BUILD_TESTING=ON. GPU: NVIDIA GeForce RTX 4070 SUPER, driver 616.92, picked withANYPS5_GPU=4070(the machine also has a GTX 1650 SUPER).Built
libSceAgcDriver,agc_driver_prepared_publication_testsandagc_driver_prepared_shaders_testswith no errors or warnings;ctest --repeat until-fail:2onagc_driver_prepared_publicationand the 19agc_driver_prepared_shaders*tests: all 20 pass on both repeats, none skipped. Not run: the lock-wait measurement, AMD, Intel, a title.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T07:59:36Z.
Thanks @ycbuzi. That covers Windows, which I hadn't run. I added your run to the Tested section and the GPU line.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-22T08:34:29Z.
Windows run of
b68c03b4e4, the update since my earlier run, merged ontomaind6eafa54d0(f8f9413d74, clean). Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release,BUILD_TESTING=ON. GPU: NVIDIA GeForce RTX 4070 SUPER, driver 616.92 (ANYPS5_GPU=4070,ANYPS5_REQUIRE_VULKAN=1).Built
libSceAgcDriver,agc_driver_prepared_publication_testsandagc_driver_prepared_shaders_testswith no errors or warnings;ctest --repeat until-fail:2onagc_driver_prepared_publicationand the 19agc_driver_prepared_shaders*tests: all 20 pass on both repeats, none skipped. Not run: the lock-wait measurement, AMD, Intel, a title.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-22T09:14:46Z.
@ycbuzi thanks for running the current head on Windows as well. I added it to your line in Tested.
AI-assisted: yes (Claude Code)