Skip to content

perf(agc): share snapshot copies and the BDA table of repeated address-based builds - #5542

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
winstrex:perf/bda-table-hits
Oct 14, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
winstrex:perf/bda-table-hits

Conversation

@winstrex

@winstrex winstrex commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

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. BdaResources then 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:

  • A read-only snapshot region (not host-backed, not writable, not sparse) takes its buffer from a per-device cache of snapshot copies, keyed by guest address, size and buffer usage. A copy is reused only when its bytes equal the snapshot's (compared on every use); otherwise a new copy replaces it. The cache keeps at most 16 MiB and 1,024 copies, least recently used first out, and the device teardown empties it (ClearSnapshotCopies). A dropped copy stays alive in every build and batch that still holds it.
  • CachedAddressTable now 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. BdaResources binds a cached table's buffer by its serial, as it does for the space's own table. APS5_VERIFY_BDA_TABLES=1 rebuilds the table on every hit and throws when the bytes differ.

Two existing comments still describe the earlier condition (the CachedTable comment in GuestBufferMemory.hpp and the serial note above TableEntry in BdaResources.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, above CachedTable:

    // The BDA table of an address-based build that takes the cached address space: its ranges and a
    // serial, by which BdaResources reuses the table buffer built for them without a compare. A
    // build with no region outside the space and no written range gets the space's own ranges and
    // serial; the others share a table per space, keyed by their written ranges and their own
    // regions' table entries (AddressTableKey), which with the space's ranges are everything
    // AddressRanges reads. Nothing without a cached address space.

BdaResources.cpp, the last two lines of the comment above TableEntry:

// A table built for (or found equal to) a cached table (GuestBufferMemory::CachedAddressTable)
// carries its serial: a build whose cached table has that serial binds it by the serial, with no
// hash and no compare.

#1364 also reuses buffer copies, but only for builds that are not address-based (!addressable in UploadPrepare and copyRegion) 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's AddressRanges(); after ClearSnapshotCopies a build gets a new copy, and no Vulkan object is left behind.
    • Each fails when the change is mutated: reusing a copy without comparing its bytes, keying copies without their usage, leaving the own regions, the written ranges or the separator out of the table key, an empty teardown, and refusing a cached table to builds with own regions.
  • Full ctest (Windows, RTX 5070 Ti): 1097 of 1098 pass. agc_driver_wave32_subgroup fails (thread 0 s_bcnt1 of a VCC compare is 0, expected 8) and fails the same way on main here.

  • python tools/check_conventions.py --base upstream/main passes.

  • CI on the fork, same steps as build.yml (run 38477341209): Linux and Windows pass. The first Windows attempt lost guest_kqueue (EVFILT_VNODE: FindFirstChangeNotification failed, error=5, nothing this change touches); the rerun passed.

  • The Pathless (PPSA01825, 02.000.000), main menu, main 0aa4cb1 against this PR:

    • One APS5_PROFILE_DRAW run 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).
    • The run with this PR used APS5_VERIFY_BDA_TABLES=1: every hit was rebuilt and compared, none differed.
    • Frame time as a confirmation, APS5_TRACE_SECONDS window of 12 s, warm caches, 5 valid runs per side alternated base/change (runs lost to the Wwise crash of Windows: resuming a write fault overwrites the guest red zone when AVX state is live #361 repeated): 102.5 ± 5.8 ms on main against 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, main first (commit 0aa4cb1 is main; d60ec357fbea is this change before AddressTableKey was split out of CachedAddressTable, with the same behaviour); the ten traces are in the attached zip.
    aps5-summary.jsonl
    {"commit":"0aa4cb1c2f2f","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"75:12"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 591.86","os":"Windows 10.0.26300","resolution":"2560x1440","window":{"spec":"seconds 75:12","first_present":1171,"ms":12000.318},"presents":128,"frame_ms":{"p50":90.226,"p90":112.609,"p99":116.532,"max":117.862},"gpu_busy_ms_per_present":29.149,"worker_busy_pct":{"queue worker 0x20":8.739,"queue worker 0x0":99.429},"top_phases_ms_per_frame":{"Driver.Packet":95.248,"Driver.Packet.draw":76.551,"Driver.Draw":76.444,"Driver.Draw.draw_and_resource_release":63.671,"Graphics.Draw":63.575,"Graphics.Draw.draw_and_resource_release":53.148,"Resources.Complete":23.217,"Driver.Packet.dispatch":10.866},"trace_dropped":false,"trace":"aps5-trace-0aa4cb1c2f2f-35040.json.gz"}
    {"commit":"0aa4cb1c2f2f","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"75:12"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 591.86","os":"Windows 10.0.26300","resolution":"2560x1440","window":{"spec":"seconds 75:12","first_present":1144,"ms":12089.818},"presents":116,"frame_ms":{"p50":102.191,"p90":123.739,"p99":140.284,"max":144.374},"gpu_busy_ms_per_present":30.109,"worker_busy_pct":{"queue worker 0x20":7.511,"queue worker 0x0":99.386},"top_phases_ms_per_frame":{"Driver.Packet":104.680,"Driver.Packet.draw":86.459,"Driver.Draw":86.342,"Driver.Draw.draw_and_resource_release":72.676,"Graphics.Draw":72.571,"Graphics.Draw.draw_and_resource_release":62.226,"Resources.Complete":25.647,"Driver.Draw.shader_compile_and_link":10.668},"trace_dropped":false,"trace":"aps5-trace-0aa4cb1c2f2f-9284.json.gz"}
    {"commit":"0aa4cb1c2f2f","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"75:12"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 591.86","os":"Windows 10.0.26300","resolution":"2560x1440","window":{"spec":"seconds 75:12","first_present":1113,"ms":12027.499},"presents":110,"frame_ms":{"p50":110.525,"p90":131.444,"p99":143.713,"max":148.704},"gpu_busy_ms_per_present":30.374,"worker_busy_pct":{"queue worker 0x0":99.387,"queue worker 0x20":8.868},"top_phases_ms_per_frame":{"Driver.Packet":111.609,"Driver.Packet.draw":90.908,"Driver.Draw":90.790,"Driver.Draw.draw_and_resource_release":76.958,"Graphics.Draw":76.852,"Graphics.Draw.draw_and_resource_release":66.341,"Resources.Complete":27.611,"Driver.Packet.dispatch":12.543},"trace_dropped":false,"trace":"aps5-trace-0aa4cb1c2f2f-10652.json.gz"}
    {"commit":"0aa4cb1c2f2f","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"75:12"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 591.86","os":"Windows 10.0.26300","resolution":"2560x1440","window":{"spec":"seconds 75:12","first_present":1136,"ms":12061.115},"presents":120,"frame_ms":{"p50":95.965,"p90":124.306,"p99":129.376,"max":132.679},"gpu_busy_ms_per_present":29.463,"worker_busy_pct":{"queue worker 0x0":99.390,"queue worker 0x20":7.585},"top_phases_ms_per_frame":{"Driver.Packet":101.077,"Driver.Packet.draw":82.564,"Driver.Draw":82.454,"Driver.Draw.draw_and_resource_release":68.897,"Graphics.Draw":68.794,"Graphics.Draw.draw_and_resource_release":58.589,"Resources.Complete":26.157,"Driver.Packet.dispatch":10.691},"trace_dropped":false,"trace":"aps5-trace-0aa4cb1c2f2f-39572.json.gz"}
    {"commit":"0aa4cb1c2f2f","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"75:12"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 591.86","os":"Windows 10.0.26300","resolution":"2560x1440","window":{"spec":"seconds 75:12","first_present":1174,"ms":12097.473},"presents":115,"frame_ms":{"p50":101.138,"p90":126.056,"p99":132.167,"max":133.739},"gpu_busy_ms_per_present":30.244,"worker_busy_pct":{"queue worker 0x0":99.428,"queue worker 0x20":7.718},"top_phases_ms_per_frame":{"Driver.Packet":106.102,"Driver.Packet.draw":87.107,"Driver.Draw":86.993,"Driver.Draw.draw_and_resource_release":73.530,"Graphics.Draw":73.424,"Graphics.Draw.draw_and_resource_release":63.376,"Resources.Complete":27.458,"Driver.Packet.dispatch":10.861},"trace_dropped":false,"trace":"aps5-trace-0aa4cb1c2f2f-20816.json.gz"}
    {"commit":"d60ec357fbea","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"75:12"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 591.86","os":"Windows 10.0.26300","resolution":"2560x1440","window":{"spec":"seconds 75:12","first_present":1209,"ms":12057.791},"presents":130,"frame_ms":{"p50":88.345,"p90":115.075,"p99":133.275,"max":139.787},"gpu_busy_ms_per_present":29.699,"worker_busy_pct":{"queue worker 0x0":99.358,"queue worker 0x20":8.198},"top_phases_ms_per_frame":{"Driver.Packet":93.154,"Driver.Packet.draw":75.577,"Driver.Draw":75.464,"Driver.Draw.draw_and_resource_release":61.291,"Graphics.Draw":61.180,"Graphics.Draw.draw_and_resource_release":50.491,"Resources.Complete":16.960,"Driver.Draw.shader_compile_and_link":11.066},"trace_dropped":false,"trace":"aps5-trace-d60ec357fbea-25204.json.gz"}
    {"commit":"d60ec357fbea","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"75:12"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 591.86","os":"Windows 10.0.26300","resolution":"2560x1440","window":{"spec":"seconds 75:12","first_present":1161,"ms":12095.404},"presents":132,"frame_ms":{"p50":89.073,"p90":110.010,"p99":114.512,"max":114.521},"gpu_busy_ms_per_present":28.725,"worker_busy_pct":{"queue worker 0x0":99.396,"queue worker 0x20":8.392},"top_phases_ms_per_frame":{"Driver.Packet":92.978,"Driver.Packet.draw":74.312,"Driver.Draw":74.205,"Driver.Draw.draw_and_resource_release":61.258,"Graphics.Draw":61.159,"Graphics.Draw.draw_and_resource_release":50.789,"Resources.Complete":17.634,"Driver.Packet.dispatch":10.647},"trace_dropped":false,"trace":"aps5-trace-d60ec357fbea-36844.json.gz"}
    {"commit":"d60ec357fbea","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"75:12"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 591.86","os":"Windows 10.0.26300","resolution":"2560x1440","window":{"spec":"seconds 75:12","first_present":1176,"ms":12021.511},"presents":137,"frame_ms":{"p50":77.977,"p90":105.817,"p99":110.289,"max":112.473},"gpu_busy_ms_per_present":28.462,"worker_busy_pct":{"queue worker 0x20":8.101,"queue worker 0x0":99.393},"top_phases_ms_per_frame":{"Driver.Packet":87.656,"Driver.Packet.draw":70.840,"Driver.Draw":70.733,"Driver.Draw.draw_and_resource_release":57.595,"Graphics.Draw":57.497,"Graphics.Draw.draw_and_resource_release":47.342,"Resources.Complete":15.981,"Driver.Draw.shader_compile_and_link":10.303},"trace_dropped":false,"trace":"aps5-trace-d60ec357fbea-19788.json.gz"}
    {"commit":"d60ec357fbea","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"75:12"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 591.86","os":"Windows 10.0.26300","resolution":"2560x1440","window":{"spec":"seconds 75:12","first_present":1170,"ms":12074.731},"presents":124,"frame_ms":{"p50":91.488,"p90":120.388,"p99":132.455,"max":153.838},"gpu_busy_ms_per_present":29.659,"worker_busy_pct":{"queue worker 0x0":99.361,"queue worker 0x20":8.650},"top_phases_ms_per_frame":{"Driver.Packet":98.180,"Driver.Packet.draw":79.821,"Driver.Draw":79.705,"Driver.Draw.draw_and_resource_release":66.105,"Graphics.Draw":65.999,"Graphics.Draw.draw_and_resource_release":55.555,"Resources.Complete":18.969,"Driver.Packet.dispatch":10.867},"trace_dropped":false,"trace":"aps5-trace-d60ec357fbea-20412.json.gz"}
    {"commit":"d60ec357fbea","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"75:12"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 591.86","os":"Windows 10.0.26300","resolution":"2560x1440","window":{"spec":"seconds 75:12","first_present":1131,"ms":12069.191},"presents":116,"frame_ms":{"p50":100.924,"p90":128.199,"p99":132.620,"max":136.837},"gpu_busy_ms_per_present":30.590,"worker_busy_pct":{"queue worker 0x0":99.336,"queue worker 0x20":8.214},"top_phases_ms_per_frame":{"Driver.Packet":105.350,"Driver.Packet.draw":84.946,"Driver.Draw":84.829,"Driver.Draw.draw_and_resource_release":70.825,"Graphics.Draw":70.716,"Graphics.Draw.draw_and_resource_release":60.133,"Resources.Complete":20.472,"Driver.Packet.dispatch":11.544},"trace_dropped":false,"trace":"aps5-trace-d60ec357fbea-17168.json.gz"}
    
  • Hades (PPSA03355), ANIMAL WELL (PPSA22520), Balatro (PPSA21402) and Dead Cells (PPSA15552), main 0aa4cb1 against 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 at NGS2: 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), main 0aa4cb1 against 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

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 5070 Ti, driver 591.86, Windows 11
  • AI-assisted: yes

bda-table-hits-traces.zip

@CnRJay CnRJay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_TABLES on the measured run, and regression runs on other titles.

Things I checked that look right:

  • A snapshot copy is only shared for regions that are not writable, not host-backed and not sparse, and a copy whose bytes differ is replaced, never overwritten. A batch still reading the old copy on the GPU keeps its shared_ptr, so there is no write-after-read hazard on reuse.
  • The table key includes the own regions' device address and permissions, so a table can't be shared between builds whose copies differ.

Smaller points:

  • The byte compare on every use reads the cached Buffer's mapped memory. Buffer prefers HOST_CACHED for the default host properties but falls back to plain HOST_VISIBLE | HOST_COHERENT, which is usually write-combined. On a device where that happens, the memcmp in snapshotCopy reads 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.
  • SnapshotCopyCache is process-wide and holds one device at a time: a call with another VkDevice clears 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->tables still evicts begin() of a std::map once 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 one snapshotCopy uses would be more predictable.
  • On the remaining 15% of misses (tables whose buffer expired because the cache holds weak references): if that matters in other titles, keeping the most recent N table buffers alive would turn those into hits too. That can be a follow-up.

@xXPBSXx

xXPBSXx commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

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 GPU log line): NVIDIA GeForce RTX 4080 SUPER. Tree: main a5f493f90 + merge of PR head f3e40434d14b930c0af3afe9e956096e75d9eed0 (local merge 79b6d0285, 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 into agc_driver_graphics_tests (libSceAgcDriver/CMakeLists.txt:1042 on main), so agc_driver_graphics is the target that runs it. (An earlier run of agc_driver_bda_device and agc_driver_bda_span_reads was 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).

@xXPBSXx

xXPBSXx commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Correction to my Windows test above: I wrote that the GPU selected by the tests was the NVIDIA GeForce RTX 4080 SUPER (Selected GPU log line). That line is not in the ctest -V logs of agc_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 GPU agc_driver_graphics used is not shown by these logs. The rest stands: agc_driver_graphics exits with code 0 in 3 of 3 runs on main a5f493f90 + this PR's head f3e40434d.

AI-assisted (Claude Code).

@winstrex

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review. Changed in 138c309: 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, ClearSnapshotCopies runs in that device's teardown, and the compile-only device never builds snapshot copies. Retaining the most recent table buffers stays a follow-up.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 14, 2026
winstrex and others added 2 commits October 14, 2026 13:16
…s-based builds

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… recency

Each cached snapshot copy keeps a CPU copy of its bytes, so the reuse
check no longer reads the buffer's mapping, which is uncached on devices
without host-cached coherent memory. An address space's table map now
evicts its least recently used table instead of its smallest key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@winstrex
winstrex force-pushed the perf/bda-table-hits branch from 138c309 to 97c3687 Compare October 14, 2026 17:46
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 14, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the snapshot-copy cache (key, byte compare against the CPU copy, LRU/budget, teardown) and the AddressTableKey table sharing against the diff; agc_driver_graphics passes on main + this PR (the only other failure in libSceAgcDriver, agc_driver_tessellation, fails the same without the change). Merging once the full Build is green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants