diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 20:38:54 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 20:38:54 -0500 |
| commit | d11e39bb3967d94de4333c0c0b23e7e847b2e3e5 (patch) | |
| tree | 731c53d4860ec95b4fce0a93ee9661cb5413e84c /tests/semantic | |
| parent | fa7f38b45f043408a75e1f0cc5d04d69058c0279 (diff) | |
| parent | 14d81dd093e91c2f9b4a21f74fe0db2d26db1199 (diff) | |
Merge pull request #599 from LuanFreitas
Diffstat (limited to 'tests/semantic')
| -rw-r--r-- | tests/semantic/spirv_mini_interp.py | 12 | ||||
| -rw-r--r-- | tests/semantic/test_buffer_load_format.py | 66 |
2 files changed, 71 insertions, 7 deletions
diff --git a/tests/semantic/spirv_mini_interp.py b/tests/semantic/spirv_mini_interp.py index a4b871f22..8bb599003 100644 --- a/tests/semantic/spirv_mini_interp.py +++ b/tests/semantic/spirv_mini_interp.py | |||
| @@ -709,10 +709,16 @@ def _invocation(text, inputs, push_constants, memory, step=False): | |||
| 709 | values[res] = x or y | 709 | values[res] = x or y |
| 710 | elif op == "OpLogicalNot": | 710 | elif op == "OpLogicalNot": |
| 711 | values[res] = not val(a[1]) | 711 | values[res] = not val(a[1]) |
| 712 | elif op in ("OpFAdd", "OpFSub", "OpFMul"): | 712 | elif op in ("OpFAdd", "OpFSub", "OpFMul", "OpFDiv"): |
| 713 | x, y = val(a[1]), val(a[2]) | 713 | x, y = val(a[1]), val(a[2]) |
| 714 | fn = (lambda p, q: f32(p + q)) if op == "OpFAdd" else ( | 714 | |
| 715 | (lambda p, q: f32(p - q)) if op == "OpFSub" else (lambda p, q: f32(p * q))) | 715 | def _fdiv(p, q): |
| 716 | if q != 0.0: | ||
| 717 | return f32(p / q) | ||
| 718 | return f32(math.copysign(math.inf, p) if p != 0.0 else math.nan) | ||
| 719 | |||
| 720 | fn = {"OpFAdd": (lambda p, q: f32(p + q)), "OpFSub": (lambda p, q: f32(p - q)), | ||
| 721 | "OpFMul": (lambda p, q: f32(p * q)), "OpFDiv": _fdiv}[op] | ||
| 716 | if isinstance(x, tuple) and isinstance(y, tuple) and len(x) == len(y) and all( | 722 | if isinstance(x, tuple) and isinstance(y, tuple) and len(x) == len(y) and all( |
| 717 | isinstance(p, float) and isinstance(q, float) for p, q in zip(x, y)): | 723 | isinstance(p, float) and isinstance(q, float) for p, q in zip(x, y)): |
| 718 | values[res] = tuple(fn(p, q) for p, q in zip(x, y)) | 724 | values[res] = tuple(fn(p, q) for p, q in zip(x, y)) |
diff --git a/tests/semantic/test_buffer_load_format.py b/tests/semantic/test_buffer_load_format.py index 0e6a369a4..d20706f4c 100644 --- a/tests/semantic/test_buffer_load_format.py +++ b/tests/semantic/test_buffer_load_format.py | |||
| @@ -7,6 +7,8 @@ OpSelect), so every dword-aligned load here also checks that the merge keeps its | |||
| 7 | 7 | ||
| 8 | Integer and f16 formats are used: their decode is exact, so the expected words need no rounding model. | 8 | Integer and f16 formats are used: their decode is exact, so the expected words need no rounding model. |
| 9 | """ | 9 | """ |
| 10 | from __future__ import annotations | ||
| 11 | |||
| 10 | import os | 12 | import os |
| 11 | import struct | 13 | import struct |
| 12 | import sys | 14 | import sys |
| @@ -30,7 +32,10 @@ BASE = 0x1000 | |||
| 30 | MEMORY = {BASE: 0x11223344, BASE + 4: 0xDEADBEEF, BASE + 8: 0xCAFEF00D, BASE + 12: 0x0BADC0DE} | 32 | MEMORY = {BASE: 0x11223344, BASE + 4: 0xDEADBEEF, BASE + 8: 0xCAFEF00D, BASE + 12: 0x0BADC0DE} |
| 31 | # V# FORMAT codes (word3 bits 12-18), from rdna2spirv.FORMATS | 33 | # V# FORMAT codes (word3 bits 12-18), from rdna2spirv.FORMATS |
| 32 | FMT_U32, FMT_U32X2, FMT_U32X4, FMT_UINT8X4, FMT_UINT16X2, FMT_UINT16X4, FMT_F16 = 20, 62, 75, 60, 27, 69, 13 | 34 | FMT_U32, FMT_U32X2, FMT_U32X4, FMT_UINT8X4, FMT_UINT16X2, FMT_UINT16X4, FMT_F16 = 20, 62, 75, 60, 27, 69, 13 |
| 35 | FMT_F32, FMT_F32X2, FMT_F32X3, FMT_UNORM8, FMT_UNORM8X4 = 22, 64, 74, 1, 56 | ||
| 33 | DST_SEL_XYZW = 4 | 5 << 3 | 6 << 6 | 7 << 9 | 36 | DST_SEL_XYZW = 4 | 5 << 3 | 6 << 6 | 7 << 9 |
| 37 | # DST_SEL (X, 0, 1, 1): code 4, 0, 1, 1 — SHADER-86 measures float 1.0 vs integer 1 | ||
| 38 | DST_SEL_X_0_1_1 = 4 | 0 << 3 | 1 << 6 | 1 << 9 | ||
| 34 | 39 | ||
| 35 | _TMP_DIR = None | 40 | _TMP_DIR = None |
| 36 | _SRC_PATH = None | 41 | _SRC_PATH = None |
| @@ -53,8 +58,9 @@ def tearDownModule(): | |||
| 53 | _TMP_DIR = None | 58 | _TMP_DIR = None |
| 54 | 59 | ||
| 55 | 60 | ||
| 56 | def load_format(fmt: int, offset: int, ncomp: int = 4) -> tuple: | 61 | def load_format(fmt: int, offset: int, ncomp: int = 4, dst_sel: int = DST_SEL_XYZW, |
| 57 | """buffer_load_format_<x..xyzw> of format `fmt` at byte `offset` of MEMORY; returns the four mrt0 words.""" | 62 | memory: dict | None = None) -> tuple: |
| 63 | """buffer_load_format_<x..xyzw> of format `fmt` at byte `offset`; returns the four mrt0 words.""" | ||
| 58 | if _TMP_DIR is None: | 64 | if _TMP_DIR is None: |
| 59 | setUpModule() | 65 | setUpModule() |
| 60 | suffix = "xyzw"[:ncomp] | 66 | suffix = "xyzw"[:ncomp] |
| @@ -65,7 +71,7 @@ def load_format(fmt: int, offset: int, ncomp: int = 4) -> tuple: | |||
| 65 | f"\ts_mov_b32 s0, {BASE:#x}\n" | 71 | f"\ts_mov_b32 s0, {BASE:#x}\n" |
| 66 | "\ts_mov_b32 s1, 0\n" # base high bits 0, stride 0 (raw) | 72 | "\ts_mov_b32 s1, 0\n" # base high bits 0, stride 0 (raw) |
| 67 | "\ts_mov_b32 s2, 0x100\n" # num_records (bytes) | 73 | "\ts_mov_b32 s2, 0x100\n" # num_records (bytes) |
| 68 | f"\ts_mov_b32 s3, {fmt << 12 | DST_SEL_XYZW:#x}\n" | 74 | f"\ts_mov_b32 s3, {fmt << 12 | dst_sel:#x}\n" |
| 69 | f"\tv_mov_b32 v1, {offset:#x}\n" | 75 | f"\tv_mov_b32 v1, {offset:#x}\n" |
| 70 | f"\tbuffer_load_format_{suffix} {vdata}, v1, s[0:3], 0 offen\n" | 76 | f"\tbuffer_load_format_{suffix} {vdata}, v1, s[0:3], 0 offen\n" |
| 71 | "\ts_waitcnt vmcnt(0)\n" | 77 | "\ts_waitcnt vmcnt(0)\n" |
| @@ -77,7 +83,11 @@ def load_format(fmt: int, offset: int, ncomp: int = 4) -> tuple: | |||
| 77 | with warnings.catch_warnings(): | 83 | with warnings.catch_warnings(): |
| 78 | warnings.simplefilter("ignore", ResourceWarning) | 84 | warnings.simplefilter("ignore", ResourceWarning) |
| 79 | spv = rdna2spirv.translate(_SRC_PATH) | 85 | spv = rdna2spirv.translate(_SRC_PATH) |
| 80 | return si.run_fragment(spv, memory=dict(MEMORY))["%out_mrt0"] | 86 | return si.run_fragment(spv, memory=dict(memory if memory is not None else MEMORY))["%out_mrt0"] |
| 87 | |||
| 88 | |||
| 89 | def f32_bits(x: float) -> int: | ||
| 90 | return struct.unpack("<I", struct.pack("<f", x))[0] | ||
| 81 | 91 | ||
| 82 | 92 | ||
| 83 | def f16_word(h: int) -> int: | 93 | def f16_word(h: int) -> int: |
| @@ -134,6 +144,50 @@ class CrossingDwordBoundary(unittest.TestCase): | |||
| 134 | self.assertEqual(load_format(FMT_UINT16X4, 6), (0xDEAD, 0xF00D, 0xCAFE, 0xC0DE)) | 144 | self.assertEqual(load_format(FMT_UINT16X4, 6), (0xDEAD, 0xF00D, 0xCAFE, 0xC0DE)) |
| 135 | 145 | ||
| 136 | 146 | ||
| 147 | class MissingComponentsRepeat(unittest.TestCase): | ||
| 148 | """SHADER-86 / RX 6700: missing channels are c mod n, not 0 or 1.""" | ||
| 149 | |||
| 150 | def test_one_component_formats_xxxx(self): | ||
| 151 | self.assertEqual(load_format(FMT_U32, 4), (0xDEADBEEF,) * 4) | ||
| 152 | self.assertEqual(load_format(FMT_F32, 4), (0xDEADBEEF,) * 4) | ||
| 153 | # 8_UNORM at offset 4: low byte 0xEF → 239/255; all four channels match. | ||
| 154 | want = f32_bits(239.0 / 255.0) | ||
| 155 | self.assertEqual(load_format(FMT_UNORM8, 4), (want,) * 4) | ||
| 156 | |||
| 157 | def test_two_and_three_component_xyxy_and_xyzx(self): | ||
| 158 | self.assertEqual(load_format(FMT_U32X2, 4), (0xDEADBEEF, 0xCAFEF00D, 0xDEADBEEF, 0xCAFEF00D)) | ||
| 159 | self.assertEqual(load_format(FMT_F32X2, 4), (0xDEADBEEF, 0xCAFEF00D, 0xDEADBEEF, 0xCAFEF00D)) | ||
| 160 | self.assertEqual(load_format(FMT_F32X3, 0), (0x11223344, 0xDEADBEEF, 0xCAFEF00D, 0x11223344)) | ||
| 161 | |||
| 162 | |||
| 163 | class UnormCorrectlyRounded(unittest.TestCase): | ||
| 164 | """SHADER-86: UNORM is OpFDiv by (2^n-1), not a reciprocal multiply. Raw 3 → 0x3c40c0c1.""" | ||
| 165 | |||
| 166 | def test_unorm8x4_raw_3(self): | ||
| 167 | mem = {BASE: 0x03030303} | ||
| 168 | want = 0x3C40C0C1 | ||
| 169 | self.assertEqual(load_format(FMT_UNORM8X4, 0, memory=mem), (want,) * 4) | ||
| 170 | # f32 reciprocal multiply disagrees (BetterJdue: 504 mismatches on rgba8). | ||
| 171 | recip = f32_bits(struct.unpack("<f", struct.pack("<f", 3.0))[0] | ||
| 172 | * struct.unpack("<f", struct.pack("<f", 1.0 / 255.0))[0]) | ||
| 173 | self.assertNotEqual(want, recip) | ||
| 174 | |||
| 175 | |||
| 176 | class DstSelOneByFormat(unittest.TestCase): | ||
| 177 | """SHADER-86: DST_SEL 1 is 1.0f for float/unorm, integer 1 for UINT.""" | ||
| 178 | |||
| 179 | def test_dst_sel_one_float_vs_uint(self): | ||
| 180 | # DST_SEL (X, 0, 1, 1): Z and W come from the constant-1 code. | ||
| 181 | f32 = load_format(FMT_F32, 4, dst_sel=DST_SEL_X_0_1_1) | ||
| 182 | self.assertEqual(f32, (0xDEADBEEF, 0, 0x3F800000, 0x3F800000)) | ||
| 183 | u32 = load_format(FMT_U32, 4, dst_sel=DST_SEL_X_0_1_1) | ||
| 184 | self.assertEqual(u32, (0xDEADBEEF, 0, 1, 1)) | ||
| 185 | unorm = load_format(FMT_UNORM8, 4, dst_sel=DST_SEL_X_0_1_1) | ||
| 186 | self.assertEqual(unorm[2:], (0x3F800000, 0x3F800000)) | ||
| 187 | u8 = load_format(FMT_UINT8X4, 4, dst_sel=DST_SEL_X_0_1_1) | ||
| 188 | self.assertEqual(u8[2:], (1, 1)) | ||
| 189 | |||
| 190 | |||
| 137 | class InterpreterConversions(unittest.TestCase): | 191 | class InterpreterConversions(unittest.TestCase): |
| 138 | """The ops the format decode needs from spirv_mini_interp: integer to float and GLSL FMin/FMax.""" | 192 | """The ops the format decode needs from spirv_mini_interp: integer to float and GLSL FMin/FMax.""" |
| 139 | 193 | ||
| @@ -159,6 +213,10 @@ class InterpreterConversions(unittest.TestCase): | |||
| 159 | self.assertEqual(self.run_expr(consts, "%r = OpExtInst %float %glsl FMax %a %b\n"), 0x3FC00000) | 213 | self.assertEqual(self.run_expr(consts, "%r = OpExtInst %float %glsl FMax %a %b\n"), 0x3FC00000) |
| 160 | self.assertEqual(self.run_expr(consts, "%r = OpExtInst %float %glsl FMin %a %b\n"), 0xC0000000) | 214 | self.assertEqual(self.run_expr(consts, "%r = OpExtInst %float %glsl FMin %a %b\n"), 0xC0000000) |
| 161 | 215 | ||
| 216 | def test_fdiv_correctly_rounded(self): | ||
| 217 | consts = "%a = OpConstant %float 3\n%b = OpConstant %float 255\n" | ||
| 218 | self.assertEqual(self.run_expr(consts, "%r = OpFDiv %float %a %b\n"), 0x3C40C0C1) | ||
| 219 | |||
| 162 | 220 | ||
| 163 | if __name__ == "__main__": | 221 | if __name__ == "__main__": |
| 164 | unittest.main() | 222 | unittest.main() |