fix(agc): run an indirect buffer a GPU write already landed in as memory holds it #22401
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22401"
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 milescoviello on 2026-11-17T14:12:23Z.
What
A queue-0 submission copies the INDIRECT_BUFFERs it calls when it is submitted. When GPU work writes one of those buffers after the submit,
Driver::refreshIndirectSegmentre-reads it as execution reaches it (#11346). It re-read only while that GPU write was still pending: in the draw pipeline, the draw finisher or an unreaped recorder batch. If the write had landed and its batch had been reaped by the time the queue reached the buffer, nothing counted as pending, and the submit-time copy ran.MXGP 26 (PPSA30839) hits this. Its frame command buffer (about 6740 dwords) is written by GPU work after the submission that calls it. When the stale copy runs, a whole frame of command chunks is skipped, with their releases to graphics' timeline label. The next frame waits for one of those values (
WAIT_REG_MEM_64 ... function 5) on graphics and on compute queue 0x20, nothing writes it again, and Unreal's 120 s RenderThread watchdog ends the title. Traced on a diagnostic build, the timeline values in queue 0's submission copies jump by about 198 per frame, and in a stalled run the last value graphics executed equalled memory, 0xc0 below the awaited value.Each indirect segment now records the write tracker generation at its copy, and whether a recorded GPU write overlapped it then (
Recorder::SnapshotWriteOverlaps, or the draw pipeline's queued writes). At execution the buffer is re-read when a write is pending, as before, and also:GuestMemory::DriverStoredOver, a new query over the tracker's driver pieces only, so guest CPU stores do not count. LikeStoredOver, it answers yes when the tracker cannot tell: write watching off, a zero generation, or a range outside the write watch. The buffer is then re-read and compared.The generation and the pending-write check are taken before the buffer is copied (
3a5461d3fd), so a driver store marked while the copy is made is still newer than the segment's generation. One window remains: a store that changes the buffer between the copy and the check that the copy matches memory leaves the segment untracked, and it runs as copied, as onmain. The debt entry records it.A buffer the CPU rewrites after its submission still runs as it was copied where the write watch covers it (
CpuRewriteAfterSubmit). Outside the write watch the driver cannot tell that rewrite from a GPU write, so it runs what memory holds, as the console's CP does when it reaches the buffer.Tested
11-20,
3a5461d3fd(the five earlier commits rebased unchanged ontomainfc973e0189, plus the generation-before-copy commit for MarcZX10's ordering point): Linux, RTX 4070 Ti, the 38 tests matching^agc_driver_gpu_written_indirect_buffer|^agc_write_tracking|^agc_driver_submit|^agc_driver_pm4|^agc_driver$pass in each of 3 runs;mainpasses the same 37 (the_unwatchedvariant is this PR's). There is no deterministic test for the ordering window itself.Windows 11 VM, NVIDIA GeForce GT 1030 (582.78), WinLibs GCC 16.2.0, Release, the same test set: 45 of 46 pass in each of 3 runs at
3a5461d3fd. The one failure,agc_driver_pm4_order_signal, fails the same way onmainf343ff4a51on that machine (3 of 3).Linux (Ubuntu 26.04.1, kernel 7.0), RTX 4070 Ti, NVIDIA 615.71, GCC 15.2, Release,
-DBUILD_TESTING=ON,ANYPS5_REQUIRE_VULKAN=1:agc_driver_gpu_written_indirect_buffergains three cases:LandedWriteBeforeRun, for an INDIRECT_BUFFER and a COND_INDIRECT_BUFFER: a dispatch writes the command buffer, the queue waits on a gate, the test waits until the write has landed (FlushGpuWrites), then opens the gate. The buffer must run what the dispatch wrote.WriteBeforeCopyLandedBeforeRun: the dispatch is recorded in an earlier submission and the queue blocks on its gate before the submission calling the buffer is copied. The write then lands, and the buffer must run what it wrote.agc_driver_gpu_written_indirect_bufferandagc_driver_gpu_written_indirect_buffer_queued_writer.main's sources:LandedWriteBeforeRunfails witha COND_INDIRECT_BUFFER whose command buffer a dispatch before it wrote, with the write landed before the queue reached it, ran the buffer as it was at submission: the marker is 0; run alone,WriteBeforeCopyLandedBeforeRunfails the same way.WriteBeforeCopyLandedBeforeRunstill fails, so the flag is needed.agc_write_trackinggainsCheckDriverStoredOver: a driver store over the range since the generation counts, one beside it or older does not, a collected CPU store does not, and a zero generation or a range outside the write watch answers yes.agc_driver_gpu_written_indirect_buffer_unwatchedruns the indirect-buffer cases withAPS5_NO_WRITE_WATCH=1.DriverStoredOveranswering no when it cannot tell), the unwatched variant fails (a COND_INDIRECT_BUFFER the CPU rewrote after its submission ran as it was copied outside the write watch) andagc_write_trackingfails (a zero generation reads as not stored over by the driver).LandedWriteBeforeRunfailed 3 of 3 there (the marker is 0), because the test's command buffer is outside the write watch on Windows and the check answered no. With the follow-ups, all four tests pass 3 of 3.agc_driver_(gpu_written|indirect|submit|pm4|rewind|cond|control_flow|ring|dcb|rendering_wait|flip)pass,CpuRewriteAfterSubmitincluded.MXGP 26 (PPSA30839), hold-Square route (MyNacon welcome, then Cross through the menus), 480 s, 20 GB cap,
APS5_NET_DISCONNECTED=1, with #22052 merged (without it MXGP stops at 13-24 s on the rect-list error):maine602d6a587+ #22052 + #22187): 6 of 14 runs stalled at 177-335 s on graphics' timeline label as described above. Two more ended on the RenderThread watchdog with no GPU wait stuck, one on an unrelated null dereference; 5 ran to 480 s, 2 of them withAPS5_LABEL_SUBMIT_NOW=1.0x1079760020, 6740 dwords;0x1079848010, 6721 dwords), each time with a driver store over it since the copy, which this PR's check catches. One run ended in a race (the controls tutorial with its HUD); it is upside down because #20827 is not in that build.main01c550960a+ #22052 (bb8d5cbf2d) against the same plus this branch (a1e661ebb8, the tree of aa67dfcb91), 4 interleaved runs each:main+ #22052Contact sheets (last three dumped frames of each run): release indirect-buffer-ab-01c550960a. The in-race geometry is stretched in places; that is a separate issue for a later PR.
3f87785b70merged ontomainf15df3256c(Windows 11, WinLibs GCC 15.2.0): the 14agc_driver_gpu_written_indirect_bufferandagc_write_trackingtests pass, 3 runs, on an RTX 5080 (NVIDIA 610.88) and on a Ryzen 7 9800X3D iGPU (AMD 2026.04.17).^agc_|^shaderon the RTX 5080 has only the two failuresmainalso has on that card (#22427, #22931).3f87785b70merged ontomain26604382a5as071febd8d1(Linux 7.3.4, GCC 16.2.1, Mesa 26.2.6, RX 7900 XT and the Ryzen 7 7800X3D iGPU, Khronos validation layer 1.4.363.1): no new failures againstmainand noVUID-orSYNC-HAZARDlines.guest_kernel_sys_paths_readlinkat_limitsfailed once in a parallel full run on the 7900 XT and passes 3/3 solo on bothmainand this PR.main7c836568c(11-19, details): with the same route,mainends at 247 s on Unreal's RenderThread watchdog in a 900 s run, whilemain+ this PR runs the full 903 s and reaches the race.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)main7c836568cit ends on the RenderThread watchdog first, see the 11-19 A/B under Tested)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T14:26:37Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge:
DriverStoredOverfails open when the tracker is unwatched, the generation is 0 or the range is not covered, so the re-read is skipped in exactly the cases the sibling check treats as stored over.What I ran
aa67dfcb91e64f3b59bd1ec4ed4954077ef83607, basemain.description.md,commits.md,manifest.jsonanddiff.patchfrom the review folder.searchfornoteIndirectSegment,SnapshotWriteOverlaps,TrackerGeneration,driverPieces,bool covers(,Queue0(),bool Busy(.catofSubmission.cpplines 225-345,GuestMemory.cpplines 1526-1534, 1790-1826, 2936-3006.Results
DriverStoredOverfalsewhereStoredOverreturnstrueFindings
Blocking
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp:2963:DriverStoredOverreturnsfalsewhen!tracker.watched,generation == 0or!tracker.covers(...). In those cases the answer is unknown, butrefreshIndirectSegmentthen skips the re-read (core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp:314), and the stale submit-time copy runs. That is the bug this PR fixes. The siblingStoredOverreturnstruein the same guard (GuestMemory.cpp:2952). A re-read that finds nothing changed costs one compare, so failing closed is cheap.Non-blocking
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp:314:copiedAtandcopiedUnderWriteare read from a local copy of the segment, so a segment copied under a write re-reads and compares its whole buffer on every execution, even when nothing changed. For a frame buffer of about 6740 dwords run every frame, that is a steady cost. This is not a correctness issue. Whether the stored entry is updated after a refresh is not verified (lines past 345 were not read).core/libs/prx/libSceAgcDriver/tests/execution/GpuWrittenIndirectBuffer.cpp:454: the new cases cover a write that landed before the run and a copy taken while a write was recorded. None overwrites a copied buffer with a driver store after the copy, so theDriverStoredOverbranch is not exercised by them (from reading the test code).Sources
Not verified
GuestMemory.cpp:2994; tracker initialisation inLifecycle.cppnot read).GuestMemory.cpp:1528), which decides whether the blocking point is reachable.noteIndirectSegmentruns during the submit-time copy, andDriverStoredOvertakes the tracker lock. Not checked.Recorder::SnapshotWriteOverlaps(Recorder.cpp:2030) not read.FlushGpuWrites,AgcDriverAwaitedWaits_nid_postfixandAwaitedWaitnot checked.Submission.cppandGuestMemory.cppat the ranges above.Submission.hpp,GuestMemory.hppandGpuWrittenIndirectBuffer.cppwere read as diff hunks only.files.jsonwas not read, so the excluded files are not listed.To get this to a good state
DriverStoredOver, returntruewhen the tracker is unwatched, the generation is 0 or the range is not covered, asStoredOverdoes.agc_driver_gpu_written_indirect_bufferandagc_driver_gpu_written_indirect_buffer_queued_writeron the new head, and post the MXGP A/B results.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T16:20:04Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
7022c3118d66d1bb7f3dbb36f3cbb582d94af68c, basemain.diff.patchfor all seven files infiles.jsonunderreviewed.repo_read.py:refreshIndirectSegment,DriverStoredOverandStoredOver,IndirectSegment, and the callers of the helpers the diff uses.Results
SnapshotWriteOverlaps,Overlaps,TrackerGenerationfoundFindings
Blocking
None.
Non-blocking
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp:314: every indirect segment with no pending write now callsGuestMemory::DriverStoredOver, which takes the tracker lock. With write watching off it answers yes, so each run re-reads the whole buffer (up to about 6740 dwords in the reported case) and compares it. The result is correct, but the cost is paid on every submission of such a buffer. Consider a comment or a counter so the cost stays visible.Not verified
noteIndirectSegment(Submission.cpp:37-38) against the copy's read of the words. If a driver store lands between that read andTrackerGeneration(), its generation is not newer thancopiedAtand the store would be missed. I did not read the copy routine to settle this.Recorder::SnapshotWriteOverlapsandDrawPipeline::Queue0().Busy()were not read beyond their signatures.files/were not opened; the review usesdiff.patchand targeted head reads.To get this to a good state
Nothing is required.
Optional: state the cost of the unwatched path in the description or a comment, and confirm the copy-time generation ordering in the copy routine.
Sources
github.com/boykopovar/AnyPS5@7022c3118d/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp (L303-L320)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T18:21:16Z.
Review: COMMENTED
fix(agc): run an indirect buffer a GPU write already landed in as memory holds it — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
manifest.json,files.jsonanddiff.patchin the run folder.refreshIndirectSegment(core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp:295-340),StoredOverand the newDriverStoredOver(core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp:2940-2970), and the helpers the diff calls (TrackerGeneration,Recorder::SnapshotWriteOverlaps).Results
TrackerGeneration,Recorder::SnapshotWriteOverlapsandDriverStoredOverfound at headcopiedUnderWriteandDriverStoredOverchecksFindings
Blocking
None.
Non-blocking
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp:314: with write watching off, or for a range outside the write watch,DriverStoredOveranswers yes. Every execution of such an indirect buffer then reads it and compares it with its copy. This is the cost the debt note records; it is a performance trade-off, not a defect.core/libs/prx/libSceAgcDriver/tests/execution/GpuWrittenIndirectBuffer.cpp:242-251: for a CPU rewrite outside the write watch, a buffer now runs what memory holds, where it used to run as copied. The new test and the debt note both state this. Worth a second look by whoever owns the console's CP behaviour, since it changes an existing expectation.Not verified
AgcDriverAwaitedWaits_nid_postfix,Pm4,Dispatchingand theWriteBeforeCopyLandedBeforeRungate logic.tracker.covers,driverPieces,possiblePiecesandentry.whole/entry.dropped/entry.possibleWhole/entry.possibleDroppedindriverStoredOver: I read the hunk, not their definitions.DrawPipeline::Queue0().OverlapsandBusy, andDrawFinisher::PendingOverlaps: used in the new lines ofSubmission.cpp:37and:307-314, not read.agc_driver_gpu_written_indirect_buffer_tests.cmakeis included by the test build. The new file is added, but I did not check theCMakeListsthat loads it.Files
diff.patchhunks and reader spot-checks: all 8 files infiles.jsonreviewed.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T01:47:53Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this; the points below are optional.
What I ran
Reviewed head
3a5461d3fd2c5ea63f979a737706796702644875using the prepared diff andrepo_read.pyfor changed code and targeted tracker context. No commands or tests were run in this review environment; test results below are author reported.Results
mainand altered tracker behaviorFindings
Blocking
None.
Non-blocking
None.
Sources
The review covered
Submission.hpp,GuestMemory.hpp,Submission.cpp,GuestMemory.cpp,WriteTracking.cpp,agc_driver_gpu_written_indirect_buffer_tests.cmake,GpuWrittenIndirectBuffer.cpp, andfix-agc-indirect-buffer-landed-gpu-write.md, all included in the prepared bundle. The core change records the tracker generation and pending-write state before copying an indirect segment, then refreshes when a tracked driver store or recorded/pending GPU write may have changed it. The added tests cover landed writes before execution, writes pending at copy time, and tracker uncertainty. Pinned diffNot verified
I did not independently run the tests, inspect CI output, or validate console behavior. The test and hardware results in the description are author reported. The documented race where a store changes the buffer between copying and the comparison remains acknowledged in the change description.
To get this to a good state
Nothing is required.