Skip to content
Merged
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
3 changes: 2 additions & 1 deletion project_tuxbox/dxil_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from .shader_prep import has_dxil_container


# Bump when the DXC arguments or the validation policy change.
# Bump when a policy change can make cached DXIL unacceptable under validation.
# Output-equivalent optimisation and fallback variants share the policy value.
POLICY = 1
IDENTITY_FILE = "dxil-identity"
LIBRARIES = ("libdxcompiler*", "libdxil*", "dxcompiler.dll", "dxil.dll")
Expand Down
37 changes: 29 additions & 8 deletions project_tuxbox/runtime_shaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,24 +212,47 @@ def archive_request(archive, source, inputs):
O0_TIMEOUT = 90


def compile_dxil(dxc, target, hlsl, output, store, request):
def validated_dxil(dxv, output, stage):
"""Return bounded, stage-correct output bytes after DXV accepts the file."""
run_tool([dxv, output], timeout=10)
data = bounded_shader(output)
if portable_stage(data) != stage:
raise ValueError("compiler returned invalid DXIL or a different shader stage")
return data


def compile_dxil(dxc, dxv, target, hlsl, output, store, request, stage):
"""Compile HLSL for the requested stage and return fully validated DXIL bytes."""
entry = [] if target == "lib_6_3" else ["-E", "main"]
unoptimised = [dxc, "-T", target, *entry, "-O0", "-Fo", output, hlsl]
# Large generated pixel programs can spend the cap in Jump Threading.
# Try this validated variant before consulting the baseline slow marker;
# an older DXC's flag rejection falls back just like a candidate timeout.
if target == "ps_6_6" and hlsl.stat().st_size >= (256 << 10):
try:
run_tool([dxc, "-T", target, *entry, "-disable-lifetime-markers",
"-Fo", output, hlsl], timeout=15)
if not output.exists():
raise ValueError("compiler returned no DXIL output")
return validated_dxil(dxv, output, stage)
except (subprocess.TimeoutExpired, ValueError):
output.unlink(missing_ok=True)
# cs_6_6 programs from this boot either finish default optimisation in
# under a second or sit in it until the cap. The ones that sit there
# compile at -O0 in a few seconds, and the driver optimises that DXIL
# again when it builds the pipeline. A request that timed out before goes
# straight to -O0 rather than spending the cap again.
if target == "cs_6_6" or (store and store.is_slow(request, target)):
run_tool(unoptimised, timeout=O0_TIMEOUT)
return
return validated_dxil(dxv, output, stage)
try:
run_tool([dxc, "-T", target, *entry, "-Fo", output, hlsl], timeout=15)
except subprocess.TimeoutExpired:
if store:
store.mark_slow(request, target)
output.unlink(missing_ok=True)
run_tool(unoptimised, timeout=O0_TIMEOUT)
return validated_dxil(dxv, output, stage)


def compile_request(source, key, tools, cache, session, *, remember=None):
Expand Down Expand Up @@ -325,17 +348,15 @@ def reuse():
target_stage = STAGE_IDS[target.split("_", 1)[0]]
if target_stage not in stages:
raise ValueError("compiler profile differs from requested shader stage")
# An earlier translator may have produced this exact HLSL.
# An earlier translator may have produced this exact HLSL. Reuse any
# validated equivalent output, including the candidate and -O0 paths;
# compiler policy changes deliberately leave warm cache entries valid.
store = DxilStore.for_cache(cache)
content = store.key(hlsl.read_bytes(), target) if store else None
data = bounded_shader(output) if store and store.fetch(content, output) else None
if data is None or portable_stage(data) != target_stage:
output.unlink(missing_ok=True)
compile_dxil(dxc, target, hlsl, output, store, key)
run_tool([dxv, output], timeout=10)
data = bounded_shader(output)
if portable_stage(data) != target_stage:
raise ValueError("compiler returned invalid DXIL or a different shader stage")
data = compile_dxil(dxc, dxv, target, hlsl, output, store, key, target_stage)
if store:
store.save(content, output)
stamp = work / "sha256"
Expand Down
124 changes: 122 additions & 2 deletions tests/test_runtime_shader_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from tempfile import TemporaryDirectory
from types import SimpleNamespace
import unittest
from unittest.mock import patch
from unittest.mock import Mock, patch

from project_tuxbox.runtime_shaders import O0_TIMEOUT, ShaderRefused, archive_request, compile_request, serve, snapshot_compiler_cache
from project_tuxbox.runtime_shaders import O0_TIMEOUT, ShaderRefused, archive_request, compile_dxil, compile_request, serve, snapshot_compiler_cache
from project_tuxbox.scarlett_prep import scarlett_requests
from project_tuxbox.shader_prep import SOURCE_CACHE, ToolFailure, prepare_for_launch

Expand Down Expand Up @@ -303,6 +303,126 @@ def run(args, *, timeout):
self.assertIn("-O0", dxc_calls[1].args[0])
self.assertEqual((self.session / (key + ".cso")).read_bytes(), dxil)

def test_large_pixel_candidate_precedes_slow_marker_and_validates_once(self):
"""Cold large ps_6_6 requests validate the candidate before publication."""
program = b"Xbox pixel shader"
key = hashlib.sha256(program).hexdigest()
source = self.requests / (key + ".dxbc")
source.write_bytes(program)
dxil = portable_program(0, b"pixel")
store = Mock()
store.key.return_value = "content"
store.fetch.return_value = False
store.is_slow.return_value = True

def run(args, *, timeout):
if args[0] == "xshader":
args[args.index("--hlsl-out") + 1].write_bytes(b"x" * (256 << 10))
args[args.index("--profile-out") + 1].write_text("ps_6_6")
elif args[0] == "dxc":
self.assertIn("-disable-lifetime-markers", args)
self.assertNotIn("-O0", args)
self.assertEqual(timeout, 15)
args[args.index("-Fo") + 1].write_bytes(dxil)
else:
self.assertEqual(args[0], "dxv")
self.assertFalse((self.cache / (key + ".cso")).exists())
self.assertEqual(args[1].read_bytes(), dxil)

with patch("project_tuxbox.runtime_shaders.run_tool", side_effect=run) as compiler, \
patch("project_tuxbox.runtime_shaders.DxilStore.for_cache", return_value=store):
self.assertTrue(compile_request(source, key, self.tools, self.cache, self.session))
self.assertFalse(compile_request(source, key, self.tools, self.cache, self.session))
self.assertEqual([call.args[0][0] for call in compiler.call_args_list], ["xshader", "dxc", "dxv"])
store.is_slow.assert_not_called()
store.mark_slow.assert_not_called()
store.save.assert_called_once()
self.assertEqual((self.session / (key + ".cso")).read_bytes(), dxil)

def test_large_pixel_candidate_failures_clean_output_and_preserve_baseline_policy(self):
"""Rejected candidates retry the baseline with its original slow-marker policy."""
hlsl, output = self.cache / "shader.hlsl", self.cache / "out.cso"
hlsl.write_bytes(b"x" * (256 << 10))
dxil = portable_program(0, b"baseline")
for failure in ("flag", "timeout", "dxv", "stage", "container", "missing"):
for slow in (False, True):
with self.subTest(failure=failure, slow=slow):
output.unlink(missing_ok=True)
store = Mock()
store.is_slow.return_value = slow
candidate = False
def run(args, *, timeout):
nonlocal candidate
if args[0] == "dxc":
candidate = "-disable-lifetime-markers" in args
if candidate:
self.assertEqual(timeout, 15)
store.is_slow.assert_not_called()
if failure != "missing":
output.write_bytes(portable_program(1, b"wrong") if failure == "stage" else b"partial")
if failure == "flag":
raise ToolFailure("dxc", 1, "unknown argument")
if failure == "timeout":
raise subprocess.TimeoutExpired("dxc", timeout)
else:
self.assertFalse(output.exists())
self.assertEqual("-O0" in args, slow)
self.assertEqual(timeout, O0_TIMEOUT if slow else 15)
output.write_bytes(dxil)
elif candidate and failure == "dxv":
raise ToolFailure("dxv", 1, "invalid DXIL")
with patch("project_tuxbox.runtime_shaders.run_tool", side_effect=run):
self.assertEqual(compile_dxil("dxc", "dxv", "ps_6_6", hlsl, output, store, "request", 0), dxil)
store.is_slow.assert_called_once_with("request", "ps_6_6")
store.mark_slow.assert_not_called()

def test_large_pixel_baseline_timeout_still_marks_slow(self):
"""A baseline timeout alone persists the slow marker and retries at O0."""
hlsl, output = self.cache / "shader.hlsl", self.cache / "out.cso"
hlsl.write_bytes(b"x" * (256 << 10))
store = Mock()
store.is_slow.return_value = False
dxil = portable_program(0, b"fallback")
def run(args, *, timeout):
if args[0] == "dxc":
self.assertFalse(output.exists())
if "-O0" not in args:
store.mark_slow.assert_not_called()
output.write_bytes(b"partial")
raise subprocess.TimeoutExpired("dxc", timeout)
self.assertEqual(timeout, O0_TIMEOUT)
output.write_bytes(dxil)
with patch("project_tuxbox.runtime_shaders.run_tool", side_effect=run) as compiler:
self.assertEqual(compile_dxil("dxc", "dxv", "ps_6_6", hlsl, output, store, "request", 0), dxil)
store.mark_slow.assert_called_once_with("request", "ps_6_6")
self.assertEqual([call.args[0][0] for call in compiler.call_args_list], ["dxc", "dxc", "dxc", "dxv"])

def test_large_pixel_startup_errors_propagate(self):
"""Tool startup errors retain their existing retryable exception behavior."""
hlsl, output = self.cache / "shader.hlsl", self.cache / "out.cso"
hlsl.write_bytes(b"x" * (256 << 10))
with patch("project_tuxbox.runtime_shaders.run_tool", side_effect=FileNotFoundError("dxc")) as compiler:
with self.assertRaises(FileNotFoundError):
compile_dxil("dxc", "dxv", "ps_6_6", hlsl, output, None, "request", 0)
self.assertEqual(compiler.call_count, 1)

def test_large_pixel_policy_leaves_other_profiles_and_small_sources_unchanged(self):
"""Only ps_6_6 sources at least 256 KiB use the candidate invocation."""
hlsl, output = self.cache / "shader.hlsl", self.cache / "out.cso"
for target, stage, size in (("ps_6_6", 0, (256 << 10) - 1), ("ps_6_0", 0, 256 << 10),
("cs_6_6", 5, 256 << 10), ("gs_6_6", 2, 256 << 10)):
with self.subTest(target=target, size=size):
hlsl.write_bytes(b"x" * size)
dxil = portable_program(stage, b"baseline")
def run(args, *, timeout):
self.assertNotIn("-disable-lifetime-markers", args)
if args[0] == "dxc":
self.assertEqual("-O0" in args, target == "cs_6_6")
output.write_bytes(dxil)
with patch("project_tuxbox.runtime_shaders.run_tool", side_effect=run) as compiler:
self.assertEqual(compile_dxil("dxc", "dxv", target, hlsl, output, None, "request", stage), dxil)
self.assertEqual([call.args[0][0] for call in compiler.call_args_list], ["dxc", "dxv"])

def test_cs_6_6_skips_the_optimiser(self):
program = b"Xbox compute shader"
key = hashlib.sha256(program).hexdigest()
Expand Down
Loading