perf(agc): link graphics pipelines from per-stage libraries #2636

Merged
adriadam10 merged 1 commit from upstream-pr/2636 into main 2026-10-10 09:39:12 +00:00
adriadam10 commented 2026-10-10 09:31:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T09:31:10Z.


What

Graphics pipelines are linked from VK_EXT_graphics_pipeline_library parts that are cached per device and reused across pipelines, instead of being created whole.

Today a draw with a new state combination calls vkCreateGraphicsPipelines, even when its shaders were already compiled for another pipeline. That happens with another blend, cull mode or vertex layout, for example. Drivers don't reuse the stages: on RADV, a second pipeline with the same shaders and only cull + blend changed costs as much as the first (median 10.4 vs 10.9 ms over 60 Astro's Playroom modules, 34 vs 32 ms with RADV's cache off).

With this change, Pipeline builds four libraries (vertex input, pre-rasterization shaders, fragment shader, fragment output), each keyed by exactly the state it is built from, and fast-links them. Cull mode, front face, depth and stencil state become dynamic (VK_EXT_extended_dynamic_state), so changing them only links. Another blend builds only the fragment output library, and another vertex layout only the vertex input one. The new PipelineLibrary.cpp keeps the libraries per device, with their own identical render pass and pipeline layout, since they outlive the pipeline that first needed them. ClearCachedPipelines destroys them with the pipelines.

When it's used: the device must report graphicsPipelineLibrary, graphicsPipelineLibraryFastLinking and extendedDynamicState (VulkanDevice enables the three extensions only then). Pipelines with per-face depth bias, rect lists, geometry shaders or a stage without a variant id, and APS5_NO_GPL=1, keep today's whole-pipeline path.

This is phase A of the plan in Discussion #2242: GPL at the draw, independent of steps 1-3. Creating the shader libraries at registration (phase B) needs stable per-stage layouts and comes later.

Tested

Linux, AMD Radeon 660M (RADV, Mesa 26.2.4), and CI's lavapipe (Mesa 25.2.8).

result
ctest -R '^agc_|^shader', GPL on (default) 319/319
same with APS5_NO_GPL=1 (today's path) 319/319
same on CI's lavapipe (GPL on), without the 4-minute buffer_format 310/310
same under VK_LAYER_KHRONOS_validation, GPL on and off no VUID only with GPL (both report the same 3 already on main: VUID-vkCmdPipelineBarrier-dstAccessMask-02816, VUID-vkCmdPipelineBarrier-srcAccessMask-02815, VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605)
pipelines in that run built from libraries (temporary trace, not in the PR) 157 linked (142 from four parts, 15 mesh from three), 14 kept whole (per-face bias, rect list, geometry)

New tests: agc_pipeline_library (with the libraries) and agc_pipeline_library_off (APS5_NO_GPL=1). The first checks with the per-device counters that, for the same shaders:

  • another blend builds only the fragment output library;
  • another cull mode and front face build no library;
  • another vertex layout builds only the vertex input library;
  • another pixel shader builds only the fragment shader library;
  • clearing the pipelines drops the libraries.

It fails with "a cull mode change rebuilt a library instead of setting dynamic state" if the cull mode is put back in the pre-rasterization key. The test device in Recorder.cpp now enables GPL the same way VulkanDevice does, so the recorder tests also draw through libraries.

Second pipeline cost, same shaders, cull and blend changed (offline bench on the 60 Astro's Playroom modules, RADV):

median max
whole pipeline (main) 10.9 ms
new fragment output library + fast link 0.039 ms 0.077 ms

On NVIDIA (RTX 4090, 615.71, Raghav314's run on PPSA36082's modules in #2242), the fast link costs 0.003 ms and the vertex input / fragment output libraries under 0.01 ms each. The fast-linked code is identical to the whole pipeline's (pipeline executable statistics), except for a vertex stage whose outputs the pixel stage doesn't read: that keeps them (up to 68 registers instead of 38). Recorded in TechnicalDebt, together with the render-pass compatibility limit (targets of other formats build the libraries again).

Cost per bind: 13 more dynamic-state commands, measured at 62 ns per bind on RADV (80 ns instead of 18 ns, 100 000 binds, median of 7).

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 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:4810216624 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2636). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T09:31:10Z. --- ### What Graphics pipelines are linked from `VK_EXT_graphics_pipeline_library` parts that are cached per device and reused across pipelines, instead of being created whole. Today a draw with a new state combination calls `vkCreateGraphicsPipelines`, even when its shaders were already compiled for another pipeline. That happens with another blend, cull mode or vertex layout, for example. Drivers don't reuse the stages: on RADV, a second pipeline with the same shaders and only cull + blend changed costs as much as the first (median 10.4 vs 10.9 ms over 60 Astro's Playroom modules, 34 vs 32 ms with RADV's cache off). With this change, `Pipeline` builds four libraries (vertex input, pre-rasterization shaders, fragment shader, fragment output), each keyed by exactly the state it is built from, and fast-links them. Cull mode, front face, depth and stencil state become dynamic (`VK_EXT_extended_dynamic_state`), so changing them only links. Another blend builds only the fragment output library, and another vertex layout only the vertex input one. The new `PipelineLibrary.cpp` keeps the libraries per device, with their own identical render pass and pipeline layout, since they outlive the pipeline that first needed them. `ClearCachedPipelines` destroys them with the pipelines. When it's used: the device must report `graphicsPipelineLibrary`, `graphicsPipelineLibraryFastLinking` and `extendedDynamicState` (`VulkanDevice` enables the three extensions only then). Pipelines with per-face depth bias, rect lists, geometry shaders or a stage without a variant id, and `APS5_NO_GPL=1`, keep today's whole-pipeline path. This is phase A of the plan in Discussion #2242: GPL at the draw, independent of steps 1-3. Creating the shader libraries at registration (phase B) needs stable per-stage layouts and comes later. ### Tested Linux, AMD Radeon 660M (RADV, Mesa 26.2.4), and CI's lavapipe (Mesa 25.2.8). | | result | |---|---| | `ctest -R '^agc_\|^shader'`, GPL on (default) | 319/319 | | same with `APS5_NO_GPL=1` (today's path) | 319/319 | | same on CI's lavapipe (GPL on), without the 4-minute `buffer_format` | 310/310 | | same under `VK_LAYER_KHRONOS_validation`, GPL on and off | no VUID only with GPL (both report the same 3 already on main: `VUID-vkCmdPipelineBarrier-dstAccessMask-02816`, `VUID-vkCmdPipelineBarrier-srcAccessMask-02815`, `VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605`) | | pipelines in that run built from libraries (temporary trace, not in the PR) | 157 linked (142 from four parts, 15 mesh from three), 14 kept whole (per-face bias, rect list, geometry) | New tests: `agc_pipeline_library` (with the libraries) and `agc_pipeline_library_off` (`APS5_NO_GPL=1`). The first checks with the per-device counters that, for the same shaders: - another blend builds only the fragment output library; - another cull mode and front face build no library; - another vertex layout builds only the vertex input library; - another pixel shader builds only the fragment shader library; - clearing the pipelines drops the libraries. It fails with "a cull mode change rebuilt a library instead of setting dynamic state" if the cull mode is put back in the pre-rasterization key. The test device in `Recorder.cpp` now enables GPL the same way `VulkanDevice` does, so the recorder tests also draw through libraries. Second pipeline cost, same shaders, cull and blend changed (offline bench on the 60 Astro's Playroom modules, RADV): | | median | max | |---|---|---| | whole pipeline (main) | 10.9 ms | | | new fragment output library + fast link | 0.039 ms | 0.077 ms | On NVIDIA (RTX 4090, 615.71, Raghav314's run on PPSA36082's modules in #2242), the fast link costs 0.003 ms and the vertex input / fragment output libraries under 0.01 ms each. The fast-linked code is identical to the whole pipeline's (pipeline executable statistics), except for a vertex stage whose outputs the pixel stage doesn't read: that keeps them (up to 68 registers instead of 38). Recorded in TechnicalDebt, together with the render-pass compatibility limit (targets of other formats build the libraries again). Cost per bind: 13 more dynamic-state commands, measured at 62 ns per bind on RADV (80 ns instead of 18 ns, 100 000 binds, median of 7). ### 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 - [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
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!2636
No description provided.