diff --git a/runtime/test/check_texture_readback_sync.cmake b/runtime/test/check_texture_readback_sync.cmake index 81ef3f24e..3905164f7 100644 --- a/runtime/test/check_texture_readback_sync.cmake +++ b/runtime/test/check_texture_readback_sync.cmake @@ -2,14 +2,37 @@ # -DOUTPUT= -P runtime/test/check_texture_readback_sync.cmake. # For a cross build, pass the configured emulator as "-DRUNNER=". # Requires the Khronos validation layer and a device that supports the texture harness. +if(NOT DEFINED OUTPUT OR OUTPUT STREQUAL "") + message(FATAL_ERROR "Specify the readback file with -DOUTPUT") +endif() +get_filename_component(spirv_dir "${SPIRV}" ABSOLUTE) +# Relative output paths are resolved by the harness from its working directory. +get_filename_component(readback "${OUTPUT}" ABSOLUTE BASE_DIR "${spirv_dir}") +if(IS_DIRECTORY "${readback}") + message(FATAL_ERROR "Readback path is a directory: ${readback}") +endif() +file(REMOVE "${readback}") +if(EXISTS "${readback}" OR IS_SYMLINK "${readback}") + message(FATAL_ERROR "Could not remove the previous readback: ${readback}") +endif() + execute_process( COMMAND "${CMAKE_COMMAND}" -E env VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer VK_LAYER_ENABLES=VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT ${RUNNER} "${TOOL}" cube "${OUTPUT}" - WORKING_DIRECTORY "${SPIRV}" TIMEOUT 90 + WORKING_DIRECTORY "${spirv_dir}" TIMEOUT 90 RESULT_VARIABLE code OUTPUT_VARIABLE log ERROR_VARIABLE log) if(NOT code STREQUAL "0" OR NOT log MATCHES "vk_texture_test cube: PASSED" OR NOT log MATCHES "Insert instance layer \"VK_LAYER_KHRONOS_validation\"" OR log MATCHES "SYNC-HAZARD|Validation Error") message(FATAL_ERROR "Texture draw with synchronization validation returned ${code}\n${log}") endif() +if(NOT EXISTS "${readback}" OR IS_DIRECTORY "${readback}") + message(FATAL_ERROR "Texture draw did not write a readback: ${readback}\n${log}") +endif() +# The cube harness writes a 64x64 VK_FORMAT_R32G32B32A32_SFLOAT frame. +math(EXPR expected_size "64 * 64 * 16") +file(SIZE "${readback}" actual_size) +if(NOT actual_size EQUAL expected_size) + message(FATAL_ERROR "Texture readback has ${actual_size} bytes, expected ${expected_size}: ${readback}\n${log}") +endif() diff --git a/tests/test_texture_readback_sync.py b/tests/test_texture_readback_sync.py new file mode 100644 index 000000000..318514e02 --- /dev/null +++ b/tests/test_texture_readback_sync.py @@ -0,0 +1,71 @@ +"""Exercise the texture synchronization CMake check with a synthetic cube harness.""" + +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest + +CHECK = Path(__file__).resolve().parents[1] / "runtime/test/check_texture_readback_sync.cmake" +CMAKE = shutil.which("cmake") +FRAME_BYTES = 64 * 64 * 16 +HARNESS = """\ +import os +from pathlib import Path +import sys + +assert sys.argv[1] == "cube" +assert os.environ["VK_INSTANCE_LAYERS"] == "VK_LAYER_KHRONOS_validation" +mode = os.environ["LEONIDA_SYNTHETIC_READBACK_MODE"] +output = Path(sys.argv[2]) +if mode in ("full", "hazard"): + output.write_bytes(bytes(64 * 64 * 16)) +elif mode == "short": + output.write_bytes(b"short") +if mode == "hazard": + print("SYNC-HAZARD", file=sys.stderr) +print('Insert instance layer "VK_LAYER_KHRONOS_validation"', file=sys.stderr) +print("vk_texture_test cube: PASSED (4096 pixels)") +""" + + +@unittest.skipUnless(CMAKE, "CMake is required to exercise the texture synchronization check") +class TextureReadbackSyncTests(unittest.TestCase): + def test_requires_fresh_complete_readback(self): + cases = ( + ("fresh", "full", False, 0), + ("valid rerun", "full", True, 0), + ("missing", "none", False, 1), + ("stale full-size file", "none", True, 1), + ("short", "short", False, 1), + ("sync hazard", "hazard", False, 1), + ) + for name, mode, preseeded, expected in cases: + with self.subTest(name=name), tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + shader_dir = root / "shaders" + shader_dir.mkdir() + output = shader_dir / "cube readback.raw" + if preseeded: + output.write_bytes(b"x" * FRAME_BYTES) + harness = root / "harness.py" + harness.write_text(HARNESS, encoding="utf-8", newline="\n") + + result = subprocess.run( + [CMAKE, f"-DRUNNER={sys.executable}", f"-DTOOL={harness}", + f"-DSPIRV={shader_dir.name}", f"-DOUTPUT={output.name}", "-P", str(CHECK)], + cwd=root, env=dict(os.environ, LEONIDA_SYNTHETIC_READBACK_MODE=mode), + capture_output=True, encoding="utf-8", errors="replace", + timeout=10, check=False, + ) + self.assertEqual(result.returncode, expected, result.stdout + result.stderr) + if expected == 0: + self.assertEqual(output.read_bytes(), bytes(FRAME_BYTES)) + if mode == "hazard": + self.assertIn("SYNC-HAZARD", result.stderr) + + +if __name__ == "__main__": + unittest.main()