fix(agc): replay stencil reference exports of indirect draws #24572
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/24572"
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 frankea on 2026-11-20T20:01:27Z.
What
On a device without
VK_EXT_shader_stencil_export(NVIDIA: the RTX 4090 on driver 621.14 does not list it), a pixel shader that exports a stencil value is emulated by the stencil replay: one pass per possible reference value, split per instance and per triangle so primitive order is kept. Before this PRprepareDrawrefused every indirect draw on that path ("stencil replay supports direct triangle lists, and triangle strips whose triangles share one face state and have no flat parameters, without restart, mesh or tessellation"), because the per-triangle split needs the vertex and instance counts when the draw is recorded.This PR replays indirect draws (DRAW_INDIRECT 0x24, DRAW_INDEX_INDIRECT 0x25 and their multi forms 0x2c and 0x38):
prepareDraw: an indirect draw that replays stencil always reads its records, and its record count, on the CPU. A newIndirectDrawPath::StencilReplayvalue ("stencil replay" on the[draws]line) uses the existing CPU path:FinishLeaseWritesand the flush hook wait for the work that writes the records, an indirect count is read and clamped to the packet's maximum, and a draw whose records all resolve to nothing is skipped. That check now resolves each record withResolveIndirectRecord(below), so an indexed draw whose records all start at or past the bound index count is skipped too; before, it only looked for a zero vertex or instance count and such a draw went on to record an empty draw (4a3fd665ca). This applies to every indirect draw read on the CPU, not only to the replay.prepareDrawmarks a written native multisample depth surface (NoteDepthSurfaceWrite) after the indirect record checks instead of right after resolving resources, so an indirect draw skipped as empty, or a replay refused for a record past 2^32, no longer leaves the surface marked host-written without recording anything (c91d6d1c7e). A record whose first instance plus instance count, or (non-indexed) first vertex plus vertex count, passes 2^32 throws with the direct draws' "stencil replay draw indices overflow", since the per-instance and per-triangle split would wrap. The refusal drops only the "direct" condition and now reads "stencil replay supports triangle lists, and triangle strips whose triangles share one face state and have no flat parameters, without restart, mesh or tessellation". Its other conditions stay (restart, fans, rect and quad lists, mesh, tessellation, and the strips that #24379'sStencilReplayStripdoes not accept). Triangle strips are no longer in this list: main replays direct strips since #24379, and this PR extends that to indirect strips.ResolveIndirectRecord: the driver's per-record rules, taken out of the CPU branch ofrecordDrawCommandsImplso that branch and the replay share them. An InPlace dimension takes the record's value and a Constant one takes the constant. Records with no vertices or instances are skipped. An indexed record starting at or past the bound index count (INDEX_BUFFER_SIZE) is skipped, and a longer one is clamped to it.recordStencilReplaytakes theIndirectRecord. It turns every non-empty record into a direct draw in record order, then splits each one per instance and per triangle as it already does for direct draws. For a triangle strip each record is its own strip: a record has count - 2 triangles, a non-indexed piece starts at the record's first vertex plus the triangle number, an indexed piece at the record's first index plus the triangle number (with the record's base vertex), and the front face flip of odd triangles (#24379) counts from the record's first triangle. The CP processes a multi-draw's records in order, and a record's instances and primitives in order, so a later record's stencil write wins wherever two records overlap. Draws that replay stencil are already never memoized.Since #24802 (merged into this branch with main in
84716a43df),recordStencilReplayreplays an unbounded export whose faces compare ALWAYS and replace on pass as one pass that clears the written bits, one pass per written bit and one pass that writes colour and depth, where the device has graphics pipeline libraries with dynamic colour write masks (or the draw writes no colour). Indirect records go through the same pass list, so each of their triangles records those passes instead of 256.The replay pipelines and the reference filtering are unchanged: DB_SHADER_CONTROL bit 1 (STENCIL_TEST_VAL_EXPORT_ENABLE) or bit 2 (STENCIL_OP_VAL_EXPORT_ENABLE, taken when
StencilOperationValueReplacesholds, value in bits 15:8 of the second MRTZ channel) selects the export. Z_ORDER (bits 5:4) must still give late tests, and KILL_ENABLE (bit 6) kills before the filter, as before.Debt: a new Functional entry records that these draws wait for the GPU work that writes their records and inherit the CPU record path's limits, and that an unbounded export costs 256 draws per triangle per instance, or at most 10 where #24802's one-pass-per-bit replay applies. The pending
fix-stencil-replay-multisamplefragment drops "Indirect draws" from the replay entry's list of refusals. That entry was already over 1000 characters, so the Conventions check warns about its length. The change here only shortens it.Tested
Unit test
tests/execution/StencilReplay.cpp: the row that expected indirect draws to be refused is replaced by:1c5a400f7d). Each kept column gets its own value and killed columns keep the old stencil.Paired runs. On main, the stencil replay test is main's code with this PR's test file.
2ec2742512d41b281004agc_driver_stencil_replay,_no_gpl,_unsuppresseddraw_offset_indices*,empty_indirect_skip,indirect_draw_pending_producer*including_gpu_indirect_disabled,list_restart*,pipelined_indirect_draw,queued_indirect_read_order,rect_list_indirect,settled_indirect_read,sparse_indirect_indices*)agc_driver_stencil_replay_tests(plain,APS5_NO_GPL=1, and withAPS5_NO_STATE_SUPPRESSION=1)stencil_ref_export_unusedsettled_indirect_readfails ("1 feature gap, 3 records settled"),queued_indirect_read_orderdoes not finish (vkWaitForFences returns VK_TIMEOUT) andstencil_ref_export_unusedfails in vkCreateGraphicsPipelines (-3); plain main with its own test filesqueued_indirect_read_orderreaches the 600 s cap; the rest passThe table above is for the earlier head
d41b281004. At1c5a400f7d(mainef7dc4452dmerged, which brings #24802), this PR's head only:1c5a400f7dagc_driver_stencil_replay,_no_gpl,_unsuppressedAPS5_NO_GPL=1indirect_draw_pending_producer(plain and--gpu-indirect-disabled),empty_indirect_skip,draw_offset_indices(plain and poisoned),list_restart(plain and compacted),pipelined_indirect_draw,rect_list_indirect,sparse_indirect_indices(5 forms),settled_indirect_read,queued_indirect_read_orderagc_driver_stencil_replay_testsplain,APS5_NO_GPL=1,APS5_NO_GPL=1 APS5_NO_STATE_SUPPRESSION=1queued_indirect_read_order(15)settled_indirect_readfails as it does on main on the MacThe tables above predate
c91d6d1c7eand4a3fd665ca(the two checks described under What); the commits after those add tests only (4ab4513049,f1752cf5ae,1ff3adfbbc,72fa74750e). #24735, which is stacked on this PR, carries this PR's code up to4a3fd665ca. Its paired RTX 4090 run atbf3390787e(maine0ef70639don the other side, driver 621.14, VK_LAYER_KHRONOS_validation 1.4.363.0 inserted in every test, 0 VUIDs) passedagc_driver_stencil_replay,_no_gpl,_unsuppressedand the 16 indirect draw runs listed above in each of 3 runs. Each test added after that was checked against a temporary driver change that it must catch, as described in https://github.com/boykopovar/AnyPS5/pull/24572#issuecomment-7251956452.Other testers: B-Howell ran this PR's head
72fa74750emerged onto main63ee1c120a(https://github.com/boykopovar/AnyPS5/pull/24572#issuecomment-7270694252) on Windows 11 Pro (build 26300), Ryzen 7 7800X3D, RTX 4080 SUPER, driver 621.14, WinLibs GCC 15.2.0 r7, Release, with the LunarG validation layer 1.4.363.1 inserted in every test:agc_driver_stencil_replay,_no_gpland_unsuppressedpassed 3 of 3 in each of 3 runs, none skipped, 0 VUIDs (main with its own test file: 3 of 3 pass, 0 VUIDs). Their control, main'sDraw.cpp/Draw.hppwith this PR's test file, fails all three only on the refusal-text check (main words that refusal differently), so on its own it does not show the new indirect cases failing on main. They did not run the 19 indirect draw tests, AMD, Linux or any title. At the first headb95dbc2c1d: haadeej084 (https://github.com/boykopovar/AnyPS5/pull/24572#issuecomment-7051854558) ran the three stencil replay forms on an RTX 5090, driver 610.88, Windows 11 (build 26200), merged onto main223b1645d2: all three pass, 0 VUIDs with VK_LAYER_KHRONOS_validation 1.4.357, and all three fail on main. Jirka135 (https://github.com/boykopovar/AnyPS5/pull/24572#issuecomment-7055694824) ran the three replay forms,agc_driver_stencil_ref_exportand the 19 indirect draw tests on an RTX 5090 (driver 617.42) and an AMD Radeon iGPU (1002:13c0, driver 32.0.21045.29003), Windows 11, merged onto main038cdd173b, with the validation layer 1.4.369 inserted and 0 VUIDs: the iGPU passed 23 of 23 in 3 of 3 runs; the RTX 5090 passed in 2 of 3 runs, and in one runagc_driver_indirect_draw_pending_producer_draw_pipelinefailed once ("the draw went through the gpu-side path 0 times"). Their 20 further runs of the fourindirect_draw_pending_producer*tests passed 20 of 20 on both main and this PR, and those tests passed in every run above and in #24735's runs.Title: Onimusha: Way of the Sword (PPSA27836 01.000.001), Windows, RTX 4090. Its first story load draws an oversized fullscreen triangle through a non-indexed DRAW_INDIRECT. The pixel shader reads depth from a 2D image at the pixel position, kills where it is zero, and exports depth plus an unbounded stencil operation value (DB_SHADER_CONTROL 0x45, 256 possible values). The target is single-sample D32_SFLOAT_S8_UINT with stencil ALWAYS/REPLACE and write mask 0xff, and there are no colour targets. On main
ee764ca7fdplus #24325 (now merged), the title stops on the indirect refusal about 865 s in. With this PR plus #24325 (one run, 30 min cap, made before the overflow refusal was added; this draw's single record passes that check): the title gets past the indirect refusal. The draw logs the once-per-process "replays all 256 stencil reference values" line, the 10 s window around it shows 2 draws averaging 12.7 s each (the 256 replay pipelines are built on first use), and the next window shows 239 draws. The run then stops on the next replay refusal, "stencil replay refuses depth or stencil feedback reads" (about 1,520 s in, and about 2 min after the load was started with CROSS). No indirect path count "stencil replay" was printed. The likeliest reading is that this refusal comes from the same draw: its pixel shader samples the depth plane of the D32S8 target it writes, andprepareDrawchecks for feedback after the 256-value log line and before the indirect read is counted. That is not proven. Replaying that draw needs a feedback-safe replay, for example a copy of the sampled plane taken before the first pass. That belongs in a separate PR.Checklist
main; no other open PR implements the same functions (#24379, merged, replays direct triangle strips; this PR builds on it for indirect strips. #10351 routes stencil values through a colour attachment, but its selector still picks the replay for this shader, so it reaches the same indirect refusal.)NotImplemented_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)Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T20:29:22Z.
Review: COMMENTED
Reviewed — no blocking findings
Runtime behavior was not independently verified, so a formal approval is not justified.
What I ran
Reviewed head
b95dbc2c1de8fd95da2ce63b100c696e37494079. Read the prepared description, commits, diff, manifest, and all five changed files throughrepo_read.py. No commands or tests were run.Results
Findings
Blocking
None identified in the reviewed code.
Non-blocking
None.
Sources
core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp:2399resolves CPU-read indirect records using the same rules as the existing CPU draw path.core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp:2811replays resolved indirect spans in record order, then splits them by instance and triangle.core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp:4288routes stencil replay through CPU record reads and checks replay index ranges for overflow.core/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cpp:370adds coverage for ordering, indexed bounds, count reads, overflow, empty records, and unbounded exports.Not verified
No tests, prechecks, or hardware runs were performed. The reported Windows and macOS test runs and the Onimusha result in
description.mdare author-reported. No files were excluded from review.To get this to a good state
Nothing is required. Optional: independently run the reported test suites and confirm behavior on the listed hardware.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T05:25:06Z.
Review: APPROVED
Reviewed — looks good
Nothing in the inspected change blocks merging; the added indirect replay path follows the existing CPU record rules and has focused coverage for record ordering, count limits, indexed bounds, overflow, and empty records.
What I ran
Reviewed head
801905e59ceb31746b31f541b3bc82c5f2b7ab09usingrepo_read.pyagainst the prepared diff. No commands or tests were run. I read all five changed files listed as reviewed inmanifest.json:Draw.hpp,Draw.cpp,StencilReplay.cpp,fix-stencil-replay-indirect-draws.md, andfix-stencil-replay-multisample.md.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Tests were not run during this review. Hardware behavior and the author-reported Windows and macOS runs were not independently verified.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T06:23:51Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the inspected implementation and supplied test reports support merging.
What I ran
Reviewed head
d41b281004ce2d1b890132b2c6a326e1bbaabb96usingrepo_read.pyto inspect the prepared diff and targeted source. No tests were run by me; the hardware runs in the description are author-reported.Results
Findings
Blocking
None.
Non-blocking
None.
Sources
Reviewed changed lines in
Draw.hpp,Draw.cpp,StencilReplay.cpp, and both changed debt fragments. Full text was available for each changed file;files.jsonwas not available as a review-folder document, so its exclusions were not verified. Targeted source context inspectedResolveIndirectRecordand indirect replay call sites.Not verified
I did not independently run tests or verify author-reported hardware results, title behavior, the complete repository context, or excluded-file contents.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T04:25:05Z.
Review: APPROVED
Reviewed — looks good
The change at head
1c5a400f7db32a248ba3a9eec4dc1b1a1e022305extends stencil replay to indirect records; the inspected implementation follows the CPU record path and splits records into ordered spans.What I ran
No commands or tests were run in this review. I read the prepared diff and targeted implementation ranges through
repo_read.py.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
I read
Draw.hpp,Draw.cpp,StencilReplay.cpp, and both changed debt fragments in the prepared diff. All five changed files are marked included inmanifest.json; the diff contains their changes. I read targetedDraw.cppranges for record resolution, replay span construction, and preparation; I did not read the full changed files or the full test additions. I did not run tests or independently confirm the author-reported Windows and macOS runs, hardware behavior, or validation results.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-27T06:08:52Z.
Review: COMMENTED
Actionable comments posted: 1
a116339e-476f-4fa6-8691-11a2746d6ab2Reviewing files that changed from the base of the PR and between
7de0fe1c18and51baf1aa96.core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cppdocs/dev/debt/changes/fix-stencil-replay-indirect-draws.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T06:11:13Z.
Review: APPROVED
Reviewed — looks good
The inspected changes consistently route stencil replay indirect records through the CPU read path and preserve record order; I did not run tests in this session.
What I ran
Reviewed head
51baf1aa968cd107c7450dde69845f3e87080265withrepo_read.pytools. No merged preview was run. No commands or tests were run in this read-only review.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Builds, automated tests, validation-layer output, and console behavior were not verified in this session. The description reports paired Windows RTX 4090 and macOS Apple M5 Max test runs; those are author-reported results. All changed files listed in the manifest were included and read:
Draw.hpp,Draw.cpp,StencilReplay.cpp, and the two stencil replay debt fragments. No changed files were excluded.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T06:52:15Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the CPU-read indirect-record path and replay preserve record order and apply the resolved draw parameters consistently.
What I ran
4a3fd665ca1f089a37eea10982b6a6c429bd8248usingrepo_read.py.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No tests or builds were run independently. Hardware behavior and the author-reported test runs were not independently verified. All changed files listed in the manifest were read:
Draw.hpp,Draw.cpp,StencilReplay.cpp,fix-stencil-replay-indirect-draws.md, andfix-stencil-replay-multisample.md. No changed files were excluded by the manifest.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T05:09:27Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the inspected code supports the documented indirect stencil replay behavior, and the reported tests are author-supplied.
What I ran
Reviewed head
43c4a875fc1419958622b485e0dec855047ac224. I readDraw.hpp,Draw.cpp,StencilReplay.cpp, both changed debt fragments,description.md,commits.md,manifest.json, anddiff.patchthroughrepo_read.py. I did not run tests or other commands.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
I did not independently run the reported Windows or macOS tests, verify console behavior, or inspect CI results. All changed files listed as reviewed in the manifest were read; no changed files were excluded.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-29T05:17:56Z.
Review: COMMENTED
Actionable comments posted: 2
3e868669-0938-4df3-b7da-fb133002c835Reviewing files that changed from the base of the PR and between
4a3fd665caand43c4a875fc.core/libs/prx/libSceAgcDriver/Graphics/include/Draw.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T06:51:31Z.
Review: APPROVED
Reviewed — looks good
The change supports stencil replay for indirect draws at
72fa74750ea176568c96802501a6150f1b98bff5; the inspected code and tests support merging, though I did not run the checks myself.What I ran
Read the prepared diff and all four changed files at the pinned head. I did not run tests or builds. The author reports paired hardware runs in the PR description; those are not runs performed for this review.
Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No tests or build were run in this review. Console behavior and the author-reported test results were not independently verified. The four changed files listed in the manifest were read:
Draw.hpp,Draw.cpp,StencilReplay.cpp, and the two debt change fragments. No changed files were excluded.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-03T06:03:53Z.
Review: APPROVED
I read the diff at
72fa74750e. Windows 11, GCC 15.2, merged intomainatcf556db18c3together with other open PRs (so not an isolated build), withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1andVK_LAYER_KHRONOS_validationon an RTX 5080 (621.24) and a Radeon iGPU (26.11.2).agc_driver_stencil_replay,_no_gpland_unsuppressed: pass on both GPUs. The loader log shows the layer inserted and no "was not found" line, and neither run has aVUID-orSYNC-HAZARDline.main's): all three fail on both GPUs with "unsafe primitive routing was not refused".Not checked: a title.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T21:43:17Z.
Review: COMMENTED
I traced the shared indirect argument resolution through the per-record, instance and triangle replay order. The regressions cover nonzero first indices, index count clamping, alternating culled strips, and a later record winning on overlap. Please retain the command trace assertions along with the pixel checks, since a pixel result alone can miss a per-instance ordering change. The paired stencil fixture is queued on lavapipe.
AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.