[PR #2239] rdna2spirv: lower computed I/J barycentrics under --f16-interp-fallback (SHADER-F16-INTERP-COMPUTED-IJ) #2242

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

📋 Pull Request Information

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

Base: main ← Head: fix/rdna2spirv-f16-interp-computed-ij


📝 Commits (1)

  • cd8d9aa rdna2spirv: lower computed I/J barycentrics under --f16-interp-fallback

📊 Changes

11 files changed (+512 additions, -63 deletions)

View changed files

📝 docs/design.md (+1 -0)
📝 runtime/test/shaders/build.py (+6 -6)
📝 runtime/test/shaders/check.py (+4 -4)
📝 scripts/run_gpu_tests.py (+4 -5)
➕ tests/semantic/fixtures/ps_ij_computed_deriv_fallback.asm (+30 -0)
📝 tests/semantic/spirv_mini_interp.py (+24 -0)
📝 tests/semantic/test_interp_computed_ij.py (+7 -7)
➕ tests/semantic/test_interp_computed_ij_fallback.py (+312 -0)
📝 tests/test_run_gpu_tests.py (+17 -13)
📝 tests/test_shader_build_fallbacks.py (+30 -16)
📝 tools/rdna2spirv.py (+77 -12)

📄 Description

Claimed in the coordination issue as: SHADER-F16-INTERP-COMPUTED-IJ (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7188273837)

Depends on: #1426 (orthogonal topic: #1426 handles --bary-fallback with Flat vec4[3] geometry-shader pass-through and leaves F16_HALVES and not BARY_FALLBACK refused; listed here because adjacent hunk context in tools/rdna2spirv.py and tests/test_shader_build_fallbacks.py sits nearby on merge)

What and why

When per_vertex_inputs is unavailable and no geometry shader is used (--f16-interp-fallback without --bary-fallback, e.g. on MoltenVK / Apple Silicon where BuiltIn BaryCoordKHR is available without PerVertexKHR or geometry shaders), interp_ij_analysis in tools/rdna2spirv.py refused all 207 pixel shaders in the corpus that compute custom (I, J) barycentrics into VGPRs (computed I/J pair under --f16-interp-fallback: no PerVertexKHR inputs). OpCapability InterpolationFunction (InterpolateAtOffset) cannot be used without enabling sampleRateShading and is rejected on BaryCoordKHR by SPIRV-Cross MSL.

Because perspective-correct BaryCoordKHR (I, J) already absorbs 1/W, every smooth-interpolated input P (both standard f32 attributes and decoded --f16-interp-fallback halves) satisfies the exact affine relation P = P_0 + I \cdot P_{10} + J \cdot P_{20} across all four invocations of a 2 \times 2 screen quad. Under F16_HALVES and not BARY_FALLBACK:

  • Hoists OpDPdxFine and OpDPdyFine (OpCapability DerivativeControl, core Vulkan 1.0, zero device feature bits, zero extra Locations, zero geometry shader) of BaryCoordKHR.y/.z and each computed (attribute, component) channel into uniform %entry.
  • Guards degenerate zero-area, subnormal, ±inf, and NaN determinants using integer IEEE-754 normal-range bit comparisons (0x00800000 <= (OpBitcast_u32(det) & 0x7FFFFFFF) < 0x7F800000, avoiding OpFDiv-by-zero and MoltenVK's OpFOrdNotEqual NaN hazard) so degenerate quads cleanly fall back to pixel-center P_c.
  • Evaluates P(I', J') = P_c + \alpha \cdot d_x P + \beta \cdot d_y P with pure scalar ALU at each v_interp site, including inside non-uniform EXEC-masked branches and loops.
  • Leaves --bary-fallback (BARY_FALLBACK) refusal and default PerVertexKHR emission completely unchanged.

Tested

  • Full corpus (--f16-interp-fallback): 26,296 / 26,503 -> 26,503 / 26,503 (100%) translated and 26,503 / 26,503 pass spirv-val (--target-env vulkan1.2 --allow-offset-texture-operand); all 207 / 207 previously refused pixel shaders pass spirv-val, remain byte-identical under default flags, and remain refused under --bary-fallback alone.
  • Vulkan GPU hardware (LEONIDA_F16_INTERP_FALLBACK=1 LEONIDA_BARY_FALLBACK=0, VK_LAYER_KHRONOS_validation 1.4.363):
    • runtime/test/shaders/build.py vs_ids ps_ij_computed ps_interp_f16_high vs_f16_pack: 4/4 translated, 4/4 spirv-val pass (OpCapability FragmentBarycentricKHR + OpCapability DerivativeControl, zero PerVertexKHR).
    • vk_pipeline_check: 2/2 PS modules ok, 2/2 PS pipelines ok, 0 validation errors.
    • vk_harness vs_ids.spv ps_ij_computed.spv (64x64): 4,096 / 4,096 pixels pass (exact_xyw = 4096/4096, exact_z_bits = 4096/4096, max_err_z = 0.0); scripts/run_gpu_tests.py --filter ps_ij_computed passes 1/1.
  • Translator & unit suites: python -m compileall -q make_game.py tools tests, tests/semantic/test_interp_computed_ij_fallback.py, tests/semantic/test_interp*.py (211/211), tests/test_shader_build_fallbacks.py (7/7), tests/test_run_gpu_tests.py (71/71), tests/test_text_write_line_endings.py, tests/test_docs_*.py, and python -I scripts/check_no_game_data.py across all touched files (all pass).

GPUs tested: NVIDIA GeForce RTX 3090, driver 610.88 (Vulkan 1.4.341, Khronos validation layer 1.4.363), Windows 11 x64

AI-assisted: yes

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.

🔄 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/2239 **Author:** [@zmasi](https://github.com/zmasi) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rdna2spirv-f16-interp-computed-ij` --- ### 📝 Commits (1) - [`cd8d9aa`](https://github.com/ubiquitarianism/leonida/commit/cd8d9aa7a8972a879f3307249c27761e36bfe8b8) rdna2spirv: lower computed I/J barycentrics under --f16-interp-fallback ### 📊 Changes **11 files changed** (+512 additions, -63 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -0) 📝 `runtime/test/shaders/build.py` (+6 -6) 📝 `runtime/test/shaders/check.py` (+4 -4) 📝 `scripts/run_gpu_tests.py` (+4 -5) ➕ `tests/semantic/fixtures/ps_ij_computed_deriv_fallback.asm` (+30 -0) 📝 `tests/semantic/spirv_mini_interp.py` (+24 -0) 📝 `tests/semantic/test_interp_computed_ij.py` (+7 -7) ➕ `tests/semantic/test_interp_computed_ij_fallback.py` (+312 -0) 📝 `tests/test_run_gpu_tests.py` (+17 -13) 📝 `tests/test_shader_build_fallbacks.py` (+30 -16) 📝 `tools/rdna2spirv.py` (+77 -12) </details> ### 📄 Description Claimed in the coordination issue as: `SHADER-F16-INTERP-COMPUTED-IJ` (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7188273837) Depends on: #1426 (orthogonal topic: #1426 handles `--bary-fallback` with `Flat vec4[3]` geometry-shader pass-through and leaves `F16_HALVES and not BARY_FALLBACK` refused; listed here because adjacent hunk context in `tools/rdna2spirv.py` and `tests/test_shader_build_fallbacks.py` sits nearby on merge) ### What and why When `per_vertex_inputs` is unavailable and no geometry shader is used (`--f16-interp-fallback` without `--bary-fallback`, e.g. on MoltenVK / Apple Silicon where `BuiltIn BaryCoordKHR` is available without `PerVertexKHR` or geometry shaders), `interp_ij_analysis` in `tools/rdna2spirv.py` refused all 207 pixel shaders in the corpus that compute custom `(I, J)` barycentrics into VGPRs (`computed I/J pair under --f16-interp-fallback: no PerVertexKHR inputs`). `OpCapability InterpolationFunction` (`InterpolateAtOffset`) cannot be used without enabling `sampleRateShading` and is rejected on `BaryCoordKHR` by SPIRV-Cross MSL. Because perspective-correct `BaryCoordKHR` $(I, J)$ already absorbs $1/W$, every smooth-interpolated input $P$ (both standard `f32` attributes and decoded `--f16-interp-fallback` halves) satisfies the exact affine relation $P = P_0 + I \cdot P_{10} + J \cdot P_{20}$ across all four invocations of a $2 \times 2$ screen quad. Under `F16_HALVES and not BARY_FALLBACK`: - Hoists `OpDPdxFine` and `OpDPdyFine` (`OpCapability DerivativeControl`, core Vulkan 1.0, zero device feature bits, zero extra `Location`s, zero geometry shader) of `BaryCoordKHR.y/.z` and each computed `(attribute, component)` channel into uniform `%entry`. - Guards degenerate zero-area, subnormal, `±inf`, and `NaN` determinants using integer IEEE-754 normal-range bit comparisons (`0x00800000 <= (OpBitcast_u32(det) & 0x7FFFFFFF) < 0x7F800000`, avoiding `OpFDiv`-by-zero and MoltenVK's `OpFOrdNotEqual` NaN hazard) so degenerate quads cleanly fall back to pixel-center $P_c$. - Evaluates $P(I', J') = P_c + \alpha \cdot d_x P + \beta \cdot d_y P$ with pure scalar ALU at each `v_interp` site, including inside non-uniform `EXEC`-masked branches and loops. - Leaves `--bary-fallback` (`BARY_FALLBACK`) refusal and default `PerVertexKHR` emission completely unchanged. ### Tested - **Full corpus (`--f16-interp-fallback`):** `26,296 / 26,503` -> **`26,503 / 26,503` (`100%`)** translated and **`26,503 / 26,503` pass `spirv-val`** (`--target-env vulkan1.2 --allow-offset-texture-operand`); all `207 / 207` previously refused pixel shaders pass `spirv-val`, remain byte-identical under default flags, and remain refused under `--bary-fallback` alone. - **Vulkan GPU hardware (`LEONIDA_F16_INTERP_FALLBACK=1 LEONIDA_BARY_FALLBACK=0`, `VK_LAYER_KHRONOS_validation` `1.4.363`):** - `runtime/test/shaders/build.py vs_ids ps_ij_computed ps_interp_f16_high vs_f16_pack`: `4/4` translated, `4/4` `spirv-val` pass (`OpCapability FragmentBarycentricKHR` + `OpCapability DerivativeControl`, zero `PerVertexKHR`). - `vk_pipeline_check`: `2/2` PS modules ok, `2/2` PS pipelines ok, `0` validation errors. - `vk_harness vs_ids.spv ps_ij_computed.spv` (`64x64`): **`4,096 / 4,096` pixels pass (`exact_xyw = 4096/4096`, `exact_z_bits = 4096/4096`, `max_err_z = 0.0`)**; `scripts/run_gpu_tests.py --filter ps_ij_computed` passes `1/1`. - **Translator & unit suites:** `python -m compileall -q make_game.py tools tests`, `tests/semantic/test_interp_computed_ij_fallback.py`, `tests/semantic/test_interp*.py` (`211/211`), `tests/test_shader_build_fallbacks.py` (`7/7`), `tests/test_run_gpu_tests.py` (`71/71`), `tests/test_text_write_line_endings.py`, `tests/test_docs_*.py`, and `python -I scripts/check_no_game_data.py` across all touched files (all pass). GPUs tested: NVIDIA GeForce RTX 3090, driver 610.88 (Vulkan 1.4.341, Khronos validation layer 1.4.363), Windows 11 x64 AI-assisted: yes ### 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. --- <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#2242
No description provided.