perf(agc): mirror reused vertex and index snapshots in device memory #16760
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16760"
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-07T15:48:55Z.
What
Summary. A vertex or index snapshot that a draw reuses and binds is copied once into device-local memory, and later draws bind that copy instead of the host snapshot, so vertex fetch stops reading host memory over PCIe. On an RTX 2080 Ti on Sonic Origins' S3K menu page, the 178 shadow-caster draws per present that have no pixel program drop from 2.6-3.1 ms to under about 0.5 ms, the
drawclass of[gputime]from 19.5-21.0 ms to 15.7-17.7 ms per present, and the traced GPU busy time per present from 40.9-41.0 ms to 33.5-37.1 ms. FPS does not change, because the frame is limited by the CPU. The mirrors count in the existing 1 GiB draw-input budget, with a 512 MiB cap on mirrors inside it, andAPS5_NO_INPUT_MIRRORS=1turns them off.Recorder::ReusableDrawSnapshottakes a newmirroredflag, whichCopyDrawInputsets unless the caller keeps the copy on the host (hostOnly). With it, a kept vertex or index snapshot found unchanged gets a device-local mirror at that first reuse:input-mirror[gputime]class;Memory:
Buffer::WithinDeviceLocalHeadroom, the check staging shadows already use (#14815: the memory type the constructor picks, the rounded size, the last eighth of the device-local budget left free), before anything is dropped or evicted; a refused allocation leaves every entry in place;DrawInputMirrorBudget(512 MiB): a new mirror first drops the mirrors of the least recently used entries, whose host snapshots stay, so a budget filled by cold mirrors still serves hot buffers;DrawInputBudget, 1 GiB) next to its host snapshot, so the pool's least-recently-used eviction trims both together; making a mirror evicts older entries, never its own, to stay within the budget;VK_EXT_memory_budgetthere is no heap check, and a mirror is allocated likemain's other device-local buffers.Index copies of mesh draws stay on the host: a mesh draw never binds its index copy, since the mesh shader reads its indices from guest memory through the descriptor
setMeshIndexBufferwrites, and the mesh restart table reads the copy on the CPU.BindsIndexCopy(state)(false for every mesh draw) decides it. Every kept vertex and index copy now hasVK_BUFFER_USAGE_TRANSFER_SRC_BIT, since it is the source of its mirror. The other lookups, storage snapshots, zero-padded vertex copies (SnapshotUse::PaddedVertex) and the owned untracked copies of #13793 (ReusableOwnedDrawInput), are unchanged and never mirrored.Recording the copy ends a render pass a draw left open. On S3K this happens about 0.2 to 0.5 times per present, because a snapshot is mirrored once and then reused.
Equivalence: the mirror is a byte copy of the host snapshot the draw would have bound. On
maina kept vertex or index snapshot is rewritten in place only where #9875 fills a kept vertex window that a later draw reads below; that fill drops the snapshot's mirror (its pool and mirror bytes), and the same reuse mirrors the filled buffer. Otherwise only storage snapshots are refreshed, and a store drops a vertex or index snapshot together with its mirror, so the bound bytes are the same for every input.Tests (
tests/Recorder.cpp):agc_draw_input_mirrorsandagc_draw_input_mirrors_off(APS5_NO_INPUT_MIRRORS=1) run the same cases; each prints which mode it ran in, and the ctest requires that line;BindsIndexCopyis false for a mesh draw; a mirrored vertex window that a later draw reads below gets a new mirror whose bytes, read back from the GPU, hold the filled range (vertexWindowMirrorTests, also inagc_driver_recorder);drawInputReuseTestschecks the reused copies' bytes in both modes, and runs inagc_driver_recordertoo;a draw input mirror is not counted in the draw input budget), the device-local check removed (a draw input was mirrored on a device whose memory is all device-local), the switch ignored (a draw input was mirrored with APS5_NO_INPUT_MIRRORS set), the old refusal at a full mirror budget (a full mirror budget did not drop the least recently used mirror for a new one), the old headroom check on the first device-local type with the unrounded size (a mirror whose allocation the device-local headroom refuses was made), mesh draws allowed a mirror (a mesh draw's index copy may be mirrored although the mesh shader reads its indices from guest memory), and the fill-in's mirror drop removed (filling in a mirrored vertex window kept its old mirror).Related:
mainas955f51545f) copies only the vertex records a draw fetches and fills a kept vertex window in place when a later draw reads below it. This pull request's merge ofmaindrops the snapshot's mirror in that fill, putshostOnlyandmirroredafter #9875'sfromandlowestparameters, and keeps the copies of #9875's window tests on the host, since those tests read the snapshot on the CPU.shadowfield toDrawSnapshotnext to this pull request'smirror. Storage snapshots are never mirrored, so the two only meet in the struct and its initializer.tests/cmake/agc_driver_recorder_tests.cmake.Tested
Windows 11 (10.0.26300), NVIDIA GeForce RTX 2080 Ti (driver 617.14, no resizable BAR), i9-9900K, 32 GB; Sonic Origins (PPSA05324, 02.002.000) on the S3K page of its menu, whose 3D background is the scene, at 3840x2160. The head is
1572e37e27on a merge ofmain17fec48224, andmainbelow is17fec48224.agc_draw_input_mirrorsandagc_draw_input_mirrors_offpass, and each case listed above fails on its deliberate mistake.cteston the head: 5001 of 5001 pass. Three tests whose executable shows a Windows "Entry Point Not Found" dialog were left out of the run (guest_kernel_sys_threads_main,guest_kernel_sys_threads_main_patched,net_ipv6only_patched).main8b96d9879d(head367908bfbb). On it: the focused tests (agc_driver_recorder,agc_draw_snapshot_cap,agc_draw_input_mirrors,agc_draw_input_mirrors_off,agc_draw_inputs_in_place,agc_draw_inputs_copied,agc_draw_input_reuse) pass; the validation set below plusagc_draw_input_reusepasses 77 of 77, with noVUID-error, no missing-layer line, and the same 25 warnings; the fullctestpasses 5490 of 5493 with the same three left out. The three failures come frommainand not from this pull request:prx_thread_local_destructorsandtest_thread_local_destructorsreport thestatic thread_localshared_ptrinResourceMaterializer::ResolveImageTablesShared(#18827;core/shader/recompileris identical tomain), andagc_driver_upload_cache_read_only_flushdoes not build with MinGW (setenvis not declared inUploadCacheReadOnlyFlush.cpp). The measurements below are on the earlier base.VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1andctest -V, on the draw, vertex, index, mesh and snapshot tests (agc_driver_draw_*,agc_driver_vertex_*,agc_driver_rect_list_*,agc_driver_mesh_*,agc_driver_graphics,agc_driver_zero_stride_vertex_fetch,agc_draw_inputs_*,agc_draw_snapshot_cap,agc_driver_recorder, plus the two new tests on the head): 70 of 70 pass onmainand 72 of 72 on the head. Neither log has aVUID-error or awas not found but was requested by env var VK_INSTANCE_LAYERSline, and both have the same 25 warnings (24Undefined-Value-ShaderOutputNotConsumed-DynamicRendering, 1Undefined-Value-ShaderOutputNotConsumed).[gputime](APS5_PROFILE_GPU=1 APS5_PROFILE_GPU_DRAWS=1, warm shader caches, boots alternatingmainand this pull request, means per present over the steady 10 s blocks on the page):drawclassstorage-uploadinput-mirrormainmainmain(2.6-3.1 ms per present); with this pull request it drops out of the programs each block lists in both boots (the last listed costs about 80 ms per block, 0.5 ms per present).storage-upload,copy,shadow-publish) are 10-16% slower than in boots 1 and 4, so 1 against 4 and 3 against 2 are the comparable pairs.APS5_TRACE_SECONDS=200:15and no otherAPS5_TRACE_*orAPS5_PROFILE_*switch, warm shader caches, two rounds alternatingmainand this pull request in one session, standard output not redirected. A first set of these rounds ran beside the same outside GPU load and is not shown; its secondmaincapture lost its GPU timing.main17fec482241572e37e27main17fec482241572e37e27main956775078dagainst571e9d704a,APS5_SAMPLE_WORKER,APS5_SAMPLE_COMMITTER), the draw committer was incommitDraw77.3% and 78.6% of the time, and the queue worker waited inDrawPipeline::Enqueue17.8% and 18.9% of the time. The frame is limited by the CPU here, so a GPU saving does not reach it;worker_busy_pct(97%) counts the worker's wait inEnqueueas busy.trace_dropped), as every S3K capture at 200:15 does; the summary lines are still complete.Summary lines, in run order:
tools/trace_digest.py, run 1, main
seconds 200:15 from present 4488: 205 presents, frame p50 73.0 / p90 79.4 / p99 86.0 / max 91.5 ms, GPU busy 41.0 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
17fec48224, 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 40.9, Graphics.Draw 30.5, Graphics.Draw.draw_and_resource_release 23.0, Driver.Packet 18.0, Driver.Packet.draw 14.0, Driver.Draw 13.6, VideoOut.Flip 12.2, Driver.Present 12.0, VideoOut.Flip.present 11.9, Recorder.WaitSerial 11.8
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
tools/trace_digest.py, run 2, this pull request
seconds 200:15 from present 4438: 209 presents, frame p50 71.5 / p90 75.8 / p99 80.0 / max 88.1 ms, GPU busy 33.5 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
1572e37e27, 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 33.5, Graphics.Draw 29.8, Graphics.Draw.draw_and_resource_release 23.3, Driver.Packet 17.5, Driver.Packet.draw 13.5, Driver.Draw 13.2, VideoOut.Flip 10.2, Driver.Present 10.0, VideoOut.Flip.present 10.0, Recorder.WaitSerial 9.9
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
tools/trace_digest.py, run 3, main
seconds 200:15 from present 4408: 208 presents, frame p50 71.8 / p90 76.3 / p99 80.6 / max 81.2 ms, GPU busy 40.9 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
17fec48224, 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 40.9, Graphics.Draw 29.5, Graphics.Draw.draw_and_resource_release 22.9, Driver.Packet 17.3, Driver.Packet.draw 13.4, Driver.Draw 13.0, VideoOut.Flip 12.5, Driver.Present 12.4, VideoOut.Flip.present 12.3, Recorder.WaitSerial 12.3
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
tools/trace_digest.py, run 4, this pull request
seconds 200:15 from present 4443: 205 presents, frame p50 72.7 / p90 77.4 / p99 80.8 / max 82.9 ms, GPU busy 37.1 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
1572e37e27, 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 36.8, Graphics.Draw 30.3, Graphics.Draw.draw_and_resource_release 23.3, Driver.Packet 17.8, Driver.Packet.draw 13.7, Driver.Draw 13.4, VideoOut.Flip 12.1, Driver.Present 12.0, VideoOut.Flip.present 11.9, Recorder.WaitSerial 11.9
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
The two traces of each round are attached: m15-review-s3k-round1-traces.zip (runs 1 and 2) and m15-review-s3k-round2-traces.zip (runs 3 and 4).
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 gabichuuuuu on 2026-11-07T16:50:22Z.
Review: COMMENTED
Read against the base
72a1308d27. The mirror itself looks correct: no CPU reader touches a mirrored buffer, sinceHighestDrawIndexandKeepDrawInputskip reused copies andMeshRestartTableasks forcpuReads. Every removal goes througheraseDrawSnapshot, which also subtracts the mirror's bytes. Three smaller points:restartTableis false, soCopyDrawInputgetscpuReads = false(Draw.cpp:1763–1766). On the first reuse,mirrorDrawInputrecords the copy, closes the open pass and charges both budgets. ButrecordDrawCommandsImplreturns in thestate.stages.meshbranch beforevkCmdBindIndexBuffer, and the mesh shader reads its indices from guest memory through the descriptor thatsetMeshIndexBufferwrites. PassingrestartTable || state.stages.meshascpuReadsfor the index copy would keep those snapshots on the host.mirrorDrawInputreturns{}oncedrawInputMirrorBytes + bytes > DrawInputMirrorBudget(Recorder.cpp:3286). A mirror is only dropped when its snapshot is erased: through pool pressure, a guest store orKeepDrawSnapshot. So after 512 MiB of mirrors that are never reused again, while the whole pool stays under 1 GiB, no hot vertex buffer gets a mirror again. Dropping the mirrors of least-recent entries first (mirror.reset()and the byte counts) looks safe, because a draw in the open batch keeps its buffers throughKeep(mirror)andkeepRecordedDraw.Buffer::WithinDeviceLocalHeadroom(Resources.cpp:58) already checks the memory type the constructor will really pick, with the rounded allocation size. Here the check uses the firstDEVICE_LOCALtype with the unrounded size, and runs before the eviction loop. So entries can be evicted even when the allocation then fails.Read only; not built or run here.
AI-assisted: yes.