fix(agc): count copied guest buffers and draw snapshots in the batch byte budget #2283
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2283"
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 MathiasDevelopes on 2026-10-09T14:32:22Z.
What
The [recorder](https://github.com/boykopovar/AnyPS5/blob/main/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp) bounds the bytes a batch keeps alive:
BoundKeptBytessubmits an open batch atKeptBytesBudget(512 MiB) and waits for the oldest in-flight batches past twice that. Only bytes passed toRecorder::Keep(object, bytes)count, and a recorded dispatch or draw kept itsShaderResources(and a draw itsDrawBindingssnapshots) with a size of 0. The host-visible copies of guest buffers that these objects own were never counted.Planet Coaster (PPSA01736) binds a 124 MiB writable guest buffer in nearly every draw after its intro. The process went from 8.7 GB to 20.7 GB private memory within three seconds. 98 buffers of 124 MiB were created and 12 released, until:
GuestBufferMemory::CopiedBytes()now reports the bytes of its regions that are served by a copy,ShaderResources::CopiedBytes()forwards it, and the dispatch keep, the draw keep and the draw snapshots pass their bytes toRecorder::Keep. The existing bound then applies to them; nothing else about the bound changes.Related open PRs: #1364 (and #1366, which contains it) counts retained draw upload pages in the same budget; it does not touch these keeps. #1997 passes sizes for its own image merge buffers. The call sites above are untouched by both.
This is a bound, not a fix of the copies themselves: the title still re-copies that buffer for each draw (see the related region fix in a separate PR).
Tested
Windows 11, MinGW-w64 15.2.0, RTX 3060.
agc_driver_bda_resources:CopiedBytes()is 0 before an upload and, after it, either 0 (served by an import) or the one copied region's size.tools/check_conventions.py --base origin/mainpasses.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🤖 Generated with [Claude Code](https://claude.com/claude-code)
Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-10-09T14:32:30Z.
Review: COMMENTED
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T14:44:21Z.
I reviewed the complete six-file diff. The byte accounting follows the retained
GuestBufferMemoryregions and draw snapshots, and the new test checks the no-copy/import path as well as a copied upload. The Windows full-suite report and Linux/Windows/macOS relinker jobs are green; the upstream full Linux and Windows Build jobs were skipped. Could an authorized collaborator run the required full Build workflow on this PR head before merge?Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:01:42Z.
Review: CHANGES_REQUESTED
Thanks, the bound makes sense. Build and the libSceAgcDriver ctest scope are green here (the new assertions fail to compile without the change). Points to address:
tests/BdaResources.cpp:431:CopiedBytes() == 0 || CopiedBytes() == sizeof(guest)accepts both outcomes. Pin the expected value for the case the test sets up (copy vs. import), or build one case for each.Keepcalls (VulkanDevice.cpp:3318,Draw.cpp:1176,ShaderResources.cpp:3159) leaves it green. Please add a test showing that the kept bytes reach the recorder's budget (see the existingagc_recorder_kept_bytes_testsfixture).ShaderResourcesretained by several draws/dispatches is not counted once per keep, otherwise the batch is submitted earlier than the budget says.Imported from boykopovar/AnyPS5. Originally posted by MathiasDevelopes on 2026-10-10T09:59:26Z.
@adriadam10
Thanks for the review. Pushed 26d59264 (and 55b09c0f, which drops comments the conventions check rejects):
BdaResources: the check now pins CopiedBytes() == sizeof(guest). guest is stack memory no allocation registers, so no host import serves it and the two views merge into one copied region (the WriteBack publishing the aliased write below already depends on that copy).
New device test agc_driver_recorded_kept_bytes (tests/execution/RecordedKeptBytes.cpp). It reads the open batch's kept bytes (new Recorder::OpenKeptBytes) after a recorded dispatch writing a copied 64 KiB buffer, after a recorded draw reading a copied 64 KiB buffer (into a resident target, so the draw is recorded rather than synchronous), and after two draws reading a registered, write-watched 64 KiB buffer in place, which take a draw snapshot.
I checked it against each revert separately: removing the dispatch keep, the draw keep or the snapshot keep each fails it, and so does counting a reused snapshot twice. It takes about 0.2 s on lavapipe.
The mock fixture gets SharedOwnerCountsOncePerBatch for the counting rule itself.
It was counted once per keep, so you were right. A cached ShaderResources (the dispatch resource cache, the draw SharedResourceCache, recipe templates) is one object shared by every draw or dispatch that hits it. Reusable() accepts device-local staged regions, which have a buffer, so CopiedBytes() can be nonzero for a shared object. Draw snapshots were affected too: ReusableDrawSnapshot hands the next draw the same buffer. Recorder::KeepBytes(owner, bytes) now counts an owner once per batch through a per-batch set, and all three sites use it. An object used by two batches that are both in flight still counts once in each. That errs toward waiting early, never late.
Tested on Linux with lavapipe, with the PR merged into current main: the libSceAgcDriver ctest scope gives 320/320 passing (27 skipped because llvmpipe has subgroup size 8).
Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-10-10T10:05:20Z.
Additional platform validation on standalone PR head
6e3e5e48edc11c15f72b57b266876f5832382f75:Linux: passed — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja.
Release /
BUILD_TESTING=ON: built the selected test targets; 1 CTest cases, 0 failures, 0 CTest skips.Tests:
agc_driver_graphics.Reproduce after building the test targets:
ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^agc_driver_(bda_resources|graphics)$".Vulkan: Mesa lavapipe 26.2.4 software Vulkan; no native Linux GPU/retail-title validation.
Internal limitation: write-watched guest-heap mirror subcases returned early because WSL userfaultfd async write protection is unavailable. The other graphics assertions ran; zero CTest skips does not include this internal early return.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T17:24:55Z.
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/Graphics/include/Recorder.hppL87Imported from boykopovar/AnyPS5. Originally posted by orishamir on 2026-10-10T19:12:17Z.
55b09c0fmerges cleanly onto currentmain(4a747c48) but the new test does not build there:tests/execution/RecordedKeptBytes.cpp:97callsGuestAllocations::Mutation().Add(block, bytes, true, true), and sincec11a9cb5("address-based builds map only memory mapped for the GPU")Addtakes a fifthgpuargument, soagc_driver_recorded_kept_bytes_testsfails to compile (no matching function for call to ... Add(uint8_t*&, std::size_t&, bool, bool)). #374 and #1619 made the same one-argument adaptation.With that call changed to
Add(block, bytes, true, true, true)locally, the three review points check out on llvmpipe:agc_driver_recorded_kept_bytes,agc_recorder_kept_bytes,agc_driver_bda_deviceandagc_driver_bda_span_readspass, and the new test fails under each keep-site revert tomainseparately:VulkanDevice.cpp("a dispatch writing a copied 65536 bytes buffer left 0 bytes in its batch's kept bytes"),Draw.cpp("a draw reading a copied 65536 bytes buffer left 0 bytes ..."),ShaderResources.cpp("a draw snapshotting a 65536 bytes buffer left 0 bytes ...").KeepBytescounts an owner once per batch throughkeptOwners.AI-assisted: yes (Claude Code Fable 5.1 xhigh)
Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-10T21:56:31Z.
Windows cross-test of #2283 head 55b09c0f, applied to the 007 integration stack at
79dd488f. I included the fifthgpu=trueargument inGuestAllocations::Mutation().Add, as identified by orishamir. Public integration head f4d4d041: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-2283-cross-test . Original authorship preserved.Release MinGW GCC 15.2, Windows 11, RTX 5070 Ti, SPIRV-Tools OFF, Vulkan required: 4/4 selected CTest checks pass (
agc_driver_recorded_kept_bytes,agc_recorder_kept_bytes,agc_driver_graphics,agc_host_import_unmap). The execution regression exercises copied dispatch and draw keeps. Its reusable draw-snapshot subcase explicitly returns early here because Windows direct imports use comparisons; this is not a claim that all internal snapshot subcases ran. The owner-deduplication mock regression does run.This is an integration-stack result, not a standalone build of #2283 on main. A fresh 007 run is next; no game-result claim for this head yet. My standalone prototype
01754bf0on main262c0316also built and passed its 3 selected tests, but I am keeping it as a diagnostic comparison, not submitting a competing PR.AI-assisted (Codex).
Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-10T22:00:17Z.
Linux cross-test of 55b09c0f. Setup: CachyOS (kernel 7.2.9, GCC 16.2.1), RTX 3080 (NVIDIA 615.71.09), Release.
Merged onto
main262c0316, SPIRV-Tools onIt merges cleanly. I applied orishamir's adaptation of
tests/execution/RecordedKeptBytes.cpp:97(Add(block, bytes, true, true, true)). As orishamir found, the test doesn't compile onmainwithout it. Full suite: of 712 tests, 707 pass, 4 are skipped and 1 fails.agc_driver_recorded_kept_bytes,agc_recorder_kept_bytes,agc_driver_graphics,agc_driver_bda_deviceandagc_driver_bda_span_reads.video_out_flip_*tests, under ctest's offscreen SDL driver.agc_driver_dcc_keys_first_write, a known failure on this machine (#2767). Neither PR touches it.007 First Light (PPSA11386)
On
main, 007 stops earlier (#2523 row 4), so this was tested on the 007 stack from #2523: Scramblrrrr's 79dd488f with local commits, SPIRV-Tools off, default asynchronous draws, one 240 s run each.An earlier trace on the 75e319f5 stack from #2523, which has the same
GuestBufferMemorycode and the same 0-byte draw keep, showed the Planet Coaster pattern at a larger size. Draw builds copied one or both of two sparse 512 MiB regions into new host-visible buffers, about 1 GiB per build, and the draws kept those copies with 0 bytes. On 79dd488f without this PR, available memory falls from 33.8 to 6.1 GiB within 7 s, and my watchdog stops the title.With the three commits cherry-picked:
vkAllocateMemoryerrors orwhat(). The cold cache compiles 2,194 shader variants, and none logscould not be prepared.Combined with #2992 there's no jump either: the lowest available memory is 27.2 GiB. One thing to know about that combination:
CopiedBytes()countsregion.end - region.begin, but #2992 cuts a sparse region's copy atbufferEnd. With both applied, a cut region is counted at its full size, so the batch is submitted earlier than its real kept bytes require. The full table is in #2523.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-10T22:04:07Z.
Follow-up to #2283: I reproduced a buffer-only enforcement gap and pushed 0280ac32 on
codex/recorder-buffer-only-budget-followup(parent integrationf4d4d041):github.com/Scramblrrrr/AnyPS5@0280ac32. This is a 24-line follow-up, not a competing copied-byte implementation.The existing bound checks are in sampled/storage texture lookups. A dispatch with no images can keep adding counted buffers without reaching either check. The new Vulkan regression records one buffer-only dispatch, seeds that open batch at the existing byte budget using a kept test holder, then dispatches to a distinct buffer. It requires that the preceding batch has submitted. The helper holds
GuestMemory::GpuMutex, matching the driver's calling convention; no giant allocation or title data is needed.Negative control: with #2283's byte accounting but no entry checks, the regression fails:
a buffer-only dispatch did not submit the preceding batch at its kept-byte budget. Positive: budget checks at ordinary/recipe dispatch and draw entry points, before recording new work, make it pass. The four selected Windows tests pass again with Vulkan required. Snapshot subcase remains explicitly untested here as reported above. Conventions and diff checks pass. These are integration-stack checks, not a standalone whole-suite result.@MathiasDevelopes, happy for you to fold the follow-up into #2283 with authorship preserved if useful. It depends on #2283's recorded-kept-bytes fixture; the integration also has the already reported fifth
gpu=trueregistration argument.The currently running 007 process still has f4d4d041, not this follow-up, so I am not attributing its behavior to these entry checks. At 369 flips it remains black, paused/resumed under the memory guard during the same large NVIDIA pipeline compilation. No playability claim.
AI-assisted (Codex).
Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-10T22:25:23Z.
Linux check of @Scramblrrrr's follow-up 0280ac32. Setup: CachyOS (kernel 7.2.9, GCC 16.2.1), RTX 3080 (NVIDIA 615.71.09), Release, SPIRV-Tools on. Tested on
maina3c43ad2with #2283 (55b09c0f) merged, then f4d4d041 (the fifthgpuargument) and 0280ac32 cherry-picked.video_out_flip_{lifetime,reopen,controls,overlay}, under theSDL_VIDEODRIVER=offscreentheir CMake registration sets. One fails:agc_driver_dcc_keys_first_write, a known failure on this machine (#2767).agc_driver_recorded_kept_bytes,agc_recorder_kept_bytes,agc_driver_graphicsandagc_host_import_unmap.agc_driver_recorded_kept_bytesprintsrecorded kept bytes tests passedwithout "(draw snapshots not tested)", so the reusable-snapshot case that returned early on your Windows run passes on this setup.BoundKeptBytes()entry checks reverted (both source files set back to its parent) and its test kept,agc_driver_recorded_kept_bytesfails witha buffer-only dispatch did not submit the preceding batch at its kept-byte budget.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T22:37:12Z.
Review: APPROVED
Thanks, the new tests address the earlier review. Checked on current
main:main:GuestAllocations::Mutation().Addnow takes a fifthgpuargument (tests/execution/RecordedKeptBytes.cpp:97, found by @orishamir). Added as our own commitAdd(block, bytes, true, true, true).bda_resources,recorder_kept_bytesandrecorded_kept_bytespass. Without the change the tests don't compile; removing theKeepBytescall atVulkanDevice.cpp:3489orDraw.cpp:1436makesagc_driver_recorded_kept_bytesfail.Merging once the full Build workflow is green. The buffer-only follow-up (0280ac32) can go in its own PR.