fix(agc): mirror only unimported tails of registered heaps #8380

Merged
Zenodeon merged 2 commits from upstream-pr/8380 into main 2026-10-24 11:28:57 +00:00
Zenodeon commented 2026-10-20 11:49:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Zenodeon on 2026-10-20T11:49:09Z.


What

When a registered heap exceeds the host-import budget, import the fitting prefix and mirror only its unimported tail. Previously a budget refusal could mirror the whole registration, including bytes already represented by an import. Keep the original registration identity for lifetime tracking while keying and allocating each mirror by its selected physical range.

Rebased onto main ea695358ee1ccaf3d108b720d82c5d88413d43dc, dropping the merged #7292 prerequisite. This PR contains the standalone resource fix and a focused test follow-up. The partial mirror uses main's #7647 reservation and sparse-resident accounting: reserveHeapBytes, ResidentBuffer::Create, accounted, listed and unlistMirror, including reservation rollback and retirement. Removed the extra blank lines after importPieces.

Related work: #9514 changes budget-refusal eviction before the address-copy cap. Agreed merge order is #9514 first, then rebase this PR onto the resulting main. Its implementation is excluded from this branch. The local combined build needs one bookkeeping adaptation in #9514's added retry path: replace imported.push_back(range->address) with imported.emplace_back(range->address, range->address + range->bytes) to match this PR's imported-range tracking.

Tested

  • Windows 11 Pro 10.0.26200; NVIDIA RTX 4070, driver 610.88; GCC 15.2, Release, SPIR-V Tools enabled. Rebuilt complete libs and recorded local library hashes. No game or shader patches included.
  • Same 16 focused tests on clean main ea695358ee1ccaf3d108b720d82c5d88413d43dc and candidate 41d4affd6191d6b799f838a8e56b216335b563a2: 16/16 passed on each platform (Windows RTX 4070 and Ubuntu 26.04.1 under WSL2, lavapipe / llvmpipe LLVM 21.1.8, Mesa 26.0.8). Linux main was rerun during this follow-up; Windows main results are from the unchanged pinned base's prior matched run. Used ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and verbose CTest with a 30-second individual timeout. Verified the layer actually loaded; all four logs have no missing-layer warnings, skipped tests or VUID- errors.
  • Coverage: graphics/BDA device and span reads, real GPU resident heap mirrors, CPU device selection, draw-pipeline span reads, copied/sparse mirror unwatch with and without space caching, sparse budget, run unwatch, address-space teardown and specialized BDA shader ABI none/full.
  • Prior head 57851797857808c16b0380ba08f8df118e02dc36, composed locally with #9514 head f2e4290e9c75880fc964f56528414713d2e21e96 and the imported-range bookkeeping adaptation above: 18/18 passed, adding agc_address_copy_cap and agc_address_copy_cap_remembered. Actual validation layer loaded, no skipped tests, missing-layer warnings or VUID- errors. This composition predates the test-only follow-up and remains local.
  • Reproduced the old CI failure locally on lavapipe: read heap 0x769c65ff0000 had no imported prefix; the lower-addressed write heap 0x769c657e0000 had the 131072-byte prefix. The test incorrectly assumed allocation order equaled registry address order. This was not a sparse-block granularity difference. The follow-up verifies the exact prefix on the selected heap and tests both allocation orders with resident and copied fallback mirrors.
  • Negative control: the updated real GPU regression over main fails the first registered heap has no exact two-block imported prefix. The earlier Windows-only mocked cases also fail on main (a heap exceeding the import budget did not import its fitting prefix). No Vulkan validation errors in the negative controls.
  • partialHeapImportBudgetTests covers a 384 KiB registration with a 256 KiB imported prefix and exact 128 KiB tail, mirror replacement, first/last bytes, CPU writes, GPU write-back, short final windows, neighbors and retirement. Added the simultaneous import-window and budget-limit case.
  • Real GPU regression exercises sparse-backed and copied fallback mirrors for an 8 MiB registration with an asserted 128 KiB imported prefix. Its sparse tail accounts for 64 KiB when the selected heap is the read heap, or 128 KiB when it is the write heap (tail data plus the GPU-written block). Copied fallback accounts for the 8 MiB minus the verified prefix. GPU reads and subsequent CPU edits preserve exact values and neighboring bytes. These are mirror accounting results, not total physical VRAM, process RAM or FPS measurements; allocator slabs may remain allocated.
  • Gameplay, cross-title and extended stability checks have not been rerun on this revision. No FPS or process-memory improvement is claimed.
  • git diff --check and tools/check_conventions.py --base upstream/main pass. The required --base origin/main invocation reports inherited findings against the older fork main; those findings are outside this topic diff.

Checklist

  • Based on current main; no other open PR implements the same functions — related #9514 composition and merge order described above
  • 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: #9514 for merge order; its code is not included
  • Fixes: none
  • Unblocks: none; no current gameplay claim
  • GPUs tested: NVIDIA RTX 4070, driver 610.88, Windows 11 Pro 10.0.26200; lavapipe / llvmpipe LLVM 21.1.8, Mesa 26.0.8, Ubuntu 26.04.1 under WSL2
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4935279311 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8380). Originally posted by [Zenodeon](https://github.com/Zenodeon) on 2026-10-20T11:49:09Z. --- ### What When a registered heap exceeds the host-import budget, import the fitting prefix and mirror only its unimported tail. Previously a budget refusal could mirror the whole registration, including bytes already represented by an import. Keep the original registration identity for lifetime tracking while keying and allocating each mirror by its selected physical range. Rebased onto main `ea695358ee1ccaf3d108b720d82c5d88413d43dc`, dropping the merged #7292 prerequisite. This PR contains the standalone resource fix and a focused test follow-up. The partial mirror uses main's #7647 reservation and sparse-resident accounting: `reserveHeapBytes`, `ResidentBuffer::Create`, `accounted`, `listed` and `unlistMirror`, including reservation rollback and retirement. Removed the extra blank lines after `importPieces`. Related work: #9514 changes budget-refusal eviction before the address-copy cap. Agreed merge order is **#9514 first, then rebase this PR onto the resulting main**. Its implementation is excluded from this branch. The local combined build needs one bookkeeping adaptation in #9514's added retry path: replace `imported.push_back(range->address)` with `imported.emplace_back(range->address, range->address + range->bytes)` to match this PR's imported-range tracking. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Windows 11 Pro 10.0.26200; NVIDIA RTX 4070, driver 610.88; GCC 15.2, Release, SPIR-V Tools enabled. Rebuilt complete `libs` and recorded local library hashes. No game or shader patches included. - Same 16 focused tests on clean main `ea695358ee1ccaf3d108b720d82c5d88413d43dc` and candidate `41d4affd6191d6b799f838a8e56b216335b563a2`: **16/16 passed on each platform** (Windows RTX 4070 and Ubuntu 26.04.1 under WSL2, lavapipe / llvmpipe LLVM 21.1.8, Mesa 26.0.8). Linux main was rerun during this follow-up; Windows main results are from the unchanged pinned base's prior matched run. Used `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` and verbose CTest with a 30-second individual timeout. Verified the layer actually loaded; all four logs have no missing-layer warnings, skipped tests or `VUID-` errors. - Coverage: graphics/BDA device and span reads, real GPU resident heap mirrors, CPU device selection, draw-pipeline span reads, copied/sparse mirror unwatch with and without space caching, sparse budget, run unwatch, address-space teardown and specialized BDA shader ABI none/full. - Prior head `57851797857808c16b0380ba08f8df118e02dc36`, composed locally with #9514 head `f2e4290e9c75880fc964f56528414713d2e21e96` and the imported-range bookkeeping adaptation above: **18/18 passed**, adding `agc_address_copy_cap` and `agc_address_copy_cap_remembered`. Actual validation layer loaded, no skipped tests, missing-layer warnings or `VUID-` errors. This composition predates the test-only follow-up and remains local. - Reproduced the old CI failure locally on lavapipe: read heap `0x769c65ff0000` had no imported prefix; the lower-addressed write heap `0x769c657e0000` had the 131072-byte prefix. The test incorrectly assumed allocation order equaled registry address order. This was not a sparse-block granularity difference. The follow-up verifies the exact prefix on the selected heap and tests both allocation orders with resident and copied fallback mirrors. - Negative control: the updated real GPU regression over main fails `the first registered heap has no exact two-block imported prefix`. The earlier Windows-only mocked cases also fail on main (`a heap exceeding the import budget did not import its fitting prefix`). No Vulkan validation errors in the negative controls. - `partialHeapImportBudgetTests` covers a 384 KiB registration with a 256 KiB imported prefix and exact 128 KiB tail, mirror replacement, first/last bytes, CPU writes, GPU write-back, short final windows, neighbors and retirement. Added the simultaneous import-window and budget-limit case. - Real GPU regression exercises sparse-backed and copied fallback mirrors for an 8 MiB registration with an asserted 128 KiB imported prefix. Its sparse tail accounts for 64 KiB when the selected heap is the read heap, or 128 KiB when it is the write heap (tail data plus the GPU-written block). Copied fallback accounts for the 8 MiB minus the verified prefix. GPU reads and subsequent CPU edits preserve exact values and neighboring bytes. These are **mirror accounting results, not total physical VRAM, process RAM or FPS measurements**; allocator slabs may remain allocated. - Gameplay, cross-title and extended stability checks have not been rerun on this revision. No FPS or process-memory improvement is claimed. - `git diff --check` and `tools/check_conventions.py --base upstream/main` pass. The required `--base origin/main` invocation reports inherited findings against the older fork main; those findings are outside this topic diff. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions — related #9514 composition and merge order described above - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: <!-- #PR, or none --> #9514 for merge order; its code is not included - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none; no current gameplay claim - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> NVIDIA RTX 4070, driver 610.88, Windows 11 Pro 10.0.26200; lavapipe / llvmpipe LLVM 21.1.8, Mesa 26.0.8, Ubuntu 26.04.1 under WSL2 - [x] AI-assisted: <!-- yes / no --> yes
TheSovietWaffle commented 2026-10-20 13:47:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-20T13:47:15Z.


AMD data point: RTX 3080 (NVIDIA 617.42) and AMD Radeon RDNA 2 iGPU (1002:164e, 32.0.21045.5002), Windows 10 Pro 22H2, WinLibs GCC 15.2.0, Release, head 9a20ec2b8: agc_driver_graphics (which includes the new BdaResources.cpp cases) passes 3 of 3 on both GPUs.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6292369742 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8380#issuecomment-6292369742). Originally posted by [TheSovietWaffle](https://github.com/TheSovietWaffle) on 2026-10-20T13:47:15Z. --- AMD data point: RTX 3080 (NVIDIA 617.42) and AMD Radeon RDNA 2 iGPU (`1002:164e`, 32.0.21045.5002), Windows 10 Pro 22H2, WinLibs GCC 15.2.0, Release, head `9a20ec2b8`: `agc_driver_graphics` (which includes the new `BdaResources.cpp` cases) passes 3 of 3 on both GPUs. AI-assisted: yes (Claude Code).
EagleBearerYS commented 2026-10-22 00:45:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T00:45:07Z.


@Zenodeon a merge-order note: #9514 changes the plain import in AcquireRegistered, at the spot where this PR answers a refused import with budgeted pieces. When the host import budget refuses a range, #9514 retries the import with eviction, and only when copying the range would take the build past APS5_ADDRESS_COPY_MAX_MIB. A range your pieces serve isn't copied, so #9514's retry doesn't run for it, and the two should compose. As this one depends on #7292, a draft, #9514 proposes going first, and this PR would rebase over it. If you'd rather this one went first, say so and #9514 will list it under Depends on.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329889133 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8380#issuecomment-6329889133). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-22T00:45:07Z. --- @Zenodeon a merge-order note: #9514 changes the plain import in `AcquireRegistered`, at the spot where this PR answers a refused import with budgeted pieces. When the host import budget refuses a range, #9514 retries the import with eviction, and only when copying the range would take the build past `APS5_ADDRESS_COPY_MAX_MIB`. A range your pieces serve isn't copied, so #9514's retry doesn't run for it, and the two should compose. As this one depends on #7292, a draft, #9514 proposes going first, and this PR would rebase over it. If you'd rather this one went first, say so and #9514 will list it under Depends on.
MarcZX10 commented 2026-10-22 02:05:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T02:05:41Z.


I am reviewing #9514, which changes AcquireRegistered at the import site adjacent to #8380’s piece-import branch. Its author proposes that #9514 merge first, then #8380 rebase; the stated rationale is that piece-served ranges do not take the budget-refusal retry. Please confirm whether that order is safe for #8380, or describe the needed sequence, so both PRs can record the same agreed dependency/order before merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6330933996 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8380#issuecomment-6330933996). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T02:05:41Z. --- I am reviewing #9514, which changes `AcquireRegistered` at the import site adjacent to #8380’s piece-import branch. Its author proposes that #9514 merge first, then #8380 rebase; the stated rationale is that piece-served ranges do not take the budget-refusal retry. Please confirm whether that order is safe for #8380, or describe the needed sequence, so both PRs can record the same agreed dependency/order before merge.
adriadam10 commented 2026-10-23 16:35:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-23T16:35:48Z.

Review: CHANGES_REQUESTED


#7292 is merged, so the first commit can be dropped; the branch needs a rebase onto current main, and it is not mechanical:

  • GuestBufferMemory.cpp acquireMirror conflicts in four places. main now accounts heap mirrors through unlistMirror, reserveHeapBytes, ResidentBuffer::Create and mirror->accounted (#7647); this PR still uses heapMirrorsBeside(selected) and state.heapBytes +=/-=. The partial selected range (address/bytes) has to go through the new reservation and error paths (heapMirrorError(range->address, range->bytes, held, ...), mirror->listed).
  • After rebasing, re-run the new partialHeapImportBudgetTests with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, validation layer, and check the MirrorCounters().heapBytes assertions still hold with resident (sparse-backed) mirrors.
  • GuestBufferMemory.cpp:1694-1695: two blank lines after importPieces.
  • #9514 touches the same import site in AcquireRegistered; @billythemenace2 confirmed it composes if #9514 lands first, so rebase over it if it merges before you push.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5631467729 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8380#pullrequestreview-5631467729). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-23T16:35:48Z. **Review:** CHANGES_REQUESTED --- #7292 is merged, so the first commit can be dropped; the branch needs a rebase onto current `main`, and it is not mechanical: - `GuestBufferMemory.cpp` `acquireMirror` conflicts in four places. `main` now accounts heap mirrors through `unlistMirror`, `reserveHeapBytes`, `ResidentBuffer::Create` and `mirror->accounted` (#7647); this PR still uses `heapMirrorsBeside(selected)` and `state.heapBytes +=/-=`. The partial `selected` range (address/bytes) has to go through the new reservation and error paths (`heapMirrorError(range->address, range->bytes, held, ...)`, `mirror->listed`). - After rebasing, re-run the new `partialHeapImportBudgetTests` with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, validation layer, and check the `MirrorCounters().heapBytes` assertions still hold with resident (sparse-backed) mirrors. - `GuestBufferMemory.cpp:1694-1695`: two blank lines after `importPieces`. - #9514 touches the same import site in `AcquireRegistered`; @billythemenace2 confirmed it composes if #9514 lands first, so rebase over it if it merges before you push.
adriadam10 commented 2026-10-23 20:05:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-23T20:05:25Z.

Review: CHANGES_REQUESTED


The full Build on this head fails agc_driver_resident_heap_mirror on the Linux job (lavapipe, run 40011794592), in the new PartialImportTests:

  • ResidentHeapMirror.cpp:294: partial heap import: its mirror holds 131072 bytes, expected 65536. Log: host import budget: 0 MiB live of 0 MiB ... 1 imports (8 MiB) refused so far. The test is new in this PR, so main has no equivalent failure.
  • On lavapipe the sparse mirror of the tail accounts two blocks where the RTX 4070 run accounts one. Either the imported prefix or the sparse block granularity differs per driver, and the test pins Block for the non-copied case.

Please check which it is: if the code selects a different prefix/tail on lavapipe, fix it there; if only the granularity differs, derive the expected bytes from the imported prefix and the device's sparse block size instead of the fixed Block. The copied case (expected = HeapBytes - 2 * Block) needs the same check.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633829785 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8380#pullrequestreview-5633829785). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-23T20:05:25Z. **Review:** CHANGES_REQUESTED --- The full Build on this head fails `agc_driver_resident_heap_mirror` on the Linux job (lavapipe, run 40011794592), in the new `PartialImportTests`: - `ResidentHeapMirror.cpp:294`: `partial heap import: its mirror holds 131072 bytes, expected 65536`. Log: `host import budget: 0 MiB live of 0 MiB ... 1 imports (8 MiB) refused so far`. The test is new in this PR, so main has no equivalent failure. - On lavapipe the sparse mirror of the tail accounts two blocks where the RTX 4070 run accounts one. Either the imported prefix or the sparse block granularity differs per driver, and the test pins `Block` for the non-copied case. Please check which it is: if the code selects a different prefix/tail on lavapipe, fix it there; if only the granularity differs, derive the expected bytes from the imported prefix and the device's sparse block size instead of the fixed `Block`. The copied case (`expected = HeapBytes - 2 * Block`) needs the same check.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!8380
No description provided.