"""Compile the amplification stage of the runtime's mesh pipelines and write its code as Rust. python -I tools/make_amplify_dxil.py [DXC] Compiles `d3d12/crates/leonida-gpu/src/indirect/amplify.hlsl` (`as_6_6`, entry `main`) with DXC and writes the container's bytes to `amplify_dxil.rs` beside it, which the runtime builds in (`indirect/amplify.rs`). DXC is the argument, else the newest `dxc.exe` of an installed Windows SDK, else `dxc` on the path. It signs its output when the validator (`dxil.dll`) lies beside it, as in the Windows SDK and in the releases of microsoft/DirectXShaderCompiler; D3D12 takes only signed code, so an unsigned result is refused here. The stage is the runtime's own code, not the title's: no game file is read. Standard library only. """ import glob import os import shutil import subprocess import sys import tempfile ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) HERE = os.path.join(ROOT, "d3d12", "crates", "leonida-gpu", "src", "indirect") SOURCE = os.path.join(HERE, "amplify.hlsl") OUT = os.path.join(HERE, "amplify_dxil.rs") def find_dxc(): if len(sys.argv) > 1: return sys.argv[1] kits = os.path.join(os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)"), "Windows Kits", "10", "bin") found = sorted(glob.glob(os.path.join(kits, "10.*", "x64", "dxc.exe"))) if found: return found[-1] return shutil.which("dxc") def signed(code): # The container's hash (16 bytes after the magic) is all zeros when nobody signed it. return code[:4] == b"DXBC" and any(code[4:20]) def main(): dxc = find_dxc() if not dxc: sys.exit("no DXC: name it, or install the Windows SDK") with tempfile.TemporaryDirectory() as scratch: out = os.path.join(scratch, "amplify.cso") done = subprocess.run([dxc, "-nologo", "-T", "as_6_6", "-E", "main", "-Fo", out, SOURCE], capture_output=True, text=True) if done.returncode != 0: sys.exit(f"DXC failed:\n{done.stdout}{done.stderr}") with open(out, "rb") as f: code = f.read() if not signed(code): sys.exit("DXC's output is not signed (no dxil.dll beside it?): D3D12 would refuse it") lines = [ "// Written by tools/make_amplify_dxil.py from amplify.hlsl (DXC, as_6_6, signed): the amplification", "// stage's code for `amplify.rs`. Make it again with that tool after a change of the source.", "", f"pub(super) const DXIL: [u8; {len(code)}] = [", ] for at in range(0, len(code), 16): lines.append(" " + " ".join(f"0x{b:02x}," for b in code[at : at + 16])) lines.append("];") with open(OUT, "w", encoding="utf-8", newline="\n") as f: f.write("\n".join(lines) + "\n") print(f"{OUT}: {len(code)} bytes from {dxc}") if __name__ == "__main__": try: # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode sys.stdout.reconfigure(errors="replace") except (AttributeError, ValueError, OSError): pass main()