perf(agc): prebuild graphics pipeline shader libraries off the GpuMutex on first visit #25434

Merged
tablefrogpillow merged 1 commit from upstream-pr/25434 into main 2026-11-23 06:00:54 +00:00
tablefrogpillow commented 2026-11-22 03:07:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tablefrogpillow on 2026-11-22T03:07:57Z.


What

First-visit graphics draws built their pipeline shader libraries while holding GuestMemory::GpuMutex(). Driver::commitDraw takes the mutex and holds it across VulkanDevice::Draw, so the pre-rasterization and fragment-shader GraphicsPipelineLibrary compiles for a never-seen pipeline ran with the mutex held and blocked the queue worker at fill — the first-visit freezes of #25352.

This prebuilds those libraries before commitDraw takes the mutex, and only on a pipeline's first visit, so the warm path adds no per-draw build work:

  • VulkanDevice::PrebuildGraphicsLibrariesOnFirstVisit folds the draw's shader variantIds into a key and checks a per-device set. On the common (warm) path it returns after one mutex-guarded hash-set lookup and calls no vkCreateGraphicsPipelines. On a first visit it calls the existing, already off-lock PrebuildGraphicsLibraries and records the key.
  • commitDraw calls it once, before TagGpuLockSite and the std::lock_guard.

How the off-lock build is published, and who races. PrebuildGraphicsLibraries builds the library parts through the same code a mutex-held link uses (LinkPipelineFromLibraries, partsOnly), which already drops the pipeline-library store's own mutex around each vkCreateGraphicsPipelines and inserts the finished VkPipeline back into the per-device store under that store mutex. That store is the single publication point: the later mutex-held link reads the same keyed cache and hits it. Because the build runs with the store mutex dropped, the journal replay, the registration prepare pool and several draw-commit threads can already build the same part at once; the store resolves that by keeping the first inserted part and destroying the duplicate (the raced branch in LinkPipelineFromLibraries). The new first-visit set adds only a best-effort filter under its own mutex — if two threads race the very first visit of one pipeline they both prebuild once (the store drops one) and one of them may still compile under the lock that single time. It never changes which pipeline a draw binds, only whether the compile happened before or under the mutex.

A per-part builtLocked/linkedLocked counter on PipelineLibraryCounters, incremented at the vkCreateGraphicsPipelines sites when GpuMutex().HeldByThisThread(), backs the test and the measurement below; it mirrors the existing computePipelinesCreatedLocked.

Tested

AMD Radeon RX Vega 56 (RADV VEGA10), Mesa 26.2.6, Linux 6.8, VK_LAYER_KHRONOS_validation 1.4.x inserted, ANYPS5_NO_SHADER_CACHE=1 (cold pipeline cache):

  • New test agc_driver_graphics_pipeline_outside_gpu_mutex_tests passes with the change and fails without it (without the prebuild a first-visit draw builds its pre-rasterization and fragment libraries under the mutex). No new VUID- validation errors.
  • Cat Quest 3, per-part count of pipeline libraries built while the mutex is held, n=3 deterministic: pre-rasterization 8 → 0, fragment 12 → 0. The cheap interface parts (vertex-input 8 → 7, fragment-output 10 → 9) and the final link stay under the mutex; they compile no shaders. Renders identically, no throughput change.

I cannot reproduce the multi-second stall on the pipeline-light titles I have (Cat Quest 3's whole library set is ~2 ms), so the above is the counter evidence that the shader compiles move off the mutex, not a wall-clock number on a title that actually freezes. The PPSA06207 Molly's-story cold rerun is the wall-clock check.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Fixes: #25352
  • Unblocks: none
  • GPUs tested: AMD Radeon RX Vega 56 (RADV VEGA10), Mesa 26.2.6, Linux 6.8 (AGC)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5435151101 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25434). Originally posted by [tablefrogpillow](https://github.com/tablefrogpillow) on 2026-11-22T03:07:57Z. --- ### What First-visit graphics draws built their pipeline **shader libraries while holding `GuestMemory::GpuMutex()`**. `Driver::commitDraw` takes the mutex and holds it across `VulkanDevice::Draw`, so the pre-rasterization and fragment-shader `GraphicsPipelineLibrary` compiles for a never-seen pipeline ran with the mutex held and blocked the queue worker at `fill` — the first-visit freezes of #25352. This prebuilds those libraries **before** `commitDraw` takes the mutex, and **only on a pipeline's first visit**, so the warm path adds no per-draw build work: - `VulkanDevice::PrebuildGraphicsLibrariesOnFirstVisit` folds the draw's shader `variantId`s into a key and checks a per-device set. On the common (warm) path it returns after one mutex-guarded hash-set lookup and calls no `vkCreateGraphicsPipelines`. On a first visit it calls the existing, already off-lock `PrebuildGraphicsLibraries` and records the key. - `commitDraw` calls it once, before `TagGpuLockSite` and the `std::lock_guard`. **How the off-lock build is published, and who races.** `PrebuildGraphicsLibraries` builds the library parts through the same code a mutex-held link uses (`LinkPipelineFromLibraries`, `partsOnly`), which already drops the pipeline-library store's own mutex around each `vkCreateGraphicsPipelines` and inserts the finished `VkPipeline` back into the per-device store under that store mutex. That store is the single publication point: the later mutex-held link reads the same keyed cache and hits it. Because the build runs with the store mutex dropped, the journal replay, the registration prepare pool and several draw-commit threads can already build the same part at once; the store resolves that by keeping the first inserted part and destroying the duplicate (the `raced` branch in `LinkPipelineFromLibraries`). The new first-visit set adds only a best-effort filter under its own mutex — if two threads race the very first visit of one pipeline they both prebuild once (the store drops one) and one of them may still compile under the lock that single time. It never changes which pipeline a draw binds, only whether the compile happened before or under the mutex. A per-part `builtLocked`/`linkedLocked` counter on `PipelineLibraryCounters`, incremented at the `vkCreateGraphicsPipelines` sites when `GpuMutex().HeldByThisThread()`, backs the test and the measurement below; it mirrors the existing `computePipelinesCreatedLocked`. ### Tested AMD Radeon RX Vega 56 (RADV VEGA10), Mesa 26.2.6, Linux 6.8, `VK_LAYER_KHRONOS_validation` 1.4.x inserted, `ANYPS5_NO_SHADER_CACHE=1` (cold pipeline cache): - New test `agc_driver_graphics_pipeline_outside_gpu_mutex_tests` passes with the change and fails without it (without the prebuild a first-visit draw builds its pre-rasterization and fragment libraries under the mutex). No new `VUID-` validation errors. - Cat Quest 3, per-part count of pipeline libraries built while the mutex is held, n=3 deterministic: pre-rasterization 8 → 0, fragment 12 → 0. The cheap interface parts (vertex-input 8 → 7, fragment-output 10 → 9) and the final link stay under the mutex; they compile no shaders. Renders identically, no throughput change. I cannot reproduce the multi-second stall on the pipeline-light titles I have (Cat Quest 3's whole library set is ~2 ms), so the above is the counter evidence that the shader compiles move off the mutex, not a wall-clock number on a title that actually freezes. The PPSA06207 Molly's-story cold rerun is the wall-clock check. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [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] Fixes: #25352 - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX Vega 56 (RADV VEGA10), Mesa 26.2.6, Linux 6.8 (AGC) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-22 06:13:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T06:13:08Z.

Review: COMMENTED


Reviewed — no blocking findings

The inspected change moves first visit shader library construction before the GPU mutex; I did not run the new test or read every changed file, so the evidence is insufficient for approval.

What I ran

No commands or tests were run. I inspected the prepared diff and targeted source ranges with repo_read.py; exact head: 2cb997da06754c69194fc3543a2756c135ffe835.

Results

Check Result Note
Prepared diff review passed Inspected relevant hunks
New test not run No test execution available
Full changed-file review not run Some changed files remain unread

Findings

Blocking

None verified in the inspected code.

Non-blocking

None.

Not verified

The full text for core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp was excluded from the review folder because it was too large; I read only lines 5140–5200 through the pinned reader. I did not read all lines of the included changed files or inspect the full new test. The prepared bundle included five changed files: VulkanDevice.hpp, Draw.cpp, VulkanDevice.cpp, PipelineLibrary.hpp, PipelineLibrary.cpp, and GraphicsPipelineOutsideGpuMutex.cpp (six files total); I read targeted ranges from Draw.cpp, VulkanDevice.cpp, PipelineLibrary.cpp, and the diff, but not the full text of every included file. Author-reported test and measurement results were not independently verified.

To get this to a good state

Nothing is required. Optional: run agc_driver_graphics_pipeline_outside_gpu_mutex_tests on this head and complete review of the remaining changed-file text before treating the review as complete.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6010689774 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25434#pullrequestreview-6010689774). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T06:13:08Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The inspected change moves first visit shader library construction before the GPU mutex; I did not run the new test or read every changed file, so the evidence is insufficient for approval. ## What I ran No commands or tests were run. I inspected the prepared diff and targeted source ranges with `repo_read.py`; exact head: `2cb997da06754c69194fc3543a2756c135ffe835`. ## Results | Check | Result | Note | |---|---|---| | Prepared diff review | passed | Inspected relevant hunks | | New test | not run | No test execution available | | Full changed-file review | not run | Some changed files remain unread | ## Findings ### Blocking None verified in the inspected code. ### Non-blocking None. ## Not verified The full text for `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` was excluded from the review folder because it was too large; I read only lines 5140–5200 through the pinned reader. I did not read all lines of the included changed files or inspect the full new test. The prepared bundle included five changed files: `VulkanDevice.hpp`, `Draw.cpp`, `VulkanDevice.cpp`, `PipelineLibrary.hpp`, `PipelineLibrary.cpp`, and `GraphicsPipelineOutsideGpuMutex.cpp` (six files total); I read targeted ranges from `Draw.cpp`, `VulkanDevice.cpp`, `PipelineLibrary.cpp`, and the diff, but not the full text of every included file. Author-reported test and measurement results were not independently verified. ## To get this to a good state Nothing is required. Optional: run `agc_driver_graphics_pipeline_outside_gpu_mutex_tests` on this head and complete review of the remaining changed-file text before treating the review as complete.
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!25434
No description provided.