perf(agc): move per-draw thread-locals to TEB slots #6615
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6615"
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-10-15T23:04:49Z.
What
With MinGW, every
thread_localaccess goes through__emutls_get_address, a winpthreads key lookup. #6292 moved the draw path's locks and its hottest thread-locals to TEB slots (HostThreadSlot). This moves the ones left on the revalidation path, which every draw and dispatch that reuses cached resources takes:ShaderResources::fastRevalidate's five scratch vectors andRevalidate's four (overlapping,refreshed,versions,regions) now go throughThreadScratch<T, Tag>(): the thread's object in aHostThreadSlot, created on first use and destroyed with the thread byThreadOwned, as before;Texture.cpp'srefreshingexemption, read byStorageTexture::FlushPendingand two other pending-store scans, is aHostThreadSlot<const StorageTexture*, RefreshingTag>;Driver::sampledReadis aHostThreadSlot<bool, SampledReadTag>, throughsampledRead()andsetSampledRead().On the S2 page of Sonic Origins Plus's main menu, the queue worker spent, per frame (APS5 sampler, 15 s):
__emutls_get_address: 1.24 to 1.51 in six runs without this change, 0.92 and 1.17 in two runs with it;SampledReadScope: 0.15 to 0.24 without, 0.07 and 0.13 with;fastRevalidate: 0.66 to 0.72 without, 0.56 and 0.63 with.That is about 1% of the worker's frame (34 to 37 samples), so the frame times below move within run-to-run noise. Thread-locals elsewhere stay as they are.
Tested
Windows 11 (10.0.26300), WinLibs GCC 15.2.0 (UCRT), Release, NVIDIA GeForce RTX 2080 Ti, driver 617.14.
53a0a27bb(c4a6636f7) without conflicts.agc_driver_image_array_window_past_layer_limitis not run: MinGW does not build itsalignas(65536).agc_driver_pipelined_dispatchfailed once under--parallel 4. It passed 8 of 8 runs alone.1d4164552.Driver.hppkeeps main's indirect-source declarations next tosampledReadandsetSampledRead, andTexture.cppkeeps main'sBufferPool.hppinclude.StorageTexture::WaitForFlushing(5937ef03e) reads the refreshing texture throughRefreshing::Get().agc_driver_image_array_window_past_layer_limitis not run: main does not compileImageArrayWindowPastLayerLimit.cppwith MinGW (alignas(65536)).agc_driver_pass_input_hazard(added inba4571a25) fails on plain main1d4164552as well, 0 of 4 runs alone.c84249186, with this change the full ctest suite passes 1321 of 1323 tests.agc_driver_gpu_indirect_arguments_misaligned_writer_aheadis not run: main itself does not compile it with MinGW (alignas(65536)).agc_driver_multisample_fill_host_onlycan fail ("the sentinel fill did not land"): that is main's own race, #6512, which #6522 fixes, and #6292 already depends on #6522 for it. It passed in this run, and with #6522's line added it failed 0 of 200 runs (8 at a time) on main72c5eed1f.guest_shader_linktimed out once. It returns frommainwith the driver's threads still running, the exit hang of #6791 (#6925 fixes it foragc_shader_helpers):Driver::stop, run atDLL_PROCESS_DETACH, waits on threads thatExitProcessalready ended. Run alone 400 times, 8 at a time, it hangs in 55 and 58 runs on plain mainc84249186, 74 with #6292 and 97 with this change.APS5_TRACE_SECONDS=200:15on the same machine in one session, caches warm. Traces attached.The four windows of the moving background I captured in that session read 112.3 to 119.3 ms at the median, so this pair's gap is mostly noise. The FPS shown in the title went from 9.58 to 9.74 (median of six readings), and a sampled pair taken an hour earlier went from 9.07 to 9.21.
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)Traces and summary lines (baseline 4e46e7eba, whose summary says a0dd42cf9b44; this change 99bf270c7, whose summary says 4e46e7ebac25):
teb-traces.zip
The two summary lines, with
tools/trace_digest.pyfor each trace:Baseline (summary commit a0dd42cf9b44)
seconds 200:15 from present 2708: 124 presents, frame p50 119.3 / p90 133.3 / p99 151.6 / max 172.5 ms, GPU busy 84.7 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit a0dd42cf9b44 (dirty), dropped: yes
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 84.7, Driver.Present 32.5, Driver.Present.render_fence_wait 32.0, Vulkan.Retire 32.0, Vulkan.Retire.wait 32.0, Driver.Packet 21.3, Driver.Packet.draw 20.4, Driver.Draw 20.2, Driver.Draw.draw_and_resource_release 5.0, Graphics.Draw 4.9
Longest waits
This change (summary commit 4e46e7ebac25)
seconds 200:15 from present 3226: 130 presents, frame p50 113.5 / p90 128.9 / p99 143.9 / max 144.3 ms, GPU busy 83.3 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit 4e46e7ebac25 (dirty), dropped: yes
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 83.2, Driver.Present 33.0, Driver.Present.render_fence_wait 32.5, Vulkan.Retire 32.5, Vulkan.Retire.wait 32.5, Driver.Packet 19.4, Driver.Packet.draw 18.6, Driver.Draw 18.5, Driver.Draw.draw_and_resource_release 4.1, Graphics.Draw 4.0
Longest waits
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-16T17:39:13Z.
Rebase handoff — rebased onto current
main(both commits, authorship unchanged). The conflicts were the lock/TLS replacements landing on structuresmainhas since extended: inDriver.hppandVulkanDevice.cppthe PR'sAgcDriver::Mutexswap now applies tomain's fuller field sets (draw relocations, journal replay, pipeline counters), and inPipeline.cppboth includes are kept with the mutex swap plus the PR'sscratchKeyfield.Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release,
BUILD_TESTING=ON, AMD RX 9070 XT / RADV):tools/check_conventions.py --base origin/mainpassesmainregression (agc_driver_tessellationsegfault, #4727), not this PRTo pick it up:
Head is
5c61f9f1d.Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-16T22:14:01Z.
Data point from a CPU-bound scene on Windows: RTX 3060 12 GB (NVIDIA 617.42), i5-12400F, Windows 10 19045, MinGW GCC Release. Minecraft Dungeons II (PPSA16064 01.100.100), title screen, no input, 200 s rows,
APS5_PROFILE_DRAW=1 APS5_PROFILE_GPU=1;mainbc3c0fc13vsmain+ this PR, interleaved with five other PRs in one batch, two rounds. FPS is the mean of the last 10[present]windows, GPU busy and idle gaps per frame over the last 8. This scene is CPU-bound on this machine (45-47 ms of idle gaps per frame).mainWithin the run-to-run spread (about ±1.5 % here), no regression, 0 fatal.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by moong15 on 2026-10-19T13:16:48Z.
Evidence from Stellar Blade on Windows (MinGW build): this stack gives about +7% presents in gameplay over two pairs.
What was measured. This PR's stack (#6615 including #6292; #6522 was already in our tree) ported onto a downstream perf branch: origin/main
4ffb0cd12plus local AGC performance changes. The port keeps the PR's mechanism (atomic mutexes, explicit GPU-lock recursion, TEB slots) adapted to our tree. Base is the same tree without the port. It is not a clean main-vs-PR comparison.ab1a4186593e, port1b89c36184a1(both clean builds,dirty: false)APS5_FLAT_3D_COLOR_TARGET=1 APS5_CONTINUE_AFTER_FLIP_FAILURE=1 APS5_PREBUILD_THREADS=16 APS5_PIPELINE_DMA=1 APS5_HOST_IMPORT_MIB=16384 APS5_TRACE_SECONDS=0:150 APS5_TRACE_SUMMARY_EVERY=10 APS5_TRACE_EVENTS=4194304, warm shader cache, noAPS5_PROFILE_*APS5_PAD_INPUT_FILE, every 5 s from 8 s). Window: 49-150 s after launch, which is in-world gameplay after the first load.trace_droppedis false in every window.Before the port, a queue-0 sample profile of steady gameplay had about 4.9% of queue-0 time in winpthreads lock and TLS calls (
__emutls_get_address2.1% including its spin lock 1.5%,pthread_mutex_lock/timedlock1.9%, unlock 0.5%, rwlock 0.4%).Traces and summary files are available on request.
trace_digest, run 1 (base)
1912 frames over 149995.1 ms (no summary line found)
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 134.8, Driver.Packet.memory_wait 65.6, batch 35.4, Driver.Packet.dispatch 30.0, Graphics.Draw 27.7, Driver.Commit.draw 27.6, GpuMutex.hold.draw 27.3, Graphics.Draw.draw_and_resource_release 23.4, Driver.Dispatch 20.9, Driver.Packet.draw 15.6
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
trace_digest, run 2 (port)
2103 frames over 149914.4 ms (no summary line found)
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 123.2, Driver.Packet.memory_wait 59.8, batch 33.3, Driver.Packet.dispatch 26.8, Graphics.Draw 24.4, Driver.Commit.draw 24.3, GpuMutex.hold.draw 24.1, Graphics.Draw.draw_and_resource_release 20.8, Driver.Dispatch 18.2, Driver.Packet.draw 14.6
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
trace_digest, run 3 (base)
1888 frames over 149975.6 ms (no summary line found)
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 136.7, Driver.Packet.memory_wait 66.7, batch 35.6, Driver.Packet.dispatch 30.7, Graphics.Draw 27.2, Driver.Commit.draw 27.2, GpuMutex.hold.draw 26.9, Graphics.Draw.draw_and_resource_release 23.5, Driver.Dispatch 21.6, Driver.Packet.draw 15.2
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
trace_digest, run 4 (port)
2059 frames over 149945.3 ms (no summary line found)
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 124.8, Driver.Packet.memory_wait 60.8, batch 35.0, Driver.Packet.dispatch 26.6, Graphics.Draw 25.9, Driver.Commit.draw 25.8, GpuMutex.hold.draw 25.6, Graphics.Draw.draw_and_resource_release 22.1, Driver.Dispatch 17.9, Driver.Packet.draw 14.8
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T12:15:59Z.
This pull request is labeled
perf, so it needs the summary line of a capture onmainand one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste bothaps5-summary.jsonllines, attach the two traces and add the output oftools/trace_digest.py.Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-21T18:05:51Z.
Review: COMMENTED
The profile is honest about the size:
__emutls_get_address1.24-1.51 samples per frame down to 0.92-1.17,fastRevalidate0.66-0.72 to 0.56-0.63, "about 1% of the work". That is a small gain for 813 lines, stacked on #6292 (708 lines) and rebased onto it repeatedly.ThreadScratch<T, Tag>layer plusHostThreadSlotconversions of nine scratch vectors and two thread-locals. Every future thread-local on the revalidation path now has to choose betweenthread_localand this, and Linux/macOS keepthread_local, so the two behaviors must stay identical. State the rule for new code, and havecheck_conventions.pyflag a newthread_localin the draw path, otherwise this regresses silently.APS5_PROFILE_*) so the 1% can be seen or dropped.HostThreadSlotconversions can go in with #6292 as the second stage I suggested there, and this PR closes.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-30T18:44:09Z.
Review: COMMENTED
On the NVIDIA CI build failure at
07bb9f628('refreshing' was not declared in this scope,Texture.cpp): this is a merge skew with #8911, not a toolchain issue.#8911 (
68f51b5a0b, merged 25/10, after this head) added a new reader of the oldthread_local refreshingin thenothingToStorelambda at the top ofStorageTexture::FlushPending. This PR replaces that variable with theRefreshingHostThreadSlot, and git merges the two cleanly, so the new line keeps naming a variable that no longer exists. It is the only error: a-k 0build oflibSceAgcDriveron the merge withmainf91137142reports just this line.Fix, one line in the merged
FlushPending:With it,
libSceAgcDriverbuilds (Linux, GCC 15.2, Release,-DANYPS5_ENABLE_SPIRV_TOOLS=ON). On an RTX 3070 (NVIDIA 595.99.02),agc_driver_queued_index_read_order(#8911's test) and the 39 tests inagc_driver_graphics_testspass, 40/40 in each of 3 runs. I did not run the full suite.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.