fix(recompiler): read NumWorkgroups in every mesh shader so RADV sizes its user data #3645

Merged
alfalfa-star merged 2 commits from upstream-pr/3645 into main 2026-10-12 04:27:55 +00:00
alfalfa-star commented 2026-10-11 14:42:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-11T14:42:03Z.


What

On RADV (Mesa 26.2.4, RX 9070 XT / GFX12), a mesh draw that follows a vertex draw in the same command buffer can hang the GPU. The gfx ring times out, the ring reset fails, and the driver does a MODE1 reset with VRAM lost, which takes the desktop down with it.

Title: Kena: Bridge of Spirits (PPSA01802). Its layered volume draws run our recompiled NGG geometry program as a mesh shader, interleaved with ordinary vertex draws. They become reachable with #1077 (plus #425/#454), and every run hung the GPU 40-55 s in. It also reproduced with the Layer routing switched off, so the cause is the mesh dispatch itself.

Root cause (RADV)

radv_bind_pre_rast_shader (radv_cmd_buffer.c, Mesa 26.2.4) updates vtx_base_sgpr, vtx_emit_num and uses_drawid only when the bound shader has the AC_UD_VS_BASE_VERTEX_START_INSTANCE user SGPR slot. A mesh shader gets that slot only if it reads NumWorkgroups (3 SGPRs) or DrawIndex (1 SGPR). Our mesh shaders read neither, so the state of the previously bound vertex shader stays: 3 SGPRs (base vertex, draw id, start instance), with uses_drawid set.

radv_emit_userdata_mesh then:

  • emits SET_SH_REG with the stale count of 3;
  • writes only the draw id (1 value), since this mesh shader has no grid size;
  • emits DISPATCH_MESH_DIRECT, which now lands inside the register payload.

The CP takes the dispatch header and X as register values and then parses Y/Z/initiator as packet headers.

Evidence, from three hangs:

  • RADV_DEBUG=hang trace: the last reached trace point is followed by
    c0037600 SET_SH_REG          (count 3)
    0000008f                     SPI_SHADER_USER_DATA_GS_3 <- 0
    c0034e00                     SPI_SHADER_USER_DATA_GS_4 <- 0xc0034e00   <- the DISPATCH_MESH_DIRECT header
    00000001                     SPI_SHADER_USER_DATA_GS_5 <- 1
    00000007 / 00000001 / 00000002   "Unknown packet type 0"
    
    and the preceding vertex draw wrote GS_3..GS_5 with 3 values.
  • The amdgpu devcoredump agrees:
    • GRBM_STATUS shows only CP_BUSY / GUI_ACTIVE, with SPI, GE, PA, SC, CB and DB all idle, so no shader is stuck;
    • CP_PFP_HEADER_DUMP lists 0x00000007 and 0x00000002 as parsed "headers".
  • A minimal mesh draw with nothing bound before it does not hang.

Fix (workaround)

Every mesh shader the recompiler emits now loads NumWorkgroups.x and declares zero vertices when it is 0. No dispatch produces that, so behaviour is unchanged, and the read gives the shader the grid-size slot. RADV then updates its state at bind, announces 3 values and writes x, y, z. gl_NumWorkGroups was already declared for mesh shaders with device-memory LDS; it is now declared for all of them.

TechnicalDebt records the workaround and when it can be dropped (once a fixed Mesa is the minimum). I'll report the RADV bug to Mesa separately.

Tested

Linux (Arch), GCC, Release, RX 9070 XT (RADV GFX1201, Mesa 26.2.4), on main a11c8f7f.

  • agc_driver_mesh_tests now requires every compiled mesh program to declare and load BuiltIn NumWorkgroups:
    • with main's emitter it fails: the mesh program does not read NumWorkgroups, so RADV can keep a previous vertex pipeline's user SGPR count for its dispatch;
    • with the fix it passes, and its draws still render correctly.
  • Full ctest: 831/832. The one failure, guest_video_out_output_mode, passes on its own; it flakes under parallel load and is unrelated.
  • Kena (PPSA01802) with the stack that hung (#425 + #454 + #1077) plus this fix ran 4 minutes on the RX 9070 XT with no ring timeout. Without the fix it hung at 40-55 s in each of three runs.
  • tools/check_conventions.py passes.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4819861478 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3645). Originally posted by [alfalfa-star](https://github.com/alfalfa-star) on 2026-10-11T14:42:03Z. --- ### What On RADV (Mesa 26.2.4, RX 9070 XT / GFX12), a mesh draw that follows a vertex draw in the same command buffer can **hang the GPU**. The gfx ring times out, the ring reset fails, and the driver does a MODE1 reset with VRAM lost, which takes the desktop down with it. **Title:** Kena: Bridge of Spirits (PPSA01802). Its layered volume draws run our recompiled NGG geometry program as a mesh shader, interleaved with ordinary vertex draws. They become reachable with #1077 (plus #425/#454), and every run hung the GPU 40-55 s in. It also reproduced with the Layer routing switched off, so the cause is the mesh dispatch itself. ### Root cause (RADV) `radv_bind_pre_rast_shader` (radv_cmd_buffer.c, Mesa 26.2.4) updates `vtx_base_sgpr`, `vtx_emit_num` and `uses_drawid` only when the bound shader has the `AC_UD_VS_BASE_VERTEX_START_INSTANCE` user SGPR slot. A mesh shader gets that slot only if it reads `NumWorkgroups` (3 SGPRs) or `DrawIndex` (1 SGPR). Our mesh shaders read neither, so the state of the previously bound **vertex** shader stays: 3 SGPRs (base vertex, draw id, start instance), with `uses_drawid` set. `radv_emit_userdata_mesh` then: - emits `SET_SH_REG` with the stale count of 3; - writes only the draw id (1 value), since this mesh shader has no grid size; - emits `DISPATCH_MESH_DIRECT`, which now lands inside the register payload. The CP takes the dispatch header and X as register values and then parses Y/Z/initiator as packet headers. Evidence, from three hangs: - `RADV_DEBUG=hang` trace: the last reached trace point is followed by ```text c0037600 SET_SH_REG (count 3) 0000008f SPI_SHADER_USER_DATA_GS_3 <- 0 c0034e00 SPI_SHADER_USER_DATA_GS_4 <- 0xc0034e00 <- the DISPATCH_MESH_DIRECT header 00000001 SPI_SHADER_USER_DATA_GS_5 <- 1 00000007 / 00000001 / 00000002 "Unknown packet type 0" ``` and the preceding vertex draw wrote GS_3..GS_5 with 3 values. - The amdgpu devcoredump agrees: - `GRBM_STATUS` shows only CP_BUSY / GUI_ACTIVE, with SPI, GE, PA, SC, CB and DB all idle, so no shader is stuck; - `CP_PFP_HEADER_DUMP` lists `0x00000007` and `0x00000002` as parsed "headers". - A minimal mesh draw with nothing bound before it does not hang. ### Fix (workaround) Every mesh shader the recompiler emits now loads `NumWorkgroups.x` and declares zero vertices when it is 0. No dispatch produces that, so behaviour is unchanged, and the read gives the shader the grid-size slot. RADV then updates its state at bind, announces 3 values and writes x, y, z. `gl_NumWorkGroups` was already declared for mesh shaders with device-memory LDS; it is now declared for all of them. TechnicalDebt records the workaround and when it can be dropped (once a fixed Mesa is the minimum). I'll report the RADV bug to Mesa separately. ### Tested Linux (Arch), GCC, Release, RX 9070 XT (RADV GFX1201, Mesa 26.2.4), on main a11c8f7f. - `agc_driver_mesh_tests` now requires every compiled mesh program to declare **and load** `BuiltIn NumWorkgroups`: - with main's emitter it fails: `the mesh program does not read NumWorkgroups, so RADV can keep a previous vertex pipeline's user SGPR count for its dispatch`; - with the fix it passes, and its draws still render correctly. - Full `ctest`: 831/832. The one failure, `guest_video_out_output_mode`, passes on its own; it flakes under parallel load and is unrelated. - Kena (PPSA01802) with the stack that hung (#425 + #454 + #1077) plus this fix ran 4 minutes on the RX 9070 XT with no ring timeout. Without the fix it hung at 40-55 s in each of three runs. - `tools/check_conventions.py` passes. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
blawesom commented 2026-10-11 15:19:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-11T15:19:45Z.


Independent pre-review on Linux (full CI runs only happen on manual trigger, so recording one): CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON.

  • Builds; tools/check_conventions.py --base origin/main passes.
  • Full suite: 832/832 passed (all tests including the Vulkan suites on AMD RX 9070 XT / RADV).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110517462 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3645#issuecomment-6110517462). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-11T15:19:45Z. --- Independent pre-review on **Linux** (full CI runs only happen on manual trigger, so recording one): CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`. - Builds; `tools/check_conventions.py --base origin/main` passes. - Full suite: **832/832 passed** (all tests including the Vulkan suites on AMD RX 9070 XT / RADV).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!3645
No description provided.