"""Semantic interpreter tests for buffer_load_format_* (the V# FORMAT decode in tools/rdna2spirv.py). The element is read from PhysicalStorageBuffer memory at V#.base + offset. An element under 4 bytes starts at a byte shift inside its first dword, and the translation merges dword 1 above it: (w0 >> bsh) | (w1 << (32 - bsh)), picking w0 when bsh is 0. The interpreter rejects shifts of 32 or more (undefined in SPIR-V, even on the discarded side of an OpSelect), so every dword-aligned load here also checks that the merge keeps its shift below 32. Integer and f16 formats are used: their decode is exact, so the expected words need no rounding model. """ from __future__ import annotations import os import struct import sys import tempfile import unittest import warnings HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(os.path.dirname(HERE)) TOOLS_DIR = os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools") sys.dont_write_bytecode = True sys.path.insert(0, TOOLS_DIR) sys.path.insert(0, HERE) import rdna2spirv # noqa: E402 import spirv_mini_interp as si # noqa: E402 STUB_DXBC = b"DXBC" + bytes(24) + struct.pack(" tuple: """buffer_load_format_ of format `fmt` at byte `offset`; returns the four mrt0 words.""" if _TMP_DIR is None: setUpModule() suffix = "xyzw"[:ncomp] vdata = "v2" if ncomp == 1 else f"v[2:{1 + ncomp}]" regs = [f"v{2 + min(i, ncomp - 1)}" for i in range(4)] asm = ( "; test_buffer_load_format\n" f"\ts_mov_b32 s0, {BASE:#x}\n" "\ts_mov_b32 s1, 0\n" # base high bits 0, stride 0 (raw) "\ts_mov_b32 s2, 0x100\n" # num_records (bytes) f"\ts_mov_b32 s3, {fmt << 12 | dst_sel:#x}\n" f"\tv_mov_b32 v1, {offset:#x}\n" f"\tbuffer_load_format_{suffix} {vdata}, v1, s[0:3], 0 offen\n" "\ts_waitcnt vmcnt(0)\n" f"\texp mrt0 {', '.join(regs)} done vm\n" "\ts_endpgm\n" ) with open(_SRC_PATH, "w", encoding="utf-8") as f: f.write(asm) with warnings.catch_warnings(): warnings.simplefilter("ignore", ResourceWarning) spv = rdna2spirv.translate(_SRC_PATH) return si.run_fragment(spv, memory=dict(memory if memory is not None else MEMORY))["%out_mrt0"] def f32_bits(x: float) -> int: return struct.unpack(" int: return struct.unpack("