Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
XIIYAZ:fix/shader-stencil-ref-unsigned
Nov 22, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
XIIYAZ:fix/shader-stencil-ref-unsigned

Conversation

@XIIYAZ

@XIIYAZ XIIYAZ commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 10351 core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp L2388-2390
PR 19821 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp L169

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6235 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 9356 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 9387 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 9508 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 10193 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 11483 core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 11551 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 12944 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 13928 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 13929 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 14871 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
PR 15548 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 16571 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
PR 17611 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 20248 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
PR 20519 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 22424 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 23243 core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 23244 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 23553 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp

@JJenkx

JJenkx commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Windows check of head b1c1de1c21, merged onto main c7102526b5. Setup: Windows 11 Enterprise 22621, AMD RX 6700 XT, AMD driver 26.10.1, MinGW-w64 GCC 15.2 UCRT, Release.

All 10 selected tests (agc_driver_depth_expand_out_of_place_inline, agc_driver_fill_completion_labels_chain, agc_driver_fill_completion_labels_cpu_disjoint, agc_driver_fill_completion_labels_cpu_overlap, ...):

  • GPU, through the shared Windows GPU FIFO with ANYPS5_REQUIRE_VULKAN=1 (merge e07d737ced): all 10 passed in 3 of 3 runs, 30 of 30 outcomes. The binaries matched the hashes recorded at preparation, and the receipt shows exit 0 and app_count=0.
  • CPU, Vulkan hidden (merge b2f8c5a8f4): 3 passed and 7 skipped (they need a device), 0 failed, in each of 3 runs.

AI-assisted: yes.

@kurexw kurexw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Jazeupoi

Jazeupoi commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Tested this head (b1c1de1c21) on Apple M4, macOS 27.0.1, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.1), x86-64 Release under Rosetta 2. Base: main d3074de315 with #17425 and #11049 (needed for the macOS build) and #20845 merged locally; "this PR" is the same tree with this head merged (clean merge). All builds clean.

  • agc_driver_stencil_ref_export_unused run alone 10 times (ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1): 10/10 fail on the base, 0/10 with this PR.
  • ctest -R 'stencil|depth_expand|export' (58 tests), twice per build: the base failed only that test both times; with this PR all 58 pass both times.
  • SuperTuxKart (PPSA69421) race, same scripted input, after one warm-up run so the changed shaders were cached: frame p50 157.6 ms on the base, 146.9 ms with this PR (140.8 ms in the warm-up run); it draws the same.

Also run under the Khronos validation layer 1.4.363 (VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer): the layer is inserted (no was not found messages), and this PR adds no VUID- messages; the base and this PR report the same ones.

AI-assisted: yes (Claude Code)

@domx4q

domx4q commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Ran the focused tests with the Khronos validation layer on Linux and an NVIDIA GPU, since CI runs the GPU tests only on lavapipe and the Tested section is on MoltenVK only. The pattern covers all 21 tests with stencil in their name on current main.

Setup: Arch Linux (kernel 7.3.4), GCC 16.2.1, Release, NVIDIA GeForce RTX 4090 (vendor 0x10de, device 0x2684, Ada), proprietary driver 620.70.06, Vulkan 1.4.357, Khronos validation layer 1.4.363.1. Head b1c1de1c21 merged onto main fbaae8a79d with --no-ff (clean, as f3f3be79a8). Main baseline is a Release build of fbaae8a79d on the same machine.

ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer \
ctest --test-dir build -R 'stencil' -V
main fbaae8a79d this PR
Result 21 of 21 passed 21 of 21 passed
Insert instance layer "VK_LAYER_KHRONOS_validation" 29 29
was not found but was requested by env var VK_INSTANCE_LAYERS 0 0
VUID- none none

All 21 pass on both sides; every device selected the RTX 4090. agc_driver_stencil_ref_export is skipped on both sides (skipped, the device lacks VK_EXT_shader_stencil_export; ctest counts it as passed). The driver does not expose VK_EXT_shader_stencil_export, so the shaders take the stencilReplay path, which this PR leaves on the signed private variable; the new unsigned FragStencilRefEXT output is not emitted on this device. The run shows the replay path, including agc_driver_stencil_ref_export_unused, agc_driver_stencil_replay and agc_driver_stencil_replay_no_gpl, is unchanged on NVIDIA.

No new VUID- errors; all stencil tests pass on Ada (the stencil-export path itself cannot run on this driver).

AI-assisted: yes (Claude Code)

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head b1c1de1c21, applied with git apply on main fd4aeed483 together with the other 29 PRs of one batch (#24470, #24601, #17439, #17546, #21117, #21261, #21741, #21995, #22067, #22398, #23111, #23346, #23864, #24377, #24475, #24556, #14106, #14109, #20050, #20161, #20622, #21237, #22215, #22764, #22884, #22938, #23745, #24399, #24460). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
none: no test of its own in this PR - -

Whole batch: the full ctest -j 12 (6525 tests) on the RTX 3080 has 5 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Traced to other PRs of the batch, each by reverting it and testing again: #21117 (guest_pad); none is in this PR's area. The 2125 agc_/video tests on the iGPU have 1 failing: agc_driver_stencil_plane_memory (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants