fix(shader): declare FragStencilRefEXT unsigned so MoltenVK compiles it with the stencil output disabled #19966
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/19966"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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):The recompiler declares
FragStencilRefEXTas a signed 32-bit integer and writes it from the shader body, which is its own function called frommain. Without a stencil attachment MoltenVK has SPIRV-Cross disable the[[stencil]]output, and SPIRV-Cross then declares the replacement local inmainwith 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) andxcrun metalgive the same error; with the default options (stencil output enabled) it compiles.VK_EXT_shader_stencil_exportonly requires a scalar 32-bit integer forFragStencilRefEXT, so this declares the built-in as unsigned:SpirvEmitterHelpers.cpp: the built-in usesTypeU32.SpirvModuleEmitter.cpp: the export stores the rawu32into the built-in; the replay emulation's private variable staysi32and keeps its bitcast.ShaderValidation.cpp: the fragment built-in check acceptsu32as well asi32forFragStencilRefEXT.With the unsigned type both declarations are
uint, and the same SPIR-V edited touintcompiles with and without--msl-disable-frag-stencil-ref-builtin.This touches the same stencil store in
SpirvModuleEmitter.cppas #10351 (which adds a stencil attachment branch there and is currently conflicting withmain); the changes are independent, and whichever lands second keeps both: the attachment branch from #10351, and the rawu32store 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, onmain290ee1aa0fwith 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.*stencil*tests and the graphics validation unit test pass, includingagc_driver_stencil_ref_export,agc_driver_stencil_replayandagc_driver_stencil_replay_no_gpl.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, nowas not found but was requested, and there are noVUID-messages.ctest -R '^(agc|shader|job)', 1578 tests,-j 4, compared with the same run before the change: no test fails that passed before. 13agc_driver_image_table*tests hit the validation-output regex under-j 4in 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:SampleMaskinput passed into the body function asuint (&)[1]and read without[0]) are a separate SPIRV-Cross path and are not changed here.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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
b1c1de1c21merged alone onto main3b17977ba6, compared with3b17977ba6itself. I ran the 29 AGC tests that fail on main on this Mac, withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationandVK_LOADER_DEBUG=layer, throughctest --test-dir build -R <those 29> -V, three runs per side. Every Vulkan test loggedInsert instance layer "VK_LAYER_KHRONOS_validation"and none loggedwas not found but was requested by env var VK_INSTANCE_LAYERS.agc_driver_stencil_ref_export_unusedThe other 27 tests gave the same result on both sides.
agc_driver_label_after_import_copyis 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). TheVUID-lines on both sides are main's existingVUID-VkDeviceCreateInfo-sType-unique(a duplicatedVkPhysicalDevicePortabilitySubsetFeaturesKHRin the device pNext chain) andVUID-VkSamplerCreateInfo-samplerMipLodBias-04467; this PR adds none.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.jsonwas not available in the prepared review bundle.Results
Findings
Blocking
None.
Non-blocking
ShaderValidation.cpp:167permitsu32forFragStencilRefEXT, and the emitter now declares and stores the built-in as unsigned. The signed private replay variable remainsi32and is still bitcast tou32before 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.jsonwas unavailable, so the completereviewedversusfilesinventory 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.