Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions tests/test_rdna2spirv_stage_cache.py
Original file line number Diff line number Diff line change
@@ -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 <root>/index.json and <root>/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()
13 changes: 9 additions & 4 deletions tools/rdna2spirv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading