fix(agc): read-trap a pending image at a label only when it is host imported #20183

Open
XIIYAZ wants to merge 2 commits from upstream-pr/20183 into main
XIIYAZ commented 2026-11-14 19:37:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XIIYAZ on 2026-11-14T19:37:03Z.


What

On macOS, both agc_driver_label_ordered_flush cases fail:

agc_driver_label_ordered_flush_unimported: label ordered flush: the label was visible before the image store it follows
agc_driver_label_ordered_flush:            label ordered flush: no label store took the GPU-ordered flush over the host import

On macOS, StoreReadbackBeforeLabel read-traps a small pending image at a label instead of storing it. Its pages become PROT_NONE, and the first CPU read faults into ResolveLabelTrap, which stores the image through FlushGpuWrites. With both cases' label traps off (APS5_NO_LABEL_READ_TRAP=1), both pass.

--unimported (APS5_NO_HOST_IMPORT=1). The image has no host import, so its store is a GPU readback whose bytes reach guest memory only through the batch's completion write-back. A trace in ResolveLabelTrap shows the image no longer pending once FlushGpuWrites returned (pendingAfter=0), yet the read that faulted still found fewer than the 8 stored texels the test requires after the label. The resolver lets that read through without the results. With a host import, the GPU writes the image's bytes through the import, and the trapped read sees them. That is why the default case's 8 runs pass the same data check.

A pending image is now read-trapped at a label only when a host import covers it (HostImportCovers). Otherwise the label stores it, as it does on Linux and Windows, which orders it before the label.

Default case. Its check counts label stores that took the GPU-ordered flush (TakeOrderedSkipCounts().flushes). On macOS the image is trapped instead of flushed at the label, so that count stays 0, although the image is still ordered before the label: the data check passes. The case now runs with APS5_NO_LABEL_READ_TRAP=1 on macOS, so it checks the GPU-ordered flush there as it does elsewhere. The trap path keeps its own coverage in agc_small_tiled_shadow_label and the other Recorder.cpp label-trap cases.

Tested

Setup:

  • Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0
  • full Release build with -DCMAKE_OSX_ARCHITECTURES=x86_64; tests run under Rosetta
  • ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1
  • on main with the open macOS build fixes applied

Results:

  • agc_driver_label_ordered_flush and agc_driver_label_ordered_flush_unimported fail before this change and pass after it. agc_driver_label_ordered_flush_disabled passes before and after.
  • ctest -R 'label|shadow|recorder_' (57 tests) passes.
  • I ran ctest -R '^(agc|shader|job)' (1578 tests, -j 1) and compared it with the same run before this change: both cases are fixed and nothing that passed before fails because of this change. agc_driver_msaa_dcc_clear_2 failed once in that run; it is intermittent on this machine and failed the same way in earlier runs without this change.

Linux and Windows have no label traps and are unchanged.

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: Apple M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5315429331 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20183). Originally posted by [XIIYAZ](https://github.com/XIIYAZ) on 2026-11-14T19:37:03Z. --- ### What On macOS, both `agc_driver_label_ordered_flush` cases fail: ``` agc_driver_label_ordered_flush_unimported: label ordered flush: the label was visible before the image store it follows agc_driver_label_ordered_flush: label ordered flush: no label store took the GPU-ordered flush over the host import ``` On macOS, `StoreReadbackBeforeLabel` read-traps a small pending image at a label instead of storing it. Its pages become `PROT_NONE`, and the first CPU read faults into `ResolveLabelTrap`, which stores the image through `FlushGpuWrites`. With both cases' label traps off (`APS5_NO_LABEL_READ_TRAP=1`), both pass. **`--unimported` (`APS5_NO_HOST_IMPORT=1`).** The image has no host import, so its store is a GPU readback whose bytes reach guest memory only through the batch's completion write-back. A trace in `ResolveLabelTrap` shows the image no longer pending once `FlushGpuWrites` returned (`pendingAfter=0`), yet the read that faulted still found fewer than the 8 stored texels the test requires after the label. The resolver lets that read through without the results. With a host import, the GPU writes the image's bytes through the import, and the trapped read sees them. That is why the default case's 8 runs pass the same data check. A pending image is now read-trapped at a label only when a host import covers it (`HostImportCovers`). Otherwise the label stores it, as it does on Linux and Windows, which orders it before the label. **Default case.** Its check counts label stores that took the GPU-ordered flush (`TakeOrderedSkipCounts().flushes`). On macOS the image is trapped instead of flushed at the label, so that count stays 0, although the image is still ordered before the label: the data check passes. The case now runs with `APS5_NO_LABEL_READ_TRAP=1` on macOS, so it checks the GPU-ordered flush there as it does elsewhere. The trap path keeps its own coverage in `agc_small_tiled_shadow_label` and the other `Recorder.cpp` label-trap cases. ### Tested Setup: - Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0 - full Release build with `-DCMAKE_OSX_ARCHITECTURES=x86_64`; tests run under Rosetta - `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1` - on `main` with the open macOS build fixes applied Results: - `agc_driver_label_ordered_flush` and `agc_driver_label_ordered_flush_unimported` fail before this change and pass after it. `agc_driver_label_ordered_flush_disabled` passes before and after. - `ctest -R 'label|shadow|recorder_'` (57 tests) passes. - I ran `ctest -R '^(agc|shader|job)'` (1578 tests, `-j 1`) and compared it with the same run before this change: both cases are fixed and nothing that passed before fails because of this change. `agc_driver_msaa_dcc_clear_2` failed once in that run; it is intermittent on this machine and failed the same way in earlier runs without this change. Linux and Windows have no label traps and are unchanged. ### 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 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: Apple M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3) - [x] AI-assisted: yes
MarcZX10 commented 2026-11-14 20:21:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T20:21:57Z.

Review: APPROVED


Reviewed — looks good

The change restricts Apple label read traps to host imported textures and disables that trap for the ordered-flush test on Apple; the inspected code supports the stated intent.

What I ran

No commands or tests were run. Review was against head 1e1d7fd12c5c8f44dd58f9ba71e114fe58b4aef8 using repo_read.py.

Results

Check Result Note
Source review passed Changed logic and test configuration inspected
Tests not run Results in the PR description were author reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full text of core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp was excluded from the review bundle; only the relevant function range and the HostImportCovers implementation were read through repo_read.py. The changed CMake test file was read in full. No runtime behavior or reported test results were independently verified.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5906911880 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20183#pullrequestreview-5906911880). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-14T20:21:57Z. **Review:** APPROVED --- # Reviewed — looks good The change restricts Apple label read traps to host imported textures and disables that trap for the ordered-flush test on Apple; the inspected code supports the stated intent. ## What I ran No commands or tests were run. Review was against head `1e1d7fd12c5c8f44dd58f9ba71e114fe58b4aef8` using `repo_read.py`. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Changed logic and test configuration inspected | | Tests | not run | Results in the PR description were author reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The full text of `core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp` was excluded from the review bundle; only the relevant function range and the `HostImportCovers` implementation were read through `repo_read.py`. The changed CMake test file was read in full. No runtime behavior or reported test results were independently verified. ## To get this to a good state Nothing is required.
kurexw commented 2026-11-16 23:50:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-16T23:50:32Z.

Review: COMMENTED


Tested on macOS 27.2 / Apple M4 with MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1, validation layer 1.4.363), x86-64 build under Rosetta. This is the only GPU I tested.

Head 1e1d7fd12c merged alone onto main 3b17977ba6, compared with 3b17977ba6 itself. I ran the 29 AGC tests that fail on main on this Mac, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and VK_LOADER_DEBUG=layer, through ctest --test-dir build -R <those 29> -V, three runs per side. Every Vulkan test logged Insert instance layer "VK_LAYER_KHRONOS_validation" and none logged was not found but was requested by env var VK_INSTANCE_LAYERS.

test main (3 runs) this PR (3 runs)
agc_driver_label_ordered_flush Failed, Failed, Failed Passed, Passed, Passed
agc_driver_label_ordered_flush_unimported Failed, Failed, Failed Passed, Passed, Passed

The other 26 tests gave the same result on both sides.

agc_driver_label_after_import_copy is the one other difference: main 1 of 3 failed, this PR 3 of 3. Since this PR changes label read traps, I checked it further. A sequential 15-run repeat failed 15/15, but the machine was heavily loaded by a parallel build at the time. So I built main, this PR and #20172 side by side and ran their test binaries interleaved (A, B, C, A, B, C, ...), 20 runs each, so all three saw the same load:

failed / 20 message on failure
main 3b17977ba6 5 "round N: a CPU read after the label that followed the copy found K of 262144 destination bytes not yet copied"
this PR 2 same
#20172 0 —

So this PR doesn't make that test worse. It's main's existing MoltenVK label-visibility flake, which #20172 addresses. 0 VUID- lines in all 60 runs, and every run logged the layer insertion.

VUID- lines: identical per test on both sides (30 on main, 30 with this PR, over the three runs). The VUID- lines on both sides are main's existing VUID-VkDeviceCreateInfo-sType-unique (a duplicated VkPhysicalDevicePortabilitySubsetFeaturesKHR in the device pNext chain) and VUID-VkSamplerCreateInfo-samplerMipLodBias-04467; this PR adds none.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5930038018 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20183#pullrequestreview-5930038018). Originally posted by [kurexw](https://github.com/kurexw) on 2026-11-16T23:50:32Z. **Review:** COMMENTED --- Tested on macOS 27.2 / Apple M4 with MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1, validation layer 1.4.363), x86-64 build under Rosetta. This is the only GPU I tested. Head `1e1d7fd12c` merged alone onto main `3b17977ba6`, compared with `3b17977ba6` itself. I ran the 29 AGC tests that fail on main on this Mac, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` and `VK_LOADER_DEBUG=layer`, through `ctest --test-dir build -R <those 29> -V`, three runs per side. Every Vulkan test logged `Insert instance layer "VK_LAYER_KHRONOS_validation"` and none logged `was not found but was requested by env var VK_INSTANCE_LAYERS`. | test | main (3 runs) | this PR (3 runs) | | --- | --- | --- | | `agc_driver_label_ordered_flush` | Failed, Failed, Failed | Passed, Passed, Passed | | `agc_driver_label_ordered_flush_unimported` | Failed, Failed, Failed | Passed, Passed, Passed | The other 26 tests gave the same result on both sides. `agc_driver_label_after_import_copy` is the one other difference: main 1 of 3 failed, this PR 3 of 3. Since this PR changes label read traps, I checked it further. A sequential 15-run repeat failed 15/15, but the machine was heavily loaded by a parallel build at the time. So I built main, this PR and #20172 side by side and ran their test binaries interleaved (A, B, C, A, B, C, ...), 20 runs each, so all three saw the same load: | | failed / 20 | message on failure | | --- | --- | --- | | main `3b17977ba6` | 5 | "round N: a CPU read after the label that followed the copy found K of 262144 destination bytes not yet copied" | | this PR | 2 | same | | #20172 | 0 | — | So this PR doesn't make that test worse. It's main's existing MoltenVK label-visibility flake, which #20172 addresses. 0 `VUID-` lines in all 60 runs, and every run logged the layer insertion. `VUID-` lines: identical per test on both sides (30 on main, 30 with this PR, over the three runs). The `VUID-` lines on both sides are main's existing `VUID-VkDeviceCreateInfo-sType-unique` (a duplicated `VkPhysicalDevicePortabilitySubsetFeaturesKHR` in the device pNext chain) and `VUID-VkSamplerCreateInfo-samplerMipLodBias-04467`; this PR adds none.
MarcZX10 commented 2026-11-21 11:23:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T11:23:48Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this; the points below are optional.

What I ran

Reviewed head 38b8426e6c7101de69af653952bb08fe4c52c967. No commands or tests were run. Read the prepared description, commits, manifest, diff, full text of agc_driver_label_ordered_flush_tests.cmake, and relevant ranges in Texture.cpp and GuestBufferMemory.cpp. Texture.cpp is listed as too large for the review folder; its relevant lines were read through repo_read.py.

Results

Check Result Note
Changed code review passed Reviewed both changed files
Tests not run Results in the description are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

The reported macOS test runs were not independently reproduced. No other files were read; manifest.json lists two changed files and both were reviewed. No files were excluded by files.json in the available review bundle.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6004878970 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20183#pullrequestreview-6004878970). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T11:23:48Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this; the points below are optional. ## What I ran Reviewed head `38b8426e6c7101de69af653952bb08fe4c52c967`. No commands or tests were run. Read the prepared description, commits, manifest, diff, full text of `agc_driver_label_ordered_flush_tests.cmake`, and relevant ranges in `Texture.cpp` and `GuestBufferMemory.cpp`. `Texture.cpp` is listed as too large for the review folder; its relevant lines were read through `repo_read.py`. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Reviewed both changed files | | Tests | not run | Results in the description are author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The reported macOS test runs were not independently reproduced. No other files were read; `manifest.json` lists two changed files and both were reviewed. No files were excluded by `files.json` in the available review bundle. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 22:23:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T22:23:55Z.

Review: COMMENTED


The trial merge onto a1b75b0947 conflicts in Texture.cpp. Please retain a macOS control that proves the imported image still takes the label read-trap route. The default macOS fixture now sets APS5_NO_LABEL_READ_TRAP=1, and the unimported case can verify eager publication but cannot cover the remaining imported path. My review is source-based and I have no macOS result.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6179662227 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20183#pullrequestreview-6179662227). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T22:23:55Z. **Review:** COMMENTED --- The trial merge onto `a1b75b0947` conflicts in `Texture.cpp`. Please retain a macOS control that proves the imported image still takes the label read-trap route. The default macOS fixture now sets `APS5_NO_LABEL_READ_TRAP=1`, and the unimported case can verify eager publication but cannot cover the remaining imported path. My review is source-based and I have no macOS result. 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/20183:upstream-pr/20183'
git switch 'upstream-pr/20183'

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