Skip to content

rdna2spirv: load BaryCoordKHR for untouched or conditional v0/v1 in computed I/J pairs - #2742

Merged
ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
Kosmabb8:fix/computed-ij-bary-init
Nov 22, 2026
Merged

ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
Kosmabb8:fix/computed-ij-bary-init

Conversation

@Kosmabb8

Copy link
Copy Markdown
Contributor

Claim: #2716 (comment)

Problem

In tools/rdna2spirv.py:translate(), no_bary strips v_interp source VGPRs before calling read_before_write_vgprs(no_bary) so pristine v_interp pairs don't pull in BaryCoordKHR. Later, interp_ij_analysis() promotes a v_interp_p1/p2 pair to computed I/J (PerVertexKHR) whenever either v0 or v1 is written anywhere in the shader, and emission loads both I and J from %vgpr.

When a pixel shader overwrites only one coordinate (e.g. v_mov_b32 v0, 0 while leaving v1 as hardware J, or vice versa) or overwrites v0/v1 conditionally inside a branch, nothing outside v_interp reads the untouched coordinate, so g.frag_inputs omits bary_i/bary_j. The emitted SPIR-V reads 0.0 from zero-initialized %vgpr[0]/%vgpr[1] instead of BaryCoordKHR.y/.z, silently miscompiling the interpolated attribute.

Fix

  • After interp_ij_analysis(insns, g) annotates computed sites with ins["ij"], run read_before_write_vgprs keeping the source VGPRs of computed v_interp sites (and in branchy shaders, any v0/v1 named by a computed site's ij), registering any missing 0: "bary_i" / 1: "bary_j" entries in g.frag_inputs (plus g.cu(r)).
  • Pristine shaders and straight-line shaders that unconditionally overwrite both v0 and v1 before v_interp remain unchanged.
  • Add 3 regression tests to tests/semantic/test_interp_computed_ij.py covering only-I overwritten, only-J overwritten, and conditional overwrite across a branch (both taken and untaken paths).

Validation

  • Positive control: all 3 new tests fail on unmodified main (49475796), returning 0.0 instead of 2*I / 2*J for the untouched/untaken coordinate.
  • After fix (Windows 11, Python 3.12.10, CPU only, synthetic fixtures, no game files):
    • python -m unittest discover -s tests/semantic -p "test_interp*.py": 138/138 passed (18/18 in test_interp_computed_ij.py)
    • python -I tests/test_resource_hygiene.py: 2/2 passed
    • python -I tests/test_text_write_line_endings.py: 2/2 passed
    • python -m compileall -q make_game.py tools tests: passed

Limitations

  • Verified on CPU with spirv_mini_interp and synthetic fixtures only (no GPU run, no spirv-val/llvm-mc, no retail shader corpus).

AI-assisted.

@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent Vulkan check at fb50143d on the RTX 5070 (Windows, 621.24, required fragment subgroup 32). With a synthetic fullscreen UV triangle, both one-coordinate overwrites and both directions of the scalar-branch test match the pixel-center reference. Pristine interpolation and unconditionally overwriting both coordinates also pass. The 138 interpolation semantic tests passed.

One related case remains: EXEC can skip a VGPR write without a scalar branch. This straight-line pixel shader illustrates it:

s_and_saveexec_b32 s10, s0
v_mov_b32_e32 v0, 0
s_mov_b32 exec_lo, s10
v_interp_p1_f32_e32 v2, v0, attr0.x
v_interp_p2_f32_e32 v2, v1, attr0.x
v_interp_p1_f32_e32 v3, v0, attr0.y
v_interp_p2_f32_e32 v3, v1, attr0.y
exp mrt0 v2, v3, v2, v3 done vm
s_endpgm

Pass s0=0 and neither coordinate should be overwritten, so pixel (0,0) at 64×64 should give UV (0.0078125, 0.0078125). The PR gives (0, 0.0078125): read_before_write_vgprs counts the masked v0 write as unconditional, and branchy is false. With s0=0x55555555, the same error affects every lane that skipped the write. A marker written under the same EXEC mask distinguished the two groups independently of pixel-to-lane mapping.

I tested broadening the existing ij_used guard to branchy or any(writes_exec(i) for i in insns). With that local change, all 34 draws (134,912 pixels) passed the analytic reference within 2e-6, including zero, half and full EXEC masks with I, J, and both coordinates overwritten, at 64×64 and 96×40. The 138 interpolation tests still pass and every shader passed Vulkan 1.2 SPIR-V validation. This was synthetic Vulkan coverage.

@kapitche

kapitche commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested on Linux (WSL2 Ubuntu, Python 3.10.12) and native Windows 11 (Python 3.13.1), CPU only: pass. Head fb50143, clean merge with main.

@Kosmabb8

Copy link
Copy Markdown
Contributor Author

Thanks @duudiis for the independent RTX 5070 verification and for catching the straight-line EXEC-masked write case! Updated in 0a556344 to broaden the guard to branchy or any(writes_exec(i) for i in insns) and added a regression test in tests/semantic/test_interp_computed_ij.py covering both EXEC-masked execution (s_mov_b32 exec_lo, s0) and s_and_saveexec_b32 prologue loading (139/139 test_interp*.py, resource hygiene, line endings, and compileall passing on Windows 11 / Python 3.12.10, CPU only). AI-assisted.

@padhossein

Copy link
Copy Markdown
Contributor

Independent follow-up for computed I/J (fb50143d). The 18 interp tests pass, but I reproduced a remaining branch case using an LLVM-assembled synthetic PS. Take the PR's conditional-overwrite case and insert v_mov_b32_e32 v5, v0 / v_mov_b32_e32 v6, v1 after the join, then use v5/v6 in the p1/p2 pairs. On both base and this head I get (0, 1) on both paths, while the path that skips the overwrite should give (0.5, 1) for I=0.25, J=0.5. The source temporaries in ij_used are 5/6, and the linear scan has already marked v0 written. In a local experiment, conservatively initializing both hardware coordinates when a branchy shader has computed I/J restored (0.5, 1) and (0, 1) on the two paths. Could we cover the copied-coordinate case? Both SPIR-V modules pass spirv-val. Results above are from the semantic interpreter, since local llvmpipe lacks the barycentric extension.

AI-assisted (Codex).

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 0a556344338640a331d85785b6dea3c4ac3ac237 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Independent synthetic Vulkan check of 0a556344338640a331d85785b6dea3c4ac3ac237 on Linux 7.0.0-43 (RTX 5090, NVIDIA 595.99.02, fragment subgroup 32, LLVM 21.1.8, SPIRV-Tools 2026.1).

  • All 4,096 pixels matched on both paths of the scalar branch with direct v0/v1 interpolation sources. Both cases of the new straight-line EXEC test also matched, with the overwrite masked out and with all lanes enabled.
  • I repeated the copied-coordinate check with the same two copies after the scalar branch join. When the branch skipped the I overwrite, all 4,096 pixels still had zero I on this head. J matched in every pixel. The path that actually overwrote I passed. At pixel (1,1), expected XY was (0.0234375, 0.0234375) on the skipped-overwrite path, but the readback was (0, 0.0234375).
  • All modules passed Vulkan 1.2 SPIR-V validation and the 19 checked-in computed-I/J semantic tests passed.

AI-assisted verification.

@Marukooh

Copy link
Copy Markdown
Contributor

The GPU A/B isolates the effect of 0a556344 relative to its parent fb50143d on CachyOS Linux, RTX 5070 Ti, NVIDIA 620.73.04, and a required fragment subgroup of 32. I used the same handwritten fullscreen triangle and compared XY against the pristine interpolation draw.

  • Straight-line EXEC with the I overwrite masked out: 0/4,096 matching pixels on the parent and 4,096/4,096 on the head. With all lanes enabled, both revisions matched all 4,096 pixels.
  • The copied-coordinate scalar-branch case reported above still has 0/4,096 matching pixels on the path that skips the overwrite, on both revisions. J matches and the path that does overwrite I matches all 4,096 pixels. This independently confirms the 5090 result on the 5070 Ti and shows that this case predates the EXEC follow-up.
  • All six modules passed SPIR-V validation for Vulkan 1.2, and all 139 interpolation semantic tests passed on the head.

AI-assisted verification. Only synthetic fixtures were used.

@Kosmabb8

Kosmabb8 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @padhossein for catching the copied-coordinate case! Reproduced locally (got (0.0, 1.0) instead of (0.5, 1.0) on the untaken path when v0/v1 were copied to v5/v6 after the conditional write). Updated in 460aea12 to initialize both v0 (bary_i) and v1 (bary_j) whenever a shader with computed I/J has branches or EXEC writes, and added test_conditional_overwrite_then_copied_coords_preserves_hardware_barycentrics to tests/semantic/test_interp_computed_ij.py (140/140 test_interp*.py, 7/7 test_ps_*.py, resource hygiene, line endings, and compileall passing on Windows 11 / Python 3.12.10, CPU only, synthetic fixtures). AI-assisted.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 460aea12a191bd0eee9c29c56acae18bf570e891 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Retested the copied-coordinate GPU case on 460aea12a191bd0eee9c29c56acae18bf570e891, Linux 7.0.0-43 (RTX 5090, NVIDIA 595.99.02, fragment subgroup 32, LLVM 21.1.8, SPIRV-Tools 2026.1).

  • The same copied-coordinate draw with the I overwrite skipped improved from 0/4,096 matching pixels on 0a556344 to 4,096/4,096. At pixel (1,1), XY is now (0.0234375, 0.0234375). When the overwrite executes, I is still zero and J matches at every pixel.
  • Both direct-coordinate branch paths and both straight-line EXEC cases also matched all 4,096 pixels per draw. All five synthetic modules passed Vulkan 1.2 SPIR-V validation and all 20 focused semantic tests passed.

AI-assisted verification.

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.

7 participants