perf(agc): suppress redundant graphics pipeline and dynamic state commands #16752
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16752"
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 chitters2004 on 2026-11-07T15:13:49Z.
What
Every recorded graphics draw binds its pipeline and records every dynamic state command it uses, even when its command buffer already holds that pipeline and those values. In the GTA V (PPSA04263) standing scene measured below,
mainissues 18.6 dynamic state commands and 0.98 graphics pipeline binds per draw.Each recorder batch now owns a
CommandState(Recorder::GraphicsState()) that remembers the graphics pipeline bound in its command buffer and the value of each of the 19 dynamic state slots the draw path sets (viewport, scissor, depth bias, depth bounds, cull mode, front face, depth test, write and compare, depth bounds test, stencil test, and per face the stencil op, compare mask, write mask and reference). A bind or setter whose value the command buffer already holds is skipped.Pipeline::Begin,Continue, the back-face and second-channel binds, the stencil replay reference, the sample order depth reorder and the first-sample copy inRecorder::RecordUncountedrecord through the batch'sCommandState, so the draw after one of those passes records only what differs.vkBeginCommandBuffer, and a pipeline bind forgets every slot the new pipeline fixes statically. The state belongs to the batch, so a recycled command buffer handle and a batch reopened after a failed submit start with nothing known.Context::Function,Context::Resolvedand the device setup lookup take a constevalFunctionNamethat rejects a graphics pipeline bind, a tracked dynamic state command,vkCmdBindShadersEXTorvkCmdExecuteCommandsat compile time. The device table holds those entry points as write-onlyTrackedFunctionmembers that onlyCommandStateand the newContext::BindComputePipelineread. Presentation, the performance overlay and tests, which record their own command buffers, use a local untrackedCommandState.CommandStatecannot be copied or moved.docs/dev/debt/libSceAgcDriver.md: a rawdeviceProclookup cast to the function type, and a secondCommandStatebegun on a recorder batch's command buffer.APS5_NO_STATE_SUPPRESSIONrecords every bind and setter, as before (docs/dev/ENVIRONMENT.md).Overlap, checked on 2026-11-07 against
main46552cb461withtools/pr_overlap.pyand by hand: the tool reports 13 open pull requests that change lines this branch changes and 98 more that conflict withmainand share files with it (78 of the 111 have a commit since 2026-10-24; I read the hunks of 29 of them). None implements the same thing. #9503, #10351, #11551, #12840 and #12944 change the same functions (SampleOrders::Depth,recordPreparedDraw,recordStencilReplay,pushDrawConstants,VulkanDevice::recordDispatch), so whichever lands second needs a manual rebase; the others share only test, debt orENVIRONMENT.mdlines in other hunks. #10049, #12863, #15670, #15845 and #16286 are disjoint. Rechecked against4977f64052: the tool now confirms #9503, #9712, #9897, #10141, #10351, #12944, #14032, #15570, #16373 and #16572. #16572 (set color write masks per draw through dynamic state) adds a dynamic state that this change's tracker must own: with this change, recordingvkCmdSetColorWriteMaskEXToutside the tracker does not compile. If #16572 lands first, a rebase of this branch onto it that tracks the masks like the other dynamic states is ready (fork branchdraft/src/refresh/r1-on-16572); it suppresses an identical mask set and records a changed or wider one, with a unit test that a mutant ignoring mask values turns red.Open pull requests that record a graphics pipeline bind or a tracked dynamic state command must go through
CommandState(orContext::BindComputePipelinefor a compute bind) once both are inmain, because a direct call throughContext::FunctionorContext::Resolvedno longer compiles; none is a prerequisite, soDepends onis none:SampleOrders::Depth): its pipeline bind goes throughCommandState::BindPipelineonRecorder::GraphicsState().recordPreparedDraw): stencil state goes throughCommandState::SetStencilOp,SetStencilCompareMask,SetStencilWriteMaskandSetStencilReference.depthBounds(recordStencilReplay,pushDrawConstants): its fallback setsSetDepthBoundsTestEnableandSetDepthBoundsthroughCommandState.PresentationPass::draw,FirstSampleCopy::Record,GpuColorTransfer::dispatchand theTextureDetilerdispatches), #10043 (pipeline bind, viewport and scissor in the newPresentationUpscaler.cpp), #12840 (VulkanDevice::recordDispatchand a store-page gather inRecorder.cpp) and #15792 (Recorder::RecordChangedCopy) add such calls; their text merges, but each call has to move toCommandStateorContext::BindComputePipeline.Tested
Head
1781488bcbonmain4977f64052.Refreshed onto
main4977f64052. Head1781488bcb;git range-diffagainstb0fe73c6ddshows the same code patches in all 8 commits; only the debt entry of commits 3 to 6 and 8 changed context, becausemainadded two entries above it. Local RTX 3080 gate on both, same environment as below (ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation), built one after the other in one build tree, with the focused AGC set of all five pull requests of this series (242 tests on this head, 232 onmain): both 100 % passed, noVUID-line in either log or in replay subprocess logs, and neither log containswas not found but was requested by env var VK_INSTANCE_LAYERS. Fork CI on the rebased head against4977f64052: run 42375092958: Linux 46 of 53 passed, 7 skipped; Windows 45 of 55 passed, 9 skipped, andagc_driver_recorderfailed, as it does onmain. The skips include the newagc_driver_pipeline_optimized_readback_color32_unsuppressedand_color64_unsuppressed, skipped on lavapipe like their existing forms; the RTX 3080 gate runs and passes them. NoVUID-line on either side, conventions check passed. The measurements and full runs below were on the previous base46552cb461; the code they measured is unchanged.Vulkan validation. RTX 3080,
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ctest --test-dir build -R 'agc|write_tracking|guest|indirect_draw|draw_finisher|depth_bias|draw_pipeline' -Vonb0fe73c6dd(3209 tests) and onmain46552cb461(3199 tests). Every test failing in either full run was then rerun alone on both builds, one build at a time with nothing else on the GPU, usingctest -Rwith the union of the 16 names and the same environment: on both, 15 of 16 passed and onlyagc_driver_direct_write_completionfailed, which fails onmaintoo (#15547); the other failures of the full runs were timeouts and0xc0000409exits under load from concurrent gates and did not reproduce alone. Both full logs hold the same 18VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605lines and no otherVUID-; neither containswas not found but was requested by env var VK_INSTANCE_LAYERS. The change adds no validation error.Fork CI (GitHub runners, Mesa lavapipe with the Khronos validation layer, head against
main): run 42329061255, rebased there onto46552cb461. Linux and Windows each ran 39 tests: 31 passed, 0 failed, 8 skipped, noVUID-line on either side, conventions check passed. The eight skips are the same onmainexcept for the two new_unsuppressedcolor readback variants, and all have the same lavapipe reasons: the masked pixel shader needs at least 32 subgroup lanes (the fourcolor32/color64readback tests and their two new variants), and the device has no 2x color and depth targets (two sample order tests); the RTX 3080 gate above runs them.Tests.
agc_command_statedrives the tracker through real objects: a pipeline bind, every tracked dynamic state, a D16 stencil target stored as D24 that caches and emits the converted depth bias for both faces with and without graphics pipeline libraries,Pipeline::Continueswapping from the fast-linked to the optimized handle within one tracker and suppressing afterwards,DynamicSlotsrejecting a dynamic state the tracker does not follow, and a second render pass of a batch that records nothing for an identical draw.agc_graphics_state_recorder(and_unsuppressed): a realRecorderreopened over a reused command buffer handle, and a batch reopened on a recycled handle after a failed submit, record the pipeline bind and every setter again; two interleaved recorders never suppress each other; a draw afterRecordUncounted, also when it throws, or afterRecordSampleOrderrecords only what differs.agc_driver_depth_bias_per_facechecks the recorded per-face pixels; recorded per-face draws set the viewport and scissor once and the synchronous path twice. The optimized readback, stencil replay and per-face depth bias tests also run withAPS5_NO_STATE_SUPPRESSION(_unsuppressed) and the per-face test withAPS5_DRAW_TRANSITIONS(_transitions).bf99218c1b, same tests): a diagnostic build that carries the previous batch's tracker state into the batch opened after an injected failed submit turnsagc_graphics_state_recorderred at "the first draw after a failed submit on a recycled command buffer handle" (the draw is suppressed instead of recording all 16 commands); without the mutant it passes. The compile-time check is its own negative control: recordingvkCmdSetViewportthroughContext::Functiondoes not compile.agc_command_statefirst failed 8 of 40 runs because the sample-location mock ignoredblockOptimizedLink; after that test fix it passed 150 of 150 runs and 100 of 100 on the then frozen commit.Per-draw cost, replay. Identical frames, so the change is the only difference. A 3-frame capture of the scene below (
APS5_FRAME_CAPTURE, 9,364 draws) is replayed withagc_frame_replay --repeat 5through the real submit path, with the same pipelined-draw settings as the boots; passes 2 to 5 count. Base46552cb461and each build alternate run by run. Every pass of every run drew exactly 9,364 draws and every run matched the base pixels exactly (pixels_identical1.0). The metric is committer wall time per commit, per draw; base is about 53.2 µs per draw. The replay tool ismain'stools/FrameReplay.cppwith #12565, #12567, #12568, #11435 and #13984 merged and a local retire of resident images before each memory restore so--repeatworks; it is local and not part of this pull request (per-build summaries and binary provenance attached).b0fe73c6dd46552cb461f62ea7bd080e323b1bdcThe calibration build reads its injected 1.0 µs, so the replay resolves per-draw costs below 1 µs; base against base reads +0.10 ± 0.37. The two null controls stay within noise. The three changes add up: -1.35, -0.81 and -0.44 sum to -2.60, inside the merged build's -2.99 ± 0.44. Thread CPU columns are attached but not used: Windows thread times tick in 15.6 ms steps, too coarse for 1 µs per draw. The replay cannot show contention with the title's own threads, frame pacing, or GPU-bound time.
Frame time, stacked boots.
main46552cb461against the three changes merged on it, one warm-up boot each, then two ABBA pairs in the scene below on the current host: mean ms/frame 147.0 to 136.0 (-7.5 %) and 136.7 to 131.3 (-4.0 %); p99 168.8 to 181.9 and 192.6 to 188.7. These are not attributed to the change: the merged build drew 13 % fewer draws per present in those boots (1,497 and 1,611 against 1,783 and 1,801), and draw count varies from boot to boot with the scene, which is why the per-draw figure above comes from the replay. The older per-pull-request pairs below were taken on this branch before its rebase and on the host state before the changes listed under Host; the counts are per pull request and stay.Performance, older per-PR pairs. GTA V (PPSA04263, content 01.011.001), standing scene, 3840x2160, default in-game settings. Two interleaved pairs,
mainthen this change, twice, after one warm-up boot of each build that is not a sample. Each capture is the 30 s trace window from 235 s after launch, about 30 s after the player stands still (202 to 206 s in every capture), with pipelined draws (APS5_DRAW_PIPELINE=1,APS5_PIPELINED_DRAWS=32,APS5_PIPELINE_DISPATCH=1).mainwas7383be1a19; the change was86104970d7, this branch before its rebase onto46552cb461(git range-diffshows the same patches; only the context lines of the debt entry differ).7383be1a19c786104970d79d7383be1a19c786104970d79dChange minus main, mean ms/frame: pair 1 +2.50, pair 2 -6.31, mean -1.90 (-1.4 %); p50 +0.6 and -7.5; p99 +42.9 and +6.7.
Frame time is within noise. Twelve
maincaptures of this scene across six blocks give a mean of 137.6 ms per frame with a standard deviation of 1.85 ms (1.35 %), and the two pair deltas have opposite signs; two pairs resolve about 3 % at best. The p99 rose in both pairs (+42.9 and +6.7 ms); in a 30 s capture of about 220 frames the p99 is set by the two or three longest frames, so a single pair's p99 moves with single long frames; in the stacked boots below it moved both ways (+13.1 and -3.9 ms).Why frame time barely moves: the scene is CPU-bound and the draw committer is its critical path. In a 60 s stack sample of
mainin this scene (1 ms sampling, 3,843 samples per thread, digests attached),finishDraw/commitDrawis 79.8 % of the committer's samples inclusive,Graphics::Draw76.1 %,prepareDraw53.4 %,recordPreparedDraw21.8 % andcollectWrites12.2 %; about half the sampled stacks are cut at 64 frames, so inclusive shares are lower bounds. The queue worker shows 95 % busy in every capture, but 53 % of its samples areDrawFinisher::Drainreadingsteady_clock::nowwhile it waits, so that figure overstates its load. GPU busy is 33.8 ms of a 137 ms frame.The work this change removes, counted in separate boots of the same scene (75 s after the player stands still,
APS5_PROFILE_DRAW=1, so counts only, never timing) with a local counter patch that counts thevkCmdBindPipeline(graphics) andvkCmd Set*calls the draw code requests and those actually recorded; the patch is applied identically to both sides and is not part of this PR (diag-base.patch, diag-r1.patch):mainboot 1mainboot 2Per frame that is about 51,000 fewer dynamic state commands (52,794 to 1,785) and 1,600 fewer pipeline binds (2,783 to 1,175). The boots drew 2,827 to 2,853 draws per present; the per-draw rows carry the comparison. Requests fall too because the draw path no longer asks for state a recorded pass already set.
The same boots print the draw committer's cost per commit (
[draw] pipelined (10 s, depth 32): N commits at X us each, one line per 10 s; a commit is one draw or other operation the committer runs from its queue). Over the seven 10 s blocks of the window,mainaveraged 35.4 and 34.4 us per commit (blocks 34.2 to 36.6 us) and this change 33.6 us (blocks 33.2 to 34.2 us), about 1.3 us (3.8 %) less. At about 3,660 commits per present in these boots that is about 4.9 ms per present on the committer, the thread on the critical path (see above).APS5_PROFILE_DRAWadds its own clock reads and turns off the draw finisher handoff, so these are a comparison between builds, not a timing.Summary lines (
tools/trace_digest.pyJSON summary, one per capture, in the order of the table):Traces: main, pair 1 aps5-trace-7383be1a19c7-24960.json.gz, change, pair 1 aps5-trace-86104970d79d-10600.json.gz, main, pair 2 aps5-trace-7383be1a19c7-13360.json.gz, change, pair 2 aps5-trace-86104970d79d-19600.json.gz. Counter lines: counters-base.txt, counters-base2.txt, counters-cand.txt.
tools/performance_compare.pyoutput per pair: pair 1, pair 2.tools/trace_digest.pyoutput per capture: main, pair 1, change, pair 1, main, pair 2, change, pair 2.Deviations from the Performance report form, the same in every capture (deviations.txt, environment per capture in the
*-env.txtfiles):APS5_ADDRESS_COPY_MAX_MIB=512,APS5_NO_PAD_INPUT=1,APS5_LOG_TIMESTAMPS=1andAPS5_TRACE_SUMMARY_EVERY=10.APS5_DUMP_FRAMES=80,APS5_DUMP_FRAMES_EVERY=300) let the script see the menus and check the view; the log is redirected to a file the script reads.ANYPS5_SHADER_CACHE_DIR,__GL_SHADER_DISK_CACHE_PATH).trace_dropped), so slice counts are partial; the summary totals are complete.maincapture ran at 21:09 and the other three at 21:16 to 21:31, with one killed boot between them that was not kept.Host: AMD Ryzen 7 5800X (16 threads), 32 GB, NVIDIA GeForce RTX 3080 (driver 621.14, Vulkan 1.4.357), Windows 10.0.26220, desktop on AC power with the High performance scheme, display 2560x1440 at 179 Hz; the title renders 3840x2160 (host-report.txt). That is the host of the older pairs and the counter boots above. Since then Resizable BAR is on (BAR1 16384 MiB, VBIOS 94.02.27.00.09), Windows is build 26320 and Defender excludes the build, tool and capture folders; the driver is still 621.14. The replay measurement and the stacked boots ran on that host.
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)🤖 Generated with Claude Code