perf(agc): forward an indirect record's patched user word as push data #9248
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9248"
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 fisherjoey on 2026-10-21T16:32:53Z.
What
On the CPU path of an indirect draw, each record patches a user word (base vertex, start instance or draw index) into the programs that hold it, and
Driver::drawcaptures and recompiles those stages again per record. WithAPS5_DRAW_CACHE_INDIRECT(#9243) a hit skips the first pass and reuses the fragment stage, but that per-record materialization stays: 24 to 31 us per record, about 17% of the queue 0 time on Demon's Souls, where everyDRAW_INDEX_INDIRECTpacket patchesSTART_INSTANCEinto GS user word 2 (s10) and the vertex program reads it as a value (the record'sfirstInstanceis a small object index).With
APS5_DRAW_CACHE_PUSH_WORDset, the record's word is written into the stage's forwarded push data instead: the slotshaderDataUserWordsnames, and theShaderDatabinding's descriptor word when that data lives in a buffer. That is the same patchapplyShaderDataPatchesalready makes for a relocated entry, so the write moved intopatchForwardedWordand both use it. The compiled stage is kept. A word is forwarded only when all three hold:SrtUserDataUse(asBase | asValue); for a cached stage it is the newDispatchVariant::userWordsWalked, whichtagVariantRunsrecords at insert andrelocateDrawEntrycopies. A word at or beyondTrackedWordsis never forwarded.Otherwise the record materializes the stage as before. Tessellation control and evaluation stages keep their per-record materialization too (they are patched with the record's parameters, not a word).
Why this is sound: a stage's materialization depends on its user words through four paths. The SRT walker records every word it reads in
SrtUserDataUse;ShaderDataDwordsForcopies theGetUserDatareads that survive into the push data or the ShaderData buffer;DecodeVertexStageInforeads the fetch-shader and vertex-table pointers; and the mesh front stage has hidden words. The walk is a deterministic function of the words it reads, so a word it never read cannot change the regions it captured, the descriptors it resolved, the flat SRT or the specialization. The second path is reproduced by writing the slot, the third is checked, the fourth is excluded. On a hit the cached walk is the walk for this packet, because the register key matched the user words and the validation matched the memory.relocatableWordsusesasBase & ~asValue; this check uses the union, which is stricter.APS5_VERIFY_DRAW_PUSH_WORDbuilds every forwarded stage in full as well and throws when its variant, push constants, bindings or forwarded words differ from the patched result, or when the full build's walk read the word. The[draw-cache] indirect records (10 s)profile line counts words forwarded, stages re-materialized, words refused and words verified. Both switches have rows indocs/dev/ENVIRONMENT.md.Related, not overlapping: #5609 (@TheAggie, open) and #9177 (@milescoviello, open) change
Draw.cppand the draw-cache headers near these lines (relocation by moved base addresses, and fewer per-draw allocations on the materialize path). Neither forwards a record's word, so whichever lands second needs a textual rebase only.Depends on #9243: the hit path this PR uses for cached stages (
matched[programIndex]->userWordsWalked) only sees CPU-path indirect draws when they are admitted. The switch also works on misses without it.Tested
agc_driver_draw_cache_reuse_push_wordandagc_driver_draw_cache_reuse_push_word_verify(theagc_driver_draw_cache_reuse_testsbinary withAPS5_DRAW_CACHE_INDIRECT=1 APS5_DRAW_CACHE_PUSH_WORD=1, the second also withAPS5_VERIFY_DRAW_PUSH_WORD=1): a vertex program reads the user word aDRAW_INDIRECTpacket's base vertex is patched into as a value, and four records set it to move a triangle off screen three times and on screen once. The record's value has to reach the shader on the miss and on the two hits (nothing drawn with 4, the triangle drawn with 0), 4 words forwarded, 0 stages re-materialized, 0 refused, and 4 verified in the verify variant. WithoutAPS5_DRAW_CACHE_PUSH_WORDthe same draws forward 0 words and re-materialize 4 stages, which the_indirectvariant from #9243 checks. All sevenagc_driver_draw_cache_reuse*variants pass on lavapipe (Mesa llvmpipe) and on the RTX 5070 Ti (ANYPS5_REQUIRE_VULKAN=1), on this branch at maincecb540a6, Linux, Release, GCC 16.2. Onmainthe last drawable record of a CPU-path indirect draw is queued on the draw pipeline (#6931), so the record counters are added before that enqueue as well as after the loop; the first port of this change missed that and the new tests caught it (0 of 4 words counted).RecordsKeepThePacketsFragmentWordcommit; that test runs in all seven variants here too, with the push word forwarding the vertex word per record while the fragment word stays the packet's. All seven pass on lavapipe and on the RTX 5070 Ti after the rebase.ctest -R agcon lavapipe,ANYPS5_NO_SHADER_CACHE=1: 859 of 860 pass. The one failure isagc_driver_multisample_retirement, the same one as on #9243's branch; it passes on the RTX 5070 Ti and does not reach the draw cache. The 26 draw-cache, draw-data-hit, key-miss, register-key and indirect-draw tests pass on the RTX 5070 Ti. The Khronos validation layer is not installed here, so the validation-layer comparison againstmainis not done.python3 tools/check_conventions.pyagainst #9243's branch passes.libSceAgcDriver.prx. Medians over the level windows (10 s each, 65 to 68 per run), from the[packets],[draw]and[draw-cache]profile lines.main. The base was main676269473plus #6280, #6926, #7844, #8047, #7984, #7981, #7930 and #9243 withAPS5_DRAW_CACHE_INDIRECT=1, a local stencil-export workaround this GPU needs to reach the level (never upstream), and two local diagnostics that were off. The change arm was the same set with this PR's driver andAPS5_DRAW_CACHE_INDIRECT=1 APS5_DRAW_CACHE_PUSH_WORD=1.[draw]86.0 vs 102.7 us per packet (-16.2%). Capture plus recompile 10.3 vs 24.0 us per packet. GPU busy flat (159.6 to 160.1 vs 158.4 to 160.0 ms per present). Screenshots clean.[draw-cache] indirect records: 60k to 66k patched words forwarded per 10 s, 0 stages re-materialized, 0 refused, on every change run. So on this title the patched word never feeds the SRT walker.DRAW_INDEX_INDIRECTpacket (last window of one pair): captures 0.17 vs 1.03, capture 4.5 vs 13.8 us, recompile 8.1 vs 9.1, validate 13.7 vs 13.1, total 50 to 51 vs 70 to 74 us. Validation is now the largest cost left on a hit.perflabel asks for are not attached yet. A capture pair (APS5_TRACE_SECONDS, same scene, base and change) follows as a comment.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); this change adds no stubs.mdfiles or images added to the repository (attach them to this PR)AI-assisted: yes
Performance summary lines
mainand this change, same machine and scene. The full captures, traces and digests are in https://github.com/boykopovar/AnyPS5/pull/9248#issuecomment-6326685429.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-21T16:33:07Z.
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 fisherjoey on 2026-10-21T17:44:50Z.
I checked the PRs the overlap bot named by trial-merging each head onto this branch (
git merge-tree, no build). None of them does what this PR does. TheDrawCapture.cppoverlap comes from #9243, already listed under Depends on.stageDataparameter plus thedrawCacheIndirect()condition, and put both new tests in itselsebranch. Forwarding happens per record, aftercacheDrawStages, so its entry verify never compares a forwarded word.DrawCache.cpp.userWordsWalkedis a plain field, sosizeof(DispatchVariant)invariantBytescovers it.DispatchVariantfield afterrelocatableWords, and one copy line inrelocateDrawEntry; keep both. This PR already copiesuserWordsWalkedto the relocated variant. The recipe path is gated on!drawParameters.indirect, so its donor lookup never meets a forwarded stage.memory. A forwarded stage skips re-capture only for words the SRT walker never read, so its captured regions are the packet's, and the check still covers them. On a retry it callsdraw()again, which rebuilds all of this PR's per-packet state.Apart from #9243 going first, order doesn't matter; whichever lands second takes the resolutions above.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-21T21:13:30Z.
Captures for the
perflabel, onmain. Demon's Souls PPSA01341 v1.07, in the first level, 1920x1080, Linux (Nobara, kernel 7.2), RTX 5070 Ti, driver 595.91.07. Four 900 s runs from boot into the level, one session, in this order: main, #9243, this PR, main again. The window isAPS5_TRACE_SECONDS=400:60(60 s from t+400 s, level loaded). The base for this PR is #9243 with its switch on, as in the description, so the pair here is run330 against run331; the two main captures are shown for scale and for the A/A spread. The same four captures are on #9243 with the view from that side.Setup
cecb540a6plus the local stencil-export workaround (this GPU needs it to reach the level, never upstream) plus the #6280 mip clamp, which keeps main off the Xid 31 I described on #9243. The trace's commit field, 3e72d893778d, is that local build. dd46b3e09585 is the same tree plus #9243's head b2b765ad4; ab0687554423 adds this PR's head b0442fbd8 on top. Each set differs from main only inlibSceAgcDriver.prx(checked with an aps5-libs diff: exactly one file).APS5_TRACE_*orAPS5_PROFILE_*switch.APS5_VERIFY_DRAW_PUSH_WORDwas off. The otherAPS5_*values in the env field (memory caps,APS5_IGNORE_STENCIL_EXPORT,APS5_GPU_MEASURING, the lock name) are the same on all four arms.trace_dropped: truein all four: the event buffer ofqueue worker 0x0filled after 10.2 to 12.6 s (1.0 to 1.3 M events lost,totals_full: false). The per-slice tables for that worker in the digests cover the first 10 to 13 s of the window; the totals in the summary line (frame_ms,top_phases_ms_per_frame,worker_busy_pct) are not marked full, and the phase comparison below uses those.Summary lines (
aps5-summary.jsonl, verbatim: base run330, this PR run331, then the two mains run329 and run332)Traces (a release on my fork; the assets keep their
aps5-trace-<commit>-<pid>.json.gznames and open in ui.perfetto.dev)APS5_DRAW_CACHE_INDIRECT=1: https://github.com/fisherjoey/AnyPS5/releases/download/perf-captures-20261021/aps5-trace-dd46b3e09585-3182475.json.gzAPS5_DRAW_CACHE_PUSH_WORD=1: https://github.com/fisherjoey/AnyPS5/releases/download/perf-captures-20261021/aps5-trace-ab0687554423-3238611.json.gzFrame times (ms, the 60 s window)
APS5_DRAW_CACHE_INDIRECT=1(base)APS5_DRAW_CACHE_PUSH_WORD=1A/A spread of the two main captures: p50 43 ms (3.6%), p90 131 ms, presents 50 against 48.
Reading:
mainthe pair is a small gain that one capture each cannot separate from noise. The draw path itself is a clearer signal, see below. #9243 alone is +5.6% p50 against the main mean, which is why the gap to it is larger than the gap to main.Where the frame goes, from
top_phases_ms_per_frame(ms per frame, summed over threads) and the digest's thread tableWhat the digests show:
Driver.Draw.draw_enqueuedis 386 against 500 (-23%) and against 426 and 424 on main (-9%).Shader.DrawStage, the stage materialization, falls off the top 8 (under 320 ms per frame, against 321 on the base and 380 and 405 on main). GPU busy is flat at 154 to 157 ms per present, 12 to 13% of the frame, in all four.Driver.Packet.dispatchstays at 414 (419 on the base, 374 and 409 on main), and the draw committer'sGraphics.Drawon its own thread is 169 ms per frame, down from 181 on the base but above main's 136 and 155.main: the frame here is queue 0.queue worker 0x0is 99.5% busy in all four captures, and draw plus dispatch summed over the workers is 1250 to 1350 ms per frame against a p50 of 1170 to 1270. The seven other queue workers spend 95 to 100% of the frame inDriver.Packet.memory_wait(about 0.88 of the frame each), the largest slice in each of the five slowest frames of every capture is a memory_wait onqueue worker 0x30of 1.36 to 2.02 s, and memory_wait is 6.0 to 6.2 times p50 on all four arms: it is the other queues waiting for the frame, not a stall of its own. Against the base the frame moves by about the Driver.Draw delta (-96 ms Driver.Draw, -100 ms p50). Against main the Driver.Draw delta is -75 ms but the frame moves -32 ms, with dispatch +23 ms of the difference and the rest within the A/A spread.mainthe frame is about 1200 ms and queue 0 is busy for all of it. The digests say where main's frame goes, not which of the stack's PRs account for the shorter queue 0 there, so I am not claiming that here.Digests, made with
tools/trace_digest.pyon each trace. The JSON block is the summary line above; the title-thread rows and the GPU batch list are left out for length.trace_digest.py, run330, PR 9243 dd46b3e09585 (APS5_DRAW_CACHE_INDIRECT=1)
seconds 400:60 from present 2403: 47 presents, frame p50 1271.8 / p90 1519.6 / p99 2048.9 / max 2048.9 ms, GPU busy 156.5 ms per present
NVIDIA GeForce RTX 5070 Ti, NVIDIA 595.91.07, Linux 7.2.0-202.nobara.fc44.x86_64, 1920x1080, commit dd46b3e09585, dropped: yes
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 8144.9, Driver.Packet.memory_wait 7788.9, Driver.WorkerWait 1052.0, Driver.WorkerWait.idle_and_completion_poll 1051.9, Graphics.Draw 194.1, Driver.Packet.dispatch 192.2, Graphics.Draw.draw_and_resource_release 162.4, batch 156.5, Driver.Packet.draw 151.9, Driver.Draw 151.4
Longest waits
trace_digest.py, run331, PR 9248 ab0687554423 (APS5_DRAW_CACHE_INDIRECT=1 APS5_DRAW_CACHE_PUSH_WORD=1)
seconds 400:60 from present 2312: 52 presents, frame p50 1172.3 / p90 1415.7 / p99 1536.4 / max 1536.4 ms, GPU busy 156.7 ms per present
NVIDIA GeForce RTX 5070 Ti, NVIDIA 595.91.07, Linux 7.2.0-202.nobara.fc44.x86_64, 1920x1080, commit ab0687554423, dropped: yes
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 7399.7, Driver.Packet.memory_wait 7023.3, Driver.WorkerWait 1027.7, Driver.WorkerWait.idle_and_completion_poll 1027.6, Driver.Packet.dispatch 197.2, Graphics.Draw 186.3, Driver.Packet.draw 165.9, Driver.Draw 165.6, batch 156.7, Graphics.Draw.draw_and_resource_release 153.7
Longest waits
trace_digest.py, run329, main 3e72d893778d
seconds 400:60 from present 2883: 50 presents, frame p50 1183.1 / p90 1376.2 / p99 1632.9 / max 1632.9 ms, GPU busy 155.4 ms per present
NVIDIA GeForce RTX 5070 Ti, NVIDIA 595.91.07, Linux 7.2.0-202.nobara.fc44.x86_64, 1920x1080, commit 3e72d893778d, dropped: yes
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 7662.2, Driver.Packet.memory_wait 7340.8, Driver.WorkerWait 963.2, Driver.WorkerWait.idle_and_completion_poll 963.1, Driver.Packet.dispatch 165.7, batch 155.4, Graphics.Draw 152.2, Driver.Packet.draw 146.0, Driver.Draw 145.6, Graphics.Draw.draw_and_resource_release 119.3
Longest waits
trace_digest.py, run332, main again (A/A) 3e72d893778d
seconds 400:60 from present 2351: 48 presents, frame p50 1226.0 / p90 1507.5 / p99 1802.5 / max 1802.5 ms, GPU busy 153.8 ms per present
NVIDIA GeForce RTX 5070 Ti, NVIDIA 595.91.07, Linux 7.2.0-202.nobara.fc44.x86_64, 1920x1080, commit 3e72d893778d, dropped: yes
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 7995.5, Driver.Packet.memory_wait 7649.4, Driver.WorkerWait 1053.6, Driver.WorkerWait.idle_and_completion_poll 1053.5, Driver.Packet.dispatch 183.5, Graphics.Draw 169.7, batch 157.1, Driver.Packet.draw 150.1, Driver.Draw 149.5, Graphics.Draw.draw_and_resource_release 136.8
Longest waits
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T11:52:26Z.
@fisherjoey a merge-order note for this PR and #9243 against two of ours. Ours go after yours, and rebase over them:
DrawCache.cpp): it addsDriver::DrawCacheBytes()(APS5_DRAW_CACHE_BYTES) where #9243 addsdrawCacheIndirect()and this PR addsdrawCachePushWord(). They're adjacent getters, and all of them stay.Draw.cpp): it adds arecordedOneflag before the indirect record loop, where this PR adds its forwarded-word state and lambdas. #7446 uses the flag to check the captured memory once, before the first record is drawn, and to prepare the whole draw again if that memory changed. Both stay. A redone draw forwards its record words as it would the first time.Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-22T12:41:27Z.
@EagleBearerYS thanks, that order works for us, and both resolutions match what we listed in our overlap note: the adjacent getters all stay, and the forwarded-word state sits next to
recordedOne. If either of yours lands first instead, I'll rebase these two over it the same way.AI-assisted: yes