perf(agc): retile a whole-layer linear storage write-back into the host import #13044

Merged
Gonic0 merged 5 commits from upstream-pr/13044 into main 2026-11-04 23:17:46 +00:00
Gonic0 commented 2026-10-29 23:24:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-10-29T23:24:04Z.


What

Builds on #11615. Until it merges, this branch carries its commit; only the top two commits, 76bfb762b3 and a32475a0ab, are new here.

#11615 stops seeding a linear storage write-back's pitch padding: the retile still goes into a device scratch buffer, and the rows are then copied from the scratch into the host import, one VkBufferCopy per row. On MoltenVK that per-row copy is the cost. MoltenVK encodes every region of a vkCmdCopyBuffer as its own Metal blit when it submits the command buffer, on one CPU thread.

On PPSA21564 (ASTRO BOT 01.007.000), during the logos before the intro, three 240x135 linear images with 64 layers are written back about once per frame each. Instruments' Time Profiler shows MVKCmdCopyBuffer::encode taking 75 % of MoltenVK's submit thread, and a counting wrapper around vkCmdCopyBuffer showed about 2,500 regions per write-back, both before #11615 (128-byte padding pieces) and with it (1,920-byte rows). The GPU sat idle about 70 % of the time, waiting for that thread.

A linear surface's guest layout is the retile's own output layout, so this change retiles straight into the import when all of these hold:

  • the write-back takes #11615's branch (linear, not thick, no tail mips, padding current in the import, write-watched memory);
  • one import segment covers the stored span;
  • the stored ranges are exactly the selected layers, whole;
  • the elements are 4, 8 or 16 bytes;
  • the import is in a device-local memory heap, as on unified memory (MoltenVK on Apple Silicon reports a single heap, DEVICE_LOCAL). On a discrete GPU the import is in system memory, and the retile's writes there would cross the bus element by element: an RTX 4090 run (below) was 3-5x slower with them, so those devices keep the scratch path and its bulk row copies. The retile writes narrower elements with atomics on whole words, which on a row's last word would also cover pitch padding in host memory, so those keep the scratch.

The retile shader (TextureDetile.comp, linear family) then writes each element at its pitched offset in the import and leaves the padding bytes alone. There is no scratch buffer and no copy. The barriers around the retile already cover shader writes to the import: the import-ready barrier before it, and the stored barrier after it (compute stage, shader writes). In every other case the scratch path of #11615 stays. APS5_NO_LINEAR_IMPORT_RETILE=1 forces the scratch path. StorageTexture::LinearImportRetiles() counts the write-backs that retile into the import, for the test.

Overlap

#9674 replaces geometry.GuestLayerOffset(layer) + mip.tiledOffset with geometry.GuestOffset(...) in the same retile call that this PR splits into an import arm and a scratch arm. Whichever lands second applies that offset to both arms. This PR's import arm never takes thick surfaces, which is the case #9674 adds.

Tested

Apple M5 Pro, macOS 27.2, MoltenVK from Vulkan SDK 1.4.363, x86-64 Release under Rosetta 2. main alone does not build on macOS. I ran everything in a tree with the open macOS port PRs, today's main and #11615; this commit's diff is identical there.

agc_storage_write_back (agc_driver_recorder_tests --storage-write-back-only):

  • In write-watched memory, every linear case must retile into the import.
  • New: for linear cases, every byte of the surface must hold the texel colour at its row's place (each mip's rows at its pitch) and the original pattern everywhere else. The previous check counted texel-coloured words, which a misplaced row still passes.
  • New CTest entry agc_storage_write_back_scratch runs the same test with APS5_NO_LINEAR_IMPORT_RETILE=1, and requires the scratch path.

Negative controls, each of which fails the test:

  • never taking the import path: "the linear write-back went through its scratch instead of retiling into the import", for the 4 linear cases;
  • retiling 4 bytes past the right offset: "1200 bytes differ from the texels at their rows' places and the untouched pitch padding" (and 1616, 5600 for the other cases). Without the new position check this mutation passed.

Focused tests ^(agc_storage_|agc_driver_storage_|agc_driver_linear_target_), 34 tests, with ctest -V, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1, ANYPS5_REQUIRE_VULKAN=1: 34/34 pass. The only validation message is VUID-VkDeviceCreateInfo-pProperties-04451, twice in each test, from the tests' own device creation on MoltenVK (portability subset not enabled); it does not come from this change. No log reports was not found but was requested by env var VK_INSTANCE_LAYERS.

PPSA21564 01.007.000, logos before the intro: the same build, alternating only APS5_NO_LINEAR_IMPORT_RETILE, no profiling switches. Frames are dumped every 60 presents; FPS is 480 presents over the time between dumped frames 5 and 13.

round write-back FPS dumped frame 13 at
1 scratch (#11615) 23.6 35.3 s
2 into the import 37.8 22.0 s
3 scratch (#11615) 24.0 34.4 s
4 into the import 37.5 22.1 s

Performance trace summaries (APS5_TRACE_SECONDS=6:8, inside the logos for both builds), #11615 merged into the test tree (393616a491) against the same plus this commit (b2d3dd4548), alternated:

round build presents in 8 s frame p50 p90 p99 GPU busy per present
1 #11615 182 42.8 ms 46.1 ms 49.6 ms 21.6 ms
2 this PR 299 25.8 ms 28.1 ms 37.1 ms 17.5 ms
3 #11615 179 43.4 ms 46.5 ms 52.0 ms 21.8 ms
4 this PR 293 26.2 ms 28.6 ms 42.7 ms 17.9 ms

Driver.Packet.prepare_memory falls from 23.2 to 8.8 ms per frame and Recorder.WaitSerial leaves the top phases (18.6 ms per frame before).

#11615:

{"commit":"393616a49114","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_DUMP_FRAMES":"200","APS5_DUMP_FRAMES_EVERY":"60","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"6:8"},"gpu":"Apple M5 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.2.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":18,"memory_mib":49152,"title_id":"PPSA21564","title":"ASTRO BOT","content_version":"01.007.000","resolution":"3840x2160","window":{"spec":"seconds 6:8","first_present":78,"ms":8013.507},"presents":182,"frame_ms":{"p50":42.758,"p90":46.099,"p99":49.573,"max":206.264},"gpu_busy_ms_per_present":21.620,"worker_busy_pct":{"queue worker 0x0":99.398,"queue worker 0x40":99.775,"queue worker 0x48":97.680},"top_phases_ms_per_frame":{"Driver.Packet":129.673,"Driver.Packet.memory_wait":83.277,"Driver.Packet.prepare_memory":23.180,"Recorder.WaitSerial":18.577,"Driver.Packet.dispatch":10.028,"Driver.Dispatch":8.673,"VideoOut.Flip":6.274,"Driver.Present":5.363},"trace_dropped":false,"trace":"aps5-trace-393616a49114-33281.json.gz"}

This PR:

{"commit":"b2d3dd45484d","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_DUMP_FRAMES":"200","APS5_DUMP_FRAMES_EVERY":"60","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"6:8"},"gpu":"Apple M5 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.2.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":18,"memory_mib":49152,"title_id":"PPSA21564","title":"ASTRO BOT","content_version":"01.007.000","resolution":"3840x2160","window":{"spec":"seconds 6:8","first_present":84,"ms":8022.698},"presents":299,"frame_ms":{"p50":25.824,"p90":28.092,"p99":37.076,"max":192.246},"gpu_busy_ms_per_present":17.473,"worker_busy_pct":{"queue worker 0x0":98.960,"queue worker 0x40":99.721,"queue worker 0x48":95.601,"queue worker 0x3a":0.024},"top_phases_ms_per_frame":{"Driver.Packet":78.092,"Driver.Packet.memory_wait":48.928,"Driver.Packet.prepare_memory":8.799,"Driver.Packet.dispatch":7.618,"VideoOut.Flip":6.783,"Driver.Dispatch":6.184,"Driver.Present":5.676,"VideoOut.Flip.present":5.656},"trace_dropped":false,"trace":"aps5-trace-b2d3dd45484d-33661.json.gz"}

Traces: #11615, this PR.

The game runs were taken before review restricted the import path to 4-, 8- and 16-byte elements. This title's three images are 8 bytes per element (format 71), so they take the same path with the final commit.

For comparison, #11615 on its own did not change this scene on MoltenVK: 22.6 and 23.0 FPS without it, 22.9 and 23.2 with it, since the row copies keep the region count.

Not tested on Linux, Windows or another GPU. On drivers where a many-region vkCmdCopyBuffer is cheap, this change saves the scratch allocation and one copy of the stored bytes per write-back.

After review (a32475a0ab, device-local heap condition): on an RTX 4090 the direct retile made the title 3-5x slower (reported in review); the import there is outside a device-local heap, so it now keeps the scratch path. On the Mac, where the heap is device-local, the path is unchanged: the 34 focused tests pass again (agc_storage_write_back now expects the import retile only for an import in a device-local heap and fails if a write-back retiles into one outside it), and the logos run at 32.0 FPS against 21.1 with APS5_NO_LINEAR_IMPORT_RETILE=1 (one run each, in a tree without #9385). Not run on a discrete GPU.

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: #11615
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M5 Pro, MoltenVK (Vulkan SDK 1.4.363), macOS 27.2
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5076997351 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13044). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-10-29T23:24:04Z. --- ### What **Builds on #11615.** Until it merges, this branch carries its commit; only the top two commits, `76bfb762b3` and `a32475a0ab`, are new here. #11615 stops seeding a linear storage write-back's pitch padding: the retile still goes into a device scratch buffer, and the rows are then copied from the scratch into the host import, one `VkBufferCopy` per row. On MoltenVK that per-row copy is the cost. MoltenVK encodes every region of a `vkCmdCopyBuffer` as its own Metal blit when it submits the command buffer, on one CPU thread. On PPSA21564 (ASTRO BOT 01.007.000), during the logos before the intro, three 240x135 linear images with 64 layers are written back about once per frame each. Instruments' Time Profiler shows `MVKCmdCopyBuffer::encode` taking 75 % of MoltenVK's submit thread, and a counting wrapper around `vkCmdCopyBuffer` showed about 2,500 regions per write-back, both before #11615 (128-byte padding pieces) and with it (1,920-byte rows). The GPU sat idle about 70 % of the time, waiting for that thread. A linear surface's guest layout is the retile's own output layout, so this change retiles straight into the import when all of these hold: - the write-back takes #11615's branch (linear, not thick, no tail mips, padding current in the import, write-watched memory); - one import segment covers the stored span; - the stored ranges are exactly the selected layers, whole; - the elements are 4, 8 or 16 bytes; - the import is in a device-local memory heap, as on unified memory (MoltenVK on Apple Silicon reports a single heap, `DEVICE_LOCAL`). On a discrete GPU the import is in system memory, and the retile's writes there would cross the bus element by element: an RTX 4090 run (below) was 3-5x slower with them, so those devices keep the scratch path and its bulk row copies. The retile writes narrower elements with atomics on whole words, which on a row's last word would also cover pitch padding in host memory, so those keep the scratch. The retile shader (`TextureDetile.comp`, linear family) then writes each element at its pitched offset in the import and leaves the padding bytes alone. There is no scratch buffer and no copy. The barriers around the retile already cover shader writes to the import: the import-ready barrier before it, and the `stored` barrier after it (compute stage, shader writes). In every other case the scratch path of #11615 stays. `APS5_NO_LINEAR_IMPORT_RETILE=1` forces the scratch path. `StorageTexture::LinearImportRetiles()` counts the write-backs that retile into the import, for the test. ### Overlap #9674 replaces `geometry.GuestLayerOffset(layer) + mip.tiledOffset` with `geometry.GuestOffset(...)` in the same retile call that this PR splits into an import arm and a scratch arm. Whichever lands second applies that offset to both arms. This PR's import arm never takes thick surfaces, which is the case #9674 adds. ### Tested Apple M5 Pro, macOS 27.2, MoltenVK from Vulkan SDK 1.4.363, x86-64 Release under Rosetta 2. `main` alone does not build on macOS. I ran everything in a tree with the open macOS port PRs, today's `main` and #11615; this commit's diff is identical there. **`agc_storage_write_back`** (`agc_driver_recorder_tests --storage-write-back-only`): - In write-watched memory, every linear case must retile into the import. - New: for linear cases, every byte of the surface must hold the texel colour at its row's place (each mip's rows at its pitch) and the original pattern everywhere else. The previous check counted texel-coloured words, which a misplaced row still passes. - New CTest entry `agc_storage_write_back_scratch` runs the same test with `APS5_NO_LINEAR_IMPORT_RETILE=1`, and requires the scratch path. Negative controls, each of which fails the test: - never taking the import path: "the linear write-back went through its scratch instead of retiling into the import", for the 4 linear cases; - retiling 4 bytes past the right offset: "1200 bytes differ from the texels at their rows' places and the untouched pitch padding" (and 1616, 5600 for the other cases). Without the new position check this mutation passed. Focused tests `^(agc_storage_|agc_driver_storage_|agc_driver_linear_target_)`, 34 tests, with `ctest -V`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1`, `ANYPS5_REQUIRE_VULKAN=1`: 34/34 pass. The only validation message is `VUID-VkDeviceCreateInfo-pProperties-04451`, twice in each test, from the tests' own device creation on MoltenVK (portability subset not enabled); it does not come from this change. No log reports `was not found but was requested by env var VK_INSTANCE_LAYERS`. **PPSA21564 01.007.000**, logos before the intro: the same build, alternating only `APS5_NO_LINEAR_IMPORT_RETILE`, no profiling switches. Frames are dumped every 60 presents; FPS is 480 presents over the time between dumped frames 5 and 13. | round | write-back | FPS | dumped frame 13 at | |---|---|---|---| | 1 | scratch (#11615) | 23.6 | 35.3 s | | 2 | into the import | 37.8 | 22.0 s | | 3 | scratch (#11615) | 24.0 | 34.4 s | | 4 | into the import | 37.5 | 22.1 s | Performance trace summaries (`APS5_TRACE_SECONDS=6:8`, inside the logos for both builds), `#11615` merged into the test tree (`393616a491`) against the same plus this commit (`b2d3dd4548`), alternated: | round | build | presents in 8 s | frame p50 | p90 | p99 | GPU busy per present | |---|---|---|---|---|---|---| | 1 | #11615 | 182 | 42.8 ms | 46.1 ms | 49.6 ms | 21.6 ms | | 2 | this PR | 299 | 25.8 ms | 28.1 ms | 37.1 ms | 17.5 ms | | 3 | #11615 | 179 | 43.4 ms | 46.5 ms | 52.0 ms | 21.8 ms | | 4 | this PR | 293 | 26.2 ms | 28.6 ms | 42.7 ms | 17.9 ms | `Driver.Packet.prepare_memory` falls from 23.2 to 8.8 ms per frame and `Recorder.WaitSerial` leaves the top phases (18.6 ms per frame before). #11615: ```json {"commit":"393616a49114","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_DUMP_FRAMES":"200","APS5_DUMP_FRAMES_EVERY":"60","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"6:8"},"gpu":"Apple M5 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.2.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":18,"memory_mib":49152,"title_id":"PPSA21564","title":"ASTRO BOT","content_version":"01.007.000","resolution":"3840x2160","window":{"spec":"seconds 6:8","first_present":78,"ms":8013.507},"presents":182,"frame_ms":{"p50":42.758,"p90":46.099,"p99":49.573,"max":206.264},"gpu_busy_ms_per_present":21.620,"worker_busy_pct":{"queue worker 0x0":99.398,"queue worker 0x40":99.775,"queue worker 0x48":97.680},"top_phases_ms_per_frame":{"Driver.Packet":129.673,"Driver.Packet.memory_wait":83.277,"Driver.Packet.prepare_memory":23.180,"Recorder.WaitSerial":18.577,"Driver.Packet.dispatch":10.028,"Driver.Dispatch":8.673,"VideoOut.Flip":6.274,"Driver.Present":5.363},"trace_dropped":false,"trace":"aps5-trace-393616a49114-33281.json.gz"} ``` This PR: ```json {"commit":"b2d3dd45484d","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_DUMP_FRAMES":"200","APS5_DUMP_FRAMES_EVERY":"60","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"6:8"},"gpu":"Apple M5 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.2.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":18,"memory_mib":49152,"title_id":"PPSA21564","title":"ASTRO BOT","content_version":"01.007.000","resolution":"3840x2160","window":{"spec":"seconds 6:8","first_present":84,"ms":8022.698},"presents":299,"frame_ms":{"p50":25.824,"p90":28.092,"p99":37.076,"max":192.246},"gpu_busy_ms_per_present":17.473,"worker_busy_pct":{"queue worker 0x0":98.960,"queue worker 0x40":99.721,"queue worker 0x48":95.601,"queue worker 0x3a":0.024},"top_phases_ms_per_frame":{"Driver.Packet":78.092,"Driver.Packet.memory_wait":48.928,"Driver.Packet.prepare_memory":8.799,"Driver.Packet.dispatch":7.618,"VideoOut.Flip":6.783,"Driver.Dispatch":6.184,"Driver.Present":5.676,"VideoOut.Flip.present":5.656},"trace_dropped":false,"trace":"aps5-trace-b2d3dd45484d-33661.json.gz"} ``` Traces: [#11615](https://github.com/user-attachments/files/34158859/aps5-trace-393616a49114-33281.json.gz), [this PR](https://github.com/user-attachments/files/34158858/aps5-trace-b2d3dd45484d-33661.json.gz). The game runs were taken before review restricted the import path to 4-, 8- and 16-byte elements. This title's three images are 8 bytes per element (format 71), so they take the same path with the final commit. For comparison, #11615 on its own did not change this scene on MoltenVK: 22.6 and 23.0 FPS without it, 22.9 and 23.2 with it, since the row copies keep the region count. Not tested on Linux, Windows or another GPU. On drivers where a many-region `vkCmdCopyBuffer` is cheap, this change saves the scratch allocation and one copy of the stored bytes per write-back. **After review (`a32475a0ab`, device-local heap condition):** on an RTX 4090 the direct retile made the title 3-5x slower (reported in review); the import there is outside a device-local heap, so it now keeps the scratch path. On the Mac, where the heap is device-local, the path is unchanged: the 34 focused tests pass again (`agc_storage_write_back` now expects the import retile only for an import in a device-local heap and fails if a write-back retiles into one outside it), and the logos run at 32.0 FPS against 21.1 with `APS5_NO_LINEAR_IMPORT_RETILE=1` (one run each, in a tree without #9385). Not run on a discrete GPU. ### 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: #11615 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M5 Pro, MoltenVK (Vulkan SDK 1.4.363), macOS 27.2 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/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!13044
No description provided.