fix(shader): keep every lane in a stencil export replay when the device can store through an address #18530

Merged
Capmare merged 11 commits from upstream-pr/18530 into main 2026-11-18 23:46:43 +00:00
Capmare commented 2026-11-11 20:25:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Capmare on 2026-11-11T20:25:49Z.


What

On a device without VK_EXT_shader_stencil_export (NVIDIA), a pixel shader that exports a stencil reference runs once per reference value, and the hardware stencil test of each pass can skip pixels before the shader runs. A wave branch or a lane read in the shader then sees other lanes than the guest does. StencilReplaySafe therefore refuses every shader whose exec-mask control it cannot prove independent of the lane set, and the draw aborts with stencil reference export emulation requires a replay-safe pixel shader with late tests.

Marvel's Wolverine (PPSA03671) stops on this: shader 0x11d01d0900 is a 208-word pass that loads a texel, remaps it through constant tables, divides by a uniform value and exports depth and stencil. Its s_cbranch_execz after a v_cmpx on the loaded value splits it into two return paths, and the analysis only accepts a body that rejoins a merge block.

The Vulkan spec lets a driver run a fragment shader on fewer pixels than the guest only when the shader has no side effects. A replay shader that the lane-set proof cannot clear now ends in a store through a device address behind a condition that is always false (the reference is at most 255, and the guard reads it from a push constant, so the driver cannot fold it). The driver has to run that shader on every covered pixel in every pass, as the guest does. A shader that the proof already clears gets no store and keeps the early tests, so no shader that works today gets slower. With the lane set fixed, StencilReplaySafe no longer needs the lane-mask proof or the ban on lane reads. Shader clock reads, loops, memory writes, shared memory and the other earlier checks stay. A device without PhysicalStorageBufferAddresses or Int64 keeps the earlier analysis. A device that exposes the extension (Intel and llvmpipe here) exports the stencil natively and is not affected.

The change:

  • StencilReplay.cpp: StencilReplaySafe(program, lanesFixed), StencilReplayLanesFixed(capabilities) and StencilReplayNeedsStore(program, capabilities).
  • SpirvFlowEmitter.cpp and SpirvModuleSetup.cpp: the store and the address capability for replay modules.
  • ShaderValidation.cpp: the capability and SPV_KHR_physical_storage_buffer are allowed for a replay fragment shader.
  • docs/dev/debt/shader.md: the entry says the guarantee is measured on one driver only.

Tested

Rocky Linux 10.2, i7-14700K, RTX 4070 SUPER, NVIDIA 620.70.06, Release. main a26b7827e1 and this branch, same machine. Tests were run as ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest -V -R '^agc_driver|shader_recompiler'.

main this branch
Tests passing 1062 of 1080 1062 of 1080

The 18 failing tests are the same set on both sides: agc_driver_division, agc_driver_f32_denormal_flush, agc_driver_image_table, agc_driver_image_table_packed* (11), agc_driver_mesh_vector_window_writes and agc_driver_mullit_f32_denormal_modes. Their logs carry the same VUID- counts on both sides, and neither log contains was not found but was requested by env var VK_INSTANCE_LAYERS apart from ctest echoing its regex:

2312 VUID-VkDeviceCreateInfo-pNext-pNext
  82 VUID-VkShaderModuleCreateInfo-pCode-08741
  82 VUID-VkShaderModuleCreateInfo-pCode-08739
  82 VUID-VkShaderModuleCreateInfo-pCode-08737
  14 VUID-VkPipelineShaderStageCreateInfo-pSpecializationInfo-06849
  14 VUID-VkPipelineShaderStageCreateInfo-module-parameter

New case in agc_driver_stencil_replay: a pixel shader (assembled for gfx1030, wave64) whose branch tests whether any lane has an odd column. Even columns export 255 on one path and 254 on the other, and the stencil buffer holds 255 under even and 0 under odd columns. In the guest every wave holds both kinds of column, so every pixel is drawn.

  • With this change, every pixel is drawn.
  • With the store removed and the shader still accepted, the even columns are not drawn: pixel (0, 0) is 64, expected drawn. The hardware skips the odd columns in the pass for 255, the branch flips and the even columns export 254.
  • On main, the shader is refused with stencil reference export emulation requires a replay-safe pixel shader with late tests.

agc_driver_stencil_replay also passes under the same validation settings on Intel RPL-S and llvmpipe (Mesa 25.2.7, selected with VK_DRIVER_FILES). Both export the stencil natively, so they do not run the replay.

Marvel's Wolverine: I replayed the dumped request of 0x11d01d0900 through the recompiler offline (the dump lacks one descriptor, which I supplied). It aborts on main and recompiles on this branch (5157 SPIR-V words). I have not drawn it in the game, because the run stalls on texture memory before it reaches this draw (see #12575).

The guarantee rests on the driver following the spec for shaders with side effects (chapter Fragment Operations, section Fragment Shading). The spec does not fix how the driver groups pixels into waves, so the replay assumes that every pass groups them the same way. I measured it on NVIDIA (RTX 4070 SUPER) and, through runs by mrfelek and domx4q, on an RTX 3080 Ti and an RTX 4090. JJenkx and blawesom ran it on AMD (RX 6700 XT, driver 26.10.1, Windows; RX 9070 XT with RADV, Linux). The debt entry says so. fragmentStoresAndAtomics is always required and enabled, the shader cache key already holds the capability list, and agc_driver_stencil_replay logs no new VUID.

tools/pr_overlap.py failed closed, so I searched the diffs of the 39 PRs it lists for the replay code. #10351 (stencil operation values through an attachment) changes SelectStencilExportMode and SpirvEmitterHelpers.cpp around the same lines, and #7689 changes one condition of StencilReplaySafe. Neither adds the lane guarantee. #18322 admits first-lane reads of wave-uniform values on every device and complements this change, which also covers non-uniform lane reads and wave branches but needs PhysicalStorageBufferAddresses and Int64. #16571 only touches a neighbouring debt entry in docs/dev/debt/shader.md. I will rebase on whichever lands first.

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: the stencil shader abort of PPSA03671 #1033 (the game still stops at #12575)
  • GPUs tested: NVIDIA RTX 4070 SUPER, 620.70.06, Rocky Linux 10.2 (all tests above); Intel RPL-S, Mesa 25.2.7, and llvmpipe, Mesa 25.2.7, Rocky Linux 10.2 (agc_driver_stencil_replay only)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5266311699 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18530). Originally posted by [Capmare](https://github.com/Capmare) on 2026-11-11T20:25:49Z. --- ### What On a device without `VK_EXT_shader_stencil_export` (NVIDIA), a pixel shader that exports a stencil reference runs once per reference value, and the hardware stencil test of each pass can skip pixels before the shader runs. A wave branch or a lane read in the shader then sees other lanes than the guest does. `StencilReplaySafe` therefore refuses every shader whose exec-mask control it cannot prove independent of the lane set, and the draw aborts with `stencil reference export emulation requires a replay-safe pixel shader with late tests`. Marvel's Wolverine (PPSA03671) stops on this: shader `0x11d01d0900` is a 208-word pass that loads a texel, remaps it through constant tables, divides by a uniform value and exports depth and stencil. Its `s_cbranch_execz` after a `v_cmpx` on the loaded value splits it into two return paths, and the analysis only accepts a body that rejoins a merge block. The Vulkan spec lets a driver run a fragment shader on fewer pixels than the guest only when the shader has no side effects. A replay shader that the lane-set proof cannot clear now ends in a store through a device address behind a condition that is always false (the reference is at most 255, and the guard reads it from a push constant, so the driver cannot fold it). The driver has to run that shader on every covered pixel in every pass, as the guest does. A shader that the proof already clears gets no store and keeps the early tests, so no shader that works today gets slower. With the lane set fixed, `StencilReplaySafe` no longer needs the lane-mask proof or the ban on lane reads. Shader clock reads, loops, memory writes, shared memory and the other earlier checks stay. A device without `PhysicalStorageBufferAddresses` or `Int64` keeps the earlier analysis. A device that exposes the extension (Intel and llvmpipe here) exports the stencil natively and is not affected. The change: - `StencilReplay.cpp`: `StencilReplaySafe(program, lanesFixed)`, `StencilReplayLanesFixed(capabilities)` and `StencilReplayNeedsStore(program, capabilities)`. - `SpirvFlowEmitter.cpp` and `SpirvModuleSetup.cpp`: the store and the address capability for replay modules. - `ShaderValidation.cpp`: the capability and `SPV_KHR_physical_storage_buffer` are allowed for a replay fragment shader. - `docs/dev/debt/shader.md`: the entry says the guarantee is measured on one driver only. ### Tested Rocky Linux 10.2, i7-14700K, RTX 4070 SUPER, NVIDIA 620.70.06, Release. `main` a26b7827e1 and this branch, same machine. Tests were run as `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest -V -R '^agc_driver|shader_recompiler'`. | | `main` | this branch | |---|---|---| | Tests passing | 1062 of 1080 | 1062 of 1080 | The 18 failing tests are the same set on both sides: `agc_driver_division`, `agc_driver_f32_denormal_flush`, `agc_driver_image_table`, `agc_driver_image_table_packed*` (11), `agc_driver_mesh_vector_window_writes` and `agc_driver_mullit_f32_denormal_modes`. Their logs carry the same `VUID-` counts on both sides, and neither log contains `was not found but was requested by env var VK_INSTANCE_LAYERS` apart from ctest echoing its regex: ``` 2312 VUID-VkDeviceCreateInfo-pNext-pNext 82 VUID-VkShaderModuleCreateInfo-pCode-08741 82 VUID-VkShaderModuleCreateInfo-pCode-08739 82 VUID-VkShaderModuleCreateInfo-pCode-08737 14 VUID-VkPipelineShaderStageCreateInfo-pSpecializationInfo-06849 14 VUID-VkPipelineShaderStageCreateInfo-module-parameter ``` New case in `agc_driver_stencil_replay`: a pixel shader (assembled for gfx1030, wave64) whose branch tests whether any lane has an odd column. Even columns export 255 on one path and 254 on the other, and the stencil buffer holds 255 under even and 0 under odd columns. In the guest every wave holds both kinds of column, so every pixel is drawn. - With this change, every pixel is drawn. - With the store removed and the shader still accepted, the even columns are not drawn: `pixel (0, 0) is 64, expected drawn`. The hardware skips the odd columns in the pass for 255, the branch flips and the even columns export 254. - On `main`, the shader is refused with `stencil reference export emulation requires a replay-safe pixel shader with late tests`. `agc_driver_stencil_replay` also passes under the same validation settings on Intel RPL-S and llvmpipe (Mesa 25.2.7, selected with `VK_DRIVER_FILES`). Both export the stencil natively, so they do not run the replay. Marvel's Wolverine: I replayed the dumped request of `0x11d01d0900` through the recompiler offline (the dump lacks one descriptor, which I supplied). It aborts on `main` and recompiles on this branch (5157 SPIR-V words). I have not drawn it in the game, because the run stalls on texture memory before it reaches this draw (see #12575). The guarantee rests on the driver following the spec for shaders with side effects (chapter Fragment Operations, section Fragment Shading). The spec does not fix how the driver groups pixels into waves, so the replay assumes that every pass groups them the same way. I measured it on NVIDIA (RTX 4070 SUPER) and, through runs by mrfelek and domx4q, on an RTX 3080 Ti and an RTX 4090. JJenkx and blawesom ran it on AMD (RX 6700 XT, driver 26.10.1, Windows; RX 9070 XT with RADV, Linux). The debt entry says so. `fragmentStoresAndAtomics` is always required and enabled, the shader cache key already holds the capability list, and `agc_driver_stencil_replay` logs no new VUID. `tools/pr_overlap.py` failed closed, so I searched the diffs of the 39 PRs it lists for the replay code. #10351 (stencil operation values through an attachment) changes `SelectStencilExportMode` and `SpirvEmitterHelpers.cpp` around the same lines, and #7689 changes one condition of `StencilReplaySafe`. Neither adds the lane guarantee. #18322 admits first-lane reads of wave-uniform values on every device and complements this change, which also covers non-uniform lane reads and wave branches but needs `PhysicalStorageBufferAddresses` and `Int64`. #16571 only touches a neighbouring debt entry in `docs/dev/debt/shader.md`. I will rebase on whichever lands first. ### 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: the stencil shader abort of PPSA03671 #1033 (the game still stops at #12575) - [x] GPUs tested: NVIDIA RTX 4070 SUPER, 620.70.06, Rocky Linux 10.2 (all tests above); Intel RPL-S, Mesa 25.2.7, and llvmpipe, Mesa 25.2.7, Rocky Linux 10.2 (`agc_driver_stencil_replay` only) - [x] AI-assisted: yes
MarcZX10 commented 2026-11-17 15:29:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T15:29:10Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this; the points below are optional.

What I ran

  • Head SHA: 25b10fcedb (base: main).
  • Read the PR description, commit list, manifest, and the full diff (11 files).
  • Read one range at head with the reader: core/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cpp lines 84–135.
  • No build and no tests were run in this review.

Results

Check Result Note
Diff read passed 11 of 11 changed files, as diff hunks
Description vs code passed Lane-set relaxation, store and capabilities match the diff
Build and tests not run Author-reported runs only
Callers at head not run Only one test range read

Findings

Blocking

None.

Non-blocking

  • core/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cpp:239: storeDevice holds StencilReplayLanesFixed(...), not a store decision. The final check at line 249 compares store presence with that flag, which only holds if the wave-branch shader fails the strict proof. Renaming the variable to lanesFixed would make that dependency visible.
  • core/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cpp:134: LastReplayStored is set by any OpConvertUToPtr in the replay module, not only by the reference store. Another device-address conversion would set it too. Matching the exact store sequence would make the check precise.
  • core/shader/recompiler/Recompiler.cpp:909: StencilReplaySafe and StencilReplayNeedsStore run the strict analysis again for each replay shader, and DefineOutputs repeats it. The cost is small, but the result could be computed once and passed along.

Not verified

  • Results on NVIDIA, Intel and llvmpipe are the author's; I did not build or run them.
  • The guard assumes the host writes the reference as 255 or less. The writer is not in the diff, so this is not verified.
  • The claim that fragmentStoresAndAtomics is always required and enabled is not in the diff and was not checked.
  • The claim that the shader cache key holds the capability list was not checked.
  • Callers of StencilReplaySafe outside the changed files were not read.
  • The driver behaviour the change relies on (side-effect shaders run on every covered pixel) cannot be checked from the code here; the debt entry says so.

Sources

  • Repository paths are at head 25b10fcedb; line numbers refer to that commit.

To get this to a good state

Nothing is required. The non-blocking points above are optional.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5941376875 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18530#pullrequestreview-5941376875). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-17T15:29:10Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this; the points below are optional. ## What I ran - Head SHA: 25b10fcedbcb1319e0367ab66190e1e45ff038e5 (base: main). - Read the PR description, commit list, manifest, and the full diff (11 files). - Read one range at head with the reader: `core/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cpp` lines 84–135. - No build and no tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Diff read | passed | 11 of 11 changed files, as diff hunks | | Description vs code | passed | Lane-set relaxation, store and capabilities match the diff | | Build and tests | not run | Author-reported runs only | | Callers at head | not run | Only one test range read | ## Findings ### Blocking None. ### Non-blocking - `core/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cpp:239`: `storeDevice` holds `StencilReplayLanesFixed(...)`, not a store decision. The final check at line 249 compares store presence with that flag, which only holds if the wave-branch shader fails the strict proof. Renaming the variable to `lanesFixed` would make that dependency visible. - `core/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cpp:134`: `LastReplayStored` is set by any `OpConvertUToPtr` in the replay module, not only by the reference store. Another device-address conversion would set it too. Matching the exact store sequence would make the check precise. - `core/shader/recompiler/Recompiler.cpp:909`: `StencilReplaySafe` and `StencilReplayNeedsStore` run the strict analysis again for each replay shader, and `DefineOutputs` repeats it. The cost is small, but the result could be computed once and passed along. ## Not verified - Results on NVIDIA, Intel and llvmpipe are the author's; I did not build or run them. - The guard assumes the host writes the reference as 255 or less. The writer is not in the diff, so this is not verified. - The claim that `fragmentStoresAndAtomics` is always required and enabled is not in the diff and was not checked. - The claim that the shader cache key holds the capability list was not checked. - Callers of `StencilReplaySafe` outside the changed files were not read. - The driver behaviour the change relies on (side-effect shaders run on every covered pixel) cannot be checked from the code here; the debt entry says so. ## Sources - Repository paths are at head 25b10fcedbcb1319e0367ab66190e1e45ff038e5; line numbers refer to that commit. ## To get this to a good state Nothing is required. The non-blocking points above are optional.
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!18530
No description provided.