"""rdna2spirv.py --f16-interp-fallback: v_interp_*_f16 without PerVertexKHR inputs (SHADER-196). By default a location any *_f16 interp reads is a PerVertexKHR vec4[3] of the producer's raw words, decoded and weighted in the PS (test_interp_f16_high.py). MoltenVK can't compile that decoration to MSL ("PerVertexKHR decoration is not supported in MSL"), so with the flag the producer (VS `exp paramN`, mesh params) also exports each channel's two f16 halves decoded to f32, low halves at location 28 - 2N and high halves at 29 - 2N, and the PS reads them as ordinary smooth inputs (the top is 29: MoltenVK's maxVertexOutputComponents, 124, counts gl_Position; SHADER-291). Interpolating the decoded halves is the same P0 + i(P1 - P0) + j(P2 - P0). Here: * the PS has no PerVertexKHR, no barycentric builtin, and reads 1.75 / 9.0 from the decoded-half inputs (the per-vertex values of test_interp_f16_high.py, interpolated as Vulkan would), the f32 control and the flat raw read unchanged; * the VS exports the decoded halves of every param at those locations (one-lane interpreter), and drops the ones that would land on its own params; the metadata lists both sides; * a PS whose decoded-half location is also an attribute it reads is rejected rather than mislinked; * without the flag nothing changes. The GPU check is runtime/test/shaders/ps_interp_f16_high drawn with vs_f16_pack, which run_gpu_tests.py translates with the flag where tools/vk_subgroup_info.py reports per_vertex_inputs false (MoltenVK). """ import re import struct import unittest import support import rdna2spirv from spirv_mini_interp import run_fragment from test_interp_f16_high import ATTR0, ATTR1, BARY, CTRL, HI, LO, SHADER def f32(w): return struct.unpack("= 7, up to attr12) pack all decoded halves into free locations <= 29 without colliding with any raw attribute.""" lines = [] for a in range(15): lines += [f"v_interp_p1_f32_e32 v{10 + a}, v0, attr{a}.x", f"v_interp_p2_f32_e32 v{10 + a}, v1, attr{a}.x"] for i, a in enumerate((0, 4, 5, 6, 7)): v = 30 + i lines += [f"v_interp_p1ll_f16 v{v}, v0, attr{a}.x", f"v_interp_p2_f16 v{v}, v1, attr{a}.x, v{v}"] lines += ["v_interp_p1ll_f16 v40, v0, attr7.x high", "v_interp_p2_f16 v40, v1, attr7.x, v40 high", "exp mrt0 v34, v40, v24, v10 done vm", "s_endpgm"] spv, meta = translate("ps_f16_corpus_pack", lines) self.assertEqual(meta["f16_halves"], { "0": [28, 29], "4": [26, 27], "5": [24, 25], "6": [22, 23], "7": [20, 21], }) self.assertLessEqual(max(meta["inputs"]), 29) self.assertEqual(len(meta["inputs"]), len(set(meta["inputs"]))) inputs = {f"%in_attr{a}": (float(a), 0.0, 0.0, 0.0) for a in range(15)} for a in (0, 4, 5, 6): inputs[f"%in_f16h{a}_0"] = (0.0, 0.0, 0.0, 0.0) inputs["%in_f16h7_0"] = (lerp(LO), 0.0, 0.0, 0.0) inputs["%in_f16h7_1"] = (lerp(HI), 0.0, 0.0, 0.0) words = support.mrt0_words(spv, inputs=inputs) self.assertEqual(f"{words[0] & 0xFFFF:#06x}", "0x3f00") # attr7 low: 1.75 self.assertEqual(f"{words[1] & 0xFFFF:#06x}", "0x4880") # attr7 high: 9.0 self.assertEqual(f32(words[2]), 14.0) # raw attr14 def test_high_attr_negative_canonical_slot_packs_dynamically(self): """attr15 has canonical slot 28 - 30 = -2; dynamic packing still places it in 0..29.""" lines = ["v_interp_p1ll_f16 v2, v0, attr15.x", "v_interp_p2_f16 v2, v1, attr15.x, v2", "exp mrt0 v2, v2, v2, v2 done vm", "s_endpgm"] spv, meta = translate("ps_f16_attr15", lines) self.assertEqual(meta["f16_halves"], {"15": [28, 29]}) self.assertIn("OpDecorate %in_f16h15_0 Location 28", spv) def test_location_budget_overflow_is_rejected(self): """29 raw attributes (0..28) plus 1 f16 attribute needs 31 locations > 30: still rejected.""" lines = [] for a in range(29): lines += [f"v_interp_p1_f32_e32 v{10 + a}, v0, attr{a}.x", f"v_interp_p2_f32_e32 v{10 + a}, v1, attr{a}.x"] lines += ["v_interp_p1ll_f16 v2, v0, attr0.x", "v_interp_p2_f16 v2, v1, attr0.x, v2", "exp mrt0 v2, v10, v10, v10 done vm", "s_endpgm"] with self.assertRaises(rdna2spirv.Unsupported) as ctx: translate("ps_f16_overflow", lines) self.assertIn("decoded-half locations", str(ctx.exception)) def test_off_by_default(self): spv, meta = translate("ps_f16_fallback", SHADER.splitlines(), fallback=False) self.assertIn("OpDecorate %in_attr0 PerVertexKHR", spv) self.assertNotIn("%in_f16h", spv) self.assertNotIn("f16_halves", meta) class FallbackVertexShader(unittest.TestCase): def test_exports_decoded_halves(self): spv, meta = translate("vs_f16_fallback", VS.splitlines()) for n, lo, hi in ((0, 28, 29), (1, 26, 27)): self.assertIn(f"OpDecorate %out_f16h{n}_0 Location {lo}", spv) self.assertIn(f"OpDecorate %out_f16h{n}_1 Location {hi}", spv) self.assertEqual(meta["f16_halves"], {"0": [28, 29], "1": [26, 27]}) for vid in range(3): out = run_fragment(spv, inputs={"%vertex_index": vid, "%instance_index": 0}) u, v = float((vid << 1) & 2), float(vid & 2) raw = list(out["%out_param0"]) # outputs come back as 32-bit words lo = [struct.unpack("