perf(agc): encode recorder batches on encoder threads from a recorded command stream (APS5_DEFERRED_ENCODE) #18173
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/18173"
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 Nesyth on 2026-11-10T17:53:18Z.
What
The draw committer records every Vulkan command of a batch by calling the driver, and submits the batch itself. With the two switches of this PR it does neither, and that time moves to other threads.
APS5_ASYNC_SUBMIT: the recorder hands each closed batch to a queue submitter thread, which callsvkQueueSubmitin order. Every other submit, present or wait on that queue drains the submitter first under the queue lock.APS5_DEFERRED_ENCODE(withAPS5_ASYNC_SUBMIT): a batch opened while it is set records its commands into a command stream instead of a command buffer. At submit an encoder thread (APS5_ENCODERS, 2 by default, 1 to 3) replays the stream into a command buffer of its own pool, and the submitter waits for that encode before it submits the batch in order.RecordCommandStreams, installed next toDeferDestructionToIdle), so everyvkCmd*function the driver resolves is one thunk per command. The thunk appends the call, with a copy of what its pointers name, when the command buffer is a stream handle, and calls the driver otherwise. AvkCmd*name without a thunk throws when it is resolved, so a new command cannot hand a stream handle to the driver. 53 commands are recorded.APS5_VERIFY_DEFERRED_ENCODEreplays each stream on the submitting thread instead of an encoder thread, to tell stream bugs from threading bugs.APS5_ABcan toggle them. With both unset the only change is thatvkCmd*lookups return the thunks, which call the driver.VK_EXT_descriptor_heapcommands are not in the stream: the Vulkan headers on main do not have them yet. They need thunks when #9508 lands.Five commits: the queue submitter, the command stream, the encoder threads, a fix for reusing a retired batch, and the fix for the two problems the Windows check of
ddb00039e0found (a test target that compilesPipelineCache.cppalone did not link, and the mock device ofagc_recorder_deferred_encode*lackedvkCmdPushConstants). Three targets that compileResources.cppalone get a queue access that does nothing (tests/QueueAccessNone.cpp).Design note: batches encoded and submitted off the drawing thread
Scope once this sits on #17943 and #18029
#17943 defers the
vkCmd*calls of a draw's recording window and replays them on one encoder thread per devicewhile the batch is still open; #18029 adds transfers and occlusion queries to that stream. This PR keeps two
things they do not have and drops everything else:
is closed, by one of
APS5_ENCODERSthreads (1 to 3, 2 by default), so batches encode side by side.vkQueueSubmitruns on one thread per queue, in the order thebatches were closed; the drawing thread does not wait at
Submit.Both stay opt-in (
APS5_ASYNC_SUBMIT,APS5_DEFERRED_ENCODE); with both unset the defaultvkCmd*path isthe one #17943 leaves.
Threads and ownership
APS5_DRAW_PIPELINE, otherwise the queue worker)Close), its own pool and buffers. No Recorder state, no guest memory.VkQueue)vkQueueSubmitunder the queue mutex.QueueAccess: drains the submitter, then holds the queue mutex.What is recorded
A batch opened while
APS5_DEFERRED_ENCODEis set has noVkCommandBuffer; its command handle is a streamhandle. Each recorded
vkCmd*call is appended with a copy of everything its pointers name, so the streamholds values only. Objects a command refers to are kept by the batch as they are today and released when the batch
completes, which is after its fence signalled, so after its submit and its encode.
Ordering
command buffer is the one inline recording produces (
agc_command_stream,agc_recorder_deferred_encode*compare them).
strictly in that order. An encode that finishes early waits in its ticket; a late one makes the submitter
wait (
EncodeTicket::Wait), never reorder.QueueAccessdrains the submitter before a present, a direct submit ora wait, so they see every earlier batch submitted.
(
WaitSubmitReturned) before the batch's results are read.vkCmd*the driver resolves, thecheckpoint markers and the query commands included, is a thunk that records into the stream; a
vkCmd*name without a thunk throws when it is resolved, so no path can hand a stream handle to the driver.
Where the drawing thread waits
Never while recording and never at
Submit. It waits only where it needs the queue (throughQueueAccess) or abatch's result (through the batch's fence, after its submit returned).
Failure
An exception during a replay fails the ticket. The submitter then submits that batch without commands, so its
fence and semaphores still signal in order, stores the error, and the drawing thread gets it at its next
enqueue (
RethrowFailure). A failure ofvkQueueSubmitis stored and rethrown the same way.Fallbacks
APS5_VERIFY_DEFERRED_ENCODE: every stream is replayed on the submitting thread, which separates streamerrors from threading errors.
What it adds to #17943, and what it costs
vkQueueSubmitoff the drawing thread; no settle pointwhile a batch records, because nothing touches a real command buffer before the batch is closed.
records; the submitter's waits for encodes are counted (
[encode]line).can, this PR reuses it and drops its own stream and thunks; if not, the note will say what is missing.
Measured (main
7f96086b4e, an open-world, draw-heavy title, in-run A/B, 10 pairs)Presents per 10 s 105.04 -> 116.64 (+11.50 +- 0.48 against the neighbouring phases, +11.0%); frame time
95.23 -> 85.76 ms. Traces of main and of the PR from one session are linked in the description.
Tested
Windows 11 Pro 26300, Radeon RX 7900 XTX (AMD Software 26.10.1), Ryzen 7 7800X3D, Release, MinGW-w64 GCC 15.2.
An open-world, draw-heavy title on main
7f96086b4e, the same save and scene in every boot, no other switches.In-run A/B on the PR build (
APS5_AB=30:APS5_ASYNC_SUBMIT+APS5_DEFERRED_ENCODE, phases of 30 s from 190 s after the first present, 10 pairs, read withtools/ab_split.py):Separate boots, 313 s in game each, with
APS5_TRACE_SECONDS=300:30:7f96086b4eTwo more boots of main without a trace gave 7.46 and 7.78 FPS over 229 s in game. No boot had a fatal error or a lost device. The measured build is 151796f744. The head differs from it in tests and in where the thunks are installed; one more boot of the head with both switches set gave 8.48 FPS over 143 s, again without a fatal error.
Both traces are incomplete: the queue worker and the draw committer filled their buffers inside the 30 s window, as the digests below say. The summary lines are complete.
Traces, both captured on the same machine in one session: main 7f96086b4e, this PR.
Tests: on the head, after a build of every target:
agc_command_stream,agc_driver_recorder,agc_driver_recorder_deferred_encodeand_verify,agc_recorder_deferred_encode,_one,_threeand_verify,agc_submit_progress_deferred_encodeand_verify,agc_pipeline_cache_allocation_*,agc_buffer_pool*,agc_command_batch_failure,agc_driver_pooled_buffer_range,agc_deferred_destructionand the runtime switch tests pass (28 of 28).trace_digest: main
7f96086b4eseconds 300:30 from present 4191: 219 presents, frame p50 136.5 / p90 144.7 / p99 156.2 / max 193.5 ms, GPU busy 32.7 ms per present
AMD Radeon RX 7900 XTX, AMD proprietary driver 26.10.1 (LLPC), Windows 10.0.26300, 3840x2160, commit
7f96086b4e, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 32.7, Graphics.Draw 31.1, Graphics.Draw.draw_and_resource_release 25.2, Driver.Packet 16.7, Driver.Packet.draw 13.7, Driver.Draw 13.5, VideoOut.Flip 7.7, Driver.Present 7.6, VideoOut.Flip.present 7.5, Driver.Draw.shader_compile_and_link 4.5
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
trace_digest: this PR, both switches set
seconds 300:30 from present 4494: 250 presents, frame p50 120.0 / p90 125.4 / p99 130.1 / max 131.9 ms, GPU busy 32.3 ms per present
AMD Radeon RX 7900 XTX, AMD proprietary driver 26.10.1 (LLPC), Windows 10.0.26300, 3840x2160, commit 151796f74490, dropped: yes
Incomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 32.3, Graphics.Draw 27.9, Graphics.Draw.draw_and_resource_release 22.1, Driver.Packet 14.0, Driver.Packet.draw 11.6, Driver.Draw 11.4, VideoOut.Flip 8.2, Driver.Present 8.1, VideoOut.Flip.present 8.0, Graphics.Encode 4.4
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T16:24:10Z.
Review: COMMENTED
Reviewed — comments
Changes are opt-in, but I could not verify all changed files or run the claimed checks, so a formal approval is not justified.
What I ran
Reviewed head
6cf4c1a0b5f9dd9f2eb24afc8acbbdd5131cb189using the prepared diff andrepo_read.pyreader. No build or tests were run. The description reports author-run Windows tests and performance measurements; these were not independently verified.Results
Findings
Blocking
None verified.
Non-blocking
None.
Not verified
I read
review/description.md,review/commits.md,review/manifest.json, and targeted ranges ofreview/diff.patch. I inspected selected queue submission and command stream changes. I did not read every changed file listed in the manifest; several changed files were excluded from the review folder as too large. I did not run tests, builds, or performance measurements. The performance claims and reported test results are not independently verified.To get this to a good state
Nothing is required. The points below are optional: complete review of the unread changed files and run the checks on this head before treating the review as a full validation.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T22:33:25Z.
Review: COMMENTED
The trial merge onto
c1601e56d5conflicts in the recorder, device, resource, and runtime-switch files. When resolving it, preserve the command-stream ownership tests for nestedpNextand supported arrays, as well as the queue-submit tests that keep submission order when encoding finishes out of order. The synchronous verify mode is useful to isolate stream capture, but it does not exercise handoff failure or batch retirement on the threaded path. Re-run both modes and capture paired scene traces before carrying forward a speed result.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.