|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build selected hash-bound non-point vertex variants for the iOS Metal 3 fallback.""" |
| 3 | + |
| 4 | +import argparse |
| 5 | +import hashlib |
| 6 | +import json |
| 7 | +from pathlib import Path |
| 8 | +import re |
| 9 | +import subprocess |
| 10 | +from tempfile import TemporaryDirectory |
| 11 | + |
| 12 | + |
| 13 | +def digest(data: bytes) -> str: |
| 14 | + return hashlib.sha256(data).hexdigest() |
| 15 | + |
| 16 | + |
| 17 | +def build(sources: Path, manifest: Path, libraries: Path, keys: Path, output: Path) -> int: |
| 18 | + doc = json.loads(manifest.read_text()) |
| 19 | + chain = doc["chain"] |
| 20 | + if doc["schema"] != 1 or not re.fullmatch(r"[0-9a-f]{12}", chain): |
| 21 | + raise ValueError("Invalid source manifest version or chain") |
| 22 | + if libraries.name != f"b-{chain}" or output.exists() or output.is_symlink(): |
| 23 | + raise ValueError("Use the exact base library chain and a new output directory") |
| 24 | + selected = sorted(set(keys.read_text().split())) |
| 25 | + if not selected or any(not re.fullmatch(r"[0-9a-f]{16}_b", key) for key in selected): |
| 26 | + raise ValueError("Expected a nonempty list of vertex shader keys") |
| 27 | + |
| 28 | + flags = ["-target", "air64-apple-ios16.1", "-std=metal3.0", "-fno-fast-math", |
| 29 | + "-fpreserve-invariance"] |
| 30 | + records = {} |
| 31 | + with TemporaryDirectory(prefix=".nonpoint-metal3-", dir=output.parent) as directory: |
| 32 | + tmp = Path(directory) |
| 33 | + stage = tmp / "published" |
| 34 | + stage.mkdir() |
| 35 | + for key in selected: |
| 36 | + record = doc["shaders"][key] |
| 37 | + source = (sources / (key + ".metal")).read_bytes() |
| 38 | + base = (libraries / (key + ".metallib")).read_bytes() |
| 39 | + if digest(source) != record["source_sha256"] or digest(base) != record["library_sha256"]: |
| 40 | + raise ValueError(f"Source or base library hash mismatch for {key}") |
| 41 | + needle = b"[[point_size]]" |
| 42 | + if needle not in source: |
| 43 | + raise ValueError(f"No point-size output for {key}") |
| 44 | + effective = source.replace(needle, b"[[user(dm_unused_point_size)]]") |
| 45 | + msl, air, lib = tmp / (key + ".metal"), tmp / (key + ".air"), stage / (key + ".metallib") |
| 46 | + msl.write_bytes(effective) |
| 47 | + subprocess.run(["xcrun", "-sdk", "iphoneos", "metal", *flags, |
| 48 | + f"-fmodules-cache-path={tmp / 'modules'}", "-c", msl, "-o", air], check=True) |
| 49 | + subprocess.run(["xcrun", "-sdk", "iphoneos", "metallib", air, "-o", lib], check=True) |
| 50 | + data = lib.read_bytes() |
| 51 | + if not data.startswith(b"MTLB"): |
| 52 | + raise ValueError(f"Invalid metallib for {key}") |
| 53 | + records[key] = {**record, "effective_source_sha256": digest(effective), |
| 54 | + "offline_sha256": digest(data)} |
| 55 | + msl.unlink() |
| 56 | + air.unlink() |
| 57 | + (stage / "manifest.json").write_text( |
| 58 | + json.dumps({"schema": 1, "chain": chain, "shaders": records}, sort_keys=True) + "\n" |
| 59 | + ) |
| 60 | + stage.rename(output) |
| 61 | + return len(records) |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + parser = argparse.ArgumentParser(description=__doc__) |
| 66 | + parser.add_argument("--sources", type=Path, required=True, help="Exported non-point source directory") |
| 67 | + parser.add_argument("--manifest", type=Path, help="Defaults to the source directory's manifest.json") |
| 68 | + parser.add_argument("--libraries", type=Path, required=True, help="Exact b-<chain> directory") |
| 69 | + parser.add_argument("--keys", type=Path, required=True, help="Whitespace-separated vertex shader keys") |
| 70 | + parser.add_argument("--output", type=Path, required=True, help="New nonpoint-metal3-v1 directory") |
| 71 | + args = parser.parse_args() |
| 72 | + try: |
| 73 | + count = build(args.sources, args.manifest or args.sources / "manifest.json", |
| 74 | + args.libraries, args.keys, args.output) |
| 75 | + except (ValueError, OSError, KeyError, subprocess.CalledProcessError) as error: |
| 76 | + parser.exit(1, f"{error}\n") |
| 77 | + print(f"Built {count} Metal 3 non-point variants") |
0 commit comments