feat(libSceAgcDriver): GPU-readback staging for writable-copy write-back #23596

Open
nydal91 wants to merge 1 commit from upstream-pr/23596 into main
nydal91 commented 2026-11-19 09:50:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nydal91 on 2026-11-19T09:50:32Z.


What

RecordCopyBacks GPU-copies a writable copy region of at least APS5_COPY_READBACK_MIN_KIB (default 64 KiB) into a staging buffer at completion, and WriteBack diffs that staging instead of the write-combined copy buffer, so a large descriptor write-back no longer reads uncached memory at ~10 MiB/s. The heap-mirror readback path is unchanged (same eligibility, same RecordReadback); the loop is restructured to serve both kinds from one merged-writes pass. APS5_TRACE_COPY_BACKS traces, once per range of at least 1 MiB, why a readback was or was not recorded; APS5_TRACE_BIGBUF write-back lines gain readback=. The staging uses the default HOST_VISIBLE|HOST_COHERENT memory type: Context::BufferMemoryType prefers cached GTT but falls back to the non-cached VRAM aperture when that heap's budget is exhausted, so APS5_HOST_IMPORT_MIB must leave headroom (5000 MiB on a 16 GB / 8 GiB-GTT box; 6000 fills GTT and the stall returns). Debt fragment records the headroom sensitivity and the unmeasured console equivalence.

Tested

Linux PikaOS 4 (kernel 7.3.3), Intel Core Ultra 7 265KF, AMD Radeon RX 6950 XT (RADV Mesa 26.3.0), Vulkan 1.4.363, validation layer 1.4.363.

  • Focused ctest A/B, main (7c836568c8) vs this branch, same machine/GPU: ctest --test-dir build -R "agc_driver_.*(copy|write_back|writeback|staging|bigbuf|mirror)" -V --timeout 300 with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer. Both logs show Insert instance layer "VK_LAYER_KHRONOS_validation" (51 inserts each) and no missing-layer line; 36/36 passed in both arms, zero VUID- errors in either, no new VUID types or test+VUID pairs (work/vuid_compare.py comparison; logs kept locally, available on request).
  • Title run, Marvel's Wolverine PPSA03671 v01.001.005, 300 s at APS5_HOST_IMPORT_MIB=5000, 1920x1080 fifo: the 248 MiB descriptor write-back runs 110x with readback=1 and no stall; at 6000 MiB GTT fills and the ~10 MiB/s stall returns (frame ~490), as the debt fragment records.
  • Supplementary: the same code inside a fuller local bring-up stack passed a 6047-test ctest comparison against main with zero VUID- validation errors in either arm.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 6950 XT, RADV Mesa 26.3.0, Linux PikaOS 4 (kernel 7.3.3)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5392574268 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23596). Originally posted by [nydal91](https://github.com/nydal91) on 2026-11-19T09:50:32Z. --- ### What `RecordCopyBacks` GPU-copies a writable copy region of at least `APS5_COPY_READBACK_MIN_KIB` (default 64 KiB) into a staging buffer at completion, and `WriteBack` diffs that staging instead of the write-combined copy buffer, so a large descriptor write-back no longer reads uncached memory at ~10 MiB/s. The heap-mirror readback path is unchanged (same eligibility, same `RecordReadback`); the loop is restructured to serve both kinds from one merged-writes pass. `APS5_TRACE_COPY_BACKS` traces, once per range of at least 1 MiB, why a readback was or was not recorded; `APS5_TRACE_BIGBUF` write-back lines gain `readback=`. The staging uses the default `HOST_VISIBLE|HOST_COHERENT` memory type: `Context::BufferMemoryType` prefers cached GTT but falls back to the non-cached VRAM aperture when that heap's budget is exhausted, so `APS5_HOST_IMPORT_MIB` must leave headroom (5000 MiB on a 16 GB / 8 GiB-GTT box; 6000 fills GTT and the stall returns). Debt fragment records the headroom sensitivity and the unmeasured console equivalence. ### Tested Linux PikaOS 4 (kernel 7.3.3), Intel Core Ultra 7 265KF, AMD Radeon RX 6950 XT (RADV Mesa 26.3.0), Vulkan 1.4.363, validation layer 1.4.363. - Focused ctest A/B, `main` (7c836568c8) vs this branch, same machine/GPU: `ctest --test-dir build -R "agc_driver_.*(copy|write_back|writeback|staging|bigbuf|mirror)" -V --timeout 300` with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer`. Both logs show `Insert instance layer "VK_LAYER_KHRONOS_validation"` (51 inserts each) and no missing-layer line; **36/36 passed in both arms, zero `VUID-` errors in either, no new VUID types or test+VUID pairs** (`work/vuid_compare.py` comparison; logs kept locally, available on request). - Title run, Marvel's Wolverine PPSA03671 v01.001.005, 300 s at `APS5_HOST_IMPORT_MIB=5000`, 1920x1080 fifo: the 248 MiB descriptor write-back runs 110x with `readback=1` and no stall; at 6000 MiB GTT fills and the ~10 MiB/s stall returns (frame ~490), as the debt fragment records. - Supplementary: the same code inside a fuller local bring-up stack passed a 6047-test ctest comparison against `main` with zero `VUID-` validation errors in either arm. ### 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 - [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) and the licence notice of code adapted from upstream (see AGENTS.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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 6950 XT, RADV Mesa 26.3.0, Linux PikaOS 4 (kernel 7.3.3) - [x] AI-assisted: yes
Jazeupoi commented 2026-11-19 10:18:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Jazeupoi on 2026-11-19T10:18:04Z.

Review: COMMENTED


Tested on NVIDIA, since the description's runs are on RADV.

Setup: Windows 11 Pro (build 26300), AMD Ryzen 5 3400G, NVIDIA GeForce RTX 2060 (driver 621.24), WinLibs GCC 15.2.0 r7, Release. Base main cac6e5714d with #22373 (it only adds setenv for the AGC tests on Windows); "this PR" is the same tree with head 16a62f705a merged. Your focused set, ctest -R "agc_driver_.*(copy|write_back|writeback|staging|bigbuf|mirror)" -V -j1 (36 tests), 3 runs each, with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer, ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1:

result layer inserted not found lines VUID-
base 36/36, 3/3 runs 51 per run 0 0
this PR 36/36, 3/3 runs 51 per run 0 0

To check that the new path runs on this driver, one more run of the same set on this PR with APS5_TRACE_COPY_BACKS=1: two ranges of at least 1 MiB took the staging readback (code=6, writable buffer region, not a heap mirror), a 16 MiB range in agc_driver_indexed_copy and one in agc_driver_stream_copy, and both tests passed. Ranges between 64 KiB and 1 MiB are not traced, so I can't say how many of those took it.

On point 1 of the review above: on this machine agc_driver_recorded_draw_staging runs and passes on both arms (it is not skipped), and the two existing tests above already send a 16 MiB writable copy through the staging buffer. None of the 36 fails on the base, though, so this does not give the failing-without-the-change test that was asked for.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5973111579 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23596#pullrequestreview-5973111579). Originally posted by [Jazeupoi](https://github.com/Jazeupoi) on 2026-11-19T10:18:04Z. **Review:** COMMENTED --- Tested on NVIDIA, since the description's runs are on RADV. **Setup:** Windows 11 Pro (build 26300), AMD Ryzen 5 3400G, NVIDIA GeForce RTX 2060 (driver 621.24), WinLibs GCC 15.2.0 r7, Release. Base `main` `cac6e5714d` with #22373 (it only adds `setenv` for the AGC tests on Windows); "this PR" is the same tree with head `16a62f705a` merged. Your focused set, `ctest -R "agc_driver_.*(copy|write_back|writeback|staging|bigbuf|mirror)" -V -j1` (36 tests), 3 runs each, with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer`, `ANYPS5_NO_SHADER_CACHE=1` and `ANYPS5_REQUIRE_VULKAN=1`: | | result | layer inserted | `not found` lines | `VUID-` | |---|---|---|---|---| | base | 36/36, 3/3 runs | 51 per run | 0 | 0 | | this PR | 36/36, 3/3 runs | 51 per run | 0 | 0 | To check that the new path runs on this driver, one more run of the same set on this PR with `APS5_TRACE_COPY_BACKS=1`: two ranges of at least 1 MiB took the staging readback (`code=6`, writable buffer region, not a heap mirror), a 16 MiB range in `agc_driver_indexed_copy` and one in `agc_driver_stream_copy`, and both tests passed. Ranges between 64 KiB and 1 MiB are not traced, so I can't say how many of those took it. On point 1 of the review above: on this machine `agc_driver_recorded_draw_staging` runs and passes on both arms (it is not skipped), and the two existing tests above already send a 16 MiB writable copy through the staging buffer. None of the 36 fails on the base, though, so this does not give the failing-without-the-change test that was asked for. AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-11-19 10:42:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T10:42:13Z.

Review: COMMENTED


Reviewed — no blocking findings

The changed code and documentation were inspected at head 16a62f705a2d90f2911c9c8eff66c96a7bef0326; I found no verified blocking defect.

What I ran

I inspected the prepared diff and the relevant changed-file sections using the pinned repository reader. No build or tests were run.

Results

Check Result Note
Changed-code review passed All four changed files inspected
Build and tests not run No command execution available
Negative control not run No test harness run

Findings

Blocking

None.

Non-blocking

None.

Sources

Changed implementation at the reviewed head

Not verified

Build and test results were not independently verified. The description reports test runs and performance observations; console equivalence is stated as unmeasured. The full GuestBufferMemory.cpp text was excluded from the review bundle; its changed sections were read through the pinned reader. The other changed files were read in the included bundle or at their changed sections.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5973453764 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23596#pullrequestreview-5973453764). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T10:42:13Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The changed code and documentation were inspected at head `16a62f705a2d90f2911c9c8eff66c96a7bef0326`; I found no verified blocking defect. ## What I ran I inspected the prepared diff and the relevant changed-file sections using the pinned repository reader. No build or tests were run. ## Results | Check | Result | Note | |---|---|---| | Changed-code review | passed | All four changed files inspected | | Build and tests | not run | No command execution available | | Negative control | not run | No test harness run | ## Findings ### Blocking None. ### Non-blocking None. ## Sources [Changed implementation at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/16a62f705a2d90f2911c9c8eff66c96a7bef0326/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp#L7328-L7404) ## Not verified Build and test results were not independently verified. The description reports test runs and performance observations; console equivalence is stated as unmeasured. The full `GuestBufferMemory.cpp` text was excluded from the review bundle; its changed sections were read through the pinned reader. The other changed files were read in the included bundle or at their changed sections. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 18:35:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T18:35:56Z.

Review: COMMENTED


A writable-copy readback can cause RecordCopyBacks to skip the barrier needed by a later changed-copy dispatch in the same call. The readback sets recording after a barrier with destination TRANSFER / TRANSFER_READ. The gpuCopy loop emits its barrier covering COMPUTE_SHADER / SHADER_READ only when recording is false. If the batch has both a staged writable copy and a changed copy, the compute dispatch can read shader results without the dependency that the changed-copy-only path supplies. The heap-readback path already shared this flag. The new writable-copy case makes the same problem possible without a heap mirror. Could the first barrier cover both consumers, or could the compute path track its dependency independently?

Source review of 16a62f705a. The local merge into current main conflicts in GuestBufferMemory.cpp.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6176082523 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23596#pullrequestreview-6176082523). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T18:35:56Z. **Review:** COMMENTED --- A writable-copy readback can cause `RecordCopyBacks` to skip the barrier needed by a later changed-copy dispatch in the same call. The readback sets `recording` after a barrier with destination `TRANSFER` / `TRANSFER_READ`. The `gpuCopy` loop emits its barrier covering `COMPUTE_SHADER` / `SHADER_READ` only when `recording` is false. If the batch has both a staged writable copy and a changed copy, the compute dispatch can read shader results without the dependency that the changed-copy-only path supplies. The heap-readback path already shared this flag. The new writable-copy case makes the same problem possible without a heap mirror. Could the first barrier cover both consumers, or could the compute path track its dependency independently? Source review of `16a62f705a`. The local merge into current `main` conflicts in `GuestBufferMemory.cpp`. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/23596:upstream-pr/23596'
git switch 'upstream-pr/23596'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/23596'
git switch 'upstream-pr/23596'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/23596'
git switch 'upstream-pr/23596'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/23596'
git switch 'main'
git merge --squash 'upstream-pr/23596'
git switch 'main'
git merge --ff-only 'upstream-pr/23596'
git switch 'main'
git merge 'upstream-pr/23596'
git push origin 'main'
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!23596
No description provided.