perf(agc): prepare registered shaders in the background #2583
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2583"
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 gjonn on 2026-10-10T07:02:55Z.
What
Driver::RegisterShaderruns the full registration-time preparation (graph build, structurizer,PrepareShader: RDNA to IR translation and the SRT/resource plan) on the guest thread that registers the shader. The recompiler's source and plan cache is memory-only, so this work is repeated for every registered shader on every boot, even with a warm shader disk cache. A title that registers hundreds of shaders while loading blocks its loader thread one shader at a time.This change splits
PrepareRegisteredin two:PlanRegisteredruns at registration as before. It reads the registers, validates the entry point and header metadata, decodes the code, decides deferral and builds the recompile request. Invalid registrations still fail synchronously inRegisterShader.PrepareShader) runs on a small background pool:hardware_concurrency() / 2detached pthreads (at least 2) with 64 MiB stacks, because the recompiler can recurse deeply.The snapshot is published right away and marked pending. When the job finishes, its entries and the registered ABI key are merged into
snapshot->preparedunder its mutex, the pending flag is cleared, and waiters are notified.SourceHandleFor,InvocationForandShaderPreparationTransaction::Edit/Readwait for a pending snapshot before they look at its prepared state, so a draw, dispatch or ABI resolution that arrives early waits for the in-flight job instead of reporting a missing artifact or preparing the shader a second time. If the background job throws, the error is logged and rethrown at the first use of that shader.Transactions now copy the state on
Read, as well as onEdit.Commitskips snapshots that were only read, and before swapping it appends any entries and registered ABIs that were added to the live state after the copy. Without this, a reader could iterateentrieswhile another thread was appending to it, andCommitcould drop artifacts appended concurrently by the first-use paths or the background merge.APS5_SYNC_SHADER_PREPARE=1restores the old synchronous behaviour. ABI resolution and the null pixel shader are unchanged.Tested
Windows 11, MinGW-w64 GCC, NVIDIA RTX 5090 Laptop GPU, Release build.
Title
PPSA01317, two warm-cache boots on the same machine with the same settings, with and without the background preparation. These runs used a local build that carries the same change; I did not repeat the title run on this exact branch after porting it to currentmain.PrepareResourceProgram/Structurizer/SrtPlanBuilderwhile the other game threads waited about 15 s on GPU end-of-pipe events during menu load. With the change, that stall is gone.Unit tests:
agc_shader_bindless,agc_shader_frontend_pair,agc_shader_frontend_pair_off,agc_shader_memory,agc_shader_disk_cache,agc_driver_prepared_exports,agc_driver_prepared_graphics,agc_driver_prepared_publicationand the sixagc_driver_prepared_shaders*variants.APS5_SYNC_SHADER_PREPARE=1main(092e133e)A new case in
agc_driver_prepared_shaderscovers the pending state.SourceHandleForandInvocationForcalled on a pending snapshot wait for the background result and reuse it. A failed background preparation is rethrown byShaderPreparationTransaction::Read,SourceHandleForandInvocationFor. The existing registration tests still see invalid program addresses, out-of-range entry points, truncated metadata, zero thread counts and undecodable code fail synchronously inRegisterShader.Full
^agc_CTest run with this PR: 295/296 pass in both modes. The one failure,agc_driver_f32_arithmetic_nan, also fails on unmodifiedmainon this machine.Overlap
Two open PRs change the same code. Neither does what this PR does, and they are complementary, but they will conflict textually (
git merge-treereports conflicts inShaderRegistry.cppagainst both heads).PrepareRegisteredout of the preparation writer lock so concurrent registering threads stop serialising. Preparation still runs synchronously on each registering thread. This PR stopsRegisterShaderfrom waiting for the heavy preparation at all, which is what helps a single loader thread that registers serially. Whichever lands second needs a small rebase inRegisterShaderand the transaction lock code.PrepareRegistered. That makes each background job cheaper. Here the pre-check has moved into the background half (PrepareRegistered(RegisteredPreparation&)), so #2327 would drop those two lines there after a rebase. #2327 also needs a rebase onto currentmainon its own.Checklist
main; no other open PR implements the same functions (related open PRs: see Overlap)NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T07:55:40Z.
Rebased onto current
mainand added a short section todocs/dev/ARCHITECTURE.mdas a separate commit.mainnow defers on undecodable code inPlanRegistered, and #2327 dropped the structurize pre-check, so the background half no longer builds/structurizes the graph either.prepared_shaders*/ publication / exports / graphics tests 20× with--repeat until-failall pass, the new test fails to build without the change.APS5_SYNC_SHADER_PREPARE=1): time the registering thread is blocked 280 ms vs 732 ms (median); time until everything is prepared is not shorter (853 vs 733 ms, noisy), so the gain is freeing the loader thread, as the description says.Merging once CI is green.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T08:01:13Z.
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/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppL139-141, L817Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-10T12:32:46Z.
I’m looking at this for Yōtei’s registration stall, and found a shutdown lifetime issue in
7fdcc34. The queued plan copies the device target, but its capability and extension spans point into the device-owned shader profile. The task captures only the snapshot and plan, while shutdown joins the existing driver workers and resets the devices without draining this detached pool. Registering an unused shader and shutting down before its job finishes can therefore leave the job reading released profile storage. Could the pool be included in shutdown, with the target's owner retained until every queued or active job has finished or been cancelled?There’s also a pending-state failure path: the snapshot is published before task submission, and queue insertion can throw; the result-vector allocations at lines571–572 are outside the worker's catch. Those paths need to settle and notify the snapshot too, otherwise submission failure can leave a waiter blocked and result publication can escape the worker. A latch-based shutdown test and injected submission/publication failures would cover these without relying on sleeps. This is source review only; I haven’t run or measured this branch.