# ETC fixtures (tools/fixtures.py): random ETC1 / ETC2 / EAC blocks, sorted by mode so that every ETC2 mode # (individual, differential, T, H, planar; punch-through opaque and transparent) and every EAC multiplier case is # covered, decoded by Godot 4 (etcpak's decoder, through Image.decompress). # tests/gen/etc.luau { { name, kind, w, h, data, godot (rgba8 bytes) } } import importlib.util, os, random _spec = importlib.util.spec_from_file_location("_tex_reference", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_tex_reference.py")) R = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(R) def mode(b, punch): """the ETC2 mode of an 8-byte colour block""" hi = int.from_bytes(b[:4], "big") if not punch and not (hi >> 1) & 1: return "individual" def s3(v): return v - 8 if v >= 4 else v r, g, bb = (hi >> 27) & 31, (hi >> 19) & 31, (hi >> 11) & 31 dr, dg, db = s3((hi >> 24) & 7), s3((hi >> 16) & 7), s3((hi >> 8) & 7) if not 0 <= r + dr <= 31: return "T" if not 0 <= g + dg <= 31: return "H" if not 0 <= bb + db <= 31: return "planar" return "differential" def colour_blocks(rng, n, want, punch=False, opaque=None): out = bytearray() while len(out) < n * 8: b = bytearray(rng.getrandbits(8) for _ in range(8)) if opaque is not None: b[3] = (b[3] & 0xFD) | (2 if opaque else 0) if mode(b, punch) == want: out += b return bytes(out) def eac_blocks(rng, n, zero_mult=False): out = bytearray() for _ in range(n): b = bytearray(rng.getrandbits(8) for _ in range(8)) if zero_mult: b[1] &= 0x0F out += b return bytes(out) EAC = [[-3, -6, -9, -15, 2, 5, 8, 14], [-3, -7, -10, -13, 2, 6, 9, 12], [-2, -5, -8, -13, 1, 4, 7, 12], [-2, -4, -6, -13, 1, 3, 5, 12], [-3, -6, -8, -12, 2, 5, 7, 11], [-3, -7, -9, -11, 2, 6, 8, 10], [-4, -7, -8, -11, 3, 6, 7, 10], [-3, -5, -8, -11, 2, 4, 7, 10], [-2, -6, -8, -10, 1, 5, 7, 9], [-2, -5, -8, -10, 1, 4, 7, 9], [-2, -4, -8, -10, 1, 3, 7, 9], [-2, -5, -7, -10, 1, 4, 6, 9], [-3, -4, -7, -10, 2, 3, 6, 9], [-1, -2, -3, -10, 0, 1, 2, 9], [-4, -6, -8, -9, 3, 5, 7, 8], [-3, -5, -7, -9, 2, 4, 6, 8]] def s11(c): """signed EAC R11 / RG11 -> the 11-bit values (-1023..1023) per pixel and channel, as a flat list""" two = c["kind"] == "eac_rg11s" w, h, data = c["w"], c["h"], c["data"] bw = (w + 3) // 4 out = [0] * (w * h * 2) step = 16 if two else 8 for bi in range(len(data) // step): bx, by = bi % bw, bi // bw for ch in range(2 if two else 1): b = data[bi * step + ch * 8: bi * step + ch * 8 + 8] base = b[0] - 256 if b[0] >= 128 else b[0] base = max(base, -127) mult, tab = b[1] >> 4, EAC[b[1] & 15] bits = int.from_bytes(b[2:], "big") for i in range(16): m = tab[(bits >> (45 - 3 * i)) & 7] v = base * 8 + (m * mult * 8 if mult else m) v = max(-1023, min(1023, v)) x, y = bx * 4 + i // 4, by * 4 + i % 4 if x < w and y < h: out[(y * w + x) * 2 + ch] = v return out def generate(ctx): rng = random.Random(11) cases = [] def add(name, kind, w, h, data): cases.append({"name": name, "kind": kind, "w": w, "h": h, "data": data}) add("etc1 individual", "etc1", 16, 16, colour_blocks(rng, 16, "individual")) add("etc1 differential", "etc1", 16, 16, colour_blocks(rng, 16, "differential")) add("etc1 random 13x6", "etc1", 13, 6, bytes(rng.getrandbits(8) for _ in range(8 * 8))) for m in ("individual", "differential", "T", "H", "planar"): add("etc2_rgb " + m, "etc2_rgb", 16, 16, colour_blocks(rng, 16, m)) for m in ("differential", "T", "H", "planar"): for op in (True, False): add("etc2_rgba1 %s %s" % (m, "opaque" if op else "transparent"), "etc2_rgba1", 16, 16, colour_blocks(rng, 16, m, True, op)) for m in ("individual", "differential", "T", "H", "planar"): rgba = bytearray() col = colour_blocks(rng, 16, m) alpha = eac_blocks(rng, 16, m == "planar") for i in range(16): rgba += alpha[i * 8:i * 8 + 8] + col[i * 8:i * 8 + 8] add("etc2_rgba " + m, "etc2_rgba", 16, 16, bytes(rgba)) for kind in ("eac_r11", "eac_r11s"): add(kind, kind, 16, 16, eac_blocks(rng, 16)) add(kind + " multiplier 0", kind, 8, 8, eac_blocks(rng, 4, True)) for kind in ("eac_rg11", "eac_rg11s"): add(kind, kind, 16, 12, eac_blocks(rng, 24)) add("etc2_rgb 10x5", "etc2_rgb", 10, 5, bytes(rng.getrandbits(8) for _ in range(6 * 8))) # Godot cannot decode RGB A1 or the signed EAC kinds: A1 blocks go through it as ETC2 RGB (the test checks the # punch-through rules on top; the opaque bit is set for Godot, else ETC2 RGB reads the individual mode), signed EAC is checked against s11() below (the KDFS 1.3 rule, written here) jobs, idx = [], [] for i, c in enumerate(cases): k = "etc2_rgb" if c["kind"] == "etc2_rgba1" else c["kind"] if k in ("eac_r11s", "eac_rg11s"): c["spec"] = s11(c) else: data = c["data"] if c["kind"] == "etc2_rgba1": # with the opaque bit set: ETC2 RGB's differential / T / H / planar data = bytes(v | 2 if i % 8 == 3 else v for i, v in enumerate(data)) jobs.append((data, c["w"], c["h"], R.GODOT[k], False)) idx.append(i) for i, out in zip(idx, R.godot(ctx, jobs)): cases[i]["godot"] = out ctx.emit("etc", cases)