diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 15:14:46 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 15:14:46 -0500 |
| commit | f84b63cbb6a55d93a3b156c8d861eeaa3cf68dfd (patch) | |
| tree | f25c227976c0b976740a126000155f0accb92728 | |
| parent | 2ca350449547385206052a83f7014d7878c198e8 (diff) | |
| parent | 9a031d13a6a22146558b87bf52447220e478169a (diff) | |
Merge pull request #3095 from SweatierKey
| -rw-r--r-- | tests/semantic/test_image_store_d16.py | 352 | ||||
| -rw-r--r-- | tools/rdna2spirv.py | 50 |
2 files changed, 394 insertions, 8 deletions
diff --git a/tests/semantic/test_image_store_d16.py b/tests/semantic/test_image_store_d16.py new file mode 100644 index 000000000..274415323 --- /dev/null +++ b/tests/semantic/test_image_store_d16.py | |||
| @@ -0,0 +1,352 @@ | |||
| 1 | """Semantic and validation tests for RDNA2 image_store with d16 (packed 16-bit data VGPRs). | ||
| 2 | |||
| 3 | Covers FSPX/FSR4 blocker `image_store 2D a16 d16` and all supported STORE_DIMS (1D, 2D, | ||
| 4 | 2D_ARRAY, 3D), with and without a16 / glc, across full and sparse dmasks (0x0..0xf): | ||
| 5 | - Float/norm view: IEEE binary16 halves widened via UnpackHalf2x16 -> %v4float. | ||
| 6 | - UINT view: 16-bit unsigned halves zero-extended via OpBitFieldUExtract -> %v4uint_st. | ||
| 7 | - SINT view: 16-bit signed halves sign-extended via OpBitFieldSExtract -> %v4int_st. | ||
| 8 | - Missing dmask components default to 0. | ||
| 9 | - Executes the translated unpacking and coordinate extraction IR in spirv_mini_interp | ||
| 10 | to assert bit-exact texel values and unpacked a16 coordinates, plus spirv-val Vulkan 1.2 | ||
| 11 | (native and --wave64-emulate). | ||
| 12 | """ | ||
| 13 | import json | ||
| 14 | import os | ||
| 15 | import re | ||
| 16 | import shutil | ||
| 17 | import struct | ||
| 18 | import subprocess | ||
| 19 | import sys | ||
| 20 | import tempfile | ||
| 21 | import unittest | ||
| 22 | |||
| 23 | ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||
| 24 | TOOLS = os.path.join(ROOT, "tools") | ||
| 25 | SEMANTIC = os.path.dirname(os.path.abspath(__file__)) | ||
| 26 | for _p in (TOOLS, SEMANTIC): | ||
| 27 | if _p not in sys.path: | ||
| 28 | sys.path.insert(0, _p) | ||
| 29 | |||
| 30 | import rdna2spirv # noqa: E402 | ||
| 31 | import spirv_mini_interp as si # noqa: E402 | ||
| 32 | import support # noqa: E402 | ||
| 33 | |||
| 34 | SPIRV_AS = shutil.which("spirv-as") | ||
| 35 | SPIRV_VAL = shutil.which("spirv-val") | ||
| 36 | |||
| 37 | |||
| 38 | def _f16_bits(val: float) -> int: | ||
| 39 | return struct.unpack("<H", struct.pack("<e", val))[0] | ||
| 40 | |||
| 41 | |||
| 42 | def _f32_bits(val: float) -> int: | ||
| 43 | return struct.unpack("<I", struct.pack("<f", val))[0] | ||
| 44 | |||
| 45 | |||
| 46 | def _pack_u16(lo: int, hi: int) -> int: | ||
| 47 | return (lo & 0xFFFF) | ((hi & 0xFFFF) << 16) | ||
| 48 | |||
| 49 | |||
| 50 | def _validate(spvasm: str) -> None: | ||
| 51 | if not (SPIRV_AS and SPIRV_VAL): | ||
| 52 | return | ||
| 53 | with tempfile.TemporaryDirectory() as d: | ||
| 54 | sa, sv = os.path.join(d, "m.spvasm"), os.path.join(d, "m.spv") | ||
| 55 | with open(sa, "w", encoding="utf-8", newline="\n") as f: | ||
| 56 | f.write(spvasm) | ||
| 57 | for cmd in ( | ||
| 58 | [SPIRV_AS, sa, "-o", sv, "--target-env", "vulkan1.2"], | ||
| 59 | [SPIRV_VAL, "--target-env", "vulkan1.2", "--allow-offset-texture-operand", sv], | ||
| 60 | ): | ||
| 61 | r = subprocess.run(cmd, capture_output=True, text=True) | ||
| 62 | if r.returncode != 0: | ||
| 63 | raise AssertionError(f"{cmd[0]} failed:\n{r.stderr}") | ||
| 64 | |||
| 65 | |||
| 66 | def _eval_image_store_texels(setup_lines: list[str], store_line: str) -> dict[str, tuple[int, ...]]: | ||
| 67 | """Translate setup + image_store via rdna2spirv, cut right after the three texel | ||
| 68 | OpCompositeConstructs (and coordinate construct), and execute in spirv_mini_interp. | ||
| 69 | |||
| 70 | Returns {"float": (u32, ...), "uint": (u32, ...), "sint": (u32, ...), "coords": (u32, ...)}. | ||
| 71 | """ | ||
| 72 | spv = support.translate_lines( | ||
| 73 | "ps_img_store_d16_eval", | ||
| 74 | setup_lines + [store_line, "exp mrt0 v0, v0, v0, v0 done vm", "s_endpgm"], | ||
| 75 | ) | ||
| 76 | _validate(spv) | ||
| 77 | |||
| 78 | lines = spv.splitlines() | ||
| 79 | # Find the OpCompositeConstruct lines for %v4float, %v4uint_st, %v4int_st and coords (%v2int/%v3int) | ||
| 80 | # emitted by image_store, plus the first OpSelectionMerge (which starts the active/view branch). | ||
| 81 | v4f = v4u = v4s = coord_id = None | ||
| 82 | coord_ty = None | ||
| 83 | cut_idx = None | ||
| 84 | for idx, ln in enumerate(lines): | ||
| 85 | m = re.match(r"\s*(%\w+)\s*=\s*OpCompositeConstruct\s+(%\w+)\s+(.*)$", ln) | ||
| 86 | if m: | ||
| 87 | res, ty, _ = m.groups() | ||
| 88 | if ty in ("%v2int", "%v3int"): | ||
| 89 | coord_id, coord_ty = res, ty | ||
| 90 | elif ty == "%v4float": | ||
| 91 | v4f = res | ||
| 92 | elif ty == "%v4uint_st": | ||
| 93 | v4u = res | ||
| 94 | elif ty == "%v4int_st": | ||
| 95 | v4s = res | ||
| 96 | cut_idx = idx + 1 | ||
| 97 | break | ||
| 98 | assert v4f and v4u and v4s and cut_idx is not None, "image_store texel constructs not found" | ||
| 99 | |||
| 100 | # Build a small runnable fragment ending right after v4s, writing each vector to an Output. | ||
| 101 | # For 1D coords (no %v2int/%v3int), capture the %int operand of the first OpImageWrite instead. | ||
| 102 | if coord_id is None: | ||
| 103 | for ln in lines[cut_idx:]: | ||
| 104 | m = re.match(r"\s*OpImageWrite\s+%\w+\s+(%\w+)\s+", ln) | ||
| 105 | if m: | ||
| 106 | coord_id = m.group(1) | ||
| 107 | coord_ty = "%int" | ||
| 108 | break | ||
| 109 | |||
| 110 | # Extract header up to %main = OpFunction, add four Output vars, and emit stores at cut_idx. | ||
| 111 | out_decls = [ | ||
| 112 | "%_ptr_Out_f = OpTypePointer Output %v4float", | ||
| 113 | "%_ptr_Out_u = OpTypePointer Output %v4uint_st", | ||
| 114 | "%_ptr_Out_s = OpTypePointer Output %v4int_st", | ||
| 115 | f"%_ptr_Out_c = OpTypePointer Output {coord_ty}", | ||
| 116 | "%cap_f = OpVariable %_ptr_Out_f Output", | ||
| 117 | "%cap_u = OpVariable %_ptr_Out_u Output", | ||
| 118 | "%cap_s = OpVariable %_ptr_Out_s Output", | ||
| 119 | "%cap_c = OpVariable %_ptr_Out_c Output", | ||
| 120 | ] | ||
| 121 | fn_idx = next(i for i, ln in enumerate(lines) if " = OpFunction " in ln) | ||
| 122 | # Strip bindless/image OpVariables and OpDecorate/OpEntryPoint lines irrelevant to the mini-interp, | ||
| 123 | # keeping all types/constants and the function prefix up to cut_idx. | ||
| 124 | prefix = [] | ||
| 125 | skip_hdr = ( | ||
| 126 | "OpTypeImage", "OpTypeSampledImage", "OpTypeSampler", "OpTypeRuntimeArray", "UniformConstant", | ||
| 127 | "StorageBuffer", "Private", "OpConstantComposite", "%stv", "%stimg", "%desc", "%t_desc", | ||
| 128 | "%images", "%samplers", "%itx", "DescriptorSet", "Binding", "RuntimeDescriptorArray", | ||
| 129 | "StorageImage", "NonUniform", | ||
| 130 | ) | ||
| 131 | for ln in lines[:fn_idx]: | ||
| 132 | if re.search(r"OpEntryPoint ", ln): | ||
| 133 | prefix.append('OpEntryPoint Fragment %main "main" %cap_f %cap_u %cap_s %cap_c') | ||
| 134 | elif any(tok in ln for tok in skip_hdr): | ||
| 135 | continue | ||
| 136 | else: | ||
| 137 | prefix.append(ln) | ||
| 138 | body = lines[fn_idx:cut_idx] + [ | ||
| 139 | f"OpStore %cap_f {v4f}", | ||
| 140 | f"OpStore %cap_u {v4u}", | ||
| 141 | f"OpStore %cap_s {v4s}", | ||
| 142 | f"OpStore %cap_c {coord_id}", | ||
| 143 | "OpReturn", | ||
| 144 | "OpFunctionEnd", | ||
| 145 | ] | ||
| 146 | # Remove any OpLoad/OpAccessChain to %desc_tbl (only used for idx, never feeds v4f/v4u/v4s/coords) | ||
| 147 | # by seeding a dummy memory in spirv_mini_interp? Actually %desc_tbl is UniformConstant/StorageBuffer; | ||
| 148 | # filter out instructions in body whose results are not needed by v4f/v4u/v4s/coord_id. | ||
| 149 | mini_spv = "\n".join(prefix + out_decls + body) + "\n" | ||
| 150 | |||
| 151 | # Strip desc_lookup_sgpr instructions that read %desc_tbl (not needed for texel/coord evaluation). | ||
| 152 | # Those live strictly between coords and v4f; remove lines referencing %desc_tbl or %t_desc or %user_data | ||
| 153 | # that aren't dependencies of v4f/v4u/v4s/coord_id. | ||
| 154 | def _prune_to_targets(src_lines: list[str], fn_start: int, targets: set[str]) -> str: | ||
| 155 | header = src_lines[:fn_start] | ||
| 156 | fn_lines = src_lines[fn_start:] | ||
| 157 | # Keep OpFunction, OpLabel, OpVariable, OpStore to %vgpr/%sgpr/%cap_*, and backward deps of targets. | ||
| 158 | needed = set(targets) | ||
| 159 | keep_idx: set[int] = set() | ||
| 160 | for i in range(len(fn_lines) - 1, -1, -1): | ||
| 161 | s = fn_lines[i].split(";", 1)[0].strip() | ||
| 162 | toks = s.split() | ||
| 163 | if not toks: | ||
| 164 | continue | ||
| 165 | if len(toks) >= 3 and toks[1] == "=": | ||
| 166 | res, op, args = toks[0], toks[2], toks[3:] | ||
| 167 | else: | ||
| 168 | res, op, args = None, toks[0], toks[1:] | ||
| 169 | if op in ("OpFunction", "OpLabel", "OpVariable", "OpReturn", "OpFunctionEnd"): | ||
| 170 | keep_idx.add(i) | ||
| 171 | continue | ||
| 172 | if op == "OpStore" and args and ( | ||
| 173 | args[0].startswith("%cap_") or "%vgpr" in fn_lines[i] | ||
| 174 | ): | ||
| 175 | keep_idx.add(i) | ||
| 176 | needed.update(t for t in args if t.startswith("%")) | ||
| 177 | continue | ||
| 178 | # Also keep OpStore into %vgpr pointers | ||
| 179 | if op == "OpStore": | ||
| 180 | # Check if ptr traces back to %vgpr | ||
| 181 | keep_idx.add(i) | ||
| 182 | needed.update(t for t in args if t.startswith("%")) | ||
| 183 | continue | ||
| 184 | if res in needed: | ||
| 185 | keep_idx.add(i) | ||
| 186 | needed.update(t for t in args if t.startswith("%")) | ||
| 187 | # Drop any OpStore that feeds %sgpr from %user_data (not needed here) | ||
| 188 | pruned_fn = [] | ||
| 189 | for i, ln in enumerate(fn_lines): | ||
| 190 | if i in keep_idx and "%desc_tbl" not in ln and "%user_data" not in ln and "%ud" not in ln: | ||
| 191 | pruned_fn.append(ln) | ||
| 192 | return "\n".join(header + pruned_fn) + "\n" | ||
| 193 | |||
| 194 | pruned = _prune_to_targets( | ||
| 195 | prefix + out_decls + body, | ||
| 196 | len(prefix + out_decls), | ||
| 197 | {v4f, v4u, v4s, coord_id}, | ||
| 198 | ) | ||
| 199 | outs = si.run_fragment(pruned) | ||
| 200 | |||
| 201 | def _as_words(v): | ||
| 202 | if isinstance(v, tuple): | ||
| 203 | return tuple(x & 0xFFFFFFFF for x in v) | ||
| 204 | return (v & 0xFFFFFFFF,) | ||
| 205 | |||
| 206 | return { | ||
| 207 | "float": _as_words(outs["%cap_f"]), | ||
| 208 | "uint": _as_words(outs["%cap_u"]), | ||
| 209 | "sint": _as_words(outs["%cap_s"]), | ||
| 210 | "coords": _as_words(outs["%cap_c"]), | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | class ImageStoreD16Tests(unittest.TestCase): | ||
| 215 | def test_rgba_d16_a16_float_uint_sint_values_exact(self): | ||
| 216 | # FSPX pattern: image_store 2D a16 d16 with full dmask:0xf (2 packed VGPRs: v[4:5], 1 coord VGPR: v2). | ||
| 217 | # Test positive/negative normals, subnormal, negative integer (sign-extended in SINT, zero-extended in UINT). | ||
| 218 | lo0 = _f16_bits(1.5) # 0x3E00 | ||
| 219 | hi0 = _f16_bits(-2.25) # 0xC080 -> u16 49280, i16 -16256 (0xFFFFC080) | ||
| 220 | lo1 = 0xFFFF # f16 NaN (checked via u16/i16), u16 65535, i16 -1 (0xFFFFFFFF) | ||
| 221 | hi1 = 0x8000 # f16 -0.0 (0x80000000), u16 32768, i16 -32768 (0xFFFF8000) | ||
| 222 | w0 = _pack_u16(lo0, hi0) | ||
| 223 | w1 = _pack_u16(lo1, hi1) | ||
| 224 | coord_packed = _pack_u16(17, 42) | ||
| 225 | got = _eval_image_store_texels( | ||
| 226 | [ | ||
| 227 | f"v_mov_b32_e32 v4, 0x{w0:08x}", | ||
| 228 | f"v_mov_b32_e32 v5, 0x{w1:08x}", | ||
| 229 | f"v_mov_b32_e32 v2, 0x{coord_packed:08x}", | ||
| 230 | ], | ||
| 231 | "image_store v[4:5], v2, s[8:15] dmask:0xf dim:SQ_RSRC_IMG_2D unorm a16 d16", | ||
| 232 | ) | ||
| 233 | self.assertEqual(got["coords"], (17, 42)) | ||
| 234 | self.assertEqual(got["float"][0], _f32_bits(1.5)) | ||
| 235 | self.assertEqual(got["float"][1], _f32_bits(-2.25)) | ||
| 236 | self.assertEqual(got["float"][3], 0x80000000) # -0.0 | ||
| 237 | self.assertEqual(got["uint"], (lo0, hi0, lo1, hi1)) | ||
| 238 | self.assertEqual(got["sint"], (0x00003E00, 0xFFFFC080, 0xFFFFFFFF, 0xFFFF8000)) | ||
| 239 | |||
| 240 | def test_sparse_dmasks_pack_enabled_components_consecutively(self): | ||
| 241 | # dmask:0x9 (R and A): both halves packed in a SINGLE VGPR v4 (R = low 16, A = high 16); G=B=0. | ||
| 242 | w_ra = _pack_u16(_f16_bits(0.5), _f16_bits(-4.0)) | ||
| 243 | got_ra = _eval_image_store_texels( | ||
| 244 | [ | ||
| 245 | f"v_mov_b32_e32 v4, 0x{w_ra:08x}", | ||
| 246 | "v_mov_b32_e32 v2, 3", | ||
| 247 | "v_mov_b32_e32 v3, 9", | ||
| 248 | ], | ||
| 249 | "image_store v4, v[2:3], s[8:15] dmask:0x9 dim:SQ_RSRC_IMG_2D unorm d16", | ||
| 250 | ) | ||
| 251 | self.assertEqual(got_ra["coords"], (3, 9)) | ||
| 252 | self.assertEqual(got_ra["float"], (_f32_bits(0.5), 0, 0, _f32_bits(-4.0))) | ||
| 253 | self.assertEqual(got_ra["uint"], (_f16_bits(0.5), 0, 0, _f16_bits(-4.0))) | ||
| 254 | self.assertEqual(got_ra["sint"], (0x00003800, 0, 0, 0xFFFFC400)) | ||
| 255 | |||
| 256 | # dmask:0x7 (R, G, B): 2 VGPRs v[4:5] -> R=v4.lo, G=v4.hi, B=v5.lo, A=0. | ||
| 257 | w0 = _pack_u16(0x0007, 0xFFFE) # +7, -2 | ||
| 258 | w1 = _pack_u16(0x8001, 0x1234) # -32767 (high half 0x1234 unused!) | ||
| 259 | got_rgb = _eval_image_store_texels( | ||
| 260 | [ | ||
| 261 | f"v_mov_b32_e32 v4, 0x{w0:08x}", | ||
| 262 | f"v_mov_b32_e32 v5, 0x{w1:08x}", | ||
| 263 | "v_mov_b32_e32 v2, 1", | ||
| 264 | "v_mov_b32_e32 v3, 2", | ||
| 265 | ], | ||
| 266 | "image_store v[4:5], v[2:3], s[8:15] dmask:0x7 dim:SQ_RSRC_IMG_2D unorm d16", | ||
| 267 | ) | ||
| 268 | self.assertEqual(got_rgb["uint"], (0x0007, 0xFFFE, 0x8001, 0)) | ||
| 269 | self.assertEqual(got_rgb["sint"], (0x00000007, 0xFFFFFFFE, 0xFFFF8001, 0)) | ||
| 270 | |||
| 271 | # dmask:0x4 (B only): 1 VGPR v4 -> B=v4.lo, R=G=A=0. | ||
| 272 | got_b = _eval_image_store_texels( | ||
| 273 | [ | ||
| 274 | "v_mov_b32_e32 v4, 0x12343c00", | ||
| 275 | "v_mov_b32_e32 v2, 5", | ||
| 276 | ], | ||
| 277 | "image_store v4, v2, s[8:15] dmask:0x4 dim:SQ_RSRC_IMG_1D unorm d16", | ||
| 278 | ) | ||
| 279 | self.assertEqual(got_b["coords"], (5,)) | ||
| 280 | self.assertEqual(got_b["float"], (0, 0, _f32_bits(1.0), 0)) | ||
| 281 | self.assertEqual(got_b["uint"], (0, 0, 0x3C00, 0)) | ||
| 282 | |||
| 283 | def test_all_store_dims_a16_glc_and_wave64_emulate_validate(self): | ||
| 284 | cases = [ | ||
| 285 | ("1D", "v2", "dmask:0x1", "d16"), | ||
| 286 | ("2D", "v2", "dmask:0xf", "a16 d16"), | ||
| 287 | ("2D", "v[2:3]", "dmask:0x3", "glc d16"), | ||
| 288 | ("2D_ARRAY", "v[2:3]", "dmask:0xf", "a16 d16"), | ||
| 289 | ("3D", "v[2:4]", "dmask:0x7", "glc d16"), | ||
| 290 | ("2D", "v2", "dmask:0x0", "a16 d16"), | ||
| 291 | ] | ||
| 292 | for dim, addr, dmask, extra in cases: | ||
| 293 | with self.subTest(dim=dim, dmask=dmask, extra=extra): | ||
| 294 | spv = support.translate_lines( | ||
| 295 | f"ps_img_d16_{dim.lower()}", | ||
| 296 | [ | ||
| 297 | "v_mov_b32_e32 v2, 0", | ||
| 298 | "v_mov_b32_e32 v3, 0", | ||
| 299 | "v_mov_b32_e32 v4, 0", | ||
| 300 | "v_mov_b32_e32 v5, 0", | ||
| 301 | f"image_store v[4:5], {addr}, s[8:15] {dmask} dim:SQ_RSRC_IMG_{dim} unorm {extra}", | ||
| 302 | "exp mrt0 v4, v4, v4, v4 done vm", | ||
| 303 | "s_endpgm", | ||
| 304 | ], | ||
| 305 | ) | ||
| 306 | _validate(spv) | ||
| 307 | self.assertIn("OpImageWrite", spv) | ||
| 308 | if "glc" in extra: | ||
| 309 | self.assertIn("Coherent", spv) | ||
| 310 | |||
| 311 | # Also validate a wave64 compute shader under --wave64-emulate (FSPX compute path). | ||
| 312 | with tempfile.TemporaryDirectory() as d: | ||
| 313 | os.makedirs(os.path.join(d, "disasm")) | ||
| 314 | os.makedirs(os.path.join(d, "fspx")) | ||
| 315 | s_path = os.path.join(d, "disasm", "cs_img_store_d16.s") | ||
| 316 | with open(s_path, "w", encoding="utf-8", newline="\n") as f: | ||
| 317 | f.write("; cs_img_store_d16 code 0x0..0x40, gfx1013, 0 invalid words\n") | ||
| 318 | for ln in ( | ||
| 319 | "s_mov_b64 s[16:17], exec", | ||
| 320 | "v_mov_b32_e32 v2, 0x00020001", | ||
| 321 | "v_mov_b32_e32 v4, 0x40003c00", | ||
| 322 | "v_mov_b32_e32 v5, 0x44004200", | ||
| 323 | "image_store v[4:5], v2, s[8:15] dmask:0xf dim:SQ_RSRC_IMG_2D unorm a16 d16", | ||
| 324 | "s_endpgm", | ||
| 325 | ): | ||
| 326 | f.write(f"\t{ln} ; encoding: [0x00,0x00,0x00,0x00]\n") | ||
| 327 | with open(os.path.join(d, "fspx", "cs_img_store_d16.json"), "w", encoding="utf-8", newline="\n") as f: | ||
| 328 | json.dump({"rsrc2": (16 << 1), "threads": [64, 1, 1]}, f) | ||
| 329 | old_w64 = rdna2spirv.WAVE64_EMULATE | ||
| 330 | try: | ||
| 331 | rdna2spirv.WAVE64_EMULATE = True | ||
| 332 | spv = rdna2spirv.translate(s_path) | ||
| 333 | finally: | ||
| 334 | rdna2spirv.WAVE64_EMULATE = old_w64 | ||
| 335 | _validate(spv) | ||
| 336 | |||
| 337 | def test_unsupported_dims_and_modifiers_still_refused(self): | ||
| 338 | for bad in ( | ||
| 339 | "image_store v[4:5], v[2:4], s[8:15] dmask:0xf dim:SQ_RSRC_IMG_CUBE unorm d16", | ||
| 340 | "image_store v[4:5], v2, s[8:15] dmask:0xf dim:SQ_RSRC_IMG_2D unorm d16 tfe", | ||
| 341 | "image_store v[4:5], v2, s[8:15] dmask:0xf dim:SQ_RSRC_IMG_2D unorm d16 lwe", | ||
| 342 | ): | ||
| 343 | with self.subTest(bad=bad): | ||
| 344 | with self.assertRaises(rdna2spirv.Unsupported): | ||
| 345 | support.translate_lines( | ||
| 346 | "ps_img_d16_bad", | ||
| 347 | ["v_mov_b32_e32 v2, 0", bad, "exp mrt0 v2, v2, v2, v2 done vm", "s_endpgm"], | ||
| 348 | ) | ||
| 349 | |||
| 350 | |||
| 351 | if __name__ == "__main__": | ||
| 352 | unittest.main() | ||
diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index a91102192..a0ad3b343 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py | |||
| @@ -10508,8 +10508,10 @@ class Gen: | |||
| 10508 | def image_store(self, a, mods, dim): | 10508 | def image_store(self, a, mods, dim): |
| 10509 | """image_store vData, vAddr, T#: write the dmask components to a storage image. The view is picked at run | 10509 | """image_store vData, vAddr, T#: write the dmask components to a storage image. The view is picked at run |
| 10510 | time from T# word1 FORMAT [28:20]: *_UINT -> uint view, *_SINT -> sint view, anything else -> float view | 10510 | time from T# word1 FORMAT [28:20]: *_UINT -> uint view, *_SINT -> sint view, anything else -> float view |
| 10511 | (UNORM/SNORM/FLOAT convert from the float bits the shader wrote). Missing components are 0.""" | 10511 | (UNORM/SNORM/FLOAT convert from the float bits the shader wrote). Missing components are 0. |
| 10512 | if dim not in self.STORE_DIMS or "d16" in mods: | 10512 | With d16, enabled components are packed two 16-bit values per consecutive VGPR (low half first): IEEE f16 |
| 10513 | widened to f32 for float/norm views, zero-extended u16 for UINT views, and sign-extended i16 for SINT views.""" | ||
| 10514 | if dim not in self.STORE_DIMS or any(m in mods for m in ("tfe", "lwe")): | ||
| 10513 | raise Unsupported("image_store " + dim.split("SQ_RSRC_IMG_")[-1] + (" a16" if "a16" in mods else "") | 10515 | raise Unsupported("image_store " + dim.split("SQ_RSRC_IMG_")[-1] + (" a16" if "a16" in mods else "") |
| 10514 | + (" d16" if "d16" in mods else "")) | 10516 | + (" d16" if "d16" in mods else "")) |
| 10515 | di, _, _, nc = self.STORE_DIMS[dim] | 10517 | di, _, _, nc = self.STORE_DIMS[dim] |
| @@ -10518,14 +10520,48 @@ class Gen: | |||
| 10518 | dm = next((m for m in mods if m.startswith("dmask:")), "dmask:0xf") | 10520 | dm = next((m for m in mods if m.startswith("dmask:")), "dmask:0xf") |
| 10519 | dmask = int(dm.split(":")[1], 0) | 10521 | dmask = int(dm.split(":")[1], 0) |
| 10520 | vdata, addr = a[0], self.addr_regs(a[1]) | 10522 | vdata, addr = a[0], self.addr_regs(a[1]) |
| 10523 | if vdata.get("k") != "vgpr": | ||
| 10524 | raise Unsupported("image_store vdata") | ||
| 10521 | raw = [self.load_reg("vgpr", r) for r in addr] | 10525 | raw = [self.load_reg("vgpr", r) for r in addr] |
| 10522 | if "a16" in mods: | 10526 | if "a16" in mods: |
| 10523 | raw = self.unpack_image_a16_uints(raw) | 10527 | raw = self.unpack_image_a16_uints(raw) |
| 10524 | if len(raw) < nc: | 10528 | if len(raw) < nc: |
| 10525 | raise Unsupported("image_store coords") | 10529 | raise Unsupported("image_store coords") |
| 10526 | # The enabled components come from consecutive VGPRs, in component order (dmask 0x9: R = vD, A = vD+1). | 10530 | if "d16" in mods: |
| 10527 | regs = iter(range(vdata["r"], vdata["r"] + 4)) | 10531 | # Packed 16-bit data: enabled dmask components occupy consecutive 16-bit halves (low first) across |
| 10528 | vals = [self.load_reg("vgpr", next(regs)) if dmask >> c & 1 else self.cu(0) for c in range(4)] | 10532 | # ceil(popcount(dmask) / 2) VGPRs starting at vData. |
| 10533 | n_en = sum((dmask >> c) & 1 for c in range(4)) | ||
| 10534 | d16_regs = [self.load_reg("vgpr", vdata["r"] + i) for i in range((n_en + 1) // 2)] | ||
| 10535 | f_pairs = [] | ||
| 10536 | for reg in d16_regs: | ||
| 10537 | v2 = self.tmp() | ||
| 10538 | self.emit(f"{v2} = OpExtInst %v2float %glsl UnpackHalf2x16 {reg}") | ||
| 10539 | f_pairs.append(v2) | ||
| 10540 | fv, uv, sv = [], [], [] | ||
| 10541 | pos = 0 | ||
| 10542 | for c in range(4): | ||
| 10543 | if (dmask >> c) & 1: | ||
| 10544 | ri, hi = pos >> 1, pos & 1 | ||
| 10545 | reg = d16_regs[ri] | ||
| 10546 | fc, uc, sc = self.tmp(), self.tmp(), self.tmp() | ||
| 10547 | self.emit(f"{fc} = OpCompositeExtract %float {f_pairs[ri]} {hi}") | ||
| 10548 | self.emit(f"{uc} = OpBitFieldUExtract %uint {reg} {self.cu(16 * hi)} {self.cu(16)}") | ||
| 10549 | self.emit(f"{sc} = OpBitFieldSExtract %uint {reg} {self.cu(16 * hi)} {self.cu(16)}") | ||
| 10550 | fv.append(fc) | ||
| 10551 | uv.append(uc) | ||
| 10552 | sv.append(self.as_int(sc)) | ||
| 10553 | pos += 1 | ||
| 10554 | else: | ||
| 10555 | fv.append(self.tof(self.cu(0))) | ||
| 10556 | uv.append(self.cu(0)) | ||
| 10557 | sv.append(self.as_int(self.cu(0))) | ||
| 10558 | else: | ||
| 10559 | # The enabled components come from consecutive VGPRs, in component order (dmask 0x9: R = vD, A = vD+1). | ||
| 10560 | regs = iter(range(vdata["r"], vdata["r"] + 4)) | ||
| 10561 | vals = [self.load_reg("vgpr", next(regs)) if dmask >> c & 1 else self.cu(0) for c in range(4)] | ||
| 10562 | fv = [self.tof(v) for v in vals] | ||
| 10563 | uv = vals | ||
| 10564 | sv = [self.as_int(v) for v in vals] | ||
| 10529 | coords = [self.as_int(c) for c in raw[:nc]] | 10565 | coords = [self.as_int(c) for c in raw[:nc]] |
| 10530 | idx = self.desc_lookup_sgpr(a[2], 8) | 10566 | idx = self.desc_lookup_sgpr(a[2], 8) |
| 10531 | t1 = self.sgpr_word(a[2], 1) | 10567 | t1 = self.sgpr_word(a[2], 1) |
| @@ -10538,11 +10574,9 @@ class Gen: | |||
| 10538 | iv = self.tmp() | 10574 | iv = self.tmp() |
| 10539 | self.emit(f"{iv} = OpCompositeConstruct {'%v2int' if nc == 2 else '%v3int'} {' '.join(coords)}") | 10575 | self.emit(f"{iv} = OpCompositeConstruct {'%v2int' if nc == 2 else '%v3int'} {' '.join(coords)}") |
| 10540 | is_u, is_s = self.fmt_int_flags(t1) | 10576 | is_u, is_s = self.fmt_int_flags(t1) |
| 10541 | fv = [self.tof(v) for v in vals] | ||
| 10542 | sv = [self.as_int(v) for v in vals] | ||
| 10543 | v4f, v4u, v4s = self.tmp(), self.tmp(), self.tmp() | 10577 | v4f, v4u, v4s = self.tmp(), self.tmp(), self.tmp() |
| 10544 | self.emit(f"{v4f} = OpCompositeConstruct %v4float {' '.join(fv)}") | 10578 | self.emit(f"{v4f} = OpCompositeConstruct %v4float {' '.join(fv)}") |
| 10545 | self.emit(f"{v4u} = OpCompositeConstruct %v4uint_st {' '.join(vals)}") | 10579 | self.emit(f"{v4u} = OpCompositeConstruct %v4uint_st {' '.join(uv)}") |
| 10546 | self.emit(f"{v4s} = OpCompositeConstruct %v4int_st {' '.join(sv)}") | 10580 | self.emit(f"{v4s} = OpCompositeConstruct %v4int_st {' '.join(sv)}") |
| 10547 | act = self.active_b() | 10581 | act = self.active_b() |
| 10548 | L = {k: self.tmp() + "_" + k for k in ("do", "end", "s", "rest", "e2", "u", "f", "e3")} | 10582 | L = {k: self.tmp() + "_" + k for k in ("do", "end", "s", "rest", "e2", "u", "f", "e3")} |