perf(agc): cache a draw key the second time it is seen #5216
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5216"
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 oneandonlydean on 2026-10-13T14:53:42Z.
What
A draw whose key has no entry in the draw cache captures its stages, builds a variant per stage (a copy of every captured region) and inserts a draw entry. A key that never comes back pays for the variants and the entry for nothing, and its entry pushes a live one out of the 4096-entry cache. Draws whose user data changes every frame are such keys: in a level of PPSA21564 almost no lookup finds an entry (152,112 lookups, 90 hits per 10 s), and
cacheDrawStages(the variant copies and the insert) is 4.3% of the queue-0 worker's samples, before the frees of the evicted entries.The driver now remembers the keys it has seen once (
DrawKeySightings): 16,384 keys in four-way buckets, four times the cache's size. A key gets its entry at its second sight. A key that already has an entry still takes new variants at once. A full bucket replaces its oldest key, so keys that share a bucket don't push each other out unless more than four keys of that bucket come between two sights of one of them.The cost is one more full draw for a key that does repeat: its third draw is the first hit, not its second. What a draw records is unchanged; only when an entry is made changes.
This is the idea of #3985, which was closed. Its table was direct-mapped, so two recurring keys in one slot evicted each other and were never cached; the buckets here are the fix discussed there. It was closed for no frame-time gain in a menu where few keys repeat; the scenes below where keys don't repeat do gain.
Tested
Linux (CachyOS, kernel 7.2, GCC 16.2, RTX 4090, NVIDIA 610.57.04), Release,
BUILD_TESTING=ON.agc_driver_draw_cache_reuseandagc_driver_draw_cache_reuse_offnow draw three times and require 3 lookups, 2 without an entry and 1 insert. With only that change to the test onmainboth fail:the three draws made 3 draw-cache lookups (1 without an entry), expected 3 lookups with 2 absent. The same test runs a one-bucket table: two keys drawn in turn are both admitted at their second sight, and a full bucket replaces its oldest key.ctest -R "agc|shader": 477 of 477 on this branch (main075169f7b3).python3 tools/check_conventions.py --base origin/mainpasses.PPSA21564,
main2a2a3dc650against the same commit with this change, same build options, warm shader and pipeline caches (warm-up runs not counted), runs alternated on an otherwise idle machine. Title and menu are frames per second over fixed windows. The levels advance a fixed step per frame below 30 fps, so they are milliseconds per present over the same level frame indices.mainAPS5_PROFILE_DRAW=1)In that later level, with the profile on, key, lookup and validate fall from 4.2 to 1.6 us per draw packet.
Summary lines of
APS5_TRACE_SECONDS=90:20in the same later level, withoutAPS5_PROFILE_DRAW:mainfirst, this change second (p50 400.5 to 372.2 ms per present). The builds were made from exported trees, socommitreadsunknown. Both traces filled the per-thread event buffer (trace_dropped); the frame times come from the presents and are complete.APS5_LOOP_GUARDandAPS5_DUMP_FRAMESare set by my test harness on both sides; the other three variables it sets are not read bymain. The trace files are not attached (the PR was opened from the command line); I can add them.Not tested: Windows, AMD.
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 mrfelek on 2026-10-13T19:57:54Z.
Data point from another title: Diablo II: Resurrected (PPSA03912), Linux, RTX 3080 Ti, in game. The draw cache hits about 0.1% there (46087 lookups per 10 s, 43 validated), the same pattern as described here. I ran this PR together with #5407 and #4947 (not isolated); no abort, and the window-title FPS went from about 2.4 to about 3.6 on average, noisy because the run swaps. Details on #5407.
AI-assisted; the runs are from my machine.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T20:11:40Z.
Holding this for review and a few more runs before it merges. What would help:
mainagainstmain, same scene, a few alternating runs each: frame time and the[draw]cache line (lookups, hits, validated). Runs combined with other PRs can't show what this one does.main(frame dumps or screenshots at the same point). A wrong hit would draw with stale state, so a picture that differs matters more than the fps.Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-13T20:53:44Z.
A/B for this PR alone, as requested above. Diablo II: Resurrected (PPSA03912, 01.039.000), Linux, RTX 3080 Ti (NVIDIA 615.71.09), RAM-limited machine (31 GB, the run swaps),
APS5_HOST_IMPORT_MIB=16384,APS5_THREAD_PRIORITY=high.dacd78525plus three PRs this title needs to reach its game (#1186, #5205, the later-slice follow-up on #5003). B: A plus this PR and nothing else. OnlylibSceAgcDriver.prxdiffers between the two builds; the same runtime and settings, alternating runs A1 B1 A2 B2 A3 B3.Mean 36.5 on A and 39.3 on B, about 8% more presents, and the two sets do not overlap. Within a run the windows are steady (within one present). The GPU busy time is the same, and the CPU idle gap per frame goes from about 120 ms to about 100 ms, so the gain is small. The hit rate stays at about 0.2% (the title's user data changes on almost every draw), so this PR saves the insert and eviction work and nothing more there.
One thing to weigh: the title segfaulted while loading in 2 of the 5 launches with this PR (B1 and B2, both fine on retry) and 0 of 3 without it. Both crashes were a lookup in a hash table with a null table pointer inside the game's own code (same function, offsets 0x1b9d787 and 0x1b9d7a9 in the relinked executable), which looks like a race between game threads at load and not the driver, and the title has crashed at load before on builds without this PR. Five launches is too few to rule this PR out, so I would not read more into it than that.
AI-assisted; the runs are from my machine and I can attach the
[draw-cache]and[present]lines of each run.Imported from boykopovar/AnyPS5. Originally posted by bcortezf on 2026-10-13T21:27:31Z.
Isolated A/B of this PR against
main, same machine and scene, alternating runs, warm caches. PPSA30528 (Red Dead Redemption), gameplay after the intro, Windows 11, RTX 4070 SUPER (driver 616.64), Ryzen 7 7800X3D, 3840x2160,APS5_TRACE_SECONDS=100:5.About +10%. No aborts. On top of #494 + #1193 + #1202 it is neutral: 6.33 / 5.72 / 7.64 FPS without it, 6.28 / 6.07 / 7.67 with it. It merges with #494 with three adjacent-line conflicts (
Driver.hpp,DrawCache.cpp,DrawCapture.cpp): bothinsertDrawEntrysignatures kept, andadmitDrawKey(drawKey)added to thecacheDrawStagescondition. I compared frame times only, no screenshot diff.