fix(agc): drop the vertex gl_Layer output from rect-list pipelines #16923

Merged
frankea merged 5 commits from upstream-pr/16923 into main 2026-11-15 19:52:26 +00:00
frankea commented 2026-11-07 22:38:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-11-07T22:38:58Z.


What

This is the rect-list counterpart of #16891 and is stacked on it: it reuses DemoteVertexLayerOutput from Graphics/include/PatchVertexLayer.hpp, which #16891 adds, so this branch starts at the #16891 head 673188baf4 (its three commits and a merge of main). Only the last three commits are new: a test and two one-line changes in Graphics/src/Pipeline.cpp.

A rect-list draw runs the guest vertex shader in front of a generated tessellation control shader (BuildRectListShaders). That control shader reads gl_Position, gl_ClipDistance and the parameters the pixel shader uses, and checks that the three corners form an axis-aligned rectangle. When the vertex shader also writes gl_Layer, MoltenVK reads the corners at the wrong offset, and on this route the check then fails: "rect-list requires finite nondegenerate axis-aligned positions with equal positive W". This PR creates the vertex module of a rect-list pipeline with gl_Layer demoted to a private variable, as #16891 does for barycentric patch pipelines. It also creates rect-list pipelines without the VkPipelineCache on MoltenVK, as guest tessellation and barycentric patch pipelines already are.

Why

The mechanism is the one #16891 describes. MoltenVK runs the vertex shader as a compute kernel that captures its outputs to a buffer. The control shader cannot read gl_Layer, so SPIRV-Cross pads the control input struct with a member for it, and that member is sorted among the user locations, before gl_Position. The vertex kernel's own struct keeps gl_Layer as a built-in after gl_ClipDistance.

MSL that the local MoltenVK generated (MVK_CONFIG_DEBUG=1) for a rect-list pipeline whose vertex shader writes one parameter, three clip distances and a layer:

  • vertex kernel output: out_param_0, gl_Position, gl_ClipDistance[3], uint gl_Layer;
  • rect-list control shader input: m_32, uint m_326, gl_Position, gl_ClipDistance[3].

The control shader therefore reads gl_Position 16 bytes late and at the wrong stride. This happens with a fresh pipeline cache as well, so no cache reuse is needed to hit it.

Dropping the layer changes nothing that is rendered. In Vulkan the layer comes from the last pre-rasterization stage. The generated rect-list evaluation shader writes no layer, so these draws already render to the first layer on every driver. Forwarding the layer through the rect-list stages is a separate change and is not part of this PR.

The pipeline cache change is needed for the same reason as in #16891. MoltenVK reuses a cached shader conversion when the new pipeline has every interface entry that the cached conversion marked as used, and padding members are not marked as used. The rect-list control shader is the same for every vertex shader that feeds the same parameters. Measured: a pipeline cache persisted by the build without this change, then loaded by a build with only the demotion commit, still fails the one-parameter layer cases of the new test (6 failed checks). With the cache bypass as well, the same stale cache file passes all 8 cases. The new test itself does not exercise this, because it starts without a persisted cache. Rect-list pipelines never use pipeline libraries, so no un-demoted library module can be linked in. The cost is that on MoltenVK every rect-list pipeline now converts its shaders to MSL once per session instead of loading the conversion from the persisted cache; rect-list draws are common, and this first-use cost was not measured in a title.

Quad-list draws do not take this route. They are expanded to triangle lists on the host and go through no generated control shader.

Tested

macOS 27.0.1 on an Apple M5 Max (Rosetta x86-64 build), with MoltenVK 1.4.2 from Vulkan SDK 1.4.363.0 and with a local MoltenVK 1.4.2 build. All runs used ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1, on both ICDs. "Before" is the #16891 head 673188baf4 with the new test added.

  • New agc_driver_rect_list_vertex_layer. It draws a rectangle through the rect-list stages, reading per-vertex values with v_interp_mov, for vertex shaders with and without gl_Layer, next to clip distances and up to four parameters. Each draw runs once with a fresh pipeline cache and once after the other vertex shader's pipeline seeded the cache. Every pixel's coverage and values are checked.
    • Before, it fails 9 checks on both ICDs: every draw of a layer vertex shader is refused by the rect-list position check. All draws without a layer pass, including the seeded ones.
    • With this PR it passes all 8 cases on both ICDs.
    • With only the cache bypass commit it still fails 9 checks, so the test covers the demotion commit.
  • Unchanged by this PR, same result before and with it on both ICDs:
    • pass: agc_driver_pixel_barycentric_emulation, agc_driver_rect_list_corner, agc_driver_barycentric_patch_vertex_layer, agc_driver_pipeline_journal, agc_driver_layered_array_color_draw, agc_driver_layered_volume_draw, agc_driver_zero_position_vertex, agc_driver_vertex_clip_cull_draw, agc_tessellation_pipeline_cache, agc_fragment_barycentric;
    • pass with APS5_NO_GPL=1: agc_driver_draw_cache_reuse, agc_driver_draw_cache_relocation, agc_driver_draw_data_hit_user_words;
    • skip: agc_driver_mesh_barycentric_emulation and agc_driver_tessellation_geometry (no mesh or geometry shaders).
  • Fail before and with this PR alike on this Mac, as on main in #16891:
    • agc_driver_tessellation: the hull's relative patch index across instances;
    • agc_driver_rect_list_indirect: the same rect-list position refusal, but its vertex shaders write no layer, so it is not this bug. It aborts on its first refused draw, so it covers neither result;
    • agc_driver_provoking_vertex: provokingVertexLast is unsupported.
  • With VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (local ICD) the layer loaded in every run, and the new test, rect_list_corner, pixel_barycentric_emulation and barycentric_patch_vertex_layer pass with no VUID- reported.
  • The changed file is a guard file, so the shader cache source version does not change.
  • The cache bypass applies only on MoltenVK. The demotion applies on every driver, where the evaluation stage already decides the layer. gabichuuuuu ran this head d117ed22d4 on native drivers, merged on main 302864d48c, Windows 11 on an NVIDIA Turing laptop GPU (driver 610) and an Intel iGPU (driver 101.70xx), with the validation layer: the new agc_driver_rect_list_vertex_layer, agc_driver_barycentric_patch_vertex_layer, agc_driver_rect_list_corner, agc_driver_rect_list_indirect and agc_driver_pixel_barycentric_emulation all pass in two runs on each GPU, with no VUID- on either side. The two layer tests also pass there without the Pipeline.cpp changes, as expected for a padding layout that only SPIRV-Cross produces, so they do not fail falsely on native drivers.

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: #16891
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M5 Max, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0 and a local MoltenVK 1.4.2 build), macOS 27.0.1
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5206505636 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16923). Originally posted by [frankea](https://github.com/frankea) on 2026-11-07T22:38:58Z. --- ### What This is the rect-list counterpart of #16891 and is stacked on it: it reuses `DemoteVertexLayerOutput` from `Graphics/include/PatchVertexLayer.hpp`, which #16891 adds, so this branch starts at the #16891 head 673188baf4 (its three commits and a merge of main). Only the last three commits are new: a test and two one-line changes in `Graphics/src/Pipeline.cpp`. A rect-list draw runs the guest vertex shader in front of a generated tessellation control shader (`BuildRectListShaders`). That control shader reads gl_Position, gl_ClipDistance and the parameters the pixel shader uses, and checks that the three corners form an axis-aligned rectangle. When the vertex shader also writes gl_Layer, MoltenVK reads the corners at the wrong offset, and on this route the check then fails: "rect-list requires finite nondegenerate axis-aligned positions with equal positive W". This PR creates the vertex module of a rect-list pipeline with gl_Layer demoted to a private variable, as #16891 does for barycentric patch pipelines. It also creates rect-list pipelines without the VkPipelineCache on MoltenVK, as guest tessellation and barycentric patch pipelines already are. ### Why The mechanism is the one #16891 describes. MoltenVK runs the vertex shader as a compute kernel that captures its outputs to a buffer. The control shader cannot read gl_Layer, so SPIRV-Cross pads the control input struct with a member for it, and that member is sorted among the user locations, before gl_Position. The vertex kernel's own struct keeps gl_Layer as a built-in after gl_ClipDistance. MSL that the local MoltenVK generated (MVK_CONFIG_DEBUG=1) for a rect-list pipeline whose vertex shader writes one parameter, three clip distances and a layer: - vertex kernel output: `out_param_0`, `gl_Position`, `gl_ClipDistance[3]`, `uint gl_Layer`; - rect-list control shader input: `m_32`, `uint m_326`, `gl_Position`, `gl_ClipDistance[3]`. The control shader therefore reads gl_Position 16 bytes late and at the wrong stride. This happens with a fresh pipeline cache as well, so no cache reuse is needed to hit it. Dropping the layer changes nothing that is rendered. In Vulkan the layer comes from the last pre-rasterization stage. The generated rect-list evaluation shader writes no layer, so these draws already render to the first layer on every driver. Forwarding the layer through the rect-list stages is a separate change and is not part of this PR. The pipeline cache change is needed for the same reason as in #16891. MoltenVK reuses a cached shader conversion when the new pipeline has every interface entry that the cached conversion marked as used, and padding members are not marked as used. The rect-list control shader is the same for every vertex shader that feeds the same parameters. Measured: a pipeline cache persisted by the build without this change, then loaded by a build with only the demotion commit, still fails the one-parameter layer cases of the new test (6 failed checks). With the cache bypass as well, the same stale cache file passes all 8 cases. The new test itself does not exercise this, because it starts without a persisted cache. Rect-list pipelines never use pipeline libraries, so no un-demoted library module can be linked in. The cost is that on MoltenVK every rect-list pipeline now converts its shaders to MSL once per session instead of loading the conversion from the persisted cache; rect-list draws are common, and this first-use cost was not measured in a title. Quad-list draws do not take this route. They are expanded to triangle lists on the host and go through no generated control shader. ### Tested macOS 27.0.1 on an Apple M5 Max (Rosetta x86-64 build), with MoltenVK 1.4.2 from Vulkan SDK 1.4.363.0 and with a local MoltenVK 1.4.2 build. All runs used `ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1`, on both ICDs. "Before" is the #16891 head 673188baf4 with the new test added. - New `agc_driver_rect_list_vertex_layer`. It draws a rectangle through the rect-list stages, reading per-vertex values with v_interp_mov, for vertex shaders with and without gl_Layer, next to clip distances and up to four parameters. Each draw runs once with a fresh pipeline cache and once after the other vertex shader's pipeline seeded the cache. Every pixel's coverage and values are checked. - Before, it fails 9 checks on both ICDs: every draw of a layer vertex shader is refused by the rect-list position check. All draws without a layer pass, including the seeded ones. - With this PR it passes all 8 cases on both ICDs. - With only the cache bypass commit it still fails 9 checks, so the test covers the demotion commit. - Unchanged by this PR, same result before and with it on both ICDs: - pass: `agc_driver_pixel_barycentric_emulation`, `agc_driver_rect_list_corner`, `agc_driver_barycentric_patch_vertex_layer`, `agc_driver_pipeline_journal`, `agc_driver_layered_array_color_draw`, `agc_driver_layered_volume_draw`, `agc_driver_zero_position_vertex`, `agc_driver_vertex_clip_cull_draw`, `agc_tessellation_pipeline_cache`, `agc_fragment_barycentric`; - pass with `APS5_NO_GPL=1`: `agc_driver_draw_cache_reuse`, `agc_driver_draw_cache_relocation`, `agc_driver_draw_data_hit_user_words`; - skip: `agc_driver_mesh_barycentric_emulation` and `agc_driver_tessellation_geometry` (no mesh or geometry shaders). - Fail before and with this PR alike on this Mac, as on main in #16891: - `agc_driver_tessellation`: the hull's relative patch index across instances; - `agc_driver_rect_list_indirect`: the same rect-list position refusal, but its vertex shaders write no layer, so it is not this bug. It aborts on its first refused draw, so it covers neither result; - `agc_driver_provoking_vertex`: provokingVertexLast is unsupported. - With `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (local ICD) the layer loaded in every run, and the new test, `rect_list_corner`, `pixel_barycentric_emulation` and `barycentric_patch_vertex_layer` pass with no `VUID-` reported. - The changed file is a guard file, so the shader cache source version does not change. - The cache bypass applies only on MoltenVK. The demotion applies on every driver, where the evaluation stage already decides the layer. gabichuuuuu [ran this head d117ed22d4 on native drivers](https://github.com/boykopovar/AnyPS5/pull/16923#pullrequestreview-5815972674), merged on `main` 302864d48c, Windows 11 on an NVIDIA Turing laptop GPU (driver 610) and an Intel iGPU (driver 101.70xx), with the validation layer: the new `agc_driver_rect_list_vertex_layer`, `agc_driver_barycentric_patch_vertex_layer`, `agc_driver_rect_list_corner`, `agc_driver_rect_list_indirect` and `agc_driver_pixel_barycentric_emulation` all pass in two runs on each GPU, with no `VUID-` on either side. The two layer tests also pass there without the `Pipeline.cpp` changes, as expected for a padding layout that only SPIRV-Cross produces, so they do not fail falsely on native drivers. ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/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: #16891 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M5 Max, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0 and a local MoltenVK 1.4.2 build), macOS 27.0.1 - [x] AI-assisted: yes (Claude Code)
gabichuuuuu commented 2026-11-08 01:39:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T01:39:41Z.

Review: COMMENTED


Ran d117ed22d4 (so #16891 as well) merged on main 302864d48c on Windows 11 x64 (WinLibs GCC 15.2), on an NVIDIA laptop GPU (Turing, driver 610) and an Intel iGPU (driver 101.70xx), with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (Khronos validation layer 1.4.368), ctest -R '^agc_driver_(rect_list|barycentric_patch_vertex_layer|pixel_barycentric_emulation)' -V.

This only checks that native drivers are unaffected; the failure itself is MoltenVK's.

main main + the two new test files only change
NVIDIA 3/3 5/5 5/5, twice
Intel 3/3 5/5 5/5, twice
  • On both GPUs agc_driver_rect_list_vertex_layer and agc_driver_barycentric_patch_vertex_layer pass even without the Pipeline.cpp changes, so here nothing reads gl_Position at the wrong offset, as expected for a padding layout only SPIRV-Cross produces. The tests also don't fail falsely on native drivers.
  • agc_driver_rect_list_corner, agc_driver_rect_list_indirect and agc_driver_pixel_barycentric_emulation pass on both sides.
  • The layer was inserted in every test, no log has was not found but was requested by env var VK_INSTANCE_LAYERS, and no log on either side has a VUID-, so dropping the vertex gl_Layer output doesn't trip an interface-matching check on these drivers. To be sure validation messages reach these logs, the same layer with best practices on printed 28 BestPractices- warnings into an agc_driver_recorder log on each GPU.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5815972674 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16923#pullrequestreview-5815972674). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T01:39:41Z. **Review:** COMMENTED --- Ran `d117ed22d4` (so #16891 as well) merged on `main` 302864d48c on Windows 11 x64 (WinLibs GCC 15.2), on an NVIDIA laptop GPU (Turing, driver 610) and an Intel iGPU (driver 101.70xx), with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (Khronos validation layer 1.4.368), `ctest -R '^agc_driver_(rect_list|barycentric_patch_vertex_layer|pixel_barycentric_emulation)' -V`. This only checks that native drivers are unaffected; the failure itself is MoltenVK's. | | `main` | `main` + the two new test files only | change | |---|---|---|---| | NVIDIA | 3/3 | 5/5 | 5/5, twice | | Intel | 3/3 | 5/5 | 5/5, twice | - On both GPUs `agc_driver_rect_list_vertex_layer` and `agc_driver_barycentric_patch_vertex_layer` pass even without the `Pipeline.cpp` changes, so here nothing reads gl_Position at the wrong offset, as expected for a padding layout only SPIRV-Cross produces. The tests also don't fail falsely on native drivers. - `agc_driver_rect_list_corner`, `agc_driver_rect_list_indirect` and `agc_driver_pixel_barycentric_emulation` pass on both sides. - The layer was inserted in every test, no log has `was not found but was requested by env var VK_INSTANCE_LAYERS`, and no log on either side has a `VUID-`, so dropping the vertex gl_Layer output doesn't trip an interface-matching check on these drivers. To be sure validation messages reach these logs, the same layer with best practices on printed 28 `BestPractices-` warnings into an `agc_driver_recorder` log on each GPU. 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!16923
No description provided.