"""scripts/shader_graph.py — 노드 라이브러리 리플렉션, 위상 정렬, 타입 검사, 생성된 GLSL 컴파일.""" import importlib.util import json import shutil import subprocess from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[1] SHADERS = ROOT / "src/vkgs/shaders" _spec = importlib.util.spec_from_file_location("shader_graph", ROOT / "scripts/shader_graph.py") sg = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(sg) LIB = sg.parse_library((SHADERS / "nodes/isp.glsl").read_text()) def _gen(nodes): return sg.generate({"nodes": nodes, "target": {"params_resource": "p"}}, LIB, "t", ["nodes/isp.glsl"]) def test_library_signatures(): assert LIB["exposure"]["inputs"] == [("c", "vec3")] assert LIB["exposure"]["ctx"] and LIB["exposure"]["params"] == [("ev", 0.0, -3.0, 3.0)] assert LIB["mix"]["inputs"] == [("a", "vec3"), ("b", "vec3")] assert LIB["luma"]["ret"] == "float" and not LIB["luma"]["ctx"] assert "return" not in LIB # 본문 안의 호출은 노드가 아니다 def test_order_and_params(): # 선언 순서가 뒤섞여도 입력 → 출력 순서로 펼친다 src, table, dot = _gen({ "out": {"op": "output", "in": ["e"]}, "e": {"op": "exposure", "in": ["i"], "params": {"ev": 0.5}}, "i": {"op": "input"}, }) body = src[src.index("void main"):] assert body.index("v_i =") < body.index("v_e =") < body.index("imageStore") assert table["params"] == [{"name": "e.ev", "node": "e", "op": "exposure", "index": 0, "default": 0.5, "min": -3.0, "max": 3.0}] assert table["resource"] == "p" and '"i" -> "e"' in dot def test_branches_and_types(): src, table, _ = _gen({ "i": {"op": "input"}, "y": {"op": "luma", "in": ["i"]}, "g": {"op": "gray", "in": ["y"]}, "m": {"op": "mix", "in": ["i", "g"], "params": {"amount": 0.25}}, "o": {"op": "output", "in": ["m"]}, }) assert "float v_y = n_luma(v_i);" in src and "n_mix(v_i, v_g, x, P(0))" in src @pytest.mark.parametrize("nodes, msg", [ ({"i": {"op": "input"}, "a": {"op": "exposure", "in": ["b"]}, "b": {"op": "exposure", "in": ["a"]}, "o": {"op": "output", "in": ["a"]}}, "순환"), ({"i": {"op": "input"}, "y": {"op": "luma", "in": ["i"]}, "e": {"op": "exposure", "in": ["y"]}, "o": {"op": "output", "in": ["e"]}}, "vec3"), ({"i": {"op": "input"}, "e": {"op": "nope", "in": ["i"]}, "o": {"op": "output", "in": ["e"]}}, "없는 연산"), ({"i": {"op": "input"}, "e": {"op": "exposure", "in": ["i"], "params": {"evv": 1}}, "o": {"op": "output", "in": ["e"]}}, "모르는 매개변수"), ({"i": {"op": "input"}, "m": {"op": "mix", "in": ["i"]}, "o": {"op": "output", "in": ["m"]}}, "입력 2개"), ({"i": {"op": "input"}, "e": {"op": "exposure", "in": ["i"]}}, "output"), ({"i": {"op": "input"}, "o": {"op": "output", "in": ["zz"]}}, "없는 노드"), ]) def test_errors(nodes, msg): with pytest.raises(sg.GraphError, match=msg): _gen(nodes) @pytest.mark.skipif(shutil.which("glslc") is None, reason="glslc 없음") @pytest.mark.parametrize("graph", sorted((ROOT / "configs/shader_graphs").glob("*.json")), ids=lambda p: p.stem) def test_repo_graphs_compile(graph, tmp_path): comp = sg.build(graph, SHADERS, tmp_path) table = json.loads(comp.with_suffix(".params.json").read_text()) assert table["count"] == len(table["params"]) > 0 subprocess.run(["glslc", "--target-env=vulkan1.2", "-O", "-I", str(SHADERS), str(comp), "-o", str(tmp_path / "x.spv")], check=True)