fix(agc): test depth before pixel shaders that ask for it #4210
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4210"
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 karanvora2599 on 2026-10-12T01:25:10Z.
What
A pixel shader whose
DB_SHADER_CONTROLsetsDEPTH_BEFORE_SHADER(bit 12) now getsEarlyFragmentTests, andZ_ORDEREARLY_Z_THEN_RE_Z(3) counts as early depth likeEARLY_Z_THEN_LATE_Z(1).Background.
DEPTH_BEFORE_SHADERis what a pixel shader compiled for early depth/stencil sets: the depth and stencil tests, and their writes, happen before the shader runs, even when the shader kills pixels or writes memory. The precheck accepts the bit (State.cpp,ShaderControlMask), butDecodePixelStageInfonever read it.EarlyFragmentTestsonly whenZ_ORDERwas 1 and they had no kill or exports. That leaves two differences from the hardware:EarlyFragmentTests, because the writes of pixels that later fail the depth test must still happen. So on every host GPU, NVIDIA included, these shaders ran for every pixel of the draw before their depth test.Source.
src/amd/registers/gfx10.jsonin Mesa:DB_SHADER_CONTROLbit 12 isDEPTH_BEFORE_SHADER, andZ_ORDER3 isEARLY_Z_THEN_RE_Z.src/amd/vulkan/radv_shader.c,radv_precompute_registers_hw_fs) setsDEPTH_BEFORE_SHADERfrom the shader'searly_fragment_test.EARLY_Z_THEN_LATE_ZtoEARLY_Z_THEN_RE_Zfor its GFX12 HiZ workaround (radv_cmd_buffer.c). Both run depth before the shader.How.
earlyZis set forDEPTH_BEFORE_SHADER, and forZ_ORDER1 or 3 under the existing no-kill and no-export conditions (Z export, mask export, and the stencil reference exportmainadded since).earlyZkeeps its place in the request, so the serialization version doesn't change.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release. Devices:
GALLIUM_OVERRIDE_CPU_CAPS=avx.The branch is rebased onto
mainat33ae282e4, which added the stencil reference export in the same lines. The resolution is the one @blawesom proposed:main's!stencilRefExportEnablejoins theZ_ORDERconditions, and the backend keeps only its alpha-to-coverage guard. Everything below ran on that base.earlyFragmentTestsTestsinagc_driver_graphics:DB_SHADER_CONTROLvalues: Z_ORDER 0 to 3 with and without kill or Z export, andDEPTH_BEFORE_SHADERwith kill, with Z export and with Z_ORDER 0 and 3.EarlyFragmentTestsis declared, on the request itself and after a serialization round trip.DB_SHADER_CONTROL 48 did not declare early fragment tests(48 is Z_ORDER 3).alphaToCoveragefor Z_ORDER 1 (0x10) andDEPTH_BEFORE_SHADER(0x1000), and checks that neither declaresEarlyFragmentTests, directly and after a round trip. With the backend's alpha-to-coverage guard removed it fails:DB_SHADER_CONTROL 16 with alpha-to-coverage declared early fragment tests.stencilRefExportTestsinagc_driver_graphicsalready checks thatZ_ORDER1 with a stencil reference export stays late. It now also checks thatDEPTH_BEFORE_SHADERwith a stencil reference export decodes as early. With the stencil export made to blockDEPTH_BEFORE_SHADERas well, it fails:DEPTH_BEFORE_SHADER with a stencil reference export did not get early tests.agc_driver_graphics, andagc_driver_alpha_to_coverage, pass on all three devices.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 348 tests):agc_driver_stencil_ref_exportis skipped, as driver 591.86 has noVK_EXT_shader_stencil_export. I left out the BDA tests that lose the device on driver 591.86 (#3291):agc_driver_flat_loads*,agc_driver_global_loads*,agc_driver_flat_apertures*,agc_driver_pixel_interlock.agc_driver_stencil_ref_export, exceptagc_driver_ds_swizzleandagc_driver_f32_dot2_rounding, which fail the same way onmain.agc_driver_stencil_ref_exportsegfaults within 0.4 s in some runs: 2 of 5 runs withmain's code and 2 of 5 with this branch, passing otherwise.Limits.
TechnicalDebt.mdrecords that the bit follows Mesa, and that its combination with a Z export, a mask export, a stencil export or alpha-to-coverage was not measured.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T01:37:42Z.
Three small, well-scoped perf/correctness fixes from the same author — reviewed individually since one of them is a real correctness bug, not just an optimization.
#4212 (LDS atomic double-fence): Confirmed exactly as described.
AtomicUpdateTyped's compare-and-swap loop already ends its LDS path with a workgroup-scoped acquire-release barrier, andSharedAtomicUpdateemitted a byte-for-byte identical second one right after — same scope, same semantics, same state, truly redundant. The GDS path keeps its second barrier correctly, because GDS's loop barrier is device-scoped, not workgroup-scoped, so there it isn't a duplicate. The LDS/GDS split is driven by the same enum the loop itself already branches on, not a separate heuristic that could misclassify. One cosmetic nit: the body's instruction list omitsEmitSharedAtomicCmpstF32, which also routes through this same function and gets the same fix.#4211 (bindless table double-search): Also confirmed clean. The key question was whether the two searches are provably identical, not just usually identical — and they are: the runtime image-mode variants are copies of the base image that only ever mutate format/numeric-class/dimension/cube-type fields, never the four fields the search actually reads (
indirectRoot,source,indirectSearchIterations,indirectMappingOffset). Since those are untouched across every mode, the second search could never have produced a different slot. Minor nuance worth noting, not a defect: the search is now also evaluated on two arms (FMASK, and the depth-bits-fail case) that previously skipped it entirely — practically inert for non-bindless images, but worth a mention in case a future bindless+FMASK combination surfaces a different failure-message path than before.#4210 (early depth decode) — this one is a genuine correctness bug, not a perf nit, and the fix is right. The pixel-stage decoder's precheck already accepted the
DEPTH_BEFORE_SHADERbit but never actually read it, andZ_ORDER==3(EARLY_Z_THEN_RE_Z) was never treated as early depth at all — onlyZ_ORDER==1was. WithoutEarlyFragmentTestsdeclared, a Vulkan driver can't discard an occluded pixel before a shader that writes memory, so on every host GPU those shaders were running — and writing memory — for pixels the real hardware would never have reached. That's silent output corruption, not just wasted work. The fix is correct and the alpha-to-coverage exception is properly preserved. One gap worth flagging: the fix deliberately letsDEPTH_BEFORE_SHADERset early-Z even when combined with a Z-export or mask-export, which the accompanying TechnicalDebt entry only partially covers — it notes the bit-plus-Z-export combination as unmeasured but doesn't mention the mask-export case. Also, the new test can't actually exercise the alpha-to-coverage clause end-to-end, since that flag is only ever set by the driver, not by anything the decode test constructs — the "still tests late" claim rests on reading the code, not on a test proving it.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T02:31:12Z.
Thanks @Dextroid17. Both points are fair, and both are fixed in b4860abf3, which is also rebased onto current
main:TechnicalDebt.mdentry now also namesDEPTH_BEFORE_SHADERwith a mask export as unmeasured.earlyFragmentTestsTestsnow sets the request'salphaToCoveragefor Z_ORDER 1 and forDEPTH_BEFORE_SHADER, and checks that neither declaresEarlyFragmentTests, directly and after a serialization round trip. With the backend's alpha-to-coverage guard removed it fails:DB_SHADER_CONTROL 16 with alpha-to-coverage declared early fragment tests. It passes on the RTX 4060, the Intel UHD and lavapipe 25.2.8.I've updated the description to match.
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-12T12:26:05Z.
Rebase handoff — this branch was ~335 commits behind
mainand conflicting. I rebased it onto currentmainand verified the result; your commit's authorship is unchanged.The conflicts were semantic —
mainlanded the stencil-reference export work in the same regions this PR touches. The rebased merge keeps both:ShaderInputState.cpp: main'sstencilRefExportEnabledecode plus this PR'sdepthBeforeShaderdecode; theearlyZcomputation now takes the PR'sdepthBeforeShader ||trigger and relaxedZ_ORDERcheck with main's!stencilRefExportEnableblocker added (the PR's guard set didn't know about stencil export yet)SpirvModuleSetup.cpp: main'sStencilExportEXTcapability block is kept; theEarlyFragmentTestsemission uses the PR's simplified guard, since the blockers now live in the decoder-sideearlyZ(including the stencil one above)TechnicalDebt.md: both entries kept (main'sSTENCIL_TEST_VAL_EXPORT_ENABLE, this PR'sDEPTH_BEFORE_SHADER)Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release,
BUILD_TESTING=ON, AMD RX 9070 XT / RADV):tools/check_conventions.py --base origin/mainpassesTo pick it up:
Head is
923ed60ae. Happy to adjust the resolution if you'd merge the guard sets differently.Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T14:12:28Z.
Thanks @blawesom for the rebase and the RX 9070 XT run. Your resolution is right:
main's!stencilRefExportEnablebelongs with the other blockers inearlyZ, and the backend keeps only the alpha-to-coverage guard.I pushed an equivalent rebase onto
mainat33ae282e4(04f97e12a) rather than fast-forwarding to yours, becausemainhad moved again. It adds two things:Z_ORDERearly tests and among the unmeasuredDEPTH_BEFORE_SHADERcombinations.stencilRefExportTestschecks thatDEPTH_BEFORE_SHADERwith a stencil reference export still decodes as early. Making the stencil export block it as well fails that check.I retested on the RTX 4060, Intel UHD and lavapipe 25.2.8; the description has the results.
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-12T17:15:30Z.
Independent verification of your rebase — the new head builds and passes the full local suite on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release,
BUILD_TESTING=ON, AMD RX 9070 XT / RADV): 952/952, conventions green. From my side this is merge-ready.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T17:46:10Z.
Both points read as closed on this head. Read at
04f97e12a(body and diff) — read, not rebuilt here.docs/dev/TechnicalDebt.md:580says the unmeasured combination is "a Z export, a mask export, a stencil export or alpha-to-coverage together". The gap I flagged was that it said Z-export only, which is what made the fix's scope read narrower than it is.alphaToCoverageon the request itself forZ_ORDER1 (0x10) and forDEPTH_BEFORE_SHADER(0x1000), checks that neither declaresEarlyFragmentTestsdirectly and after a serialization round trip, and the run with the guard removed fails with the message you quote. That was the half I said rested on reading the code rather than on a test.stencilRefExportTestsaddition —DEPTH_BEFORE_SHADERwith a stencil reference export decodes as early — matches the rebase resolution @blawesom described, and it is the pairing that makes the conservative conditions readable.Nothing further from me on this one. @blawesom's 952/952 on the rebased head is the independent run; the three device results in the body are the author's and I have not repeated them.
Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T13:30:52Z.
Checked on current
main: the only conflict was the TechnicalDebt entry, since the page was split intodocs/dev/debt/. I moved theDEPTH_BEFORE_SHADERline todocs/dev/debt/libSceAgcDriver.mdin a separate commit on your branch; the code is unchanged.agc_driver_graphics(the newearlyFragmentTestsTests) fails with the code change reverted and passes with it.agc_driver_tessellation, which segfaults inside RADV on the PR and onmainalike.Merging once the full Build is green.