From ce7eb9d3c801088415b2b472a801eaaf1132ca2d Mon Sep 17 00:00:00 2001 From: timbot103 <306940367+timbot103@users.noreply.github.com> Date: Mon, 30 Nov 2026 22:38:43 +1000 Subject: [PATCH] shader: support packed screen position in fixed slot 57 draws Refs #3560. Verify native slot 57 rendering and refusal with source-built data. AI-assisted. --- docs/experiments/slot57-packed-position.md | 50 +++++++++++ src/direct_compute_cache.cpp | 52 +++++++----- ...tartup_native_slot57_packed_position.cmake | 9 ++ tests/native_fixed_graphics_psi1_check.py | 63 ++++++++++++-- ...tup_native_slot57_packed_position_check.py | 85 +++++++++++++++++++ 5 files changed, 232 insertions(+), 27 deletions(-) create mode 100644 docs/experiments/slot57-packed-position.md create mode 100644 tests/cmake/startup_native_slot57_packed_position.cmake create mode 100644 tests/startup_native_slot57_packed_position_check.py diff --git a/docs/experiments/slot57-packed-position.md b/docs/experiments/slot57-packed-position.md new file mode 100644 index 0000000000..92c208c33e --- /dev/null +++ b/docs/experiments/slot57-packed-position.md @@ -0,0 +1,50 @@ +# Fixed slot 57 packed screen position + +The startup replay in [#3560](https://github.com/R0Y00/Xbox2Windows/issues/3560) +found a fullscreen layer that stayed stale when its native pixel shader was +refused. The captured fixed `PSI1` record had `SPI_PS_INPUT_ADDR=0x8080` and +`NUM_INTERP=0`. Bit 7 reserves the first VGPR, so the packed screen X and Y +from `POS_FIXED_PT` occupy `v1`. The existing `0x8002` layout puts packed XY in +`v2`. + +The fixed-entry compiler now accepts the `0x8080` record with zero +interpolants through the existing packed-position lowering. The lowered +pixel input must be the floating-point `SV_Position` row with X and Y in use. +The raw shader determines the consumed inputs when a reviewed catalog +variant has different code or unused `PARAM` attributes. The broader fixed +entry rules in [#3527](https://github.com/R0Y00/Xbox2Windows/pull/3527) are +separate work on the same validation function. + +The source-built runtime fixture submits the fixed state words to slot 57 +with native shaders enabled. For both a raw pair and a non-exact catalog +pair, it checks 16 pixels whose red and green channels encode their screen +coordinates, and another 16 pixels that remain clear outside the scissor. +The wrong-address control must refuse the pipeline and leave all 32 pixels +clear. The compiler fixture also checks cold and warm cache paths, a shader +that reads the wrong VGPR, and a shader that leaves screen position unused. + +## Reproduce + +Initialize the Windows toolchain and install the Python test dependencies +as described in the [build guide](../../README.md#first-build). + +```powershell +cmake --preset release +cmake --build --preset release --target compile_dxil startup_stream_consumer +$filter = '^(native_(fixed_graphics_psi1|direct_container|graphics_interface_library)|startup_native_slot57_packed_position_(warp|hardware))$' +ctest --preset release --no-tests=error -j 1 -R $filter -V +cmake --preset dev +cmake --build --preset dev --target compile_dxil startup_stream_consumer +ctest --preset dev --no-tests=error -j 1 -R $filter -V +``` + +The baseline compiler at `077bff3a` refused the source-built fixed entry with +`unsupported fixed pixel entry layout`, and the corresponding baseline WARP +draw refused the pipeline. The patch on that base passed all five selected +checks in both Release and Debug on Windows 11 build 26200, MSVC 19.44.35230 +and DXC from Windows SDK 10.0.26100.0. The adapters were an RTX 5080 with driver +32.0.16.2140 and in-box WARP 10.0.26100.9818. The D3D12 debug layer was +unavailable. Swapping the two coordinate exports in a temporary source-built +shader caused both adapters to fail the pixel check as expected. + +The captured title pass still needs a matched startup replay. diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index 2806ca64db..3b7698f864 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -768,6 +768,14 @@ DirectGraphicsPair prepare_direct_graphics(std::string_view a, std::string_view bool exact_pixel_code = false; Bytes entry; const bool fixed_entry = pixel_entry.size() == 12 && pixel_entry.substr(0,4) == "PSI1"; + uint32_t address = 0, interpolants = 0; + if (fixed_entry) { + std::memcpy(&address, pixel_entry.data() + 4, 4); + std::memcpy(&interpolants, pixel_entry.data() + 8, 4); + } + // Bit 7 allocates the VGPR before POS_FIXED_PT. The packed XY input is + // consequently v1, while the existing 0x8002 layout places it at v2. + const bool packed_position_only = fixed_entry && address == 0x8080 && interpolants == 0; bool packed_xy = false; std::optional captured_interp_count; std::optional> stage_wave32; @@ -775,13 +783,12 @@ DirectGraphicsPair prepare_direct_graphics(std::string_view a, std::string_view // Fixed slot 57 exposes SPI_PS_INPUT_ADDR and SPI_PS_IN_CONTROL but no .cdxil // sidecar. The raw frontend derives both stage interfaces from the programs; // hand it the same psin1 record the reflected path would build. - uint32_t address, interpolants; - std::memcpy(&address, pixel_entry.data() + 4, 4); - std::memcpy(&interpolants, pixel_entry.data() + 8, 4); - if (address != 2 && address != 0x8002) - throw UnsupportedComputeShader("unsupported fixed pixel entry layout"); - if (!interpolants || interpolants > 7) - throw UnsupportedComputeShader("unsupported fixed pixel entry control"); + if (!packed_position_only) { + if (address != 2 && address != 0x8002) + throw UnsupportedComputeShader("unsupported fixed pixel entry layout"); + if (!interpolants || interpolants > 7) + throw UnsupportedComputeShader("unsupported fixed pixel entry control"); + } captured_interp_count = interpolants; packed_xy = address == 0x8002; entry = "psin1"; @@ -807,18 +814,17 @@ DirectGraphicsPair prepare_direct_graphics(std::string_view a, std::string_view if (fixed_entry) { // Fixed slot 57 supplies ADDR and NUM_INTERP, not ENA. Intersect // the observed ABI with the reviewed stage and the raw consumers. - uint32_t address, interpolants; - std::memcpy(&address, pixel_entry.data() + 4, 4); - std::memcpy(&interpolants, pixel_entry.data() + 8, 4); - if (!interpolants || interpolants > 7 || interpolants > pixel_defaults.attributes.size()) - throw UnsupportedComputeShader("captured interpolation count disagrees with pixel interface"); + if (!packed_position_only) { + if (!interpolants || interpolants > 7 || interpolants > pixel_defaults.attributes.size()) + throw UnsupportedComputeShader("captured interpolation count disagrees with pixel interface"); + packed_xy = address == 0x8002 && pixel_defaults.pixel_input_address == 0x302; + if (!(address == 2 && pixel_defaults.pixel_input_address == 2) && !packed_xy) + throw UnsupportedComputeShader("unsupported fixed pixel entry layout"); + } captured_interp_count = interpolants; - packed_xy = address == 0x8002 && pixel_defaults.pixel_input_address == 0x302; - if (!(address == 2 && pixel_defaults.pixel_input_address == 2) && !packed_xy) - throw UnsupportedComputeShader("unsupported fixed pixel entry layout"); // Center I/J and, in the specialized position variant, packed XY - // are the only allocated inputs. The raw frontend checks each - // register use; the interpolants are checked below after linking. + // are the supported inputs. The frontend checks each register + // use and the linked inputs are checked below. entry = "psin1"; word(entry, address); word(entry, address); @@ -858,7 +864,15 @@ DirectGraphicsPair prepare_direct_graphics(std::string_view a, std::string_view interfaces.at(GpuStage::ps).target_components = targets; pair = translate_reflected_graphics(interfaces.at(GpuStage::vs),interfaces.at(GpuStage::ps),layout); } else pair = translate_graphics_pair(a, b, layout, pixel_entry, first_stage, targets, stage_wave32); - if (fixed_entry && !reflected && captured_interp_count) { + if (packed_position_only) { + // The captured zero count describes the running program. A catalog + // variant can still contain PARAM attributes that it has dropped. + const auto &inputs = pair.pixel.inputs; + if (inputs.size() != 1 || inputs.front().name != "SV_Position" || inputs.front().kind != 3 || + inputs.front().index != 0 || inputs.front().component != 9 || inputs.front().first != 0 || + (inputs.front().used & 3) != 3 || inputs.front().interpolation != 4) + throw UnsupportedComputeShader("fixed packed pixel entry requires screen XY without interpolants"); + } else if (fixed_entry && !reflected && captured_interp_count) { for (const auto &r : pair.pixel.inputs) { if (r.kind == 28 || r.name == "SV_Position") continue; if (r.name != "TEXCOORD" || r.index / 4 >= *captured_interp_count) @@ -866,7 +880,7 @@ DirectGraphicsPair prepare_direct_graphics(std::string_view a, std::string_view } } pair.target_components = targets; - if (!entry.empty() && reflected) { + if (!packed_position_only && !entry.empty() && reflected) { const auto &attrs = interfaces.at(GpuStage::ps).attributes; std::vector channels(attrs.size()); for (const auto &r : pair.pixel.inputs) { diff --git a/tests/cmake/startup_native_slot57_packed_position.cmake b/tests/cmake/startup_native_slot57_packed_position.cmake new file mode 100644 index 0000000000..654259a8ba --- /dev/null +++ b/tests/cmake/startup_native_slot57_packed_position.cmake @@ -0,0 +1,9 @@ +if(TARGET x2w_shader_compiler AND TARGET startup_stream_consumer AND EXISTS "${INTAKE_PYTHON}" + AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_native_slot57_packed_position_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_native_slot57_packed_position_check.py" + "$" "${DXC_BIN_DIR}" ${adapter}) + set_tests_properties(startup_native_slot57_packed_position_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + endforeach() +endif() diff --git a/tests/native_fixed_graphics_psi1_check.py b/tests/native_fixed_graphics_psi1_check.py index 9b2f578847..344522ca44 100644 --- a/tests/native_fixed_graphics_psi1_check.py +++ b/tests/native_fixed_graphics_psi1_check.py @@ -1,17 +1,18 @@ -"""Fixed VS/PS pairs with a slot-57 PSI1 entry and no reviewed .cdxil sidecar. +"""Fixed VS/PS pairs with a slot-57 PSI1 entry. The fixtures are source-built raw gfx1013 programs. The test derives the minimal pixel interface from SPI_PS_INPUT_ADDR, SPI_PS_IN_CONTROL and the programs themselves, and checks that unsupported masks or a contradictory -interface are refused before any stage is published. +interface are refused before any stage is published. The packed-position case +also exercises a catalog variant with a different entry layout. """ +import hashlib import pathlib -import struct import subprocess import sys import tempfile -from native_direct_container_check import publish, words +from native_direct_container_check import patched_pixel, publish, signature, vertex, words END = 0xBF810000 @@ -41,18 +42,23 @@ def packed_position_pixel(): return words(0x7E000302, 0xF800180F, 0x01010101, END) +def packed_xy_pixel(prefix=()): + # Bit 7 reserves v0. Bit 15 delivers packed XY in v1. Export its low half. + return words(*prefix, 0x7E000CF9, 0x00040601, 0xF800180F, 0, END) + + def main(compiler, sdk): compiler, sdk = pathlib.Path(compiler), pathlib.Path(sdk) with tempfile.TemporaryDirectory() as tmp: base = pathlib.Path(tmp) cache = base / "cache" - def compile_case(name, pixel, entry, expected=None, expect_hit=None): + def compile_case(name, pixel, entry, expected=None, expect_hit=None, *, case_cache=cache, vertex_code=VS): requests = base / (name + "-requests") output = base / (name + "-output") - key = publish(requests, pixel, VS, ROOT, slot57_inputs=entry) + key = publish(requests, pixel, vertex_code, ROOT, slot57_inputs=entry) result = subprocess.run( - [compiler, sdk, cache, "gfx1013-container-pair", "--requests", requests, output], + [compiler, sdk, case_cache, "gfx1013-container-pair", "--requests", requests, output], capture_output=True, text=True) if expected is not None: assert result.returncode and expected in result.stderr, (result.stdout, result.stderr) @@ -81,7 +87,48 @@ def compile_case(name, pixel, entry, expected=None, expect_hit=None): [sdk / "dxc.exe", "-dumpbin", output / f"{key}.ps.cso"], text=True) assert "SV_Position" in dump or "loadInput" in dump, dump - # Only the two reviewed address forms are accepted without an interface. + def check_screen_position(key, output): + dump = subprocess.check_output( + [sdk / "dxc.exe", "-dumpbin", output / f"{key}.ps.cso"], text=True) + assert "SV_Position" in dump and "@dx.op.loadInput.f32" in dump, dump + assert "TEXCOORD" not in dump and "SV_Barycentrics" not in dump, dump + + # The fixed capture has zero interpolants and consumes only packed XY. + packed_entry = (0x8080, 0) + raw_position_vs = words(0x7E020B00, 0xF80008CF, 0x01010101, END) + + def compile_packed_case(name, pixel, entry=packed_entry, **kwargs): + return compile_case(name, pixel, entry, vertex_code=raw_position_vs, **kwargs) + + check_screen_position(*compile_packed_case("packed-zero", packed_xy_pixel(), expect_hit=0)) + compile_packed_case("packed-zero", packed_xy_pixel(), expect_hit=1) + + # The available catalog pixel shares the embedded ID, but reads float + # position using a different entry layout. The capture describes the + # actual raw pixel, which must still use the packed v1 position. + catalog_cache = base / "catalog-cache" + interfaces = catalog_cache / "interfaces" + interfaces.mkdir(parents=True) + pixel_id = (0xD5800000, 255, 37, 0xD5800000, 255, 41) + raw_pixel = packed_xy_pixel(pixel_id) + reference = [*pixel_id, 0x7E000302] + (interfaces / (hashlib.sha256(raw_pixel).hexdigest() + ".cdxil")).write_bytes( + patched_pixel(len(reference), reference, input_address=0x0302, + input_signature=((b"ISG1", signature(b"SV_Position", system=1)),))) + (interfaces / (hashlib.sha256(raw_position_vs).hexdigest() + ".cdxil")).write_bytes(vertex()) + check_screen_position(*compile_packed_case("packed-zero-catalog", raw_pixel, + expect_hit=0, case_cache=catalog_cache)) + + compile_packed_case("packed-zero-missing-position", packed_xy_pixel(), (0x0080, 0), + expected="unsupported fixed pixel entry layout") + compile_packed_case("packed-with-interpolants", packed_xy_pixel(), (0x8080, 1), + expected="unsupported fixed pixel entry layout") + compile_packed_case("packed-zero-wrong-register", packed_position_pixel(), + expected="unsupported pixel entry VGPR 2") + compile_packed_case("packed-zero-unused-position", words(0x7E0002F2, 0xF800180F, 0, END), + expected="fixed packed pixel entry requires screen XY without interpolants") + + # Other address forms still require a reviewed interface or refusal. for address in (0x8003, 0x302, 3): compile_case(f"address-{address:04x}", center_pixel(), (address, 1), expected="unsupported fixed pixel entry layout") diff --git a/tests/startup_native_slot57_packed_position_check.py b/tests/startup_native_slot57_packed_position_check.py new file mode 100644 index 0000000000..a176d0e297 --- /dev/null +++ b/tests/startup_native_slot57_packed_position_check.py @@ -0,0 +1,85 @@ +"""Draw the zero-interpolant fixed packed-position ABI through the native slot 57 path.""" +import hashlib +import os +from pathlib import Path +import re +import struct +import subprocess +import sys +import tempfile + +from native_direct_container_check import patched_pixel, vertex, words +from startup_stream_draw_check import vop1, vop2 + + +END = 0xbf810000 +VS_ID = [0xd5800000, 255, 0x123, 0xd5800000, 255, 0x456] +PS_ID = [0xd5800000, 255, 0x789, 0xd5800000, 255, 0xabc] +CLEAR = bytes((0, 0, 255, 255)) + + +def vertex_program(): + # Fullscreen triangle computed from SV_VertexID, with no PARAM exports. + code = [vop2(0x1a, 1, 129, 0), vop2(0x1b, 1, 130, 1), vop2(0x1b, 2, 130, 0)] + for reg in (1, 2): + code.extend([vop1(6, reg, 256 + reg), vop2(8, reg, 244, reg), vop2(3, reg, 243, reg)]) + return words(*VS_ID, *code, vop1(1, 3, 128), vop1(1, 4, 242), 0xf80008cf, 0x04030201, END) + + +def pixel_program(): + # v1 is packed XY. Red and green encode X and Y independently on the 8x4 target. + return words(*PS_ID, + vop2(0x1b, 2, 255, 1), 0xffff, vop2(0x16, 3, 144, 1), + vop1(6, 2, 258), vop1(6, 3, 259), + vop2(8, 2, 255, 2), 0x3e000000, vop2(8, 3, 255, 3), 0x3e000000, + vop1(1, 4, 128), vop1(1, 5, 242), 0xf800180f, 0x05040302, END) + + +def check(folder, consumer, sdk, adapter): + vs, ps, root, pixels = (folder / name for name in ("vs", "ps", "root", "pixels")) + stages = vertex_program(), pixel_program() + vs.write_bytes(stages[0]) + ps.write_bytes(stages[1]) + gradient = b"".join(bytes((round(255 * x / 8), round(255 * y / 8), 0, 255)) if x < 4 else CLEAR + for y in range(4) for x in range(8)) + # The catalog variants share the embedded shader IDs but have different code and a PARAM. + catalog = (vertex(program=VS_ID + [END], varying=True), + patched_pixel(7, PS_ID + [vop1(1, 0, 128)], input_address=0x302, + attributes=[(6, 0, 1, 0, 0, 0, 0, 2)])) + for case, address, expected in (("raw", 0x8080, gradient), + ("catalog", 0x8080, gradient), + ("wrong-address", 0x0080, CLEAR * 32)): + cache = folder / case + cache.mkdir() + if case == "catalog": + interfaces = cache / "interfaces" + interfaces.mkdir() + for stage, companion in zip(stages, catalog): + (interfaces / (hashlib.sha256(stage).hexdigest() + ".cdxil")).write_bytes(companion) + env = dict(os.environ, X2W_SHADER_CACHE=str(cache), X2W_NATIVE_SHADERS=sdk, X2W_NATIVE_SHADERS_ONLY="1", + X2W_D3D12_DEBUG="1", X2W_D3D12_RENDER="1", X2W_D3D12_PERMISSIVE="1", X2W_D3D12_LIST_NOOP="1") + run = subprocess.run([consumer, vs, ps, root, adapter, pixels, "slot57", f"58:{address:x},5c:0"], + env=env, capture_output=True, text=True, timeout=45) + print(run.stdout, end="") + if run.returncode == 77: + return 77 + assert run.returncode == 0, run.stderr + assert not re.search(r"x2w-d3d12-debug .* sev=[01] ", run.stderr), run.stderr + for line in run.stderr.splitlines(): + if "debug layer:" in line: + print(line) + if case == "wrong-address": + assert "unsupported fixed pixel entry layout" in run.stderr and "-> drawn;" not in run.stderr, run.stderr + else: + assert "ready=1 cache(frontend,vs,ps)=" in run.stderr and "-> drawn;" in run.stderr, run.stderr + entries = list(cache.glob("req_*.ps57in")) + assert len(entries) == 1 and entries[0].read_bytes() == struct.pack("<4sII", b"PSI1", address, 0), entries + actual = pixels.read_bytes() + assert actual == expected, f"{case}: actual {actual.hex()} expected {expected.hex()}" + print(f"{adapter} {case}: " + ("32 clear pixels" if case == "wrong-address" else "16 XY and 16 clear pixels")) + return 0 + + +if __name__ == "__main__": + with tempfile.TemporaryDirectory(prefix="slot57-position-") as directory: + sys.exit(check(Path(directory), *sys.argv[1:]))