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

Open
opened 2026-11-26 21:21:06 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2397
Author: @DadaCap
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: mesh-prim-index-clamp


📝 Commits (1)

  • d48d8b3 rdna2spirv: keep mesh primitive indices below the vertex count

📊 Changes

4 files changed (+139 additions, -23 deletions)

View changed files

📝 docs/design.md (+1 -1)
➕ tests/semantic/fixtures/ms_prim_null_clamp.asm (+37 -0)
📝 tests/semantic/test_ms_prim_export.py (+72 -9)
📝 tools/rdna2spirv.py (+29 -13)

📄 Description

Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7201444406

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2397 **Author:** [@DadaCap](https://github.com/DadaCap) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `mesh-prim-index-clamp` --- ### 📝 Commits (1) - [`d48d8b3`](https://github.com/ubiquitarianism/leonida/commit/d48d8b3b0aafba90a26702cee9efe9fb912ee50c) rdna2spirv: keep mesh primitive indices below the vertex count ### 📊 Changes **4 files changed** (+139 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) ➕ `tests/semantic/fixtures/ms_prim_null_clamp.asm` (+37 -0) 📝 `tests/semantic/test_ms_prim_export.py` (+72 -9) 📝 `tools/rdna2spirv.py` (+29 -13) </details> ### 📄 Description Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7201444406 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 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2398
No description provided.