perf(shader): emit both registration layouts of a pixel shader from one program #4884
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4884"
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 nikowolf123 on 2026-10-13T01:48:09Z.
What
On a cold cache, registering a pixel shader builds its front end twice.
PrepareRegisteredprepares every pixel shader through oneShaderPreparationContext, first with the plan's layout and then again withpushConstantSizeBytes = 0(ShaderRegistry.cpp:754). The layout is not part of the source key, so both requests share oneSourceEntry. But the first variant consumes the entry's front-end program (findOrCompileVariant, Recompiler.cpp:952-953). The second layout therefore runsPrepareResourceProgramagain (translation, SSA, folding and the eliminator passes,SrtWalker,ResourceTracker) and then the interface passes, although none of them reads the layout. #2526 shares only decode, CFG and structurization between the two calls.With this change, the context keeps the program it last emitted from, after the interface passes, and emits the second layout from it:
compileVariantis split intoprepareInterface(ApplyStaticInterface, identity and dead-code removal,ShaderInfoCollector) andemitVariant(binding allocation, SPIR-V emission, the registration word-limit fallback, validation).compileVariantis now the composition of the two and behaves as before. This is the first commit.bindingLayoutCompleteand emits from that program, which the word-limit fallback already does within one variant (Recompiler.cpp:488-494). The compiled information is copied with a newIrProgram::CompiledInfo() const&instead of being moved out.shaderHash, which loop-guard fault records carry.PrepareRegisteredcall, so no program outlives its registration (the rule of #471). A compute pair (full, then partial threads, two sources) never holds two programs at once.entries.front(). Calls without a context (Recompile,CaptureResources,PrepareShaderwithout one) take the old path.APS5_NO_PERF_LAYOUT_PAIR=1restores the old path. It is listed inNeutralSwitches, so it does not change disk-cache keys.ResourcePrograms()counts the programs built through a context, for the tests.This does not fix #4399. There, simonbbby's RTX 3090 profile logs both registration variants of one ps5-opengl pixel shader (5,413 code dwords) at 1,273.6 ms and 1,207.2 ms. That shows the duplicate exists on a real shader and costs about as much as the first variant. But the title's ~2 s wait is lost to pipeline creation (~2.2 s), as MarcZX100 concluded in that issue. By default
RegisterShaderhands the preparation to the backgroundRegistrationPreparePool. The saving reaches frame time only where something waits for it: a draw or dispatch that needs a shader whose preparation is still pending (AwaitRegisteredPreparation),Driver::ResolveShaderAbiwhen it prepares an ABI that was not registered (inline, on the calling thread), andAPS5_SYNC_SHADER_PREPARE=1.Open PRs #4339 (wave64 layout choice inside
compileVariant), #4048 and #4475 (lines of the emission step) edit nearby lines. The conflicts are textual; whichever merges second needs a rebase. #4271 (IR reclamation aroundResourceProgram) and my #4883 (front-end resource plan in the disk cache) are merged, and this branch is rebased onto both. After a plan hit the entry has no program, so the first layout builds it through the context and this change lets the second layout reuse it.Tested
WSL2 Ubuntu (kernel 6.18), GCC, i9-14900KF, lavapipe, configured like CI (Release, SPIRV-Tools ON):
APS5_NO_PERF_LAYOUT_PAIR=1set for the whole run, made before the test-only follow-up commits, also had 0 failures and the same skips. The last commit only adds a check toagc_shader_memory_tests; its seven ctests pass on the head.tools/check_test_registration.pypasses.tools/check_skips.pyflagsguest_video_out_high_frame_rate("current display does not provide 120 Hz"); unmodifiedmain43e3a70f8skips it the same way in the same tree.agc_shader_layout_pairandagc_shader_layout_pair_off(0.3-1.3 s each under load; also run insideagc_shader_memory). A pixel shader with a buffer load is prepared through one context with a 128-byte push range and with none, in both orders and in five configurations: plain modules, registration modules at push offset 0, registration modules at offset 128 (the fragment slot on devices with graphics pipeline libraries), a word limit that forces the fallback to plain modules, and registration modules at offset 128 with two interpolated inputs. Each pair must build one program with reuse and two without.UsesPushData, every artifact field except the variant id (fragment parameters element by element), and the compiled shader information. The materialized result must also match an uncachedRecompile. The test also checks that the context does not reuse a program across code addresses or across sources at one address, that it releases the kept program when it builds another one, and thatCompiledInfo()andTakeCompiledInfo()agree on a compute program with a nonzero user-data base and scratch size.agc_shader_layout_pairfails and_offpasses. Never reusing the program fails with "the second layout ignored the reuse switch", because it builds 2 programs asmaindoes. The other controls remove the source check (the SPIR-V differs from the isolated compile), the code-address check, the release before another build, or thebindingLayoutCompletereset ("binding layout already allocated"), empty the bindings of the copied compiled information, or change the barycentric flag, a fragment parameter or the runtime ABI version of the reused artifact. Zeroing the user-data base, scratch size and export mask of the copy fails both tests, since that check does not depend on the switch.agc_shader_memory_testsbuilds and passes on the refactor commit and on the perf commit alone.Performance: I have no PS5 and no game files, so there is no trace from the Performance report form. The closest equivalent without a title is a synthetic measurement:
PrepareRegistereddoes: one context,PrepareShaderwith a 128-byte push range (A), then with none (B). The program is a chain of buffer loads, f32 ALU and exec-masked branches.ANYPS5_NO_SHADER_CACHE=1, so the in-memory and disk caches are cold. The build is Release with SPIRV-Tools OFF, asrelease.ymlbuilds.maineachPrepareShaderfrees its own program inside A and B. On the branch the one program is freed with the context, so pair = A + B + teardown is the comparable total.main43e3a70f8and this branch were built in the same tree and run alternately, 10 rounds (5 in each order), on the same machine in one session. Other builds moved the load average between 2 and 23 (median 6), so absolute times are noisy.Medians of each column, ms:
A is a few percent lower on the branch only because
mainfrees the first program inside A, while the branch frees it in the teardown column. B onmainalso includes freeing its program and B on the branch does not, so the pair column is the fair comparison.In every run, the SPIR-V and binding layout of both layouts hash the same on
mainand on the branch. With registration modules these synthetic modules exceed the 200,000-word registration limit, so each variant is emitted twice (module, then fallback), and that emission is most of what remains of B. How much a title gains depends on how much of its cold pixel-shader registration is front end; I have not measured a title. I can attach the harness source, the scripts that build and run it, and the raw JSONL (one line per process, with timings and output hashes). An earlier batch stopped the timer before the context was destroyed and reported 2-3 points more pair saving; this batch supersedes it.Native Windows 11 (WinLibs GCC 15.2.0, Release with tests) on an RTX 5080 (NVIDIA 616.92), same head: full
ctestwithANYPS5_REQUIRE_VULKAN=1, 1021 tests, 0 failed, 7 skipped astools/check_skips.py --job windowsexpects;agc_shader_layout_pairandagc_shader_layout_pair_offpass. Not tested with a title.main0c3dc9ca2for this pull request: it builds in the CI configuration and the 34 shader, pipeline-library, structurizer and control-flow tests (ctest -R 'agc_shader|agc_driver_prepared|agc_pipeline_library|agc_structurizer|control_flow') pass on lavapipe.main0b6d033b8(head 7028edaa2 is now49de39478). One conflict, inRecompiler.cpp: #4883 addedFrontEndBuilds()where this change addsShaderPreparationContext::ResourcePrograms(), and both are kept. The rest applied cleanly, and the diff againstmainhas the same added and removed lines as before (7 files, +239/-18).Since
0c3dc9ca2, #4271 makesResourceProgramclear block register state and reclaim unused values (PrepareShaderpassesreclaim = false), and #4883 loads a stored resource plan ingetSourcebefore the front end runs. Neither changes which program the context keeps or when it releases it. After a stored-plan hit the entry has no program, so the first layout builds one through the context and the second reuses it. I checked this with a cache directory that held only the stored plans of the--layout-pairfixtures: each pair built one program, or two withAPS5_NO_PERF_LAYOUT_PAIR=1, and both layouts matched their isolated compiles. The fixture's later checks assume no disk cache, as ctest runs it. The stored formats are unchanged, soFormatVersionstays 30. #4757 was closed unmerged, so there is nothing of it to keep.Tests on
49de39478, WSL2 lavapipe configured like CI: the full ctest had 0 failures out of 1112 tests, with 43 skipped;tools/check_skips.pyflags onlyguest_video_out_high_frame_rate(no 120 Hz display), as before. All 29 tests ofctest -R 'agc_shader|shader|recompiler'pass, including the two layout-pair tests,agc_shader_memory,agc_shader_disk_cacheand both front-end pair tests.agc_shader_memory_testsalso builds and passes on the refactor commit and on the perf commit alone. The branch also merges cleanly with the newermain107190d11, and that merge passes the full ctest on the same setup (0 failures out of 1114 tests, 43 skipped, the samecheck_skips.pyresult).Native Windows 11 (WinLibs GCC 15.2.0, Release with tests) on an RTX 5080 (NVIDIA 616.92), with
ANYPS5_REQUIRE_VULKAN=1: all 29 tests of the same filter pass. The full ctest had 1 failure out of 1135 tests, with 7 skipped ascheck_skips.py --job windowsexpects. The failure isguest_avplayer("enable video failed"), which fails intermittently in this tree (10 of 24 runs). Its executable is built only from libSceAvPlayer (which compiles in libkernel'sTimedWait.cpp), libc and the prebuilt FFmpeg, none of which this branch changes, andmain's Windows CI passes it.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
Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-13T03:47:44Z.
I reviewed #4884 against #4757. Your retained interface program changes variant preparation; #4757 fixes a reproduced CPU collision in materialized-result memo reuse. The direct overlap is the shared test insertion points and Windows CMake test-property list. I see no duplicated implementation of the memo identity fix.
I propose landing #4757 first, then rebasing #4884 while retaining the owned snapshot and exact Matches checks at both lookups. Both layout-pair CTests and agc_shader_snapshot_memo should remain registered, and the collision case should be checked with layout reuse enabled and with APS5_NO_PERF_LAYOUT_PAIR=1. I have not tested the combined branches. Would you agree to this order?
AI-assisted: yes. — Codex
Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-13T08:42:21Z.
Agreed: #4757 first. Once it merges I'll rebase #4884 onto it, keep the owned snapshot and the exact
Matcheschecks at both lookups, keepagc_shader_layout_pair,agc_shader_layout_pair_offandagc_shader_snapshot_memoregistered, and run the collision case with layout reuse on and withAPS5_NO_PERF_LAYOUT_PAIR=1before pushing.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T15:48:01Z.
Checked on Linux (PR merged onto current main, scoped build + ctest of libSceAgc/libSceAgcDriver):
agc_shader_layout_pair,agc_shader_layout_pair_offandagc_shader_memorypass; without the change the tests don't build (ResourcePrograms()). The reuse is keyed on the source entry and the code address, the kept program is released when another is built, andbindingLayoutCompleteis reset the same way the word-limit fallback already does. Not verified here: the gain on a real title (the numbers are synthetic, as the description says). Merging once the Windows build is green; #4757 will need a rebase on top.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T07:18:55Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/shader/recompiler/Recompiler.cppL467-468core/libs/prx/libSceAgcDriver/CMakeLists.txtL1313