Skip to content

feat(agc): draw alpha-tested packed-target merges that write depth as a depth pass and an EQUAL merge pass - #97

Open
Thimo188 wants to merge 36 commits into
mainfrom
upstream-pr/9335
Open

Thimo188 wants to merge 36 commits into
mainfrom
upstream-pr/9335

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#9335, originally opened by frankea.

Source commit: 62f116beb2149c812e23fa6fa1c7363742519427. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

A partial or blended write into a packed color target merges inside the pixel shader through boykopovar#8564's pixel-interlock read-modify-write, which forces early fragment tests. That throws when the shader kills pixels under late tests and the draw writes depth (kills under late tests, draw writes depth or stencil): with early tests the killed fragments would still write depth, so alpha-tested holes would occlude what is drawn behind them. Gran Turismo 7 stops on this in the World Map with an alpha-tested draw into an 11_11_10 target (boykopovar#9020's blend), reverse-Z: DB_DEPTH_CONTROL ZFUNC GEQUAL with depth writes, no stencil, one target.

This branch started as boykopovar#9020 plus its own commits, with merges of boykopovar#9020's branch into it; boykopovar#9020, boykopovar#11557 and boykopovar#8564 are all in main now, so its diff against main is the split alone (13 files). Its commits: 5451af8 (the split), 4768b4a (no split when a program of the draw may write memory), 5198b9c and 829eb0d (the split targets returned from the draw), 91e0e9d (no split when the draw clears depth), 1454d7b (a second, unmerged target in the GPU test), 8eb63a2 (the split mask kept through the label-recapture retry), 8872e28 (the depth-clear check inside LateTestMergeSplitTargets), 65ad383 (the split mask kept through main's recapture retries), dc3f7e7 (the GPU test skips on devices without shaderStorageImageReadWithoutFormat) 704f9bf (no split when the shader exports depth, stencil or a sample mask or runs with a quad sample mask), fa2d107 (no split on a device without the merge pass's capabilities), 99f4f9c (no split when the draw runs the depth-bounds test), 385f83d (no split when the depth compare fails at equal depth), f8a53dc (the debt entries moved into the branch's fragment), 5889dff (both passes drawn at the unsplit draw's render extent), 01d6097 (a GPU test of the occlusion-query refusal with a shader that writes no memory) and 62f116b (both passes prepared before either draws). The latest merge, ad5b083, merges main e0ef706 (the earlier c26a94d merged f281831). The split alone: frankea/AnyPS5@374556c...5451af8, and the memory check: frankea@4768b4ab4

  • The driver draws such a draw twice, patching two context registers and restoring them afterwards. The first pass clears the merged target's 4 bits in CB_TARGET_MASK, so it has no merge: the title's own late tests, kill and depth write run as they are, and depth ends up as on the console. The second pass keeps only those 4 bits and sets DB_DEPTH_CONTROL Z_WRITE_ENABLE to 0 and ZFUNC to EQUAL (2). It writes no depth or stencil, which is the shape feat(agc): merge partial writes into 4_4_4_4 UNORM color targets boykopovar/AnyPS5#8564 already merges with a kill under late tests, so it blends exactly the fragments the kill keeps whose depth equals what the first pass left. Field positions and the CompareFrag encoding are from Mesa's gfx103.json (DB_DEPTH_CONTROL bits 2 and 6:4, CB_TARGET_MASK 4 bits per target, DB_SHADER_CONTROL KILL_ENABLE bit 6 and DEPTH_BEFORE_SHADER bit 12).
  • Driver::draw decides after decoding the state and before capturing any shader (LateTestMergeSplitTargets, from the decoded state and DB_RENDER_CONTROL), returns a new DrawVerdict::Split with the merged target's CB_TARGET_MASK bits in an out parameter, which the draw's label-recapture retry passes on, and PacketExecution then runs both passes through the unchanged Driver::draw, so each goes through the usual decode, capture, pipeline and draw caches under its own register key. EQUAL needs bit-identical depth in both passes: they run the same vertex program with the same viewport and depth bias, and positions are already decorated Invariant. With the target bits it returns the unsplit decode's render extent, which drawLateTestMergeSplit sets on the queue for both passes and clears on restore; DecodeState uses it as the render extent, so both passes get the unsplit draw's scissor and default viewport from the same registers, and it throws a named refusal if a pass's attachments do not cover it. The extent is mixed into the draw register key, so a pass's cached decode never matches an ordinary draw with the same registers.
  • What differs from the console, in the debt entry: where two of the draw's own fragments overlap, only the one whose depth remains after the draw blends, while the console blends each fragment that passes at its own moment, in order. Under a strict compare (LESS, GREATER), a fragment at exactly the stored depth blends in the second pass though it fails on the console. An alpha test leaves mostly opaque fragments, so the first difference is small; the second doesn't arise under GEQUAL or LEQUAL.
  • Still throw, with the existing refusal: a draw that writes stencil (the first pass would change the stencil the second tests), ZFUNC NEVER, DEPTH_BEFORE_SHADER, alpha-to-coverage, multisampled targets, draws while an occlusion query counts samples (the second pass would count them again), draws that clear depth (DB_RENDER_CONTROL.DEPTH_CLEAR_ENABLE), whose first pass stores the clear value the second pass would compare against, and draws that merge into more than one color target (possible since feat(agc): merge a draw into more than one unclamped normalized color target boykopovar/AnyPS5#12916). Nor is a draw split whose shader every packed merge refuses: one that exports depth, stencil or a sample mask (DB_SHADER_CONTROL bits 0, 1 and 8, as DecodePixelStageInfo reads them) or runs with a quad sample mask (PA_SC_AA_MASK). LateTestMergeSplitTargets reads these from the draw's decoded pixel stage info, whose quad masks are already in host sample order. The split prepares both passes before either one draws: with a new QueueState::splitPrepare, finishDraw returns after every stage compile and before the first snapshot or record, so a RefusedDraw under APS5_SKIP_REFUSED_DRAWS or a DrawCompilePending under APS5_ASYNC_DRAW_COMPILE in either pass leaves the packet with no recorded work. The two passes then draw under a thread-local SynchronousDrawCompileScope, so neither can go pending between them, and a split pass is never handed to the draw finisher. A draw is not split when one of its programs may write guest memory: both passes run every stage, so a store, atomic or GDS operation in the vertex or pixel shader would run twice. The check is ProgramWritesMemory on each program and on a user-data tail continuation, the one the draw finisher uses for held writes, which also counts jumps it doesn't follow. Such a draw takes the unsplit path and throws the refusal above.

Tested

Machine Tests (ctest --test-dir build -R <pattern> -V, 3 runs each side) main e0ef706 head 62f116b Validation layer Logs on the box
Windows 11 (10.0.26200), RTX 4090, NVIDIA 621.14, WinLibs GCC 15.2.0, Release (-O3 -DNDEBUG), tree C:\src\wt-14217, build dir build, CPU affinity 0x3FFF The 10 agc_driver_late_test_merge_split entries (plain plus _counting_pixel, _counting_vertex, _counting_query, _depth_clear, _equal_depth, _narrow_merge, _mask_export, _quad_mask, _pending_merge), agc_driver_graphics, agc_driver_packed_float_blend, agc_driver_packed_unorm4444_partial_write, agc_driver_sample_mask_draw, agc_driver_stencil_known_replace. Env: ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer 5/5 passed in each of 3 runs. main does not have the 9 split entries. 15/15 passed in each of 3 runs Khronos layer from Vulkan SDK 1.4.363.0 (C:\VulkanSDK\1.4.363.0\Bin\VkLayer_khronos_validation.json, HKLM ExplicitLayers). Insert instance layer "VK_LAYER_KHRONOS_validation": 6 lines per run on main, 26 per run on head. was not found but was requested by env var VK_INSTANCE_LAYERS: 0 on both. VUID- lines: 0 on main, 0 on head. Lines with Undefined-Value-ShaderOutputNotConsumed-DynamicRendering warnings, counted on the earlier run of 01d6097: 20 per run on main, 35 per run on head; the extra 15 are all in agc_driver_late_test_merge_split C:\src\pr-results\p9335c\ holds run.txt, main-vl-{1,2,3}.log, head-vl-{1,2,3}.log, revert-vl-{1,2,3}.log and the build logs. Chain script: C:\src\pr-results\p9335c.cmd

Both sides used the same GPU and build directory, built one after the other. main runs the 5 shared tests; it has none of the agc_driver_late_test_merge_split entries. Control on the 4090: with Draw.cpp back at its 01d6097 version (no prepare passes), _pending_merge fails in all 3 runs with drew only its depth pass (the RGBA8 target changed, the 11_11_10 merge target did not) on attempt 4, with 0 VUID- lines.

Checklist

Summary by CodeRabbit

  • Bug Fixes
    • Improved rendering of partially written packed-color targets when late depth or stencil tests are involved, preserving expected color and depth results in supported cases.
    • Draws that cannot safely use split rendering continue to be rejected rather than producing potentially incorrect output.
  • Tests
    • Added coverage for depth comparisons, clears, shader masks, multiple render targets, and shader counter side effects.
  • Documentation
    • Documented the supported merge-splitting behavior and its limitations.

… writes depth as a depth pass and an EQUAL merge pass
# Conflicts:
#	docs/dev/debt/shader.md
# Conflicts:
#	core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
# Conflicts:
#	docs/dev/debt/libSceAgcDriver.md
…e memory

Both passes of the split run every stage of the draw, so a store, atomic
or GDS operation in its vertex or pixel shader would run twice. Such a
draw is no longer split; it takes the unsplit path and throws there like
any merge that kills under late tests and writes depth. The check is the
one the finisher uses for held writes: ProgramWritesMemory on each
program and on a user-data tail continuation, which also counts jumps it
does not follow.

agc_driver_late_test_merge_split gets two modes, --counting-pixel and
--counting-vertex: a draw without a merge first shows the shader adds to
a buffer counter, then the split draw with the same shader must throw
the late-test refusal and leave the counter unchanged.
# Conflicts:
#	core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
#	core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
#	core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp
# Conflicts:
#	docs/dev/debt/libSceAgcDriver.md
# Conflicts:
#	core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp
finishDraw's retry after recordQueuedLabelsAfterCapture passed a fresh local to the recursive draw, so a Split it returned would reach drawLateTestMergeSplit without its CB_TARGET_MASK bits. The draw tail now carries the caller's output, as it carries the queue and the rejection string.

The DB depth copy retry draws a queue of its own, which drawLateTestMergeSplit would not redraw. That queue clears DB_SHADER_CONTROL, so it never kills and never splits; the retry now requires that.
…stMergeSplitTargets

With DB_RENDER_CONTROL DEPTH_CLEAR_ENABLE, DecodeState forces the depth write and ZFUNC ALWAYS, so the merge pass's Z_WRITE_ENABLE and EQUAL patch cannot take effect. The draw already skipped the split for it, but at its own call site; the check now sits with the other conditions, which the decode tests reach. A depth-clearing draw keeps the existing refusal before either pass, which the new --depth-clear run of agc_driver_late_test_merge_split_tests checks.
…eadWithoutFormat

The merge pass reads its packed target without a format, so the recompiler refuses its pixel shader on devices without shaderStorageImageReadWithoutFormat (Intel UHD 630 on Mesa). Skip there, as the test already does without pixel interlock.
# Conflicts:
#	core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
A draw that exports depth, stencil or a sample mask, or runs with a quad
sample mask, is refused by every packed target merge. Splitting it drew
the depth pass before the merge pass threw, so the refused draw left its
depth and unmerged targets behind. LateTestMergeSplitTargets now takes
the decoded pixel stage info and keeps such draws whole, so they take the
existing refusal before either pass.
# Conflicts:
#	core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
@github-actions github-actions Bot added unblocks title Names a title ID on the Unblocks line review queue Next in the maintainer review queue: checks pass, no open change request, no conflict labels Dec 2, 2026
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 1 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 2 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 3 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 4 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 5 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 6 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 7 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 8 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 9 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 10 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 11 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 12 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 13 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 14 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 15 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 16 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 17 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 18 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 19 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md
PR 20 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_late_test_merge_split_tests.cmake
core/libs/prx/libSceAgcDriver/tests/execution/LateTestMergeSplit.cpp
docs/dev/debt/changes/late-kill-merge.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review queue Next in the maintainer review queue: checks pass, no open change request, no conflict unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants