perf(agc): share snapshot copies and the BDA table of repeated address-based builds #5542
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5542"
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 winstrex on 2026-10-13T23:17:12Z.
What
Address-based builds that repeat the same work could not reuse their BDA table. On The Pathless's main menu every address-based build (about 44 per frame) takes the cached address space and adds two snapshot regions of its own; the snapshots are copied into new buffers on each build, so their device addresses, and with them the table, never repeat.
BdaResourcesthen merged the space's few thousand ranges with the build's own, hashed them, searched the table cache and built a new table buffer every time ([bda-table]0 hits of 3,911 in 10 s), although the same five space and snapshot combinations recur on every frame.Two changes make the table repeat:
ClearSnapshotCopies). A dropped copy stays alive in every build and batch that still holds it.CachedAddressTablenow also serves a build with regions of its own over the cached address space. Its table is a function of the space's ranges (immutable while the space lives), the build's written ranges and its own regions' table entries, so those two are the key (AddressTableKey) of the space's table map, which already held the tables of builds with written ranges.BdaResourcesbinds a cached table's buffer by its serial, as it does for the space's own table.APS5_VERIFY_BDA_TABLES=1rebuilds the table on every hit and throws when the bytes differ.Two existing comments still describe the earlier condition (the
CachedTablecomment inGuestBufferMemory.hppand the serial note aboveTableEntryinBdaResources.cpp); the conventions check only takes comment changes with a technical-debt change, so I left them as they are.Proposed wording for the two comments
GuestBufferMemory.hpp, aboveCachedTable:BdaResources.cpp, the last two lines of the comment aboveTableEntry:#1364 also reuses buffer copies, but only for builds that are not address-based (
!addressableinUploadPrepareandcopyRegion) and only for host-backed or writable regions, so it does not reach these snapshots.Tested
Windows 11, RTX 5070 Ti, WinLibs GCC 15.2.0 r7, Release.
agc_driver_graphics(tests/BdaResources.cpp) gets two cases:addressTableKeyTests: keys of builds that differ only in their written ranges, or only in the device address of an own region, differ; written ranges and own regions with the same words give different keys.snapshotTableTests, over a real cached address space: two builds of one snapshot with the same bytes share its device copy and their BDA table; a snapshot with other bytes gets a copy of its own and another table; a build bound by descriptor never takes a copy made for BDA; a build with written ranges shares the snapshot copy; every cached table equals the build'sAddressRanges(); afterClearSnapshotCopiesa build gets a new copy, and no Vulkan object is left behind.Full
ctest(Windows, RTX 5070 Ti): 1097 of 1098 pass.agc_driver_wave32_subgroupfails (thread 0 s_bcnt1 of a VCC compare is 0, expected 8) and fails the same way onmainhere.python tools/check_conventions.py --base upstream/mainpasses.CI on the fork, same steps as
build.yml(run 38477341209): Linux and Windows pass. The first Windows attempt lostguest_kqueue(EVFILT_VNODE: FindFirstChangeNotification failed, error=5, nothing this change touches); the rerun passed.The Pathless (PPSA01825, 02.000.000), main menu,
main0aa4cb1c2against this PR:APS5_PROFILE_DRAWrun per side: an address-based build went from 854 to 592 µs on average, at about 44 builds per frame on both sides, so the address-based builds went from 37.5 to 26.1 ms per frame (-11.4 ms).[bda-table]went from 0 hits of 3,911 to 4,269 of 4,994 (85 %). The remaining misses are tables whose buffer expired: the table cache holds weak references, so when every build that used a table has finished, the next build of the same key builds the buffer again from the cached ranges. The snapshot copies saw no differing bytes and no evictions in steady state (64,034 reuses, 14 new copies, all at start-up).APS5_VERIFY_BDA_TABLES=1: every hit was rebuilt and compared, none differed.APS5_TRACE_SECONDSwindow of 12 s, warm caches, 5 valid runs per side alternated base/change (runs lost to the Wwise crash of #361 repeated): 102.5 ± 5.8 ms onmainagainst 94.7 ± 6.1 ms with this PR (9.78 ± 0.58 against 10.60 ± 0.67 FPS). The change was faster in each of the five pairs, by 0.8 to 21.4 ms; the spread of this batch is large, so the per-build numbers above are the measurement and the frame time only confirms their direction.Summary lines of the ten runs,
mainfirst (commit0aa4cb1c2fismain; d60ec357fbea is this change beforeAddressTableKeywas split out ofCachedAddressTable, with the same behaviour); the ten traces are in the attached zip.aps5-summary.jsonl
Hades (PPSA03355), ANIMAL WELL (PPSA22520), Balatro (PPSA21402) and Dead Cells (PPSA15552),
main0aa4cb1c2against this PR, 90 s each with 8 dumped frames: the same points reached (the first three keep running at their menus; Dead Cells stops on both sides atNGS2: non-default listener orientation is unsupported), the same set of[gpu]log lines, ANIMAL WELL's frames identical; Hades's and Balatro's frames differ only where their animations or loading were when dumped (Hades still loading on one side at frame 4, at the menu on both from frame 6), which I checked by eye.LEGO Horizon Adventures (PPSA14632, 01.004.000),
main0aa4cb1c2against this PR, 2 runs per side of 160 s with 8 dumped frames into the intro: all four reach the intro cinematic and keep running, with the same[gpu]log lines and no errors; the dumped frames differ only where the cinematic was when each one was taken (the two runs with this PR also differ from each other there), and the last frame matches.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)bda-table-hits-traces.zip
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T00:56:32Z.
Review: COMMENTED
This is a good one. It removes the work at its source (the table differs only because snapshot copies get new device addresses), and the evidence is what CONTRIBUTING asks for: summary lines, traces, alternated runs,
APS5_VERIFY_BDA_TABLESon the measured run, and regression runs on other titles.Things I checked that look right:
shared_ptr, so there is no write-after-read hazard on reuse.Smaller points:
Buffer's mapped memory.BufferprefersHOST_CACHEDfor the default host properties but falls back to plainHOST_VISIBLE | HOST_COHERENT, which is usually write-combined. On a device where that happens, thememcmpinsnapshotCopyreads from uncached memory on every build, which is much slower and could cancel part of the gain. Keeping a CPU-side copy of the bytes in the cache entry, or a hash plus the guest generation, would make the check independent of the memory type.SnapshotCopyCacheis process-wide and holds one device at a time: a call with anotherVkDeviceclears it. During device replacement, or with a compile-only device alongside, two devices would keep clearing each other's entries. A map keyed by device, like the other per-device caches, would avoid that.space->tablesstill evictsbegin()of astd::maponce it reaches 64 entries, which is the lexicographically smallest key, not the oldest. That was harmless while the keys were only write sets. Now that own-region keys share the map, a scene with more than 64 combinations would evict an arbitrary one per miss. An LRU like the onesnapshotCopyuses would be more predictable.Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-14T03:15:07Z.
Windows test of #5542: Windows 10 (19045), MinGW-w64 GCC 15.2, Release,
ninja -j 4, ctest called directly; GPU selected by the execution tests (Selected GPUlog line): NVIDIA GeForce RTX 4080 SUPER. Tree:maina5f493f90+ merge of PR headf3e40434d14b930c0af3afe9e956096e75d9eed0(local merge79b6d0285, no conflict). Logs:rtx5542_*_N.log,rtx5542_result.txt.1 ctest targets, 3 runs each, exit code 0 in 3 of 3 runs:
agc_driver_graphics.Test selection: the file the PR changes,
tests/BdaResources.cpp, is compiled intoagc_driver_graphics_tests(libSceAgcDriver/CMakeLists.txt:1042onmain), soagc_driver_graphicsis the target that runs it. (An earlier run ofagc_driver_bda_deviceandagc_driver_bda_span_readswas the wrong selection and is not part of this result.)Not tested: that the new or changed tests fail without the change (no baseline run), other ctest targets, the AMD GPU unless named above, Linux, a title run.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-14T03:15:45Z.
Correction to my Windows test above: I wrote that the GPU selected by the tests was the NVIDIA GeForce RTX 4080 SUPER (
Selected GPUlog line). That line is not in thectest -Vlogs ofagc_driver_graphics, the target this result is about; it appeared only in the logs of another target (agc_driver_bda_span_reads) that I had run by mistake and that does not belong to this result. So which GPUagc_driver_graphicsused is not shown by these logs. The rest stands:agc_driver_graphicsexits with code 0 in 3 of 3 runs onmaina5f493f90+ this PR's headf3e40434d.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by winstrex on 2026-10-14T05:59:30Z.
Thanks for the careful review. Changed in 138c3090c: each cached snapshot copy now keeps a CPU copy of its bytes and the reuse check compares against that, so it no longer reads the buffer's mapping whatever its memory type; and the space's table map evicts the least recently used table instead of the smallest key (new test case). On The Pathless the CPU copies hold about 17 KB, and address-based builds measure the same as before in 3+3 alternated runs. The snapshot cache holds one device at a time on purpose, like the image mirrors next to it: only one device builds resources at a time,
ClearSnapshotCopiesruns in that device's teardown, and the compile-only device never builds snapshot copies. Retaining the most recent table buffers stays a follow-up.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T20:40:03Z.
Checked the snapshot-copy cache (key, byte compare against the CPU copy, LRU/budget, teardown) and the
AddressTableKeytable sharing against the diff;agc_driver_graphicspasses on main + this PR (the only other failure inlibSceAgcDriver,agc_driver_tessellation, fails the same without the change). Merging once the full Build is green.