mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 20:48:38 +00:00
`mesh_compute_vs.py --help` took `--help` as the spirv dir, found no index.json and exited 0 with no output; a path that does not exist did the same, so a typo looked like a successful run with nothing to do. -h/--help now print the docstring and exit 0, and a path that is not a directory is one ERROR line and exit 1, like the tool's other errors. build_for_index / write_for_index are unchanged. Same fix as #1379 for bary_fallback_gs.py.
152 lines
6.6 KiB
Python
152 lines
6.6 KiB
Python
"""Unit tests for tools/mesh_compute_vs.py (standard library; the assembly checks need SPIRV-Tools on PATH)."""
|
|
import contextlib
|
|
import io
|
|
import json
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, os.path.join(ROOT, "tools"))
|
|
import mesh_compute_vs as vs # noqa: E402
|
|
import rdna2spirv # noqa: E402
|
|
|
|
HAVE_SPIRV_TOOLS = bool(shutil.which("spirv-as") and shutil.which("spirv-val"))
|
|
|
|
|
|
def layout(maxv=64, maxp=3, clip=0, cull=0, params=()):
|
|
"""A real `mesh_compute` index entry, from the translator's own layout function."""
|
|
return rdna2spirv.mesh_compute_layout(maxv, maxp, clip, cull, list(params))
|
|
|
|
|
|
def write_index(directory, index):
|
|
path = os.path.join(directory, "index.json")
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
json.dump(index, f)
|
|
return path
|
|
|
|
|
|
class Interface(unittest.TestCase):
|
|
def test_entry_point_carries_the_mesh_shader_locations(self):
|
|
asm = vs.vs_spvasm(layout(clip=5, cull=2, params=(2, 9)))
|
|
entry = asm.split("OpEntryPoint Vertex", 1)[1].splitlines()[0]
|
|
for name in ("%vertex_index", "%pc", "%out_pos", "%out_clip", "%out_cull", "%out2", "%out9"):
|
|
self.assertIn(name, entry)
|
|
for decoration in ("BuiltIn ClipDistance", "BuiltIn CullDistance", "Location 2", "Location 9"):
|
|
self.assertIn(decoration, asm)
|
|
|
|
def test_no_clip_or_cull_means_no_such_outputs(self):
|
|
asm = vs.vs_spvasm(layout(params=(3,)))
|
|
self.assertNotIn("ClipDistance", asm)
|
|
self.assertNotIn("CullDistance", asm)
|
|
self.assertNotIn("%out_clip", asm)
|
|
self.assertIn("Location 3", asm)
|
|
|
|
def test_the_index_drives_write_for_index(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
self.assertEqual(vs.write_for_index(tmp), []) # no index.json: nothing to do
|
|
write_index(tmp, {"ms_none": {"subgroup_size": 64}, "ms_null": None,
|
|
"ms_a": {"mesh_compute": layout(clip=1)}})
|
|
written = vs.write_for_index(tmp)
|
|
self.assertEqual([os.path.basename(p) for p in written], ["ms_a.mesh_vs.spvasm"])
|
|
with open(written[0], encoding="utf-8") as f:
|
|
self.assertEqual(f.read(), vs.vs_spvasm(layout(clip=1)))
|
|
|
|
|
|
class Assembly(unittest.TestCase):
|
|
def test_every_layout_assembles_and_validates(self):
|
|
if not HAVE_SPIRV_TOOLS:
|
|
self.skipTest("spirv-as / spirv-val not on PATH")
|
|
layouts = {
|
|
"ms_plain": layout(),
|
|
"ms_clip": layout(clip=1),
|
|
"ms_full": layout(clip=5, cull=2, params=(2, 5)),
|
|
"ms_small": layout(maxv=1, maxp=256),
|
|
"ms_large": layout(maxv=128, maxp=64, clip=4, params=(0,)),
|
|
}
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
write_index(tmp, {name: {"mesh_compute": lay} for name, lay in layouts.items()})
|
|
built = vs.build_for_index(tmp)
|
|
self.assertEqual([os.path.basename(p) for p in built],
|
|
[name + ".mesh_vs.spv" for name in sorted(layouts)])
|
|
for spv in built:
|
|
self.assertGreater(os.path.getsize(spv), 0)
|
|
self.assertTrue(os.path.isfile(spv[: -len(".spv")] + ".spvasm"))
|
|
|
|
def test_a_rejected_shader_names_the_tool(self):
|
|
if not HAVE_SPIRV_TOOLS:
|
|
self.skipTest("spirv-as / spirv-val not on PATH")
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
asm = os.path.join(tmp, "bad.mesh_vs.spvasm")
|
|
with open(asm, "w", encoding="utf-8") as f:
|
|
f.write("OpCapability NothingOfTheSort\n")
|
|
with mock.patch.object(vs, "write_for_index", return_value=[asm]):
|
|
with self.assertRaises(RuntimeError) as ctx:
|
|
vs.build_for_index(tmp)
|
|
self.assertIn("spirv-as failed for", str(ctx.exception))
|
|
|
|
|
|
class Cli(unittest.TestCase):
|
|
def run_main(self, argv, **env):
|
|
out, err = io.StringIO(), io.StringIO()
|
|
with mock.patch.dict(os.environ, env):
|
|
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
|
rc = vs.main(argv)
|
|
return rc, out.getvalue(), err.getvalue()
|
|
|
|
def test_a_missing_spirv_tool_is_one_error_line(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
write_index(tmp, {"ms_a": {"mesh_compute": layout()}})
|
|
rc, stdout, stderr = self.run_main(["mesh_compute_vs.py", tmp], PATH="")
|
|
self.assertEqual(rc, 1)
|
|
self.assertEqual(stdout, "")
|
|
self.assertEqual(len(stderr.splitlines()), 1)
|
|
self.assertIn("spirv-as not found on PATH", stderr)
|
|
|
|
def test_a_wrong_argument_count_is_a_usage_error(self):
|
|
rc, stdout, stderr = self.run_main(["mesh_compute_vs.py"])
|
|
self.assertEqual(rc, 2)
|
|
self.assertEqual(stdout, "")
|
|
self.assertIn("Vertex shaders that draw", stderr)
|
|
|
|
def test_help_prints_usage(self):
|
|
for flag in ("-h", "--help"):
|
|
with self.subTest(flag=flag):
|
|
rc, stdout, stderr = self.run_main(["mesh_compute_vs.py", flag])
|
|
self.assertEqual(rc, 0)
|
|
self.assertIn("mesh_compute_vs.py <spirv dir>", stdout)
|
|
self.assertEqual(stderr, "")
|
|
|
|
def test_a_missing_directory_is_one_error_line(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
missing = os.path.join(tmp, "missing")
|
|
with mock.patch.object(vs, "build_for_index") as build:
|
|
rc, stdout, stderr = self.run_main(["mesh_compute_vs.py", missing])
|
|
build.assert_not_called()
|
|
self.assertEqual(os.listdir(tmp), [])
|
|
self.assertEqual(rc, 1)
|
|
self.assertEqual(stdout, "")
|
|
self.assertEqual(len(stderr.splitlines()), 1)
|
|
self.assertIn("not a directory", stderr)
|
|
|
|
def test_an_unreadable_index_is_one_error_line(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = write_index(tmp, {})
|
|
os.mkdir(os.path.join(tmp, "dir"))
|
|
# tool_path() runs first, so stub it out too: otherwise a machine without SPIRV-Tools reports
|
|
# the missing tool instead of the write error this test is about.
|
|
with mock.patch.object(vs, "tool_path", return_value="spirv-as"), \
|
|
mock.patch.object(vs, "write_for_index",
|
|
side_effect=OSError(13, "Permission denied", path)):
|
|
rc, _, stderr = self.run_main(["mesh_compute_vs.py", tmp])
|
|
self.assertEqual(rc, 1)
|
|
self.assertEqual(len(stderr.splitlines()), 1)
|
|
self.assertIn("Permission denied", stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|