fix(recompiler): replay every eight-bit stencil reference a pixel shader may export #7006
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7006"
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-17T04:08:00Z.
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.StencilReferenceValuesreturned no values, and the draw was refused, whenever the prover could not bound the export to at most 16 values:The replay shader compares only the low eight bits of the export (
SpirvFlowEmitter.cpp,OpBitwiseAnd 0xffbeforeOpIEqual), so 0 to 255 covers every value any export can produce: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 currentmain.agc_masked_select(two new cases: an unprovable export replays all 256 values; a proven 300 replays as 44),agc_driver_stencil_replay,agc_driver_stencil_replay_no_gpl,agc_driver_stencil_ref_export_unusedandagc_driver_sample_mask_depth_stencilpass.agc_driver_stencil_ref_exportis skipped (it needs the extension).agc_driver_stencil_replayasserted that the column shader (ColumnPixelCode, stencil = column index) was refused. It now draws it withREPLACE_OPand checks that anEQUAL 5stencil test passes exactly column 5.APS5_NO_HOST_IMPORT=1and a local merge of #5915, #6557, #6727 and #6762: the first scene load stopped at the error above (pixel shaders0x2112444f00and0x210687de00). With this change it passes that point and goes on to the separate #6570 stop (sample-preserving guest publication of native multisample depth is not implemented).FragStencilRefEXTpath and does not reach this code).tools/check_conventions.py --base origin/mainpasses. #6762 also editsStencilReferenceValues(it adds a shift for the operation value export); the two conflict in that function and the merge is mechanical.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T04:08:18Z.
Thank you for your contribution! Authors with no pull requests merged into
maincan 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.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T23:31:58Z.
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.
main)docs/dev/debt/libSceAgcDriver.mdL65-66docs/dev/debt/libSceAgcDriver.mdL65-66docs/dev/debt/libSceAgcDriver.mdL65-66Some 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.
core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T23:57:59Z.
Thanks for this. The change is correct: the replay pass already compares only the low eight bits of the export (
SpirvFlowEmitter.cpp,OpBitwiseAnd 0xffbeforeOpIEqual), 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:54still 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:
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.Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-18T17:10:42Z.
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:main6efb7261bwith #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_stencilandagc_masked_selectpass 3 runs of 3. The 860M exposesVK_EXT_shader_stencil_export, so this is the nativeFragStencilRefpath the description mentions.AI-assisted
Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-18T19:36:33Z.
Tested on Onimusha: Way of the Sword (PPSA27836), where #4276's refusal now stops the first story-mode load (#6374).
Tree:
upstream/mainc4c2b3aa7+ #6727 + #6762 + this PR (059aa7d59). Windows 11 Pro 26200, RTX 5070 (NVIDIA 617.42, noVK_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
StencilReferenceValuesand theStencilRefcase inDefineOutputs, and they conflict. #6762 adds ashiftoverload: withDB_SHADER_CONTROL0x45 the exported value sits in bits 8..15. I resolved the merge as follows: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.
shader 0x2107d3b200 stage 5: ... requires a proven set of at most 16 values.0x2107d3b200is not reported at all. The load now stops later in the same screen on a different shader and the next check inDefineOutputs: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
Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-18T19:40:26Z.
Follow-up on the
0x211203ed00refusal above. I replayed the request from the game log offline withagc_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 notpsOrderedPixelShader(0).StencilReplaySafereturns false on its first line, becauseprogram.Info().usesDmais set:values=256: the reference set is unproven, so this PR falls back to all 256 values.Why
usesDma.APS5_TRACE_BDA=1shows a single global read: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 asv_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 theDB_SHADER_CONTROL0x45 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 anyusesDmaprogram, 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
Imported from boykopovar/AnyPS5. Originally posted by Capmare on 2026-10-19T18:00:46Z.
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.
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.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.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.
Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-19T19:01:02Z.
@oneandonlydean rebased on main
585075cb7(nowf4a4b8aea, 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:docs/dev/debt/libSceAgcDriver.mdnow 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.pystill warns that this entry is over 1,000 characters; it was 1,560 on main.)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 anagc_driver_stencil_replayrun).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_feedbackandagc_driver_sample_mask_depth_stencilpass on both, exceptagc_driver_stencil_ref_exportskipped on the RTX 3080 (noVK_EXT_shader_stencil_export) andagc_driver_stencil_replayskipped on the Radeon (has it).AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-19T19:10:18Z.
@simonbronk on the
usesDmaquestion: I think read-only BDA is replay-safe, and I have a branch for it. Reading the BDA paths:record_bda_fault(SpirvBdaFault.cpp): a compare-exchange from empty, so every replay pass that faults records the same first fault or nothing.BdaResources::CheckFaultthrows for every reason except the loop guard, and loops are already refused byStencilReplaySafe. 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 the guarded SRT reads.get_bda_write_pointer,note_bda_write) only exists in programs withbdaWrites, which stay refused.One more refusal in the way: on the draw side,
ShadersWriteMemoryreturns true for anyBdaPagetablebinding, 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).StencilReplaySafeno longer refusesusesDmaalone, compiled shaders carrybdaWrites(artifact field and disk cache entry), andShadersWriteMemorycounts the BDA page table as a write only for a program withbdaWrites. A newagc_masked_selectcase 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 & 0xffreference, now at a different address per run) gets pastusesDmaand is then refused by the mask check (MaskReplaySafety; it branches on EXEC), the same class as @Capmare's Wolverineexec mask dependence: mask-dependent branch.AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-20T00:18:27Z.
Follow-up on Onimusha's stencil shader (PPSA27836, the pixel shader @simonbronk posted above) for whoever owns the
MaskReplaySafetyproof from #5466. With read-only BDA allowed (#8145), it passesStencilReplaySafeand is then refused byMaskReplaySafety::controlon 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:ExecZero), with predicate!helper && (|imageRead.y| >= 1.0 || NaN). The predicate isstable()but not uniform.optionalBodyaccepts 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:SetAttributeorDiscardinside the region;inactiveEqualsits skipped value, withinactiveEqualsfollowed through the region's own phis.control()already validates, and ballots used only for the lane's own bit (LaneIdplumbing), makequadObservedfail. Exempting both needs a pre-pass that runsstable()/any()on the conditions before the traversal. I only partly finished this.ballot(P)should equal P. The exec-restore phi at the merge has taken value%1317(INotEqualof the lane's own bit of a ballot of the predicate) and skipped value%372(the predicate'sLogicalAnd). These are equal on every active lane, butinactiveEqualscan'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).