Skip to content

spirv_gs_to_compute: guard OpEmitVertex on n < OutputVertices (SHADER-617) - #3319

Open
timelery wants to merge 1 commit into
ItsNotRexed:mainfrom
timelery:shader-617-gs-emit-bounds
Open

timelery wants to merge 1 commit into
ItsNotRexed:mainfrom
timelery:shader-617-gs-emit-bounds

Conversation

@timelery

Copy link
Copy Markdown
Contributor

Fixes SHADER-617 (finding by @Vonsek; claimed in #3232 comment).

Problem

In tools/spirv_gs_to_compute.py:270-294, rewrite() emitted OpEmitVertex stores to %gsc_rec and %gsc_meta and incremented %gsc_n without checking n < max_verts (OutputVertices). A geometry shader emitting more than OutputVertices vertices overflowed primitive p's slots and corrupted primitive p + 1's vertex records and meta count slot (and left OpEndPrimitive / OpReturn using the unclamped %gsc_n).

Changes

  • tools/spirv_gs_to_compute.py (rewrite):
    • Guard OpEmitVertex's record/meta SSBO stores and %gsc_n increment behind OpULessThan %bool %n %gsc_k<mv> (OpSelectionMerge + OpBranchConditional). Extra vertices past OutputVertices are cleanly dropped, preserving the invariant 0 <= %gsc_n <= OutputVertices so OpEndPrimitive and OpReturn also stay strictly within primitive p's meta slice (including OutputVertices == 0 and repeated OpEndPrimitive calls after dropped emits).
    • Track the final split label per original basic block (block_tail) and rewrite OpPhi predecessor labels in a post-pass over main so both forward if/else merges and backward loop-header back-edges stay valid SPIR-V whenever OpEmitVertex (or the entry primitive-count check) splits a block.
  • tests/test_spirv_gs_to_compute_bounds.py: Stdlib-only unittest suite (runs under both python -I and pytest) executing the lowered compute SPIR-V across multi-primitive buffers with sentinel guards:
    • Over-emitting (OutputVertices 3, 5 emits) leaves primitive p + 1's rec and meta untouched and clamps p's count to 3.
    • Repeated OpEndPrimitive after dropped emits stays within primitive p's slice.
    • OutputVertices 0 drops all emits and writes count 0.
    • Under-emitting (2 < 4) preserves exact count and per-strip flags.
    • Forward OpPhi across split %entry and %then blocks + backward loop-header OpPhi across a split %latch block (5 loop iterations clamped to OutputVertices 3).
    • Assembles every rewritten module to binary SPIR-V via tools/spirv_asm.py (and runs spirv-as + spirv-val when available on PATH).
  • docs/design.md: Updated the Geometry shaders row with the OutputVertices emit-bound guard and test reference.

Verification (CPU, Windows 11, Python 3.14; no game files, no GPU needed)

  • python -I tests/test_spirv_gs_to_compute_bounds.py -v: 6/6 OK (including tools/spirv_asm.py assembly)
  • PHI_GS OpPhi parity check (%x = OpPhi %uint %u1 %t %u0 %gsc_body): OK
  • python -I tests/test_docs_links.py: 5/5 OK
  • python -I tests/test_docs_repo_paths.py: 2/2 OK
  • python -m compileall -q make_game.py tools tests: OK

AI-assisted.

@shdann

shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested bff06ea on Windows, CPython 3.14.7, CPU only (no GPU, no game files): pass for the part that needs no SPIRV-Tools or llvm-mc.

  • python -m pytest -q -rs tests/test_spirv_gs_to_compute.py tests/test_spirv_gs_to_compute_bounds.py (venv with the pinned tests/requirements.txt): 7 passed, 11 skipped (they need spirv-as/spirv-val or an AMDGPU llvm-mc).
  • Negative control: with tools/spirv_gs_to_compute.py from the merge base (f847748): 5 failed, 2 passed, the same 11 skipped.
  • Extra probe in the assembly interpreter: a hand-built geometry shader that stores (1,0,0,1), emits three vertices and ends the primitive, with OutputVertices = 1. Running invocation 1 of 2 left invocation 0's sentinel record (77,77,77,77) intact, wrote exactly one (1,0,0,1) record, and produced metadata [77,77,1,1].

Not checked here: assembly and spirv-val of the rewritten modules. Note for whoever merges: #3326 fixes the same item (SHADER-617). No defect found. AI-assisted.

@JRamos1993

Copy link
Copy Markdown
Contributor

Ran this on main f847748, which the head is already based on (Windows 11, Python 3.14, SPIRV-Tools from Vulkan SDK 1.4.363.0, CPU only). The tests pass:

  • tests/test_spirv_gs_to_compute_bounds.py 6 OK. spirv-as and spirv-val run in all six: a failing spirv-val stub on PATH turns all six into errors.
  • pytest tests/test_spirv_gs_to_compute.py 12 passed, with an AMDGPU llvm-mc.

With the tool from main, 5 of the 6 new tests fail. Three mutations each fail 2–4 of them: guarding on n < OutputVertices + 1, moving the %gsc_n increment out of the guard, and not recording the split block for OpPhi.

One shape the split doesn't handle: an OpEmitVertex in a loop header, before its OpLoopMerge. I built it with the test's _make_gs and max_verts=3:

%entry: OpBranch %hdr
%hdr:   %i = OpPhi %uint %u0 %entry %i1 %cont; OpStore %out_pos %v_a; OpEmitVertex
        %more = OpULessThan %bool %i %u5; OpLoopMerge %exit %cont None; OpBranchConditional %more %cont %exit
%cont:  %i1 = OpIAdd %uint %i %u1; OpBranch %hdr
%exit:  OpEndPrimitive; OpReturn

The input passes spirv-val. After rewrite(), though, the OpLoopMerge sits in the new merge block while the back-edge still targets %hdr. spirv-val then fails with Back-edges ... can only be formed between a block and a loop header. On main, the same module lowers to valid SPIR-V.

rdna2spirv's loop headers hold only OpLoopMerge + OpBranch, so translated shaders probably don't reach this. Raising Unsupported when a block being split goes on to an OpLoopMerge would keep the output valid.

AI-assisted (Claude Code).

@timelery

Copy link
Copy Markdown
Contributor Author

Thanks @JRamos1993 for catching that structured-CFG edge case! Rebased onto main (aa2988c) and updated in b4b0f6a: rewrite() now tracks blocks split by OpEmitVertex and raises Unsupported when OpLoopMerge appears later in the same split block, with a regression test (test_emit_vertex_in_loop_header_before_loop_merge_rejected) in tests/test_spirv_gs_to_compute_bounds.py (7/7 OK). AI-assisted, CPU only, no game files.

@timelery
timelery force-pushed the shader-617-gs-emit-bounds branch from bff06ea to b4b0f6a Compare November 22, 2026 22:34
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.

3 participants