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

Merged
TheSovietWaffle merged 1 commit from upstream-pr/8145 into main 2026-10-23 16:46:25 +00:00
TheSovietWaffle commented 2026-10-20 00:15:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-20T00:15:26Z.


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 #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); #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 #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

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4929147270 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8145). Originally posted by [TheSovietWaffle](https://github.com/TheSovietWaffle) on 2026-10-20T00:15:26Z. --- ### 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 #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); #7006 edits the same debt entry, so whichever lands second needs a one-line merge there. ### 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) - [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 (PPSA27836 #5769 then stops at the mask check) - [x] GPUs tested: NVIDIA RTX 3080, 617.42, Windows 10 22H2; AMD Radeon RDNA 2 iGPU (`1002:164e`), 32.0.21045.5002, Windows 10 22H2 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
TheSovietWaffle commented 2026-10-20 11:07:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-20T11:07:58Z.


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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6289483396 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8145#issuecomment-6289483396). Originally posted by [TheSovietWaffle](https://github.com/TheSovietWaffle) on 2026-10-20T11:07:58Z. --- 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).
wachi35676 commented 2026-10-20 12:18:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T12:18:53Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290672397 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8145#issuecomment-6290672397). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-20T12:18:53Z. --- 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 commented 2026-10-20 18:33:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Lindamea1 on 2026-10-20T18:33:06Z.


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 #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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6298270594 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8145#issuecomment-6298270594). Originally posted by [Lindamea1](https://github.com/Lindamea1) on 2026-10-20T18:33:06Z. --- 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 #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 commented 2026-10-21 06:20:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-10-21T06:20:43Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6309502672 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8145#issuecomment-6309502672). Originally posted by [akkadbakkad89](https://github.com/akkadbakkad89) on 2026-10-21T06:20:43Z. --- 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.
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!8145
No description provided.