Skip to content

fix(recompiler): replay stencil exports of pixel shaders that only read through BDA - #8145

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
TheSovietWaffle:fix/recompiler-stencil-replay-readonly-bda
Oct 23, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
TheSovietWaffle:fix/recompiler-stencil-replay-readonly-bda

Conversation

@TheSovietWaffle

Copy link
Copy Markdown
Contributor

What

On devices without VK_EXT_shader_stencil_export, StencilReplaySafe refused every program with usesDma, including pixel shaders whose only BDA accesses are reads (Onimusha: Way of the Sword, PPSA27836: one global load, then image and buffer reads; @simonbronk's question on #7006). The draw side refused them too: ShadersWriteMemory counted any BdaPagetable binding as a memory write.

Replaying such a shader once per reference value is idempotent:

  • Its only side effect is the first-fault record in record_bda_fault, a compare-exchange from empty, so every 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 StencilReplaySafe already refuses loops. 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 guarded SRT reads.
  • The written-page tracking (get_bda_write_pointer, note_bda_write) exists only in programs with bdaWrites, which stay refused.

Changes:

  • 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.
  • The stencil reference export replay entry in docs/dev/debt/libSceAgcDriver.md names read-only BDA reads next to guarded SRT reads.

Tested

Windows 10 Pro 22H2 (19045), WinLibs GCC 15.2.0 r7, Release, on main 3aa35dc01.

  • New agc_masked_select case: read-only BDA is replayable and BDA writes are not; it fails without the change. agc_shader_disk_cache round-trips the new flag.
  • agc_masked_select, agc_shader_disk_cache, 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_driver_stencil_null_feedback pass on both GPUs below, 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).
  • Onimusha's stencil shader, with this change and fix(recompiler): replay every eight-bit stencil reference a pixel shader may export #7006, gets past usesDma and is then refused by MaskReplaySafety (it branches on EXEC), so this does not unblock the title by itself.
  • tools/check_conventions.py --base origin/main: 0 errors; it warns that the stencil reference export replay debt entry is over 1,000 characters, which it already was on main. tools/pr_overlap.py was not run (no gh on this host); fix(recompiler): replay every eight-bit stencil reference a pixel shader may export #7006 edits the same debt entry, so whichever lands second needs a one-line merge there.

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 (PPSA27836 Onimusha: Way of the Sword (PPSA27836) #5769 then stops at the mask check)
  • GPUs tested: NVIDIA RTX 3080, 617.42, Windows 10 22H2; AMD Radeon RDNA 2 iGPU (1002:164e), 32.0.21045.5002, Windows 10 22H2
  • AI-assisted: yes

🤖 Generated with Claude Code

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Oct 20, 2026
@github-actions

github-actions Bot commented Oct 20, 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 6959 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp L150, L261
core/shader/recompiler/Recompiler.cpp L663-664, L1010
core/shader/recompiler/Recompiler.hpp L680
core/shader/recompiler/ShaderDiskCache.cpp L525, L589
PR 9186 docs/dev/debt/libSceAgcDriver.md L82-83
PR 9266 core/libs/prx/libSceAgcDriver/tests/MaskedSelect.cpp L1068
PR 9317 core/libs/prx/libSceAgcDriver/tests/MaskedSelect.cpp L1068
docs/dev/debt/libSceAgcDriver.md L83
PR 9700 docs/dev/debt/libSceAgcDriver.md L82-83
PR 10049 docs/dev/debt/libSceAgcDriver.md L82-83

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 2434 docs/dev/debt/libSceAgcDriver.md
PR 4072 core/shader/recompiler/Recompiler.cpp
PR 4448 docs/dev/debt/libSceAgcDriver.md
PR 4449 docs/dev/debt/libSceAgcDriver.md
PR 5021 core/shader/recompiler/Recompiler.cpp
PR 5058 docs/dev/debt/libSceAgcDriver.md
PR 5609 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 5925 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6186 docs/dev/debt/libSceAgcDriver.md
PR 6188 core/shader/recompiler/Recompiler.hpp
PR 6194 docs/dev/debt/libSceAgcDriver.md
PR 6223 docs/dev/debt/libSceAgcDriver.md
PR 6235 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.hpp
PR 6273 docs/dev/debt/libSceAgcDriver.md
PR 6280 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/ShaderDiskCache.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6300 docs/dev/debt/libSceAgcDriver.md
PR 6412 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6481 docs/dev/debt/libSceAgcDriver.md
PR 6488 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 6498 docs/dev/debt/libSceAgcDriver.md
PR 6539 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 6548 docs/dev/debt/libSceAgcDriver.md
PR 6549 docs/dev/debt/libSceAgcDriver.md
PR 6612 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
docs/dev/debt/libSceAgcDriver.md
PR 6804 docs/dev/debt/libSceAgcDriver.md
PR 6808 docs/dev/debt/libSceAgcDriver.md
PR 6867 docs/dev/debt/libSceAgcDriver.md
PR 6868 docs/dev/debt/libSceAgcDriver.md
PR 6924 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 6970 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 6973 docs/dev/debt/libSceAgcDriver.md
PR 6977 docs/dev/debt/libSceAgcDriver.md
PR 6987 docs/dev/debt/libSceAgcDriver.md
PR 6989 docs/dev/debt/libSceAgcDriver.md
PR 6995 docs/dev/debt/libSceAgcDriver.md
PR 7002 docs/dev/debt/libSceAgcDriver.md
PR 7201 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 7256 docs/dev/debt/libSceAgcDriver.md
PR 7275 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
PR 7307 docs/dev/debt/libSceAgcDriver.md
PR 7410 core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/StencilReplay.cpp
PR 7449 docs/dev/debt/libSceAgcDriver.md
PR 7484 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 7550 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 7604 docs/dev/debt/libSceAgcDriver.md
PR 7618 docs/dev/debt/libSceAgcDriver.md
PR 7622 core/shader/recompiler/ShaderDiskCache.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7684 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 7689 core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 7844 docs/dev/debt/libSceAgcDriver.md
PR 7893 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7910 docs/dev/debt/libSceAgcDriver.md
PR 7978 docs/dev/debt/libSceAgcDriver.md
PR 8014 docs/dev/debt/libSceAgcDriver.md
PR 8077 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 8223 docs/dev/debt/libSceAgcDriver.md
PR 8319 docs/dev/debt/libSceAgcDriver.md
PR 8365 docs/dev/debt/libSceAgcDriver.md
PR 8489 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 8542 docs/dev/debt/libSceAgcDriver.md
PR 8564 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
docs/dev/debt/libSceAgcDriver.md
PR 8592 docs/dev/debt/libSceAgcDriver.md
PR 8740 docs/dev/debt/libSceAgcDriver.md
PR 8999 docs/dev/debt/libSceAgcDriver.md
PR 9020 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9077 docs/dev/debt/libSceAgcDriver.md
PR 9335 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9356 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 9361 docs/dev/debt/libSceAgcDriver.md
PR 9387 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9431 docs/dev/debt/libSceAgcDriver.md
PR 9432 docs/dev/debt/libSceAgcDriver.md
PR 9489 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
PR 9592 docs/dev/debt/libSceAgcDriver.md
PR 9672 docs/dev/debt/libSceAgcDriver.md
PR 9673 docs/dev/debt/libSceAgcDriver.md
PR 9674 docs/dev/debt/libSceAgcDriver.md
PR 9675 docs/dev/debt/libSceAgcDriver.md
PR 9676 docs/dev/debt/libSceAgcDriver.md
PR 9748 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9891 docs/dev/debt/libSceAgcDriver.md
PR 9892 docs/dev/debt/libSceAgcDriver.md
PR 9893 docs/dev/debt/libSceAgcDriver.md
PR 9895 docs/dev/debt/libSceAgcDriver.md
PR 9897 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 9898 docs/dev/debt/libSceAgcDriver.md
PR 10000 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
PR 10064 docs/dev/debt/libSceAgcDriver.md
PR 10193 core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/ShaderDiskCache.cpp
docs/dev/debt/libSceAgcDriver.md

@github-actions github-actions Bot added shader core/shader docs agc libSceAgc* needs rebase Conflicts with main, rebase needed and removed unblocks title Names a title ID on the Unblocks line labels Oct 20, 2026
…ad through BDA

StencilReplaySafe refused every program with usesDma, including pixel
shaders whose only BDA accesses are reads (Onimusha: Way of the Sword,
PPSA27836: one global load, then image and buffer reads). Replaying such
a shader per reference value is idempotent: its only side effect is the
first-fault record, a compare-exchange that keeps the first fault; a
fault is never retried but thrown by CheckFault, and replayed draws with
a BDA table already wait and check faults before publishing. Written-page
tracking exists only in programs with bdaWrites, which stay refused.

Compiled shaders now carry bdaWrites, and ShadersWriteMemory counts a BDA
page table as a memory write only for a program that writes through it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TheSovietWaffle
TheSovietWaffle force-pushed the fix/recompiler-stencil-replay-readonly-bda branch from 1b03461 to 9b84f9d Compare October 20, 2026 11:07
@TheSovietWaffle

Copy link
Copy Markdown
Contributor Author

Rebased on main 4f99cbffd (now 9b84f9d5a) after #7006 merged. The only conflict was the stencil reference export replay entry in docs/dev/debt/libSceAgcDriver.md; it now keeps #7006's text and adds "and read-only BDA reads may report faults" after the guarded SRT reads.

On this head, RTX 3080 (617.42) and Radeon RDNA 2 iGPU (1002:164e, 32.0.21045.5002), Windows 10: agc_masked_select, agc_shader_disk_cache, 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_driver_stencil_null_feedback pass, except the replay tests skipped on the Radeon and agc_driver_stencil_ref_export skipped on the RTX 3080 (extension present or absent). check_conventions.py reports only the existing over-length warning for that entry.

AI-assisted: yes (Claude Code).

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

Copy link
Copy Markdown
Contributor

Tested on an RTX 5070 Ti (NVIDIA 617.42, no VK_EXT_shader_stencil_export, so the replay path runs) and an AMD Radeon iGPU (AMD proprietary 2.0.353, which has the extension). Windows 11 (26300), WinLibs GCC 15.2.0, Release, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1.

The branch no longer merges cleanly onto current main 02fb1ce53: docs/dev/debt/libSceAgcDriver.md conflicts. main has since rewritten the stencil reference export replay entry, which now replays all 256 values for an export the prover cannot bound and says "internal host faults". The branch still carries the old "at most sixteen values" text. For testing I kept main's entry and added only "and read-only BDA reads" to it, so the PR needs a rebase on that line.

  • agc_masked_select, agc_shader_disk_cache (plus _long_paths and _large_file), agc_driver_stencil_replay, stencil_replay_no_gpl, stencil_ref_export_unused, stencil_null_feedback and sample_mask_depth_stencil: these pass 3 of 3 on each GPU. agc_driver_stencil_ref_export skips on NVIDIA because the GPU has no extension.
  • Negative control: this PR's MaskedSelect.cpp alone on main fails with read-only BDA reads can be replayed and BDA writes cannot. ShaderDiskCache.cpp alone doesn't compile on main (CompiledShaderArtifact has no member bdaWrites), as expected.
  • Related set of 58 tests (stencil, msaa, multisample, label, tessellation, write_tracking, resident and so on, plus agc_driver_graphics): 58 of 58 pass on each GPU. On this AMD driver agc_driver_stencil_replay_no_gpl passes on both main and the candidate.
  • From the code: ShaderResources::ChecksFaults() is bda != nullptr, so a replayed draw with a BDA table takes the wait-and-check path, as the description says.

Not run: the validation layer, a title, and a disk cache written before this change. The cache directory is keyed by the hash in ShaderCacheSourceVersion, so the new bdaWrites field gets a new directory.

AI-assisted: yes (Claude Code); results above were run on this machine.

@Lindamea1

Copy link
Copy Markdown
Contributor

Ran this with Monster Hunter Wilds (PPSA24191, #7972) on Linux: RTX 5090 (NVIDIA 595.99.02, no VK_EXT_shader_stencil_export, so the replay path runs), GCC 16.2.1, Release, this head 9b84f9d5a applied onto main acda0b06f.

  • On main, on its first boot, the title stops after its EULA screens on shader 0x224b431a00 stage 5: ShaderRecompiler::Recompile: stencil reference export emulation requires a replay-safe pixel shader with late tests. It is a full-screen pixel shader that exports depth and stencil and does one read-only global_load_dword through a pointer it reads from a buffer, so usesDma refuses it.
  • With this PR that refusal is gone. The shader is then refused by MaskReplaySafety ("own-lane mask not stable" at the s_mov_b64 exec, s[20:21] restore), as the description says for Onimusha. Two shapes cause it: the EXEC copies are translated as exec & WQM(Ballot(true)) since fix(shader): preserve pixel helper lanes in EXEC copies #6291, which maskStable and helpers() do not accept, and the s_cbranch_execz regions have multi-block bodies, which optionalBody does not accept. So this PR alone does not unblock this title either.
  • With a local prover change for those two shapes (not part of this PR), the shader replays, and the draw is refused at Draw.cpp:2309: stencil replay refuses depth or stencil feedback reads. A diagnostic run showed why. The shader reads through BDA, so the draw's build is address-based and InPlaceReads() lists every imported range (1689 here); two 8 MiB ranges among them overlap the 1920x1080 depth target. The shader's own V#s do not. Since this PR makes BDA-reading shaders replayable, that check will refuse their draws whenever the depth target lies in imported memory. With the check disabled (an experiment only: the depth image is host-side and single-sample, so a pass cannot read an earlier pass's writes through guest memory) the title got one step further, to sample-preserving guest publication of native multisample depth is not implemented.

Separately, the replayed draw also hit guest buffer offset lies outside the shader's push constants on the way. That one is on main itself and has its own PR: #8615.

AI-assisted: yes (Claude Code). I ran the builds and the title on my machine.

@akkadbakkad89

Copy link
Copy Markdown
Contributor

Boundary datapoint for this PR — a shader it does not unblock, from a real title.

Shader: PPSA20397 (Assassin's Creed Shadows), hash 0x407325e400, stage 5 — captured RecompileRequest run through the IR pipeline + StencilReplaySafe locally on this branch (9b84f9d5a1).

Result: StencilReplaySafe still returns false — but not on the check this PR removes. program.Info().usesDma was already false; the shader dies inside MaskReplaySafety::Check at maskStable() on an own-mask writer. The mask chain is Ballot/WqmU64 helper-lane bookkeeping, so masks is set and the analyzer can't prove the exec mask stable across replays.

StencilReferenceValues: count=256   (unbounded ImageRead-derived ref -> full 8-bit set)
DefinedForKept: 1
ReplaySafe: 0                       <- maskStable failure, same as main

The draw is also 4×MSAA with all-REPLACE/ALWAYS stencil ops (stencil[pixel] = imageRead.y & 0xff), so even past safety it would need the multisample path.

So the usesDma relaxation covers read-through-BDA titles (Onimusha) but not this class: WQM/ballot mask writers. For anyone hitting "requires a replay-safe pixel shader with late tests" after this lands, the remaining frontier is maskStable on WQM helper-lane masks (+ samples == 1), tracked by #4276.

@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the diff against main: bdaWrites is set for writes and atomics (ResourceTracker.cpp), so programs that write through BDA stay refused by StencilReplaySafe and still count as memory writes in ShadersWriteMemory. The flag is carried through the artifact and the disk cache (round-trip covered in agc_shader_disk_cache), and the new agc_masked_select case fails without the change. Read-only BDA replay is confirmed on an RTX 5090 (Monster Hunter Wilds) and an RTX 5070 Ti by @Lindamea1 and @wachi35676; merged.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants