Files
EVEngine/scripts/compile_graphics_aa_shaders.py
T
西风逍遥游andCursor Agent 3543a7343d feat(graphics): classic anti-aliasing (FXAA / SMAA / SSAA / NFAA) (#28)
* feat(graphics): add classic image-space anti-aliasing

Implement AntiAliasing (FXAA, SMAA-lite, SSAA resolve, NFAA) as a
Volumetric-style post API with quality presets, embedded SPIR-V shaders,
Squirrel bindings, tests, and docs.

Co-authored-by: 西风逍遥游 <sunxfancy@gmail.com>

* test(aa): fix zeroerr REQUIRE(a && b) ambiguity

Split combined pointer checks so Expression decomposition does not
conflict with built-in operator&&.

Co-authored-by: 西风逍遥游 <sunxfancy@gmail.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-08-11 17:00:25 +08:00

71 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""Compile graphics anti-aliasing GLSL fragments to SPIR-V C++ include arrays."""
from __future__ import annotations
import struct
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SHADER_DIR = ROOT / "src" / "modules" / "graphics" / "shaders"
FRAGS = [
"aa_fxaa.frag",
"aa_smaa.frag",
"aa_ssaa.frag",
"aa_nfaa.frag",
]
def spv_to_inc(spv_path: Path, array_name: str, out_path: Path) -> None:
data = spv_path.read_bytes()
if len(data) % 4 != 0:
raise SystemExit(f"SPIR-V size not multiple of 4: {spv_path}")
words = struct.unpack(f"<{len(data) // 4}I", data)
if not words or words[0] != 0x07230203:
raise SystemExit(f"bad SPIR-V magic in {spv_path}")
lines = [f"static const uint32_t {array_name}[] = {{"]
row: list[str] = []
for i, w in enumerate(words):
row.append(f"0x{w:08x}")
if len(row) == 8:
lines.append(" " + ", ".join(row) + ",")
row = []
if row:
lines.append(" " + ", ".join(row) + ",")
lines.append("};")
lines.append(f"static const size_t {array_name}_count = {len(words)};")
lines.append("")
out_path.write_text("\n".join(lines), encoding="utf-8")
print(f"wrote {out_path.relative_to(ROOT)} ({len(words)} words)")
def compile_frag(src: Path) -> Path:
out = src.with_suffix(".spv")
cmd = ["glslc", "-fshader-stage=frag", str(src), "-o", str(out)]
r = subprocess.run(cmd, capture_output=True, text=True)
if r.returncode != 0:
raise SystemExit(f"glslc failed for {src.name}:\n{r.stderr or r.stdout}")
return out
def main() -> int:
if not SHADER_DIR.is_dir():
raise SystemExit(f"missing shader dir: {SHADER_DIR}")
for name in FRAGS:
src = SHADER_DIR / name
if not src.is_file():
raise SystemExit(f"missing {src}")
spv = compile_frag(src)
stem = src.stem
array = f"{stem}_frag_spv"
spv_to_inc(spv, array, SHADER_DIR / f"{stem}_frag_spv.inc")
spv.unlink(missing_ok=True)
return 0
if __name__ == "__main__":
sys.exit(main())