#!/bin/sh # Regenerates crates/rune/src/shaders/uber.metallib (wgpu's Metal backend), crates/rune/src/shaders/uber_native.metallib # (Rune's own, `metal-backend`) and crates/rune/src/render/uber/metallib_hash.rs from # crates/rune/src/shaders/{clip,shader}.wgsl using wgpu::naga and Xcode's Metal toolchain, the Windfoil # variant's crates/rune/src/shaders/uber_windfoil{,_native}.metallib and render/uber/windfoil_hash.rs, then # crates/rune-gpu/src/shaders/builtin_native{,_ios,_ios_sim}.metallib and crates/rune-gpu/src/metal/builtin_table.rs: # every other pipeline Rune, rune-fx and rune-light ship, captured from Rune's Metal backend. # App builds embed the committed libraries and do not run xcrun. Needs a Mac with a GPU. set -eu cd "$(dirname "$0")/.." if ! xcrun -sdk macosx --find metal >/dev/null 2>&1; then echo "error: xcrun -sdk macosx metal not found (install Xcode or Metal Toolchain)" >&2 exit 1 fi tmp="$(mktemp -d "${TMPDIR:-/tmp}/rune-metallib.XXXXXX")" trap 'rm -rf "$tmp"' EXIT if ! cargo test --locked -p rune --features metal-backend --lib render::metallib::tests::dump_uber_msl -- --ignored --exact --nocapture >"$tmp/dump.txt" 2>&1; then cat "$tmp/dump.txt" >&2 exit 1 fi python3 - "$tmp/dump.txt" "$tmp" <<'PY' import pathlib, re, sys text = pathlib.Path(sys.argv[1]).read_text() out = pathlib.Path(sys.argv[2]) def extract(tag: str) -> str: m = re.search(rf"---BEGIN_{tag}---\n(.*?)\n?---END_{tag}---", text, re.S) if not m: raise SystemExit(f"missing {tag} in dump output") return m.group(1) (out / "wgsl.blake3").write_text(extract("WGSL_BLAKE3").strip()) (out / "msl.blake3").write_text(extract("MSL_BLAKE3").strip()) (out / "flags.txt").write_text(extract("METAL_FLAGS").strip()) (out / "abi.fnv64").write_text(extract("ABI_FNV64").strip()) (out / "options.fnv64").write_text(extract("OPTIONS_FNV64").strip()) (out / "vs.metal").write_text(extract("VS_MSL") + "\n") (out / "fs.metal").write_text(extract("FS_MSL") + "\n") (out / "native.blake3").write_text(extract("NATIVE_MSL_BLAKE3").strip()) (out / "native_flags.txt").write_text(extract("NATIVE_METAL_FLAGS").strip()) (out / "native_vs.metal").write_text(extract("NATIVE_VS_MSL") + "\n") (out / "native_fs.metal").write_text(extract("NATIVE_FS_MSL") + "\n") (out / "windfoil.blake3").write_text(extract("WINDFOIL_WGSL_BLAKE3").strip()) (out / "windfoil_abi.fnv64").write_text(extract("WINDFOIL_ABI_FNV64").strip()) (out / "windfoil_vs.metal").write_text(extract("WINDFOIL_VS_MSL") + "\n") (out / "windfoil_fs.metal").write_text(extract("WINDFOIL_FS_MSL") + "\n") (out / "windfoil_native_vs.metal").write_text(extract("WINDFOIL_NATIVE_VS_MSL") + "\n") (out / "windfoil_native_fs.metal").write_text(extract("WINDFOIL_NATIVE_FS_MSL") + "\n") PY flags="$(cat "$tmp/flags.txt")" # shellcheck disable=SC2086 xcrun -sdk macosx metal $flags -c "$tmp/vs.metal" -o "$tmp/vs.air" # shellcheck disable=SC2086 xcrun -sdk macosx metal $flags -c "$tmp/fs.metal" -o "$tmp/fs.air" xcrun -sdk macosx metallib "$tmp/vs.air" "$tmp/fs.air" -o crates/rune/src/shaders/uber.metallib native_flags="$(cat "$tmp/native_flags.txt")" # shellcheck disable=SC2086 xcrun -sdk macosx metal $native_flags -c "$tmp/native_vs.metal" -o "$tmp/native_vs.air" # shellcheck disable=SC2086 xcrun -sdk macosx metal $native_flags -c "$tmp/native_fs.metal" -o "$tmp/native_fs.air" xcrun -sdk macosx metallib "$tmp/native_vs.air" "$tmp/native_fs.air" -o crates/rune/src/shaders/uber_native.metallib python3 - "$tmp" crates/rune/src/shaders/uber.metallib crates/rune/src/render/uber/metallib_hash.rs crates/rune/src/shaders/uber_native.metallib <<'PY' import pathlib, sys tmp = pathlib.Path(sys.argv[1]) metallib_path = pathlib.Path(sys.argv[2]) hash_rs_path = pathlib.Path(sys.argv[3]) wgsl_hash = (tmp / "wgsl.blake3").read_text().strip() msl_hash = (tmp / "msl.blake3").read_text().strip() native_hash = (tmp / "native.blake3").read_text().strip() data = metallib_path.read_bytes() def fnv64(data): h = 0xcbf29ce484222325 for b in data: h = ((h ^ b) * 0x100000001b3) & 0xFFFFFFFFFFFFFFFF return h h = fnv64(data) native_h = fnv64(pathlib.Path(sys.argv[4]).read_bytes()) native_cfg = '#[cfg(all(test, target_os = "macos", feature = "metal-backend"))]\n' hash_rs_path.write_text( "// Generated by scripts/precompile-metal-shaders.sh, or refreshed from the freshness test when MSL is unchanged.\n" '#[cfg(any(test, target_os = "macos"))]\n' f'pub(super) const UBER_WGSL_BLAKE3: &str = "{wgsl_hash}";\n' '#[cfg(any(test, target_os = "macos"))]\n' f'pub(super) const UBER_ABI_FNV64: u64 = 0x{(tmp / "abi.fnv64").read_text().strip()};\n' "#[cfg(all(test, target_os = \"macos\"))]\n" f'pub(super) const UBER_MSL_OPTIONS_FNV64: u64 = 0x{(tmp / "options.fnv64").read_text().strip()};\n' "#[cfg(test)]\n" f'pub(super) const UBER_MSL_BLAKE3: &str = "{msl_hash}";\n' "#[cfg(test)]\n" f"pub(super) const UBER_METALLIB_FNV64: u64 = 0x{h:016x};\n" + native_cfg + f'pub(super) const UBER_NATIVE_MSL_BLAKE3: &str = "{native_hash}";\n' + native_cfg + f"pub(super) const UBER_NATIVE_METALLIB_FNV64: u64 = 0x{native_h:016x};\n" ) print(f"wrote {metallib_path} ({len(data)} bytes, fnv64=0x{h:016x})") PY # The Windfoil uber variant (crates/rune/src/render/variant.rs), which only a renderer drawing # glyphs from outlines loads: uber_windfoil.metallib (wgpu) and uber_windfoil_native.metallib (Rune's). # shellcheck disable=SC2086 xcrun -sdk macosx metal $flags -c "$tmp/windfoil_vs.metal" -o "$tmp/windfoil_vs.air" # shellcheck disable=SC2086 xcrun -sdk macosx metal $flags -c "$tmp/windfoil_fs.metal" -o "$tmp/windfoil_fs.air" xcrun -sdk macosx metallib "$tmp/windfoil_vs.air" "$tmp/windfoil_fs.air" -o crates/rune/src/shaders/uber_windfoil.metallib # shellcheck disable=SC2086 xcrun -sdk macosx metal $native_flags -c "$tmp/windfoil_native_vs.metal" -o "$tmp/windfoil_native_vs.air" # shellcheck disable=SC2086 xcrun -sdk macosx metal $native_flags -c "$tmp/windfoil_native_fs.metal" -o "$tmp/windfoil_native_fs.air" xcrun -sdk macosx metallib "$tmp/windfoil_native_vs.air" "$tmp/windfoil_native_fs.air" -o crates/rune/src/shaders/uber_windfoil_native.metallib printf '%s\n' \ "// Generated by scripts/precompile-metal-shaders.sh. Empty until a Mac has built uber_windfoil.metallib." \ "pub(super) const WINDFOIL_WGSL_BLAKE3: &str = \"$(cat "$tmp/windfoil.blake3")\";" \ "pub(super) const WINDFOIL_ABI_FNV64: u64 = 0x$(cat "$tmp/windfoil_abi.fnv64");" \ > crates/rune/src/render/uber/windfoil_hash.rs echo "wrote crates/rune/src/shaders/uber_windfoil.metallib and uber_windfoil_native.metallib" # Every other built-in pipeline (crates/rune-gpu/src/metal/builtin.rs), captured once and compiled # for macOS, iOS and the iOS simulator. MSL 3.0 needs macOS 13 and iOS 16. RUNE_DISABLE_METALLIB # makes the uber shader compile from WGSL so it's captured too: iOS has no uber_native.metallib. cap="$tmp/capture" mkdir -p "$cap" if ! RUNE_GPU_BACKEND=metal RUNE_DISABLE_METALLIB=1 RUNE_METAL_CAPTURE="$cap" cargo test --locked -p rune-light --features rune/metal-backend --test builtin_shaders >"$tmp/capture.txt" 2>&1; then cat "$tmp/capture.txt" >&2 exit 1 fi ios_flags="$(echo "$native_flags" | sed 's/-mmacosx-version-min=13.0/-mios-version-min=16.0/')" sim_flags="$(echo "$native_flags" | sed 's/-mmacosx-version-min=13.0/-mios-simulator-version-min=16.0/')" for target in "macosx:$native_flags:builtin_native" "iphoneos:$ios_flags:builtin_native_ios" "iphonesimulator:$sim_flags:builtin_native_ios_sim"; do sdk="${target%%:*}" rest="${target#*:}" flags="${rest%:*}" name="${rest##*:}" mkdir -p "$cap/$sdk" export sdk flags # shellcheck disable=SC2016 find "$cap" -maxdepth 1 -name '*.metal' -print0 | xargs -0 -P 8 -n 1 sh -c 'xcrun -sdk "$sdk" metal $flags -Wno-unused-variable -c "$1" -o "$(dirname "$1")/$sdk/$(basename "${1%.metal}").air"' sh xcrun -sdk "$sdk" metallib "$cap/$sdk"/*.air -o "crates/rune-gpu/src/shaders/$name.metallib" done python3 - "$cap" crates/rune-gpu/src/metal/builtin_table.rs <<'PY' import pathlib, sys cap = pathlib.Path(sys.argv[1]) rows = "".join(p.read_text() for p in sorted(cap.glob("*.row"))) sources = "".join(f' "{p.stem}",\n' for p in sorted(cap.glob("*.source"))) pathlib.Path(sys.argv[2]).write_text( "// Generated by scripts/precompile-metal-shaders.sh: the built-in shaders' WGSL hashes, and each\n" "// precompiled pipeline's key, function, sized bindings, workgroup size and workgroup memory sizes,\n" "// sorted by key.\n" f"const SOURCES: &[&str] = &[\n{sources}];\n" f"const FUNCTIONS: &[(&str, &str, &[(u32, u32)], [u32; 3], &[u32])] = &[\n{rows}];\n" ) print(f"wrote {sys.argv[2]} ({len(list(cap.glob('*.row')))} pipelines)") PY