Skip to content

tools: spirv_gs_to_compute.py runs a translated GS as a compute capture pass (SHADER-159) - #1331

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
undeadhuman37:shader-159-gs-compute-capture
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
undeadhuman37:shader-159-gs-compute-capture

Conversation

@undeadhuman37

Copy link
Copy Markdown
Contributor

What and why

Apple GPUs have no geometry stage, so gs_tri (and the 195 $GS_VoxelizeGeneric GS) can't run there. @fruvs showed a compute capture pass followed by a draw works for gs_tri. This adds that lowering as a tool:

  • New tools/spirv_gs_to_compute.py: it rewrites a translated Geometry module into a GLCompute module (64x1x1), one invocation per input primitive. The per-vertex inputs (Position, Location N) are read from an input buffer (set 1, binding 0: primitive count, then vec4 data). Each OpEmitVertex copies the outputs to a record buffer (binding 1). A meta buffer (binding 2) holds each primitive's vertex count and strip ends. PrimitiveId reads give the primitive, and InvocationId gives 0. Push constants and set 0 are untouched. --layout writes the slot order and sizes. The tool refuses, with a message, what it doesn't handle: Invocations > 1, non-vec4 varyings, whole-array input loads.
  • tests/test_spirv_gs_to_compute.py: translates gs_tri.asm, lowers it, and checks the layout, the module (no GS opcodes or outputs left, 6 record stores) and spirv-val, plus the command line.
  • docs/design.md: the "Geometry shaders" row says what it does and what's not done.

Tested

Apple A18 Pro, macOS 26.6.2, SPIRV-Cross 1.4.363.0, system Metal compiler (MSL 3.1).

  • gs_tri lowered, validated (spirv-val), converted with SPIRV-Cross and run natively:
    • 65 random triangles (two workgroups, so the bounds check runs): 195/195 records exact. gs_tri copies 6 dwords: the position, then the attribute's xy. All 65 primitives have count 3 and the strip end after vertex 2.
    • A fullscreen triangle captured, then drawn from the records with a plain Metal vertex/fragment pair: 4096/4096 pixels match the gradient that run_gpu_tests.py checks for gs_tri.
  • pytest tests/test_spirv_gs_to_compute.py: 4 passed.

Not done here: wiring this into a runtime draw path, GS with cross-lane operations or more than one invocation, and the game's GS (no game files used).

Checklist

  • No game data, keys or decryption, including in logs or reports quoted here.
  • No translator rule changed. The tool is described in docs/design.md.

AI-assisted (Claude Code): it wrote the tool and the tests, and ran everything above on the A18 Pro.

🤖 Generated with Claude Code

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Reviewed d1474ee: 2 findings

  1. tools/spirv_gs_to_compute.py:213-220: the primitive-count check splits main's entry block, so the rest of that block now sits under %gsc_body, but an OpPhi that names the entry label as its predecessor isn't updated. The translator does emit those: its block_label is %entry until the first new label (rdna2spirv.py:210, used by the guarded-load OpPhi at ~1689). Hand-built GS (vulkan1.2, valid under spirv-val): entry block OpSelectionMerge %m / OpBranchConditional %c %t %m, then %m: %x = OpPhi %uint %u1 %t %u0 %entry, then 3x emit. After the rewrite spirv-val fails: OpPhi's incoming basic block <id> '47' is not a predecessor of <id> '60'. Renaming the entry label in OpPhi operands to %gsc_body would fix it.
  2. tests/test_spirv_gs_to_compute.py:20-61: the tests use make_shader without @requires_tools, which the other make_shader tests (e.g. test_mesh_compute.py) have. Without llvm-mc they error instead of skipping: here it's 1 failed, 3 errors (TypeError: unsupported operand type(s) for +: 'NoneType' and 'list', conftest.py:166).

Ran: Windows 11, Python 3.13.7, SPIRV-Tools from Vulkan SDK 1.4.363.0, no llvm-mc. pytest tests/test_spirv_gs_to_compute.py: 1 failed, 3 errors (finding 2). python -m compileall -q make_game.py tools tests: OK. Hand-built SPIR-V through the CLI: a straight-line triangle GS (Position + Location 1, 3 emits) lowers and passes spirv-val --target-env vulkan1.2; the OpPhi variant above fails.

Review was AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested d1474ee on Windows 11 with llvm-mc 23.1.6 and the SPIR-V tools: pytest tests/test_spirv_gs_to_compute.py: 4 passed. Without the new tool, all 4 fail. On a Linux box without llvm-mc, the shared make_shader fixture in tests/conftest.py raises TypeError: LLVM_MC is None + list instead of skipping. That's a pre-existing fixture issue, not this PR's, but these tests will error rather than skip there.

@bobcatchris

Copy link
Copy Markdown
Contributor

Heads-up on the red windows job (run 45011536967): it is not your change. The CPU runtime tests step fails only on

49/67 Test #49: d3d12_predicated_copy ...***Failed
98% tests passed, 1 tests failed out of 66

which is the failure that made main red at c0ac5fd and was fixed on main by merged #1314 (675e35b). Your head d1474ee1 predates that commit, so rebasing onto current main (f85582e) or re-running the job should turn it green; main is green on all four workflows at f85582e.

All other checks pass, including both semantic-tests legs and translator-tests.

undeadhuman37 added a commit to undeadhuman37/Leonida that referenced this pull request Nov 22, 2026
… without llvm-mc

Review of ItsNotRexed#1331 (kotorvr):
- the primitive-count check splits main's entry block, so an OpPhi naming the
  entry label as a predecessor now names %gsc_body. New test with a
  hand-built GS that branches in its entry block (spirv-val failed before).
- the tests that translate gs_tri.asm are marked @requires_tools, like the
  other make_shader tests, so they skip instead of erroring without llvm-mc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@undeadhuman37

Copy link
Copy Markdown
Contributor Author

Thanks @kotorvr, both findings were right. Both are fixed in e420d60, rebased on main f85582e:

  1. OpPhi from the split entry block: an OpPhi that names the entry label as a predecessor now names %gsc_body. The new test test_phi_from_entry_block_names_the_split_block uses your hand-built shape (the entry block branches, OpPhi ... %u0 %entry, three emits). The tool before the fix fails spirv-val on it (OpPhi's incoming basic block ... is not a predecessor), and the fixed one passes.
  2. Tests without llvm-mc: the tests that translate gs_tri.asm are now @requires_tools. With LLVM_MC pointing nowhere: 1 passed, 4 skipped.

Rechecked on the Apple A18 Pro: pytest tests/test_spirv_gs_to_compute.py 5 passed. gs_tri lowered and run natively gives 195/195 records exact for 65 random triangles, and 4096/4096 pixels for the drawn capture.

AI-assisted. No game files.

@undeadhuman37
undeadhuman37 force-pushed the shader-159-gs-compute-capture branch from d1474ee to e420d60 Compare November 22, 2026 10:47
ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested e420d60 on RTX 4090 (driver 621.14, 32-wide subgroups), Windows 11 26200: pass

  • RelWithDebInfo Ninja build of runtime/ (MSVC, Vulkan SDK 1.4.363.0); ctest: 67/67
  • python -I scripts/run_gpu_tests.py: 105/133 passed, 28 skipped (wave64 tests skip without emulation); with LEONIDA_W64_EMULATE=1: 133/133 passed. main f85582e on the same machine: 105/133 passed, 28 skipped and 133/133 passed
  • python -m unittest discover -s tests/semantic: 1178 OK (main: 1178)
  • python -m pytest tests --ignore=tests/semantic: 3370 passed, 33 skipped (main: 3365 passed, 33 skipped)

No game files used. AI-assisted run (Claude Code).

@bobcatchris

Copy link
Copy Markdown
Contributor

M2 data point for the CPU tests (no overlap with the A18 Pro results in the PR body).

Apple M2 (Mac mini, Mac14,3), macOS 26.4 (25E246), MoltenVK 1.4.2, Homebrew llvm-mc 23.1.6, Python 3.13.18 in an isolated venv with pytest 9.2.0. Fetched head e420d60 into a separate read-only worktree; no edits and no pushes.

pytest tests/test_spirv_gs_to_compute.py on that head: 5 passed in 0.38s.

Not run: the PR's native Metal validation (65 random triangles and the fullscreen-triangle draw). This CLT-only Mac has no metal/metalc/metallib utility and no spirv-cross on PATH, so I cannot convert the lowered module to MSL or build a Metal harness here. That is a tooling limitation, not a failure of the patch.

No game files.
AI-assisted.

… without llvm-mc

Review of ItsNotRexed#1331 (kotorvr):
- the primitive-count check splits main's entry block, so an OpPhi naming the
  entry label as a predecessor now names %gsc_body. New test with a
  hand-built GS that branches in its entry block (spirv-val failed before).
- the tests that translate gs_tri.asm are marked @requires_tools, like the
  other make_shader tests, so they skip instead of erroring without llvm-mc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@undeadhuman37
undeadhuman37 force-pushed the shader-159-gs-compute-capture branch from e420d60 to 8a79601 Compare November 22, 2026 11:36
@ItsNotRexed
ItsNotRexed merged commit 11b8937 into ItsNotRexed:main Nov 22, 2026
10 of 11 checks passed
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.

5 participants