fix(shader): keep every lane in a stencil export replay when the device can store through an address #18530
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/18530"
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 Capmare on 2026-11-11T20:25:49Z.
What
On a device without
VK_EXT_shader_stencil_export(NVIDIA), a pixel shader that exports a stencil reference runs once per reference value, and the hardware stencil test of each pass can skip pixels before the shader runs. A wave branch or a lane read in the shader then sees other lanes than the guest does.StencilReplaySafetherefore refuses every shader whose exec-mask control it cannot prove independent of the lane set, and the draw aborts withstencil reference export emulation requires a replay-safe pixel shader with late tests.Marvel's Wolverine (PPSA03671) stops on this: shader
0x11d01d0900is a 208-word pass that loads a texel, remaps it through constant tables, divides by a uniform value and exports depth and stencil. Itss_cbranch_execzafter av_cmpxon the loaded value splits it into two return paths, and the analysis only accepts a body that rejoins a merge block.The Vulkan spec lets a driver run a fragment shader on fewer pixels than the guest only when the shader has no side effects. A replay shader that the lane-set proof cannot clear now ends in a store through a device address behind a condition that is always false (the reference is at most 255, and the guard reads it from a push constant, so the driver cannot fold it). The driver has to run that shader on every covered pixel in every pass, as the guest does. A shader that the proof already clears gets no store and keeps the early tests, so no shader that works today gets slower. With the lane set fixed,
StencilReplaySafeno longer needs the lane-mask proof or the ban on lane reads. Shader clock reads, loops, memory writes, shared memory and the other earlier checks stay. A device withoutPhysicalStorageBufferAddressesorInt64keeps the earlier analysis. A device that exposes the extension (Intel and llvmpipe here) exports the stencil natively and is not affected.The change:
StencilReplay.cpp:StencilReplaySafe(program, lanesFixed),StencilReplayLanesFixed(capabilities)andStencilReplayNeedsStore(program, capabilities).SpirvFlowEmitter.cppandSpirvModuleSetup.cpp: the store and the address capability for replay modules.ShaderValidation.cpp: the capability andSPV_KHR_physical_storage_bufferare allowed for a replay fragment shader.docs/dev/debt/shader.md: the entry says the guarantee is measured on one driver only.Tested
Rocky Linux 10.2, i7-14700K, RTX 4070 SUPER, NVIDIA 620.70.06, Release.
maina26b7827e1and this branch, same machine. Tests were run asANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest -V -R '^agc_driver|shader_recompiler'.mainThe 18 failing tests are the same set on both sides:
agc_driver_division,agc_driver_f32_denormal_flush,agc_driver_image_table,agc_driver_image_table_packed*(11),agc_driver_mesh_vector_window_writesandagc_driver_mullit_f32_denormal_modes. Their logs carry the sameVUID-counts on both sides, and neither log containswas not found but was requested by env var VK_INSTANCE_LAYERSapart from ctest echoing its regex:New case in
agc_driver_stencil_replay: a pixel shader (assembled for gfx1030, wave64) whose branch tests whether any lane has an odd column. Even columns export 255 on one path and 254 on the other, and the stencil buffer holds 255 under even and 0 under odd columns. In the guest every wave holds both kinds of column, so every pixel is drawn.pixel (0, 0) is 64, expected drawn. The hardware skips the odd columns in the pass for 255, the branch flips and the even columns export 254.main, the shader is refused withstencil reference export emulation requires a replay-safe pixel shader with late tests.agc_driver_stencil_replayalso passes under the same validation settings on Intel RPL-S and llvmpipe (Mesa 25.2.7, selected withVK_DRIVER_FILES). Both export the stencil natively, so they do not run the replay.Marvel's Wolverine: I replayed the dumped request of
0x11d01d0900through the recompiler offline (the dump lacks one descriptor, which I supplied). It aborts onmainand recompiles on this branch (5157 SPIR-V words). I have not drawn it in the game, because the run stalls on texture memory before it reaches this draw (see #12575).The guarantee rests on the driver following the spec for shaders with side effects (chapter Fragment Operations, section Fragment Shading). The spec does not fix how the driver groups pixels into waves, so the replay assumes that every pass groups them the same way. I measured it on NVIDIA (RTX 4070 SUPER) and, through runs by mrfelek and domx4q, on an RTX 3080 Ti and an RTX 4090. JJenkx and blawesom ran it on AMD (RX 6700 XT, driver 26.10.1, Windows; RX 9070 XT with RADV, Linux). The debt entry says so.
fragmentStoresAndAtomicsis always required and enabled, the shader cache key already holds the capability list, andagc_driver_stencil_replaylogs no new VUID.tools/pr_overlap.pyfailed closed, so I searched the diffs of the 39 PRs it lists for the replay code. #10351 (stencil operation values through an attachment) changesSelectStencilExportModeandSpirvEmitterHelpers.cpparound the same lines, and #7689 changes one condition ofStencilReplaySafe. Neither adds the lane guarantee. #18322 admits first-lane reads of wave-uniform values on every device and complements this change, which also covers non-uniform lane reads and wave branches but needsPhysicalStorageBufferAddressesandInt64. #16571 only touches a neighbouring debt entry indocs/dev/debt/shader.md. I will rebase on whichever lands first.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)agc_driver_stencil_replayonly)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T15:29:10Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
25b10fcedb(base: main).core/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cpplines 84–135.Results
Findings
Blocking
None.
Non-blocking
core/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cpp:239:storeDeviceholdsStencilReplayLanesFixed(...), not a store decision. The final check at line 249 compares store presence with that flag, which only holds if the wave-branch shader fails the strict proof. Renaming the variable tolanesFixedwould make that dependency visible.core/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cpp:134:LastReplayStoredis set by anyOpConvertUToPtrin the replay module, not only by the reference store. Another device-address conversion would set it too. Matching the exact store sequence would make the check precise.core/shader/recompiler/Recompiler.cpp:909:StencilReplaySafeandStencilReplayNeedsStorerun the strict analysis again for each replay shader, andDefineOutputsrepeats it. The cost is small, but the result could be computed once and passed along.Not verified
fragmentStoresAndAtomicsis always required and enabled is not in the diff and was not checked.StencilReplaySafeoutside the changed files were not read.Sources
25b10fcedb; line numbers refer to that commit.To get this to a good state
Nothing is required. The non-blocking points above are optional.