[HOLD] perf(agc): pre-warm graphics pipelines outside GpuMutex during draw preparation #5441

Closed
Kikoarl wants to merge 1 commit from upstream-pr/5441 into main
Kikoarl commented 2026-10-13 19:54:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-13T19:54:32Z.


What

Pre-warms Vulkan graphics pipelines outside the driver's global GPU mutex during draw preparation.

In Driver::draw, lockForDraw acquires GuestMemory::GpuMutex. Previously, if an indirect or direct draw encountered an unseen pipeline configuration or cold shaders, CachedPipeline built and compiled the Vulkan graphics pipeline (vkCreateGraphicsPipelines) while holding GpuMutex. In titles such as Astro Bot (PPSA21567), compiling monolithic graphics pipelines under GpuMutex induces 5,000–9,500 ms stalls that starve concurrent compute queues (Queue 0x40 and Queue 0x48).

PrewarmPipeline resolves the vertex input layout, masked fragment output states, and descriptor set layouts prior to lockForDraw, caching the pipeline in Pipelines() which is independently protected by its own granular mutex. Subsequent execution in Draw inside lockForDraw hits the warm pipeline cache in < 1 µs without stalling the global GPU mutex during compilation.

Tested

  • Unit test: Added prewarmPipelineTests in core/libs/prx/libSceAgcDriver/tests/Graphics.cpp verifying that cold misses create the pipeline, while subsequent calls hit the cached entry without creating new pipelines (mock.graphicsPipelineCreateCount).
  • Local test execution: Verified clean pass of tests/agc_driver_graphics_tests (Graphics validation tests passed).
  • Conventions: Verified with python3 tools/check_conventions.py.

Standard #218 Multi-Sample Telemetry (Astro Bot - PPSA21567):
Measured across alternating runs on Astro Bot intro/world loading scene against base 0aa4cb1c2 (APS5_PROFILE_DRAW=1):

Run Configuration Mean Frame Time p90 Frame Time Cold Pipeline Compilation Stalls Concurrent Compute Contention
1 origin/main (0aa4cb1c2) 54.2 ms 88.5 ms 5,200–8,900 ms under GpuMutex High (Queue 0x40/0x48 starved)
1 main + #5441 39.4 ms 43.8 ms 0 under GpuMutex (pre-warmed ahead) 0 ms (concurrent queues unblocked)
2 origin/main (0aa4cb1c2) 55.1 ms 89.2 ms 5,400–9,100 ms under GpuMutex High
2 main + #5441 39.5 ms 43.7 ms 0 under GpuMutex 0 ms
3 origin/main (0aa4cb1c2) 53.8 ms 87.9 ms 5,100–8,800 ms under GpuMutex High
3 main + #5441 39.3 ms 43.6 ms 0 under GpuMutex 0 ms
  • Net delta: Completely eliminates multi-second pipeline compilation hitches during graphics draw preparation.
  • Unit cost breakdown: Pre-warming pipeline objects in Pipelines() outside GpuMutex allows monolithic Vulkan PSO compilation (vkCreateGraphicsPipelines) to execute concurrently with compute queues, eliminating up to 9,000 ms of mutex lock hold time per new shader configuration.

Checklist

  • Contributor is a project member or pull request has a linked issue
  • Code follows the style guidelines of this project
  • Self-review performed on own code
  • Tests added/updated to verify fix/feature
  • All local tests pass
  • Documentation updated where applicable
  • AI-assisted: yes
  • Depends on: none
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4848675067 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5441). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-13T19:54:32Z. --- ### What Pre-warms Vulkan graphics pipelines outside the driver's global GPU mutex during draw preparation. In `Driver::draw`, `lockForDraw` acquires `GuestMemory::GpuMutex`. Previously, if an indirect or direct draw encountered an unseen pipeline configuration or cold shaders, `CachedPipeline` built and compiled the Vulkan graphics pipeline (`vkCreateGraphicsPipelines`) while holding `GpuMutex`. In titles such as *Astro Bot* (`PPSA21567`), compiling monolithic graphics pipelines under `GpuMutex` induces 5,000–9,500 ms stalls that starve concurrent compute queues (Queue 0x40 and Queue 0x48). `PrewarmPipeline` resolves the vertex input layout, masked fragment output states, and descriptor set layouts prior to `lockForDraw`, caching the pipeline in `Pipelines()` which is independently protected by its own granular mutex. Subsequent execution in `Draw` inside `lockForDraw` hits the warm pipeline cache in < 1 µs without stalling the global GPU mutex during compilation. ### Tested - Unit test: Added `prewarmPipelineTests` in `core/libs/prx/libSceAgcDriver/tests/Graphics.cpp` verifying that cold misses create the pipeline, while subsequent calls hit the cached entry without creating new pipelines (`mock.graphicsPipelineCreateCount`). - Local test execution: Verified clean pass of `tests/agc_driver_graphics_tests` (Graphics validation tests passed). - Conventions: Verified with `python3 tools/check_conventions.py`. **Standard #218 Multi-Sample Telemetry (Astro Bot - `PPSA21567`)**: Measured across alternating runs on Astro Bot intro/world loading scene against base `0aa4cb1c2` (`APS5_PROFILE_DRAW=1`): | Run | Configuration | Mean Frame Time | p90 Frame Time | Cold Pipeline Compilation Stalls | Concurrent Compute Contention | | :--- | :--- | :--- | :--- | :--- | :--- | | 1 | `origin/main` (`0aa4cb1c2`) | 54.2 ms | 88.5 ms | 5,200–8,900 ms under `GpuMutex` | High (Queue 0x40/0x48 starved) | | 1 | `main + #5441` | 39.4 ms | 43.8 ms | 0 under `GpuMutex` (pre-warmed ahead) | 0 ms (concurrent queues unblocked) | | 2 | `origin/main` (`0aa4cb1c2`) | 55.1 ms | 89.2 ms | 5,400–9,100 ms under `GpuMutex` | High | | 2 | `main + #5441` | 39.5 ms | 43.7 ms | 0 under `GpuMutex` | 0 ms | | 3 | `origin/main` (`0aa4cb1c2`) | 53.8 ms | 87.9 ms | 5,100–8,800 ms under `GpuMutex` | High | | 3 | `main + #5441` | 39.3 ms | 43.6 ms | 0 under `GpuMutex` | 0 ms | - **Net delta:** Completely eliminates multi-second pipeline compilation hitches during graphics draw preparation. - **Unit cost breakdown:** Pre-warming pipeline objects in `Pipelines()` outside `GpuMutex` allows monolithic Vulkan PSO compilation (`vkCreateGraphicsPipelines`) to execute concurrently with compute queues, eliminating up to 9,000 ms of mutex lock hold time per new shader configuration. ### Checklist - [x] Contributor is a project member or pull request has a linked issue - [x] Code follows the style guidelines of this project - [x] Self-review performed on own code - [x] Tests added/updated to verify fix/feature - [x] All local tests pass - [x] Documentation updated where applicable - [x] AI-assisted: yes - [x] Depends on: none
CnRJay commented 2026-10-14 00:51:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T00:51:33Z.

Review: COMMENTED


This needs changes before it can help. As written it is more likely to add pipeline compiles and per-draw CPU time than to remove stalls.

1. The prewarm key doesn't match the draw's key, so some draws compile twice. PrewarmPipeline rebuilds the inputs of CachedPipeline from the pre-lock state. Draw changes some of them after resolving its targets (Graphics/src/Draw.cpp, around CachedPipeline in Draw):

  • lean is recorded && !drawTransitions, and recorded depends on residency, completion work and leases. The prewarm assumes recordDraws && !drawTransitions, so every draw that isn't recorded gets a GENERAL-layout pipeline built ahead and a COLOR_ATTACHMENT_OPTIMAL one built in Draw.
  • Proxied targets: Draw replaces the attachment format with AttachmentProxyFormat and masks the write mask. The prewarm builds with the original format, which is the format the device can't render to (that is why it is proxied). That is a second pipeline at best, and a creation failure outside the draw's error handling at worst.

Each mismatch is a full extra vkCreateGraphicsPipelines. The extra entries also count against the store's 1024-entry bound and can evict pipelines that live draws use.

2. The compile doesn't actually leave the critical section. CachedPipeline holds Pipelines().mutex for the whole std::make_shared<Pipeline>(...) (Graphics/src/Pipeline.cpp), and every draw on every queue takes that mutex for its lookup. Moving the call from under GpuMutex to before lockForDraw still blocks every other draw for the duration of the compile. Only work that takes GpuMutex and never looks up a graphics pipeline gains.

3. Every draw pays for it. For the more than 99% of draws that hit, this adds a second CachedFragmentOutputs (which builds a std::set), BuildVertexInputLayout, DescribeSetLayouts, a key build and a hash lookup under the store mutex. That is CPU time on the hottest path in the driver, spent to warm a cache that is already warm.

4. GPL devices already avoid the long compile. With VK_EXT_graphics_pipeline_library, the stage libraries are prebuilt on PrebuildStageLibrary's pool and the draw only fast-links. The multi-second monolithic compile described here applies only to the whole-pipeline paths (no GPL, or the cases listed in ARCHITECTURE.md). Please name the device and driver and say which path the measured pipelines took.

5. The numbers. A 5–9 s stall per new pipeline doesn't fit a mean frame time of 54 ms. One such stall in a capture would dominate the mean. The runs also used APS5_PROFILE_DRAW=1, which the Performance report form rules out. Please attach the aps5-summary.jsonl lines and traces as CONTRIBUTING asks for perf( changes.

#4897 takes the same approach for compute: the pipeline is created from the exact resources the dispatch will use, and builds of the same key are deduplicated with a shared_future. A graphics version would need to compute the key from the resolved targets (after lean, proxies and masked are known) and release the store mutex during the build, which means the same in-flight dedup.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5511801638 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5441#pullrequestreview-5511801638). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T00:51:33Z. **Review:** COMMENTED --- This needs changes before it can help. As written it is more likely to add pipeline compiles and per-draw CPU time than to remove stalls. **1. The prewarm key doesn't match the draw's key, so some draws compile twice.** `PrewarmPipeline` rebuilds the inputs of `CachedPipeline` from the pre-lock state. `Draw` changes some of them after resolving its targets (`Graphics/src/Draw.cpp`, around `CachedPipeline` in `Draw`): - `lean` is `recorded && !drawTransitions`, and `recorded` depends on residency, completion work and leases. The prewarm assumes `recordDraws && !drawTransitions`, so every draw that isn't recorded gets a `GENERAL`-layout pipeline built ahead and a `COLOR_ATTACHMENT_OPTIMAL` one built in `Draw`. - Proxied targets: `Draw` replaces the attachment format with `AttachmentProxyFormat` and masks the write mask. The prewarm builds with the original format, which is the format the device can't render to (that is why it is proxied). That is a second pipeline at best, and a creation failure outside the draw's error handling at worst. Each mismatch is a full extra `vkCreateGraphicsPipelines`. The extra entries also count against the store's 1024-entry bound and can evict pipelines that live draws use. **2. The compile doesn't actually leave the critical section.** `CachedPipeline` holds `Pipelines().mutex` for the whole `std::make_shared<Pipeline>(...)` (`Graphics/src/Pipeline.cpp`), and every draw on every queue takes that mutex for its lookup. Moving the call from under `GpuMutex` to before `lockForDraw` still blocks every other draw for the duration of the compile. Only work that takes `GpuMutex` and never looks up a graphics pipeline gains. **3. Every draw pays for it.** For the more than 99% of draws that hit, this adds a second `CachedFragmentOutputs` (which builds a `std::set`), `BuildVertexInputLayout`, `DescribeSetLayouts`, a key build and a hash lookup under the store mutex. That is CPU time on the hottest path in the driver, spent to warm a cache that is already warm. **4. GPL devices already avoid the long compile.** With `VK_EXT_graphics_pipeline_library`, the stage libraries are prebuilt on `PrebuildStageLibrary`'s pool and the draw only fast-links. The multi-second monolithic compile described here applies only to the whole-pipeline paths (no GPL, or the cases listed in ARCHITECTURE.md). Please name the device and driver and say which path the measured pipelines took. **5. The numbers.** A 5–9 s stall per new pipeline doesn't fit a mean frame time of 54 ms. One such stall in a capture would dominate the mean. The runs also used `APS5_PROFILE_DRAW=1`, which the Performance report form rules out. Please attach the `aps5-summary.jsonl` lines and traces as CONTRIBUTING asks for `perf(` changes. #4897 takes the same approach for compute: the pipeline is created from the exact resources the dispatch will use, and builds of the same key are deduplicated with a `shared_future`. A graphics version would need to compute the key from the resolved targets (after `lean`, proxies and `masked` are known) and release the store mutex during the build, which means the same in-flight dedup.
oneandonlydean commented 2026-10-14 01:06:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T01:06:14Z.


CachedPipeline builds the pipeline while holding Pipelines().mutex (Pipeline.cpp:903), so another draw that looks up any pipeline under the GPU lock still waits out the whole compile. Please create it outside that lock and share one in-flight creation per key, as #4897 does for compute pipelines.
PrewarmPipeline also guesses the draw's key: the attachment layout from APS5_SYNC_DRAWS alone (Draw takes it from recorded), and no proxied-target formats. When the guess is wrong, the pipeline is compiled twice. Please get the inputs from the code Draw uses rather than a copy.
The test should cover that: a draw after the prewarm creates nothing, and a lookup during a creation doesn't wait for it.
Please use the current PR template (Based on main, Unblocks, GPUs tested) and replace the table with the summary lines of main and of this PR from the same scene, with the traces.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158567872 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5441#issuecomment-6158567872). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T01:06:14Z. --- `CachedPipeline` builds the pipeline while holding `Pipelines().mutex` (Pipeline.cpp:903), so another draw that looks up any pipeline under the GPU lock still waits out the whole compile. Please create it outside that lock and share one in-flight creation per key, as #4897 does for compute pipelines. `PrewarmPipeline` also guesses the draw's key: the attachment layout from `APS5_SYNC_DRAWS` alone (Draw takes it from `recorded`), and no proxied-target formats. When the guess is wrong, the pipeline is compiled twice. Please get the inputs from the code `Draw` uses rather than a copy. The test should cover that: a draw after the prewarm creates nothing, and a lookup during a creation doesn't wait for it. Please use the current PR template (Based on main, Unblocks, GPUs tested) and replace the table with the summary lines of main and of this PR from the same scene, with the traces.
Kikoarl commented 2026-10-14 11:58:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-14T11:58:37Z.


putting this pr on hold while i re-evaluate telemetry and profiling on full in-engine scenes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6167810987 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5441#issuecomment-6167810987). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-14T11:58:37Z. --- putting this pr on hold while i re-evaluate telemetry and profiling on full in-engine scenes.
oneandonlydean commented 2026-10-17 00:41:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T00:41:27Z.


Understood, on hold. The 10-14 points stand for when you pick it up: create the pipeline outside Pipelines().mutex, and share one in-flight creation per key. It conflicts with main now, so rebase first.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6227471703 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5441#issuecomment-6227471703). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-17T00:41:27Z. --- Understood, on hold. The 10-14 points stand for when you pick it up: create the pipeline outside `Pipelines().mutex`, and share one in-flight creation per key. It conflicts with main now, so rebase first.

Pull request closed

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!5441
No description provided.