diff options
| author | Rafael <rafaelalvesp2005@gmail.com> | 2026-11-23 15:35:19 -0300 |
|---|---|---|
| committer | Rafael <rafaelalvesp2005@gmail.com> | 2026-11-23 15:35:34 -0300 |
| commit | 721341dc53c54a68ed08587e1bb5ab9583e48458 (patch) | |
| tree | 995fb34aff79b0c51ba7b94f45bc354a3d2a7f05 /tests/test_run_gpu_tests_python_assembler.py | |
| parent | c8f263917100f66e600f68a494e5cfe90c5cba17 (diff) | |
| parent | 6dda0630bd6fc0514baadb94fb98e36bd7d64c0b (diff) | |
Merge main into fix/walk-bvh_index
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests/test_run_gpu_tests_python_assembler.py')
| -rw-r--r-- | tests/test_run_gpu_tests_python_assembler.py | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/tests/test_run_gpu_tests_python_assembler.py b/tests/test_run_gpu_tests_python_assembler.py new file mode 100644 index 000000000..31a99aa90 --- /dev/null +++ b/tests/test_run_gpu_tests_python_assembler.py | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | """Tests for pure-Python SPIR-V assembly and --disasm-dir / --no-validate in run_gpu_tests.py and rdna2spirv.py.""" | ||
| 2 | import io | ||
| 3 | import json | ||
| 4 | import os | ||
| 5 | from pathlib import Path | ||
| 6 | import struct | ||
| 7 | import sys | ||
| 8 | import tempfile | ||
| 9 | from types import SimpleNamespace | ||
| 10 | import unittest | ||
| 11 | from unittest.mock import patch | ||
| 12 | |||
| 13 | ROOT = Path(__file__).resolve().parents[1] | ||
| 14 | sys.path.insert(0, str(ROOT / "scripts")) | ||
| 15 | sys.path.insert(0, str(ROOT / "tools")) | ||
| 16 | |||
| 17 | import rdna2spirv | ||
| 18 | import run_gpu_tests | ||
| 19 | |||
| 20 | |||
| 21 | class PythonAssemblerTests(unittest.TestCase): | ||
| 22 | def test_resolve_assembler_auto_and_explicit(self) -> None: | ||
| 23 | self.assertEqual(run_gpu_tests.resolve_assembler("python"), "python") | ||
| 24 | self.assertEqual(run_gpu_tests.resolve_assembler("spirv-as"), "spirv-as") | ||
| 25 | with patch.object(run_gpu_tests.shutil, "which", return_value="/usr/bin/spirv-as"): | ||
| 26 | self.assertEqual(run_gpu_tests.resolve_assembler("auto"), "spirv-as") | ||
| 27 | with patch.object(run_gpu_tests.shutil, "which", return_value=None): | ||
| 28 | self.assertEqual(run_gpu_tests.resolve_assembler("auto"), "python") | ||
| 29 | with self.assertRaises(ValueError): | ||
| 30 | run_gpu_tests.resolve_assembler("bogus") | ||
| 31 | |||
| 32 | def test_spirv_as_python_assembles_helper_shaders(self) -> None: | ||
| 33 | with tempfile.TemporaryDirectory() as tmp: | ||
| 34 | for name, src in ( | ||
| 35 | ("vs_tri.spv", run_gpu_tests.VS_TRI_SPVASM), | ||
| 36 | ("fs_passthru.spv", run_gpu_tests.make_fs_passthru(0)), | ||
| 37 | ): | ||
| 38 | out = Path(tmp) / name | ||
| 39 | run_gpu_tests.spirv_as(src, str(out), assembler="python") | ||
| 40 | data = out.read_bytes() | ||
| 41 | self.assertGreaterEqual(len(data), 20) | ||
| 42 | self.assertEqual(struct.unpack("<I", data[:4])[0], 0x07230203) | ||
| 43 | |||
| 44 | def test_rdna2spirv_assemble_spirv_no_validate_skips_spirv_val(self) -> None: | ||
| 45 | with tempfile.TemporaryDirectory() as tmp: | ||
| 46 | spvasm = Path(tmp) / "fs_passthru.spvasm" | ||
| 47 | spv = Path(tmp) / "fs_passthru.spv" | ||
| 48 | asm = run_gpu_tests.make_fs_passthru(0) | ||
| 49 | with patch.object(rdna2spirv, "_run_spirv_tool", | ||
| 50 | side_effect=AssertionError("external spirv tool called")) as run_tool: | ||
| 51 | res = rdna2spirv.assemble_spirv(str(spvasm), str(spv), asm_text=asm, | ||
| 52 | assembler="python", validate=False) | ||
| 53 | run_tool.assert_not_called() | ||
| 54 | self.assertEqual(res.returncode, 0) | ||
| 55 | self.assertTrue(spv.is_file()) | ||
| 56 | self.assertEqual(struct.unpack("<I", spv.read_bytes()[:4])[0], 0x07230203) | ||
| 57 | |||
| 58 | def test_rdna2spirv_cli_python_assembler_and_no_validate(self) -> None: | ||
| 59 | with tempfile.TemporaryDirectory() as tmp: | ||
| 60 | root = Path(tmp) | ||
| 61 | disasm = root / "disasm" | ||
| 62 | fssm = root / "fssm" | ||
| 63 | out = root / "out" | ||
| 64 | disasm.mkdir() | ||
| 65 | fssm.mkdir() | ||
| 66 | # Minimal valid compute listing (s_endpgm) and synthetic 0x148-byte FSSM header. | ||
| 67 | (disasm / "cs_noop.s").write_text("; synthetic\n\ts_endpgm\n", encoding="utf-8", newline="\n") | ||
| 68 | hdr = bytearray(0x148) | ||
| 69 | hdr[0:4] = b"FSSM" | ||
| 70 | struct.pack_into("<4I", hdr, 0x138, 0x8088, 64, 1, 1) | ||
| 71 | (fssm / "cs_noop.fssm").write_bytes(bytes(hdr)) | ||
| 72 | stdout = io.StringIO() | ||
| 73 | with patch("sys.stdout", stdout), \ | ||
| 74 | patch.object(sys, "argv", [ | ||
| 75 | "rdna2spirv.py", str(disasm), "--out", str(out), "--assembler", "python", "--no-validate", | ||
| 76 | ]), \ | ||
| 77 | patch.object(rdna2spirv, "_run_spirv_tool", | ||
| 78 | side_effect=AssertionError("external spirv tool called")): | ||
| 79 | rc = rdna2spirv.main() | ||
| 80 | self.assertIn(rc, (0, None)) | ||
| 81 | self.assertIn("1 shaders: 1 translated, 1 assembled (spirv-val skipped)", stdout.getvalue()) | ||
| 82 | spv = out / "cs_noop.spv" | ||
| 83 | self.assertTrue(spv.is_file()) | ||
| 84 | self.assertEqual(struct.unpack("<I", spv.read_bytes()[:4])[0], 0x07230203) | ||
| 85 | index = json.loads((out / "index.json").read_text(encoding="utf-8")) | ||
| 86 | self.assertIn("cs_noop", index) | ||
| 87 | |||
| 88 | def test_missing_disasm_dir_exits_2(self) -> None: | ||
| 89 | with tempfile.TemporaryDirectory() as tmp: | ||
| 90 | build = Path(tmp) / "build" | ||
| 91 | build.mkdir() | ||
| 92 | ext = ".exe" if os.name == "nt" else "" | ||
| 93 | for prog in run_gpu_tests.TEST_PROGRAMS: | ||
| 94 | (build / (prog + ext)).write_bytes(b"") | ||
| 95 | missing = Path(tmp) / "no_such_disasm" | ||
| 96 | err = io.StringIO() | ||
| 97 | with patch("sys.stderr", err), \ | ||
| 98 | patch.object(sys, "argv", ["run_gpu_tests.py", "--build", str(build), "--disasm-dir", str(missing)]): | ||
| 99 | rc = run_gpu_tests.main() | ||
| 100 | self.assertEqual(rc, 2) | ||
| 101 | self.assertIn("--disasm-dir is not a directory", err.getvalue()) | ||
| 102 | |||
| 103 | def test_run_suite_reuses_existing_work_listings(self) -> None: | ||
| 104 | listing = b"; synthetic\n\ts_endpgm\n" | ||
| 105 | run_command = run_gpu_tests.run_cmd | ||
| 106 | for placement in ("in-place", "hard-link", "separate"): | ||
| 107 | with self.subTest(placement=placement), tempfile.TemporaryDirectory() as tmp: | ||
| 108 | work = Path(tmp) / "work" | ||
| 109 | working = work / "disasm" / "cs_lds.s" | ||
| 110 | working.parent.mkdir(parents=True) | ||
| 111 | source = working if placement == "in-place" else Path(tmp) / "source" / working.name | ||
| 112 | source.parent.mkdir(exist_ok=True) | ||
| 113 | source.write_bytes(listing) | ||
| 114 | if placement == "hard-link": | ||
| 115 | try: | ||
| 116 | os.link(source, working) | ||
| 117 | except OSError as exc: | ||
| 118 | self.skipTest(f"hard links unavailable: {exc}") | ||
| 119 | |||
| 120 | args = SimpleNamespace(assembler="python", no_validate=True, disasm_dir=str(source.parent), | ||
| 121 | filter="cs_lds", build=str(work), require_pass=True) | ||
| 122 | |||
| 123 | def run(command, env=None, gpu=True): | ||
| 124 | if not gpu: | ||
| 125 | selected = Path(command[3]) | ||
| 126 | self.assertNotEqual(selected, working.parent) | ||
| 127 | self.assertEqual((selected / working.name).read_bytes(), listing) | ||
| 128 | return run_command(command, env=env, gpu=False) | ||
| 129 | self.assertTrue(Path(command[1]).is_file()) | ||
| 130 | return 0, "synthetic GPU result" | ||
| 131 | |||
| 132 | with (patch.object(run_gpu_tests, "run_cmd", side_effect=run) as launches, | ||
| 133 | patch.object(sys, "stdout", io.StringIO())): | ||
| 134 | self.assertEqual(run_gpu_tests.run_suite(args, str(work)), 0) | ||
| 135 | self.assertEqual(launches.call_count, 2) | ||
| 136 | self.assertEqual(source.read_bytes(), listing) | ||
| 137 | self.assertEqual(working.read_bytes(), listing) | ||
| 138 | |||
| 139 | |||
| 140 | if __name__ == "__main__": | ||
| 141 | unittest.main() | ||