perf(libSceAgcDriver): invalidate draw input snapshots by range #16879
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16879"
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 winstrex on 2026-11-07T19:47:58Z.
What
The draw input snapshot cache (vertex, index and read-only storage copies kept by the
Recorder) dropped every kept copy whenever any guest allocation changed anywhere, because reuse compared the globalGuestAllocationsGeneration. Soul Hackers 2 (PPSA03743) maps, frees or protects memory about 25 times per frame, so almost nothing survived and the committer copied the same unchanged ranges again every frame.A kept copy now stores
ForgetSerial()read before the copy and is reused only while no forget stamp over its own range is newer (ForgetStamp, the per-64 KiB-block stampsForgetPagesalready raises for every changed range, asTexture.cppuses them). A copy taken or checked while a forget is being stored is never reused. CPU and GPU writes are still checked byUnchangedSince.Why this is as safe as the global generation: the three cases the generation covered and the range stamps did not are closed. Each has a test, except installing the invalidator when no page cache could be allocated (see Tests).
RegisterImage,RegisterMainImage) reports its range.RequireUnpinnedalone: an in-place heap reallocation, a direct memory decommit) reports that range.Block granularity and slot aliasing can only cause extra invalidations, never missed ones (278 of 8.6 million checks in Soul Hackers 2).
Tested
Soul Hackers 2, 3840x2160, scene at 169 s, 20 s window; RTX 5070 Ti, NVIDIA 621.01, Windows 11. Latest measurement, at this PR's head
873e614efd, one pass per build. Both builds are local merges of #9712 (1fc6f03ffa) intomain4d5d7184da, the second also with this PR:main+ #9712 (cca3000cc6)main+ #9712 + this PR (942e17eec3)Neither pass waits on present (swapchain 1484x835). The 77 ms present wait in the earlier passes below came from the display being off, which shrank the swapchain to 1100x619; it was not caused by code. With this PR the committer no longer limits the frame; the queue worker does (
Driver.Packet41.8 ms per frame).Traces:
sh2-on-9712-4d5d7184da-pair.zip
Summary lines, latest measurement (aps5-summary.jsonl)
main + #9712:
main + #9712 + this PR:
trace_digest: main + #9712 (latest)
seconds 169:20 from present 3238: 257 presents, frame p50 77.5 / p90 80.7 / p99 90.2 / max 94.8 ms, GPU busy 14.2 ms per present
NVIDIA GeForce RTX 5070 Ti, NVIDIA 621.01, Windows 10.0.26300, 3840x2160, commit cca3000cc636, dropped: yes
Incomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Graphics.Draw 53.9, Driver.Packet 38.3, Driver.Packet.draw 31.6, Driver.Draw 31.4, Driver.Draw.draw_enqueued 28.0, Graphics.Draw.vertex_upload 27.1, Graphics.Draw.draw_and_resource_release 14.2, batch 14.2, Graphics.Draw.index_upload 5.5, Driver.Packet.dispatch 4.4
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
trace_digest: main + #9712 + this PR (latest)
seconds 169:20 from present 4492: 442 presents, frame p50 45.1 / p90 47.8 / p99 50.6 / max 57.1 ms, GPU busy 12.4 ms per present
NVIDIA GeForce RTX 5070 Ti, NVIDIA 621.01, Windows 10.0.26300, 3840x2160, commit 942e17eec313, dropped: yes
Incomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Graphics.Draw 20.0, Graphics.Draw.draw_and_resource_release 12.8, batch 12.4, Driver.Packet 11.7, Driver.Packet.draw 7.2, Driver.Draw 7.1, Driver.Draw.draw_enqueued 5.1, Driver.Packet.dispatch 3.2, Driver.Dispatch 2.4, Recorder.Submit 1.2
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Earlier measurements, kept for history:
Soul Hackers 2, 3840x2160, scene at 169 s, 20 s window; RTX 5070 Ti, Windows 11. Frame p50 in ms, two alternated passes per build:
maindd00a9c54emain+ #9712On
mainthe scene is limited by the GPU reading the copies over PCIe (80 to 85 % busy), so the gain is small; #9712 removes that limit and the gain shows. A lab build with the mapping check removed entirely reaches 44.0 and 43.7 ms, so this PR is at the ceiling of any registry invalidation scheme here.No regression (one pass per build, uncapped present): Hades 18.0 → 17.7 ms, The Pathless 19.1 → 19.3 ms (p50, within noise).
After merging
main0f7eb1e23d(which brings #13793), Soul Hackers 2 again: 120.4, 107.6 ms onmainand 85.2, 105.3 ms with this PR; the committer'sGraphics.Drawhalves in both passes (61.7, 59.5 → 30.1, 30.1 ms per frame), and the second pass is held by the GPU (92 ms busy per frame). On thatmainplus #9712, both builds now sit at 78 ms, with or without this PR, waiting about 77 ms per frame for the swapchain image, so the #9712 pair above cannot be repeated there for now.After merging
main4a467be387(9fbc1a91e6, which brings the write index of #12745), two more passes per side: 140.4, 131.7 ms onmainand 92.8, 99.2 ms with this PR.Conflicts: merges cleanly into
main, but conflicts textually with #9712, #15570 and #16760 (Recorder.cpp), my #9875 (Recorder.hpp,Recorder.cpp,Draw.cpp), and #13809 (sameKeepDrawSnapshotandDrawInputCopysignatures). #13793 is merged and resolved here: its owned draw inputs keep their own proof, and their snapshot entries carry its forget serial. #12745 is merged and resolved here too: it installs the invalidator also when its write index is on, and this PR installs it always. With the write index off, thebumpSubscriptionscall that #12745 added toForgetPageshas no subscriber and returns at its first check.Traces:
sh2-main-pair.zip
sh2-on-9712-pair.zip
Measurement details
Measured at 426e8253a3 on
maindd00a9c54e; the branch now mergesmain4a467be387(9fbc1a91e6), and the validation, the full suite, the mutation check and the last passes were run on that head. NoAPS5_PROFILE_*variable, log not redirected, captured withAPS5_TRACE_SECONDS. Soul Hackers 2 01.002.000, Hades 01.003.000 (1920x1080, window 150:10), The Pathless 02.000.000 (2560x1440, window 110:10), NVIDIA 621.01, Windows 10.0.26300. The passes onmain, Hades and The Pathless used a shader cache emptied beforehand and warmed by one discarded pass ofmainper title. The #9712 pair:main6c1efc7c59merged with #9712 at601e9e99a9(e290eec638), plus this PR (444645e6a2). Hades and The Pathless ran withANYPS5_PRESENT_MODE=immediate.dd00a9c54e)On
mainthe committer'sGraphics.Drawdrops from 52.7 to 22.6 ms per frame, butRecorder.Syncon the same thread grows from 12.7 to 31.3 ms, so the frame gains less while the GPU stays the limit. On top of #9712 the committer drops from 53.7 to 21.3 ms per frame and the queue worker (93 % busy) becomes the limit; this PR measured 44.2 ms in the same session as the ceiling runs.Summary lines (aps5-summary.jsonl)
main:
main + this PR:
main + #9712:
main + #9712 + this PR:
trace_digest: main
seconds 169:20 from present 3008: 193 presents, frame p50 103.4 / p90 109.4 / p99 113.1 / max 116.4 ms, GPU busy 83.3 ms per present
NVIDIA GeForce RTX 5070 Ti, NVIDIA 621.01, Windows 10.0.26300, 3840x2160, commit
dd00a9c54e, dropped: yesSlowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 83.3, Driver.Packet 68.7, Graphics.Draw 52.7, Driver.Packet.draw 40.5, Driver.Draw 40.3, Driver.Draw.draw_enqueued 35.2, Graphics.Draw.vertex_upload 24.8, Graphics.Draw.draw_and_resource_release 16.4, Recorder.Sync 12.9, Driver.Packet.dispatch 7.1
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
trace_digest: main + this PR
seconds 169:20 from present 3012: 210 presents, frame p50 95.2 / p90 100.9 / p99 106.3 / max 110.0 ms, GPU busy 80.6 ms per present
NVIDIA GeForce RTX 5070 Ti, NVIDIA 621.01, Windows 10.0.26300, 3840x2160, commit 426e8253a3ea, dropped: yes
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 81.0, Driver.Packet 67.0, Recorder.Sync 31.4, Graphics.Draw 22.6, Driver.Packet.draw 16.5, Driver.Draw 16.2, Graphics.Draw.draw_and_resource_release 14.5, Driver.Draw.draw_enqueued 11.7, Driver.Packet.dispatch 7.7, Driver.Dispatch 5.4
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
trace_digest: main + #9712
seconds 169:20 from present 6718: 269 presents, frame p50 73.9 / p90 78.4 / p99 83.0 / max 85.1 ms, GPU busy 14.8 ms per present
NVIDIA GeForce RTX 5070 Ti, NVIDIA 621.01, Windows 10.0.26300, 3840x2160, commit e290eec638c2, dropped: yes
Incomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Graphics.Draw 51.5, Driver.Packet 35.4, Driver.Packet.draw 29.0, Driver.Draw 28.8, Graphics.Draw.vertex_upload 27.3, Driver.Draw.draw_enqueued 25.6, batch 14.8, Graphics.Draw.draw_and_resource_release 13.7, Driver.Packet.dispatch 4.2, Graphics.Draw.index_upload 4.0
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
trace_digest: main + #9712 + this PR
seconds 169:20 from present 4638: 451 presents, frame p50 44.0 / p90 47.0 / p99 49.9 / max 56.2 ms, GPU busy 12.3 ms per present
NVIDIA GeForce RTX 5070 Ti, NVIDIA 621.01, Windows 10.0.26300, 3840x2160, commit 444645e6a22f, dropped: yes
Incomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Graphics.Draw 19.6, Graphics.Draw.draw_and_resource_release 12.5, batch 12.3, Driver.Packet 11.2, Driver.Packet.draw 7.1, Driver.Draw 7.0, Driver.Draw.draw_enqueued 5.1, Driver.Packet.dispatch 2.7, Driver.Dispatch 2.0, Recorder.Submit 1.1
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Tests, mutations and cost of the check
Tests: new
agc_page_state_forget_stamps(fresh process: a registry change after the first forget serial is stamped, a backing renewal is stamped, registering the main image and another image stamps each once);drawSnapshotForgetTestsinagc_driver_recorderandagc_draw_snapshot_cap(a change outside the range keeps the copy and the highest index, a change over it, a backing renewal and a decommit drop them, a containing snapshot is dropped, a copy taken during a forget is not reused); the existing tests that expected any registry mutation to drop a copy now check that an unrelated one keeps it and a decommit of the range drops it. Each of these seven mutations fails at least one of these tests: theunpinnedloop inGuestAllocationsEndreports nothing;RegisterMainImageregisters withoutrecordChange;RegisterImageregisters withoutrecordChange;ForgetSerial()no longer callsPages().initialize();MappingUnchangedaccepts an odd stored serial;MappingUnchangedignoresForgetStamp;MappingUnchangedcompares the stamp with<instead of<=. Not covered: putting thearenaCached || imageCachedcondition back aroundGuestAllocationsSetInvalidator_nid_postfix(&ForgetPages)fails no test, because every test process reserves the guest arena at load, so the arena page cache is always allocated; the reservation is static in libc and has no test switch. Redone at9fbc1a91e6, with the libraries the tests load rebuilt for each change and a clean pass before each one: the same seven fail a test, and the install condition still fails none.Cost of the check, microbenchmark (hot cache):
ForgetStamp1.6 ns for 1 block, 6.9 ns for 16, 23 ns for 64. In Soul Hackers 2: about 2,470 checks per frame over 4 to 5 blocks on average, about 60 us per frame in total.Validation layer and full suite
Validation layer:
ctest -R "agc|video_out|shader" -VwithVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1onmain4a467be387(1633 tests) and on9fbc1a91e6(1634), same machine:agc_driver_quad_pixel_mask,agc_driver_msaa_dcc_clear_2,_4,_8andagc_driver_native_depth_publicationfail on both, the rest pass, the layer loaded in both, noVUID-message in either.Full ctest suite on
9fbc1a91e6(local, with GPU): 4862 of 4868 pass;vulkan_validation_gate_commands(symlink privilege) and the five tests above fail; the five also fail onmain4a467be387in the validation run above.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)main4d5d7184da); earlier: 98.5–103.4 → 83.9–95.2 ms onmaindd00a9c54eSummary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T21:55:18Z.
Review: COMMENTED
Read
cba91d189bagainstmain.I checked why per-range stamps are as safe as the global generation.
ForgetPagesraises the slots with a release before it increments the serial, andMappingUnchangedreads the serial before the slots. A copy kept with serial K always reads guest memory after K is loaded (CopyDrawInput,CopyZeroPaddedDrawInput,InPlaceIndexRange,PrepareDrawBindings). Sostamp <= Kmeans the copy was taken after any forget of that range had started, and so after the mapping change that triggered it. Every registry mutator records its range before it changes anything:Add,Remove,Protect,UnmapandUnregisterImagealready did, and this PR addsRegister*Imageand theunpinnedloop.GuestAllocationsInvalidatestill calls the invalidator. The padded-vertex path recomputesreadableand compares it with the kept value, so a range that becomes newly mapped beyondderivedis still caught. I found no missed invalidation compared withmain.Test coverage of one claim. The description says each of the three closed cases has a test. On
mainthe invalidator is already installed in every fixture:agc_page_state_testsrequires the guest arena, and the Windows image cache is on by default, so the base conditionarenaCached || imageCachedis true and no test fails without the unconditionalGuestAllocationsSetInvalidator_nid_postfix(&ForgetPages)atGuestMemory.cpp:689. The tests do cover the other half, thatForgetSerial()now initializesPages(). The code change is right; I'd just say in the description that the no-cache case is untested.Not run here; this is from reading the code.
AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T05:18:06Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the range-based snapshot invalidation logic and the inspected regression tests support the stated behavior.
What I ran
Reviewed head
b76fdc0cd21bc3a4a02f2b49dc5c961fe04b2f46using the prepared diff andrepo_read.pyfor targeted source ranges. No tests or build commands were run. No merged-preview check was performed.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The full contents of
core/libs/prx/libSceAgcDriver/tests/Recorder.cppandcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppwere excluded from the review folder; only targeted ranges at the pinned head were read. Build, automated test results, and physical-console behavior were not verified. Other changed files were reviewed from the prepared diff and targeted context; files excluded by rule were not present in the reviewed-file manifest.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T08:40:07Z.
Review: COMMENTED
Reviewed — no blocking findings
The reviewed code supports the range-based invalidation approach, but the review is incomplete because two changed files were excluded from the bundle and tests were not run.
What I ran
Reviewed head
5336b7786e5657de46f43d4ee34ba7428f9341e9usingrepo_read.pyto inspect the prepared diff and targeted source ranges. No tests or builds were run.Results
Negative control
Not run; no tests were executed.
Findings
Blocking
None identified in the code inspected.
Non-blocking
None.
Sources
Behavior claims are based on the prepared diff and source at the reviewed head; no permalinks were available through the reader interface.
Not verified
ShaderResources.cppandtests/Recorder.cppwere excluded as full files; only relevant ranges were read throughrepo_read.py. Other portions of changed files were not read in full. Tests, build, and console behavior were not verified. The changed files listed as included in the manifest were:GuestMemory.cpp,Draw.hpp,Recorder.hpp,Draw.cpp,Recorder.cpp,PageStates.cpp, andagc_page_state_tests.cmake; relevant ranges were inspected, not every line. Excluded changed files:ShaderResources.cppandtests/Recorder.cpp.To get this to a good state
Nothing is required. Optional: inspect the remaining portions of the excluded files and run the relevant tests before making a formal approval decision.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-02T23:22:39Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Concurrent page invalidations can make the forget serial appear stable while a range is still being invalidated, allowing a stale snapshot to be reused.
What I ran
Reviewed head
02cd81ca11c9700c4da82e50a79c5a56608f052eusing the prepared diff and targeted source ranges fromrepo_read.py. No tests or builds were run; this review environment is read-only, and running tests was not established to avoid compilation.Results
Negative control
Not run; no non-compiling negative control was identified.
Findings
Blocking
Recorder.cpp:3838—MappingUnchangedtreats an evenForgetSerial()as proof that no forget is in progress.ForgetPagesusesfetch_addat both entry and exit, so overlapping calls can make the serial even while one call is still storing page state. A lookup can then observe an even serial and an old range stamp and reuse a snapshot while its mapping is being invalidated. This is reachable through direct invalidation paths that do not serialize on the guest allocation registry mutex. Use synchronization that reliably represents all active forget operations, and add a concurrent regression test.Sources
Not verified
No tests, builds, or runtime behavior were verified. Targeted diff and source ranges were read for the changed implementation and related invalidation callers. Full text was omitted from the review folder for
core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppandcore/libs/prx/libSceAgcDriver/tests/Recorder.cpp; targeted ranges were inspected through the pinned reader.files.jsonlists no rule-excluded files.To get this to a good state
Make the in-progress invalidation check safe when
ForgetPagescalls overlap, and add a regression test that exercises concurrent invalidations. Then rerun the relevant tests on the new head.Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-12-02T23:24:54Z.
Review: COMMENTED
Actionable comments
99fc4314-d1fb-4358-aa36-3b577519884aReviewing files that changed from the base of the PR and between
5336b7786eand02cd81ca11.core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Draw.hppcore/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libc/src/GuestAllocations.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T22:37:18Z.
Review: COMMENTED
The trial merge onto
c1601e56d5conflicts inGuestAllocations.cpp. Keep the invalidator spanning allocation mutation and stamp publication when rebasing. The meaningful pair is reuse after an unrelated mapping change and invalidation after an overlapping one, including an old cached index-range result. The new range-specific proof needs both controls. Any speed claim needs a pairedtrace_digest.pycomparison for the same scene and machine.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.