rdna2spirv: keep mesh primitive indices below the vertex count (SHADER-MESH-PRIM-INDEX-CLAMP) #2397

Open
github-bot wants to merge 1 commit from github-bot/gh-2397 into main AGit
Owner

@⁠DadaCap on GitHub · 2026-11-26 19:18 UTC · original
DadaCap:mesh-prim-index-clamp → main · +139 −23 · 4 files · 1 commits · open

Claimed in the coordination issue as: #1608

Depends on: none.

What and why

A GFX10 mesh shader exports each primitive with exp prim: three 10-bit index fields and bit 31, "null primitive (skip)". The NGG epilogues compare each index field with the vertex count. When a field is at or past it, they set bit 31 and leave the fields unchanged, so a null primitive can carry indices up to 511. The hardware skips such a primitive. The translator wrote the 9-bit fields straight into PrimitiveTriangleIndicesEXT and set only CullPrimitiveEXT, but VK_EXT_mesh_shader leaves an index at or past the vertex count undefined even on a culled primitive. Mesa's own mesh lowering clamps the indices (ac_nir_lower_ngg.c line 4112, Mesa 22.3). docs/design.md already listed this as Open in the mesh primitive export row. It is a suspected cause of device losses in mesh draws on NVIDIA, which has no null-primitive path like AMD's. That cause is not confirmed.

The rule

In both mesh epilogues, the MeshEXT finale in assemble() and mesh_compute_epilogue() for --mesh-compute, through one shared helper (ms_prim_indices, ms_prim_count in tools/rdna2spirv.py):

  • index k = null ? 0 : umin(field_k, vertex count - 1). The 9-bit fields still leave out the edge flags.
  • primitive count = vertex count == 0 ? 0 : min(count, PSV0 max), so vertex count - 1 never wraps and no primitive points into an empty vertex array.
  • CullPrimitiveEXT (and the --mesh-compute slot's culled word) is unchanged. Normal primitives with in-range indices are unchanged.

No other path writes primitive indices (PrimitiveLineIndicesEXT or point indices are never emitted). The row in the docs/design.md contract table now states the rule and how it was confirmed, in place of the Open note.

Tested

  • tests/semantic/test_ms_prim_export.py gains MeshNullPrimitiveClamp with a new hand-written fixture, fixtures/ms_prim_null_clamp.asm (no game data). It runs 128 threads in two wave64 waves at vertex counts 0, 10, 100 and 128. Its null primitives carry fields 300, 511, 0x3FF (bit 9 set), and the vertex count itself; its normal primitives have in-range indices, with and without edge flags. On both paths it checks that every written index is below the vertex count, that null primitives write (0, 0, 0), that normal primitives and cull flags are unchanged, and that the counts become (0, 0) with no vertex. The existing fixture's null primitives now expect (0, 0, 0).
  • Before/after: on main the new assertions fail (raw fields such as (300, 511, 511) and (V, 0, 1) written, and counts (0, 128) at vertex count 0). With the change, test_ms_*.py and test_mesh*.py all pass.
  • Full translator suite: python -I scripts/run_semantic_tests.py tests/semantic: 500 files, 6,375 tests, 0 files failed (122 s, 24 jobs, Windows 11).
  • spirv-val --target-env vulkan1.2 accepts the translated fixture as MeshEXT and with --mesh-compute, plain and with --wave64-emulate.
  • python -m compileall -q tools tests and python -I scripts/check_no_game_data.py on the changed files: clean.

GPU confirmation in the retail game: pending. The change is checked on the CPU only (semantic interpreter); no GPU was used.

AI-assisted: yes (Claude Code, Opus 5.5)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr

**@⁠DadaCap** on GitHub · 2026-11-26 19:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2397) `DadaCap:mesh-prim-index-clamp` → `main` · +139 −23 · 4 files · 1 commits · open Claimed in the coordination issue as: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 Depends on: none. ### What and why A GFX10 mesh shader exports each primitive with `exp prim`: three 10-bit index fields and bit 31, "null primitive (skip)". The NGG epilogues compare each index field with the vertex count. When a field is at or past it, they set bit 31 and leave the fields unchanged, so a null primitive can carry indices up to 511. The hardware skips such a primitive. The translator wrote the 9-bit fields straight into `PrimitiveTriangleIndicesEXT` and set only `CullPrimitiveEXT`, but `VK_EXT_mesh_shader` leaves an index at or past the vertex count undefined even on a culled primitive. Mesa's own mesh lowering clamps the indices (`ac_nir_lower_ngg.c` line 4112, Mesa 22.3). `docs/design.md` already listed this as Open in the mesh primitive export row. It is a suspected cause of device losses in mesh draws on NVIDIA, which has no null-primitive path like AMD's. That cause is not confirmed. ### The rule In both mesh epilogues, the `MeshEXT` finale in `assemble()` and `mesh_compute_epilogue()` for `--mesh-compute`, through one shared helper (`ms_prim_indices`, `ms_prim_count` in `tools/rdna2spirv.py`): - index k = `null ? 0 : umin(field_k, vertex count - 1)`. The 9-bit fields still leave out the edge flags. - primitive count = `vertex count == 0 ? 0 : min(count, PSV0 max)`, so `vertex count - 1` never wraps and no primitive points into an empty vertex array. - `CullPrimitiveEXT` (and the `--mesh-compute` slot's culled word) is unchanged. Normal primitives with in-range indices are unchanged. No other path writes primitive indices (`PrimitiveLineIndicesEXT` or point indices are never emitted). The row in the `docs/design.md` contract table now states the rule and how it was confirmed, in place of the Open note. ### Tested - `tests/semantic/test_ms_prim_export.py` gains `MeshNullPrimitiveClamp` with a new hand-written fixture, `fixtures/ms_prim_null_clamp.asm` (no game data). It runs 128 threads in two wave64 waves at vertex counts 0, 10, 100 and 128. Its null primitives carry fields 300, 511, 0x3FF (bit 9 set), and the vertex count itself; its normal primitives have in-range indices, with and without edge flags. On both paths it checks that every written index is below the vertex count, that null primitives write (0, 0, 0), that normal primitives and cull flags are unchanged, and that the counts become (0, 0) with no vertex. The existing fixture's null primitives now expect (0, 0, 0). - Before/after: on `main` the new assertions fail (raw fields such as (300, 511, 511) and (V, 0, 1) written, and counts (0, 128) at vertex count 0). With the change, `test_ms_*.py` and `test_mesh*.py` all pass. - Full translator suite: `python -I scripts/run_semantic_tests.py tests/semantic`: 500 files, 6,375 tests, 0 files failed (122 s, 24 jobs, Windows 11). - `spirv-val --target-env vulkan1.2` accepts the translated fixture as `MeshEXT` and with `--mesh-compute`, plain and with `--wave64-emulate`. - `python -m compileall -q tools tests` and `python -I scripts/check_no_game_data.py` on the changed files: clean. GPU confirmation in the retail game: pending. The change is checked on the CPU only (semantic interpreter); no GPU was used. AI-assisted: yes (Claude Code, Opus 5.5) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
A GFX10 mesh epilogue marks a primitive null (bit 31) when an index field
is at or past the vertex count and keeps the field, so a null primitive can
carry indices up to 511. Vulkan leaves such an index undefined even on a
culled primitive. Both mesh finales (MeshEXT and --mesh-compute) now write
index k as null ? 0 : umin(field_k, vertex count - 1), as Mesa clamps them,
and a primitive count of 0 when the vertex count is 0. CullPrimitiveEXT is
unchanged.

Test: test_ms_prim_export.py gains MeshNullPrimitiveClamp with a
hand-written 128-thread wave64 fixture at vertex counts 0, 10, 100 and 128;
it fails on the old translator. docs/design.md moves the Open note into the
mesh primitive export row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:36:02 +00:00
Author
Owner

@⁠wavehs on GitHub · 2026-11-26 19:36 UTC · original

Independent check on macOS (not the author's Windows run), applying the PR diff to a clean main 2d43417d4 worktree. The diff applies cleanly and the code reads correctly: ms_prim_indices shifts field k by 10k, masks 0x1FF, takes UMin(field, vertexCount - 1) and selects 0 for a null primitive; ms_prim_count zeroes the count at vertex count 0. At vertex count 0 %mvl wraps, but the count is 0 so no primitive store runs. Normal in-range primitives and the cull word are unchanged.

Negative control (main translator, PR's test and fixture only): test_ms_prim_export.py -> Ran 5 tests, FAILED (failures=390), first difference - (67, 68, 69) / + (0, 0, 0).

With the PR: Ran 5 tests in 0.947s, OK.

Full translator suite with the PR (macOS 27.2.1 arm64, CPython 3.15.2, 12 jobs):

500 files, 6375 tests, 0 files failed in 70 s

python3 -m compileall -q on the changed files and python3 -I scripts/check_no_game_data.py on all four changed files (including the new fixture) are clean. I did not run the retail GPU A/B, so the NVIDIA device-loss cause remains unconfirmed as the PR says.

AI-assisted (verification only; no game files, no GPU).

**@⁠wavehs** on GitHub · 2026-11-26 19:36 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2397#issuecomment-7201773589) Independent check on macOS (not the author's Windows run), applying the PR diff to a clean `main` `2d43417d4` worktree. The diff applies cleanly and the code reads correctly: `ms_prim_indices` shifts field k by `10k`, masks `0x1FF`, takes `UMin(field, vertexCount - 1)` and selects 0 for a null primitive; `ms_prim_count` zeroes the count at vertex count 0. At vertex count 0 `%mvl` wraps, but the count is 0 so no primitive store runs. Normal in-range primitives and the cull word are unchanged. **Negative control** (main translator, PR's test and fixture only): `test_ms_prim_export.py` -> `Ran 5 tests`, `FAILED (failures=390)`, first difference `- (67, 68, 69) / + (0, 0, 0)`. **With the PR**: `Ran 5 tests in 0.947s`, `OK`. **Full translator suite** with the PR (macOS 27.2.1 arm64, CPython 3.15.2, 12 jobs): ```text 500 files, 6375 tests, 0 files failed in 70 s ``` `python3 -m compileall -q` on the changed files and `python3 -I scripts/check_no_game_data.py` on all four changed files (including the new fixture) are clean. I did not run the retail GPU A/B, so the NVIDIA device-loss cause remains unconfirmed as the PR says. AI-assisted (verification only; no game files, no GPU).
Author
Owner

@⁠lixonik on GitHub · 2026-11-26 20:43 UTC · original · reviewed

On Windows 11, all five test_ms_prim_export tests passed. I also adapted the synthetic fixture to clear the null bit while keeping out-of-range indices. At a vertex count of 10, lanes 1 and 65 produced (9, 9, 9) and lanes 2 and 66 produced (9, 0, 1), with the cull flag clear. Both MeshEXT and --mesh-compute passed this check in the interpreter.

AI-assisted.

**@⁠lixonik** on GitHub · 2026-11-26 20:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2397#pullrequestreview-6080301456) · **reviewed** On Windows 11, all five `test_ms_prim_export` tests passed. I also adapted the synthetic fixture to clear the null bit while keeping out-of-range indices. At a vertex count of 10, lanes 1 and 65 produced `(9, 9, 9)` and lanes 2 and 66 produced `(9, 0, 1)`, with the cull flag clear. Both `MeshEXT` and `--mesh-compute` passed this check in the interpreter. AI-assisted.
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-26 20:48 UTC · original

Both shader paths passed synthetic GPU readbacks on PR head d48d8b3b0a, Windows 11, RTX 4090, NVIDIA 616.64, using --wave64-emulate on 32-wide subgroups.

Native mesh draws. I varied the final primitive export in ms_tri.asm and drew through VK_EXT_mesh_shader into a 64×64 RGBA32F target. With three vertices, the normal triangle (0,1,2) and the two non-null inputs (0,1,3) and (0,1,511) each produced the expected gradient at all 4,096 pixels. The last two require the third index to clamp to 2. Setting the null bit left all pixels at the clear value. Requesting one primitive with zero vertices also left all pixels clear. All five modules passed Vulkan 1.2 SPIR-V validation, and the Vulkan loader confirmed the Khronos validation layer was active for the draws.

Compute output. I adapted vk_compute_test locally to put a guarded output slot's device address in push words 36–37. One 128-thread workgroup wrote its slot, which I read as data. I checked the counts, vertex positions, three indices and cull word for every emitted primitive, as well as unwritten bytes and both guards.

  • Eleven vertex counts (0, 1, 2, 9, 10, 63, 64, 65, 100, 127, 128), each with the fixture's null bits set and cleared, plus two in-range controls: 24/24 whole-buffer matches on this PR.
  • Same inputs with parent translator 2d43417d49: 2/24 matched the new rule, both in-range controls. All 24 matched the old rule. The parent comparison used compute readback only.
  • The Khronos validation layer reported zero errors in the 48 compute runs.

This verifies the index and primitive-count rules on hardware. Whether the change resolves device loss in a game run remains open.

**@⁠ecox982745** on GitHub · 2026-11-26 20:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2397#issuecomment-7203098879) Both shader paths passed synthetic GPU readbacks on PR head `d48d8b3b0a`, Windows 11, RTX 4090, NVIDIA 616.64, using `--wave64-emulate` on 32-wide subgroups. **Native mesh draws.** I varied the final primitive export in `ms_tri.asm` and drew through `VK_EXT_mesh_shader` into a 64×64 RGBA32F target. With three vertices, the normal triangle `(0,1,2)` and the two non-null inputs `(0,1,3)` and `(0,1,511)` each produced the expected gradient at all 4,096 pixels. The last two require the third index to clamp to 2. Setting the null bit left all pixels at the clear value. Requesting one primitive with zero vertices also left all pixels clear. All five modules passed Vulkan 1.2 SPIR-V validation, and the Vulkan loader confirmed the Khronos validation layer was active for the draws. **Compute output.** I adapted `vk_compute_test` locally to put a guarded output slot's device address in push words 36–37. One 128-thread workgroup wrote its slot, which I read as data. I checked the counts, vertex positions, three indices and cull word for every emitted primitive, as well as unwritten bytes and both guards. - Eleven vertex counts (0, 1, 2, 9, 10, 63, 64, 65, 100, 127, 128), each with the fixture's null bits set and cleared, plus two in-range controls: **24/24** whole-buffer matches on this PR. - Same inputs with parent translator `2d43417d49`: **2/24** matched the new rule, both in-range controls. All 24 matched the old rule. The parent comparison used compute readback only. - The Khronos validation layer reported zero errors in the 48 compute runs. This verifies the index and primitive-count rules on hardware. Whether the change resolves device loss in a game run remains open.
Author
Owner

@⁠OreoFR on GitHub · 2026-11-27 03:05 UTC · original

Retail data from profile M (RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11, TdrDelay=60), with the mesh programs translated with this PR. The rest of the shader pack is unchanged.

  • Without this PR, same build: device lost in the mesh section of the load at about 304-333 s (Aftermath Timeout, a mesh shader active).
  • With it, 4 runs: no device loss in the mesh section in any of them. One run stopped earlier on an unrelated freeze (fixed in #2418). One got through the mesh section and on to 740 s, reaching the world phase (more than 63k indirect draws emitted). MEASURED (n=4).
  • Two device losses happened later in other runs, with a compute program active (Timeout), not a mesh shader.

So on this driver the mesh-section loss looks gone with this PR. AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠OreoFR** on GitHub · 2026-11-27 03:05 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2397#issuecomment-7208690683) Retail data from profile M (RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11, `TdrDelay=60`), with the mesh programs translated with this PR. The rest of the shader pack is unchanged. - Without this PR, same build: device lost in the mesh section of the load at about 304-333 s (Aftermath Timeout, a mesh shader active). - With it, 4 runs: no device loss in the mesh section in any of them. One run stopped earlier on an unrelated freeze (fixed in #2418). One got through the mesh section and on to 740 s, reaching the world phase (more than 63k indirect draws emitted). `MEASURED (n=4)`. - Two device losses happened later in other runs, with a compute program active (Timeout), not a mesh shader. So on this driver the mesh-section loss looks gone with this PR. AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠OreoFR on GitHub · 2026-11-27 03:05 UTC · original

CORRECTION to my comment above: "4 runs" was wrong. 13 runs used the mesh programs translated with this PR, all on the same machine.

  • No device loss with a mesh shader active in any of them.
  • 4 device losses with Aftermath Timeout: 3 with the same compute program active (at 327, 337 and 474 s), and 1 with no warp records in the dump (306 s).
  • 9 runs had no device loss; several of those stopped on the freeze that #2418 fixes, or on a memory guard.
  • Before this PR, same machine: the losses at 304-333 s had a mesh shader active.

The conclusion stands: no mesh-shader hang with this PR. What remains is a compute-program timeout, a separate issue. MEASURED (n=13). AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠OreoFR** on GitHub · 2026-11-27 03:05 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2397#issuecomment-7208695994) CORRECTION to my comment above: "4 runs" was wrong. 13 runs used the mesh programs translated with this PR, all on the same machine. - **No device loss with a mesh shader active in any of them.** - 4 device losses with Aftermath Timeout: 3 with the same compute program active (at 327, 337 and 474 s), and 1 with no warp records in the dump (306 s). - 9 runs had no device loss; several of those stopped on the freeze that #2418 fixes, or on a memory guard. - Before this PR, same machine: the losses at 304-333 s had a mesh shader active. The conclusion stands: no mesh-shader hang with this PR. What remains is a compute-program timeout, a separate issue. `MEASURED (n=13)`. AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-27 04:43 UTC · original · approved these changes

Approving d48d8b3b0.

Read ms_prim_indices/ms_prim_count in both finales. A null primitive writes index 0, the others umin(field, vertex count - 1), so no written index reaches past the vertex count, which VK_EXT_mesh_shader leaves undefined even on a culled primitive. With a vertex count of 0 the %mvl subtraction wraps, but ms_prim_count makes the primitive count 0 then, so no primitive is written with it. --mesh-compute still stores the null bit as the fourth word, and CullPrimitiveEXT is untouched.

Merged onto main d1dff7949 together with #2416, #2355, #2357, #2360 and #2366 (no conflicts), on Linux with Python 3.13: test_ms_prim_export.py 5 passed (1,168 subtests); the full translator suite (run_semantic_tests.py tests/semantic) has only the 8 failures main has here (7 in test_fspx_dot4_op_sel_disasm, test_int32_to_f64), and tests/test_translate.py only main's ps_sdwa_f16_const.asm (an llvm-mc version difference in this environment). AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠tiguan26** on GitHub · 2026-11-27 04:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2397#pullrequestreview-6083580929) · **approved these changes** Approving d48d8b3b0. Read `ms_prim_indices`/`ms_prim_count` in both finales. A null primitive writes index 0, the others `umin(field, vertex count - 1)`, so no written index reaches past the vertex count, which `VK_EXT_mesh_shader` leaves undefined even on a culled primitive. With a vertex count of 0 the `%mvl` subtraction wraps, but `ms_prim_count` makes the primitive count 0 then, so no primitive is written with it. `--mesh-compute` still stores the null bit as the fourth word, and `CullPrimitiveEXT` is untouched. Merged onto `main` `d1dff7949` together with #2416, #2355, #2357, #2360 and #2366 (no conflicts), on Linux with Python 3.13: `test_ms_prim_export.py` 5 passed (1,168 subtests); the full translator suite (`run_semantic_tests.py tests/semantic`) has only the 8 failures `main` has here (7 in `test_fspx_dot4_op_sel_disasm`, `test_int32_to_f64`), and `tests/test_translate.py` only `main`'s `ps_sdwa_f16_const.asm` (an llvm-mc version difference in this environment). AI-assisted (Claude Code, Claude Opus 5.5).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2397/head:github-bot/gh-2397'
git switch 'github-bot/gh-2397'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2397'
git switch 'github-bot/gh-2397'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2397'
git switch 'github-bot/gh-2397'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2397'
git switch 'main'
git merge --squash 'github-bot/gh-2397'
git switch 'main'
git merge --ff-only 'github-bot/gh-2397'
git switch 'main'
git merge 'github-bot/gh-2397'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.