fix(shader): declare FragStencilRefEXT unsigned so MoltenVK compiles it with the stencil output disabled #19966

Merged
XIIYAZ merged 1 commit from upstream-pr/19966 into main 2026-11-22 01:13:24 +00:00
XIIYAZ commented 2026-11-14 09:54:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XIIYAZ on 2026-11-14T09:54:07Z.


What

On MoltenVK, a pixel shader that exports the stencil reference fails to compile when the render pass has no stencil attachment (agc_driver_stencil_ref_export_unused):

[mvk-error] VK_ERROR_INITIALIZATION_FAILED: Shader library compile failed (Error code 3):
program_source:61:5: error: no matching function for call to '_22'

The recompiler declares FragStencilRefEXT as a signed 32-bit integer and writes it from the shader body, which is its own function called from main. Without a stencil attachment MoltenVK has SPIRV-Cross disable the [[stencil]] output, and SPIRV-Cross then declares the replacement local in main with the SPIR-V type (int gl_FragStencilRefARB;) while the body function takes Metal's built-in type (thread uint& gl_FragStencilRefARB), so the call has no match. This reproduces outside AnyPS5 with the SDK's own tools on the SPIR-V MoltenVK logs: spirv-cross --msl --msl-version 30100 --msl-disable-frag-stencil-ref-builtin (vulkan-sdk-1.4.363.0) and xcrun metal give the same error; with the default options (stencil output enabled) it compiles.

VK_EXT_shader_stencil_export only requires a scalar 32-bit integer for FragStencilRefEXT, so this declares the built-in as unsigned:

  • SpirvEmitterHelpers.cpp: the built-in uses TypeU32.
  • SpirvModuleEmitter.cpp: the export stores the raw u32 into the built-in; the replay emulation's private variable stays i32 and keeps its bitcast.
  • ShaderValidation.cpp: the fragment built-in check accepts u32 as well as i32 for FragStencilRefEXT.

With the unsigned type both declarations are uint, and the same SPIR-V edited to uint compiles with and without --msl-disable-frag-stencil-ref-builtin.

This touches the same stencil store in SpirvModuleEmitter.cpp as #10351 (which adds a stencil attachment branch there and is currently conflicting with main); the changes are independent, and whichever lands second keeps both: the attachment branch from #10351, and the raw u32 store for the built-in from this one.

Tested

Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0, full Release build with -DCMAKE_OSX_ARCHITECTURES=x86_64 (tests under Rosetta), ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, on main 290ee1aa0f with the open macOS build fixes (#17425, #11049, #11437, #19903, #19920) applied:

  • agc_driver_stencil_ref_export_unused: fails before with the error above, passes after.
  • All 15 *stencil* tests and the graphics validation unit test pass, including agc_driver_stencil_ref_export, agc_driver_stencil_replay and agc_driver_stencil_replay_no_gpl.
  • With VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer (layer from the same SDK), the 15 stencil tests pass, the loader inserts the layer in each run, no was not found but was requested, and there are no VUID- messages.
  • ctest -R '^(agc|shader|job)', 1578 tests, -j 4, compared with the same run before the change: no test fails that passed before. 13 agc_driver_image_table* tests hit the validation-output regex under -j 4 in the run with the change and pass when rerun serially with it, so I count them as load-dependent.

Not run on lavapipe, AMD or NVIDIA; the change keeps the signed private variable for the replay path that devices without stencil export use.

The two other MoltenVK compile failures with the same shape (agc_driver_pixel_sample_coverage, agc_driver_sample_order: SampleMask input passed into the body function as uint (&)[1] and read without [0]) are a separate SPIRV-Cross path and are not changed here.

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 M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5309930155 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19966). Originally posted by [XIIYAZ](https://github.com/XIIYAZ) on 2026-11-14T09:54:07Z. --- ### What On MoltenVK, a pixel shader that exports the stencil reference fails to compile when the render pass has no stencil attachment (`agc_driver_stencil_ref_export_unused`): ``` [mvk-error] VK_ERROR_INITIALIZATION_FAILED: Shader library compile failed (Error code 3): program_source:61:5: error: no matching function for call to '_22' ``` The recompiler declares `FragStencilRefEXT` as a signed 32-bit integer and writes it from the shader body, which is its own function called from `main`. Without a stencil attachment MoltenVK has SPIRV-Cross disable the `[[stencil]]` output, and SPIRV-Cross then declares the replacement local in `main` with the SPIR-V type (`int gl_FragStencilRefARB;`) while the body function takes Metal's built-in type (`thread uint& gl_FragStencilRefARB`), so the call has no match. This reproduces outside AnyPS5 with the SDK's own tools on the SPIR-V MoltenVK logs: `spirv-cross --msl --msl-version 30100 --msl-disable-frag-stencil-ref-builtin` (vulkan-sdk-1.4.363.0) and `xcrun metal` give the same error; with the default options (stencil output enabled) it compiles. `VK_EXT_shader_stencil_export` only requires a scalar 32-bit integer for `FragStencilRefEXT`, so this declares the built-in as unsigned: - `SpirvEmitterHelpers.cpp`: the built-in uses `TypeU32`. - `SpirvModuleEmitter.cpp`: the export stores the raw `u32` into the built-in; the replay emulation's private variable stays `i32` and keeps its bitcast. - `ShaderValidation.cpp`: the fragment built-in check accepts `u32` as well as `i32` for `FragStencilRefEXT`. With the unsigned type both declarations are `uint`, and the same SPIR-V edited to `uint` compiles with and without `--msl-disable-frag-stencil-ref-builtin`. This touches the same stencil store in `SpirvModuleEmitter.cpp` as #10351 (which adds a stencil attachment branch there and is currently conflicting with `main`); the changes are independent, and whichever lands second keeps both: the attachment branch from #10351, and the raw `u32` store for the built-in from this one. ### Tested Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0, full Release build with `-DCMAKE_OSX_ARCHITECTURES=x86_64` (tests under Rosetta), `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`, on `main` `290ee1aa0f` with the open macOS build fixes (#17425, #11049, #11437, #19903, #19920) applied: - `agc_driver_stencil_ref_export_unused`: fails before with the error above, passes after. - All 15 `*stencil*` tests and the graphics validation unit test pass, including `agc_driver_stencil_ref_export`, `agc_driver_stencil_replay` and `agc_driver_stencil_replay_no_gpl`. - With `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer` (layer from the same SDK), the 15 stencil tests pass, the loader inserts the layer in each run, no `was not found but was requested`, and there are no `VUID-` messages. - `ctest -R '^(agc|shader|job)'`, 1578 tests, `-j 4`, compared with the same run before the change: no test fails that passed before. 13 `agc_driver_image_table*` tests hit the validation-output regex under `-j 4` in the run with the change and pass when rerun serially with it, so I count them as load-dependent. Not run on lavapipe, AMD or NVIDIA; the change keeps the signed private variable for the replay path that devices without stencil export use. The two other MoltenVK compile failures with the same shape (`agc_driver_pixel_sample_coverage`, `agc_driver_sample_order`: `SampleMask` input passed into the body function as `uint (&)[1]` and read without `[0]`) are a separate SPIRV-Cross path and are not changed here. ### 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 M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3) - [x] AI-assisted: yes
kurexw commented 2026-11-16 23:01:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-16T23:01:44Z.

Review: COMMENTED


Tested on macOS 27.2 / Apple M4 with MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1, validation layer 1.4.363), x86-64 build under Rosetta. This is the only GPU I tested.

Head b1c1de1c21 merged alone onto main 3b17977ba6, compared with 3b17977ba6 itself. I ran the 29 AGC tests that fail on main on this Mac, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and VK_LOADER_DEBUG=layer, through ctest --test-dir build -R <those 29> -V, three runs per side. Every Vulkan test logged Insert instance layer "VK_LAYER_KHRONOS_validation" and none logged was not found but was requested by env var VK_INSTANCE_LAYERS.

test main (3 runs) this PR (3 runs)
agc_driver_stencil_ref_export_unused Failed, Failed, Failed Passed, Passed, Passed

The other 27 tests gave the same result on both sides.

agc_driver_label_after_import_copy is flaky on main as well: main Failed, Passed, Passed, this PR Failed, Failed, Failed. Every failure on either side is the same timing check ("round N: a CPU read after the label that followed the copy found K of 262144 destination bytes not yet copied"), and it varied the same way across the other PRs I tested in the same session, so I don't attribute it to this change.

VUID- lines: identical per test on both sides (30 on main, 30 with this PR, over the three runs). The VUID- lines on both sides are main's existing VUID-VkDeviceCreateInfo-sType-unique (a duplicated VkPhysicalDevicePortabilitySubsetFeaturesKHR in the device pNext chain) and VUID-VkSamplerCreateInfo-samplerMipLodBias-04467; this PR adds none.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5929446783 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19966#pullrequestreview-5929446783). Originally posted by [kurexw](https://github.com/kurexw) on 2026-11-16T23:01:44Z. **Review:** COMMENTED --- Tested on macOS 27.2 / Apple M4 with MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1, validation layer 1.4.363), x86-64 build under Rosetta. This is the only GPU I tested. Head `b1c1de1c21` merged alone onto main `3b17977ba6`, compared with `3b17977ba6` itself. I ran the 29 AGC tests that fail on main on this Mac, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` and `VK_LOADER_DEBUG=layer`, through `ctest --test-dir build -R <those 29> -V`, three runs per side. Every Vulkan test logged `Insert instance layer "VK_LAYER_KHRONOS_validation"` and none logged `was not found but was requested by env var VK_INSTANCE_LAYERS`. | test | main (3 runs) | this PR (3 runs) | | --- | --- | --- | | `agc_driver_stencil_ref_export_unused` | Failed, Failed, Failed | Passed, Passed, Passed | The other 27 tests gave the same result on both sides. `agc_driver_label_after_import_copy` is flaky on main as well: main Failed, Passed, Passed, this PR Failed, Failed, Failed. Every failure on either side is the same timing check ("round N: a CPU read after the label that followed the copy found K of 262144 destination bytes not yet copied"), and it varied the same way across the other PRs I tested in the same session, so I don't attribute it to this change. `VUID-` lines: identical per test on both sides (30 on main, 30 with this PR, over the three runs). The `VUID-` lines on both sides are main's existing `VUID-VkDeviceCreateInfo-sType-unique` (a duplicated `VkPhysicalDevicePortabilitySubsetFeaturesKHR` in the device pNext chain) and `VUID-VkSamplerCreateInfo-samplerMipLodBias-04467`; this PR adds none.
MarcZX10 commented 2026-11-19 14:58:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T14:58:50Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the reviewed code supports the reported fix, and the optional note below is for validation coverage.

What I ran

Reviewed head b1c1de1c21df8b27187f180617396d7a7f20050b. No build or tests were run. I read the PR description, commit summary, manifest, diff, and all three changed files; I also inspected the stencil replay store and fragment setup that use the changed variable. files.json was not available in the prepared review bundle.

Results

Check Result Note
Diff and changed code passed All three changed files read
Build and tests not run Author reports macOS build and test runs

Findings

Blocking

None.

Non-blocking

  • ShaderValidation.cpp:167 permits u32 for FragStencilRefEXT, and the emitter now declares and stores the built-in as unsigned. The signed private replay variable remains i32 and is still bitcast to u32 before comparison, which keeps the two paths consistent. Consider adding a focused regression test for validation of both signed and unsigned stencil reference outputs; no such test was verified in this review.

Not verified

The author-reported MoltenVK compilation, macOS build, and test results were not independently run. Other GPU vendors and physical-console behavior were not verified. files.json was unavailable, so the complete reviewed versus files inventory could not be confirmed.

To get this to a good state

Nothing is required. Optional: add focused test coverage for the signed and unsigned stencil-reference output cases.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5977416033 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19966#pullrequestreview-5977416033). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T14:58:50Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the reviewed code supports the reported fix, and the optional note below is for validation coverage. ## What I ran Reviewed head `b1c1de1c21df8b27187f180617396d7a7f20050b`. No build or tests were run. I read the PR description, commit summary, manifest, diff, and all three changed files; I also inspected the stencil replay store and fragment setup that use the changed variable. `files.json` was not available in the prepared review bundle. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | All three changed files read | | Build and tests | not run | Author reports macOS build and test runs | ## Findings ### Blocking None. ### Non-blocking - `ShaderValidation.cpp:167` permits `u32` for `FragStencilRefEXT`, and the emitter now declares and stores the built-in as unsigned. The signed private replay variable remains `i32` and is still bitcast to `u32` before comparison, which keeps the two paths consistent. Consider adding a focused regression test for validation of both signed and unsigned stencil reference outputs; no such test was verified in this review. ## Not verified The author-reported MoltenVK compilation, macOS build, and test results were not independently run. Other GPU vendors and physical-console behavior were not verified. `files.json` was unavailable, so the complete `reviewed` versus `files` inventory could not be confirmed. ## To get this to a good state Nothing is required. Optional: add focused test coverage for the signed and unsigned stencil-reference output cases.
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!19966
No description provided.