perf(agc): hand aliased storage memory over without the guest round trip #4286
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4286"
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 AlxcBTW on 2026-10-12T04:22:11Z.
Sword of the Sea (PPSA25344) menu, with host imports write-watched: frame time 369 → 326 ms, GPU 128 → 104 ms per frame (and 495-517 → 355-362 ms with the unit shadow on). With Windows' default compare-mode imports the change is neutral; see Configurations below.
What
Unreal's render graph aliases transient resources: the memory of a storage image A that still has results pending is handed to a surface B of another format and shape in the same frame.
StorageTexture::Refreshof B stored A's results first and then uploaded B from that memory. That is a retile and a store through guest memory (PCIe on host-imported memory), then a detile back, at every hand-over. On Sword of the Sea most of the frame's storage write-backs and refresh uploads were these round trips, and the refresh waited for them.Where B's content does not come from guest memory, A's results now stay pending:
Only 64 KiB block units qualify, and only when:
What happens to them afterwards:
markLayersPending), they are dropped. B's write-back then overwrites every byte they would have stored.With
APS5_PROFILE_DRAW, a[storage] hand-overline counts the units every 10 s. There is no switch: the old order is kept wherever B's content comes from guest memory.Measured
Sword of the Sea menu, Windows 11, RTX 5070 Laptop (AMD iGPU hidden). Two builds of
maina11c8f7f7plus the lab's local stack, one with and one without this commit; the libraries are otherwise the same. Rows of 180 s, alternating, with the figures per 10 s:Per 10 s with the change: 85.6 k units left pending under a refreshed surface (48.9 k of them taken on the device), 64.8 k superseded by the surface's own results, 3.7 k stored later, no fallbacks.
The picture at 1:1 does not change. The menu's dark specks grow at the same rate per frame with and without the change: 0.027 % per frame without, 0.026 % with. Interpolated to the same frame, the dark share of the sky band is 12.2 % and 12.1 % at frame 431, and 16.2 % and 16.0 % at frame 589. The top luma is 74 → 72 without the change and 75 → 71 with it. The specks are the depth fast clear that #3816 addresses, unrelated to this change.
Before this shape, an experimental build with a verify mode stored everything as before and checked every hand-over decision against what a later reader of the memory saw. It reported 0 mismatches in 4.26 M checked blocks over two 180 s runs.
Configurations
The rows above ran with host imports kept write-watched (
APS5_WRITE_WATCH_IMPORTS=watchwith a local change that lets it apply to Windows direct imports) andAPS5_NO_UNIT_SHADOW=1. The hand-over removes write-backs and refresh uploads that watched imports trigger. Measured again onmaind146ec1b6vsmain+ this change, 180 s rows, alternating:mainframe msSo on Windows with default settings this change neither helps nor hurts this title; the gain applies where imports stay watched, which on Linux is the case when the import probe finds that the driver does not resolve write protection. A Linux measurement would show which applies there.
Tested
Windows 11 (26200), WinLibs GCC 15.2.0, Release, rebased onto
maind146ec1b6.agc_driver_alias_handover_memoryfails onmainwiththe refresh of a surface under clear keys stored the pending results of the image it aliases over its memory, and passes with this change. A 256x256 RGBA8 image A has results pending; a 128x128 RGBA16F surface B under 0000 keys over A's first two units is refreshed. Afterwards guest memory still holds the old bytes there and A stays pending. B's own results then supersede A's two units, and guest memory ends with B's results over B and A's elsewhere.agc_driver_alias_handovercovers B under clear keys and B from memory (taken on the device), each with three orders: A never read again, read before B wrote, and written again after the hand-over. Each order runs in unwatched and watched memory, with and without the hand-over (SetAliasHandOverForTests). The guest bytes and B's texels are the same both ways, and the hand-over counts are the expected ones: units handed over, superseded and stored later.ctest -R "^agc_": 424/424 pass, including bothagc_driver_alias_handovertests. Before the rebase, onmainbab7b3285: 420/422 (agc_write_trackingandagc_driver_prepared_shaders_registered_workgroup_syncfailed the same way without this change).ctest -E "^agc_driver|^video_out|^guest_video", ond146ec1b6plus this change): 582/583 pass.log_macrosfails there ("a short line differs"); the change does not touch it.tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T04:36:53Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppL1974Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T05:02:02Z.
Preparing a Windows native cross-test of your
fd0bfc6a5change, with your author retained. It applies cleanly in our newer-main native-test checkout: https://github.com/Scramblrrrr/AnyPS5/tree/codex/alias-hand-over-native-cross-test , head 83550becbdf519b85e226ad495f2892b3d97e514 (parent 2da9b8f1e, containing our warm host-hole test).Not built, tested or staged yet. I will run both alias hand-over fixtures plus nearby storage/recorder checks after the active 007 run. The actual game still uses our older runtime-image-table integration, so this native branch is not a title-running stack. Reviewing #3813 and the full final #3777 chain as prerequisites for updating that integration; no competing implementation.
AI-assisted: Codex. No title data uploaded.
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T05:02:57Z.
@Scramblrrrr thanks for cross-testing. A note so the numbers line up: the gain depends on the import mode, see the Configurations section I added to the description. With Windows' default compare-mode imports the change was neutral on our title (301/285 vs 292/292 ms). With imports kept write-watched it was 495-517 → 355-362 ms. So if your title stack runs default imports, expect the fixtures to pass and little frame-time change; a watched-import config, or Linux where the probe leaves imports watched, is where the hand-over shows.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T05:19:57Z.
Source-only follow-up prepared on the actual 007 integration: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-alias-hand-over-cross-test , head 97737cb6b (full hash available on branch).
Starting from the tested/running clip-cull head 188d80147, this carries the complete merged #3777 chain (including global-idle scoping and waking queue zero), the complete merged #3813 lifetime/tests chain, then #4286
fd0bfc6a5. All original authors retained; the carries applied cleanly through three-way integration. No local depth-reuse bypass added.Not built, tested or staged yet. The current game remains graphics 188d80147 with the fresh clip cache. Native storage lifetime, idle-writeback, alias hand-over and nearby recorder checks are next after this run. The separate newer-main native branch 83550becb also remains prepared, not tested. No FPS claim for this storage stack.
AI-assisted: Codex. No title data uploaded.
Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T05:36:15Z.
Windows native cross-test on the actual 007 integration passed 13/13 selected tests in 9.38 s, no skips, on RTX 5070 Ti / driver 617.14. Head 97737cb6b221be51987bf09ba52d29dc5fe2e280, branch https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-alias-hand-over-cross-test . Carries #4286
fd0bfc6a5after the complete #3777 and #3813 chains, on our tested clip/cull integration. Original authors retained.Both alias_handover and alias_handover_memory passed, as did storage_upload_lifetime, storage_upload_failure, store_idle_results, graphics, BDA, GPU buffer formats, draw-cache reuse with GPL on/off and all three clip/cull modes. This is the retail integration branch; the separate newer-main branch 83550becb remains untested.
Not staged into 007 yet and no 007 FPS claim. The previous game run reached the rendered menu and then hit existing #2975 during campaign loading, so I am adding the already merged #3523 before the next run. That source-only follow-up is 903d5034b and is building now.
AI-assisted: Codex. No title data uploaded.
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-12T06:29:47Z.
Pre-review found a deterministic test failure on Linux/RADV — flagging early since full CI doesn't run automatically on PRs.
Independent pre-review (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release,
BUILD_TESTING=ON, AMD RX 9070 XT / RADV GFX1201):tools/check_conventions.pypasses.agc_driver_alias_handoverfails 5/5 runs (the--alias-handover-memory-onlysibling passes):The failing assertion is the
imageHolds(imageA, 256, 256, red)check afterimageA->Refresh()— A loses its results after the alias hand-over on this setup. Along the way the log showsstorage image …: guest memory changed while GPU results were pending; keeping the CPU's blocksseveral times, which may be related to the watched-memory path.Everything else in the suite passes (922/923 — only this test). Since the assertion is GPU-result visibility, this may be driver/timing-sensitive (RADV here) — if it's green on your setup, it's worth a look at whether the read-back path has a race the RADV timing exposes. Reproduce with:
Happy to provide the full test output or run any variant that would help narrow it down.
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T06:30:39Z.
@blawesom thanks, that's exactly the kind of check this needed. It's notable that the failing arm is Off (hand-over disabled, i.e. the old order), in watched memory, after A was read before B wrote. Together with the
guest memory changed while GPU results were pending; keeping the CPU's blockslines, that points at the watched-memory path on Linux treating the driver's own stores as CPU writes and dropping A's results. If so, it's an assumption in the test's Off expectations that holds on Windows but not on Linux, rather than a race in the hand-over. I'm reproducing it on Linux now (WSL2 with lavapipe first) and will either fix the test's expectations for that arm, or the path itself if it's a real bug, and report back here. The full log of the failing run would help. Thanks for the offer.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T07:24:26Z.
@blawesom thanks, the 5/5 made this easy to pin down. The hand-over isn't involved, and main is fine in the configuration it actually runs on your card: the test's watched arm forced a configuration main never uses on RADV. Fixed in
0a82d7bee.Cause. The watched arm called
SetImportWatch(ImportWatch::Watch), which overrides the import probe. On Linux,ProbeImportWriteProtectionchecks whether the device's own reads and writes of a host import resolve the userfaultfd write protection. If they do, imported ranges are compared instead of watched, and your output should have[write-watch] host imports resolve write protection: yes (...)near the top.On amdgpu they do. RADV imports a host pointer as a userptr BO without the read-only flag (libdrm's
amdgpu_create_bo_from_user_mempassesANONONLY | REGISTER | VALIDATE). Every command submission that carries it faults all of its pages in writable again:amdgpu_cs_parser_bos→amdgpu_ttm_tt_get_user_pages→hmm_range_faultwithHMM_PFN_REQ_WRITE. With async uffd-wp, that write fault only clears the WP bit (do_wp_page), so the nextPAGEMAP_SCANreports every page of the import as written. The tracker then stamps all of A's blocks as CPU stores, although no byte changed.In "A read before B wrote, Off", A's refresh sees CPU stores over the units it still holds pending (the half B doesn't cover). The drop rule gives those blocks to the CPU, which is your
keeping the CPU's blocksline. A re-uploads the 0x55 fill there, and the check fails. That log line alone isn't the signal, by the way: the unwatched arm prints it four times on NVIDIA and on lavapipe too.Fix. On Linux the watched arm now follows the probe. Where imports are compared, it prints
host imports are compared on this device: alias hand-over (watched) not tested; the unwatched arm andagc_driver_alias_handover_memoryrun as before. Windows keeps the forced watch, which passes on NVIDIA. There is no driver change.Verified.
alias hand-over (watched), B under clear keys, A read before B wrote, Off: A read again as A lost its results, with the same log line. Without the emulation it passes.yes (0 of 4 scratch pages written after a GPU read, 4 after a GPU write, 0 after the import), so lavapipe compares its imports too. With the fix, the watched arm reports "not tested" and both alias tests pass. Forced to watch (the old test), lavapipe fails as well, at a different check:A read before B wrote: B's results did not reach guest memory under Off. Its copies into the import are CPU stores.ctest -R ^agc_on Windows/NVIDIA: 424 of 424 passed. Fork CI with0a82d7beemerged into main2ca6198e9: Linux/lavapipe 940 of 940 passed (check_skipsgreen), Windows 934 of 934.tools/check_conventions.pypasses.Could you rerun
ctest -R agc_driver_alias_handover -Von RADV with0a82d7bee? The watched arm should now report "not tested". Please also paste the[write-watch] host imports resolve write protectionline, so I can check the probe's numbers on GFX1201.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-12T07:43:09Z.
Reran on RADV with
0a82d7beeas requested — the fix works here.The probe line on GFX1201:
ctest -R agc_driver_alias_handover -V(same machine as the original 5/5 failure — CachyOS, kernel 7.2.9, GCC 16.2.1, Release, RX 9070 XT / RADV GFX1201):agc_driver_alias_handover: Passed (0.42 s) — the watched arm now reportshost imports are compared on this device: alias hand-over (watched) not tested, exactly as predictedagc_driver_alias_handover_memory: PassedFull suite: 923/923 passed (was 922/923 — the only failure is gone). Conventions still pass.
For the record on the original failure mode: the machine reports
4 of 4 ... after a GPU read— i.e. amdgpu'sHMM_PFN_REQ_WRITEfault-in on every submission, matching your root-cause analysis. The old test forcingImportWatch::Watchover the compare-mode probe was indeed testing a configuration this device never runs.Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T07:43:52Z.
@blawesom thanks for the rerun. The probe numbers match the mechanism: 4 of 4 pages are reported written after a GPU read, so RADV compares its imports, and the watched arm correctly reports "not tested" there. From our side the PR is ready: Windows/NVIDIA, Linux/lavapipe (fork CI) and now RADV GFX1201 all pass.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T09:15:05Z.
Two things before this can be tested:
Texture.cpp:1626andTexture.cpp:2174flush with the hand-over filters and then flush again without them when the keys turn out different (thefallbackscounter). Falling back after the fact isn't allowed here; please choose the path from the keys before the first flush, or throw.mainselects by itself gets faster? Windows defaults are neutral (301/285 vs 292/292 ms), the 495-517 → 355-362 ms row forces watched imports, and blawesom's probe line shows RADV compares host imports as well. For +930 lines inStorageTextureit needs a before/after on a title in such a configuration.Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T09:35:10Z.
@oneandonlydean fair points, and the second one decides it. In the configurations
mainselects by itself I have no gain to show:maindoesn't choose on Windows.So +930 lines in
StorageTexturefor a path no default configuration benefits from isn't worth carrying, and the after-the-fact fallback you flagged would need a redesign on top. I'm closing this. If a configuration turns up wheremainwatches imports and storage round trips show in the frame time, I'll come back with a before/after from that configuration and the path chosen from the keys up front. Thanks for the careful review, and thanks again to @blawesom for the RADV runs.AI-assisted: yes (Claude Code)
Pull request closed