Skip to content

perf(agc): store labels in unregistered host memory on the GPU - #11

Closed
savvaniss wants to merge 4 commits into
Get-fix:ps4/limbo-gpufrom
savvaniss:perf/ps4-device-lock-wait
Closed

savvaniss wants to merge 4 commits into
Get-fix:ps4/limbo-gpufrom
savvaniss:perf/ps4-device-lock-wait

Conversation

@savvaniss

@savvaniss savvaniss commented Oct 20, 2026 •

Copy link
Copy Markdown

What

A label or store into memory no host import covers (WriteLabelOnGpu reason 3) makes the recording thread wait for all recorded work (WaitIdle) and then write it with the CPU. c9ab6a7 chose that over a store at batch reap because a later label stored on the GPU could become visible first (boykopovar#1458).

VideoOut's buffer labels hit that path on every PS4 flip. PatchFlip in libSceGnmDriver writes 1 to VideoOutBufferLabelAddress(handle) + index * 8 with WRITE_DATA before the flip packet. That address is bufferLabels inside VideoOut's host-side config object, and no guest allocation covers it. So once per frame the draw committer ran commitLabel, found no import and waited for the device under the GPU mutex. The queue worker then waited for the committer at the next DUMP_CONST_RAM. This is the ~69 ms per frame that #10's numbers showed as Driver.Packet.device_lock: that timer mark covers the draw pipeline drain.

This PR stores such labels on the GPU instead:

  • Graphics::UnregisteredHostImport imports the pages holding the label's bytes, rounded to the device's import alignment. It does so only when no guest allocation range overlaps them and, on Windows, every page is committed MEM_PRIVATE memory with exactly PAGE_READWRITE protection in a single allocation.
  • WriteLabelOnGpu uses that import when HostImportFor finds none. It records the store into the open batch as for any imported label, and Recorder::Keeps the import with that batch so the buffer outlives the store.
  • The GPU stores the label in stream order after the work before it, as the console's CP does. A later label cannot become visible first, and nothing waits.
  • Memory that fails the checks (image or mapped sections, read-only, guard or no-access pages, ranges spanning allocations, partly registered pages) or whose import fails still returns reason 3 and waits as today.
  • The libSceAgcDriver debt entry for unimported labels now names what still waits.

Nothing depends on the title: any RELEASE_MEM, WRITE_DATA, DMA_DATA, COPY_DATA or DUMP_CONST_RAM target in host memory outside the guest registry takes this path (VideoOut's buffer labels, a label on a native thread stack).

The second commit, 75bc9a49b7, fixes a driver test the first one broke. A DMA_DATA copy over 64 KiB into host memory that no import covers waits for the batches that write its source (Recorder::SyncThrough) and copies on the CPU, unless something can still store over its destination; then it drains the device. Before the first commit, a label into such memory was written by the CPU after a drain, so nothing was pending there. Now that label is a GPU store in the open batch, and agc_driver's copy behind a label into its destination drained.

  • The copy now waits for the batches that store into its destination, as it already did for its source, and then checks again.
  • Only what still stores there after that wait drains as before: a storage image's write-back, a unit shadow, or a label another queue has not recorded yet.

This branch is #10's commit edf584d568 plus these two; review 4e16f45972 and 75bc9a49b7.

Tested

Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, Release, on boykopovar#6977's head 93eeee014 with #8 and #10, which this branch stacks on.

  • agc_driver_unregistered_label_on_gpu (new, Vulkan):

    • A 4-byte label into a VirtualAlloc block that is not registered returns reason 0, and the word stays 0 until the batch is submitted.
    • A later label into a registered block lands after it. When the later label is visible, the first holds its value.
    • A label into a read-only page of the unregistered block still returns 3 and stores nothing.

    It passes on the RX 9070 XT. Without the driver change it fails with "a label in unregistered host memory returned reason 3 instead of 0". The existing agc_driver_unimported_label_order (PPSA21567: submission stops on "unsupported PM4 packet type: header 0x00000001" where a RELEASE_MEM header is expected boykopovar/AnyPS5#1458's case, a label in the test image's static data) still passes; that memory is MEM_IMAGE and keeps the wait.

  • Validation layer: 48 focused tests on the base (perf(agc): read recorded store bytes in captures instead of waiting for the GPU #10's build, edf584d568) and the same 48 plus the new one here, on the RX 9070 XT (ANYPS5_GPU=AMD), with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. They are perf(agc): read recorded store bytes in captures instead of waiting for the GPU #10's set plus every label, flip, DMA and WRITE_DATA test: agc_dma_data_tests, agc_write_data_patch_tests, agc_driver_flip, agc_driver_flip_compute, agc_driver_pipelined_dma, agc_driver_write_data_written_ahead and the video_out_flip_* modes. All pass, except the 11 video_out_flip_* modes that ctest skips on both sides (the build runs them under SDL's offscreen driver, which has no Vulkan on Windows).

    • Per test, both sides log the same VUIDs, all already on the base: VUID-vkCmdPipelineBarrier-srcAccessMask-02815 (agc_unit_shadow, agc_shadowed_key_stores), VUID-vkBeginCommandBuffer-commandBuffer-00050 (agc_driver_partially_resident_pending_stores) and VUID-vkDestroyDevice-device-05137 (agc_driver_vertex_target_range_copied).
    • Every device the tests opened was the RX 9070 XT, and no log contains was not found but was requested by env var VK_INSTANCE_LAYERS.
  • video_out_flip_tests run directly with SDL_VIDEODRIVER=windows under the same layers, both sides. labels, labels_main, lifetime, reopen, decode, controls, overlay and unopened pass with no VUID line. ps4, eop and eop_main fail the same way on both sides: on feat: run PS4 games on the AGC driver — Limbo (CUSA01664) in game (integration branch) boykopovar/AnyPS5#6977's branch the test's 7-dword RELEASE_MEM is rejected as an invalid packet size, and ps4 stops at "a PS4 buffer set was changed to tiled". eop_aftergpu exits 127 on both. None of that involves this change.

  • agc_driver (existing) failed on the first commit. Its testCopyIntoHostMemory runs a plain copy into a std::vector and a copy behind a WRITE_DATA into the same vector. The bytes were right in both, but the run behind the label failed with "a copy into host memory drained instead of syncing its source" (a temporary tag on the message showed it was that run, not the plain one). With the second commit it passes 10 runs out of 10, and agc_driver_unregistered_label_on_gpu still passes 10 out of 10.

  • Validation layer for the second commit: the 49 tests above plus agc_driver, on the first commit (4e16f45972) and on the second, with the same GPU and environment.

    • First commit: agc_driver fails and the other 38 that run pass.
    • Second commit: all 39 that run pass. The 11 video_out_flip_* modes are skipped on both sides, as above.
    • Each test logs the same VUIDs on both sides: the three listed above, plus VUID-vkDestroyDevice-device-05137 in agc_driver on both.
    • In the first of two full runs on the second commit, agc_driver_store_over_untracked failed once with "the result was never held pending". It submits only register writes and dispatches, so it never reaches the changed DMA_DATA path. It passed in the second full run and in 20 repeats.
  • The Monopoly numbers below are from the first commit. The second changes only DMA_DATA copies over 64 KiB into host memory that no import covers.

  • tools/check_conventions.py --base edf584d568 passes.

  • tools/pr_overlap.py --repo Get-fix/AnyPS5 --base ps4/limbo-gpu reports fix(agc): PS4 display, textures and GCN instructions for CUSA03312 #1, on the ShaderDiskCache.cpp lines of perf(agc): bound vertex-indexed guest buffers by the draw's vertex range #8's commit, and perf(recompiler): translate GCN buffer formats without a private table #12 to perf(agc): check a raw shader's address range only when its snapshot is read again #15, which stack on this branch.

Monopoly Plus (CUSA03312), RX 9070 XT, APS5_RESIDENT_LINEAR=1, on the local PS4 stack the title needs to start: boykopovar#6977's head with #1, #2, #5, #8 and #10, plus local stand-ins. Base is that stack (2c09e4328d), and change is the same stack with this commit cherry-picked (684cb22e80).

  • Profile of the base (APS5_PROFILE_DRAW=1) on the language screen, per 10 s:

    • The worker waited 5.4 to 5.8 s for the draw pipeline to drain, at about 5,200 DUMP_CONST_RAM.
    • The committer held the GPU mutex at the label site 84 times (once per flip) for 5.2 to 5.5 s, at most 67 ms, almost all of it in GPU waits under the lock. Every idle sync on the [recorder] line came from commitLabel's WaitIdle.
    • A temporary log showed each such label as a 4-byte WRITE_DATA of 1 with reason 3, alternating between two addresses 8 bytes apart and waiting 62 to 67 ms. The memory is private read-write heap with no registered range, and the writer is PatchFlip.
  • The same profile with the change: 84 label holds in 30 ms (at most 0.4 ms), no GPU wait under the lock, 0.3 s of pipeline drains.

  • Alternating runs, APS5_TRACE_SUMMARY_EVERY=15 with a 120:15 window, both builds on the language screen from about 30 s:

    build fps frame p50 GPU busy ms/present device_lock ms/frame memory_wait ms/frame presenter render_fence_wait ms/frame
    base 8.34, 8.26, 8.37 119.2 to 121.0 ms 90.2 to 91.0 68.7 to 70.0 not in the top eight not in the top eight
    change 8.33, 8.32, 8.30 120.1 to 120.4 ms 90.6 to 90.9 not in the top eight 63.9 to 64.1 57.6 to 57.8

    The frame rate does not change: this screen is GPU-bound. Each frame's command buffer starts with a WAIT_REG_MEM on a buffer label that the previous flip releases, and that flip waits for the frame's GPU work. The time the worker spent waiting for the device lock is now spent in that wait, and the presenter waits for the GPU instead of the committer. The change removes a device drain under the GPU mutex once per flip; this title's pacing hides it.

  • Where the ~90 ms of GPU time per present goes, measured with temporary per-draw timestamps keyed by pixel shader variant (a local hack, not in this PR). [gputime] puts it all in the draw class; labels, uploads, write-backs and the present blit take under 0.3 ms.

    • The language screen draws a 3D scene behind the menu: meshes of 1,095 to 9,573 indices into 1920x1088 color and 2048x1152 depth-only passes.
    • Six pixel shader variants take about 85 ms per frame: about 140 draws of one at 200 us each, 33 of another at 640 us, eight 1.3 ms depth-only draws, and one 24-index full-screen pass of 6.2 ms. The ~345 UI draws per frame take about 3 us each.
    • The per-draw times are the same with APS5_NO_HOST_IMPORT=1, so reading buffers from host memory is not the cause, and they do not follow the index count. That points at the execution of those recompiled shaders, a separate investigation.
  • Summary lines of the first pair:

    • Base: {"commit":"2c09e4328dc6","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_RESIDENT_LINEAR":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"120:15","APS5_TRACE_SUMMARY_EVERY":"15"},"gpu":"AMD Radeon RX 9070 XT","driver":"AMD proprietary driver 26.8.1 (LLPC)","os":"Windows 10.0.26300","resolution":"1920x1080","presents":126,"frame_ms":{"p50":119.365,"p90":120.376,"p99":141.206,"max":141.665},"gpu_busy_ms_per_present":90.208,"top_phases_ms_per_frame":{"Driver.Packet":118.356,"Driver.Packet.device_lock":68.705,"Driver.Packet.draw":44.898,"Driver.Draw":44.572,"Driver.Draw.shader_compile_and_link":36.066,"Shader.DrawStage":34.068,"Shader.DrawStage.prepared_invocation":16.661,"Shader.DrawStage.materialize":10.099},"trace_dropped":true,"trace":"aps5-trace-2c09e4328dc6-27408.json.gz"}
    • Change: {"commit":"684cb22e80c4","dirty":false,"env":{...same...},"gpu":"AMD Radeon RX 9070 XT","driver":"AMD proprietary driver 26.8.1 (LLPC)","os":"Windows 10.0.26300","resolution":"1920x1080","presents":125,"frame_ms":{"p50":120.123,"p90":121.942,"p99":123.349,"max":124.271},"gpu_busy_ms_per_present":90.867,"top_phases_ms_per_frame":{"Driver.Packet":118.599,"Driver.Packet.memory_wait":63.895,"Driver.Present":63.634,"Driver.Present.render_fence_wait":57.711,"Vulkan.Retire":57.71,"Vulkan.Retire.wait":57.702,"Driver.Packet.draw":44.046,"Driver.Draw":43.716},"trace_dropped":true,"trace":"aps5-trace-684cb22e80c4-28284.json.gz"}
    • Both traces dropped the worker's slices partway; the summary totals cover the whole window.
  • tools/trace_digest.py of the same two traces (slices kept before the drop):

    • Base: worker busy 49.7 ms and waiting 0.1 ms per frame, GPU 90.2. Largest slices: Driver.Packet 49.8, Driver.Packet.device_lock 29.7, Driver.Packet.draw 19.4.
    • Change: worker busy 22.7 ms and waiting 28.7 ms per frame, presenter waiting 63.4, GPU 90.8. Largest slices: Driver.Present 63.6, Driver.Present.render_fence_wait 57.7, Driver.Packet 51.3, Driver.Packet.memory_wait 28.7.
    • The traces are not attached. They stay on the test machine and can be shared.

Checklist

🤖 Generated with Claude Code

savvaniss and others added 3 commits October 20, 2026 12:09
A GCN vertex shader reaches its fetch subroutine through s_swappc. The
driver links that subroutine into the program, so its buffer loads stay
generic guest-buffer loads indexed by v0 rather than lifted vertex
attributes. Their V#s describe the whole vertex ring, so every draw bound
and, when the draw cache reused a draw at a moved base, snapshotted the
ring whole.

The resource tracker now proves, per buffer resource, that every access
is an IDXEN load whose index is the VertexIndex builtin and whose VGPR
offset and SOFFSET are immediates, and records how far past
index * stride those loads read (formatted loads count 16 bytes). The
value reaches the driver as DescriptorBinding::bufferVertexExtent (shader
disk cache format 37).

For a direct draw on the vertex path without mesh or tessellation
stages, a read-only element with that proof is bound, alias-checked and
snapshotted when its V# moves only up to maxIndex * stride plus the
extent, rounded up to 16 bytes and never past the V#'s own size. maxIndex
is the bound vertex attributes already use: the highest index plus the
base vertex, or the last vertex of an auto draw. A recipe hit whose
template was bound for another vertex range is a miss. Indirect draws,
written elements, swizzled or ADD_TID V#s and any other access keep the
whole range.

Monopoly Plus (CUSA03312) draws its quads from a 40 MB ring whose base
advances 0x60 bytes per draw. Its 0x27ffff0-byte draw snapshots are gone,
and its language screen runs at about 6.1 instead of 2.9 frames per
second on an RX 9070 XT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…or the GPU

A COPY_DATA, DMA_DATA or DUMP_CONST_RAM store of up to 64 KiB that the
driver resolves while GPU work is open is recorded into the batch as a
vkCmdUpdateBuffer into the host import, and its range becomes a pending
write. The next draw's resource capture reads the dumped descriptor
table through the flush hook, which submits the batch and waits for the
GPU although the driver already holds the bytes.

The recorder now keeps the bytes of stores wider than the 64-byte label
table too, in record order, and flags them overwritten like table
entries when later GPU work writes over them. Recorder::LookupStoredBytes
composes a range from the newest store of each dword. It refuses a dword
without a store, one a later GPU write covers, a label behind a
completion action and a range under a queued label. Everything recorded
before the store lands first and the store lands before anything
recorded after it, so the bytes are what the next draw reads on the GPU.

classifyPendingWrite answers KnownValue for such a range when the caller
takes the bytes (captures, cached draw and dispatch validation), so the
capture reads them without the wait. Callers that pass no buffer, and
every other pending write, sync as before. APS5_NO_STORED_KNOWN_VALUES=1
turns the path off, and APS5_VERIFY_KNOWN_VALUES=1 checks each served
word against the GPU's; the [capture] profile line now reports both
counts.

Monopoly Plus (CUSA03312) dumps 144- to 512-byte descriptor tables from
constant RAM about 100 times per frame on its language screen, each
followed by a draw whose capture waited about 1 to 2 ms for the GPU.
That screen now runs at about 8.3 instead of 6.1 frames per second on
an RX 9070 XT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A label or store into memory no host import covers made the recording
thread wait for all recorded work and write it with the CPU (boykopovar#1458).
VideoOut's buffer labels live in host memory no guest allocation
covers, so every PS4 flip label drained the device under the GPU lock:
the draw committer waited about 63 ms per frame and the queue worker
waited for the committer at the next DUMP_CONST_RAM.

Such memory is now imported for the batch that stores into it, when its
pages are committed private read-write memory of one allocation and no
guest range overlaps them. The GPU stores the label in stream order, as
the console's CP does. Memory that fails those checks or the import
keeps the wait.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…raining

A DMA_DATA copy into host memory that no import covers waits for the
batches that write its source (Recorder::SyncThrough) and copies on the
CPU, unless something can still store over its destination; then it
drains the device. Until the previous commit a label into such memory
was written by the CPU after a drain, so nothing was pending there. Now
that label is a GPU store in the open batch, and a copy behind a label
into its destination drained: agc_driver failed in its label case with
"a copy into host memory drained instead of syncing its source".

The copy now waits for the batches that store into its destination as
it does for its source, then checks again. Only what still stores there
after that wait (a storage image's write-back, a unit shadow, a label
another queue has not recorded yet) drains as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Get-fix

Get-fix commented Oct 21, 2026

Copy link
Copy Markdown
Owner

Thanks @savvaniss. boykopovar#6977 is now a draft integration branch, and its own work is split into boykopovar#9429 to boykopovar#9433 (details). The maintainers asked that the branch not land anything without its own review, so could you open this upstream against main, straight on main for the changes that are not PS4-only, and on top of boykopovar#9431 for the rest, with Depends on? Once it's open, I'll merge it into ps4/limbo-gpu, list it as a member and run Limbo with it. ps4/limbo-gpu now moves to the split branches, so this pull request may show conflicts here. This one stacks on #10, which stacks on #8.

AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

@savvaniss

Copy link
Copy Markdown
Author

Moved upstream with #10 and #21 as boykopovar#9898 on main, with 75bc9a49b7 folded into its commit. Closing this one.

@savvaniss savvaniss closed this Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants