fix(agc): preserve all bytes in image-backed buffer copies #6249

Merged
Ondra97 merged 3 commits from upstream-pr/6249 into main 2026-10-27 01:56:04 +00:00
Ondra97 commented 2026-10-15 07:35:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-15T07:35:37Z.


What

I reproduced incorrect raw DMA copy results on main d92a8dad390af1807cc91622b924252141816a5a. A completed GPU dispatch leaves a cached 128×120 RGBA16F surface occupying 131,072 guest bytes. The image-copy shortcut copies its texels but leaves 8,192 destination bytes unchanged. A subsequent DMA_DATA reader of offset 0x15400 receives 0xa5a5a5a5 instead of the source payload 0x3b3a3938. The same packets with APS5_NO_COPY_ALIAS=1 copy every byte correctly. The packet's byte count follows the existing decoder and AMD PAL's BuildDmaData.

I require proven image coverage of the entire guest byte range before selecting the image alias. The proof reuses StorageTexture::uncoveredBytes, including its handling of tile edges, row pitch and mip gaps/tails. A range without complete proof uses the existing byte-transfer path.

The coverage guard is a plan.refusal in the shared PlanAlias, immediately after the source lookup and before any destination lookup or creation. AliasCopyPossible and AliasCopy therefore use the same coverage rule. The guard remains specific to raw buffer-copy alias selection; image-copy callers keep their existing semantics. Prerequisite #5480, including its no-device test-registration fix, is now merged into main. This rebase drops those carried commits and retains this PR's two commits, followed by a final main-update merge.

The PM4 regression covers a fresh padded destination, a padded destination with older pending image results, and a fully covered 128×128 surface. It compares every source/destination byte after GPU completion and includes a subsequent 4-byte DMA payload consumer. All guest allocations remain alive throughout the cases. The no-alias control runs the same packets.

The additional preflight regression uses a cached source image without a source host import and an imported destination. A padded source must be refused before execution; registering it then enables the correct buffer transfer. A fully covered source must still support an image alias. The no-alias control verifies the buffer path for both layouts. Moving the guard back into AliasCopy alone makes this regression fail.

The original overlap review covered 62 exact shared PR implementations, including full diffs for 21 large branches. The #5480 diff (9 files, +163/-55) and the combined guard/test changes were reviewed during the original integration; #5480 is now part of main. #6197 changes import coverage, #5998 retained-image budgeting and #5761 untracked snapshots; these do not preserve the missing raw bytes.

Tested

Original before/after measurements, captured on 2026-10-15 on the same PC and GPU workload, compare main d92a8dad390af1807cc91622b924252141816a5a with the original fix 9886b3d96dc20bd068a9fe752d6275fb315cc85e (Windows 11, GCC 15.2.0, NVIDIA Windows driver 32.0.16.1714, shader disk cache disabled):

Measurement Before (main) After (original fix)
Incorrect bytes, fresh padded destination (131,072-byte copy) 8,192 0
Incorrect bytes, cached/pending padded destination 8,192 0
Follow-up DMA payload in the original fresh-destination reproduction 0xa5a5a5a5 0x3b3a3938
Image aliases for the fully covered 128×128 positive control 1 1

Validation refreshed on 2026-10-20. Tested head cf459245b8b17b3e1bdcc7efc1440e7db03ad5ae includes main d5c956a10087914b52922fd3ece6439ccbb3e741. The prerequisite #5480 and its no-device test-registration fix are already merged; the topic retains its two original fix/regression commits plus a final main-update merge. The byte-coverage regression source is unchanged from the previous published head.

Windows (native host): Windows 11 Pro 10.0.26300, WinLibs MinGW-w64 GCC 15.2.0, Release; AMD Ryzen 9 7900X, 32 GB RAM, NVIDIA GeForce RTX 4070 Ti SUPER, Windows driver 32.0.16.1742 (NVIDIA 617.42).

  • On both tested revisions, relinker, libs and all executable targets needed by the focused tests built successfully. This includes the runtime and all patched libraries; unrelated test fixtures were not all built or run.
  • The 19 shared focused CTests ran on main 329b9aa1a7a413b562263f47a6b6a5863bfc205f and the final change. Subsequent main updates were inspected: AGC, shader and libc sources stayed identical, and the only libkernel change was in the POSIX implementation excluded on Windows. The main-side results remain the comparison for these unchanged Windows paths; the final change was rebuilt and tested after the update. Both byte-copy variants additionally ran on the change. Runs used ANYPS5_REQUIRE_VULKAN=1, ANYPS5_GPU=NVIDIA, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, verbose logging and serial execution on the same machine and GPU.
  • Khronos Validation Layer 1.4.335 was loaded from the process-local VK_LAYER_PATH; its DLL has a valid LunarG signature. The loader logs confirm actual layer insertion. Neither complete log contains a missing requested layer message or a VUID- validation error.
Validation Main Change
Shared focused CTests passed 19/19 19/19
Additional byte-copy CTests passed n/a 2/2
Failed or skipped tests 0 0
VUID- errors in the complete log 0 0
Missing requested validation layer messages 0 0

The padded PM4 cases reported zero incorrect bytes and payload 0x3b3a3938; the fully covered positive control retained one image alias. Direct padded preflight refused the unimported source and used buffer path 1 after registration; direct dense preflight used image path 3. The no-alias control used the buffer path for both layouts. agc_driver_small_copy_sync and upstream agc_driver_alias_copy_label passed. Patched-library validation loaded 352 libraries with zero failures.

Conventions, whitespace, main-sync and test-registration audits passed. The earlier guard-location mutation check was not repeated; the regression source remains unchanged.

After configuring the full Release build with BUILD_TESTING=ON and the supported MinGW toolchain, build the executable targets corresponding to the selected tests, plus relinker and libs. Run this shared pattern on both checkouts:

$env:ANYPS5_REQUIRE_VULKAN = "1"
$env:ANYPS5_GPU = "NVIDIA"
$env:ANYPS5_NO_SHADER_CACHE = "1"
$env:VK_LAYER_PATH = "C:\path\to\validation-layer"
$env:VK_INSTANCE_LAYERS = "VK_LAYER_KHRONOS_validation"
$env:VK_LOADER_DEBUG = "layer"
$shared = '^(agc_driver|agc_driver_small_copy_sync|agc_driver_alias_copy_label|agc_driver_copied_writer_copy|agc_driver_stream_copy|agc_space_pending_writes|agc_pending_write_snapshot|agc_pending_key_stores|agc_host_import_unmap|agc_host_import_retirement|agc_host_import_second_mapping|agc_copied_direct_ordering|agc_driver_pending_exact_surface|agc_driver_host_import_at_map|agc_driver_host_import_teardown|agc_driver_host_import_windows|agc_driver_graphics|agc_driver_pm4|patched_libraries)$'
ctest --test-dir build-windows --no-tests=error --output-on-failure -V -j 1 -R $shared

The full change-side pattern, including the two additional byte-copy cases, is:

$all = '^(agc_driver|agc_driver_small_copy_sync|agc_driver_copy_surface_bytes(_no_alias)?|agc_driver_alias_copy_label|agc_driver_copied_writer_copy|agc_driver_stream_copy|agc_space_pending_writes|agc_pending_write_snapshot|agc_pending_key_stores|agc_host_import_unmap|agc_host_import_retirement|agc_host_import_second_mapping|agc_copied_direct_ordering|agc_driver_pending_exact_surface|agc_driver_host_import_at_map|agc_driver_host_import_teardown|agc_driver_host_import_windows|agc_driver_graphics|agc_driver_pm4|patched_libraries)$'
ctest --test-dir build-windows --no-tests=error --output-on-failure -V -j 1 -R $all

Limits: no retail title, other GPU vendor or console timing run. Direct preflight and real PM4 packet tests establish byte-copy correctness for the exercised paths without a game compatibility or speed claim. Hosted full Windows jobs require collaborator dispatch under CONTRIBUTING.md; skipped jobs are not passes.

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 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: NVIDIA GeForce RTX 4070 Ti SUPER, Windows driver 32.0.16.1742, Windows 11 Pro 10.0.26300
  • AI-assisted: yes — Codex. I am responsible for the change.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4869859395 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6249). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-15T07:35:37Z. --- ### What I reproduced incorrect raw DMA copy results on main `d92a8dad390af1807cc91622b924252141816a5a`. A completed GPU dispatch leaves a cached 128×120 RGBA16F surface occupying 131,072 guest bytes. The image-copy shortcut copies its texels but leaves 8,192 destination bytes unchanged. A subsequent DMA_DATA reader of offset `0x15400` receives `0xa5a5a5a5` instead of the source payload `0x3b3a3938`. The same packets with `APS5_NO_COPY_ALIAS=1` copy every byte correctly. The packet's byte count follows the existing decoder and [AMD PAL's BuildDmaData](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/hw/gfxip/gfx9/gfx9CmdUtil.cpp#L2311). I require proven image coverage of the entire guest byte range before selecting the image alias. The proof reuses `StorageTexture::uncoveredBytes`, including its handling of tile edges, row pitch and mip gaps/tails. A range without complete proof uses the existing byte-transfer path. The coverage guard is a `plan.refusal` in the shared `PlanAlias`, immediately after the source lookup and before any destination lookup or creation. `AliasCopyPossible` and `AliasCopy` therefore use the same coverage rule. The guard remains specific to raw buffer-copy alias selection; image-copy callers keep their existing semantics. Prerequisite #5480, including its no-device test-registration fix, is now merged into main. This rebase drops those carried commits and retains this PR's two commits, followed by a final main-update merge. The PM4 regression covers a fresh padded destination, a padded destination with older pending image results, and a fully covered 128×128 surface. It compares every source/destination byte after GPU completion and includes a subsequent 4-byte DMA payload consumer. All guest allocations remain alive throughout the cases. The no-alias control runs the same packets. The additional preflight regression uses a cached source image without a source host import and an imported destination. A padded source must be refused before execution; registering it then enables the correct buffer transfer. A fully covered source must still support an image alias. The no-alias control verifies the buffer path for both layouts. Moving the guard back into `AliasCopy` alone makes this regression fail. The original overlap review covered 62 exact shared PR implementations, including full diffs for 21 large branches. The #5480 diff (9 files, +163/-55) and the combined guard/test changes were reviewed during the original integration; #5480 is now part of main. #6197 changes import coverage, #5998 retained-image budgeting and #5761 untracked snapshots; these do not preserve the missing raw bytes. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Original before/after measurements, captured on 2026-10-15 on the same PC and GPU workload, compare main `d92a8dad390af1807cc91622b924252141816a5a` with the original fix `9886b3d96dc20bd068a9fe752d6275fb315cc85e` (Windows 11, GCC 15.2.0, NVIDIA Windows driver 32.0.16.1714, shader disk cache disabled): | Measurement | Before (main) | After (original fix) | |---|---:|---:| | Incorrect bytes, fresh padded destination (131,072-byte copy) | 8,192 | 0 | | Incorrect bytes, cached/pending padded destination | 8,192 | 0 | | Follow-up DMA payload in the original fresh-destination reproduction | `0xa5a5a5a5` | `0x3b3a3938` | | Image aliases for the fully covered 128×128 positive control | 1 | 1 | Validation refreshed on 2026-10-20. Tested head `cf459245b8b17b3e1bdcc7efc1440e7db03ad5ae` includes main `d5c956a10087914b52922fd3ece6439ccbb3e741`. The prerequisite #5480 and its no-device test-registration fix are already merged; the topic retains its two original fix/regression commits plus a final main-update merge. The byte-coverage regression source is unchanged from the previous published head. **Windows (native host):** Windows 11 Pro 10.0.26300, WinLibs MinGW-w64 GCC 15.2.0, Release; AMD Ryzen 9 7900X, 32 GB RAM, NVIDIA GeForce RTX 4070 Ti SUPER, Windows driver 32.0.16.1742 (NVIDIA 617.42). - On both tested revisions, `relinker`, `libs` and all executable targets needed by the focused tests built successfully. This includes the runtime and all patched libraries; unrelated test fixtures were not all built or run. - The 19 shared focused CTests ran on main `329b9aa1a7a413b562263f47a6b6a5863bfc205f` and the final change. Subsequent main updates were inspected: AGC, shader and libc sources stayed identical, and the only libkernel change was in the POSIX implementation excluded on Windows. The main-side results remain the comparison for these unchanged Windows paths; the final change was rebuilt and tested after the update. Both byte-copy variants additionally ran on the change. Runs used `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_GPU=NVIDIA`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, verbose logging and serial execution on the same machine and GPU. - Khronos Validation Layer 1.4.335 was loaded from the process-local `VK_LAYER_PATH`; its DLL has a valid LunarG signature. The loader logs confirm actual layer insertion. Neither complete log contains a missing requested layer message or a `VUID-` validation error. | Validation | Main | Change | |---|---:|---:| | Shared focused CTests passed | 19/19 | 19/19 | | Additional byte-copy CTests passed | n/a | 2/2 | | Failed or skipped tests | 0 | 0 | | `VUID-` errors in the complete log | 0 | 0 | | Missing requested validation layer messages | 0 | 0 | The padded PM4 cases reported zero incorrect bytes and payload `0x3b3a3938`; the fully covered positive control retained one image alias. Direct padded preflight refused the unimported source and used buffer path 1 after registration; direct dense preflight used image path 3. The no-alias control used the buffer path for both layouts. `agc_driver_small_copy_sync` and upstream `agc_driver_alias_copy_label` passed. Patched-library validation loaded 352 libraries with zero failures. Conventions, whitespace, main-sync and test-registration audits passed. The earlier guard-location mutation check was not repeated; the regression source remains unchanged. After configuring the full Release build with `BUILD_TESTING=ON` and the supported MinGW toolchain, build the executable targets corresponding to the selected tests, plus `relinker` and `libs`. Run this shared pattern on both checkouts: ```powershell $env:ANYPS5_REQUIRE_VULKAN = "1" $env:ANYPS5_GPU = "NVIDIA" $env:ANYPS5_NO_SHADER_CACHE = "1" $env:VK_LAYER_PATH = "C:\path\to\validation-layer" $env:VK_INSTANCE_LAYERS = "VK_LAYER_KHRONOS_validation" $env:VK_LOADER_DEBUG = "layer" $shared = '^(agc_driver|agc_driver_small_copy_sync|agc_driver_alias_copy_label|agc_driver_copied_writer_copy|agc_driver_stream_copy|agc_space_pending_writes|agc_pending_write_snapshot|agc_pending_key_stores|agc_host_import_unmap|agc_host_import_retirement|agc_host_import_second_mapping|agc_copied_direct_ordering|agc_driver_pending_exact_surface|agc_driver_host_import_at_map|agc_driver_host_import_teardown|agc_driver_host_import_windows|agc_driver_graphics|agc_driver_pm4|patched_libraries)$' ctest --test-dir build-windows --no-tests=error --output-on-failure -V -j 1 -R $shared ``` The full change-side pattern, including the two additional byte-copy cases, is: ```powershell $all = '^(agc_driver|agc_driver_small_copy_sync|agc_driver_copy_surface_bytes(_no_alias)?|agc_driver_alias_copy_label|agc_driver_copied_writer_copy|agc_driver_stream_copy|agc_space_pending_writes|agc_pending_write_snapshot|agc_pending_key_stores|agc_host_import_unmap|agc_host_import_retirement|agc_host_import_second_mapping|agc_copied_direct_ordering|agc_driver_pending_exact_surface|agc_driver_host_import_at_map|agc_driver_host_import_teardown|agc_driver_host_import_windows|agc_driver_graphics|agc_driver_pm4|patched_libraries)$' ctest --test-dir build-windows --no-tests=error --output-on-failure -V -j 1 -R $all ``` Limits: no retail title, other GPU vendor or console timing run. Direct preflight and real PM4 packet tests establish byte-copy correctness for the exercised paths without a game compatibility or speed claim. Hosted full Windows jobs require collaborator dispatch under `CONTRIBUTING.md`; skipped jobs are not passes. ### 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) - [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: NVIDIA GeForce RTX 4070 Ti SUPER, Windows driver 32.0.16.1742, Windows 11 Pro 10.0.26300 - [x] AI-assisted: yes — Codex. I am responsible for the change.
oneandonlydean commented 2026-10-17 02:04:52 +00:00 (Migrated from github.com)

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


Heads-up on an overlap with #5480 (just rebased onto main): this PR's ImageCoversGuestBytes guard and #5480's PlanAlias refactor overlap. Whichever lands second moves the guard into PlanAlias after the source lookup as a plan.refusal, so AliasCopyPossible refuses the same copies AliasCopy does.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6228285231 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6249#issuecomment-6228285231). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-17T02:04:52Z. --- Heads-up on an overlap with #5480 (just rebased onto main): this PR's `ImageCoversGuestBytes` guard and #5480's `PlanAlias` refactor overlap. Whichever lands second moves the guard into `PlanAlias` after the source lookup as a `plan.refusal`, so `AliasCopyPossible` refuses the same copies `AliasCopy` does.
Ondra97 commented 2026-10-17 09:31:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-17T09:31:14Z.


I have integrated this in c0dcc05947 and declared #5480 as a prerequisite. The coverage guard is now a PlanAlias refusal immediately after the source lookup, shared by preflight and execution. The new regression fails when the guard is moved back into AliasCopy alone. Full validation results and platform limits are in the updated description. #5480 should land first.

AI-assisted: yes — Codex.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6232316327 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6249#issuecomment-6232316327). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-17T09:31:14Z. --- I have integrated this in c0dcc05947 and declared #5480 as a prerequisite. The coverage guard is now a `PlanAlias` refusal immediately after the source lookup, shared by preflight and execution. The new regression fails when the guard is moved back into `AliasCopy` alone. Full validation results and platform limits are in the updated description. #5480 should land first. AI-assisted: yes — Codex.
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!6249
No description provided.