"""Wave32 pixel shaders stay isolated when Vulkan runs them in a 64-lane subgroup. The inputs are deliberately different in the two physical halves. Each translated PS is run both as one 64-lane subgroup and as two 32-lane subgroups; outputs must match the independent logical-wave32 expectations. """ import os import struct import sys import unittest HERE = os.path.dirname(os.path.abspath(__file__)) sys.dont_write_bytecode = True sys.path.insert(0, HERE) import support import spirv_mini_interp as si def fbits(value): return struct.unpack("= 4] helpers = [i for i, lane in enumerate(lanes) if lane % 32 < 4] if first_quad == "helpers" else [] inputs = [float(lane // 32) for lane in lanes] judged = [i for i in range(len(lanes)) if i not in helpers] expected = [tuple(fbits(c) for c in texels[1 + lanes[i] // 32]) for i in judged] for subgroup_size in (32, 64): with self.subTest(first_quad=first_quad, subgroup_size=subgroup_size): got = run_ps(spv, subgroup_size, inputs, lanes, push_constants=[base, 0], memory=memory, resources={"%desc_map": desc_map, "%images": images}, helpers=helpers) self.assertEqual([got[i] for i in judged], expected) def test_readfirstlane_with_zero_exec_falls_back_to_first_running_lane(self): # When EXEC == 0 across a wave32 fragment half and lane 0 of that half is absent (e.g. uncovered # first quad), v_readfirstlane_b32 must fall back to wave32_first_running_lane() rather than # shuffling from absent lane 0 (SPIR-V / Vulkan UB, SHADER-1016). spv = support.translate_lines("ps_wave32_readfirstlane_zero_exec", [ "v_interp_mov_f32_e32 v2, p0, attr0.x", "s_mov_b32 exec_lo, 0", "v_readfirstlane_b32 s20, v2", "s_mov_b32 exec_lo, -1", "v_mov_b32_e32 v6, s20", "exp mrt0 v6, v6, v6, v6 done vm", "s_endpgm", ]) lanes = [lane for lane in range(64) if lane % 32 >= 4] inputs = [float(lane + 1) for lane in lanes] # Lowest running lane is 4 (value 5.0) in half 0 and 36 (value 37.0) in half 1. expected = [(fbits(5.0 if lane < 32 else 37.0),) * 4 for lane in lanes] for subgroup_size in (32, 64): with self.subTest(subgroup_size=subgroup_size): self.assertEqual(run_ps(spv, subgroup_size, inputs, lanes), expected) def test_scalar_spill_vgpr_survives_absent_lanes(self): # Pure scalar-spill VGPRs (v_writelane_b32 / v_readlane_b32 with immediate lanes and non-VGPR # sources, never used as vector operands) must be parked in %sgpr rather than shuffled from # physical subgroup lanes: in fragment waves for small triangles or after discards, target # lanes (e.g. 0..3 or 16..19) may not be running at all, making OpGroupNonUniformShuffle UB. spv = support.translate_lines("ps_wave32_scalar_spill_absent_lanes", [ "s_mov_b32 s20, 0x11112222", "s_mov_b32 s21, 0x33334444", "s_mov_b32 s22, 0x55556666", "s_mov_b32 s23, 0x77778888", "v_writelane_b32 v48, s20, 0", "v_writelane_b32 v48, s21, 1", "v_writelane_b32 v48, s22, 16", "v_writelane_b32 v48, s23, 19", "s_mov_b32 s20, 0", "s_mov_b32 s21, 0", "s_mov_b32 s22, 0", "s_mov_b32 s23, 0", "v_readlane_b32 s4, v48, 0", "v_readlane_b32 s5, v48, 1", "v_readlane_b32 s6, v48, 16", "v_readlane_b32 s7, v48, 19", "v_mov_b32_e32 v6, s4", "v_mov_b32_e32 v7, s5", "v_mov_b32_e32 v8, s6", "v_mov_b32_e32 v9, s7", "exp mrt0 v6, v7, v8, v9 done vm", "s_endpgm", ]) self.assertNotIn("OpGroupNonUniformShuffle", spv) want = (0x11112222, 0x33334444, 0x55556666, 0x77778888) for name, lanes in ( ("lanes_0_3_absent", [lane for lane in range(64) if lane % 32 >= 4]), ("single_quad_only", [lane for lane in range(64) if lane % 32 < 4]), ): inputs = [0.0] * len(lanes) expected = [want] * len(lanes) for subgroup_size in (32, 64): with self.subTest(case=name, subgroup_size=subgroup_size): self.assertEqual(run_ps(spv, subgroup_size, inputs, lanes), expected) if __name__ == "__main__": unittest.main()