fix(recompiler): replay stencil exports of pixel shaders that only read through BDA #8145
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8145"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-20T00:15:26Z.
What
On devices without
VK_EXT_shader_stencil_export,StencilReplaySaferefused every program withusesDma, 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:ShadersWriteMemorycounted anyBdaPagetablebinding as a memory write.Replaying such a shader once per reference value is idempotent:
record_bda_fault, a compare-exchange from empty, so every pass that faults records the same first fault or nothing.BdaResources::CheckFaultthrows for every reason except the loop guard, andStencilReplaySafealready refuses loops. Replayed draws whose resources have a BDA table already take theChecksFaults()path inDraw.cpp(not recordable), so they wait and check faults before publishing, like guarded SRT reads.get_bda_write_pointer,note_bda_write) exists only in programs withbdaWrites, which stay refused.Changes:
StencilReplaySafeno longer refusesusesDmaalone.bdaWrites(artifact field and disk cache entry), andShadersWriteMemorycounts the BDA page table as a write only for a program withbdaWrites.docs/dev/debt/libSceAgcDriver.mdnames 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.agc_masked_selectcase: read-only BDA is replayable and BDA writes are not; it fails without the change.agc_shader_disk_cacheround-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_stencilandagc_driver_stencil_null_feedbackpass on both GPUs below, exceptagc_driver_stencil_ref_exportskipped on the RTX 3080 (noVK_EXT_shader_stencil_export) andagc_driver_stencil_replayskipped on the Radeon (has it).usesDmaand is then refused byMaskReplaySafety(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.pywas not run (noghon this host); #7006 edits the same debt entry, so whichever lands second needs a one-line merge there.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)1002:164e), 32.0.21045.5002, Windows 10 22H2🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-20T11:07:58Z.
Rebased on main
4f99cbffd(now9b84f9d5a) after #7006 merged. The only conflict was the stencil reference export replay entry indocs/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_stencilandagc_driver_stencil_null_feedbackpass, except the replay tests skipped on the Radeon andagc_driver_stencil_ref_exportskipped on the RTX 3080 (extension present or absent).check_conventions.pyreports only the existing over-length warning for that entry.AI-assisted: yes (Claude Code).
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
main02fb1ce53:docs/dev/debt/libSceAgcDriver.mdconflicts.mainhas 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 keptmain'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_pathsand_large_file),agc_driver_stencil_replay,stencil_replay_no_gpl,stencil_ref_export_unused,stencil_null_feedbackandsample_mask_depth_stencil: these pass 3 of 3 on each GPU.agc_driver_stencil_ref_exportskips on NVIDIA because the GPU has no extension.MaskedSelect.cppalone onmainfails withread-only BDA reads can be replayed and BDA writes cannot.ShaderDiskCache.cppalone doesn't compile onmain(CompiledShaderArtifacthas no memberbdaWrites), as expected.agc_driver_graphics): 58 of 58 pass on each GPU. On this AMD driveragc_driver_stencil_replay_no_gplpasses on bothmainand the candidate.ShaderResources::ChecksFaults()isbda != 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 newbdaWritesfield gets a new directory.AI-assisted: yes (Claude Code); results above were run on this machine.
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 head9b84f9d5aapplied ontomainacda0b06f.main, on its first boot, the title stops after its EULA screens onshader 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-onlyglobal_load_dwordthrough a pointer it reads from a buffer, sousesDmarefuses it.MaskReplaySafety("own-lane mask not stable" at thes_mov_b64 exec, s[20:21]restore), as the description says for Onimusha. Two shapes cause it: the EXEC copies are translated asexec & WQM(Ballot(true))since #6291, whichmaskStableandhelpers()do not accept, and thes_cbranch_execzregions have multi-block bodies, whichoptionalBodydoes not accept. So this PR alone does not unblock this title either.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 andInPlaceReads()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, tosample-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 constantson the way. That one is onmainitself and has its own PR: #8615.AI-assisted: yes (Claude Code). I ran the builds and the title on my machine.
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 — capturedRecompileRequestrun through the IR pipeline +StencilReplaySafelocally on this branch (9b84f9d5a1).Result:
StencilReplaySafestill returns false — but not on the check this PR removes.program.Info().usesDmawas already false; the shader dies insideMaskReplaySafety::CheckatmaskStable()on an own-mask writer. The mask chain isBallot/WqmU64helper-lane bookkeeping, somasksis set and the analyzer can't prove the exec mask stable across replays.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
usesDmarelaxation 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 ismaskStableon WQM helper-lane masks (+samples == 1), tracked by #4276.