Skip to content

fix(recompiler): replay every eight-bit stencil reference a pixel shader may export - #7006

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
TheSovietWaffle:fix/recompiler-stencil-replay-every-value
Oct 20, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
TheSovietWaffle:fix/recompiler-stencil-replay-every-value

Conversation

@TheSovietWaffle

Copy link
Copy Markdown
Contributor

What

Devices without VK_EXT_shader_stencil_export (NVIDIA) emulate a pixel shader's stencil reference export by replaying the draw once per reference value, each pass killing the fragments whose exported value differs. StencilReferenceValues returned no values, and the draw was refused, whenever the prover could not bound the export to at most 16 values:

shader 0x2112444f00 stage 5: ShaderRecompiler::Recompile: stencil reference export emulation requires a proven set of at most 16 values

The replay shader compares only the low eight bits of the export (SpirvFlowEmitter.cpp, OpBitwiseAnd 0xff before OpIEqual), so 0 to 255 covers every value any export can produce:

  • An export the prover cannot bound now replays all 256 values instead of being refused.
  • Proven values are reduced to their low eight bits, as the comparison sees them. A proven 300 was kept as 300 and could never match a pass.
  • The other refusals (a kept path without a stencil export, early tests, ordered pixel shaders, replay-unsafe programs, the draw-level conditions in Draw.cpp) are unchanged.

Cost: a draw with more than one replay value is already split per triangle in recordStencilReplay, so an unbounded export records up to 256 passes per triangle of that draw. Draws with a provable export keep their current pass count.

Refs #4276.

Tested

Windows 10 Pro 22H2 (19045), RTX 3080 (NVIDIA 617.42, no VK_EXT_shader_stencil_export), WinLibs GCC 15.2.0 r7, Release, on current main.

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
  • Unblocks: PPSA27836 on NVIDIA past the stencil export stop (no bring-up discussion)
  • GPUs tested: NVIDIA RTX 3080, 617.42, Windows 10 Pro 22H2 (19045)
  • AI-assisted: yes

🤖 Generated with Claude Code

@github-actions github-actions Bot added the open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 label Oct 17, 2026
@github-actions

Copy link
Copy Markdown

Thank you for your contribution! Authors with no pull requests merged into main can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.

@github-actions github-actions Bot closed this Oct 17, 2026
@github-actions github-actions Bot removed the open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 label Oct 17, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 17, 2026
@github-actions

github-actions Bot commented Oct 17, 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 6498 docs/dev/debt/libSceAgcDriver.md L65-66
PR 7002 docs/dev/debt/libSceAgcDriver.md L65-66
PR 8223 docs/dev/debt/libSceAgcDriver.md L65-66

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 1191 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 1368 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 1395 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 2434 docs/dev/debt/libSceAgcDriver.md
PR 4072 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 4448 docs/dev/debt/libSceAgcDriver.md
PR 4449 docs/dev/debt/libSceAgcDriver.md
PR 5058 docs/dev/debt/libSceAgcDriver.md
PR 5431 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 5441 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 5499 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
docs/dev/debt/libSceAgcDriver.md
PR 5609 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 5925 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6146 docs/dev/debt/libSceAgcDriver.md
PR 6188 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 6194 docs/dev/debt/libSceAgcDriver.md
PR 6202 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 6223 docs/dev/debt/libSceAgcDriver.md
PR 6235 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
PR 6273 docs/dev/debt/libSceAgcDriver.md
PR 6280 docs/dev/debt/libSceAgcDriver.md
PR 6292 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 6300 docs/dev/debt/libSceAgcDriver.md
PR 6412 docs/dev/debt/libSceAgcDriver.md
PR 6481 docs/dev/debt/libSceAgcDriver.md
PR 6539 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 6548 docs/dev/debt/libSceAgcDriver.md
PR 6549 docs/dev/debt/libSceAgcDriver.md
PR 6615 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 6636 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6683 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6718 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 6727 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6808 docs/dev/debt/libSceAgcDriver.md
PR 6820 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
PR 6867 docs/dev/debt/libSceAgcDriver.md
PR 6868 docs/dev/debt/libSceAgcDriver.md
PR 6926 docs/dev/debt/libSceAgcDriver.md
PR 6946 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6947 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6977 docs/dev/debt/libSceAgcDriver.md
PR 7085 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7201 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
PR 7256 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7332 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7446 docs/dev/debt/libSceAgcDriver.md
PR 7449 docs/dev/debt/libSceAgcDriver.md
PR 7505 docs/dev/debt/libSceAgcDriver.md
PR 7604 docs/dev/debt/libSceAgcDriver.md
PR 7618 docs/dev/debt/libSceAgcDriver.md
PR 7642 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7668 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7831 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
PR 7862 docs/dev/debt/libSceAgcDriver.md
PR 7888 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp
PR 7948 docs/dev/debt/libSceAgcDriver.md
PR 8099 docs/dev/debt/libSceAgcDriver.md

@github-actions github-actions Bot added shader core/shader agc libSceAgc* labels Oct 17, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks for this. The change is correct: the replay pass already compares only the low eight bits of the export (SpirvFlowEmitter.cpp, OpBitwiseAnd 0xff before OpIEqual), so replaying 0 to 255 covers every possible export. Masking the proven values also fixes a real bug: a proven 300 could never match a pass, so those fragments were dropped.

One required change before merge:

  • docs/dev/debt/libSceAgcDriver.md:54 still says stencil reference replay "supports at most sixteen values proved from constant, select and phi exports". Please update it to describe the new behaviour: an export the prover cannot bound replays all 256 eight-bit values, one pass per value per triangle, while proven exports keep their reduced set.

One suggestion, not required:

  • Log once (per pipeline or per process) when a draw takes the 256-value path. On devices without VK_EXT_shader_stencil_export, an unbounded export on a large mesh records 256 × triangles × instances draws, and a log line would make that slowdown easy to attribute.

@LeandroLP23

Copy link
Copy Markdown
Contributor

AMD run, since the description lists AMD as not tested. Radeon 860M (RDNA 3.5, driver 32.0.13058.8001), Windows 11 26300, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. Build: main 6efb7261b with #7524, #7006, #6612, #6929, #7119, #6896 and #6856 merged together (each merge clean, built once).

agc_driver_stencil_replay, agc_driver_stencil_replay_no_gpl, agc_driver_stencil_ref_export, agc_driver_stencil_ref_export_unused, agc_driver_sample_mask_depth_stencil and agc_masked_select pass 3 runs of 3. The 860M exposes VK_EXT_shader_stencil_export, so this is the native FragStencilRef path the description mentions.

AI-assisted

@simonbronk

Copy link
Copy Markdown
Contributor

Tested on Onimusha: Way of the Sword (PPSA27836), where #4276's refusal now stops the first story-mode load (#6374).

Tree: upstream/main c4c2b3a + #6727 + #6762 + this PR (059aa7d). Windows 11 Pro 26200, RTX 5070 (NVIDIA 617.42, no VK_EXT_shader_stencil_export), WinLibs GCC, Release, cold shader cache. GPU tests on this tree: ctest -R "agc|shader|video_out" 758/758 on NVIDIA.

Overlap with #6762. Both PRs change StencilReferenceValues and the StencilRef case in DefineOutputs, and they conflict. #6762 adds a shift overload: with DB_SHADER_CONTROL 0x45 the exported value sits in bits 8..15. I resolved the merge as follows:

// SpirvEmitterHelpers.cpp
const auto values = StencilReferenceValues(state.program, PixelInfo(state).psStencilOperationValueExport ? 8u : 0u);
if (values.empty()) throw std::runtime_error("stencil reference export emulation found no stencil export");
// StencilReplay.cpp, StencilReferenceValues(program, shift)
if (!values) { std::vector<std::uint32_t> all(256u); std::iota(all.begin(), all.end(), 0u); return all; }
for (const auto value : *values) result.insert((value >> shift) & 0xffu);

Whichever lands second needs this resolution. I'm fine with this one going first; I'll rebase #6762 on top.

Game result. Same path as on #6374: menus, New Game, difficulty, layout, then the first story-mode load. One run.

  • Before (same tree without this PR): the load stopped on shader 0x2107d3b200 stage 5: ... requires a proven set of at most 16 values.
  • With this PR, that refusal is gone; 0x2107d3b200 is not reported at all. The load now stops later in the same screen on a different shader and the next check in DefineOutputs:
[gpu] driver failure: shader 0x211203ed00 stage 5: ShaderRecompiler::Recompile: stencil reference export emulation requires a replay-safe pixel shader with late tests

That message covers three conditions (psEarlyZ, psOrderedPixelShader, !StencilReplaySafe), and I have not yet split which one this shader hits. No device loss in this run either.

AI-assisted: yes

@simonbronk

simonbronk commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up on the 0x211203ed00 refusal above. I replayed the request from the game log offline with agc_shader_replay --prepare (same tree, ANYPS5_NO_SHADER_CACHE=1), using temporary prints that are not part of any PR.

Which check fails. Not psEarlyZ (0) and not psOrderedPixelShader (0). StencilReplaySafe returns false on its first line, because program.Info().usesDma is set:

shared=0 bdaWrites=0 dma=1 faultBuffer=0 guardedSlots=0
values=256

values=256: the reference set is unproven, so this PR falls back to all 256 values.

Why usesDma. APS5_TRACE_BDA=1 shows a single global read:

[bda] LoadAddressU32 at pc 0x00000100: global access, offset dynamic

The rest of the shader is reads only (an image_load, buffer_load_dword/dwordx2/dwordx3), no memory writes and no atomics. The exported value is built at 0x164 as v_and_or_b32 v0, s0, 0xff, v0 (s0 = s4 >> 8). It is shifted left by 8 at 0x188 and exported to MRTZ at 0x190. That is the DB_SHADER_CONTROL 0x45 layout #6762 handles, with the stencil operation value in bits 8..15. The value depends on loaded data, which is why the set is not provable.

Question. StencilReplaySafe (from #5466) rejects any usesDma program, including read-only global loads like this one. A replay of a pixel shader that only reads memory looks idempotent to me. Is the exclusion there for a reason I'm missing (fault buffer, BDA robustness, cost of 256 replays), or could read-only BDA (usesDma && !bdaWrites) be allowed? I'm not changing it myself, since it's your area and #5466's.

AI-assisted: yes

@github-actions github-actions Bot added unblocks title Names a title ID on the Unblocks line needs rebase Conflicts with main, rebase needed labels Oct 18, 2026
@Capmare

Capmare commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Marvel's Wolverine (PPSA03671) hits the same refusal this PR addresses, and further refusals follow it. Linux, RTX 4070 SUPER, the PR #7395 head with local patches. I tested an equivalent local change (all 256 values when the prover has no set), not this PR's head.

  1. Before: shader 0x11d01d0900 stage 5: ShaderRecompiler::Recompile: stencil reference export emulation requires a proven set of at most 16 values. The game stops about two minutes after the intro starts.
  2. With the 256-value replay, the same shader is then refused by the replay safety check: stencil reference export emulation requires a replay-safe pixel shader: exec mask dependence: mask-dependent branch. That is a separate refusal that this PR does not change, so the title needs more than this PR.
  3. Past the safety check, the draw stops at AGC graphics: stencil replay supports direct triangle lists without restart, mesh or tessellation. Here the topology is a triangle strip.

Not tested: this PR's head itself, other GPUs, Windows.

…der may export

Without VK_EXT_shader_stencil_export (NVIDIA), stencil reference exports
are emulated by replaying the draw once per value. An export the prover
could not bound to at most 16 values was refused. The replay shader
compares the export's low eight bits, so an unbounded export now
replays all 256 values, and proven values are reduced to their low
eight bits as the comparison sees them.

Refs boykopovar#4276

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TheSovietWaffle
TheSovietWaffle force-pushed the fix/recompiler-stencil-replay-every-value branch from 059aa7d to f4a4b8a Compare October 19, 2026 19:00
@TheSovietWaffle

Copy link
Copy Markdown
Contributor Author

@oneandonlydean rebased on main 585075cb7 (now f4a4b8aea, resolving the overlap with #6762 the way @simonbronk describes above: StencilReferenceValues(program, shift) falls back to all 256 values and masks (value >> shift) & 0xff), with both review points:

  • The stencil reference export replay entry in docs/dev/debt/libSceAgcDriver.md now says proven exports replay the low eight bits of their proven values and an export the prover cannot bound replays all 256, one pass per value per triangle. (check_conventions.py still warns that this entry is over 1,000 characters; it was 1,560 on main.)
  • The first draw that takes the 256-value path logs once per process: Stencil replay: a draw replays all 256 stencil reference values because its pixel shader's export is not bounded, recording 256 passes per triangle and instance (seen once in an agc_driver_stencil_replay run).

On this head, RTX 3080 (617.42) and AMD Radeon RDNA 2 iGPU (1002:164e, 32.0.21045.5002), Windows 10: agc_masked_select, agc_driver_stencil_replay, agc_driver_stencil_replay_no_gpl, agc_driver_stencil_ref_export, agc_driver_stencil_ref_export_unused, agc_driver_stencil_null_feedback and agc_driver_sample_mask_depth_stencil pass on both, except agc_driver_stencil_ref_export skipped on the RTX 3080 (no VK_EXT_shader_stencil_export) and agc_driver_stencil_replay skipped on the Radeon (has it).

AI-assisted: yes (Claude Code).

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@TheSovietWaffle

Copy link
Copy Markdown
Contributor Author

@simonbronk on the usesDma question: I think read-only BDA is replay-safe, and I have a branch for it. Reading the BDA paths:

  • A read-only BDA program's only side effect is the first-fault record in record_bda_fault (SpirvBdaFault.cpp): a compare-exchange from empty, so every replay pass that faults records the same first fault or nothing.
  • A fault is never retried: BdaResources::CheckFault throws for every reason except the loop guard, and loops are already refused by StencilReplaySafe. Replayed draws whose resources have a BDA table already take the ChecksFaults() path in Draw.cpp (not recordable), so they wait and check faults before publishing, like the guarded SRT reads.
  • The written-page tracking (get_bda_write_pointer, note_bda_write) only exists in programs with bdaWrites, which stay refused.

One more refusal in the way: on the draw side, ShadersWriteMemory returns true for any BdaPagetable binding, so a read-only BDA pixel shader is still refused there ("stencil replay refuses face splits and shader memory writes").

Branch: https://github.com/TheSovietWaffle/AnyPS5/tree/fix/recompiler-stencil-replay-readonly-bda (one commit on main 585075cb7). StencilReplaySafe no longer refuses usesDma alone, compiled shaders carry bdaWrites (artifact field and disk cache entry), and ShadersWriteMemory counts the BDA page table as a write only for a program with bdaWrites. A new agc_masked_select case checks that read-only BDA is replayable and BDA writes are not (it fails without the change); the stencil replay, stencil export, sample-mask and disk-cache tests pass on an RTX 3080 and an AMD Radeon RDNA 2 iGPU. It touches the same debt entry as this PR, so whichever lands second needs a one-line merge there.

On Onimusha it is not enough by itself: with this and this PR, the same shader (s4 & 0xff reference, now at a different address per run) gets past usesDma and is then refused by the mask check (MaskReplaySafety; it branches on EXEC), the same class as @Capmare's Wolverine exec mask dependence: mask-dependent branch.

AI-assisted: yes (Claude Code).

@TheSovietWaffle

Copy link
Copy Markdown
Contributor Author

Follow-up on Onimusha's stencil shader (PPSA27836, the pixel shader @simonbronk posted above) for whoever owns the MaskReplaySafety proof from #5466. With read-only BDA allowed (#8145), it passes StencilReplaySafe and is then refused by MaskReplaySafety::control on block 0. I tried relaxing the proof locally and found three separate gaps. I'm leaving the design to you rather than opening a PR:

  1. Multi-block optional bodies. Block 0 branches on EXEC (ExecZero), with predicate !helper && (|imageRead.y| >= 1.0 || NaN). The predicate is stable() but not uniform. optionalBody accepts only a single-block body, but this body is a region: blocks 2–8 with nested branches, merging at block 9. Generalizing it to a single-entry region that ends at the merge worked locally, under three conditions:
    • no SetAttribute or Discard inside the region;
    • values leave the region only through merge phis;
    • each merge phi's taken value inactiveEquals its skipped value, with inactiveEquals followed through the region's own phis.
  2. Branch conditions and own-lane ballots counted as quad observations. Inside the region, the EXEC/VCC branch conditions that control() already validates, and ballots used only for the lane's own bit (LaneId plumbing), make quadObserved fail. Exempting both needs a pre-pass that runs stable()/any() on the conditions before the traversal. I only partly finished this.
  3. A lane's own bit of ballot(P) should equal P. The exec-restore phi at the merge has taken value %1317 (INotEqual of the lane's own bit of a ballot of the predicate) and skipped value %372 (the predicate's LogicalAnd). These are equal on every active lane, but inactiveEquals can't see it. I didn't attempt this one.

I tested (1) and part of (2) only against this shader, offline from its retained RecompileRequest, and I can share that request. With all three, Onimusha's first scene on NVIDIA would stop needing a stencil workaround here. #6570 still blocks it separately.

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* docs shader core/shader unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants