perf(agc): retile a whole-layer linear storage write-back into the host import #13044
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13044"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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,
76bfb762b3anda32475a0ab, 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
VkBufferCopyper row. On MoltenVK that per-row copy is the cost. MoltenVK encodes every region of avkCmdCopyBufferas 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::encodetaking 75 % of MoltenVK's submit thread, and a counting wrapper aroundvkCmdCopyBuffershowed 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:
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 thestoredbarrier after it (compute stage, shader writes). In every other case the scratch path of #11615 stays.APS5_NO_LINEAR_IMPORT_RETILE=1forces the scratch path.StorageTexture::LinearImportRetiles()counts the write-backs that retile into the import, for the test.Overlap
#9674 replaces
geometry.GuestLayerOffset(layer) + mip.tiledOffsetwithgeometry.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.
mainalone does not build on macOS. I ran everything in a tree with the open macOS port PRs, today'smainand #11615; this commit's diff is identical there.agc_storage_write_back(agc_driver_recorder_tests --storage-write-back-only):agc_storage_write_back_scratchruns the same test withAPS5_NO_LINEAR_IMPORT_RETILE=1, and requires the scratch path.Negative controls, each of which fails the test:
Focused tests
^(agc_storage_|agc_driver_storage_|agc_driver_linear_target_), 34 tests, withctest -V,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1: 34/34 pass. The only validation message isVUID-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 reportswas 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.Performance trace summaries (
APS5_TRACE_SECONDS=6:8, inside the logos for both builds),#11615merged into the test tree (393616a491) against the same plus this commit (b2d3dd4548), alternated:Driver.Packet.prepare_memoryfalls from 23.2 to 8.8 ms per frame andRecorder.WaitSerialleaves the top phases (18.6 ms per frame before).#11615:
This PR:
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
vkCmdCopyBufferis 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_backnow 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 withAPS5_NO_LINEAR_IMPORT_RETILE=1(one run each, in a tree without #9385). Not run on a discrete GPU.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code