#!/usr/bin/env python3 """gpu/vk/tex_pack_bc7.h: Evil HD's texture pack as BC7, prepared once (vk_texture.cpp texpack). Checks the prepared file's header and sizes, the levels, the 2 x 2 halving, the tiers by the card's memory, and that the encoder writes BC7 mode 6 blocks that keep a flat colour exactly. Compiles a small harness with the C++ compiler given. python -I -B tests/test_tex_pack_bc7.py [--cxx g++] """ import argparse import os import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent RUN_ALL_ARGS = ["--cxx", "g++"] HARNESS = r''' #include "tex_pack_bc7.h" #include static int failures; #define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %d: %s\n", __LINE__, #x); failures++; } } while (0) namespace tb = mkwvk::tex_pack_bc7; // (mode 6: bit 6 of the first byte; the endpoints and p-bits of a flat block decode to its colour at every index) static void decode6(const uint8_t b[16], uint8_t rgba[4]) { uint64_t w[2]; std::memcpy(w, b, 16); auto bits = [&](int pos, int n) -> uint32_t { uint64_t v = pos < 64 ? (w[0] >> pos) | (pos + n > 64 ? w[1] << (64 - pos) : 0) : w[1] >> (pos - 64); return (uint32_t)(v & ((1ull << n) - 1)); }; int pos = 7, e[2][4]; for (int c = 0; c < 4; c++) e[0][c] = bits(pos, 7), pos += 7, e[1][c] = bits(pos, 7), pos += 7; const int p0 = bits(pos, 1), p1 = bits(pos + 1, 1); pos += 2; const int idx = bits(pos, 3); static const int kw[16] = {0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64}; for (int c = 0; c < 4; c++) { const int a = e[0][c] * 2 + p0, z = e[1][c] * 2 + p1; rgba[c] = (uint8_t)(((64 - kw[idx]) * a + kw[idx] * z + 32) >> 6); } } int main() { // sizes and levels CHECK(tb::level_bytes(4, 4) == 16 && tb::level_bytes(5, 5) == 64 && tb::level_bytes(1, 1) == 16 && tb::level_bytes(2048, 2048) == 4194304); CHECK(tb::full_levels(2048, 2048) == 12 && tb::full_levels(682, 682) == 10 && tb::full_levels(1, 1) == 1 && tb::full_levels(8, 2) == 4); CHECK(tb::file_bytes(8, 8, 4) == tb::kHeader + 64 + 16 + 16 + 16); // the header uint8_t buf[256] = {}; tb::Header h; h.w = 8, h.h = 8, h.levels = 4, h.png_size = 123456789012ull; tb::write_header(buf, h); tb::Header r; CHECK(tb::read_header(buf, (size_t)tb::file_bytes(8, 8, 4), &r) && r.w == 8 && r.h == 8 && r.levels == 4 && r.png_size == 123456789012ull); CHECK(!tb::read_header(buf, (size_t)tb::file_bytes(8, 8, 4) - 1, &r)); // (cut short) CHECK(!tb::read_header(buf, 10, &r)); h.levels = 5; // (more levels than 8 x 8 has) tb::write_header(buf, h); CHECK(!tb::read_header(buf, (size_t)tb::file_bytes(8, 8, 4), &r)); buf[0] ^= 1; CHECK(!tb::read_header(buf, sizeof buf, &r)); // halving: the mean of 2 x 2, an odd edge's last texel repeated std::vector px(3 * 3 * 4); for (size_t i = 0; i < px.size(); i++) px[i] = (uint8_t)(i * 7); uint32_t ow = 0, oh = 0; const auto hv = tb::half(px, 3, 3, &ow, &oh); CHECK(ow == 1 && oh == 1 && hv.size() == 4); CHECK(hv[0] == (uint8_t)((0 + 4 * 7 + 12 * 7 + 16 * 7 + 2) / 4)); // the encoder: mode 6, a flat colour kept std::vector flat(8 * 8 * 4); for (size_t i = 0; i < flat.size(); i += 4) flat[i] = 200, flat[i + 1] = 100, flat[i + 2] = 50, flat[i + 3] = 255; std::vector out((size_t)tb::level_bytes(8, 8)); tb::encode(flat.data(), 8, 8, out.data()); for (int b = 0; b < 4; b++) { CHECK(out[b * 16] == 0x40); uint8_t c[4]; decode6(&out[b * 16], c); CHECK(std::abs(c[0] - 200) <= 1 && std::abs(c[1] - 100) <= 1 && std::abs(c[2] - 50) <= 1 && c[3] >= 254); } // the tiers, by the card's own memory CHECK(tb::tier_for(12282) == tb::kAll && tb::tier_for(16376) == tb::kAll && tb::tier_for(11000) == tb::kAll); CHECK(tb::tier_for(10240) == tb::kNoBig && tb::tier_for(8192) == tb::kNoBig && tb::tier_for(6144) == tb::kSmall && tb::tier_for(4096) == tb::kNone); CHECK(tb::tier_wants(tb::kAll, 1024, 1024) && tb::tier_wants(tb::kNoBig, 512, 1023) && !tb::tier_wants(tb::kNoBig, 1024, 256)); CHECK(tb::tier_wants(tb::kSmall, 511, 256) && !tb::tier_wants(tb::kSmall, 512, 256) && !tb::tier_wants(tb::kNone, 256, 256)); CHECK(tb::tier_env("3") == 3 && tb::tier_env("0") == 0 && tb::tier_env(nullptr) == -1 && tb::tier_env("") == -1 && tb::tier_env("4") == -1 && tb::tier_env("12") == -1); std::fprintf(stderr, "%s\n", failures ? "failed" : "ok"); return failures ? 1 : 0; } ''' def check_source(): tex = (ROOT / "gpu/vk/vk_texture.cpp").read_text(encoding="utf-8") problems = [] for needle, why in ( ('#include "tex_pack_bc7.h"', "vk_texture.cpp does not include the BC7 pack header"), ("if (tex->pack_state == 1 || !texpack::on() || !texpack::ready.load(std::memory_order_acquire)) return VK_NULL_HANDLE;", "pack_view uses the pack before it is prepared (textures would turn HD one by one)"), ("ci.format = VK_FORMAT_BC7_UNORM_BLOCK;", "the pack's images are not BC7"), ("tb::tier_wants((tb::Tier)tier, fw, fh)", "pack_view does not take the tier"), ("if (now) texpack::prepare_start();", "the prepare does not start when Evil HD is on"), ("tb::tier_for(heap >> 20)", "the tier is not by the card's own memory"), ("? (uint64_t)std::clamp(atoi(getenv(\"RX_VK_TEX_PACK_LINE\")), 20, 98) : 95;", "the emergency line is not 95% by default")): if needle not in tex: problems.append(why) for gone in ("texpack::cap", "kPackBudget", "make_line", "vkCmdBlitImage(cmd, tex->hd"): if gone in tex: problems.append("vk_texture.cpp still has the old pack budget or blits: " + gone) menu = (ROOT / "gpu/vk/vk_menu.cpp").read_text(encoding="utf-8") if "texpack_prepare_text(t, sizeof t)" not in menu: problems.append("the menu does not show the prepare's progress") return problems def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument("--cxx", default=os.environ.get("CXX", "g++")) a = p.parse_args() problems = check_source() with tempfile.TemporaryDirectory(prefix="tex-pack-bc7-") as tmp: src, exe = Path(tmp) / "t.cpp", Path(tmp) / "t" src.write_text(HARNESS, encoding="utf-8") build = subprocess.run([a.cxx, "-std=c++17", "-O1", "-Wall", "-Wextra", "-Werror", "-I", str(ROOT / "gpu/vk"), str(src), "-o", str(exe)]) if build.returncode: raise SystemExit("the harness did not compile: see the compiler's output above") run = subprocess.run([str(exe)], capture_output=True, text=True) sys.stderr.write(run.stderr) if run.returncode: problems.append("the harness failed (above)") for pr in problems: print("FAIL: " + pr) if problems: return 1 print("PASS: the pack's BC7 files, levels, halving and encoder, and the tiers by the card") return 0 if __name__ == "__main__": sys.exit(main())