fix(agc): bound a strided buffer's rounded-up last record by its allocation #2912

Merged
TheAggie merged 3 commits from upstream-pr/2912 into main 2026-10-10 23:42:57 +00:00
TheAggie commented 2026-10-10 18:24:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-10T18:24:40Z.


What

RADV rounds a strided buffer descriptor's record count up, so stride * records can run up to one stride past the end of the buffer's allocation. That range then no longer fits inside the allocation's host import, and GuestBufferMemory copies it on the CPU, whole, on every draw that binds it.

RetroArch's Dolphin core (PPSA99169) does this on every draw:

  • it binds a 48 MiB stream buffer, 0x4009200000+0x3000000, with stride 20 and records = 0x266667;
  • 0x3000000 / 20 = 2516582.4, rounded up to 2516583, gives 0x300000c, 12 bytes past the allocation;
  • the allocation itself is host-imported (host import of 0x4009200000+0x3000000 ok), but the descriptor's range isn't contained in it;
  • APS5_PROFILE_DRAW shows 14000 copied regions on the CPU: ... 14000 outside imports (672000 MiB) after about 60 s, all of them this one range;
  • one thread spends nearly all its time in copyRegion → GuestMemory::Read → memmove.

The fix: when a strided descriptor ends less than one stride past the end of the readable registered allocation that contains its base, the range is bound up to the allocation's end.

  • Why it's safe: the bytes past the allocation can only belong to the rounded-up last record, and no fetch inside the buffer reads them.
  • Cost: the registry is consulted only when the range ends less than one stride past a 16 KiB page boundary, so other descriptors pay nothing.
  • Code: RegisteredReadableEnd sits next to RegisteredReadableCovers in GuestBufferMemory.

Tested

  • Unit test: agc_driver_graphics (BdaResources.cpp, stridedOverhangTests) binds a stride-20 descriptor over a registered 32 KiB allocation with the record count rounded up. It checks that the bound range ends at the allocation, and that a descriptor inside its allocation keeps its length. With the ShaderResources.cpp change reverted, the test fails with a strided buffer's rounded-up last record was bound past its allocation.
  • Suite: agc|shader|recompiler|guest|bda: 553 passed, 1 skipped, 1 failed. The failure is guest_memory, which fails the same way on main here (CheckGuestModuleImageProtection's dlopen).
  • RetroArch: Dolphin core with the GameCube 240p Test Suite went from 0.7-0.8 FPS to 27-29 FPS. The menu renders the same as before.
    • Caveat: RetroArch can't run the Dolphin core on main alone yet. This measurement is from my integration branch: a main from 2026-10-07 plus my open PRs, #423 and local changes, with this commit cherry-picked unchanged.
  • Setup: Windows 11, MinGW-w64 GCC 15.2.0, RTX 5070 Ti.

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 (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4813466237 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2912). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-10T18:24:40Z. --- ### What RADV rounds a strided buffer descriptor's record count up, so `stride * records` can run up to one stride past the end of the buffer's allocation. That range then no longer fits inside the allocation's host import, and `GuestBufferMemory` copies it on the CPU, whole, on every draw that binds it. RetroArch's Dolphin core (PPSA99169) does this on every draw: - it binds a 48 MiB stream buffer, `0x4009200000+0x3000000`, with stride 20 and `records = 0x266667`; - 0x3000000 / 20 = 2516582.4, rounded up to 2516583, gives `0x300000c`, 12 bytes past the allocation; - the allocation itself is host-imported (`host import of 0x4009200000+0x3000000 ok`), but the descriptor's range isn't contained in it; - `APS5_PROFILE_DRAW` shows `14000 copied regions on the CPU: ... 14000 outside imports (672000 MiB)` after about 60 s, all of them this one range; - one thread spends nearly all its time in `copyRegion` → `GuestMemory::Read` → `memmove`. The fix: when a strided descriptor ends less than one stride past the end of the readable registered allocation that contains its base, the range is bound up to the allocation's end. - **Why it's safe:** the bytes past the allocation can only belong to the rounded-up last record, and no fetch inside the buffer reads them. - **Cost:** the registry is consulted only when the range ends less than one stride past a 16 KiB page boundary, so other descriptors pay nothing. - **Code:** `RegisteredReadableEnd` sits next to `RegisteredReadableCovers` in `GuestBufferMemory`. ### Tested - **Unit test:** `agc_driver_graphics` (`BdaResources.cpp`, `stridedOverhangTests`) binds a stride-20 descriptor over a registered 32 KiB allocation with the record count rounded up. It checks that the bound range ends at the allocation, and that a descriptor inside its allocation keeps its length. With the `ShaderResources.cpp` change reverted, the test fails with `a strided buffer's rounded-up last record was bound past its allocation`. - **Suite:** `agc|shader|recompiler|guest|bda`: 553 passed, 1 skipped, 1 failed. The failure is `guest_memory`, which fails the same way on `main` here (`CheckGuestModuleImageProtection`'s `dlopen`). - **RetroArch:** Dolphin core with the GameCube 240p Test Suite went from 0.7-0.8 FPS to 27-29 FPS. The menu renders the same as before. - Caveat: RetroArch can't run the Dolphin core on `main` alone yet. This measurement is from my integration branch: a `main` from 2026-10-07 plus my open PRs, #423 and local changes, with this commit cherry-picked unchanged. - **Setup:** Windows 11, MinGW-w64 GCC 15.2.0, RTX 5070 Ti. ### 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 (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
fma-rc commented 2026-10-10 22:09:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-10T22:09:56Z.


Ran 9a469b0e on Linux (GCC 16.2.1), merged onto main dc105f8a in a branch with eight other open PRs; agc_driver_graphics passes on an RTX 5080 and on RADV (Radeon 610M).

One case where the clamp cuts bytes that are backed: in stridedOverhangTests I registered a second readable allocation of 0x8000 bytes directly after the first one (mutation.Add(block + allocation, allocation, true, false, true)) and bound the same descriptor again (stride 20, 1639 records, 32780 bytes). The bound range is still 32768 on both GPUs, so the last record's 12 bytes in the second allocation are left out. RegisteredReadableEnd only looks up the allocation that holds the base address.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6102708320 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2912#issuecomment-6102708320). Originally posted by [fma-rc](https://github.com/fma-rc) on 2026-10-10T22:09:56Z. --- Ran 9a469b0e on Linux (GCC 16.2.1), merged onto main dc105f8a in a branch with eight other open PRs; `agc_driver_graphics` passes on an RTX 5080 and on RADV (Radeon 610M). One case where the clamp cuts bytes that are backed: in `stridedOverhangTests` I registered a second readable allocation of 0x8000 bytes directly after the first one (`mutation.Add(block + allocation, allocation, true, false, true)`) and bound the same descriptor again (stride 20, 1639 records, 32780 bytes). The bound range is still 32768 on both GPUs, so the last record's 12 bytes in the second allocation are left out. `RegisteredReadableEnd` only looks up the allocation that holds the base address.
adriadam10 commented 2026-10-10 23:22:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T23:22:06Z.

Review: CHANGES_REQUESTED


Thanks, the approach is sound and stridedOverhangTests fails without the change here too. One thing to fix, already reproduced by @fma-rc:

  • core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp:2671 clamps to RegisteredReadableEnd(address), which only looks at the allocation holding the base. When another readable registered allocation starts right at that end, the last record's bytes are backed but get cut. Clamp only when the bytes past the allocation end are not covered, e.g. !RegisteredReadableCovers(allocationEnd, end - allocationEnd), and add fma-rc's adjacent-allocation case (second 0x8000 allocation right after the first; the bound range must stay 32780) to stridedOverhangTests.

Please also fill in the FPS numbers' setup in Tested once this is rebased on current main if you can re-measure; the current ones come from an integration branch.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481169464 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2912#pullrequestreview-5481169464). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T23:22:06Z. **Review:** CHANGES_REQUESTED --- Thanks, the approach is sound and `stridedOverhangTests` fails without the change here too. One thing to fix, already reproduced by @fma-rc: - `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp:2671` clamps to `RegisteredReadableEnd(address)`, which only looks at the allocation holding the base. When another readable registered allocation starts right at that end, the last record's bytes are backed but get cut. Clamp only when the bytes past the allocation end are not covered, e.g. `!RegisteredReadableCovers(allocationEnd, end - allocationEnd)`, and add fma-rc's adjacent-allocation case (second `0x8000` allocation right after the first; the bound range must stay 32780) to `stridedOverhangTests`. Please also fill in the FPS numbers' setup in **Tested** once this is rebased on current `main` if you can re-measure; the current ones come from an integration branch.
TheAggie commented 2026-10-10 23:28:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-10T23:28:00Z.


Thanks @fma-rc, good catch, and it's deliberate. I tried exactly that change: clamp only when no readable allocation starts at the end of the first one. It breaks the case this PR is for.

In RetroArch's Dolphin core, the 48 MiB stream buffer at 0x4009200000 is followed directly by another registered, readable allocation at 0x400c200000. The descriptor's overhang (+0x300000c) even merges with a descriptor region there (0x400c200008+0x10). With the extra check, the clamp no longer applies, and Dolphin is back at 0.74 FPS (27-29 FPS with the PR as it is).

So being backed by a neighbour doesn't separate your case from that one. What does separate them is that the bytes past the allocation aren't part of the buffer the records describe:

  • RADV rounds num_records up so that an attribute fetch inside the last vertex works.
  • A fetch that reaches past the buffer's end is out of bounds in Vulkan terms.
  • So leaving the neighbour's 12 bytes out of the range only drops reads the guest isn't allowed to make.

I've pushed f6e16ceb, which pins that in stridedOverhangTests: with a second readable allocation right after the first, the bound range still ends at the first allocation.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103314065 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2912#issuecomment-6103314065). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-10T23:28:00Z. --- Thanks @fma-rc, good catch, and it's deliberate. I tried exactly that change: clamp only when no readable allocation starts at the end of the first one. It breaks the case this PR is for. In RetroArch's Dolphin core, the 48 MiB stream buffer at `0x4009200000` is followed directly by another registered, readable allocation at `0x400c200000`. The descriptor's overhang (`+0x300000c`) even merges with a descriptor region there (`0x400c200008+0x10`). With the extra check, the clamp no longer applies, and Dolphin is back at 0.74 FPS (27-29 FPS with the PR as it is). So being backed by a neighbour doesn't separate your case from that one. What does separate them is that the bytes past the allocation aren't part of the buffer the records describe: - RADV rounds `num_records` up so that an attribute fetch inside the last vertex works. - A fetch that reaches past the buffer's end is out of bounds in Vulkan terms. - So leaving the neighbour's 12 bytes out of the range only drops reads the guest isn't allowed to make. I've pushed f6e16ceb, which pins that in `stridedOverhangTests`: with a second readable allocation right after the first, the bound range still ends at the first allocation. AI-assisted (Claude Code).
adriadam10 commented 2026-10-10 23:39:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T23:39:59Z.

Review: APPROVED


Checked on the PR rebased onto current main: build + ctest of libSceAgcDriver green, and agc_driver_graphics fails without the ShaderResources.cpp change.

  • Withdrawing my earlier request about adjacent allocations: the Dolphin buffer is followed by another readable allocation, and clamping there is what removes the CPU copies. The bytes past the allocation are only the rounded-up num_records of the last record, and stridedOverhangTests now pins it.
  • Added a TechnicalDebt line ("Strided buffer overhang", Unknown function info) recording that assumption: it is not measured on hardware.

Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481210859 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2912#pullrequestreview-5481210859). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T23:39:59Z. **Review:** APPROVED --- Checked on the PR rebased onto current `main`: build + ctest of libSceAgcDriver green, and `agc_driver_graphics` fails without the `ShaderResources.cpp` change. - Withdrawing my earlier request about adjacent allocations: the Dolphin buffer is followed by another readable allocation, and clamping there is what removes the CPU copies. The bytes past the allocation are only the rounded-up `num_records` of the last record, and `stridedOverhangTests` now pins it. - Added a TechnicalDebt line ("Strided buffer overhang", Unknown function info) recording that assumption: it is not measured on hardware. 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!2912
No description provided.