fix(agc): wait for address-based writers before reading indirect draw records #17850
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/17850"
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 adam25136 on 2026-11-09T18:05:55Z.
What
The driver reads some indirect draw records and counts on the CPU. These reads now first finish the earlier batches whose address-based builds can store to guest memory, as the CPU read of
DISPATCH_INDIRECTarguments has done since #6786.An address-based store (FLAT/GLOBAL, or through a descriptor the recompiler cannot resolve to a static buffer) is not recorded as a pending write: its batch holds a lease on the registered allocations, and the flush hook does not wait for it. So these reads could see guest memory from before such a store ran:
Driver::finishDraw(Execution/src/Driver/Draw/Draw.cpp), for the drawsClassifyIndirectDrawkeeps off the GPU (for example an indexed draw withoutINDEX_BUFFER_SIZE);Driver::draw, where a stale zero count or stale empty records skip the draw;prepareDraw(Graphics/src/Draw.cpp), when the GPU does not read the records.Only leases whose build can store are waited for.
CountLeaseOutcometakes!ShaderResources::BdaReadOnly(), based on thebdaReadOnlyflagWriteBackalready uses to decide whether an address-based build breaks proofs. It comes from a scan of each stage's SPIR-V for stores and atomics throughPhysicalStorageBufferpointers; unreadable SPIR-V counts as storing.Graphics::FinishLeaseWritesfinishes the recorder up to the newest such batch. The two driver sites takeGpuMutexonly whileLeaseWritesMayBePending()reports a storing lease counted since the last such wait, so the queue thread stays off the mutex otherwise.FinishLeaseWorkandFinishLeaseWritesshare their submit-and-finish step (finishLeaseBatches).LeaseStats::writingcounts the leases that can store, shown in the[address-sync]profile line.The debt entry "CPU reads behind an address-based build" is cut to the reads that still do not wait: the non-indexed copy kernel's count record (oneandonlydean showed on #13599 that it is read without the lease wait), the record
recordMeshArgumentsreads while recording, theawaitsTitle/orderReleasedchecks, and the draw reads behind a draw that queue 0's draw pipeline (on by default) has queued but not yet recorded. Such a draw counts its lease only when recorded, andDrawWriteRangesgives the pipeline no range for its address-based stores. That gap is left for a separate change.New test
IndirectDrawPendingProducer.cpp(agc_driver_indirect_draw_pending_producer, its_draw_pipelineand_plan_verifyvariants, and_gpu_indirect_disabledwithAPS5_NO_GPU_INDIRECT_DRAW=1). A one-thread compute dispatch writes the draw record or count withglobal_store_dwordx3, then a draw of a triangle covering the target follows. Every writer case checks that the producer's lease was deferred and counted as able to store, that the store landed, that every pixel was drawn, and that the draw took the expected path once without being skipped as empty. The cases: records the GPU reads; an indexed draw withoutINDEX_BUFFER_SIZE; an indexed count-indirect multi-draw whose count the empty-draw skip reads; a draw behind a dispatch that only loads through an address (global_load_dword), whose lease must not be counted as able to store; and, in_gpu_indirect_disabled, a draw the device's fallback reads. A producer on a compute queue behind a label wait was tried and left out: with the two driver waits reverted it still passed in all three registrations that run it, so it does not reliably show the bug.No title has been seen to read such memory here; the change and the test come from the debt entry.
Overlap:
tools/pr_overlap.py --branch(in a full clone, 1,978 open pull requests) lists four that change lines here:readStartline just before each of the three added calls.It could not compare 75 more that conflict with
main. In GitHub's patch of each, context lines included:finishDraw, which would need the same call;FinishLeaseWork(FinishThroughOpenedwhile an async lane is active); that step is now shared withFinishLeaseWrites, so whichever lands second changes it once, for both;CountLeaseOutcomecall inkeepRecordedDraw, and #10193, #13928 and #13929 (Celegans12) insert one three below, with unchanged lines between.Tested
Windows 10 Pro 22H2 (19045), i9-12900KF, NVIDIA GeForce RTX 5080 (
10de:2c02, driver 621.14), WinLibs GCC 15.2.0, Release,BUILD_TESTING=ON, withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1. Headc95426ff42merged ontomain52c33518f8unless stated.ctest -R '^agc_driver_indirect_draw_pending_producer' -V: all four registrations pass, each log withSelected GPU name=NVIDIA GeForce RTX 5080.libSceAgcDriver.prxstaged next to it was the one just built):_gpu_indirect_disabledfinishDrawprepareDraw_gpu_indirect_disabledwritesforced to truewritesforced to falseThese ran on this change before its rebase onto
52c33518f8; the 27 commits it moved over change nolibSceAgcDriverfile.Execution/src/Driver/Draw/Draw.cpp(the two driver waits) in the merge: 3 of the 4 registrations fail;_gpu_indirect_disabled, which theprepareDrawwait covers, passes. Restored, all 4 pass.agc_driver_pipelined_indirect_draw,agc_driver_indirect_arguments_pending_producer,agc_driver_indexed_copy_gpu_parameters,agc_driver_queued_indirect_read_orderandagc_driver_empty_indirect_skippass.tools/select_tests.pypicks 2,685 tests for the changed files, andselect_tests.py ctest build --parallel 4 --timeout 120on the merge fails 3. Two of them,agc_driver_quad_pixel_mask(#14237) andguest_kernel_sys_paths_symlinkat_patched(a timeout), fail inmain52c33518f8's full suite on this machine too (4,824 tests; it also times outguest_kernel_sys_paths_symlinkat, which is not selected). The third,agc_driver_unregistered_label_on_gpu, passed three reruns. It fails onmaintoo, now and then, with the same message ("the label held 0x00000000 when a later label had landed"): run alternately, 1 of 150 runs onmainand 2 of 150 with this change failed (2 of 579 onmainoverall). From its code, it reaches none of the changed functions. The 13 tests that skip skip onmaintoo.^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu(1,594 on the merge, 1,597 inmain's run), throughctest -V --parallel 4withVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(1.4.367) andVK_LOADER_LAYERS_DISABLE=~implicit~; all pass exceptagc_driver_quad_pixel_maskon both sides. Neither log has aVUID-line orwas not found but was requested by env var VK_INSTANCE_LAYERS. Both have the same 61Undefined-Value-ShaderOutputNotConsumed/OutputNotWrittenwarnings in the same 14 tests, and the four new registrations have none. A rerun of them on this head withVK_LOADER_DEBUG=layershowsInsert instance layer "VK_LAYER_KHRONOS_validation"8 times.-DANYPS5_ENABLE_SPIRV_TOOLS=ON(as the Linux CI job builds): the four registrations pass in three runs, 1.47 to 1.61 s each.python tools/check_test_registration.py buildandpython tools/check_conventions.py --base origin/main(with this title and description) pass.maine37e9fa3f9before opening (head03334ebfd3,c95426ff42with the debt entry's last sentence reworded); none of the 48 commits since52c33518f8toucheslibSceAgcDriveror the debt file, and the four registrations pass there.main84f4ad4f06, with an earlier head of this change whose library code differs only in names, this profile line's wording and the shared helper;APS5_PROFILE_DRAW=1 APS5_TRACE_SUMMARY_EVERY=30 APS5_TRACE_BENCH=1 APS5_LOG_TIMESTAMPS=1, 3 to 3.5 minutes of the same scene each, both sides carrying one local line,sceAjmInitializecomparing only the low 32 bits of its reserved argument as #16974 first proposed (it now drops the check; without the line this title faults in its Render thread). In every run with the change, the[address-sync]line counts 934 to 1,215 deferred leases and none by a build that stores through addresses, so the two driver sites never took the mutex. Frame times differed between runs of the same build by up to 13%, and between the two sides by less in both directions (mean 493.8 ms per present with the change, 499.3 without, after the first 60 s).Not covered: AMD and Intel GPUs, Linux (the test's
mmapallocation path is not run here), a PS5, a title whose address-based stores these waits catch, and the four reads the debt entry keeps open.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)🤖 Generated with Claude Code