fix(agc): drain the draw pipeline before a vertex table read overtakes a queued write #7691
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7691"
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 Celegans12 on 2026-10-19T00:02:21Z.
What
Driver::drawdecodes a vertex stage's fetch tables (the attribute words and the vertex buffer V#s they name) withGuestMemory::ReadinGraphics::DecodeVertexStageInfo. What a draw's shaders read goes throughShaderMemory, whosequeryPendingWritedrains queue 0's draw pipeline when the range overlaps a write that is still queued; the decode reads skip that check. With the default depth of 8, a draw can take its vertex buffer V# from a table before a queued item has stored into it, and then fetch from the old buffer.DecodeOrderedVertexStageInfo(DrawDecode.cpp) decodes, and when one of the decode's reads overlaps a queued write, drains the pipeline (DrainReason::Capture, asqueryPendingWritedoes) and decodes again. Writes the draw finisher still holds count as queued, as inOverlaps.[draw] pipelinedline (APS5_PROFILE_DRAW) counts the drains the decode causes.decodeVertexInfoinDriver::drawuses it at all four call sites.I found this reading the draw path while profiling Ghost of Yōtei (PPSA26344); I haven't seen a title draw wrong because of it.
Related: #1896 checks reads for its pipelined indirect draws (
APS5_PIPELINE_INDIRECT); this covers the default path, on other lines. #7446 compares captured memory before recording, which can't see a write that is still queued.Tested
main0db47b263, configure -DBUILD_TESTING=ON: the touched draw-pipeline, draw-cache and decode tests pass (22/22,--repeat until-fail:3);agc_driver_vertex_table_read_orderalso withAPS5_DRAW_PIPELINE=1.agc_driver_vertex_table_read_order: stores queued into the buffer and attribute tables drain and are seen, also one that finishes during the decode; a store elsewhere stays queued, and a table a finished item wrote, or a store elsewhere that retires mid-decode, drains nothing. It doesn't link onmain; with the drain removed it fails ("the vertex buffer V# was read before the queued store ..."), without the retired-count check too ("the decode kept the vertex buffer of an attribute word that a queued store replaced during the decode").main0a0ed721: the same tests pass (22/22).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 savvaniss on 2026-10-19T07:43:29Z.
Windows build note: this head
695fa3554merges intomain6b8228b2bwithout conflicts, but it no longer compiles there:On
main,DecodeRead(Graphics/include/ShaderInputState.hpp) has aBytes()accessor instead of abytesmember, so line 41'sread.bytes.size()needs updating for the currentmain. Because of that I couldn't runagc_driver_vertex_table_read_orderon the Intel UHD 770 and AMD RX 9070 XT. I'll rerun it once the head builds onmain(WinLibs GCC 15.2.0 r7, Release, Windows 11 Pro 26300).AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-19T22:04:44Z.
Thanks for catching the build break, @savvaniss.
DecodeReadlost itsbytesmember to theBytes()accessor onmain, and @adriadam10 has already rebased the branch and pushed the two-line change (fe5ae13d4) that reads throughBytes()inDrawDecode.cppand in the test, so I left the branch as it is.I built that head merged into current
main(eaa5c9238) and reran the focused tests:agc_driver_vertex_table_read_order,agc_driver_graphics, the draw-cache reuse tests, theagc_driver_pipelined_*tests andagc_driver_vertex_user_data_tailpass, 15/15 on Windows (RTX 3080 Laptop, driver 610.62) and 15/15 on lavapipe in WSL.Imported from boykopovar/AnyPS5. Originally posted by VicRaster on 2026-10-19T22:25:30Z.
Independent review of
fe5ae13d41133c094d7b1e71653e9efd7b9ca9f3: I can reproduce a completed queued attribute-table store escaping the post-decode check inDecodeOrderedVertexStageInfo.The helper decodes before checking Busy/Overlaps. If the earlier writer completes during that decode and is removed from the queue, the helper can return the earlier vertex-buffer selection.
The synthetic test below uses the existing
GuestMemory::SetReadGuardfacility to force that sequence without changing production code:0x1000). An earlier FIFO item is held before storing an attribute that selects buffer 1 (0x7000); its declared write range covers the attribute table.0x1000, although the completed attribute store selects0x7000.The writer is an earlier item in the same FIFO, matching the ordering premise of this PR's test. The read guard only controls scheduling; the queued callback performs the attribute write. The atomic release and queue mutex order the reads and write. No timing sleep establishes the interleaving.
Tested on Windows / Ryzen 9 3900X, WinLibs GCC 15.2.0, Release,
BUILD_TESTING=ON, with a fresh build of the AGC runtime and its dependencies from this head. GitHub's merge-test commitf7603664f2224f1b3c2d585baf11519a047bd55dhad the same complete source tree (b45dbbbf4f07e4af0fcb02d3b025258641594bb1). Runtime lookup was restricted to the fresh build, supported toolchain and Windows system directories.agc_driver_vertex_table_read_order--producer-first0x7000--drain-first0x70000x1000, expected0x7000The drain-first control releases the writer when it observes the default Capture drain request (
0x100), so a path that drains before decoding can complete without relying on the speculative-read gate. The test reports which release path occurred. Source/commit conventions also passed for the added file.This establishes a helper-level ordering failure. I have not reproduced incorrect rendered output or a game regression, and this CPU test does not exercise Vulkan or host imports. The later direct queued path carries materialised stages into
commitDrawwithout rerunning this decoder, but some shaders perform independent runtime descriptor reads, so I am not claiming every draw using these tables is affected.This is an intentionally failing review reproducer, to adapt alongside the final fix. To reproduce, save the file below as
core/libs/prx/libSceAgcDriver/tests/execution/VertexTableReadOrderCompletion.cppon this head. With the supported Windows build environment already set up:The completion test is expected to fail on this head with
a completed earlier attribute store escaped the post-decode overlap check; both controls should pass. The result suggests the ordering proof needs to account for producers that finish during decoding, rather than only those still pending afterward.VertexTableReadOrderCompletion.cpp
AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-20T00:26:09Z.
@embersas, your reproducer was right: the post-decode check only looked at writers that were still queued, so a store that finished between our read and the check got past it. I pushed 837e7d1fa on top of @adriadam10's fe5ae13d4 (no force-push) to close that window.
DecodeOrderedVertexStageInfonow notes whether queue 0 was busy before it decodes. If it was, it still drains when a read overlaps a pending write. Otherwise it reads every recorded attribute word and V# again and keeps the decode only if all the bytes are unchanged. Any difference means a writer finished mid-decode, so it drains and decodes again. A writer that's popped before the overlap check is visible to the reread, because the check takes the same queue mutex the committer pops under. So the window is closed, and unrelated queued work still doesn't cause a drain (the existing "overlaps nothing queued" case still holds).I put your sequence into
agc_driver_vertex_table_read_orderas a fourth case instead of adding a second executable. The read guard releases a queued attribute store at the V# read and waits for its item to retire. I also ran your file unchanged against both heads.Details
Windows 11, RTX 3080 Laptop (driver 610.62), MinGW-w64 GCC 15.2.0, Release, three runs each:
0x1000(release cause 1)0x7000(release cause 1)--producer-first--drain-firstagc_driver_vertex_table_read_orderwith the new caseOn 837e7d1fa merged with
main, the 15 focused tests (agc_driver_vertex_table_read_order,agc_driver_graphics, the draw-cache reuse tests, theagc_driver_pipelined_*tests andagc_driver_vertex_user_data_tail) pass with--repeat until-fail:3: on Windows merged witheb7500680, and on lavapipe in WSL (CI's Linux job) merged with34b1b77a.Imported from boykopovar/AnyPS5. Originally posted by VicRaster on 2026-10-20T02:06:42Z.
I independently rebuilt and retested
fe5ae13d41133c094d7b1e71653e9efd7b9ca9f3and837e7d1faa7a5a323c6db6adec4b8b1d3e9ace43on Windows with the supported WinLibs GCC 15.2.0 r7 toolchain.The unchanged standalone completion-during-decode reproducer fails 3/3 on the old runtime (
0x1000) and passes 3/3 on the fixed runtime (0x7000), with read-gate release cause 1. Both producer-first and Capture-drain-first controls pass 3/3 on both revisions. Your expanded integrated test also fails 3/3 against the old runtime at the new assertion and passes 3/3 on the fixed runtime.I also tested two input semantics and four recorded reads, using explicit gates: changing the first recorded V# during a later attribute read, and changing the second attribute index during its descriptor read. Both return stale results on the old runtime and pass on the fix. An unrelated held queue item remains queued with zero Capture drain requests.
The queue-mutex publication argument checks out for earlier declared writers in the same queue. I found no further production ordering defect within that contract. These are CPU helper tests, not verification of rendered output, a game improvement or performance. I did not independently repeat the broader Windows/Linux GPU test set.
One optional test-hardening point: the integrated fixture captures stack Gate objects by reference, while the Gate destructor releases without waiting for worker completion and the outer error handler does not stop the queue. Shared gate lifetime and explicit failure cleanup would make exceptional paths safer. This is a source-inspection caveat; no cleanup failure occurred in these runs, and it does not invalidate the successful ordering retest.
AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-20T02:55:07Z.
@embersas, thanks for rebuilding both heads yourself and for the two extra gate cases (a changed V# during a later attribute read, a changed index during its descriptor read). Those cover ground our own runs didn't.
You were right about the fixture, so I took the hardening as well: 80057705d only touches the test.
Holdnow returns a shared gate and the queued store keeps its own copy, so nothing in the queue points at a stack object anymore, andGateno longer releases itself in a destructor. On both the success and the failure path the test clears the read guard, releases every gate it created and stops queue 0, so a failing check doesn't leave a held store behind. The four cases and their assertions are unchanged, and the fix inDrawDecode.cppis untouched.With the new fixture, the test still fails 3/3 against fe5ae13d4's
DrawDecode.cppat the completion assertion and passes with 837e7d1fa's. To check the error path, I also added a failing check right after a store is queued as a throwaway local change. The test then exits with 1 in under a second, 3/3, with no hang.Details
Windows 11, RTX 3080 Laptop (driver 610.62), WinLibs GCC 15.2.0, Release, on 80057705d merged with
mainc2b0159d6:agc_driver_vertex_table_read_order,agc_driver_graphics, the draw-cache reuse tests, theagc_driver_pipelined_*tests,agc_driver_vertex_user_data_tail) pass with--repeat until-fail:3, plusagc_driver_vertex_table_read_orderwithuntil-fail:5and three direct runs; test registration check okDrawDecode.cppswapped in: fails 3/3, "the decode kept the vertex buffer of an attribute word that a queued store replaced during the decode"Linux (CI's job on lavapipe in WSL), merged with
mainc942b37c: the same 15 tests pass.Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-21T17:42:55Z.
Review: COMMENTED
The ordering argument is right: the decode reads skip the check that
ShaderMemorymakes throughqueryPendingWrite, so a vertex V# can be taken from a table before a queued store lands. The author says no title has drawn wrong because of it, so this is a precaution on the hottest path, and the cost needs to be shown.Driver.Drawper packet or the summary lines of main and this PR on Ghost of Yotei (PPSA26344, the title it was found on) or any pipelined title, noAPS5_PROFILE_*.DrainReason::Capture) is the right action when it happens. How often does it happen? Add the count of drains caused by the decode to the[draw] pipelinedline so the cost of the correctness is visible in the title.Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-21T21:26:05Z.
Thanks, the retired-item count is the better design, and it holds up in the code. I went through the case I was worried about: a store the decode already read that only lands later. That store's item is either still queued when we check, so the overlap drains, or it retired during the decode, so the count moved and the read overlaps a range noted at the start. Either way the decode is redone. It's in 3000d848c.
Before the decode, the helper takes the retired count and the write ranges queued at that moment, under the pipeline mutex. That copy only happens when the queue is busy, and it's a handful of ranges for depth 8. After the decode, one locked check drains and decodes again when a read overlaps a write that is still queued, or when the count moved and a read overlaps a range from the start. The reread is gone. The
[draw] pipelinedline now ends with "N decode drains".I added the test you asked for: the decode reads a V# that a finished item stored while the pipeline holds an unrelated item, and nothing drains. There's a second new case where a store elsewhere retires mid-decode and nothing drains either. The existing cases still fail without the fix: 3 of 3 with the retired-count branch removed ("the decode kept the vertex buffer of an attribute word that a queued store replaced during the decode"), and 3 of 3 with no check at all ("the vertex buffer V# was read before the queued store ..."). The 15 focused tests pass on Windows and on the Linux merge.
The
Driver.Draw/ summary-line timings on Yōtei, main against this PR, are still owed. I'll post them once I can run that pair.