fix(shader): export valid-mask colors without branching on EXEC #14341
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14341"
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 LeandroLP23 on 2026-11-01T10:06:19Z.
What
On Windows,
agc_driver_quad_pixel_maskfails in the implicit-LOD case under a partial quad mask, on both AMD and NVIDIA (#14237). The pixel samples the wrong mip level: level 0 on an RTX 5060 Ti, levels 3 and 4 on a Radeon 860M, where level 2 is expected.The cause is not the demoted helpers. A pixel export with
vmset was stored insideif (exec) { ... }. The sampled value is only used inside that branch, so both Windows drivers move theOpImageSampleImplicitLodinto it, out of uniform control flow, and the derivatives lose the neighbouring lanes. I checked this by editing the generated SPIR-V by hand on both GPUs:OpDemoteToHelperInvocationremovedOpKills removedtrueThe fix keeps the EXEC semantics but drops the divergent control flow, only where it matters. A pixel program whose result depends on helper lanes either has
neighbourLaneReadsset (implicit-LOD samples,ImageQueryLod, DPP/swizzle/permute/readlane/ballot, WQM) or reads an offset barycentric (aGetBuiltinof a sample or centre barycentric kind), which goes throughInterpolateAtOffset. In such a program, an MRT export without a color merge now storesselect(exec, new, previous)unconditionally.previousis the last color the invocation stored to that output, kept in aPrivateshadow per output, zero-initialized withOpConstantNull, because the packed and reversed-order output specializations only accept whole stores to theOutputvariable. A lane with EXEC clear therefore keeps its earlier color, including when a latervmexport makes it valid again, and the helper-lane reads stay in uniform control flow. Every other pixel export keeps its EXEC branch as onmain, and so do targets with a color merge and the alpha-to-coverage store (in its own branch).New checks:
agc_driver_quad_pixel_maskalso runs the sequence from review: red to MRT 0 with EXEC set, then green to MRT 0 withvmand EXEC clear, then a NULLvm doneexport with EXEC set. It expects red. It passes onmain, fails on this PR's first version (52fcd28bba, which stored green), and passes now.agc_shader_quad_mask_no_demotechecks that the color store of a pixel program with a live cross-lane read (ds_permutewith EXEC left at the live lanes) is not nested in any selection or loop construct, with and without a quad mask. That check needs no GPU. It fails withmain's emitter and passes here.20acd940ce, whose check looked for the offset kinds in the program's input list, which records only the base barycentric.Tested
Windows 11, NVIDIA RTX 5060 Ti: full
ctest -j 6withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,main26fa1212b8against81b3d01f31.main: 2 of 3,265 failed,agc_driver_quad_pixel_maskandagc_driver_vulkan_test_device_no_driver(the elevated-loader case of #14069).agc_driver_vulkan_test_device_no_driverfailed.VUID-line.Windows 11, AMD Radeon 860M:
ctest -R '^(agc_|shader)'(1,217 tests) on81b3d01f31, same environment.agc_driver_quad_pixel_maskandagc_shader_quad_mask_no_demotepass.agc_driver_mesh,agc_driver_msaa_dcc_srgb_clear_2/4/8andagc_driver_sample_order_grid_reference. They fail the same way withmain'sSpirvModuleEmitter.cpp.VUID-lines, and the layer was loaded.Windows 11, NVIDIA RTX 5060 Ti (621.01), head
d35348be5eonmainb611e634f7:agc_driver_quad_pixel_maskandagc_shader_memorywith the same environment.main: the quad mask test fails; this PR: both pass. The layer was loaded and there are noVUID-lines.Independent runs:
d35348be5ewith #9503 onmainc3a5290c7d:agc_driver_sample_order_grid_referencepasses 3 of 3 (0 color and 0 depth samples differ), where #9503 alone leaves 42 color samples.agc_driver_quad_pixel_maskpasses onmainand on this head there.81b3d01f31: the RTX 5070 Ti (621.01) fails onmainand passes with this PR; the Ryzen 7 7700X iGPU (RDNA 2, AMD 24.30.18) passes on both. NoVUID-lines.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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T10:14:16Z.
Review: COMMENTED
Reviewed — no blocking findings
The unconditional store is sound on the paths I read: a lane with a clear valid mask is killed at every return unless it is a helper invocation, so its stored value is discarded. One non-blocking question remains about helper invocations.
What I ran
Reviewed head
25eb2a4c36d8d62f6567161fc0e7f0842c256824against base26fa1212b82f76dd49cf12d65e445d6ff3e0844c; the precheck merge preview is clean. Environment: Linux x86-64, Python 3.11. I read the full diff and the emitter paths listed under Sources at this head. No local build or shader test was run.The precheck's ABI and test-quality helpers were missing from the PR head tree, so I ran both by hand from the main checkout.
Results
git diff --check, conventions and PR template (precheck)possible-unregistered-cpp-testfinding forcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp; false positive, the file is registered incore/libs/prx/libSceAgcDriver/tests/cmake/agc_shader_memory_tests.cmakeconventions_and_pr_template, unedited precheck output
Findings
Blocking
None found.
Non-blocking
EmitKillIfPixelValidMaskInactivekeeps a helper invocation alive even when its valid mask is clear (SpirvFlowEmitter.cpp#L30-L45). Such a lane now executes the unconditional store, where the EXEC branch previously skipped it. Please state why a value written by a helper lane is not observable on the target, or cover that case in the GPU evidence. This is a question, not a request for a change.Sources
vmexport: SpirvModuleEmitter.cpp#L1840-L1841, #L2164-L2167.vm, no color merge): SpirvModuleEmitter.cpp#L2271-L2273.Not verified
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:41:10Z.
Review: COMMENTED
The diagnosis is clear: sinking the sample into the EXEC branch is what breaks the derivatives, and the table of hand-edited modules shows it. The fix is wider than the bug, though, and the PR carries no cost figure for that.
1. It applies to every pixel shader, not only to the shaders that hit the bug.
preserveInactiveisstage == Pixel && kind == Mrt && no merge for the target. Nothing in it looks atvm, a quad mask, or an implicit-LOD sample. So every ordinary pixel export in every title now gets aPrivateshadow variable, anOpLoadof it, a 4-wideOpCompositeConstructof EXEC, anOpSelect, and two stores, where before it had one branch. In the common case (EXEC set for the whole shader, a single export) a driver compiler can usually fold the select away, but only if it can prove EXEC is uniform. The shader here reads EXEC as a runtime value, so on a shader that doess_mov execor wave-mask work before the export the compiler keeps 4v_cndmaskper export per target and an extra live range. With 8 MRTs that is 32 selects per pixel. The newagc_shader_quad_mask_no_demotetest counts nesting, not instructions, so it does not show either.Please restrict
preserveInactiveto the cases that need it: a program with a quad mask or demote, an export withvm, or a program that samples with implicit LOD after an EXEC change. Everything else keeps the branch it has onmain. If you want it unconditional, show the cost.2. The SPIR-V and pipeline compile-time cost is not measured. The description tests correctness on four GPUs and runs the full suite, but gives no figure for SPIR-V size or pixel-stage compile time. A before/after of
spirv-dis | wc -landDriver.Draw.preparems per frame on one pixel-heavy scene would settle it. Pipeline compile time matters on a cold cache, where every pixel variant is compiled at the first draw.3. Uninitialized shadow. The first export reads the
Privateshadow before any store. That is undefined, so a lane with EXEC clear on the first export gets an undefined color, wheremain's branch left the output undefined as well. Fine for correctness, but state that thepreviousvalue is only meaningful after a store, and consider anOpConstantNullinitializer so drivers can fold the first select when EXEC is a known constant.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T07:49:40Z.
Review: COMMENTED
Reviewed — no blocking findings; the valid-mask export path has no hosted run of its execution test
This PR changes how the SPIR-V emitter writes pixel exports when the shader reads helper lanes. Inactive lanes now keep their previous value through a private shadow variable, instead of branching on
EXEC. The Windows run passed three shader cases, but the execution test for this path,QuadPixelMask.cpp, is a stub on Windows and did not run. I read the production change in full.What I ran
20acd940ce94896ade451a0dc142e28c9244ecc3; base5bae1a4bc2cffc66c13e37ce8e63ae48b1b5dd1e. 4 files, +68/-3:SpirvBackend/src/SpirvModuleEmitter.cpp(+35/-, the export path),SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp(+1),libSceAgcDriver/tests/ShaderMemory.cpp(+28), andlibSceAgcDriver/tests/execution/QuadPixelMask.cpp(+7).SpirvModuleEmitter.cppchange in full. When a pixel MRT export is not merged and the shader reads neighbour lanes or non-barycentric inputs, the export writes through a privateSelectbetween the new value (active lanes) and the previous value (inactive lanes). The shadow variable is added to the interface list only for SPIR-V 1.4 and later. Otherwise the code keeps the originalEXECbranch.ShaderMemory.cppadditions by their names only, andQuadPixelMask.cppby its names only.agc_shader_quad_mask_no_demote(Test #1909),agc_shader_result_memo_budget_masked(Test #1913),agc_shader_table_snapshot_reuse_unmasked(Test #1904) andagc_shader_memory(Test #1917) passed. TheQuadPixelMaskexecution test does not appear in the run; on Windows it is a stub unless it is listed inANYPS5_AGC_EXECUTION_TESTS, and it is not.Results
QuadPixelMaskexecution test (the changed path)SpirvModuleEmitter.cpp(read)Read-only check output
Precheck was not run: no PR-head code is run here. Diff check: passed. Conventions: passed. ABI audit: not applicable. Stack check: clean, no declared dependencies, no errors. Hosted:
gh api repos/boykopovar/AnyPS5/commits/20acd940ce94896ade451a0dc142e28c9244ecc3/check-runs, and the Windows job log (21 of 21).Negative control
Not run here. A negative control needs a local build and run of the execution test, and code from this head is not run here.
Findings
Blocking
None from the code read.
Non-blocking
QuadPixelMask) did not run on Windows. The hosted shader cases that passed do not exercise the valid-mask export on a helper-lane shader. A run ofQuadPixelMaskon a host that builds the execution tests would close that gap.Sources
github.com/boykopovar/AnyPS5@20acd940ce/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppgithub.com/boykopovar/AnyPS5@20acd940ce/core/libs/prx/libSceAgcDriver/tests/execution/QuadPixelMask.cppgithub.com/boykopovar/AnyPS5@20acd940ce/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppwindowson 20acd940ce94896ade451a0dc142e28c9244ecc3Not verified
QuadPixelMaskexecution test (stub on Windows).ShaderMemory.cppadditions.To get this to a good state
QuadPixelMaskat this head in hosted CI, or add it to the Windows execution list.Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T14:40:59Z.
Review: COMMENTED
A Windows NVIDIA run on a GPU where
agc_driver_quad_pixel_maskalready passes onmain, to check that this change keeps it passing.Setup: Windows 11 x64, NVIDIA laptop GPU (Turing), driver 610, WinLibs GCC 15.2.0 r7 UCRT, Release, every AGC execution test built. Head
608b3860c7merged ontomain4e44ed59b2(clean merge), withmain4e44ed59b2from the same build directory as the control.ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1,Selected GPUwas the NVIDIA GPU. No validation layer on this machine.agc_driver_quad_pixel_maskmain4e44ed59b2No
pixel (1, 0) channel 0 is 32, expected 96on either side. This driver is older than 617.42, which #14237 reports as passing, so it does not reproduce the 621.x failure. It does show that this change does not regress Turing.AI-assisted: yes.