Skip to content

fix(recompiler): fold draw offsets that the shader only reloads after the fetch add - #7591

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
mrfelek:fix/recompiler-offset-sgpr-reuse
Oct 19, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
mrfelek:fix/recompiler-offset-sgpr-reuse

Conversation

@mrfelek

@mrfelek mrfelek commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

What

An indirect draw goes GPU-side only when the vertex shader's draw-offset SGPRs (base vertex, start instance) are read by nothing but the fetch add that EmbeddedVertexFetchAnalyzer folds into the draw (vertexOffsetShared / vertexIndexObserved). Diablo II: Resurrected (PPSA03912) fails that check on almost every indirect draw (patched SGPR not folded, about 3,000 per 10 s), so each draw reads its arguments on the CPU. Once per frame that read waits for the compute dispatch that writes the instance count.

The game's vertex shaders read the offset SGPR only in the fetch add. Four parts of the check were too wide for them:

  • s_load_dword* base width. scalarOperandWidth counted every SMEM base as 4 SGPRs. s_load_* takes a 64-bit address (2 SGPRs); only s_buffer_load_* takes a 4-SGPR V#. So s_load_dwordx2 s[4:5], s[26:27] counted as a read of s28 and s29.
  • Reuse after the add. These shaders reload the offset SGPRs after the add (s_buffer_load_dwordx16 s[28:43], ..., s_load_dwordx2 s[28:29], ...) and read only the new values. The shared check now walks the program from the start:
    • a read of the register outside the fetch adds still makes it shared;
    • a full overwrite ends the walk with "not shared", unless a fetch add follows it or a later branch goes back to or before it. The overwrite has to be a load, an SOP1/SOP2 result, s_movk_i32 or v_readlane/v_readfirstlane. s_cmov, s_bitset* and s_movreld don't count, and s_movrels*/s_movreld* anywhere make the register shared;
    • forward branches are followed: an overwrite that a pending forward branch can skip does not count;
    • a backward branch, or anything else that leaves straight-line code, falls back to the previous whole-program check.
  • s_mov_b64 exec, -1 before the add. The shaders enable every lane before the fetch add. With all lanes on, the add runs on every lane, so the folded index is the same. The observed check now ignores this write. s_mov_b32 exec_lo, -1 stays observed (only half of a wave64).
  • v_sad_u32 carry. carryObserved returned true when destination2 was not decoded. v_sad_u32 has no carry output, so its destination2 stays Unknown. An unclamped v_sad_u32 add now has no observable carry.

On the D2R scene below, indirect draws on the CPU path go from 2,970 to 57 per 10 s. What remains is a DRAW_INDIRECT whose shader has no fetch add and never reads s20/s21 at entry. That case is not part of this PR.

Tested

Linux: Ubuntu 26.10 (kernel 7.3), Ryzen 9 5950X, RTX 3080 Ti (NVIDIA 615.71.09 open), GCC 15.3.0, Release.

Windows: Windows 11 Pro 26200, i7-11850H, RTX A2000 Laptop GPU (NVIDIA 610.60), WinLibs GCC 15.2.0, Release.

Linux / AMD: Ubuntu 26.04.1 (kernel 7.0), Ryzen 9 PRO 8945HS, Radeon 780M (RADV, Mesa 26.0.8), GCC 15.2.0, Release.

  • agc_driver_draw_offset_indices gets four analysis cases:

    • an unclamped v_sad_u32 vertex add: not shared;
    • s_mov_b64 exec, -1 before the adds: not shared;
    • s16/s17 reloaded by s_load_dwordx2 from s[14:15] after the adds and then read: not shared;
    • the same read after a branch that can skip the write of s16: shared.

    The first three fail on main. The fourth passes on both, and guards the branch handling.

  • These tests pass on all three machines: agc_driver_draw_offset_indices, agc_embedded_vertex_fetch, agc_embedded_vertex_fetch_aliasing, and the agc_driver_* tests gpu_indirect_arguments_misaligned_writer_ahead, gpu_indirect_arguments_written_ahead, indirect_arguments_written_ahead, indirect_patched_stage, rect_list_indirect, scalar_swappc_fetch, vertex_fetch and zero_stride_vertex_fetch.

  • D2R (PPSA03912) on Linux: in game with a saved character, Graphics Mode "Performance", no input. Measured on main a62c724 and on this change on top of it (b383ad4), each relinked with its own relinker. APS5_PROFILE_DRAW=1 APS5_TRACE_SUMMARY_EVERY=10, 120 s from 60 s after the character loads, main and this change run alternately. The branch was then rebased onto 81cb863 without conflicts, and later onto 3686960 after fix(recompiler): recognize plain no-carry draw offset adds #6919 merged (one conflicting line in the analyzer: both early returns, VAddNcU32 and VSadU32, kept); the related tests pass at that head (2234121).

main a62c724 this change
Indirect draws GPU-side / CPU-side, per 10 s 0 / 2,970 3,021 / 57
Presents per 10 s 56.8 56.7, 58.6
Frame p50 (trace_digest, steady intervals) 174–178 ms 168–174 ms

trace_digest.py rows from the same runs (steady part):

main a62c724c3   | 95-105  | 58 | 172.4 | 182.9 | 188.3 | 188.3 | 107.9 | 98 % | Driver.Packet 312.4 |
                 | 146-156 | 58 | 174.1 | 181.7 | 190.7 | 190.7 | 107.9 | 98 % | Driver.Packet 312.4 |
this change b383ad424 | 118-128 | 60 | 167.3 | 175.1 | 180.8 | 180.8 | 100.8 | 98 % | Driver.Packet 301.0 |
                 | 148-158 | 59 | 170.0 | 175.5 | 179.4 | 179.4 | 103.2 | 98 % | Driver.Packet 302.6 |

On main alone the gain is small, because another wait dominates this scene: a depth bind reads HTILE under a pending fill, about 3.5 s per 10 s (#6505). With #6505 applied, and at Graphics Mode "Quality", the indirect-argument read became the main remaining stall. It was about 2.95 s per 10 s, once per frame, at DRAW_INDIRECT/DRAW_INDEX_INDIRECT indirect-args. This change took that scene from 60 to 63 presents per 10 s (main 1b0c7cf + #6505, same machine).

  • D2R (PPSA03912) on Linux, 2026-10-20, the game's default settings, current main: the same scene with no change to the game's settings, on main 3686960 (which now has perf(agc): take the HTILE words pending fills leave instead of reading them #6505) and this head (2234121) on top of it, each relinked with its own relinker. APS5_PROFILE_GPU=1 APS5_PROFILE_DRAW=1 APS5_TRACE_SUMMARY_EVERY=10, 120 s from 60 s after the character loads, run alternately (main, this, this, main). In the first pair the character loaded late, so only 4 steady intervals count there.
main 3686960 this change 2234121
Indirect draws GPU-side / CPU-side, per 10 s 0 / 3,301 and 0 / 3,045 3,074 / 58 and 3,094 / 59
Indirect argument reads, per 10 s 3,710 and 3,485 ms 3,706 and 3,372 ms
Presents per 10 s (steady) 61.5, 58.2 59.8, 57.0
GPU busy per present (second pair) ~153 ms ~165 ms

The change still moves the indirect draws to the GPU path, but on current main at default settings the frame rate doesn't rise: the time spent reading indirect arguments stays at about 3.4–3.7 s per 10 s, so something still waits for the dispatch that writes them; I haven't traced what. GPU busy per present is about 12 ms higher with the change in the second pair. The presents differ by 1–2 per 10 s in both pairs, within run-to-run noise.

trace_digest.py rows (second pair, steady part):

main 3686960c5   | 154-164 | 60 | 167.2 | 172.9 | 176.8 | 176.8 | 152.4 | 98 % | Driver.Packet 255.4 |
                 | 164-174 | 60 | 167.9 | 176.7 | 186.9 | 186.9 | 153.5 | 98 % | Driver.Packet 257.4 |
this 223412185   | 150-160 | 57 | 176.5 | 181.4 | 188.9 | 188.9 | 164.9 | 98 % | Driver.Packet 271.9 |
                 | 160-170 | 57 | 175.7 | 182.3 | 208.0 | 208.0 | 165.2 | 98 % | Driver.Packet 271.7 |
  • D2R on Windows: the game loads in game with this PR, but the laptop GPU has 4 GB and the scene thrashes, so I have no Windows FPS numbers.
  • I did not compare frames image by image. In play the town renders normally with this change.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none. fix(recompiler): recognize plain no-carry draw offset adds #6919, which changes the same analyzer next to these lines, has merged; this branch is rebased onto it (main 3686960).
  • Fixes: none
  • Unblocks: none (PPSA03912 Diablo II: Resurrected (PPSA03912) #5146 is already in game; this is a speedup)
  • GPUs tested: NVIDIA RTX 3080 Ti, driver 615.71.09, Linux; NVIDIA RTX A2000 Laptop GPU, driver 610.60, Windows 11; AMD Radeon 780M, RADV Mesa 26.0.8, Linux. @savvaniss also ran agc_driver_draw_offset_indices on Windows 11 with an Intel UHD 770 (32.0.101.7088) and an AMD RX 9070 XT (32.0.31041.1004): it fails on main and passes with this PR on both (comment)
  • AI-assisted: yes

AI-assisted; the runs are from my machine(s).

@savvaniss

Copy link
Copy Markdown
Contributor

Intel and AMD Windows data: agc_driver_draw_offset_indices fails on main and passes with this PR on an Intel UHD 770 and an AMD RX 9070 XT.

Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, Release, every execution test built, ANYPS5_NO_SHADER_CACHE=1, serial CTest. Intel UHD Graphics 770 (8086:a780), driver 32.0.101.7088. AMD Radeon RX 9070 XT (1002:7550), driver 32.0.31041.1004, with VkCache emptied first. This head 4784b0652 merged into main f3059421f. "Without" means this PR's test files on main f3059421f, with none of its other changes.

Tests, ×3 UHD 770 RX 9070 XT
This head: agc_driver_draw_offset_indices pass pass
Without fails fails

I also ran full CTest with this PR and the 14 other open AGC PRs I tested in the same batch merged together (#7647, #7641, #7630, #7627, #7603, #7600, #7579, #7535, #7533, #7474, #7462, #7050, #7048, #7650). Neither GPU showed a failure beyond main's own, except #7650's new test.

AI-assisted: yes (Claude Code)

@mrfelek

mrfelek commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@savvaniss thanks for the Intel and AMD runs. I've added them to the description, together with Linux/AMD: Radeon 780M (RADV, Mesa 26.0.8, Ubuntu 26.04), where all 11 related tests pass.

AI-assisted; the runs are from my machine(s).

@klosixgaming-arch

Copy link
Copy Markdown
Contributor

Tested on Windows 11, MinGW-w64 GCC 15.2.0, NVIDIA GeForce RTX 5090 (driver 617.42), Ryzen 9 9950X3D. Head 4784b0652 merged onto current main (a373e85); the merge is clean.

  • agc_driver_draw_offset_indices passes with the change.
  • On main with only the extended test file it fails: a program that copies v5 and v8 before its fetch adds: folded value observable outside the fetch add: vertex 1 instance 1; expected 1 1 (and the same message for the "fetch adds are the only readers" and "reads the instance add's carry" programs), so the new cases do fail without the fix.

Not tested: Diablo II: Resurrected (PPSA03912), so the patched SGPR not folded count in the description is unchecked here. I did not read the SGPR walk in EmbeddedVertexFetch.cpp closely enough to approve; the part worth a second pair of eyes is the list of overwrite kinds that end the walk as "not shared" (loads, SOP1/SOP2 results, s_movk_i32, v_readlane/v_readfirstlane), since a wrong entry would fold an offset the shader still reads.

AI-assisted: yes. Written with AI assistance and reviewed by me.

@mrfelek

mrfelek commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

@klosixgaming-arch thanks for the RTX 5090 run, and for pointing at the right spot. That's the list where a wrong entry would fold an offset the shader still reads. Here is why each entry is a full overwrite:

Ends the walk as "not shared" Why the old value can't survive
s_load_dword*, s_buffer_load_dword* Every destination dword is written. The register counts only inside the decoded destination width.
SOP1 / SOP2 results The sdst is written unconditionally. For a 64-bit result only the low SGPR counts, so a later read of the high one still makes it shared.
s_movk_i32 Writes the sign-extended immediate. No other SOPK opcode is on the list, so s_addk/s_mulk/s_cmovk don't count.
v_readlane_b32, v_readfirstlane_b32 Write one SGPR from a VGPR lane, whatever exec is.

Left out on purpose:

  • s_cmov_b32/b64: keep the old value when SCC is 0.
  • s_bitset0/1: change one bit and keep the rest.
  • s_movreld: the destination comes from m0, so the register written isn't known. s_movrels*/s_movreld* anywhere in the program make the register shared.

Two other points:

  • Reads come first. Each instruction is checked for a read before the overwrite test, so s_add_u32 s16, s16, 1 counts as a read (shared), not as a kill.
  • Branches: an overwrite that a pending forward branch can skip doesn't end the walk, and the read after it still counts. The last analysis case (the read after a branch that can skip the write of s16) covers that. A backward branch or an indirect jump goes back to the old whole-program check.

AI-assisted; the runs are from my machine(s).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
… the fetch add

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BywPXoZACVi8ALV4JKvuPZ
@mrfelek
mrfelek force-pushed the fix/recompiler-offset-sgpr-reuse branch from 4784b06 to 2234121 Compare October 19, 2026 14:20
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@adriadam10 adriadam10 self-assigned this Oct 19, 2026
@adriadam10
adriadam10 merged commit 1292e25 into boykopovar:main Oct 19, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Read the full diff and the new test encodings (s_load_dwordx2, SOP2, s_cbranch_scc0, v_sad_u32, s_mov_b64 exec,-1 decode as the cases describe). The overwrite list only contains full writes; reads after the kill, skipped kills and an add after the overwrite all stay shared, and backward branches fall back to the whole-program check. Third-party runs in the thread cover Windows (Intel, AMD, NVIDIA); merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants