diff --git a/docs/testing.md b/docs/testing.md index 62f5ced44a..1647a85a91 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -655,8 +655,9 @@ fallback"), so the device needs geometry shaders instead. `ps_interp_f16_high` and `ms_f16_pack` read 16-bit interpolants as `PerVertexKHR` inputs, which need the extension too and which `--bary-fallback` can't express. With `LEONIDA_F16_INTERP_FALLBACK=1` they read halves the producer -exports decoded instead (see Macs, below) and run with or without the bary fallback; `run_gpu_tests.py` turns that -on by itself where the device lacks the extension. Under the bary fallback without it they skip. +exports decoded instead (see Macs, below) and run with or without the bary fallback; `run_gpu_tests.py`, `build.py` +and `check.py` turn that on by themselves where the device lacks the extension. Under the bary fallback without it +they skip. Intel Arc with Mesa ANV currently advertises the native barycentric feature but has a driver bug where translated pixel shaders can read `BaryCoordKHR.i` as zero (SHADER-925). `make_game.py` detects ANV and automatically translates @@ -682,8 +683,10 @@ are not covered: none has been measured. MoltenVK can't convert to Metal ("PerVertexKHR decoration is not supported in MSL"). `run_gpu_tests.py` asks `tools/vk_subgroup_info.py` whether the device can take them and, on MoltenVK, translates with `--f16-interp-fallback` by itself: the vertex or mesh shader then also exports the decoded f16 halves -(`docs/design.md`, "16-bit interpolation without `PerVertexKHR`"). `LEONIDA_F16_INTERP_FALLBACK=1` or `0` forces it -on or off; `build.py` (for `check.py`) translates with the flag only when that variable is `1`. `ms_f16_pack` also runs +(`docs/design.md`, "16-bit interpolation without `PerVertexKHR`"). `build.py` and `check.py` ask the same question, +so the two-step form translates and judges the shaders the same way: `ps_interp_f16_high` passes and `ps_ij_computed`, +whose computed I/J pair needs those inputs, is left out and reported as `SKIP`. `LEONIDA_F16_INTERP_FALLBACK=1` or `0` +forces the fallback on or off in all three; set it for `build.py` and `check.py` alike. `ms_f16_pack` also runs on a Mac with `LEONIDA_MESH_COMPUTE=1 LEONIDA_W64_EMULATE=1`. ## GPUs without VK_KHR_maintenance8 (NVIDIA Pascal and older) diff --git a/runtime/test/shaders/build.py b/runtime/test/shaders/build.py index 377f7025e0..d3e0974231 100644 --- a/runtime/test/shaders/build.py +++ b/runtime/test/shaders/build.py @@ -22,8 +22,11 @@ With LEONIDA_BARY_FALLBACK=1, pixel shaders that read raw barycentrics are translated with --bary-fallback, and each gets a pass-through geometry shader, .bary_gs.spv, that check.py draws it with (tools/bary_fallback_gs.py). With the variable unset, build.py/check.py follow the Mesa ANV probe automatically. -Under that or LEONIDA_F16_INTERP_FALLBACK=1, ps_ij_computed isn't built: its computed I/J pair needs PerVertexKHR -inputs, and check.py reports it as SKIP. +With LEONIDA_F16_INTERP_FALLBACK=1, 16-bit interpolants are translated with --f16-interp-fallback (decoded halves from +the producer instead of PerVertexKHR inputs); 0 turns that off. With the variable unset, build.py/check.py turn it on +where tools/vk_subgroup_info.py reports no PerVertexKHR inputs (MoltenVK), as scripts/run_gpu_tests.py does. +Under either fallback, ps_ij_computed isn't built: its computed I/J pair needs PerVertexKHR inputs, and check.py +reports it as SKIP. With LEONIDA_PC_TAIL_UBO=1, every shader is translated with --pc-tail-ubo (s0..s31 in 128 push-constant bytes, the heap base and code base in a uniform block at set 1), for devices whose maxPushConstantsSize is 128; set it for check.py too, whose test programs then bind that block (runtime/test/vk_pc_tail.h). @@ -45,7 +48,7 @@ MC = mc_command.mc_command() RSRC2, GROUP = 0x8088, (64, 1, 1) -# Not built under LEONIDA_BARY_FALLBACK=1 or LEONIDA_F16_INTERP_FALLBACK=1 (see main). +# Not built under the barycentric or the f16 interpolation fallback, set or picked from the device (see main). NEEDS_PER_VERTEX_INPUTS = ("ps_ij_computed",) sys.path.insert(0, HERE) # this script is also run with python -I from compute_fixture import compute_header, shift_user_data, user_data_shift # noqa: E402 @@ -73,6 +76,31 @@ def barycentric_fallback(): return False +def f16_interp_fallback_override(): + v = (os.environ.get("LEONIDA_F16_INTERP_FALLBACK") or "").strip().lower() + if v in ("1", "true", "yes", "on"): + return True + if v in ("0", "false", "no", "off"): + return False + return None + + +def f16_interp_fallback(): + """LEONIDA_F16_INTERP_FALLBACK=1 (or 0). Unset, it is on where the device can't take PerVertexKHR inputs: + tools/vk_subgroup_info.py reports per_vertex_inputs false (MoltenVK), the rule of scripts/run_gpu_tests.py. + A device that gives no answer leaves it off.""" + override = f16_interp_fallback_override() + if override is not None: + return override + try: + r = subprocess.run([sys.executable, "-I", os.path.join(TOOLS, "vk_subgroup_info.py")], + capture_output=True, text=True, timeout=60) + answer = json.loads(r.stdout.strip().splitlines()[-1]) + return answer.get("per_vertex_inputs") is False + except (OSError, ValueError, IndexError, subprocess.SubprocessError): + return False + + def asm_mattr(path): for ln in Path(path).read_text(encoding="utf-8").splitlines(): s = ln.strip() @@ -191,7 +219,7 @@ def translate_tests(work, names): cmd.append("--bary-fallback") if os.environ.get("LEONIDA_FMA") in ("glsl", "khr", "exact"): # GLSL Fma (default), OpFmaKHR, or emulated cmd += ["--fma", os.environ["LEONIDA_FMA"]] - if os.environ.get("LEONIDA_F16_INTERP_FALLBACK") == "1": # f16 interpolation without PerVertexKHR (MoltenVK) + if f16_interp_fallback(): # f16 interpolation without PerVertexKHR (MoltenVK) cmd.append("--f16-interp-fallback") if os.environ.get("LEONIDA_TEXOFF_FALLBACK") == "1": # texel offsets without VK_KHR_maintenance8 cmd.append("--texel-offset-fallback") @@ -227,7 +255,7 @@ def main(): names = [n for n in names if n not in skipped] # A computed I/J pair needs PerVertexKHR inputs, which both fallbacks take away: the translator refuses it there, # and check.py and run_gpu_tests.py report it as SKIP. Leave it out instead of losing the whole batch. - if barycentric_fallback() or os.environ.get("LEONIDA_F16_INTERP_FALLBACK") == "1": + if barycentric_fallback() or f16_interp_fallback(): per_vertex = [n for n in names if n in NEEDS_PER_VERTEX_INPUTS] if per_vertex: print("not built (computed I/J pairs need PerVertexKHR inputs, off under the fallback):", diff --git a/runtime/test/shaders/check.py b/runtime/test/shaders/check.py index 5abe2ab0a0..203d8f8356 100644 --- a/runtime/test/shaders/check.py +++ b/runtime/test/shaders/check.py @@ -55,6 +55,31 @@ def barycentric_fallback(): except (OSError, ValueError, IndexError, subprocess.SubprocessError): return False + +def f16_interp_fallback_override(): + v = (os.environ.get("LEONIDA_F16_INTERP_FALLBACK") or "").strip().lower() + if v in ("1", "true", "yes", "on"): + return True + if v in ("0", "false", "no", "off"): + return False + return None + + +def f16_interp_fallback(): + """As build.py decides it, so that both look at shaders translated the same way: LEONIDA_F16_INTERP_FALLBACK=1 (or + 0), else on where tools/vk_subgroup_info.py reports per_vertex_inputs false (MoltenVK). No answer leaves it off.""" + override = f16_interp_fallback_override() + if override is not None: + return override + try: + tools = os.path.abspath(os.path.join(HERE, "..", "..", "..", "tools")) + r = subprocess.run([sys.executable, "-I", os.path.join(tools, "vk_subgroup_info.py")], + capture_output=True, text=True, timeout=60) + answer = json.loads(r.stdout.strip().splitlines()[-1]) + return answer.get("per_vertex_inputs") is False + except (OSError, ValueError, IndexError, subprocess.SubprocessError): + return False + # The programs every run needs. Multi-config CMake generators (Visual Studio, Xcode, Ninja Multi-Config) put each of # them in build/ instead of build/ (TEST-38). TEST_PROGRAMS = ("vk_compute_test", "vk_store_test", "vk_harness") @@ -759,9 +784,10 @@ def salu_ext_test(tools, spirv, env=None): W64_EMULATED = os.environ.get("LEONIDA_W64_EMULATE") == "1" # LEONIDA_BARY_FALLBACK=1/0 overrides the device probe for raw barycentrics. Unset, Mesa ANV selects the fallback. BARY_FALLBACK = barycentric_fallback() -# LEONIDA_F16_INTERP_FALLBACK=1 (as build.py reads it): *_f16 interps were translated from decoded-half inputs, with no -# PerVertexKHR inputs at all, so the translator refuses a computed I/J pair (ps_ij_computed) there. -F16_HALVES = os.environ.get("LEONIDA_F16_INTERP_FALLBACK") == "1" +# LEONIDA_F16_INTERP_FALLBACK=1/0, or the device probe when it is unset (as build.py reads it): *_f16 interps were +# translated from decoded-half inputs, with no PerVertexKHR inputs at all, so the translator refuses a computed I/J +# pair (ps_ij_computed) there. +F16_HALVES = f16_interp_fallback() W64X_DUMPS = [("cs_dpp_w64", [1, 0, 3, 2, 5, 4, 7, 6]), ("cs_w64x_readlane", [121, 121, 16, 97, 16, 100, 100, 121]), ("cs_w64x_writelane", [4660, 77, 99, 2748, 0, 0, 4660, 99]), diff --git a/tests/test_shader_build_fallbacks.py b/tests/test_shader_build_fallbacks.py index e205bfe956..e8e00c18ae 100644 --- a/tests/test_shader_build_fallbacks.py +++ b/tests/test_shader_build_fallbacks.py @@ -5,6 +5,10 @@ every test missing (0 of 147 on a Mac, where check.py needs LEONIDA_F16_INTERP_FALLBACK=1). check.py and run_gpu_tests.py report ps_ij_computed as SKIP there; build.py now leaves it out and builds the rest. +With LEONIDA_F16_INTERP_FALLBACK unset, build.py and check.py take the answer from the device, as run_gpu_tests.py +does: per_vertex_inputs false in tools/vk_subgroup_info.py (MoltenVK) turns the fallback on. Before, the two-step +form on a Mac built ps_ij_computed and ps_interp_f16_high for PerVertexKHR inputs and check.py failed both. + Standard library only; the translator is replaced by a stand-in that writes the index and modules and, like the real one, refuses ps_ij_computed under a fallback. No llvm-mc, SPIR-V tools, GPU or game data. @@ -25,16 +29,28 @@ ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BUILD = os.path.join(ROOT, "runtime", "test", "shaders", "build.py") +CHECK = os.path.join(ROOT, "runtime", "test", "shaders", "check.py") FALLBACKS = ("LEONIDA_BARY_FALLBACK", "LEONIDA_F16_INTERP_FALLBACK") +# What tools/vk_subgroup_info.py answers for a desktop GPU with PerVertexKHR inputs: neither fallback. +DESKTOP = {"barycentric_fallback": False, "per_vertex_inputs": True} -def load_build(): - spec = importlib.util.spec_from_file_location("shaders_build_under_test", BUILD) +def load(path, name): + spec = importlib.util.spec_from_file_location(name, path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module +def load_build(): + return load(BUILD, "shaders_build_under_test") + + +def probe_answer(answer): + """subprocess.run's result for the device probe: one JSON line, or nothing when no device answered.""" + return SimpleNamespace(stdout=json.dumps(answer) + "\n" if answer is not None else "") + + class BuildUnderFallbackTest(unittest.TestCase): def setUp(self): self.fresh() @@ -63,7 +79,9 @@ def fake_translate(self, work, names): (spirv / "index.json").write_text(json.dumps(index), encoding="utf-8") return 0 - def run_build(self, names, env): + def run_build(self, names, env, probe=DESKTOP): + """build.py's main() on `names` with `env` for the two variables and `probe` for the device's answer, so that + the result does not depend on the GPU of the machine running the test.""" stubs = {"spirv_stubs": types.SimpleNamespace(generate_for_index=lambda *a: None), "bary_fallback_gs": types.SimpleNamespace(build_for_index=lambda *a: None)} clean = {k: v for k, v in os.environ.items() if k not in FALLBACKS} @@ -71,6 +89,7 @@ def run_build(self, names, env): with mock.patch.dict(os.environ, {**clean, **env}, clear=True), \ mock.patch.dict(sys.modules, stubs), \ mock.patch.object(self.build, "translate_tests", self.fake_translate), \ + mock.patch.object(self.build.subprocess, "run", return_value=probe_answer(probe)), \ mock.patch.object(sys, "argv", ["build.py", str(self.out), *names]), \ contextlib.redirect_stdout(stdout): try: @@ -120,6 +139,66 @@ def test_standalone_barycentric_override_and_probe(self): with mock.patch.object(self.build.subprocess, "run", return_value=proc): self.assertIs(self.build.barycentric_fallback(), want) + def test_a_device_without_per_vertex_inputs_selects_the_f16_fallback(self): + # MoltenVK, neither variable set: ps_ij_computed is left out, as under LEONIDA_F16_INTERP_FALLBACK=1. + molten = {"barycentric_fallback": False, "per_vertex_inputs": False} + status, printed = self.run_build(["cs_store", "ps_ij_computed"], {}, probe=molten) + self.assertEqual(status, 0, printed) + self.assertEqual(self.asked, [["cs_store"]]) + self.assertIn("PerVertexKHR", printed) + # LEONIDA_F16_INTERP_FALLBACK=0 keeps the device's answer out of it. + self.fresh() + status, printed = self.run_build(["cs_store", "ps_ij_computed"], {"LEONIDA_F16_INTERP_FALLBACK": "0"}, + probe=molten) + self.assertEqual(status, 0, printed) + self.assertEqual(self.asked, [["cs_store", "ps_ij_computed"]]) + # A device that has the inputs, or none that answers, builds it too. + for probe in (DESKTOP, {}, None): + with self.subTest(probe=probe): + self.fresh() + status, printed = self.run_build(["cs_store", "ps_ij_computed"], {}, probe=probe) + self.assertEqual(status, 0, printed) + self.assertEqual(self.asked, [["cs_store", "ps_ij_computed"]]) + + def test_the_translator_gets_the_flag_the_device_asks_for(self): + # translate_tests itself, with nothing to assemble: only the translator's command line is looked at. + for env, probe, want in (({}, {"per_vertex_inputs": False}, True), ({}, DESKTOP, False), ({}, None, False), + ({"LEONIDA_F16_INTERP_FALLBACK": "1"}, DESKTOP, True), + ({"LEONIDA_F16_INTERP_FALLBACK": "0"}, {"per_vertex_inputs": False}, False)): + with self.subTest(env=env, probe=probe), tempfile.TemporaryDirectory() as tmp: + commands = [] + + def run(cmd, **kwargs): + if os.path.basename(cmd[2]) == "vk_subgroup_info.py": + return probe_answer(probe) + commands.append(cmd) + return SimpleNamespace(returncode=0) + clean = {k: v for k, v in os.environ.items() if k not in FALLBACKS} + with mock.patch.dict(os.environ, {**clean, "LEONIDA_BARY_FALLBACK": "0", **env}, clear=True), \ + mock.patch.object(self.build.subprocess, "run", run): + self.assertEqual(self.build.translate_tests(Path(tmp), []), 0) + self.assertEqual(len(commands), 1) + self.assertEqual("--f16-interp-fallback" in commands[0], want) + + def test_standalone_f16_interp_override_and_probe(self): + # build.py and check.py each decide for themselves and have to agree, or check.py would look for + # PerVertexKHR results in shaders that were built without those inputs. + check = load(CHECK, "shaders_check_under_test") + for module in (self.build, check): + for value, probe, want in (("1", DESKTOP, True), ("on", DESKTOP, True), + ("0", {"per_vertex_inputs": False}, False), + ("off", {"per_vertex_inputs": False}, False), + ("", {"per_vertex_inputs": False}, True), ("", DESKTOP, False), + ("", {}, False), ("", {"per_vertex_inputs": None}, False), ("", None, False)): + with self.subTest(module=module.__name__, value=value, probe=probe), \ + mock.patch.dict(os.environ, {"LEONIDA_F16_INTERP_FALLBACK": value}, clear=False), \ + mock.patch.object(module.subprocess, "run", return_value=probe_answer(probe)): + self.assertIs(module.f16_interp_fallback(), want) + with self.subTest(module=module.__name__, probe="does not start"), \ + mock.patch.dict(os.environ, {"LEONIDA_F16_INTERP_FALLBACK": ""}, clear=False), \ + mock.patch.object(module.subprocess, "run", side_effect=OSError("no interpreter")): + self.assertIs(module.f16_interp_fallback(), False) + def test_a_missing_module_without_a_fallback_still_fails_the_batch(self): keep = self.fake_translate