vkgs-sim / tests /test_shader_graph.py
kimjunhan's picture
Add files using upload-large-folder tool
4c75661 verified
Raw History Blame Contribute Delete
3.65 kB
"""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)