perf(agc): watch a vertex decode's queued writes without copying them #22079
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22079"
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 meh7an on 2026-11-17T04:23:23Z.
What
The vertex stage decode on the queue worker (
DecodeOrderedVertexStageInfo, from #7691) reads the vertex attribute and buffer tables from guest memory while earlier draws may still be queued on the draw pipeline's committer, and redoes the decode behind a drain when a queued or just-retired write touches what it read. Its watch cost, on every decode, three things that do not depend on whether anything overlaps:WatchWritescopied every queued item's write ranges into a new vector under the queue mutex;WrittenSincetook the mutex again and compared every read range with every copied write range;On the S3K menu page a profiled run (main
9f284c930e6) showed about 310,000 decodes per 10 s and no decode drain. The watch is now bookkeeping:WatchWritesrecords the number of retired items and the queue length, andWrittenSincelooks at the items that retired since, which the queue still holds.WatchWritestakes the lock only while the pipeline is busy (as before) and copies nothing. It pins the retired list for the length of the watch.WrittenSinceis still one lock. It checks the queued items (as before), then, if more items retired than were queued at the watch, reports a write (as before), else compares the reads with the retired items[watch.retired, retiredNow)only. A range hull of the reads skips writes that cannot overlap any read (exact: a read inside the hull that overlaps a write makes the hull overlap it).DrawPipeline::enqueuereclaims the retired items on the enqueuing thread. It now skips the reclaim while a watch is open (watchers), so a decode never loses the writes of an item that retired during it, also when another producer enqueues in that window (the draw finisher thread). A watch releases its pin whenWrittenSincereturns, or when it is destroyed (a decode that throws). The reclaim runs at the next enqueue, a decode later at most.APS5_NO_DECODE_WATCH_COUNTS=1restores the copy of the queued writes at the watch and the comparison with it (newENVIRONMENT.mdrow). The hull stays, being exact.Same answer as main. The old result was: some queued item overlaps a read, or items retired since the watch and either more retired than were queued at the watch, or a write queued at the watch overlaps a read. The queue is FIFO, so the items queued at the watch are those with sequence
[watch.retired, watch.retired + watch.items). Those that have retired since, when no more thanwatch.itemsdid, are exactly[watch.retired, retiredNow), and the pin keeps them inretiredfor the length of the watch; those that are still queued are covered by the first check. So the two results are equal in every case. The only way to get an answer other than main's is an item already reclaimed at the check, which the pin rules out; the code returns "written" there (one extra drain and redecode, the same final decode).Memory: no new cache. Retired items are released at the first enqueue after a watch closes instead of the first after they retire; a watch lasts one decode.
Overlap: no open PR changes
DecodeOrderedVertexStageInfo,DecodeVertexStageInfo,WatchWritesorWrittenSince. This comes from a diff scan of the open PRs touching the decode, pipeline and draw files (133 candidates among 1,595 open PRs, run on 2026-11-16 and again on 2026-11-17).retiredItemsincrement into a block inDrawPipeline::runto wake a dormant queue 0; the code this change touches is inenqueue,WatchWrites,WrittenSinceandReleaseRetired, not in that block.DecodeVertexStageInfocall inDrawCapture.cpp.a72186d3cfd, 185 commits after the measured base. Of those, only #17786 touchesDrawPipeline.*(FollowEpochandNoteEpochSince, not the code changed here).tools/pr_overlap.pyfailed closed: it cannot compare PRs that conflict with main. It reportedDrawPipeline.cpp,DrawPipeline.hpporDrawDecode.cppin 16 PRs (#1896, #6188, #6235, #7642, #9508, #10193, #13928, #13929, #15136, #16753, #18173, #18226, #19181, #20041, #20315, #22025) andENVIRONMENT.mdalone in 53 more. I compared the 16 by hand: none changes the reclaim inenqueue,ReleaseRetired,Watch,WatchWrites,WrittenSinceorDecodeOrderedVertexStageInfo. They add counters, includes, report lines,DrawFinishermembers, lock spinning, thread names and collect-epoch fields. #9508 addsfullWaitersbesideidleWaitersin the private members, a textual neighbour of this change's two members; #6188 addsOverlapsAnywith the same hull idea (main has it now). TheENVIRONMENT.mdrows share one table.Tested
Windows 11 (10.0.26300), NVIDIA GeForce RTX 2080 Ti (driver 617.14), Sonic Origins (PPSA05324) menu on the S3K page, 3840x2160, default environment. Measured commits: main
2a8a0d1fc338and this change on top of it (4b7d5805d3ba), built in the same build directory.Sampler (
APS5_SAMPLE_WORKERandAPS5_SAMPLE_COMMITTER, one 60 s window per arm on the S3K page, worker, 3,843 samples each):DecodeOrderedVertexStageInfoWatchWritesWrittenSinceDecodeVertexStageInfo(the reads, unchanged)DrawPipeline::Enqueue+DrainDriver::commitDrawThe two functions this changes fall from 71 to 35 samples. In four earlier windows on other mains the same two functions were 57 to 83 samples. The unchanged reads vary by more than the whole difference between the windows, which is why the decode total falls by 18 and not 36. S3K's frame is bound by the worker and the committer together (each waits about 20% for the other), and the saving is about 0.6 ms of a 53 ms frame, so it is not expected to show in the frame.
Traced runs (
APS5_TRACE_SECONDS=200:15, order main, change, change, main; frame p50 per run): main 54.2 and 52.8 ms, change 52.9 and 53.6 ms; GPU busy 27.8 to 27.9 ms per present; queue worker busy 10.4 to 10.5 ms per frame in all four. The spread between runs of the same build is larger than the difference: the frame is flat. Both arms' summary lines are in the digests below, and the four traces are attached as two zips.New tests, in
agc_driver_vertex_table_read_order(andagc_driver_vertex_table_read_order_counts_off, the same binary withAPS5_NO_DECODE_WATCH_COUNTS=1):WrittenSinceagainst the pairwise overlap of the queued writes and the reads, 300 random cases of queued writes and reads, empty reads included;Broken copies of the change, each run against these tests, are all caught (6 of 6): reclaim ignoring open watches, no retired lookup, a watch that never unpins, a hull that never grows, no more-retired-than-queued rule, and a retired window starting at the wrong item. Four fail in the switch-on run only, as they should; the hull and the more-retired rule are shared, so those two fail in both runs.
The 58 focused tests (vertex table, draw pipeline, pipelined, queued read order, draw cache, draw data hit, prepared graphics) pass on this change with
ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(ctest -R ... -V); on main the same 57 pass. Neither log has aVUID-line or "was not found but was requested by env var VK_INSTANCE_LAYERS".tools/check_conventions.py --base upstream/mainandtools/env_reference.py --checkpass. The full ctest suite was not run.Digest: main, run 1 (commit
2a8a0d1fc3)seconds 200:15 from present 5520: 276 presents, frame p50 54.2 / p90 56.3 / p99 58.9 / max 61.6 ms, GPU busy 27.8 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
2a8a0d1fc3, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 27.8, Graphics.Draw 19.2, VideoOut.Flip 13.8, Driver.Present 13.7, VideoOut.Flip.present 13.6, Recorder.WaitSerial 13.5, Driver.Present.render_fence_wait 12.8, Vulkan.Retire 12.8, Vulkan.Retire.wait 12.7, Driver.Packet 10.5
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Digest: main, run 2 (commit
2a8a0d1fc3)seconds 200:15 from present 5578: 283 presents, frame p50 52.8 / p90 54.4 / p99 56.7 / max 58.9 ms, GPU busy 27.8 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
2a8a0d1fc3, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 27.8, Graphics.Draw 18.3, VideoOut.Flip 14.0, Driver.Present 13.8, VideoOut.Flip.present 13.8, Recorder.WaitSerial 13.7, Driver.Present.render_fence_wait 12.9, Vulkan.Retire 12.9, Vulkan.Retire.wait 12.9, Driver.Packet 10.5
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Digest: this PR, run 1 (commit
4b7d5805d3)seconds 200:15 from present 5545: 282 presents, frame p50 52.9 / p90 54.8 / p99 58.3 / max 58.8 ms, GPU busy 27.8 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
4b7d5805d3, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 27.8, Graphics.Draw 18.2, VideoOut.Flip 14.0, Driver.Present 13.9, VideoOut.Flip.present 13.8, Recorder.WaitSerial 13.8, Driver.Present.render_fence_wait 13.0, Vulkan.Retire 13.0, Vulkan.Retire.wait 13.0, Driver.Packet 10.4
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Digest: this PR, run 2 (commit
4b7d5805d3)seconds 200:15 from present 5509: 279 presents, frame p50 53.6 / p90 55.4 / p99 57.6 / max 59.0 ms, GPU busy 27.9 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
4b7d5805d3, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 27.9, Graphics.Draw 18.6, VideoOut.Flip 14.1, Driver.Present 14.0, VideoOut.Flip.present 13.9, Recorder.WaitSerial 13.8, Driver.Present.render_fence_wait 13.1, Vulkan.Retire 13.1, Vulkan.Retire.wait 13.1, Driver.Packet 10.5
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
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)Traces
Main, commit
2a8a0d1fc3(two runs):m343_traces_main_2a8a0d1fc338.zip
This PR, commit
4b7d5805d3(two runs):m343_traces_change_4b7d5805d3ba.zip
Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-17T09:35:14Z.
Review: COMMENTED
Tested on macOS 27.2 on an Apple M4. This is an x86-64 build under Rosetta with Apple clang 21.0.0, on MoltenVK 1.4.2 from the LunarG Vulkan SDK 1.4.363.1.
Method. The base was
mainata72186d3cfplus #22056 and #22074, which fix the macOS build of two other targets; both have since merged. I built the base and this PR merged onto it (a clean merge) in two separate build directories. I ran them alternately, base then PR, three times each, withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationandVK_LOADER_DEBUG=layer:agc_driver_vertex_table_read_orderagc_driver_vertex_table_read_order_counts_offAll runs gave the same results. These tests open no Vulkan device, so no log has a layer insert and there is nothing for validation to check. There were 0
VUID-lines.Not tested: the speed claim. I have no title or capture to run here, so I can't compare
WatchWritesorWrittenSincesample counts on macOS.