Skip to content

perf(agc): resolve a committed shader ABI without the preparation lock - #8928

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
milescoviello:perf/abi-committed-fast-path
Oct 23, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
milescoviello:perf/abi-committed-fast-path

Conversation

@milescoviello

@milescoviello milescoviello commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

What

Every shader-ABI resolution opens a ShaderPreparationTransaction, and its first Read takes ShaderPreparationTransaction::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. A Read also 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 sceAgcCreateInterpolantMapping and sceAgcCreatePrimState. Per frame, the trace records about 2,000 Shader.ResolveAbi and 1,500 Shader.ResolveGraphicsStagesAbi calls of 20 us or more, averaging 46 us each, and nearly every one is for an ABI the title already resolved. Those threads queue on writers: in context-switch samples of the menu, 116,000 of the 142,000 times a title thread blocked inside an ABI resolution were in ShaderPreparationTransaction::Read, in pthread_mutex_lock (3 s, five threads). Half of the queue worker's idle time per frame overlaps title threads inside Shader.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 ResolveShaderAbi already did with CommittedAbi before 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 (under ResolveGraphicsAbi and 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 takes writers. 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, as TransactionLock checks, and any call that would change the state still takes writers before it reads. A transaction copies a shader's state only while it holds writers, 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 main eb9f904, then rebased onto main 3bd7173 (no conflicts, none of the 35 commits between touch these files).

Tests.

  • CommittedLinks in tests/execution/PreparedPublication.cpp:
    • While another thread's transaction holds the lock, resolving a link the shader already has returns (it waited 10 s on main).
    • A new link from a third thread still waits for that transaction.
    • Inside a transaction that already changed the shader's links, resolving a link uses the transaction's view and publishes it.
  • CommittedAbis in tests/execution/PreparedShaders.cpp (default run, also under --indirect and the uncached and debug modes) registers a vertex and a pixel shader and resolves them the way sceAgcLinkShaders does: ResolveShaderAbi, ResolveGraphicsStagesAbi and ResolveGraphicsAbi in one ShaderPreparationScope. 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 of CommittedAbis independent 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.
  • With ShaderRegistry.cpp from main, both tests fail at the first check (agc_driver_prepared_publication after 10.4 s, agc_driver_prepared_shaders after 11.1 s). With the Tracks check removed from ResolvePreparedGraphics only, CommittedLinks fails at the in-transaction check. With this PR both pass, as do agc_driver_prepared_shaders_indirect, _uncached and _debug.

Suites.

  • On the rebased head 48549d1a9c: agc_driver_prepared_publication, agc_driver_prepared_shaders and its _indirect, _uncached and _debug modes pass 3 of 3.
  • On 1bee200fa3, the same commit on eb9f904: ctest -R '^agc_' passes 850 of 850.
  • Khronos validation layer 1.4.363, on main eb9f904 and on 1bee200fa3: agc_driver_prepared_publication and agc_driver_prepared_shaders pass 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 main eea640a plus #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):

base with this PR
presents per 10 s, each run 45 to 49, 42 to 47, 43 to 48 49 to 55, 47 to 55, 49 to 56
mean of each run 46.4, 45.0, 45.3 51.6, 52.6, 53.2
frame time p50 201 to 238 ms 176 to 213 ms
Shader.ResolveAbi per frame 111 to 124 ms not among the eight largest phases

A 5 s trace window in each run (APS5_TRACE_SECONDS=220:5):

base with this PR
driver time on the five title threads per frame (traced slices plus the folded ones under 20 us) 206 to 230 ms 104 to 107 ms
Shader.ResolveAbi slices of 20 us or more per frame 75 to 87 ms 4.7 to 6.1 ms
queue worker waiting for work per frame 66 to 72 ms 33 to 39 ms
Driver.Draw on the queue worker per frame 98 to 106 ms 93 to 108 ms

With 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):

{"commit":"77df0e16ded1","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":".","APS5_TRACE_SECONDS":"220:5","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 595.99.02","os":"Linux 6.8.0-147-generic","cpu":"13th Gen Intel(R) Core(TM) i7-13700K","cpu_threads":24,"memory_mib":32022,"title_id":"PPSA34502","title":"Call of Duty\u00c2\u00ae: Black Ops II","content_version":"01.010.000","resolution":"1920x1080","window":{"spec":"seconds 220:5","first_present":7673,"ms":5142.997},"presents":24,"frame_ms":{"p50":212.063,"p90":241.201,"p99":303.509,"max":303.509},"gpu_busy_ms_per_present":27.265,"worker_busy_pct":{"queue worker 0x0":67.387},"top_phases_ms_per_frame":{"Driver.Packet":143.448,"Driver.Packet.draw":118.858,"Driver.Draw":117.817,"Shader.ResolveAbi":109.106,"Shader.ResolveGraphicsStagesAbi":83.833,"Driver.Draw.shader_compile_and_link":76.613,"Graphics.Draw":67.379,"Shader.DrawStage":62.398},"trace_dropped":false,"trace":"aps5-trace-77df0e16ded1-3.json.gz"}
{"commit":"f21f3f3d9493","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":".","APS5_TRACE_SECONDS":"220:5","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 595.99.02","os":"Linux 6.8.0-147-generic","cpu":"13th Gen Intel(R) Core(TM) i7-13700K","cpu_threads":24,"memory_mib":32022,"title_id":"PPSA34502","title":"Call of Duty\u00c2\u00ae: Black Ops II","content_version":"01.010.000","resolution":"1920x1080","window":{"spec":"seconds 220:5","first_present":7745,"ms":5036.377},"presents":27,"frame_ms":{"p50":182.164,"p90":210.082,"p99":234.718,"max":234.718},"gpu_busy_ms_per_present":27.684,"worker_busy_pct":{"queue worker 0x0":76.440},"top_phases_ms_per_frame":{"Driver.Packet":141.307,"Driver.Packet.draw":116.823,"Driver.Draw":115.697,"Graphics.Draw":74.900,"Driver.Draw.shader_compile_and_link":72.851,"Graphics.Draw.draw_and_resource_release":67.422,"Shader.DrawStage":61.820,"Shader.ResolveGraphicsStagesAbi":38.221},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":4.787,"events_lost":16236,"totals_full":false}},"trace":"aps5-trace-f21f3f3d9493-3.json.gz"}

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 in ShaderPreparationTransaction::Read, all in pthread_mutex_lock.

@ycbuzi on Windows 11 (RTX 4070 SUPER, driver 616.92, MinGW-w64 GCC 15.2.0, Release): agc_driver_prepared_publication and the 19 agc_driver_prepared_shaders* tests pass on two repeats, first on 48549d1 merged onto main b77bd58, then on the current head b68c03b merged onto main d6eafa5 (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 ResolveShaderAbi above the check added here; the two change different lines.

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 under Silent stubs in their library's technical debt file
  • 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 (BO2 itself needs feat(agc): run DB depth copies into a single-sample R32 float target #5499 to reach the menu)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4070 Ti, driver 595.99.02, Ubuntu 24.04; NVIDIA GeForce RTX 4070 SUPER, driver 616.92, Windows 11 (@ycbuzi)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the perf Performance change label Oct 21, 2026
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5115 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cpp
PR 5431 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 6188 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 6867 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cpp
PR 6977 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cpp
PR 7682 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 8592 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9431 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9432 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9508 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cpp
PR 9672 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9673 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9674 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9675 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9676 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9842 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9891 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9892 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9893 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 9895 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
PR 10193 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp
core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cpp

@ycbuzi

ycbuzi commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Windows run of 48549d1 merged onto main b77bd58 (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 with ANYPS5_GPU=4070 (the machine also has a GTX 1650 SUPER).

Built libSceAgcDriver, agc_driver_prepared_publication_tests and agc_driver_prepared_shaders_tests with no errors or warnings; ctest --repeat until-fail:2 on agc_driver_prepared_publication and the 19 agc_driver_prepared_shaders* tests: all 20 pass on both repeats, none skipped. Not run: the lock-wait measurement, AMD, Intel, a title.

@milescoviello

Copy link
Copy Markdown
Contributor Author

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)

…ront end

The still-waits check in CommittedAbis timed out after 200 ms, which the
other thread's front-end build alone could reach before it ever waited on
the preparation lock. It now waits until that build has started, gives the
thread 500 ms, and also requires the pixel shader's committed ABIs to be
unchanged. The build counter is read after background registration
preparation has settled, so an earlier test's preparation can't count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ycbuzi

ycbuzi commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Windows run of b68c03b, the update since my earlier run, merged onto main d6eafa5 (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_tests and agc_driver_prepared_shaders_tests with no errors or warnings; ctest --repeat until-fail:2 on agc_driver_prepared_publication and the 19 agc_driver_prepared_shaders* tests: all 20 pass on both repeats, none skipped. Not run: the lock-wait measurement, AMD, Intel, a title.

@milescoviello

Copy link
Copy Markdown
Contributor Author

@ycbuzi thanks for running the current head on Windows as well. I added it to your line in Tested.

AI-assisted: yes (Claude Code)

@adriadam10 adriadam10 self-assigned this Oct 23, 2026
@adriadam10
adriadam10 merged commit f3456a4 into boykopovar:main Oct 23, 2026
12 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked each fast path against the original flow: rectangle requests, primitive types 7/17 and expired links fall through to the locked path, the lazy branch of ResolveShaderAbi is untouched, and a transaction that already tracks the shader keeps its own view. CommittedLinks and CommittedAbis wait on main and pass here, and the "new link/ABI still waits" halves cover the exclusion. Windows run by @ycbuzi noted; merged.

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

Labels

agc libSceAgc* perf Performance change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants