"""SHADER-177: v_interp_p1/p2 through a barycentric pair the program computed. Hardware weights the interpolation with the values the operand VGPRs hold at the two ops, not with the hardware's own pair: Vonsek measured it on RDNA2 (#1224, synthetic 64x64 triangle, attr (0,0)/(2,0)/(0,2)) -- zeroing v0/v1 before the interp renders (0,0), i.e. P0, on every pixel, and reading the pair through swapped copies renders (v,u). The single-lane model used to ignore the operand, so those translated as the centre interpolation. Now the pair's attribute is read per-vertex and weighted with the operand values: * a v_mov of a constant into v0/v1 (snapped coordinates, the zero form of the evidence above); * arithmetic on the saved pair (evaluate-at-offset: J' = J / 2 halves the second weight); * copies in either role, swapped included -- as writes into the operand registers; a pristine plain copy of the pair stays #1224's model (centre interpolation, bit-identical .spv); * the corpus form where J is written between the p1 and the p2: the p1's I is kept and the p2's J is loaded fresh, because hardware's final value uses the I of the p1 with the J of the p2; * v_interp_mov_f32 still reads the provoking vertex when the location also has computed p1/p2. Refused instead of guessed (Unsupported, naming the pattern): a source that is neither v0/v1 nor written, or a pristine copy in the wrong role, is the #1224 guard's refusal (interp_pair_src, at emission); here: a computed pair whose p1 partial is rewritten or read between the ops (its hardware value P10*I is then observable), one whose ops are not in a single basic block (the captured I would not dominate the p2), a dangling op without its partner, and any computed pair under --bary-fallback, which has no PerVertexKHR inputs. A pristine pair (v0/v1 nothing writes) keeps the interpolated-input read: no PerVertexKHR. Pipodi's straight-line scan over the corpus found the affected 213 PS (upper bound; issue #1218, comment 7076614458); the classification and counts are in docs/design.md's SHADER-177 row. """ import struct import unittest import support import rdna2spirv # Barycentric weights of vertices 0, 1, 2 as Vulkan hands them over ((k, i, j); AMD's i, j are .y/.z). BARY = (0.25, 0.25, 0.5) I, J = BARY[1], BARY[2] # Vonsek's triangle: attr0.x = u = 0/2/0, attr0.y = v = 0/0/2, plus channels for the mov and snap checks. UV = ((0.0, 0.0, 1.0, 0.0), (2.0, 0.0, 3.0, 0.0), (0.0, 2.0, 5.0, 0.0)) ATTR0 = [tuple(struct.unpack(" None: _, words = run(["v_swap_b32 v0, v1", "exp mrt0 v0, v1, v0, v1 done vm", "s_endpgm"]) self.assertEqual(floats(words), (J, I, J, I)) def test_swap_overwrites_interpolation_weight_in_second_operand(self) -> None: # A copy of I ceases to be pristine too when it is the second operand of the swap. for weight in ("v0", "v8"): with self.subTest(weight=weight): setup = ["v_mov_b32 v8, v0"] if weight == "v8" else [] _, words = run(setup + [ "v_mov_b32 v6, 0.125", f"v_swap_b32 v6, {weight}", f"v_interp_p1_f32 v2, {weight}, attr0.x", "v_interp_p2_f32 v2, v1, attr0.x", f"v_interp_p1_f32 v3, {weight}, attr0.y", "v_interp_p2_f32 v3, v1, attr0.y", "exp mrt0 v2, v3, v6, v2 done vm", "s_endpgm", ]) self.assertEqual(floats(words), (0.25, 2 * J, I, 0.25)) def test_zeroed_pair_reads_the_provoking_vertex(self): """Vonsek's zero-source row: I' = J' = 0 interpolate to P0 = (0, 0) on every pixel.""" _, w = run(["v_mov_b32_e32 v0, 0", "v_mov_b32_e32 v1, 0", "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"]) self.assertEqual(floats(w[:2]), (0.0, 0.0)) def test_constant_pair_snaps_to_a_point(self): """Snapped coordinates: I' = J' = 0.25 pick the point a quarter along both edges.""" _, w = run(["v_mov_b32_e32 v0, 0.25", "v_mov_b32_e32 v1, 0.25", "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", "v_interp_p1_f32_e32 v4, v0, attr0.z", "v_interp_p2_f32_e32 v4, v1, attr0.z", "exp mrt0 v2, v3, v4, v2 done vm", "s_endpgm"]) # P = 0.5*P0 + 0.25*P1 + 0.25*P2 self.assertEqual(floats(w[:3]), (0.5, 0.5, 2.5)) def test_arithmetic_pair_evaluates_at_an_offset(self): """Evaluate-at-offset: keeping I and halving J halves the second weight (u stays, v halves).""" _, w = run(["v_mov_b32_e32 v10, v0", "v_mul_f32_e32 v11, 0.5, v1", "v_mov_b32_e32 v0, v10", "v_mov_b32_e32 v1, v11", "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"]) self.assertEqual(floats(w[:2]), (2 * I, J)) def test_swapped_copies_swap_the_weights(self): """Vonsek's role-swap row as a computed pair: the saved J is written into v0 and the saved I into v1, so the p1 weights with J and the p2 with I: (v, u). Pristine copies read in swapped roles carry no computed value and stay the #1224 guard's refusal (root-role enforced).""" _, w = run(["v_mov_b32_e32 v14, v1", "v_mov_b32_e32 v15, v0", # save J, save I "v_mov_b32_e32 v0, v14", "v_mov_b32_e32 v1, v15", # swap into the operands "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"]) self.assertEqual(floats(w[:2]), (2 * J, 2 * I)) def test_j_written_between_the_ops(self): """The corpus form: the p1 runs with I already computed, and J is set up between the two ops.""" _, w = run(["v_mov_b32_e32 v16, 1.0", "v_mov_b32_e32 v0, v15", # I' = 0 (v15 never written) "v_interp_p1_f32_e32 v2, v0, attr0.x", "v_mul_f32_e32 v1, 0.25, v16", # J' written after the p1 "v_interp_p2_f32_e32 v2, v1, attr0.x", "v_interp_p1_f32_e32 v3, v0, attr0.y", "v_mul_f32_e32 v1, 0.5, v16", "v_interp_p2_f32_e32 v3, v1, attr0.y", "exp mrt0 v2, v3, v2, v3 done vm", "s_endpgm"]) # x: 0 + 0*2 + 0.25*0 = 0; y: 0 + 0*0 + 0.5*2 = 1 self.assertEqual(floats(w[:2]), (0.0, 1.0)) def test_mov_reads_the_provoking_vertex_of_a_computed_location(self): """v_interp_mov_f32 of a location the computed pair also reads still gives vertex 0's value (1.0; the perspective interpolation of 1/3/5 at these weights is 3.5).""" _, w = run(["v_mov_b32_e32 v0, 0", "v_mov_b32_e32 v1, 0", "v_interp_p1_f32_e32 v2, v0, attr0.x", "v_interp_p2_f32_e32 v2, v1, attr0.x", "v_interp_mov_f32_e32 v3, p0, attr0.z", "exp mrt0 v2, v3, v2, v3 done vm", "s_endpgm"]) self.assertEqual(floats(w[:2]), (0.0, 1.0)) def test_f16_pair_through_a_computed_pair(self): """The f16 forms weight with the computed pair too, both halves.""" LO, HI = (0.5, 1.5, 2.5), (-4.0, 8.0, 16.0) pack = lambda lo, hi: struct.unpack("