fix(agc): take a host-coherent memory type for a host import (#6512) #6548

Merged
fanyzp merged 2 commits from upstream-pr/6548 into main 2026-11-14 03:24:07 +00:00
fanyzp commented 2026-10-15 19:58:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fanyzp on 2026-10-15T19:58:13Z.


What

GuestBufferMemory bound a host-pointer import to the first memory type the intersection of its bits and the buffer's offered (std::countr_zero), without looking at what that type guarantees. The imported range is the guest's own memory, which the CPU reads through the same pointer, and Vulkan offers no invalidation for imported memory (vkInvalidateMappedMemoryRanges works on memory mapped with vkMapMemory), so a candidate the device reports as not guaranteeing host coherency cannot back it: a store the GPU made through such an import can stay invisible to the CPU. Issue #6512 measures exactly that shape (in agc_driver_multisample_fill_host_only, the sentinel fill after the absorbed multisampled clear does not land, and a direct host read of the block agrees).

The import now takes a HOST_VISIBLE | HOST_COHERENT candidate, preferring a cached one among those as Context::BufferMemoryType does, and refuses the import when the device reports candidates without it (the range is copied instead, and the log names the step). A context whose memory properties are not known keeps the first candidate, as the tests' contexts do.

Not established here: which types a discrete GPU's driver reports for a host pointer, which decides whether this is #6512's cause and whether the preference or the refusal is what fixes it there. On RADV (Radeon 680M, Mesa 26.2.3) the pointer's bits are a coherent type only, which is why #6512 does not reproduce on this machine (700 runs of the test, 200 of them with the process pinned to two CPUs, plus 200 on lavapipe: 0 failures) and why the refusal path is covered by the new test only. A device whose only candidates are non-coherent now copies guest ranges instead of importing them, which is slower and correct. A dma-buf import of a guest range keeps the first-candidate choice; the same argument may apply to it and it is not changed here.

Tested

  • Linux, agc_driver_graphics_tests (new hostImportMemoryTypeTests): a device whose lowest-index host-pointer candidate is not coherent and whose second one is imports through the coherent one; a device whose candidates are all non-coherent does not import the range (it is copied). The new test fails on main (the import did not take the coherent memory type), so it covers the change.
  • agc_driver_multisample_fill_host_only_tests on this head: 50 runs on RADV, 50 more with the process pinned to two CPUs, 50 on lavapipe: 0 failures (the flake does not reproduce here, before or after the change).
  • agc_graphics_profile_draw, agc_driver_host_import_at_map, agc_host_import_retirement, agc_host_import_retire: pass. agc_host_import_sparse skips here (it wants a udmabuf range above 1024 MiB).
  • The same focused tests on this head and on main, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and VK_LOADER_DEBUG=layer (validation layer 1.4.341, newer than the bundled headers' 1.4.335): the same pass and skip on both, the loader inserting the layer in each log and neither log holding a VUID- error, on RADV and on lavapipe. The only difference between the two logs is the new refusal line.
  • Not tested: any title; no GPU other than the two above; the full ctest suite was not run.

Checklist

  • Based on current main; no other open PR implements the same functions (no open PR or issue mentions the import's memory type; #6512 is the report this follows from)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt (the note is the new entry in docs/dev/debt/libSceAgcDriver.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • No new third-party code
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: AMD Radeon 680M (RADV, Mesa 26.2.6, Linux 7.3.3, Fedora 44) and lavapipe (LLVM 22.1.8, Mesa 26.2.6) for the Vulkan tests, each with the validation layer 1.4.341; the new test runs on mock Vulkan contexts and needs no GPU
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4879063837 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6548). Originally posted by [fanyzp](https://github.com/fanyzp) on 2026-10-15T19:58:13Z. --- ### What `GuestBufferMemory` bound a host-pointer import to the first memory type the intersection of its bits and the buffer's offered (`std::countr_zero`), without looking at what that type guarantees. The imported range is the guest's own memory, which the CPU reads through the same pointer, and Vulkan offers no invalidation for imported memory (`vkInvalidateMappedMemoryRanges` works on memory mapped with `vkMapMemory`), so a candidate the device reports as not guaranteeing host coherency cannot back it: a store the GPU made through such an import can stay invisible to the CPU. Issue #6512 measures exactly that shape (in `agc_driver_multisample_fill_host_only`, the sentinel fill after the absorbed multisampled clear does not land, and a direct host read of the block agrees). The import now takes a `HOST_VISIBLE | HOST_COHERENT` candidate, preferring a cached one among those as `Context::BufferMemoryType` does, and refuses the import when the device reports candidates without it (the range is copied instead, and the log names the step). A context whose memory properties are not known keeps the first candidate, as the tests' contexts do. Not established here: which types a discrete GPU's driver reports for a host pointer, which decides whether this is #6512's cause and whether the preference or the refusal is what fixes it there. On RADV (Radeon 680M, Mesa 26.2.3) the pointer's bits are a coherent type only, which is why #6512 does not reproduce on this machine (700 runs of the test, 200 of them with the process pinned to two CPUs, plus 200 on lavapipe: 0 failures) and why the refusal path is covered by the new test only. A device whose only candidates are non-coherent now copies guest ranges instead of importing them, which is slower and correct. A dma-buf import of a guest range keeps the first-candidate choice; the same argument may apply to it and it is not changed here. ### Tested - Linux, `agc_driver_graphics_tests` (new `hostImportMemoryTypeTests`): a device whose lowest-index host-pointer candidate is not coherent and whose second one is imports through the coherent one; a device whose candidates are all non-coherent does not import the range (it is copied). The new test fails on `main` (`the import did not take the coherent memory type`), so it covers the change. - `agc_driver_multisample_fill_host_only_tests` on this head: 50 runs on RADV, 50 more with the process pinned to two CPUs, 50 on lavapipe: 0 failures (the flake does not reproduce here, before or after the change). - `agc_graphics_profile_draw`, `agc_driver_host_import_at_map`, `agc_host_import_retirement`, `agc_host_import_retire`: pass. `agc_host_import_sparse` skips here (it wants a udmabuf range above 1024 MiB). - The same focused tests on this head and on `main`, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` and `VK_LOADER_DEBUG=layer` (validation layer 1.4.341, newer than the bundled headers' 1.4.335): the same pass and skip on both, the loader inserting the layer in each log and neither log holding a `VUID-` error, on RADV and on lavapipe. The only difference between the two logs is the new refusal line. - Not tested: any title; no GPU other than the two above; the full `ctest` suite was not run. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (no open PR or issue mentions the import's memory type; #6512 is the report this follows from) - [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) (the note is the new entry in `docs/dev/debt/libSceAgcDriver.md`) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] No new third-party code - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon 680M (RADV, Mesa 26.2.6, Linux 7.3.3, Fedora 44) and lavapipe (LLVM 22.1.8, Mesa 26.2.6) for the Vulkan tests, each with the validation layer 1.4.341; the new test runs on mock Vulkan contexts and needs no GPU - [x] AI-assisted: yes
jotitadev commented 2026-10-15 21:27:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T21:27:30Z.

Review: COMMENTED


NVIDIA Linux data for the part the description leaves open: which types a discrete driver reports for a host pointer.

Machine: NVIDIA GeForce RTX 3070, driver 595.99.02, Vulkan 1.4.329, Ubuntu 26.04 (kernel 7.0.0-38), i7-8700. GCC 15.2, Release, BUILD_TESTING=ON. Head 33a87a353 merged onto main 44df21632, no conflicts. ANYPS5_GPU="NVIDIA GeForce RTX 3070", confirmed in the logs.

GPUs tested: NVIDIA GeForce RTX 3070 (595.99.02), Linux

Host-pointer candidates on this driver. These come from a small standalone Vulkan program, not AnyPS5 code: a 1 MiB anonymous private mmap, vkGetMemoryHostPointerPropertiesEXT with HOST_ALLOCATION_BIT_EXT, intersected with a storage + transfer buffer's requirements.

  • type 3: HOST_VISIBLE | HOST_COHERENT (lowest index, what main takes);
  • type 4: HOST_VISIBLE | HOST_COHERENT | HOST_CACHED (what this PR prefers).

Both candidates are coherent, so on this driver the PR changes the choice from uncached to cached. It never takes the refusal path. llvmpipe offers a single type with all four flags.

Tests

  • Test only (this PR's Graphics.cpp, GuestBufferMemory.cpp from main): agc_driver_graphics fails with the import did not take the coherent memory type. With the fix it passes 3/3.
  • agc_host_import_retire, agc_host_import_sparse, agc_driver_host_import_at_map: pass, with and without the fix.
  • agc_driver_multisample_fill_host_only (#6512): 50/50 with the PR. On unchanged main it did not fail here either: 0/230 idle, 0/30 under load.
  • ctest -R "^agc|shader|recompiler|image" -E "agc_display_buffer_dcc|agc_driver_flip|agc_driver_lds_atomics64|display|flip" -j1 --timeout 60: 601/602 pass. agc_driver_stencil_ref_export is skipped because the 3070 does not expose VK_EXT_shader_stencil_export.
  • In 20 runs each, the PR does not change agc_driver_store_over_untracked (#6347): 0/20 pass with the PR, 3/20 on main. agc_pending_key_stores (#6500): 29/30 with the PR, 26/30 on main. Both passed in the suite run above by chance.

Written with AI assistance (Claude Code). I ran the tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5540156028 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6548#pullrequestreview-5540156028). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-15T21:27:30Z. **Review:** COMMENTED --- NVIDIA Linux data for the part the description leaves open: which types a discrete driver reports for a host pointer. **Machine:** NVIDIA GeForce RTX 3070, driver 595.99.02, Vulkan 1.4.329, Ubuntu 26.04 (kernel 7.0.0-38), i7-8700. GCC 15.2, Release, `BUILD_TESTING=ON`. Head `33a87a353` merged onto `main` `44df21632`, no conflicts. `ANYPS5_GPU="NVIDIA GeForce RTX 3070"`, confirmed in the logs. GPUs tested: NVIDIA GeForce RTX 3070 (595.99.02), Linux **Host-pointer candidates on this driver.** These come from a small standalone Vulkan program, not AnyPS5 code: a 1 MiB anonymous private `mmap`, `vkGetMemoryHostPointerPropertiesEXT` with `HOST_ALLOCATION_BIT_EXT`, intersected with a storage + transfer buffer's requirements. - type 3: `HOST_VISIBLE | HOST_COHERENT` (lowest index, what `main` takes); - type 4: `HOST_VISIBLE | HOST_COHERENT | HOST_CACHED` (what this PR prefers). Both candidates are coherent, so on this driver the PR changes the choice from uncached to cached. It never takes the refusal path. llvmpipe offers a single type with all four flags. **Tests** - Test only (this PR's `Graphics.cpp`, `GuestBufferMemory.cpp` from `main`): `agc_driver_graphics` fails with `the import did not take the coherent memory type`. With the fix it passes 3/3. - `agc_host_import_retire`, `agc_host_import_sparse`, `agc_driver_host_import_at_map`: pass, with and without the fix. - `agc_driver_multisample_fill_host_only` (#6512): 50/50 with the PR. On unchanged `main` it did not fail here either: 0/230 idle, 0/30 under load. - `ctest -R "^agc|shader|recompiler|image" -E "agc_display_buffer_dcc|agc_driver_flip|agc_driver_lds_atomics64|display|flip" -j1 --timeout 60`: 601/602 pass. `agc_driver_stencil_ref_export` is skipped because the 3070 does not expose `VK_EXT_shader_stencil_export`. - In 20 runs each, the PR does not change `agc_driver_store_over_untracked` (#6347): 0/20 pass with the PR, 3/20 on `main`. `agc_pending_key_stores` (#6500): 29/30 with the PR, 26/30 on `main`. Both passed in the suite run above by chance. Written with AI assistance (Claude Code). I ran the tests above on my machine.
blawesom commented 2026-10-17 11:42:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-17T11:42:30Z.


Rebase handoff — rebased onto current main (authorship unchanged). One conflict in GuestBufferMemory.cpp: main's newer dma-buf queue-family sharing (ImportFamilies/shareDmaBufImport) kept alongside your coherentImportType helper and the extended bindImport signature.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV):

  • builds; tools/check_conventions.py --base origin/main passes
  • full suite: 1338/1339 — the only failure is the known main regression (agc_driver_tessellation segfault, #4727), not this PR

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/6548
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 43012d1d7.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233474150 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6548#issuecomment-6233474150). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-17T11:42:30Z. --- **Rebase handoff** — rebased onto current `main` (authorship unchanged). One conflict in `GuestBufferMemory.cpp`: `main`'s newer dma-buf queue-family sharing (`ImportFamilies`/`shareDmaBufImport`) kept alongside your `coherentImportType` helper and the extended `bindImport` signature. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): - builds; `tools/check_conventions.py --base origin/main` passes - **full suite: 1338/1339** — the only failure is the known `main` regression (`agc_driver_tessellation` segfault, #4727), not this PR **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/6548 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `43012d1d7`.
fanyzp commented 2026-10-17 15:44:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fanyzp on 2026-10-17T15:44:31Z.


Picked the handoff up, thanks for resolving it.

New head is 89fb35adf: cherry-picked 43012d1d7 onto current main (84b0d2f24), which applied clean with no further conflict, so ImportFamilies / shareDmaBufImport and coherentImportType sit together as in your resolution. Branch force-pushed, and the PR reports MERGEABLE again.

Re-verified here (AMD Radeon 680M, RADV, Mesa 26.2.3, Linux 7.2.8; GCC 16.2.1, Release, Ninja, BUILD_TESTING=ON):

  • tools/check_conventions.py --base origin/main: passes
  • agc_driver_graphics_tests (new hostImportMemoryTypeTests): pass
  • agc_host_import_retire_tests, agc_host_import_sparse_tests, agc_driver_host_import_at_map_tests: pass
  • agc_driver_multisample_fill_host_only_tests: 50/50
  • Back-check that the new test still enforces the fix: with GuestBufferMemory.cpp taken from main, agc_driver_graphics_tests fails with the import did not take the coherent memory type; with the fix it passes.

On the new head conventions passed. Build and Progress report are still action_required, which a maintainer approves.

The rebase resolution is blawesom's; the checks above were run here. AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6235985499 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6548#issuecomment-6235985499). Originally posted by [fanyzp](https://github.com/fanyzp) on 2026-10-17T15:44:31Z. --- Picked the handoff up, thanks for resolving it. New head is `89fb35adf`: cherry-picked `43012d1d7` onto current `main` (`84b0d2f24`), which applied clean with no further conflict, so `ImportFamilies` / `shareDmaBufImport` and `coherentImportType` sit together as in your resolution. Branch force-pushed, and the PR reports `MERGEABLE` again. Re-verified here (AMD Radeon 680M, RADV, Mesa 26.2.3, Linux 7.2.8; GCC 16.2.1, Release, Ninja, `BUILD_TESTING=ON`): - `tools/check_conventions.py --base origin/main`: passes - `agc_driver_graphics_tests` (new `hostImportMemoryTypeTests`): pass - `agc_host_import_retire_tests`, `agc_host_import_sparse_tests`, `agc_driver_host_import_at_map_tests`: pass - `agc_driver_multisample_fill_host_only_tests`: 50/50 - Back-check that the new test still enforces the fix: with `GuestBufferMemory.cpp` taken from `main`, `agc_driver_graphics_tests` fails with `the import did not take the coherent memory type`; with the fix it passes. On the new head `conventions` passed. `Build` and `Progress report` are still `action_required`, which a maintainer approves. The rebase resolution is blawesom's; the checks above were run here. AI-assisted: yes.
Queuse commented 2026-10-21 23:36:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Queuse on 2026-10-21T23:36:29Z.


Rebased branch and cached-choice coverage, cd4272bc00, main a6dcea9279; authorship preserved.

Baseline/candidate: graphics, import retirement and import-at-map pass on RTX 3050 Laptop/617.42 and Iris Xe/101.4502 (Windows 11, GCC 15.2.0-r7); Linux-only sparse import skips. Required Vulkan/validation/cache flags: zero missing layer/VUID errors.

Host-pointer probes offer Intel types 0/1/2 (0 device-local only; 2 coherent+cached), NVIDIA 3/4 (both coherent; 4 cached). Added representative cached-choice cases; removing that preference fails its assertion. #6512 is closed after #6522; this validates the separate memory-type protection, without claiming that flake or a title is fixed. AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6328968827 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6548#issuecomment-6328968827). Originally posted by [Queuse](https://github.com/Queuse) on 2026-10-21T23:36:29Z. --- [Rebased branch and cached-choice coverage](https://github.com/Queuse/AnyPS5/tree/codex/validate-pr-6548), `cd4272bc00`, main `a6dcea9279`; authorship preserved. Baseline/candidate: graphics, import retirement and import-at-map pass on RTX 3050 Laptop/617.42 and Iris Xe/101.4502 (Windows 11, GCC 15.2.0-r7); Linux-only sparse import skips. Required Vulkan/validation/cache flags: zero missing layer/VUID errors. Host-pointer probes offer Intel types 0/1/2 (0 device-local only; 2 coherent+cached), NVIDIA 3/4 (both coherent; 4 cached). Added representative cached-choice cases; removing that preference fails its assertion. #6512 is closed after #6522; this validates the separate memory-type protection, without claiming that flake or a title is fixed. AI-assisted: yes.
CnRJay commented 2026-11-01 19:52:33 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Correction (2026-11-01): the title-ID/log demand below is not a CONTRIBUTING requirement; Unblocks: none is allowed. The technical points below are unaffected.

Points on 6548:

  1. Comments in code. GuestBufferMemory.cpp adds a /// doc block for the memory-type helper (four lines plus an inline reference to Context::BufferMemoryType). The repo rule is no comments except #endif and namespace ends. 13 added comment lines in this diff in total; please remove them.

  2. Refusal becomes a copy. When no candidate memory type guarantees host coherency the import is refused and "the range is copied instead". That copy path is the slow one this feature exists to avoid, and on a device without a coherent type it is taken on every use. Please say which GPUs fall into it and attach a tools/trace_digest.py comparison of main and the change on one of them, same scene.

  3. Title evidence. The fix references #6512 but no title ID and log line showing a title hits this memory-type choice on main. Please add one.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729932293 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6548#pullrequestreview-5729932293). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:52:33Z. **Review:** COMMENTED --- **Correction (2026-11-01):** the title-ID/log demand below is not a CONTRIBUTING requirement; `Unblocks: none` is allowed. The technical points below are unaffected. Points on 6548: 1. **Comments in code.** `GuestBufferMemory.cpp` adds a `///` doc block for the memory-type helper (four lines plus an inline reference to `Context::BufferMemoryType`). The repo rule is no comments except `#endif` and namespace ends. 13 added comment lines in this diff in total; please remove them. 2. **Refusal becomes a copy.** When no candidate memory type guarantees host coherency the import is refused and "the range is copied instead". That copy path is the slow one this feature exists to avoid, and on a device without a coherent type it is taken on every use. Please say which GPUs fall into it and attach a `tools/trace_digest.py` comparison of main and the change on one of them, same scene. 3. **Title evidence.** The fix references #6512 but no title ID and log line showing a title hits this memory-type choice on main. Please add one.
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!6548
No description provided.