fix(agc): copy only the dwords a shader changed back from a GPU-copied buffer #6185

Merged
ee6aba54 merged 20 commits from upstream-pr/6185 into main 2026-11-03 08:07:13 +00:00
ee6aba54 commented 2026-10-15 01:17:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ee6aba54 on 2026-10-15T01:17:12Z.


What

A buffer region that is staged in device memory (a region the GPU copies in and later copies back, GuestBufferMemory) was copied back whole when its batch completed, and the CPU write-back path stored every byte that differed from the uploaded bytes. A store the title made into the region after the upload, in bytes the shader never touched or in bytes it touched as well, was overwritten with the GPU's copy of the stale bytes. PPSA15304 corrupted its guest heap this way: the allocator's free-list link in a block it had released after waiting for its label was replaced by texel data, and the title faulted on the next allocation.

Two changes:

  • The write-back of a buffer region the CPU copied for a dispatch uses GuestMemory::WriteChangedKeepingGuest, which stores a byte only where the result differs from the uploaded byte and guest memory still holds the uploaded byte, so a CPU write made after the upload wins (the SSE path masks with both compares; the tail loop does the same). WriteChanged and its other callers (image mirrors, heap mirrors, storage images, tiled colour targets) are unchanged.
  • The GPU copy-in of a region the work writes keeps a snapshot of the uploaded bytes, and the copy-back runs a small compute kernel (StagingCopyBack.comp, buffer device addresses) that applies the same per-byte rule to each dword: the shader's bytes land where they differ from the snapshot and the import still holds the snapshot's byte. Unaligned edges copy as before. APS5_NO_CHANGED_COPY_BACK=1 restores the whole copies, APS5_CHANGED_COPY_ALWAYS=1 makes the kernel store every byte, and APS5_TRACE_COPY_BACK=1 counts the dwords where the shader and the CPU changed the same bytes.

Tested

Linux, Ubuntu 24.04, NVIDIA GeForce RTX 5080 (driver 595.99.02). Tests:

  • testWriteChangedKeepingGuestKeepsCpuWrites (tests/Pm4.cpp) for the per-byte rule of the CPU write-back.
  • agc_copied_write_back (tests/Recorder.cpp): a dispatch over a CPU-copied buffer, CPU stores after the batch completed and before the write-back (a conflicting dword, an untouched dword and a byte inside a dword the shader changed), checked after the write-back and on the next use of the buffer. It fails with WriteChanged in place of WriteChangedKeepingGuest.
  • changedCopyTests (tests/Recorder.cpp): 70000 dwords with whole-dword and single-byte changes on both sides; every byte is checked against the per-byte rule, and the trace counter against the dwords where both sides changed a byte.
  • bdaStagingTests: CPU stores into the import after the copy-in, one in a byte the fill leaves alone inside a filled dword and one outside the filled range, checked after the copy-back.

The tests of agc_driver_recorder_tests and agc_driver_pm4_tests pass on main (95) and on the change (97, agc_changed_copy and agc_copied_write_back added) with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_LAYER_KHRONOS_validation, with 0 VUID- lines in either log; the staging, changed-copy, copied write-back, PM4 and storage write-back tests also pass with APS5_NO_CHANGED_COPY_BACK=1. In PPSA15304 the earlier per-dword kernel ran through the section after the first drop with no divergence reported by the trace counter; the title's remaining heap corruption came from the deferred DCC key stores, fixed in the follow-up PR.

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; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: PPSA15304 (no Game bring-up discussion yet), together with the follow-up PR
  • GPUs tested: NVIDIA GeForce RTX 5080, driver 595.99.02, Ubuntu 24.04 (Linux)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4866769764 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6185). Originally posted by [ee6aba54](https://github.com/ee6aba54) on 2026-10-15T01:17:12Z. --- ### What A buffer region that is staged in device memory (a region the GPU copies in and later copies back, `GuestBufferMemory`) was copied back whole when its batch completed, and the CPU write-back path stored every byte that differed from the uploaded bytes. A store the title made into the region after the upload, in bytes the shader never touched or in bytes it touched as well, was overwritten with the GPU's copy of the stale bytes. PPSA15304 corrupted its guest heap this way: the allocator's free-list link in a block it had released after waiting for its label was replaced by texel data, and the title faulted on the next allocation. Two changes: - The write-back of a buffer region the CPU copied for a dispatch uses `GuestMemory::WriteChangedKeepingGuest`, which stores a byte only where the result differs from the uploaded byte and guest memory still holds the uploaded byte, so a CPU write made after the upload wins (the SSE path masks with both compares; the tail loop does the same). `WriteChanged` and its other callers (image mirrors, heap mirrors, storage images, tiled colour targets) are unchanged. - The GPU copy-in of a region the work writes keeps a snapshot of the uploaded bytes, and the copy-back runs a small compute kernel (`StagingCopyBack.comp`, buffer device addresses) that applies the same per-byte rule to each dword: the shader's bytes land where they differ from the snapshot and the import still holds the snapshot's byte. Unaligned edges copy as before. `APS5_NO_CHANGED_COPY_BACK=1` restores the whole copies, `APS5_CHANGED_COPY_ALWAYS=1` makes the kernel store every byte, and `APS5_TRACE_COPY_BACK=1` counts the dwords where the shader and the CPU changed the same bytes. ### Tested Linux, Ubuntu 24.04, NVIDIA GeForce RTX 5080 (driver 595.99.02). Tests: - `testWriteChangedKeepingGuestKeepsCpuWrites` (tests/Pm4.cpp) for the per-byte rule of the CPU write-back. - `agc_copied_write_back` (tests/Recorder.cpp): a dispatch over a CPU-copied buffer, CPU stores after the batch completed and before the write-back (a conflicting dword, an untouched dword and a byte inside a dword the shader changed), checked after the write-back and on the next use of the buffer. It fails with `WriteChanged` in place of `WriteChangedKeepingGuest`. - `changedCopyTests` (tests/Recorder.cpp): 70000 dwords with whole-dword and single-byte changes on both sides; every byte is checked against the per-byte rule, and the trace counter against the dwords where both sides changed a byte. - `bdaStagingTests`: CPU stores into the import after the copy-in, one in a byte the fill leaves alone inside a filled dword and one outside the filled range, checked after the copy-back. The tests of `agc_driver_recorder_tests` and `agc_driver_pm4_tests` pass on `main` (95) and on the change (97, `agc_changed_copy` and `agc_copied_write_back` added) with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_LAYER_KHRONOS_validation`, with 0 `VUID-` lines in either log; the staging, changed-copy, copied write-back, PM4 and storage write-back tests also pass with `APS5_NO_CHANGED_COPY_BACK=1`. In PPSA15304 the earlier per-dword kernel ran through the section after the first drop with no divergence reported by the trace counter; the title's remaining heap corruption came from the deferred DCC key stores, fixed in the follow-up PR. ### 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 - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: PPSA15304 (no Game bring-up discussion yet), together with the follow-up PR - [x] GPUs tested: NVIDIA GeForce RTX 5080, driver 595.99.02, Ubuntu 24.04 (Linux) - [x] AI-assisted: yes
ee6aba54 commented 2026-10-15 01:37:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ee6aba54 on 2026-10-15T01:37:06Z.


The overlap with #6182 is the option list in the test binary's main (tests/Recorder.cpp), both add a --...-only entry at the same spot; whichever lands second rebases in a minute, no dependency.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6182381553 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6185#issuecomment-6182381553). Originally posted by [ee6aba54](https://github.com/ee6aba54) on 2026-10-15T01:37:06Z. --- The overlap with #6182 is the option list in the test binary's main (tests/Recorder.cpp), both add a `--...-only` entry at the same spot; whichever lands second rebases in a minute, no dependency.
ps54all commented 2026-10-15 12:45:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ps54all on 2026-10-15T12:45:52Z.


On Windows, agc_changed_copy exits with 0xc0000135 (a DLL was not found). The other agc_driver_recorder_tests modes next to it, such as agc_unit_shadow, get the libs directory on PATH from the set_tests_properties(... ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:...") list, but agc_changed_copy is not in that list. Adding it there makes the test pass (Windows 11, WinLibs GCC 15.2.0, RTX 3080; checked with this PR's commits picked onto a local tree).

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6192385988 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6185#issuecomment-6192385988). Originally posted by [ps54all](https://github.com/ps54all) on 2026-10-15T12:45:52Z. --- On Windows, `agc_changed_copy` exits with 0xc0000135 (a DLL was not found). The other `agc_driver_recorder_tests` modes next to it, such as `agc_unit_shadow`, get the libs directory on PATH from the `set_tests_properties(... ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:...")` list, but `agc_changed_copy` is not in that list. Adding it there makes the test pass (Windows 11, WinLibs GCC 15.2.0, RTX 3080; checked with this PR's commits picked onto a local tree). AI-assisted (Claude Code).
ee6aba54 commented 2026-10-15 14:08:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ee6aba54 on 2026-10-15T14:08:59Z.


Thanks, confirmed. agc_changed_copy was missing from the Windows ENVIRONMENT_MODIFICATION list next to agc_unit_shadow. It is added in cbb060641 (rebased onto main since). #6186 adds agc_guarded_key_store to the same list, since it runs from the same binary. On Linux, CMake configures cleanly and both tests pass; I could not run the Windows side here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6194082527 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6185#issuecomment-6194082527). Originally posted by [ee6aba54](https://github.com/ee6aba54) on 2026-10-15T14:08:59Z. --- Thanks, confirmed. `agc_changed_copy` was missing from the Windows `ENVIRONMENT_MODIFICATION` list next to `agc_unit_shadow`. It is added in cbb060641 (rebased onto main since). #6186 adds `agc_guarded_key_store` to the same list, since it runs from the same binary. On Linux, CMake configures cleanly and both tests pass; I could not run the Windows side here.
oneandonlydean commented 2026-10-17 02:59:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T02:59:36Z.


CI run 38994492535 on head cbb060641: Linux is green. On Windows, the new agc_changed_copy fails. The runner has no Vulkan device, so it prints "skipped, no usable Vulkan device" and returns 77. Its set_tests_properties(agc_changed_copy PROPERTIES TIMEOUT 30) line lacks SKIP_RETURN_CODE 77, unlike its neighbours (agc_sparse_run_import, agc_unit_shadow), so ctest counts the skip as a failure. Suggested fix: set_tests_properties(agc_changed_copy PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 30).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6228787657 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6185#issuecomment-6228787657). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-17T02:59:36Z. --- CI run 38994492535 on head cbb060641: Linux is green. On Windows, the new `agc_changed_copy` fails. The runner has no Vulkan device, so it prints "skipped, no usable Vulkan device" and returns 77. Its `set_tests_properties(agc_changed_copy PROPERTIES TIMEOUT 30)` line lacks `SKIP_RETURN_CODE 77`, unlike its neighbours (`agc_sparse_run_import`, `agc_unit_shadow`), so ctest counts the skip as a failure. Suggested fix: `set_tests_properties(agc_changed_copy PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 30)`.
oneandonlydean commented 2026-10-20 15:35:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T15:35:55Z.


This now conflicts with main in GuestBufferMemory.cpp (plus a one-line test list in libSceAgcDriver/CMakeLists.txt). Two of the hunks aren't mechanical, so please rebase it yourself:

  • Copy source of an import. #5390 (imports whose backing offset is not 64 KiB aligned) changed the copy source to entry->base - entry->lead. Your change sets copySourceBase = entry->base and adds copySourceAddress = entry->address. Please re-derive both under the lead offset, so an unaligned import copies from the right bytes. A test with a non-zero lead would pin it.
  • Leading barrier before the staging copy. #7489 now skips that barrier when a trailing barrier already covers it: ElideCovered(StagingOut, leading, covered), with leading = ALL_COMMANDS -> TRANSFER, SHADER_WRITE -> TRANSFER_READ. Your version also makes compute a destination stage. If you keep compute, leading has to include it too. Otherwise the elision can drop a barrier your compute write-back needs, whenever the covering barrier only reached transfer.

After the rebase, the Windows agc_changed_copy run and the paired gate run on the card are still the bar.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6294699069 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6185#issuecomment-6294699069). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T15:35:55Z. --- This now conflicts with `main` in `GuestBufferMemory.cpp` (plus a one-line test list in `libSceAgcDriver/CMakeLists.txt`). Two of the hunks aren't mechanical, so please rebase it yourself: - **Copy source of an import.** #5390 (imports whose backing offset is not 64 KiB aligned) changed the copy source to `entry->base - entry->lead`. Your change sets `copySourceBase = entry->base` and adds `copySourceAddress = entry->address`. Please re-derive both under the `lead` offset, so an unaligned import copies from the right bytes. A test with a non-zero `lead` would pin it. - **Leading barrier before the staging copy.** #7489 now skips that barrier when a trailing barrier already covers it: `ElideCovered(StagingOut, leading, covered)`, with `leading` = ALL_COMMANDS -> TRANSFER, SHADER_WRITE -> TRANSFER_READ. Your version also makes compute a destination stage. If you keep compute, `leading` has to include it too. Otherwise the elision can drop a barrier your compute write-back needs, whenever the covering barrier only reached transfer. After the rebase, the Windows `agc_changed_copy` run and the paired gate run on the card are still the bar.
ee6aba54 commented 2026-10-20 20:04:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ee6aba54 on 2026-10-20T20:04:23Z.


@oneandonlydean Rebased onto main 2185dbf92.

  • Copy source of an import: both now come from the start of the import's buffer, lead included: HostImportBufferStart returns {base - lead, address - lead}, and the region takes copySourceBase and copySourceAddress from it, so the offset address - copySourceBase is the same one HostImportOffset gives. agc_changed_copy now also runs the kernel into a buffer with a 0x1000-byte lead, addressed through HostImportBufferStart of an import with that lead: the lead bytes stay untouched and the words land after it. With the helper returning {base, address} instead, the test fails.
  • Leading barrier: kept compute, and leading now carries it: ALL_COMMANDS -> TRANSFER | COMPUTE_SHADER, SHADER_WRITE -> TRANSFER_READ | SHADER_READ, passed to ElideCovered and recorded from the same mask. OrderedByTrailing requires SHADER_READ in the covered access, and the barriers that mark coverage (MarkCovered in the fills, the shadow publish, staging in and staging out) all have ALL_COMMANDS as their destination stage.
  • docs/dev/ENVIRONMENT.md now lists APS5_NO_CHANGED_COPY_BACK, APS5_TRACE_COPY_BACK and APS5_CHANGED_COPY_ALWAYS; agc_changed_copy has SKIP_RETURN_CODE 77, so a run without the kernel counts as skipped.

Tested on Linux, Release, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, same machine and GPU; "this head" is #6186's head, which carries this PR:

Tests main 2185dbf92 this head
agc_changed_copy, agc_guarded_key_store (not on main) pass
agc_recorder_read_list, agc_host_import_unmap, agc_parked_import_unmap, agc_host_import_retirement, agc_sparse_run_import, agc_sparse_run_import_copied, agc_draw_inputs_in_place, agc_draw_inputs_copied, agc_unit_shadow, agc_driver_graphics 10 of 10 pass 10 of 10 pass
VUID- lines 0 0

On the previous base 434e965d4 (same commits), all 825 agc tests passed in a parallel run except agc_driver_lookup_log_fits_build, which passes when run alone. agc_host_import_sparse skips on this machine (/dev/udmabuf not accessible). check_conventions.py passes on both branches. #6186 is rebased onto this head.

Ubuntu 24.04, kernel 7.0.0, NVIDIA GeForce RTX 5080, driver 595.99.02.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6300302922 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6185#issuecomment-6300302922). Originally posted by [ee6aba54](https://github.com/ee6aba54) on 2026-10-20T20:04:23Z. --- @oneandonlydean Rebased onto `main` `2185dbf92`. - **Copy source of an import:** both now come from the start of the import's buffer, `lead` included: `HostImportBufferStart` returns `{base - lead, address - lead}`, and the region takes `copySourceBase` and `copySourceAddress` from it, so the offset `address - copySourceBase` is the same one `HostImportOffset` gives. `agc_changed_copy` now also runs the kernel into a buffer with a 0x1000-byte lead, addressed through `HostImportBufferStart` of an import with that lead: the lead bytes stay untouched and the words land after it. With the helper returning `{base, address}` instead, the test fails. - **Leading barrier:** kept compute, and `leading` now carries it: `ALL_COMMANDS -> TRANSFER | COMPUTE_SHADER`, `SHADER_WRITE -> TRANSFER_READ | SHADER_READ`, passed to `ElideCovered` and recorded from the same mask. `OrderedByTrailing` requires `SHADER_READ` in the covered access, and the barriers that mark coverage (`MarkCovered` in the fills, the shadow publish, staging in and staging out) all have `ALL_COMMANDS` as their destination stage. - `docs/dev/ENVIRONMENT.md` now lists `APS5_NO_CHANGED_COPY_BACK`, `APS5_TRACE_COPY_BACK` and `APS5_CHANGED_COPY_ALWAYS`; `agc_changed_copy` has `SKIP_RETURN_CODE 77`, so a run without the kernel counts as skipped. Tested on Linux, Release, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, same machine and GPU; "this head" is #6186's head, which carries this PR: | Tests | `main` `2185dbf92` | this head | |---|---|---| | `agc_changed_copy`, `agc_guarded_key_store` | (not on `main`) | pass | | `agc_recorder_read_list`, `agc_host_import_unmap`, `agc_parked_import_unmap`, `agc_host_import_retirement`, `agc_sparse_run_import`, `agc_sparse_run_import_copied`, `agc_draw_inputs_in_place`, `agc_draw_inputs_copied`, `agc_unit_shadow`, `agc_driver_graphics` | 10 of 10 pass | 10 of 10 pass | | `VUID-` lines | 0 | 0 | On the previous base `434e965d4` (same commits), all 825 `agc` tests passed in a parallel run except `agc_driver_lookup_log_fits_build`, which passes when run alone. `agc_host_import_sparse` skips on this machine (`/dev/udmabuf` not accessible). `check_conventions.py` passes on both branches. #6186 is rebased onto this head. Ubuntu 24.04, kernel 7.0.0, NVIDIA GeForce RTX 5080, driver 595.99.02. AI-assisted: yes
ahmadmuj commented 2026-10-25 16:35:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T16:35:01Z.

Review: COMMENTED


Reviewed head ce22655 (merge base f26434a), merged locally onto current main 0119103 (clean). WSL2 Ubuntu 24.04, GCC 13.3, Release, lavapipe (llvmpipe, LLVM 20.1.2). No discrete GPU, no title.

What I ran:

  • Built agc_driver_recorder_tests and agc_driver_pm4_tests. All 85 ctest entries from those two binaries pass, agc_changed_copy included (agc_unit_shadow skips here).
  • --changed-copy-only, --sparse-run-copied-only and --draw-inputs-copied-only with VK_LAYER_KHRONOS_validation: 0 VUID- lines.
  • Red check: with main's GuestMemory.cpp, agc_driver_pm4 fails at "write-back overwrote a byte the CPU changed since the upload", so testWriteChangedKeepsCpuWrites pins the CPU-side change.
  • A local printf in the new shadow loop and the kernel branch: across the recorder suite the kernel copy-back runs 5 times, all on writable staged regions.
  • check_conventions.py --base origin/main passes.

The kernel's addressing looks right to me: the target offset with lead, the 4-byte gate, the edge copies, and the added COMPUTE/SHADER bits in both barriers. A few things:

1. The snapshot is taken for every GPU-copied region, not only written ones. The loop at GuestBufferMemory.cpp L5268 only checks changedCopyBacks and the device address. gpuCopyEligible also takes read-only regions (hostBacked || writable), and since gpuCopyUsage now adds SHADER_DEVICE_ADDRESS whenever the changed copy is on, those pass the address check. Each one gets an extra device-local buffer and a second CopyBuffer that RecordCopyBacks never reads; the comment above gpuCopiesEnabled counts ~31000 misaligned copies per run on this path. writable = WritesOverlap(...) is already computed per region at L5212; gating the shadow on it should be enough. This one is from reading the code, no test here hits a read-only GPU copy so I couldn't measure it.

2. The kernel compares whole dwords, WriteChanged compares bytes. Example: the import dword is 0x11223344 at copy-in, the shader stores 0x11AA3344, then the CPU stores byte 0 = 0x55. held is 0x11223355, not before, so the kernel keeps it and the shader's 0xAA is lost. The CPU path would keep both. A byte mask in StagingCopyBack.comp would match WriteChanged: mask = bytes where value != before and held == before, store (held & ~mask) | (value & mask) when mask != 0. Related: the trace counter (L23) also counts dwords the shader did change and the kernel dropped, but the message says "the shader left at the copy-in value".

3. agc_changed_copy tests the kernel only. It calls RecordChangedCopy directly, so the snapshot in recordGpuCopies and the copySourceAddress + (alignedFrom - copySourceBase) target in RecordCopyBacks aren't covered. The lead part passes start.device + (import.base - start.guest), which is just import.address. bdaStagingTests (tests/Recorder.cpp L3369) already drives memory.RecordCopyBacks on a staged region. A CPU store into the import after recording, checked after the batch, would pin the fix end to end.

4. The debt entry still describes the old behaviour. "Device-local staging" (docs/dev/debt/libSceAgcDriver.md L241) and the comment at GuestBufferMemory.cpp L4459 say the range is copied back whole and a CPU store is rolled back. Now that only holds for the edge bytes, regions whose begin or copySourceBase isn't 4-aligned, a refused shadow, and APS5_NO_CHANGED_COPY_BACK (plus the dword case in 2 if it stays).

5. Question on the other WriteChanged callers. The mirror write-back (GuestBufferMemory.cpp L5785) and the StorageTexture write-back (Texture.cpp L5879, through WriteChangedCommitted) set their baseline to the GPU result right after the store. With the new rule, a byte the CPU changed keeps the CPU value in guest memory while the mirror or image and its baseline hold the GPU value. Does the CPU's write still get picked up on the next use there, or can they stay apart? The description only covers the copied-buffer path, so a line on this would help either way.

Limits: lavapipe only, no PPSA15304, nothing measured on a discrete GPU.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644151896 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6185#pullrequestreview-5644151896). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T16:35:01Z. **Review:** COMMENTED --- Reviewed head ce22655 (merge base f26434a), merged locally onto current main 0119103 (clean). WSL2 Ubuntu 24.04, GCC 13.3, Release, lavapipe (llvmpipe, LLVM 20.1.2). No discrete GPU, no title. What I ran: - Built `agc_driver_recorder_tests` and `agc_driver_pm4_tests`. All 85 ctest entries from those two binaries pass, `agc_changed_copy` included (`agc_unit_shadow` skips here). - `--changed-copy-only`, `--sparse-run-copied-only` and `--draw-inputs-copied-only` with `VK_LAYER_KHRONOS_validation`: 0 `VUID-` lines. - Red check: with main's `GuestMemory.cpp`, `agc_driver_pm4` fails at "write-back overwrote a byte the CPU changed since the upload", so `testWriteChangedKeepsCpuWrites` pins the CPU-side change. - A local printf in the new shadow loop and the kernel branch: across the recorder suite the kernel copy-back runs 5 times, all on writable staged regions. - `check_conventions.py --base origin/main` passes. The kernel's addressing looks right to me: the target offset with `lead`, the 4-byte gate, the edge copies, and the added COMPUTE/SHADER bits in both barriers. A few things: **1. The snapshot is taken for every GPU-copied region, not only written ones.** The loop at [GuestBufferMemory.cpp L5268](https://github.com/boykopovar/AnyPS5/blob/ce22655ae2fcfbdbd6f24e6063baf080bceef41d/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp#L5268) only checks `changedCopyBacks` and the device address. `gpuCopyEligible` also takes read-only regions (`hostBacked || writable`), and since `gpuCopyUsage` now adds `SHADER_DEVICE_ADDRESS` whenever the changed copy is on, those pass the address check. Each one gets an extra device-local buffer and a second `CopyBuffer` that `RecordCopyBacks` never reads; the comment above `gpuCopiesEnabled` counts ~31000 misaligned copies per run on this path. `writable = WritesOverlap(...)` is already computed per region at [L5212](https://github.com/boykopovar/AnyPS5/blob/ce22655ae2fcfbdbd6f24e6063baf080bceef41d/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp#L5212); gating the shadow on it should be enough. This one is from reading the code, no test here hits a read-only GPU copy so I couldn't measure it. **2. The kernel compares whole dwords, `WriteChanged` compares bytes.** Example: the import dword is 0x11223344 at copy-in, the shader stores 0x11AA3344, then the CPU stores byte 0 = 0x55. `held` is 0x11223355, not `before`, so the kernel keeps it and the shader's 0xAA is lost. The CPU path would keep both. A byte mask in `StagingCopyBack.comp` would match `WriteChanged`: `mask` = bytes where `value != before` and `held == before`, store `(held & ~mask) | (value & mask)` when `mask != 0`. Related: the trace counter ([L23](https://github.com/boykopovar/AnyPS5/blob/ce22655ae2fcfbdbd6f24e6063baf080bceef41d/core/libs/prx/libSceAgcDriver/Graphics/shaders/StagingCopyBack.comp#L23)) also counts dwords the shader did change and the kernel dropped, but the message says "the shader left at the copy-in value". **3. `agc_changed_copy` tests the kernel only.** It calls `RecordChangedCopy` directly, so the snapshot in `recordGpuCopies` and the `copySourceAddress + (alignedFrom - copySourceBase)` target in `RecordCopyBacks` aren't covered. The lead part passes `start.device + (import.base - start.guest)`, which is just `import.address`. `bdaStagingTests` ([tests/Recorder.cpp L3369](https://github.com/boykopovar/AnyPS5/blob/ce22655ae2fcfbdbd6f24e6063baf080bceef41d/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp#L3369)) already drives `memory.RecordCopyBacks` on a staged region. A CPU store into the import after recording, checked after the batch, would pin the fix end to end. **4. The debt entry still describes the old behaviour.** "Device-local staging" ([docs/dev/debt/libSceAgcDriver.md L241](https://github.com/boykopovar/AnyPS5/blob/ce22655ae2fcfbdbd6f24e6063baf080bceef41d/docs/dev/debt/libSceAgcDriver.md#L241)) and the comment at [GuestBufferMemory.cpp L4459](https://github.com/boykopovar/AnyPS5/blob/ce22655ae2fcfbdbd6f24e6063baf080bceef41d/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp#L4459) say the range is copied back whole and a CPU store is rolled back. Now that only holds for the edge bytes, regions whose `begin` or `copySourceBase` isn't 4-aligned, a refused shadow, and `APS5_NO_CHANGED_COPY_BACK` (plus the dword case in 2 if it stays). **5. Question on the other `WriteChanged` callers.** The mirror write-back ([GuestBufferMemory.cpp L5785](https://github.com/boykopovar/AnyPS5/blob/ce22655ae2fcfbdbd6f24e6063baf080bceef41d/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp#L5785)) and the StorageTexture write-back ([Texture.cpp L5879](https://github.com/boykopovar/AnyPS5/blob/ce22655ae2fcfbdbd6f24e6063baf080bceef41d/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp#L5879), through `WriteChangedCommitted`) set their baseline to the GPU result right after the store. With the new rule, a byte the CPU changed keeps the CPU value in guest memory while the mirror or image and its baseline hold the GPU value. Does the CPU's write still get picked up on the next use there, or can they stay apart? The description only covers the copied-buffer path, so a line on this would help either way. Limits: lavapipe only, no PPSA15304, nothing measured on a discrete GPU.
CnRJay commented 2026-11-01 19:52:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:52:31Z.

Review: COMMENTED


Points on 6185:

  1. Known data loss accepted in a comment. GuestBufferMemory.cpp adds a comment block: "Accepted trade-off ... a CPU write between the copy-in and the copy-back is rolled back at the unaligned ends of a range, in a region that is not 4-byte aligned and without a shadow copy". That is a lost guest store for some inputs. The repo rule is exact-or-throw: either make the unaligned ends correct or throw for them and record the gap in docs/dev/debt/libSceAgcDriver.md. A comment is neither.

  2. Comments in code. The same block, and others in this diff, break the no-comments rule. Please remove them.

  3. Speed claim. The point of the change is a smaller copy-back. Please attach tools/trace_digest.py summaries of main and the change, same machine and scene, and say what the changed-dword detection costs per submission on a large buffer.

  4. Negative control. Does the new test fail with the fix reverted, and is there a case with a CPU store between copy-in and copy-back at an unaligned end?

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729932231 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6185#pullrequestreview-5729932231). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:52:31Z. **Review:** COMMENTED --- Points on 6185: 1. **Known data loss accepted in a comment.** `GuestBufferMemory.cpp` adds a comment block: "Accepted trade-off ... a CPU write between the copy-in and the copy-back is rolled back at the unaligned ends of a range, in a region that is not 4-byte aligned and without a shadow copy". That is a lost guest store for some inputs. The repo rule is exact-or-throw: either make the unaligned ends correct or throw for them and record the gap in `docs/dev/debt/libSceAgcDriver.md`. A comment is neither. 2. **Comments in code.** The same block, and others in this diff, break the no-comments rule. Please remove them. 3. **Speed claim.** The point of the change is a smaller copy-back. Please attach `tools/trace_digest.py` summaries of main and the change, same machine and scene, and say what the changed-dword detection costs per submission on a large buffer. 4. **Negative control.** Does the new test fail with the fix reverted, and is there a case with a CPU store between copy-in and copy-back at an unaligned end?
Superseabas commented 2026-11-02 18:17:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-02T18:17:01Z.

Review: COMMENTED


Windows run of head f0a15aeb5e merged onto current main b28bb77217 (clean merge, local only), on two GPUs, plus a read of the change against @CnRJay's points.

Windows 11 Home 10.0.26200, Ryzen 7 7700X, WinLibs GCC 16.2.0 (x86_64-ucrt-posix-seh, r3), Release, BUILD_TESTING=ON. ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, every test of agc_driver_recorder_tests and agc_driver_pm4_tests, ANYPS5_GPU selecting the device (confirmed by Selected GPU):

GPU Driver main merged
NVIDIA GeForce RTX 5070 Ti (10de:2c05) 621.01 132 of 132 134 of 134
AMD Radeon Graphics, Ryzen 7 7700X iGPU (1002:164e) AMD 26.10.1 132 of 132 134 of 134
  • No VUID- lines and no missing-layer line on either side.
  • agc_changed_copy, agc_copied_write_back and agc_bda_staging with APS5_TRACE_COPY_BACK=1, 3 runs per GPU: each run logs [copy-back] ... 1 dwords where the shader and the CPU changed the same bytes; the CPU's bytes were kept, and none logs staged write-backs copy their whole ranges, so the kernel path ran rather than the fallback.
  • check_conventions.py --base main passes on the merge.

On the earlier review points, at this head:

  1. The Accepted trade-off block is not new: main already has it, and it says a CPU write into a staged range is rolled back across the whole written element range. This PR edits it to the remaining case, the unaligned ends. The same case is written out in the Device-local staging entry of docs/dev/debt/libSceAgcDriver.md, so the gap is recorded there. Whether the code comment should go now that the debt entry carries it is for a maintainer; CONVENTIONS allows debt comments only from a human.
  2. The other comment change is also an edit of an existing main comment, not a new one.
  3. The PR does not claim a speed change, so the trace_digest.py rule for speed claims does not seem to apply.
  4. Negative control: the description says agc_copied_write_back fails with WriteChanged, and @ahmadmuj's red check reproduced the CPU-side one. The unaligned-end case is the recorded gap, so no test expects it to keep the CPU's bytes.

One question on the kernel: StagingCopyBack.comp reads the import's dword (held) and writes the whole dword back when stored != 0. A CPU store into another byte of that same dword between the read and the write is rolled back. The window is much smaller than the whole-range copy it replaces, but the debt entry says a CPU store in between survives. Would it be worth either a compare-and-swap loop on the target dword, or a sentence in the debt entry for that race?

Smaller: if the copy-back pipeline cannot be created, RecordChangedCopy prints [gpu] staged write-backs copy their whole ranges and the whole range is copied as before. The debt entry lists the other cases that fall back to the whole copy, but not this one.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5741232875 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6185#pullrequestreview-5741232875). Originally posted by [Superseabas](https://github.com/Superseabas) on 2026-11-02T18:17:01Z. **Review:** COMMENTED --- Windows run of head `f0a15aeb5e` merged onto current `main` `b28bb77217` (clean merge, local only), on two GPUs, plus a read of the change against @CnRJay's points. Windows 11 Home 10.0.26200, Ryzen 7 7700X, WinLibs GCC 16.2.0 (`x86_64-ucrt-posix-seh`, r3), Release, `BUILD_TESTING=ON`. `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, every test of `agc_driver_recorder_tests` and `agc_driver_pm4_tests`, `ANYPS5_GPU` selecting the device (confirmed by `Selected GPU`): | GPU | Driver | `main` | merged | |---|---|---|---| | NVIDIA GeForce RTX 5070 Ti (`10de:2c05`) | 621.01 | 132 of 132 | 134 of 134 | | AMD Radeon Graphics, Ryzen 7 7700X iGPU (`1002:164e`) | AMD 26.10.1 | 132 of 132 | 134 of 134 | - No `VUID-` lines and no missing-layer line on either side. - `agc_changed_copy`, `agc_copied_write_back` and `agc_bda_staging` with `APS5_TRACE_COPY_BACK=1`, 3 runs per GPU: each run logs `[copy-back] ... 1 dwords where the shader and the CPU changed the same bytes; the CPU's bytes were kept`, and none logs `staged write-backs copy their whole ranges`, so the kernel path ran rather than the fallback. - `check_conventions.py --base main` passes on the merge. On the earlier review points, at this head: 1. The `Accepted trade-off` block is not new: `main` already has it, and it says a CPU write into a staged range is rolled back across the whole written element range. This PR edits it to the remaining case, the unaligned ends. The same case is written out in the `Device-local staging` entry of `docs/dev/debt/libSceAgcDriver.md`, so the gap is recorded there. Whether the code comment should go now that the debt entry carries it is for a maintainer; CONVENTIONS allows debt comments only from a human. 2. The other comment change is also an edit of an existing `main` comment, not a new one. 3. The PR does not claim a speed change, so the `trace_digest.py` rule for speed claims does not seem to apply. 4. Negative control: the description says `agc_copied_write_back` fails with `WriteChanged`, and @ahmadmuj's red check reproduced the CPU-side one. The unaligned-end case is the recorded gap, so no test expects it to keep the CPU's bytes. One question on the kernel: `StagingCopyBack.comp` reads the import's dword (`held`) and writes the whole dword back when `stored != 0`. A CPU store into another byte of that same dword between the read and the write is rolled back. The window is much smaller than the whole-range copy it replaces, but the debt entry says a CPU store in between survives. Would it be worth either a compare-and-swap loop on the target dword, or a sentence in the debt entry for that race? Smaller: if the copy-back pipeline cannot be created, `RecordChangedCopy` prints `[gpu] staged write-backs copy their whole ranges` and the whole range is copied as before. The debt entry lists the other cases that fall back to the whole copy, but not this one. AI-assisted: yes (Claude Code)
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!6185
No description provided.