perf(agc): keep the highest index of a large in-place index buffer while it is unchanged #4221
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4221"
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-12T01:43:22Z.
What
An indexed draw that binds its index buffer in place from a host import (#2253) scans the bound range for its highest index on every draw. The copied path does not: a reused snapshot carries the value it was scanned with. A draw of many indices therefore pays for the scan each time, and an indirect draw, which binds the whole declared index buffer, scans the whole buffer per draw.
The recorder now keeps the highest index of an in-place index range of 4096 indices or more, together with the write generation collected before the scan. A draw takes the kept value while the range is unchanged since (
GuestMemory::UnchangedSinceand the allocation registry generation, the proof a reused draw snapshot uses). A guest write to the range, another address, another size, another index width or another restart rule scans again. Where the imported range is not write-watched (CollectWritesreturns 0: Windows direct imports, and Linux drivers whose imports are compared), nothing is kept and every draw scans as before.A range of fewer than 4096 indices is scanned at every draw, as on
main: no write collect, no lookup. #2253 tried keeping the value for every in-place range and left it out, because in a title with small index ranges the write collect and lookup cost more than the scan (Hades II, 37.5 against 38.4 fps). The size cut is where the two costs cross, measured below; both sides of it give the same highest index, so it decides only which is paid.The scan runs on
mainfor any index range whose allocation is host-imported, so this does not need #290. #290 imports more (a larger budget), which is where one title we test with started to pay for the scans.The cut
Measured on an i9-14900KF, Linux, Release.
drawInputInPlaceTestsfor this, not part of the commit): 0.36 ns per index for 16-bit and for 32-bit indices, with or without the restart rule. 1024 indices take 0.37 us, 4096 take 1.45 us.There they cross between 2,000 and 3,000 indices. The 32-bit ranges of that title all hold 8,000 indices or more.
The cut is 4096 indices, the next power of two above the highest crossing measured. Below it a draw does what it does on
main. In the benchmark loop the call that makes the choice adds 2 to 6 ns to a scan below the cut (16 indices: 5.7 ns onmain's scan, 10.5 ns through the new function; 2048 indices: 725.5 against 727.7 ns); from 4096 indices up a kept value takes 0.32 us where the scan takes 1.45 us or more.A range that changes between its draws is scanned and kept again each time, which costs the collect, the lookup and the insert (0.3 to 1.8 us for the insert in the title) on top of the scan; at 4096 indices that is about as much again as the scan, and less above. In the title 99.8% of the draws above the cut found their value.
Tested
Linux (CachyOS, GCC 16.2.1, Release,
BUILD_TESTING=ON,ANYPS5_ENABLE_SPIRV_TOOLS=ON), RTX 4090, NVIDIA 610.57.04. The branch is one commit,1dc342dd, onmain43bc17f7.agc_draw_inputs_in_place(agc_driver_recorder_tests --draw-inputs-in-place-only) runsdrawInputInPlaceTests, whichctestdid not run before (only the binary's default path called it), with new checks. For a 16-bit range of exactly 4096 indices: the first use scans it, the second takes the kept value, the other restart rule and another size scan again, a guest write to the range gives the new highest index, and a range of restart indices alone keeps "no index". A range two bytes shorter is scanned at both of its uses. The same for 32-bit indices: 4095 indices are scanned twice, 4096 are kept. It passes, 10 of 10 repeats. WithRecorder::KnownHighestIndexmade to find nothing it fails withan unchanged in-place index range was scanned again; with theUnchangedSincecheck removed, withan in-place index range kept its highest index over a guest write; with the cut lowered to one index, withan in-place index range below the kept size was kept.agc_driver_recorder_testsbinary (default path, not inctest): 5 of 5 runs pass onmain43bc17f7and on the branch.ctest -R "agc|shader":main43bc17f7424 tests, branch 425. On both the same 2 fail (agc_vector_window,agc_driver_prepared_shaders_registered_workgroup_sync), in a parallel run and again when run alone.ctestwas not run there on this head.python3 tools/check_conventions.py --base 43bc17f7passes.APS5_PROFILE_DRAW=1, over the same 280 level frames. Both builds aremain0a8b1d32with #425, #454, #2490, #2491, #3702, #3874 and #290, which the title needs to reach the level; the first is without this commit. Other builds and tests ran on the machine during all three runs, and the two runs without the commit differ by 19%, so the frame times bound the gain loosely; the vertex phase is the part this changes:An earlier form that kept every range, on
maina11c8f7fwith the same PRs in a quieter hour, one run each: 433.1 ms without, 345.3 and 351.4 ms with.Not tested:
mainplus the size compare; a frame rate was not measured.ctest, on this head (onlyagc|shader), andmainafter43bc17f7.main(it stops before the levels) and on this branch without the other PRs.mainhas the same exposure from its scan to the GPU's read.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T01:55:02Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/CMakeLists.txtL821, L823core/libs/prx/libSceAgcDriver/CMakeLists.txtL823Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T02:42:53Z.
Data point from Sword of the Sea (PPSA25344), main menu: no measurable change in either direction.
Setup: Windows 11, RTX 5070 Laptop, WinLibs GCC 15.2. Both trees are
maina11c8f7f7plus our local patches for this title; B adds #4221, with a tests-only cherry-pick conflict and the library code unchanged. Env on both:APS5_WRITE_WATCH_IMPORTS=watch, so the in-place imports are write-watched. Rows ran 180 s each, alternating, on the same shader cache, with no device loss.DRAW_INDEX_INDIRECTper 10 sThe two
mainrows differ from each other as much as from the #4221 rows. This menu issues only ~4 indirect indexed draws per frame (~116 per 10 s at ~2.9 FPS), so the index scan isn't a visible cost here; itsq0_indirecttime is dominated by dispatches and uploads the change doesn't touch. That's neither a regression nor evidence against the change, just a title that doesn't exercise a large in-place index buffer on a hot path.AI-assisted: yes (Claude Code)