fix(shader): read the sample mask input in the pixel entry point so MoltenVK compiles it #19986

Merged
XIIYAZ merged 1 commit from upstream-pr/19986 into main 2026-11-22 01:13:48 +00:00
XIIYAZ commented 2026-11-14 10:15:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XIIYAZ on 2026-11-14T10:15:58Z.


What

On MoltenVK, a pixel shader that reads the sample mask input fails to compile when its body is the separate pixelGuestFunc (pixel shaders where bdaStopsInvocations is set and the shader is not ordered), as in agc_driver_pixel_sample_coverage and agc_driver_sample_order:

program_source:24:48: error: cast from pointer to smaller type 'int' loses information
program_source:91:5: error: no matching function for call to '_35'

The SPIR-V reads SampleMask correctly (OpAccessChain %ptr_Input_int %gl_SampleMaskIn %uint_0, then OpLoad). SPIRV-Cross maps SampleMask to Metal's scalar [[sample_mask]] and drops the [0], but because the variable is used in a function other than the entry point it also passes it into that function as thread uint (&gl_SampleMaskIn)[1], so the body reads int(gl_SampleMaskIn) from an array and main0 passes a scalar to the array parameter. This reproduces outside AnyPS5 with the SDK's tools on the SPIR-V MoltenVK logs (spirv-cross --msl --msl-version 30100, vulkan-sdk-1.4.363.0, then xcrun metal), with default options. Declaring the array as uint does not change it; reading the mask in the entry point does.

When the pixel body is a separate function, each sample mask input the body reads now goes through a private i32 copy, which the entry point fills from element 0 of the built-in just before it calls the body:

  • LoadSampleMask serves both reads (the StageInputKind::SampleMask input and the quad sample mask read of sampleMaskInputVariable); it creates the private copy on first use and records the pair in sampleMaskCopies.
  • EmitPixelEntryPoint stores each copy before OpFunctionCall pixelGuestFunc.
  • When the body is emitted directly into main (no pixelGuestFunc), the reads stay as they were.

The value is the same SampleMask[0] the body read before, read once at entry; the input does not change during the invocation.

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) and #19966 applied; this change and #19966 apply together cleanly.

  • agc_driver_pixel_sample_coverage and agc_driver_sample_order (10 swap round trips, 4 shader remap checks, 2 scene checks): fail before with the errors above, pass after.
  • With VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer (layer from the same SDK): both pass, the loader inserts the layer, no was not found but was requested, no VUID- messages.
  • ctest -R '^(agc|shader|job)', 1578 tests, -j 2, compared with the same run before both changes: no test fails that passed before; 30 failures before, 25 after.

Not run on lavapipe, AMD or NVIDIA. The remaining sample-related MoltenVK failures (agc_driver_pixel_sample_iteration, agc_driver_sample_mask_multisample) fail the same way before and after 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:5310134878 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19986). Originally posted by [XIIYAZ](https://github.com/XIIYAZ) on 2026-11-14T10:15:58Z. --- ### What On MoltenVK, a pixel shader that reads the sample mask input fails to compile when its body is the separate `pixelGuestFunc` (pixel shaders where `bdaStopsInvocations` is set and the shader is not ordered), as in `agc_driver_pixel_sample_coverage` and `agc_driver_sample_order`: ``` program_source:24:48: error: cast from pointer to smaller type 'int' loses information program_source:91:5: error: no matching function for call to '_35' ``` The SPIR-V reads `SampleMask` correctly (`OpAccessChain %ptr_Input_int %gl_SampleMaskIn %uint_0`, then `OpLoad`). SPIRV-Cross maps `SampleMask` to Metal's scalar `[[sample_mask]]` and drops the `[0]`, but because the variable is used in a function other than the entry point it also passes it into that function as `thread uint (&gl_SampleMaskIn)[1]`, so the body reads `int(gl_SampleMaskIn)` from an array and `main0` passes a scalar to the array parameter. This reproduces outside AnyPS5 with the SDK's tools on the SPIR-V MoltenVK logs (`spirv-cross --msl --msl-version 30100`, vulkan-sdk-1.4.363.0, then `xcrun metal`), with default options. Declaring the array as `uint` does not change it; reading the mask in the entry point does. When the pixel body is a separate function, each sample mask input the body reads now goes through a private `i32` copy, which the entry point fills from element 0 of the built-in just before it calls the body: - `LoadSampleMask` serves both reads (the `StageInputKind::SampleMask` input and the quad sample mask read of `sampleMaskInputVariable`); it creates the private copy on first use and records the pair in `sampleMaskCopies`. - `EmitPixelEntryPoint` stores each copy before `OpFunctionCall pixelGuestFunc`. - When the body is emitted directly into `main` (no `pixelGuestFunc`), the reads stay as they were. The value is the same `SampleMask[0]` the body read before, read once at entry; the input does not change during the invocation. ### 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) and #19966 applied; this change and #19966 apply together cleanly. - `agc_driver_pixel_sample_coverage` and `agc_driver_sample_order` (10 swap round trips, 4 shader remap checks, 2 scene checks): fail before with the errors above, pass after. - With `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer` (layer from the same SDK): both pass, the loader inserts the layer, no `was not found but was requested`, no `VUID-` messages. - `ctest -R '^(agc|shader|job)'`, 1578 tests, `-j 2`, compared with the same run before both changes: no test fails that passed before; 30 failures before, 25 after. Not run on lavapipe, AMD or NVIDIA. The remaining sample-related MoltenVK failures (`agc_driver_pixel_sample_iteration`, `agc_driver_sample_mask_multisample`) fail the same way before and after 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
MarcZX10 commented 2026-11-14 13:24:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T13:24:58Z.

Review: COMMENTED


Reviewed — no blocking findings

The inspected changes appear correct, but I could not run the reported checks, so a formal approval is not justified.

What I ran

Reviewed head 753d6e1b204bfa17a03528c3c66a9cd4062dfe8c using the prepared review bundle and repo_read.py. No build or tests were run. The author reports Apple M4/macOS 26.7.3 MoltenVK testing and 1,578 ctest cases; these results were not independently verified.

Results

Check Result Note
Diff and changed source review passed Both changed files read
Build and tests not run Author-reported results only
Negative control not run No test environment available

Findings

Blocking

None identified in the inspected changes.

Non-blocking

None.

Sources

Not verified

Build and test results were not run independently. Both changed files listed in the manifest were read. The excluded-file list was not available in the review bundle listing, so excluded files were not verified.

To get this to a good state

Nothing is required. The reported MoltenVK and ctest results remain author-reported.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5904516304 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19986#pullrequestreview-5904516304). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-14T13:24:58Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The inspected changes appear correct, but I could not run the reported checks, so a formal approval is not justified. ## What I ran Reviewed head `753d6e1b204bfa17a03528c3c66a9cd4062dfe8c` using the prepared review bundle and `repo_read.py`. No build or tests were run. The author reports Apple M4/macOS 26.7.3 MoltenVK testing and 1,578 `ctest` cases; these results were not independently verified. ## Results | Check | Result | Note | |---|---|---| | Diff and changed source review | passed | Both changed files read | | Build and tests | not run | Author-reported results only | | Negative control | not run | No test environment available | ## Findings ### Blocking None identified in the inspected changes. ### Non-blocking None. ## Sources - [SpirvModuleEmitter.cpp at reviewed head](https://github.com/boykopovar/AnyPS5/blob/753d6e1b204bfa17a03528c3c66a9cd4062dfe8c/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp) - [SpirvEmitterState.hpp at reviewed head](https://github.com/boykopovar/AnyPS5/blob/753d6e1b204bfa17a03528c3c66a9cd4062dfe8c/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp) ## Not verified Build and test results were not run independently. Both changed files listed in the manifest were read. The excluded-file list was not available in the review bundle listing, so excluded files were not verified. ## To get this to a good state Nothing is required. The reported MoltenVK and `ctest` results remain author-reported.
kurexw commented 2026-11-16 23:01:45 +00:00 (Migrated from github.com)

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

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 753d6e1b20 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_pixel_sample_coverage Failed, Failed, Failed Passed, Passed, Passed
agc_driver_sample_order Failed, Failed, Failed Passed, Passed, Passed

The other 26 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. Note that this also fixes agc_driver_sample_order here: on main it fails with the same MoltenVK fragment shader compile error (cast from pointer to smaller type 'int' loses information), which #19943 does not change on this Mac.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5929447125 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19986#pullrequestreview-5929447125). Originally posted by [kurexw](https://github.com/kurexw) on 2026-11-16T23:01:45Z. **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 `753d6e1b20` 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_pixel_sample_coverage` | Failed, Failed, Failed | Passed, Passed, Passed | | `agc_driver_sample_order` | Failed, Failed, Failed | Passed, Passed, Passed | The other 26 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. Note that this also fixes `agc_driver_sample_order` here: on main it fails with the same MoltenVK fragment shader compile error (`cast from pointer to smaller type 'int' loses information`), which #19943 does not change on this Mac.
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!19986
No description provided.