perf(agc): answer repeated depth-surface lookups from a per-thread miss memo #26681
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/26681"
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 meh7an on 2026-11-24T15:06:25Z.
What
RepeatStorageSurfaceruns once per resident color target of every draw on the committer. It first asksDepthSurfaceStoragewhether a depth surface holds the target's address, and on the Sonic Origins S3K menu page (add-ons installed) the answer is almost always no: every call takessurfacesMutexand walks the registry back to front (41 surfaces there), a few dependent pointer loads per entry. In the sampler windows below that call is 0.42 to 0.45% of the committer, and the in-process rotation shows about 340 ns of the 852 ns the rest of the function takes (RepeatStorageSurface, excluding theRefreshit calls).This PR keeps a per-thread memo of the (device, address) pairs the scan found unclaimed, each with the registry serial (
surfacesSerial, whichDepthSurfacesSerial()already returns) that was read undersurfacesMutexright before the scan. A lookup that finds its pair with the current serial returns null without the lock or the scan. 32 direct-mapped slots of 24 bytes per thread (ThreadScratch, so no emulated TLS), no budget to trim, no new environment variable and no switch.Why a remembered miss stays correct until the serial moves (read on main
05bbb26cd34):active, on the same device, notretired, and whosetarget.addressortarget.stencilAddressequals the address.targetandcontextareconstmembers, so a surface never changes address or device.surfaces().push_backinDepthSurfaceViewWithStencilWriteIntent(whatDepthSurfaceViewcalls), undersurfacesMutexand right aftersurfacesSerial.fetch_add.retiredgoes from true to false in one place, the same function, which bumps the serial first when it was true.activeis initialised true and only ever stored false (fivestore(false)sites); nothing reactivates a surface.releaseRetired,retire,ClearDepthSurfaces), which can turn a hit into a miss and never a miss into a hit.mip != 0andbaseLevel != 0return null before the memo, as before; the memo is consulted only for the question the scan answers. Positive answers (a surface was found) are never remembered, so everything after the scan (Storage, the layered views and their side effects) runs as on main.DepthSurfaceStorageScans()counts the calls that reached the scan; the new test uses it to show that five lookups of one unclaimed address scan once. Its callers (RepeatStorageSurfaceandcachedStorageTexture) are unchanged.What it shows. Per call, in one run,
RepeatStorageSurface(excluding theRefreshinside it) goes from 852 ns to 512 ns, a saving of 340 ns (40%) that holds in all ten steady windows (table below): the memo hit rate is 98.85%, and at 25,000 calls per second that is about 8.4 ms of committer time per second (0.84%). In the sampler windowsDepthSurfaceStoragegoes from 0.43% to 0.12% of the committer's samples andRepeatStorageSurfacefrom 3.32% to 3.02%. The frame is flat. The sampler windows read 16.34 FPS on main and 16.11 with the change, the traced runs 16.88 and 16.73 (frame p50 63.9 and 64.3 ms, GPU busy 30.7 ms in both), all inside this scene's run-to-run spread (main alone spans 16.64 to 17.13 in the traced runs). Both threads are close to their limit in this scene (the worker is blocked 18.7 to 20.5% of its samples inDrawPipeline::EnqueueandDrainin these windows, and the committer idled 19% of its samples in the main windows of 2026-11-23), so a committer saving of about 0.3 ms of a 62 ms frame is not expected to show in the frame rate. I am proposing it for removing one lock and one 41-entry walk from every draw, not for a measured frame-rate gain.Tested
Windows 11 Pro (10.0.26300), NVIDIA GeForce RTX 2080 Ti (driver 610.88), Intel Core i9-9900K. Sonic Origins (PPSA05324) menu on the S3K page with the four add-ons, 3840x2160, default environment. Measured commits: main
05bbb26cd34and this PR's3271af07235(one tree, built one after the other; the main arm'sdirtyflag is the new test file only, which no module of the game contains).Tests. New
RunUnclaimedintests/execution/DepthStorage.cpp(agc_driver_depth_storage): repeated lookups of an address no surface holds, of the stencil address, of another device's handle, of addresses inside the plane's memory but not at its base; a depth and a stencil surface created after those misses were remembered; 256 other misses followed by a lookup of the real plane (so that the real plane's slot is shared with a miss); a retired surface (three lookups) bound again;ClearDepthSurfaces; and the scan counter (five lookups of one unclaimed address scan once). The focused set around depth surfaces, storage images, refresh, resident targets and multisample depth (248 tests in 65 executables:agc_repeat_storage_guard,agc_driver_depth_*,agc_driver_native_depth_*,agc_driver_multisample_*,agc_driver_storage_*,agc_driver_own_refresh_*,agc_driver_refresh_*,agc_driver_color_memo_*,agc_driver_frame_replay_(multisample|single_sample)_depth,agc_driver_image_table_reuse_(retaken|retired)*,agc_driver_recorder,agc_storage_*,agc_depth_*,agc_recorder_*,agc_multisample_*,agc_import_*,agc_dcc_key*, and the neighbours listed in the regex of the log): 248/248 on main and 248/248 on the change, plain and withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_LOADER_DEBUG=layer VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationfrom the same machine and GPU (ctest --test-dir build -R <pattern> -V): 849 "Insert instance layer" lines in each validation log, no "was not found but was requested by env var VK_INSTANCE_LAYERS" line, and noVUID-line in either; the result lists are identical (the same four tests skip by design in both). The run on main used the test without the assertions that needDepthSurfaceStorageScans, which main does not have. Mutants: 10 one-line changes of the memo and the serial bumps (the serial, the address or the device dropped from the match, a miss remembered at the next serial, a found surface remembered as a miss, the lookup compared against serial 0, no bump at a creation, no bump when a retired surface is bound again, a miss never remembered, all addresses in one slot); 9 are caught byagc_driver_depth_storage, and the survivor (one shared slot) only costs scans, never a wrong answer.tools/env_reference.py --checkandtools/check_conventions.py --base upstream/mainpass.In-process rotation (a separate instrument build,
m313_instrumentation.zip:RepeatStorageSurfacecycles per call between four bodies on the committer thread in one run, so the machine state cancels; the first two windows are the game loading, windows 3 to 12 are the menu page; times exclude theRefreshinside the function):Steady windows 3 to 12: main body 852 ns per call (785-1044), memo 512 ns (467-655), 340 ns saved per call (40%), memo hit rate 98.85%, 24782 calls per second, so 8.4 ms per second of committer time (0.84%). The two variants I dropped (a one-entry memo of the described resource, and a check of the front cache entry before
findStorageByImage) read 555 and 532 ns, with hit rates of 3% and 1.6%. The instrument's other bodies are not part of this PR.Sampler (
APS5_SAMPLE_WORKERandAPS5_SAMPLE_COMMITTER, one 60 s window per arm, both orders, committed builds; benchmark leases, average outside CPU 3.1% over the four windows, every lease quiet at its start):DepthSurfaceStorageRepeatStorageSurfaceStorageTexture::refreshEnqueue+Drain05bbb26cd3)3271af0723)3271af0723)05bbb26cd3)Means of the two windows per arm:
DepthSurfaceStorage0.43% to 0.12% of the committer,RepeatStorageSurface3.32% to 3.02%,StorageTexture::refresh4.28% to 4.30% (untouched), worker blocked 19.0% to 19.9%; reading FPS 16.34 to 16.11.Traced runs (
APS5_TRACE_SECONDS=200:15, same page, both orders,tools/trace_digest.py; benchmark leases, outside CPU 2.8 to 3.2%, all quiet at their start):05bbb26cd3dirty)3271af0723)3271af0723)05bbb26cd3dirty)FPS main 16.88, this PR 16.73 (-0.9%); pair deltas +0.0% and -1.8% (main first in t1, this PR first in t2); frame p50 63.9 and 64.3 ms; GPU busy 30.7 and 30.7 ms per present; main alone spans 16.64 to 17.13. All four traces lost events the same way (the queue worker's buffer fills after about 5 s), so slice timings cover the first seconds only. The summaries and traces are attached as m313_traces_main_t1_05bbb26cd34.zip, m313_traces_main_t2_05bbb26cd34.zip, m313_traces_pr_t1_3271af07235.zip and m313_traces_pr_t2_3271af07235.zip (each zip holds the run's summary line, trace and digest); the instrumentation is m313_instrumentation.zip (the rotation generator, its base edit and the
[ab-rss]lines of the run).aps5-summary.jsonl lines: main (t1), this PR (t1), this PR (t2), main (t2)
Digest: main, run t1 (commit
05bbb26cd3)seconds 200:15 from present 4501: 226 presents, frame p50 64.4 / p90 73.1 / p99 90.7 / max 99.6 ms, GPU busy 30.4 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 610.88, Windows 10.0.26300, 3840x2160, commit
05bbb26cd3(dirty), dropped: yesIncomplete 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): batch 30.3, Graphics.Draw 23.2, Driver.Packet 13.6, Graphics.Draw.draw_and_resource_release 12.9, VideoOut.Flip 12.0, Driver.Present 11.9, VideoOut.Flip.present 11.9, Recorder.WaitSerial 11.7, Driver.Packet.draw 11.0, Driver.Present.render_fence_wait 10.8
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Digest: this PR, run t1 (commit
3271af0723)seconds 200:15 from present 4535: 226 presents, frame p50 64.5 / p90 71.3 / p99 90.6 / max 91.4 ms, GPU busy 30.7 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 610.88, Windows 10.0.26300, 3840x2160, commit
3271af0723, dropped: yesIncomplete 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): batch 30.7, Graphics.Draw 24.0, Graphics.Draw.draw_and_resource_release 13.8, Driver.Packet 13.6, VideoOut.Flip 11.9, Driver.Present 11.9, VideoOut.Flip.present 11.9, Recorder.WaitSerial 11.7, Driver.Present.render_fence_wait 10.8, Vulkan.Retire 10.8
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Overlap.
tools/pr_overlap.py --branch perf/agc-repeat-storage-surfaceover 2,501 open PRs failed closed on 29 PRs that touchDepthSurface.cpporDepthSurface.hppbut conflict with main themselves. I read the added and removed lines of those and of the other open PRs that touch depth surfaces (37 in all, 2026-11-24) for anything that creates, re-activates or un-retires a surface, or changes a surface's address or device, and for any change toDepthSurfaceStorage's lookup or toRepeatStorageSurface. None changes the lookup. #7256, #24083 and #19557 keep or add thesurfacesSerialbump at their new creation paths. #1368 and #1395 (old, conflicting with main for months, #1395 a draft) addsurfaces().push_backlines without a bump in the added lines; a creation path added by either has to bump the serial as the existing one does, and I will say so there if they are revived. The other hits are test-only, library-split moves (#24322, #24910, #24917) or unrelated lock sites (#24083, #24927, #25534, #25610, #26034, #20186: they addsurfacesMutexusers, not entries). Main moved to6fde67a5eb3while this was prepared: the branch merges cleanly (git merge-tree), and the commits in between that touchDepthSurface.cppdo not touch the registry fields 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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T07:24:10Z.
Review: APPROVED
Reviewed — looks good
The change at head
3271af07235450d733d6ca75c1aad78213b04e61adds a per-thread cache for exact depth surface misses; the inspected code and described checks support merging.What I ran
No tests or builds were run during this review. I read the prepared description, commit summary, manifest, and diff, then used
repo_read.pyto inspect the changed code and its scratch-slot helper at the pinned head.Results
Findings
Blocking
None.
Non-blocking
None.
Sources
Behavior checked in DepthSurface.cpp at the reviewed head, DepthSurface.hpp at the reviewed head, DepthStorage.cpp at the reviewed head, and the existing ThreadScratch.hpp helper. The miss cache matches on device, address, and serial; creation, rebind, and clear paths inspected advance the serial, while the cache only remembers scans that found no surface.
Not verified
No tests, build, runtime behavior, physical-console behavior, or performance captures were independently verified. The author's reported test and performance results in the PR description were not rerun. The only changed files listed in the manifest were read; no files were excluded in the manifest.
To get this to a good state
Nothing is required.