diff --git a/tests/test_rdna2spirv_stage_cache.py b/tests/test_rdna2spirv_stage_cache.py new file mode 100644 index 0000000000..2ec7af1264 --- /dev/null +++ b/tests/test_rdna2spirv_stage_cache.py @@ -0,0 +1,67 @@ +"""Regression tests for rdna2spirv.stage_of's process-local carve-index cache. + +Standard library only; no game data. + + python -I tests/test_rdna2spirv_stage_cache.py +""" +import json +import os +import sys +import tempfile +import unittest + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.join(ROOT, "tools")) +import rdna2spirv # noqa: E402 + + +def tree(root, stage): + """One synthetic carve root with /index.json and /disasm/shader.s.""" + disasm = os.path.join(root, "disasm") + os.makedirs(disasm) + with open(os.path.join(root, "index.json"), "w", encoding="utf-8", newline="\n") as f: + json.dump([{"file": "shader.dxbc", "stage": stage}], f) + listing = os.path.join(disasm, "shader.s") + with open(listing, "w", encoding="utf-8", newline="\n") as f: + f.write("\ts_endpgm\n") + return listing + + +class StageOfCacheTest(unittest.TestCase): + def setUp(self): + self.old = rdna2spirv._STAGES + rdna2spirv._STAGES = None + + def tearDown(self): + rdna2spirv._STAGES = self.old + + def test_second_root_does_not_reuse_the_first_roots_index(self): + with tempfile.TemporaryDirectory() as tmp: + first = tree(os.path.join(tmp, "first"), "ps") + second = tree(os.path.join(tmp, "second"), "cs") + self.assertEqual(rdna2spirv.stage_of("shader", first), "ps") + self.assertEqual(rdna2spirv.stage_of("shader", second), "cs") + + def test_same_root_is_still_cached(self): + with tempfile.TemporaryDirectory() as tmp: + listing = tree(os.path.join(tmp, "root"), "ms") + self.assertEqual(rdna2spirv.stage_of("shader", listing), "ms") + os.remove(os.path.join(os.path.dirname(listing), "..", "index.json")) + self.assertEqual(rdna2spirv.stage_of("shader", listing), "ms") + + def test_a_missing_index_is_not_cached(self): + with tempfile.TemporaryDirectory() as tmp: + root = os.path.join(tmp, "root") + disasm = os.path.join(root, "disasm") + os.makedirs(disasm) + listing = os.path.join(disasm, "shader.s") + with open(listing, "w", encoding="utf-8", newline="\n") as f: + f.write("\ts_endpgm\n") + self.assertEqual(rdna2spirv.stage_of("shader", listing), "?") + with open(os.path.join(root, "index.json"), "w", encoding="utf-8", newline="\n") as f: + json.dump([{"file": "shader.dxbc", "stage": "cs"}], f) + self.assertEqual(rdna2spirv.stage_of("shader", listing), "cs") + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index 13fbd034db..d1529699a7 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -17023,14 +17023,19 @@ def stage_of(name, path): """Shader stage from the carve index (the DXIL program header in each container), not the name.""" global _STAGES if _STAGES is None: - import json - idx = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(path))), "index.json") _STAGES = {} + idx = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(path))), "index.json") + key = os.path.normcase(os.path.realpath(idx)) + stages = _STAGES.get(key) + if stages is None: + import json + stages = {} if os.path.isfile(idx): with open(idx, encoding="utf-8") as f: for e in json.load(f): - _STAGES[e["file"][:-5]] = e["stage"] - return _STAGES.get(name) or (name[:2].lower() if name[2:3] == "_" else "?") + stages[e["file"][:-5]] = e["stage"] + _STAGES[key] = stages + return stages.get(name) or (name[:2].lower() if name[2:3] == "_" else "?") # Wave64 markers in the instruction text. Besides EXEC/VCC forms, 64-bit lane-mask SOP1/SOP2 ops