fix(agc): copy a sparse descriptor heap only up to its last backed byte #2992

Merged
CnRJay merged 1 commit from upstream-pr/2992 into main 2026-10-11 07:24:33 +00:00
CnRJay commented 2026-10-10 20:03:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-10T20:03:31Z.


What

PPSA28416 (STAR WARS: Galactic Racer, UE5) binds a 4 GiB GPU heap of which 528 MiB is committed. GuestBufferMemory copies a sparse region whole, so every dispatch that binds it allocates 4 GiB and fails:

AGC graphics: vkAllocateMemory buffer: Vulkan result -2

A sparse region that is copied (not imported, not mirrored) is now copied only up to the later of its last committed byte and the start of its last descriptor view (plus 4 bytes), rounded up to 256 bytes:

  • Region::lastView keeps the greatest view start merged into the region; Region::bufferEnd is the end of the shorter copy.
  • Descriptor clamps a view's range to the copy and throws if the view starts past it. addressRange ends the BDA range at the copy, so an address past it faults, as an uncommitted page does.
  • Gaps inside the copy are zeroed (pooled buffers keep old contents); committed pieces are read and written back as before.

The cut is safe for descriptors: every descriptor access in the generated SPIR-V is bounded by OpArrayLength (EmitMemoryElementInBounds), so the part past the copy reads 0 and drops stores, which is what uncommitted pages give.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, Release, RTX 4080.

  • agc_driver_graphics (sparseHeapTests in BdaResources.cpp): a 64 MiB reservation with its first 64 KiB committed, with device memory limited to 4 MiB. A descriptor build binds it whole: the view is 64 KiB, the copy holds the committed bytes, and a GPU store is written back. An address-based build with a second view at +1 MiB: that view is bound and reads zeros, the copy ends 256 bytes past it, and the BDA range is clamped to the copy. Without the fix it fails with vkAllocateMemory buffer: Vulkan result -2.
  • ctest -R "^agc_driver_graphics$|^agc_driver_bda|guest_agc": 4/4 pass. Full suite: 717/717 pass (6 VideoOut tests skipped, as on main).
  • PPSA28416: the per-dispatch allocation failure is gone.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4814071761 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2992). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-10T20:03:31Z. --- ### What PPSA28416 (STAR WARS: Galactic Racer, UE5) binds a 4 GiB GPU heap of which 528 MiB is committed. `GuestBufferMemory` copies a sparse region whole, so every dispatch that binds it allocates 4 GiB and fails: ``` AGC graphics: vkAllocateMemory buffer: Vulkan result -2 ``` A sparse region that is copied (not imported, not mirrored) is now copied only up to the later of its last committed byte and the start of its last descriptor view (plus 4 bytes), rounded up to 256 bytes: - `Region::lastView` keeps the greatest view start merged into the region; `Region::bufferEnd` is the end of the shorter copy. - `Descriptor` clamps a view's range to the copy and throws if the view starts past it. `addressRange` ends the BDA range at the copy, so an address past it faults, as an uncommitted page does. - Gaps inside the copy are zeroed (pooled buffers keep old contents); committed pieces are read and written back as before. The cut is safe for descriptors: every descriptor access in the generated SPIR-V is bounded by `OpArrayLength` (`EmitMemoryElementInBounds`), so the part past the copy reads 0 and drops stores, which is what uncommitted pages give. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Release, RTX 4080. - `agc_driver_graphics` (`sparseHeapTests` in `BdaResources.cpp`): a 64 MiB reservation with its first 64 KiB committed, with device memory limited to 4 MiB. A descriptor build binds it whole: the view is 64 KiB, the copy holds the committed bytes, and a GPU store is written back. An address-based build with a second view at +1 MiB: that view is bound and reads zeros, the copy ends 256 bytes past it, and the BDA range is clamped to the copy. Without the fix it fails with `vkAllocateMemory buffer: Vulkan result -2`. - `ctest -R "^agc_driver_graphics$|^agc_driver_bda|guest_agc"`: 4/4 pass. Full suite: 717/717 pass (6 VideoOut tests skipped, as on `main`). - PPSA28416: the per-dispatch allocation failure is gone. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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: none - [x] AI-assisted: yes
SleglJan commented 2026-10-10 22:00:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-10T22:00:19Z.


Linux cross-test of ab6104be. Setup: CachyOS (kernel 7.2.9, GCC 16.2.1), RTX 3080 (NVIDIA 615.71.09), Release.

Merged onto main 262c0316, SPIRV-Tools on
It merges cleanly. Full suite: of 711 tests, 706 pass, 4 are skipped and 1 fails.

  • Passing: agc_driver_graphics (with sparseHeapTests), agc_driver_bda_device and agc_driver_bda_span_reads.
  • Skipped: four video_out_flip_* tests, under ctest's offscreen SDL driver.
  • Failed: 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 GuestBufferMemory code, showed draw builds copying one or both of two sparse 512 MiB regions whole into new host-visible buffers, about 1 GiB per build. 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 ab6104be cherry-picked:

  • There's no jump. The lowest available memory is 27.5 GiB, peak RssShmem is 6.4 GiB against 35.4 GiB before, and the run lasts its full 240 s. Frames still stop at the unrelated stall from #2523.
  • There are no vkAllocateMemory errors or what(). The cold cache compiles 2,194 shader variants, and none logs could not be prepared.

Combined with #2283 there's no jump either: the lowest available memory is 27.2 GiB. With both applied, #2283's CopiedBytes() counts a copied region as region.end - region.begin, so a region cut here at bufferEnd is counted at its full size. The full table is in #2523.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6102628054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2992#issuecomment-6102628054). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-10T22:00:19Z. --- Linux cross-test of ab6104be. Setup: CachyOS (kernel 7.2.9, GCC 16.2.1), RTX 3080 (NVIDIA 615.71.09), Release. **Merged onto `main` 262c0316, SPIRV-Tools on** It merges cleanly. Full suite: of 711 tests, 706 pass, 4 are skipped and 1 fails. - Passing: `agc_driver_graphics` (with `sparseHeapTests`), `agc_driver_bda_device` and `agc_driver_bda_span_reads`. - Skipped: four `video_out_flip_*` tests, under ctest's offscreen SDL driver. - Failed: `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 `GuestBufferMemory` code, showed draw builds copying one or both of two sparse 512 MiB regions whole into new host-visible buffers, about 1 GiB per build. 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 ab6104be cherry-picked: - There's no jump. The lowest available memory is 27.5 GiB, peak RssShmem is 6.4 GiB against 35.4 GiB before, and the run lasts its full 240 s. Frames still stop at the unrelated stall from #2523. - There are no `vkAllocateMemory` errors or `what()`. The cold cache compiles 2,194 shader variants, and none logs `could not be prepared`. Combined with #2283 there's no jump either: the lowest available memory is 27.2 GiB. With both applied, #2283's `CopiedBytes()` counts a copied region as `region.end - region.begin`, so a region cut here at `bufferEnd` is counted at its full size. The full table is in [#2523](https://github.com/boykopovar/AnyPS5/issues/2523#issuecomment-6102597271). AI-assisted: yes (Claude Code)
Scramblrrrr commented 2026-10-11 01:03:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-11T01:03:39Z.


Windows combined cross-test: #2992 ab6104be + #2283 55b09c0f + buffer-only entry checks 0280ac32, integration cf6de015 (parent ed73f788). The four selected Vulkan-required tests pass on RTX 5070 Ti: graphics including sparseHeapTests, recorded kept bytes, recorder budget, host-import unmap.

I reproduced the shortened-copy accounting interaction reported by @SleglJan and pushed a minimal follow-up: github.com/Scramblrrrr/AnyPS5@cf6de015 . CopiedBytes() uses bufferEnd when present instead of the original end. Two assertions cover the first committed 64 KiB and the later descriptor extending the copy to 1 MiB + 256 bytes. Negative control without the accounting change fails a cut sparse heap is charged at its reserved size; positive all four selected tests pass. Conventions pass. Windows draw-snapshot subcase remains untested here; Slegl's independent Linux run covers it.

Happy for either author to fold this tiny integration follow-up with authorship preserved, depending on merge order. No competing sparse/accounting PR. The next 007 run is using cf6de015; results pending. AI-assisted (Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104018574 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2992#issuecomment-6104018574). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-11T01:03:39Z. --- Windows combined cross-test: #2992 **ab6104be** + #2283 **55b09c0f** + buffer-only entry checks **0280ac32**, integration **cf6de015** (parent ed73f788). The four selected Vulkan-required tests pass on RTX 5070 Ti: graphics including sparseHeapTests, recorded kept bytes, recorder budget, host-import unmap. I reproduced the shortened-copy accounting interaction reported by @SleglJan and pushed a minimal follow-up: https://github.com/Scramblrrrr/AnyPS5/commit/cf6de015 . `CopiedBytes()` uses `bufferEnd` when present instead of the original end. Two assertions cover the first committed 64 KiB and the later descriptor extending the copy to 1 MiB + 256 bytes. Negative control without the accounting change fails `a cut sparse heap is charged at its reserved size`; positive all four selected tests pass. Conventions pass. Windows draw-snapshot subcase remains untested here; Slegl's independent Linux run covers it. Happy for either author to fold this tiny integration follow-up with authorship preserved, depending on merge order. No competing sparse/accounting PR. The next 007 run is using cf6de015; results pending. AI-assisted (Codex).
oneandonlydean commented 2026-10-11 01:08:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T01:08:21Z.


Conflicts with main in GuestBufferMemory.cpp and BdaResources.cpp; please push the rebased commit from #3167 here. With #2283 merged, CopiedBytes() still counts a cut region as region.end - region.begin; it should stop at bufferEnd.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104052834 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2992#issuecomment-6104052834). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T01:08:21Z. --- Conflicts with main in `GuestBufferMemory.cpp` and `BdaResources.cpp`; please push the rebased commit from #3167 here. With #2283 merged, `CopiedBytes()` still counts a cut region as `region.end - region.begin`; it should stop at `bufferEnd`.
CnRJay commented 2026-10-11 01:20:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-11T01:20:53Z.


Merged current main into the branch (a fast-forward push, no force) and took the rebased cut from #3167 as the conflict resolution in GuestBufferMemory.cpp and BdaResources.cpp. CopiedBytes() now stops at bufferEnd for a cut region. agc_driver_graphics passes on Windows (MinGW, Release). This leaves a merge commit, so say if you would rather have it as a rebased branch and I will do that on your word.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104141948 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2992#issuecomment-6104141948). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-11T01:20:53Z. --- Merged current `main` into the branch (a fast-forward push, no force) and took the rebased cut from #3167 as the conflict resolution in `GuestBufferMemory.cpp` and `BdaResources.cpp`. `CopiedBytes()` now stops at `bufferEnd` for a cut region. `agc_driver_graphics` passes on Windows (MinGW, Release). This leaves a merge commit, so say if you would rather have it as a rebased branch and I will do that on your word. _🤖 Generated with [Claude Code](https://claude.com/claude-code)_
SleglJan commented 2026-10-11 01:41:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-11T01:41:59Z.


Linux re-test of dda893dc merged onto main d31b57a4, which already has #2283. Setup: CachyOS (kernel 7.2.9, GCC 16.2.1), RTX 3080 (NVIDIA 615.71.09), Release, SPIRV-Tools on.

  • Tests. Three repeats each pass for agc_driver_graphics, agc_driver_recorded_kept_bytes (with the draw-snapshot subcase running), agc_recorder_kept_bytes, agc_host_import_unmap, agc_driver_bda_device and agc_driver_bda_span_reads. Full suite: of 755 tests, 750 pass and 4 are skipped (video_out_flip_*, under the offscreen SDL driver). One fails: agc_driver_dcc_keys_first_write, which is known on this machine (#2767) and not touched by this PR.
  • One gap: nothing tests the shortened CopiedBytes() charge. With it set back to region.end - region.begin on this head, agc_driver_graphics still passes. @Scramblrrrr's cf6de015 adds two assertions to BdaResources.cpp (a cut sparse heap is charged at its reserved size / ... does not include its last view). Applied on top of this head, they pass 3/3 with the charge. With the charge set back, the test fails at the first one, a cut sparse heap is charged at its reserved size. @CnRJay, the charge came in with your merge resolution. Cherry-picking cf6de015's two test lines, as Scramblrrrr offered with his authorship kept, would put a test on it.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104292094 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2992#issuecomment-6104292094). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-11T01:41:59Z. --- Linux re-test of dda893dc merged onto `main` d31b57a4, which already has #2283. Setup: CachyOS (kernel 7.2.9, GCC 16.2.1), RTX 3080 (NVIDIA 615.71.09), Release, SPIRV-Tools on. - **Tests.** Three repeats each pass for `agc_driver_graphics`, `agc_driver_recorded_kept_bytes` (with the draw-snapshot subcase running), `agc_recorder_kept_bytes`, `agc_host_import_unmap`, `agc_driver_bda_device` and `agc_driver_bda_span_reads`. Full suite: of 755 tests, 750 pass and 4 are skipped (`video_out_flip_*`, under the offscreen SDL driver). One fails: `agc_driver_dcc_keys_first_write`, which is known on this machine (#2767) and not touched by this PR. - **One gap: nothing tests the shortened `CopiedBytes()` charge.** With it set back to `region.end - region.begin` on this head, `agc_driver_graphics` still passes. @Scramblrrrr's cf6de015 adds two assertions to `BdaResources.cpp` (`a cut sparse heap is charged at its reserved size` / `... does not include its last view`). Applied on top of this head, they pass 3/3 with the charge. With the charge set back, the test fails at the first one, `a cut sparse heap is charged at its reserved size`. @CnRJay, the charge came in with your merge resolution. Cherry-picking cf6de015's two test lines, as Scramblrrrr offered with his authorship kept, would put a test on it. AI-assisted: yes (Claude Code)
Silverash-i8 commented 2026-10-11 02:03:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-11T02:03:58Z.


Windows run on two more GPUs: Windows 11, WinLibs GCC 15.2.0 r7, Release, head merged onto main d31b57a4 alone, RTX 5060 Laptop and the Arrow Lake iGPU (ANYPS5_GPU=Intel).

  • agc_driver_bda_device and agc_driver_bda_span_reads (BdaResources.cpp) pass on both GPUs.
  • All agc_* tests: 339/339 on the RTX 5060. On the iGPU the only failures are the ones that also fail on plain main there (listed on #2114 and #3207).

It doesn't merge together with #3167 (same files), so I ran it alone.

AI-assisted (Claude Code); I ran this on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104448766 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2992#issuecomment-6104448766). Originally posted by [Silverash-i8](https://github.com/Silverash-i8) on 2026-10-11T02:03:58Z. --- Windows run on two more GPUs: Windows 11, WinLibs GCC 15.2.0 r7, Release, head merged onto `main` d31b57a4 alone, RTX 5060 Laptop and the Arrow Lake iGPU (`ANYPS5_GPU=Intel`). - `agc_driver_bda_device` and `agc_driver_bda_span_reads` (`BdaResources.cpp`) pass on both GPUs. - All `agc_*` tests: 339/339 on the RTX 5060. On the iGPU the only failures are the ones that also fail on plain `main` there (listed on #2114 and #3207). It doesn't merge together with #3167 (same files), so I ran it alone. AI-assisted (Claude Code); I ran this on my machine.
oneandonlydean commented 2026-10-11 05:05:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T05:05:41Z.


Yes, please rebase and force-push; the branch shouldn't carry a merge commit. While you're there, take the two assertions from Scramblrrrr's cf6de015: as SleglJan showed, nothing fails when the CopiedBytes() charge is set back.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105682337 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2992#issuecomment-6105682337). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T05:05:41Z. --- Yes, please rebase and force-push; the branch shouldn't carry a merge commit. While you're there, take the two assertions from Scramblrrrr's cf6de015: as SleglJan showed, nothing fails when the `CopiedBytes()` charge is set back.
adriadam10 commented 2026-10-11 07:14:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T07:14:53Z.


Checked on the PR merged onto current main (which now has #2283): scoped build + ctest of libSceAgcDriver, 354/354 pass. agc_driver_graphics (sparseHeapTests) fails without the change, so it pins the cut.

What I added, as one linear commit on top of main (net diff of your branch, merge commit dropped; it conflicted with main after #2283):

  • Two assertions on CopiedBytes() from @Scramblrrrr's cf6de015 (co-author credited). Setting the charge back to region.end - region.begin passed agc_driver_graphics before; now it fails.

Also confirmed by @SleglJan (Linux, RTX 3080) and @Scramblrrrr / @Silverash-i8 (Windows, RTX 5070 Ti / 5060 / Arrow Lake iGPU). The game itself (PPSA28416) is not verified here.

#3167 touches the same files and will need a rebase after this lands. Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106576538 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2992#issuecomment-6106576538). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T07:14:53Z. --- Checked on the PR merged onto current `main` (which now has #2283): scoped build + ctest of libSceAgcDriver, 354/354 pass. `agc_driver_graphics` (`sparseHeapTests`) fails without the change, so it pins the cut. What I added, as one linear commit on top of `main` (net diff of your branch, merge commit dropped; it conflicted with `main` after #2283): - Two assertions on `CopiedBytes()` from @Scramblrrrr's cf6de015 (co-author credited). Setting the charge back to `region.end - region.begin` passed `agc_driver_graphics` before; now it fails. Also confirmed by @SleglJan (Linux, RTX 3080) and @Scramblrrrr / @Silverash-i8 (Windows, RTX 5070 Ti / 5060 / Arrow Lake iGPU). The game itself (PPSA28416) is not verified here. #3167 touches the same files and will need a rebase after this lands. Merging once CI is green.
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!2992
No description provided.