fix(agc): drop the vertex gl_Layer output from barycentric patch pipelines #16891

Merged
frankea merged 4 commits from upstream-pr/16891 into main 2026-11-12 23:15:36 +00:00
frankea commented 2026-11-07 20:48:58 +00:00 (Migrated from github.com)

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


What

On a device without geometry shaders (MoltenVK), a pixel shader that emulates barycentrics runs behind generated tessellation stages: a pass-through control shader that copies gl_Position, gl_ClipDistance and the parameters the pixel shader reads, and an evaluation shader that selects the corner. When the vertex shader also writes gl_Layer, MoltenVK reads the wrong control-point positions; in the new test none of these triangles is drawn. This PR creates the vertex module of a barycentric patch pipeline with gl_Layer demoted to a private variable. It also creates these pipelines without the VkPipelineCache on MoltenVK, as guest tessellation pipelines already are since 5daac82d32.

Why

MoltenVK runs the vertex shader as a compute kernel that captures its outputs to a buffer, and the control shader reads that buffer through its own input struct. The two structs disagree when the vertex shader writes a built-in output that the control shader does not read. The control shader cannot read gl_Layer. MoltenVK passes gl_Layer to SPIRV-Cross as a previous-stage output at the location its reflection gave it, and SPIRV-Cross adds a padding member for it (spirv_msl.cpp, the "add all outputs from the previous stage" loop for tessellation inputs). That member is not marked as a built-in, so the LocationThenBuiltInType sort puts it 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 vertex shader with four parameters, three clip distances and a layer:

  • vertex kernel output: out_param_0..3, gl_Position at offset 64, gl_ClipDistance[6], gl_Layer, 112-byte records;
  • control shader input: m_40, m_83, m_88, m_93, uint m_98, gl_Position at offset 80, gl_ClipDistance[6], 128-byte records.

The control shader therefore reads gl_Position from the clip distances, at the wrong stride. In the new test every layer draw leaves all of its pixels unwritten, with a fresh pipeline cache too, so it needs no cache reuse.

Dropping the layer changes nothing that is rendered. In Vulkan the layer comes from the last pre-rasterization stage. The generated evaluation shader writes no layer, so these draws already render to the first layer on every driver.

The pipeline cache change is needed as well. MoltenVK reuses a cached shader conversion when the new pipeline has every interface entry that the cached conversion marked as used (SPIRVToMSLConversionConfiguration::matches, outIsUsedByShader), and the padding members are not marked as used. The generated control shader is the same for every vertex shader that feeds the same parameters. A control conversion padded for gl_Layer in a VkPipelineCache saved before this change therefore still matches after it. Measured: a persisted pipeline cache written by main, then loaded by a build with only the first commit, still leaves the one-parameter layer draws of the new test unwritten (6 failed checks; the four-parameter cases pass). With the second fix the same stale cache file passes all 8 cases. The new test itself does not exercise this, because it starts without a persisted cache.

Found on Gran Turismo 7 (PPSA01317) on an M5 Max. Seven forward-blended glass draws in the cafe scene, from vertex shaders 0x82c4402700 and 0x82c440ba00, export four parameters, three clip distances and gl_Layer. On the barycentric tessellation route they draw white wedges that converge at the screen centre. When the same pixel shaders read PerVertexKHR inputs natively (no generated stages), the wedges go away. The guest vertex data was dumped and evaluated, and it is correct. Confirmed in game on the M5 Max (macOS 27.0.1, local MoltenVK 1.4.2): a local build with both fixes, on the default emulated barycentric route, reached the cafe presentation with no white wedges and the car glass visible. Over 30 frame dumps per dialog line, the median share of fully clamped white pixels where the wedges appear was 0.5%, against 47% with the same local build without the two fixes. So the layout mismatch caused the wedges.

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.

  • New agc_driver_barycentric_patch_vertex_layer. It draws a triangle through the generated 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.
    • On main it fails 9 checks on both ICDs: every layer draw leaves all 932 interior pixels unwritten.
    • With this PR it passes all 8 cases on both ICDs.
  • Unchanged by this PR, same result on main and on this PR on both ICDs:
    • pass: agc_driver_pixel_barycentric_emulation, agc_driver_rect_list_corner, 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 on main and on this PR alike on this Mac, unrelated to this PR:
    • agc_driver_tessellation: the hull's relative patch index across instances;
    • agc_driver_rect_list_indirect: rect-list position refusal;
    • agc_driver_provoking_vertex: provokingVertexLast is unsupported.
  • With VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (local ICD), main and this PR, the layer loaded in every run:
    • suites: the new test, pixel_barycentric_emulation, rect_list_corner, zero_position_vertex, layered_array_color_draw and vertex_clip_cull_draw;
    • no new VUID-: the only ones are 2 VUID-VkGraphicsPipelineCreateInfo-topology-08773 in zero_position_vertex, present on both.
  • The changed files are guard files, so the shader cache source version does not change.

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
  • 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:5205715838 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16891). Originally posted by [frankea](https://github.com/frankea) on 2026-11-07T20:48:58Z. --- ### What On a device without geometry shaders (MoltenVK), a pixel shader that emulates barycentrics runs behind generated tessellation stages: a pass-through control shader that copies gl_Position, gl_ClipDistance and the parameters the pixel shader reads, and an evaluation shader that selects the corner. When the vertex shader also writes gl_Layer, MoltenVK reads the wrong control-point positions; in the new test none of these triangles is drawn. This PR creates the vertex module of a barycentric patch pipeline with gl_Layer demoted to a private variable. It also creates these pipelines without the VkPipelineCache on MoltenVK, as guest tessellation pipelines already are since 5daac82d32. ### Why MoltenVK runs the vertex shader as a compute kernel that captures its outputs to a buffer, and the control shader reads that buffer through its own input struct. The two structs disagree when the vertex shader writes a built-in output that the control shader does not read. The control shader cannot read gl_Layer. MoltenVK passes gl_Layer to SPIRV-Cross as a previous-stage output at the location its reflection gave it, and SPIRV-Cross adds a padding member for it (spirv_msl.cpp, the "add all outputs from the previous stage" loop for tessellation inputs). That member is not marked as a built-in, so the LocationThenBuiltInType sort puts it 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 vertex shader with four parameters, three clip distances and a layer: - vertex kernel output: `out_param_0..3`, `gl_Position` at offset 64, `gl_ClipDistance[6]`, `gl_Layer`, 112-byte records; - control shader input: `m_40`, `m_83`, `m_88`, `m_93`, `uint m_98`, `gl_Position` at offset 80, `gl_ClipDistance[6]`, 128-byte records. The control shader therefore reads gl_Position from the clip distances, at the wrong stride. In the new test every layer draw leaves all of its pixels unwritten, with a fresh pipeline cache too, so it needs no cache reuse. Dropping the layer changes nothing that is rendered. In Vulkan the layer comes from the last pre-rasterization stage. The generated evaluation shader writes no layer, so these draws already render to the first layer on every driver. The pipeline cache change is needed as well. MoltenVK reuses a cached shader conversion when the new pipeline has every interface entry that the cached conversion marked as used (`SPIRVToMSLConversionConfiguration::matches`, `outIsUsedByShader`), and the padding members are not marked as used. The generated control shader is the same for every vertex shader that feeds the same parameters. A control conversion padded for gl_Layer in a VkPipelineCache saved before this change therefore still matches after it. Measured: a persisted pipeline cache written by main, then loaded by a build with only the first commit, still leaves the one-parameter layer draws of the new test unwritten (6 failed checks; the four-parameter cases pass). With the second fix the same stale cache file passes all 8 cases. The new test itself does not exercise this, because it starts without a persisted cache. Found on Gran Turismo 7 (PPSA01317) on an M5 Max. Seven forward-blended glass draws in the cafe scene, from vertex shaders 0x82c4402700 and 0x82c440ba00, export four parameters, three clip distances and gl_Layer. On the barycentric tessellation route they draw white wedges that converge at the screen centre. When the same pixel shaders read PerVertexKHR inputs natively (no generated stages), the wedges go away. The guest vertex data was dumped and evaluated, and it is correct. [Confirmed in game](https://github.com/boykopovar/AnyPS5/pull/16891#issuecomment-6713793033) on the M5 Max (macOS 27.0.1, local MoltenVK 1.4.2): a local build with both fixes, on the default emulated barycentric route, reached the cafe presentation with no white wedges and the car glass visible. Over 30 frame dumps per dialog line, the median share of fully clamped white pixels where the wedges appear was 0.5%, against 47% with the same local build without the two fixes. So the layout mismatch caused the wedges. ### 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. - New `agc_driver_barycentric_patch_vertex_layer`. It draws a triangle through the generated 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. - On main it fails 9 checks on both ICDs: every layer draw leaves all 932 interior pixels unwritten. - With this PR it passes all 8 cases on both ICDs. - Unchanged by this PR, same result on main and on this PR on both ICDs: - pass: `agc_driver_pixel_barycentric_emulation`, `agc_driver_rect_list_corner`, `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 on main and on this PR alike on this Mac, unrelated to this PR: - `agc_driver_tessellation`: the hull's relative patch index across instances; - `agc_driver_rect_list_indirect`: rect-list position refusal; - `agc_driver_provoking_vertex`: provokingVertexLast is unsupported. - With `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (local ICD), main and this PR, the layer loaded in every run: - suites: the new test, `pixel_barycentric_emulation`, `rect_list_corner`, `zero_position_vertex`, `layered_array_color_draw` and `vertex_clip_cull_draw`; - no new `VUID-`: the only ones are 2 `VUID-VkGraphicsPipelineCreateInfo-topology-08773` in `zero_position_vertex`, present on both. - The changed files are guard files, so the shader cache source version does not change. ### 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: none - [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-07 22:36:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T22:36:04Z.

Review: COMMENTED


Non-MoltenVK cells, to check the demoted gl_Layer and the generated stages on drivers that read the interface correctly. Head 673188baf4 merged onto main 302864d48c (clean). Windows 11 x64, WinLibs GCC 15.2.0 r7, Release, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, 3 runs per GPU.

test NVIDIA laptop GPU (Turing), driver 610 Intel iGPU
agc_driver_barycentric_patch_vertex_layer (new) pass 3/3 pass 3/3
agc_driver_pixel_barycentric_emulation pass, as main pass, as main
agc_fragment_barycentric pass, as main pass, as main
agc_driver_mesh_barycentric_emulation pass, as main skipped (no mesh shaders), as main

The new test builds the tessellation stages itself through BuildBarycentricTessellationShaders, so it does exercise the patch pipeline here even though these devices have geometry shaders. So the change keeps every case right on two drivers that never had the mismatch; it can't show the MoltenVK fix itself. No validation layer on this machine.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5815378900 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16891#pullrequestreview-5815378900). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T22:36:04Z. **Review:** COMMENTED --- Non-MoltenVK cells, to check the demoted gl_Layer and the generated stages on drivers that read the interface correctly. Head `673188baf4` merged onto `main` `302864d48c` (clean). Windows 11 x64, WinLibs GCC 15.2.0 r7, Release, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`, 3 runs per GPU. | test | NVIDIA laptop GPU (Turing), driver 610 | Intel iGPU | |---|---|---| | `agc_driver_barycentric_patch_vertex_layer` (new) | pass 3/3 | pass 3/3 | | `agc_driver_pixel_barycentric_emulation` | pass, as `main` | pass, as `main` | | `agc_fragment_barycentric` | pass, as `main` | pass, as `main` | | `agc_driver_mesh_barycentric_emulation` | pass, as `main` | skipped (no mesh shaders), as `main` | The new test builds the tessellation stages itself through `BuildBarycentricTessellationShaders`, so it does exercise the patch pipeline here even though these devices have geometry shaders. So the change keeps every case right on two drivers that never had the mismatch; it can't show the MoltenVK fix itself. No validation layer on this machine. 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!16891
No description provided.