Skip to content

fix(agc): keep per-layer storage views of a 2D array in one image - #149

Open
Thimo188 wants to merge 17 commits into
mainfrom
upstream-pr/10743
Open

Thimo188 wants to merge 17 commits into
mainfrom
upstream-pr/10743

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#10743, originally opened by orezraey.

Source commit: 169535353891ac73274c805a00543458f026ef2a. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

A compute shader that writes one layer of a 2D array at a time, through a T# with BASE_ARRAY = LAST_ARRAY = N, made a separate storage image per layer: the lookup of layer N built an image of layers 0..N (the T# names N as its last layer, not the array size). The images overlap, so every dispatch flushed the previous one's pending results and uploaded its own again (storage refresh write-backs). DEATHLOOP (PPSA01670) does this every frame on a 960x540 4-layer array.

  • StorageArraySurface (in StorageSurface): a storage view of a thin 2D array without DCC, FMASK/CMASK or MSAA is served by one image of slices 0 to the largest last slice any view of that surface has named, the way StorageSurface already widens mip chains. It falls back to the view itself when the widened geometry would move the viewed slices.
  • When a wider image is created, the narrower images of the same surface are evicted (flushed) so two pending images never hold the same slice.
  • A widened lookup refreshes only the viewed slices (RefreshLayers). The binding gets a view of those slices: StorageView, AtomicView, Atomic64View, View and FirstLayerView take a layer window, and their view caches are keyed by it. A write marks only those slices dirty (MarkSliceDirty); a write through a 2D shape marks only the window's first slice.
  • agc_driver_storage_array_layers: three rounds of per-layer image_stores into a 4-layer array. After the first round one pending image holds all four layers, later rounds reuse it, and every layer holds its own values after the flush. It then registers the same address again as a 2-layer allocation and repeats the rounds there: the pending image holds layers 0..1, not the 0..3 of the earlier array, and layers 2 and 3 stay untouched. Before that it repeats the rounds as a 2-layer array at the same address without touching the allocation (same backingSerial), with the same expectations. Last, it extends a storage mip chain (which switches StorageSurface to its locked path) and repeats the rounds on another array. On main it fails with the pending image of the 4-layer array not holding every layer; on cdfc7a986f it fails with "the pending image of the 2-layer array holds layers 0..3", and on cd720c71a4 it fails the same way in the unchanged-allocation case.
  • The largest slice is kept per surface key only while the registered guest range holding the base keeps its backing (backingSerial, renewed by a new mapping or a protection change), and a view is widened only when the wider image ends inside that range; memory in no registered range is never widened.
  • Only a view that names the largest slice creates the wider image. A view whose remembered largest slice lies past its own last slice uses a cached image of more slices if there is one, and otherwise creates an image of only slices 0 to its own last, so a new image never uploads slices the view's T# does not name.
  • Copies, resolves and color targets (CachedStorageSurface, CachedStorageLayers) are never widened: AliasCopy builds its destination with CachedStorageSurface and refuses a copy whose shapes differ, so a widened image there threw "an image copy its check allowed was refused: shape" (found by ahmadmuj). agc_driver_storage_array_layers now also stores into all four layers and then asks both for layers 0..1 at the same address; with ShaderResources.cpp at 53e11f112b it fails with "a copy destination of layers 0..1 got layers 0..3".
  • Debt entry: a shorter array the title places at the same address inside the same mapping shares the longer image only while that image is cached; a 2D T# at the address of a later slice is still a separate image.

Related: boykopovar#6202 refreshes only the layers a color target covers; this PR is about storage writes and does not touch the color-target path.

Tested

  • Head 6d750b25ad (main b611e634f7 merged; the conflict was fix(agc): bind UINT views for float16 stores outside compute shaders boykopovar/AnyPS5#15741's storage descriptor writes in ShaderResources.cpp, which now pick UINT storage views with ShaderRecompiler::RuntimeAbi::UintStorageImageBinding(binding.layout.binding) instead of the binding 7 to 13 range test. Both writes keep that call and pass this PR's storageLayers[index] to Atomic64View, AtomicView and StorageView). Windows, same machine as below: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "^agc.*(array_layer|image_store|storage|depth_plane|image_atomic|image_table|alias_copy|dcc_keys_first_write|copied_writer|clear_hand)" -E usb -V, 3 runs: 96 of 96 pass each time, including agc_driver_storage_array_layers, 0 VUID-, no missing-layer message. Linux (WSL Ubuntu, GCC, tree reconfigured at this head): libs and the tests build; the same ctest set, 3 runs: 94 of 94 pass each time, 92 of them skipped (exit 77, no Vulkan driver in WSL). check_conventions passes.

  • Head 72ac10a202 (main 1c8c0efc8c merged; the conflict was in cachedStorageTexture, where main (perf(agc): hand pending results over to storage images created under clear keys boykopovar/AnyPS5#14868) now detaches a storage image whose metadata moved and hands it to StorageTexture::PrepareCreation instead of flushing it. The merge keeps this PR's layer-narrowing lookup and widening eviction, detaches as main does, and hands over only an image detached at the key that is finally created: one detached at a wider key, or before narrower images are evicted for widening, is flushed first, as this PR did before). Windows, same machine as below: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "^agc.*(array_layer|image_store|storage|depth_plane|image_atomic|image_table|alias_copy|dcc_keys_first_write|copied_writer|clear_hand)" -E usb -V, 3 runs: 90 of 90 pass each time, including agc_driver_storage_array_layers and agc_storage_clear_hand_over, 0 VUID-, no missing-layer message. Linux (WSL Ubuntu, GCC): libs and agc_driver_storage_array_layers_tests build; the test is skipped (exit 77), no Vulkan driver in WSL. check_conventions passes.

  • Head 7d2be0318b: main was merged in, with perf(agc): wait on a flush only for writers over the image layers it stores boykopovar/AnyPS5#8887 and feat(agc): sample 1D arrays past maxImageArrayLayers as rows of a 2D image boykopovar/AnyPS5#12246. Texture.hpp keeps appendStoredLayers from main next to this PR's createView(..., LayerWindow layers = {}); the debt file keeps both the 1D-array entry and the storage array widening entry. The 81 focused AGC tests (^agc.*(array_layer|image_store|storage|depth_plane|image_atomic|image_table|alias_copy|dcc_keys_first_write|copied_writer)) pass three runs in a row on Windows (MinGW, GCC 15.2) with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation: no VUID- errors and the layer was found. On Linux (WSL, GCC) they build and 79 pass three runs in a row, but 77 of those skip there, so Linux exercises only 2 of them. check_conventions passes.

Head 8be6c1abe8 (main bb04a4f1ea merged; no textual conflict, but main's new RepeatStorageSurface called StorageSurface(context, viewed), which this PR gives a widenArray parameter, so the merge did not build, as the NVIDIA GPU test reported. It now passes false, as CachedStorageSurface, the lookup it repeats, does). Windows, same machine as below: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "^agc" -V, 3 runs: 1151 of 1151 pass each time, no missing-layer message; each run logs 18 VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605, all from agc_driver_prepared_exports and agc_driver_srgb8_color_target, and main bb04a4f1ea logs the same 18 in those two tests, so this PR adds none. Linux (WSL Ubuntu, GCC, tree reconfigured at this head): builds; ctest -R "array_layer|image_store|storage|depth_plane|image_atomic|image_table|alias_copy|dcc_keys_first_write|copied_writer" -E usb, 3 runs: 75 of 75 pass each time, 74 of them skipped (exit 77, no Vulkan driver in WSL), including agc_driver_storage_array_layers. check_conventions passes.

Head 924af40153 (main 81267b2204 merged; the conflict was boykopovar#8998's UINT storage views for bindings 7 to 13 against this PR's layer window: StorageTexture::StorageView now takes both, (mip, firstLayer, uintStorage, layers), keeps boykopovar#8998's format choice, and the two storage descriptor writes in ShaderResources.cpp pass boykopovar#8998's binding test and this PR's layer window). Windows, same machine as below: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "array_layer|image_store|storage|depth_plane|image_atomic|image_table|alias_copy|dcc_keys_first_write|copied_writer" -E usb -V, 3 runs: 72 of 72 pass each time, agc_driver_storage_array_layers passes, 0 VUID-, no missing-layer message. Linux (WSL Ubuntu, GCC): builds; agc_driver_storage_array_layers is skipped (exit 77) in 3 runs, no Vulkan driver in WSL. check_conventions passes.

Head e1ee11a812 (main 9c6d153dec merged after boykopovar#9961 landed; the only conflict was the libSceAgcDriver debt file, where boykopovar#9961's new sampled-array-window entry is kept and this PR's storage-widening entry follows it; no code conflict). Windows, same machine as below: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "array_layer|image_store|storage|depth_plane|image_atomic|image_table|alias_copy|dcc_keys_first_write|copied_writer" -E usb -V, 3 runs: 68 of 69 pass each time, agc_driver_storage_array_layers passes, 0 VUID-, no missing-layer message; the failure is agc_driver_depth_plane_partial_store (boykopovar#10664), which fails on main too. Linux (WSL): these tests need a Vulkan device and WSL has none, so they skip (exit 77).

Head 09450f2499 (90ac083960 plus a test-only change that prints the base slice in the failure messages; agc_driver_storage_array_layers rerun 3 times at 09450f2499 on Windows with validation: passes, 0 VUID-; on Linux: skipped, exit 77). The set below ran at 90ac083960, on 53e11f112b, same machine as below. ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "array_layer|image_store|storage|depth_plane|image_atomic|image_table|alias_copy|dcc_keys_first_write|copied_writer" -E usb -V, 3 runs: 68 of 69 pass each time, 0 VUID-, no missing-layer message; the failure is again agc_driver_depth_plane_partial_store, as on main. Linux (WSL Ubuntu, GCC): builds; agc_driver_storage_array_layers, agc_driver_alias_copy_label and agc_driver_dcc_keys_first_write are skipped (exit 77) in 3 runs because WSL has no Vulkan driver. check_conventions passes.

Head 53e11f112b (main 16ff208dc3 merged after boykopovar#10440 landed; in ShaderResources.cpp the storage descriptor now picks boykopovar#10440's Atomic64View for 64-bit atomics and passes this PR's layer window to all three views), same machine as below. ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "array_layer|image_store|storage|depth_plane" -V, 3 runs: 53 of 54 pass each time, 0 VUID-, no missing-layer message; the failure is agc_driver_depth_plane_partial_store, which fails the same way on main 16ff208dc3 (1 of 53 failed there, 0 VUID-, no missing-layer message). Linux (WSL Ubuntu, GCC): builds; agc_driver_storage_array_layers is skipped (exit 77) in 3 runs, no Vulkan driver in WSL. check_conventions passes.

Head ec41941094 on main 2dc85609fc, same machine as below. ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "array_layer|image_store|storage|depth_plane" -V, 3 runs: 53 of 54 pass each time, 0 VUID-, no missing-layer message; the failure is again agc_driver_depth_plane_partial_store, as on main. agc_driver_storage_array_layers with only ShaderResources.cpp reverted to cd720c71a4 fails with "the pending image of the 2-layer array holds layers 0..3". Linux (WSL Ubuntu, GCC): builds; agc_driver_storage_array_layers is skipped (exit 77) in 3 runs because WSL has no Vulkan driver. check_conventions passes. The DEATHLOOP flush count below is from cdfc7a986f and was not rerun at this head.

Earlier heads:

Windows 11 Pro 26200, AMD Ryzen 7 9700X, NVIDIA GeForce RTX 5070 Ti (driver 616.64), WinLibs GCC, Release. Head cd720c71a4 on main 2dc85609fc.

  • ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R "array_layer|image_store|storage|depth_plane" -V, 3 runs at cd720c71a4: 53 of 54 pass each time, 0 VUID-, no missing-layer message. The failure is agc_driver_depth_plane_partial_store ("repeated lookups of the first layer of an unchanged array proved no refresh"), which fails the same way on main 2fade7ef4e on this machine. On that main the same set also fails agc_driver_storage_array_layers, and logs 0 VUID-.
  • Full ctest at cdfc7a986f (before the allocation bound): 2182 tests; failures agc_driver_depth_plane_partial_store and agc_host_import_cut_lifetime (both fail the same way on main 2fade7ef4e here) and select_tests (a temp-directory symlink error in tools/tests/test_select_tests.py).
  • tools/check_conventions.py --base origin/main: passes.
  • Linux (WSL Ubuntu, GCC, same checkout) at cd720c71a4: builds; agc_driver_storage_array_layers is skipped (exit 77) in 3 runs because WSL has no Vulkan driver, so the GPU path is covered only by the Windows runs.

DEATHLOOP (PPSA01670), same machine, behaviour count only (no speed or traffic claim). main cannot reach this scene yet, so both builds are main + boykopovar#10414 + boykopovar#10507 + a port of boykopovar#6192, one without and one with this PR: base fa00d49312, change d26dce2835 (this PR's head cdfc7a986f cherry-picked). Same scripted input (PSN dialog and brightness calibration screens), run with APS5_TRACE_FLUSH=1: the base build logs about 1050 storage refresh flushes of each of the four overlapping images of the 0x26a7200000 array in that flush log; the change logs none for it. What remains comes from 2D T#s at the address of each slice of a 2-layer array that the title also reads as an array (in the debt entry). Alternated base/change captures showed no frame-time difference beyond run-to-run noise, so this PR makes no performance claim.

Checklist

orezraey and others added 17 commits October 24, 2026 17:12
A T# that views one layer of a 2D array (BASE_ARRAY = LAST_ARRAY = N)
used to build a storage image of layers 0..N. Writes to each layer then
made overlapping images that flushed and re-uploaded each other on every
dispatch. Storage lookups now widen a 2D array to the largest layer seen
for its surface, bind views over the requested layers, and refresh and
mark dirty only those layers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
StorageSurface held the ExtendedChains mutex while StorageArraySurface
locked it again, which deadlocks a storage lookup of a 2D array once any
mip chain was extended. A write through a 2D shape of an array now marks
only its first slice dirty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The widest slice seen for a storage array is kept only while the registered
guest range holding its base keeps the same backing, and a view is widened
only when the wider image ends inside that range. A shorter array placed at
the address of an earlier, longer one no longer builds the longer image.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…g its last slice

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CachedStorageSurface and CachedStorageLayers no longer widen a 2D array to
the largest slice a storage view named, so AliasCopy, resolves and color
targets get an image of exactly the slices they ask for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The main merge kept both sides of the libSceAgcDriver debt conflict; main's measured entry stays.
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants