Skip to content

perf(recompiler): translate GCN buffer formats without a private table - #12

Closed
savvaniss wants to merge 5 commits into
Get-fix:ps4/limbo-gpufrom
savvaniss:perf/ps4-gcn-format-table
Closed

savvaniss wants to merge 5 commits into
Get-fix:ps4/limbo-gpufrom
savvaniss:perf/ps4-gcn-format-table

Conversation

@savvaniss

@savvaniss savvaniss commented Oct 20, 2026 •

Copy link
Copy Markdown

What

On a device with graphics pipeline libraries, vertex and pixel shaders are built once at registration (registration modules). They read a buffer V#'s word 3 in the shader. On the GCN path gcnWord3 translates its DATA_FORMAT and NUM_FORMAT into the RDNA format code through GcnBufferFormats(), a 128-entry table. It emitted that table as a Private array and stored the whole array again (OpStore of the constant composite) before every buffer access, then indexed it with the descriptor's bits.

Drivers keep a dynamically indexed Private array in per-invocation scratch memory. On the RX 9070 XT every affected pixel shader used 528 bytes of scratch and wrote the table once per buffer access, up to 2,016 scratch_store_b128 per invocation, with 117 to 200 VGPRs. The six pixel shaders that draw Monopoly Plus's language-screen scene took about 85 of its ~90 ms of GPU time per frame this way.

This PR makes the lookup a helper function, gcn_buffer_format, emitted once per module:

  • The table is packed four 7-bit entries to a 32-bit constant (32 words).
  • A five-level select tree picks the word by the index's bits; equal neighbours share a constant instead of a select, so the all-invalid runs cost nothing.
  • OpBitFieldUExtract takes the entry's byte.
  • Each buffer access calls the function once. Nothing is stored, so no driver needs memory for it, and a uniform descriptor keeps the whole lookup in scalar registers.

The result is the same code for every one of the 128 index values. Only GCN guest shaders take this path (GcnGuestShaders()); RDNA V#s carry the format code directly. Nothing depends on the title.

Tested

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

  • agc_driver_gcn_buffer_format_lookup (new, Vulkan): a GCN vertex shader in registration form loads a dword through one runtime V# and stores it through another.
    • The module must store nothing into Private memory. Without the change it fails with "the registration module stores 2 times into private memory to translate DATA_FORMAT and NUM_FORMAT".
    • It then draws once for each of the 128 DATA_FORMAT and NUM_FORMAT pairs with the same module. The loaded value must be the input when GcnBufferFormats() maps the pair to a format and 0 when it maps it to none, and nothing may land past the stored dword. A deliberately wrong byte selection fails at DATA_FORMAT 2 NUM_FORMAT 7.
    • The buffers sit in a block allocated below 2^44: a GCN V# carries 44 address bits, and the test executable's static data lies above that on Windows.
    • It passes on the RX 9070 XT.
  • Validation layer: 29 focused tests on the base (perf(agc): store labels in unregistered host memory on the GPU #11's build, 4e16f45972) and the same 29 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 the shader memory, formatted buffer, vertex fetch, registration and prepared-module tests: agc_shader_memory with its bindless, srt_read_patching, type_zero_image_materialization, vertex_index_extents, frontend_pair and layout_pair modes, agc_shader_replay_requests, agc_formatted_buffer_once, agc_formatted_buffer_byte_stores, agc_mubuf_lds_decode, agc_embedded_vertex_fetch, agc_embedded_vertex_fetch_aliasing, and agc_driver_ buffer_descriptor_runtime, formatted_store, formatted_store_unaligned, gcn_detile, graphics, interpolation_modes, pattern_buffer_fill, prepared_exports, prepared_graphics, prepared_publication, prepared_unregistered_graphics, registration_prepare_shutdown, vertex_fetch, vertex_indexed_ring, vertex_range_bindings and zero_stride_vertex_fetch. All pass on both sides.
    • Both sides log the same VUID, already on the base: VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605, 20 times, from agc_driver_prepared_exports. The new test logs none.
    • 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.
  • tools/check_conventions.py --base 4e16f45972 passes.
  • tools/pr_overlap.py --repo Get-fix/AnyPS5 --base ps4/limbo-gpu reports only 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.

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, #10 and #11, plus local stand-ins. Base is that stack (684cb22e80), and change is the same stack with this commit cherry-picked (3fe75b613c).

  • How it was found, with temporary instrumentation (local hacks, not in this PR):

    • Per-draw GPU timestamps keyed by pixel shader variant put about 85 ms per frame in six variants: about 140 draws of one at 200 us each, 33 of another at 640 us, eight 1.3 ms depth-only draws at 2048x1152, four at 1.35 ms, 13 at 0.37 ms and one 24-index full-screen pass of 6.2 ms. The busiest is a 355-dword GCN program that became 92,452 SPIR-V words; the six modules are 92k to 125k words.

    • Building every shader as a specialized module instead (registration word limit 1) took the GPU from 89 to about 12 ms per present on the same screen. Marking shader_data NonWritable changed nothing.

    • The driver's statistics and ISA (VK_KHR_pipeline_executable_properties with internal representations) showed 25 pixel shaders with 528 bytes of scratch. 512 of those bytes are the 128-entry table. Paired before and after by their sample and load counts:

      pixel shader (image samples + compare samples) VGPRs before VGPRs after scratch before scratch after scratch_store_b128 before
      105 + 60 200 106 to 121 528 B 0 2,016
      84 + 60 172 to 189 82 to 98 528 B 0 1,952 to 2,016
      63 + 60 172 to 190 82 to 93 528 B 0 1,888 to 1,920
      42 + 60 (the full-screen pass) 169 to 179 82 528 B 0 1,888 to 1,920
      84 + 0, three buffer loads 117 26 528 B 0 160

      After the change none of the 213 captured executables uses scratch. The modules grow by about 470 SPIR-V words (the helper function).

    • With the change, the draws of the 200 us variant take 14 us each.

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

    build fps frame p50 frame p90 GPU busy ms/present worker Driver.Packet ms/frame memory_wait ms/frame presenter render_fence_wait ms/frame shader_compile_and_link ms/frame
    base 8.47, 8.53, 8.49 117.1 to 118.1 ms 118.5 to 120.1 ms 89.1 to 89.3 115.7 to 116.5 63.0 to 64.0 59.1 to 59.6 not in the top eight
    change 18.13, 18.49, 18.26 53.9 to 55.3 ms 55.9 to 56.9 ms 13.5 to 13.8 52.6 to 53.6 not in the top eight not in the top eight 33.8 to 34.2

    The language screen goes from 8.5 to 18.3 fps: GPU time per present falls from 89 to 13.7 ms. The screen is no longer GPU-bound. The queue worker is now busy for the whole frame, about 34 ms of it in shader_compile_and_link, and the per-frame WAIT_REG_MEM and the presenter's fence wait that perf(agc): store labels in unregistered host memory on the GPU #11 exposed are gone from the top eight.

  • Summary lines of the first pair:

    • Base: {"commit":"684cb22e80c4","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","window":{"spec":"seconds 120:15","first_present":1901,"ms":15107.303},"presents":128,"frame_ms":{"p50":118.129,"p90":120.091,"p99":124.215,"max":124.299},"gpu_busy_ms_per_present":89.252,"worker_busy_pct":{"queue worker 0x0":98.795},"top_phases_ms_per_frame":{"Driver.Packet":116.492,"Driver.Packet.memory_wait":62.992,"Driver.Present":62.737,"Driver.Present.render_fence_wait":59.080,"Vulkan.Retire":59.080,"Vulkan.Retire.wait":59.071,"Driver.Packet.draw":43.359,"Driver.Draw":43.029},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":6.683,"events_lost":329712,"totals_full":false}},"trace":"aps5-trace-684cb22e80c4-27108.json.gz"}
    • Change: {"commit":"3fe75b613c46","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","window":{"spec":"seconds 120:15","first_present":2863,"ms":15003.565},"presents":272,"frame_ms":{"p50":55.312,"p90":56.887,"p99":59.187,"max":60.303},"gpu_busy_ms_per_present":13.791,"worker_busy_pct":{"queue worker 0x0":97.426},"top_phases_ms_per_frame":{"Driver.Packet":53.643,"Driver.Packet.draw":42.696,"Driver.Draw":42.376,"Driver.Draw.shader_compile_and_link":34.246,"Shader.DrawStage":32.528,"Shader.DrawStage.prepared_invocation":16.258,"Shader.DrawStage.materialize":9.397,"Shader.DrawStage.capture_resources":6.804},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":3.116,"events_lost":995971,"totals_full":false}},"trace":"aps5-trace-3fe75b613c46-3876.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: GPU 89.2 ms busy per frame; queue worker busy 22.0 and waiting 27.8 ms per frame, presenter waiting 62.5. Largest slices: batch 89.2, Driver.Present 62.7, Driver.Present.render_fence_wait 59.1, Driver.Packet 49.7, Driver.Packet.memory_wait 27.8.
    • Change: GPU 13.8 ms busy per frame; queue worker busy 10.2 and waiting 0.1 ms per frame, presenter waiting 0.1. Largest slices: batch 13.8, Driver.Packet 10.3, Driver.Packet.draw 8.9, Driver.Draw.shader_compile_and_link 7.1, Shader.DrawStage 4.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>
Registration modules read a GCN V#'s word 3 in the shader, and gcnWord3
mapped its DATA_FORMAT and NUM_FORMAT through a 128-entry Private array
that every buffer access stored again before indexing it. Drivers keep
that array in per-invocation scratch: on the RX 9070 XT each pixel shader
of Monopoly Plus's language screen used 528 bytes of scratch and wrote
the table up to 60 times per invocation.

The lookup is now a helper function that packs the table four entries
to a word and picks the word with a select tree, so it stays in
registers and each access calls it once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@savvaniss
savvaniss force-pushed the perf/ps4-gcn-format-table branch from 13975fe to 369c2c3 Compare October 21, 2026 11:42
@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, with the GCN buffer-format commit on boykopovar#9430 and the rest straight on main where it is not PS4-only, 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 #11.

AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

@savvaniss

Copy link
Copy Markdown
Author

Moved upstream as boykopovar#9890, straight on main now that boykopovar#9430 has merged. Its test now enables GCN decoding after opening the device, because since boykopovar#6503 the device compiles an RDNA probe when it opens. 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