perf(agc): replay an unbounded replace stencil export as one pass per bit #24802

Merged
Capmare merged 2 commits from upstream-pr/24802 into main 2026-11-21 11:11:45 +00:00
Capmare commented 2026-11-21 03:30:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Capmare on 2026-11-21T03:30:33Z.


What

On a device without VK_EXT_shader_stencil_export (NVIDIA), the stencil replay draws a pixel shader that exports a stencil value once per possible reference value. When the prover cannot bound the export, the replay records all 256 values, one pass per value per triangle and instance.

Such a draw can write the stencil with compare ALWAYS and pass REPLACE. For such a draw, the value that the shader exports goes directly into the written stencil bits. So the replay can build that value bit by bit:

  1. One pass writes 0 to the written stencil bits of every covered pixel. Color and depth writes are off.
  2. One pass per written bit writes 1 to that bit only, where the exported value has that bit. The shader compares only that bit; the stencil write mask holds only that bit.
  3. One last pass writes color and depth once, with the stencil write mask at 0.

A draw that writes 8 stencil bits records 10 passes per triangle and instance, not 256. The replay uses the bit passes only when both faces compare ALWAYS, pass REPLACE and keep the value on a depth failure (or the depth test is off), and when that gives fewer passes. The passes change depth, color and stencil write masks through dynamic state, so the draw needs graphics pipeline libraries, and dynamic color write masks when it writes color. Other draws keep one pass per value.

The change:

  • Draw.cpp: StencilReplayPasses selects the passes; recordStencilReplay records them and sets the masks of each pass.
  • Pipeline.cpp, Pipeline.hpp: MasksStencilReplayPasses and SetStencilReplayMasks.
  • SpirvFlowEmitter.cpp: the replay compares the bits that the pass word names. The high byte of the word lists the bits to ignore; a word of only low bits compares all 8 bits as before.
  • fix-stencil-replay-multisample.md: the debt entry describes the bit passes. It also records the triangle strip replay of #24379, which that PR did not put in the entry.

Marvel's Wolverine (PPSA03671) has such a draw: shader 0x11d01d0900 exports an unbounded value with compare ALWAYS and pass REPLACE on both faces and writes all 8 bits (STENCIL-BITS bitwise=1 values=256 bits=ff). With this change it records 10 passes per triangle in place of 256. I did not measure the frame time of that scene.

Tested

Rocky Linux 10.2, i7-14700K, RTX 4070 SUPER, NVIDIA 620.70.06, Release. main 2ab4b094c9 and this branch, same machine. Tests were run as VK_ADD_LAYER_PATH=<layer build> ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest -V -R '^agc_driver_stencil'.

main this branch
Tests passing 11 of 11 (agc_driver_stencil_ref_export skipped) 11 of 11 (agc_driver_stencil_ref_export skipped)

agc_driver_stencil_ref_export skips on NVIDIA on both sides, because the device has no VK_EXT_shader_stencil_export. The layer is Vulkan-ValidationLayers vulkan-sdk-1.4.335.0, built from source and added with VK_ADD_LAYER_PATH. Both logs show the loader inserting the layer 13 times from that build, neither contains was not found but was requested by env var VK_INSTANCE_LAYERS, and neither contains a VUID- line. (With the installed 1.4.328 layer, both sides logged the same set: VUID-VkDeviceCreateInfo-pNext-pNext and three VUID-VkShaderModuleCreateInfo-pCode-* IDs, 26 and 52 times each.)

New cases in agc_driver_stencil_replay, for an export the prover cannot bound, written with compare ALWAYS and pass REPLACE:

  • The draw records 10 passes with graphics pipeline libraries and dynamic color write masks, also when it writes no color; 256 without libraries (agc_driver_stencil_replay_no_gpl).
  • The stencil holds the exported value in each column: the bits the value does not set are cleared, the high bits are set, and the bits outside the write mask keep their old value.
  • An additive blend adds its color once, not once per pass.
  • With a depth test, each bit pass tests against the depth from before the draw, and the draw writes stencil only where its depth passes.

Negative control: with Draw.cpp from main and the new test, agc_driver_stencil_replay fails with an unbounded replace export recorded 256 passes.

tools/pr_overlap.py failed closed. Two open PRs change the same debt entry, Draw.cpp and StencilReplay.cpp: #24572 (stencil replay of indirect draws) and #24735 (depth feedback check against buffer descriptors). Neither changes the number of passes. The PR that lands second needs a rebase of the debt sentence; I will rebase if this one is second.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 (#24379 is merged)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA RTX 4070 SUPER, 620.70.06, Rocky Linux 10.2
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5423840355 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24802). Originally posted by [Capmare](https://github.com/Capmare) on 2026-11-21T03:30:33Z. --- ### What On a device without `VK_EXT_shader_stencil_export` (NVIDIA), the stencil replay draws a pixel shader that exports a stencil value once per possible reference value. When the prover cannot bound the export, the replay records all 256 values, one pass per value per triangle and instance. Such a draw can write the stencil with compare `ALWAYS` and pass `REPLACE`. For such a draw, the value that the shader exports goes directly into the written stencil bits. So the replay can build that value bit by bit: 1. One pass writes 0 to the written stencil bits of every covered pixel. Color and depth writes are off. 2. One pass per written bit writes 1 to that bit only, where the exported value has that bit. The shader compares only that bit; the stencil write mask holds only that bit. 3. One last pass writes color and depth once, with the stencil write mask at 0. A draw that writes 8 stencil bits records 10 passes per triangle and instance, not 256. The replay uses the bit passes only when both faces compare `ALWAYS`, pass `REPLACE` and keep the value on a depth failure (or the depth test is off), and when that gives fewer passes. The passes change depth, color and stencil write masks through dynamic state, so the draw needs graphics pipeline libraries, and dynamic color write masks when it writes color. Other draws keep one pass per value. The change: - `Draw.cpp`: `StencilReplayPasses` selects the passes; `recordStencilReplay` records them and sets the masks of each pass. - `Pipeline.cpp`, `Pipeline.hpp`: `MasksStencilReplayPasses` and `SetStencilReplayMasks`. - `SpirvFlowEmitter.cpp`: the replay compares the bits that the pass word names. The high byte of the word lists the bits to ignore; a word of only low bits compares all 8 bits as before. - `fix-stencil-replay-multisample.md`: the debt entry describes the bit passes. It also records the triangle strip replay of #24379, which that PR did not put in the entry. Marvel's Wolverine (PPSA03671) has such a draw: shader `0x11d01d0900` exports an unbounded value with compare `ALWAYS` and pass `REPLACE` on both faces and writes all 8 bits (`STENCIL-BITS bitwise=1 values=256 bits=ff`). With this change it records 10 passes per triangle in place of 256. I did not measure the frame time of that scene. ### Tested Rocky Linux 10.2, i7-14700K, RTX 4070 SUPER, NVIDIA 620.70.06, Release. `main` 2ab4b094c9 and this branch, same machine. Tests were run as `VK_ADD_LAYER_PATH=<layer build> ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest -V -R '^agc_driver_stencil'`. | | `main` | this branch | |---|---|---| | Tests passing | 11 of 11 (`agc_driver_stencil_ref_export` skipped) | 11 of 11 (`agc_driver_stencil_ref_export` skipped) | `agc_driver_stencil_ref_export` skips on NVIDIA on both sides, because the device has no `VK_EXT_shader_stencil_export`. The layer is Vulkan-ValidationLayers `vulkan-sdk-1.4.335.0`, built from source and added with `VK_ADD_LAYER_PATH`. Both logs show the loader inserting the layer 13 times from that build, neither contains `was not found but was requested by env var VK_INSTANCE_LAYERS`, and neither contains a `VUID-` line. (With the installed 1.4.328 layer, both sides logged the same set: `VUID-VkDeviceCreateInfo-pNext-pNext` and three `VUID-VkShaderModuleCreateInfo-pCode-*` IDs, 26 and 52 times each.) New cases in `agc_driver_stencil_replay`, for an export the prover cannot bound, written with compare `ALWAYS` and pass `REPLACE`: - The draw records 10 passes with graphics pipeline libraries and dynamic color write masks, also when it writes no color; 256 without libraries (`agc_driver_stencil_replay_no_gpl`). - The stencil holds the exported value in each column: the bits the value does not set are cleared, the high bits are set, and the bits outside the write mask keep their old value. - An additive blend adds its color once, not once per pass. - With a depth test, each bit pass tests against the depth from before the draw, and the draw writes stencil only where its depth passes. Negative control: with `Draw.cpp` from `main` and the new test, `agc_driver_stencil_replay` fails with `an unbounded replace export recorded 256 passes`. `tools/pr_overlap.py` failed closed. Two open PRs change the same debt entry, `Draw.cpp` and `StencilReplay.cpp`: #24572 (stencil replay of indirect draws) and #24735 (depth feedback check against buffer descriptors). Neither changes the number of passes. The PR that lands second needs a rebase of the debt sentence; I will rebase if this one is second. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.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 (#24379 is merged) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA RTX 4070 SUPER, 620.70.06, Rocky Linux 10.2 - [x] AI-assisted: yes
MarcZX10 commented 2026-11-21 03:55:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T03:55:22Z.

Review: APPROVED


Reviewed — looks good

The bitwise stencil replay path and its fallback are supported by the inspected implementation and added execution cases; no blocking defect was verified.

What I ran

Reviewed head 06a9ffc687ce9c2217b22d6fbe8547fd9b92cf6f using the prepared review bundle and repo_read.py at the pinned head. I did not run commands or tests; the test results below are author-reported.

Results

Check Result Note
Source review passed All six changed files were read
Execution tests not run Author reports 11 of 11 passing; one test skipped
Negative control not run Author reports the new assertion fails against main

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were run independently, and console behavior or frame-time impact was not measured. The author reports tests on Rocky Linux 10.2 with an RTX 4070 SUPER and NVIDIA 620.70.06; this review did not verify those results. No changed files were excluded: Pipeline.hpp, Draw.cpp, Pipeline.cpp, StencilReplay.cpp, SpirvFlowEmitter.cpp, and fix-stencil-replay-multisample.md were read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002673667 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24802#pullrequestreview-6002673667). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T03:55:22Z. **Review:** APPROVED --- # Reviewed — looks good The bitwise stencil replay path and its fallback are supported by the inspected implementation and added execution cases; no blocking defect was verified. ## What I ran Reviewed head `06a9ffc687ce9c2217b22d6fbe8547fd9b92cf6f` using the prepared review bundle and `repo_read.py` at the pinned head. I did not run commands or tests; the test results below are author-reported. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | All six changed files were read | | Execution tests | not run | Author reports 11 of 11 passing; one test skipped | | Negative control | not run | Author reports the new assertion fails against `main` | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests were run independently, and console behavior or frame-time impact was not measured. The author reports tests on Rocky Linux 10.2 with an RTX 4070 SUPER and NVIDIA 620.70.06; this review did not verify those results. No changed files were excluded: `Pipeline.hpp`, `Draw.cpp`, `Pipeline.cpp`, `StencilReplay.cpp`, `SpirvFlowEmitter.cpp`, and `fix-stencil-replay-multisample.md` were read. ## To get this to a good state Nothing is required.
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!24802
No description provided.