From a0998de32de23d823e69635293d9cda2a7d44b4d Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Tue, 24 Nov 2026 20:48:33 +0100 Subject: [PATCH 1/8] startup: publish BC3/BC7 memory SRVs whose last tile row or column is partial Device slot 70 accepted address-only BC3/BC7 views at layout 0x105 only when width and height were multiples of 64 texels. A single-mip 4 KiB standard surface pads its pitch and height to whole tiles (addrlib ComputeSurfaceInfoMacroTiled; Bc16Surface does the same), so any size in whole 4x4 blocks now takes the existing detile path. Sizes that are not whole blocks stay refused. The first publication at an address of such a view logs its size, format, whether the XG stand-in announced a copy there, and how many of the first and last 4 KiB are non-zero, with a hash (no addresses). startup_address_bc_view adds a 100x68 texture of each format with distinct padding blocks, a wrong-tile-pitch reference that must differ, and a 98x68 description that must be refused. Co-Authored-By: Claude Opus 5.5 --- docs/experiments.md | 27 ++++++++-- src/startup_stubs/d3d12_memory_srv.inl | 37 ++++++++++++- tests/startup_address_bc_view_check.py | 72 +++++++++++++++++++------- tests/startup_address_srv.cpp | 14 ++++- 4 files changed, 124 insertions(+), 26 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index fdc1d1086a..40c5e40f49 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -10899,11 +10899,18 @@ ctest --preset release --no-tests=error -R '^(startup_memory_srv_report|startup_ The translated draw path can sample a read-only BC3_UNORM or BC7_UNORM SRV published through device slot 70 without a prior resource object. The tested -layout is 0x105, 4 KiB standard non-XOR. Width and height must be multiples of -64 texels, with one mip, one slice, and one sample. Each tile holds 16 by 16 -compressed blocks. The runtime copies the 16-byte blocks to their desktop -footprint rows before uploading them. Ordinary placed R8 views and the XG -BC7 layout probe retain their existing behavior. +layout is 0x105, 4 KiB standard non-XOR. Width and height must be whole 4x4 +blocks, with one mip, one slice, and one sample. Each tile holds 16 by 16 +compressed blocks. A partial last tile column or row is padded to a whole tile, +as AMD addrlib does for a single-mip thin surface: `ComputeSurfaceInfoMacroTiled` +aligns pitch and height to the block size and places mip 0 at offset 0, and the +address of a block is its tile index (row times tiles per padded pitch, plus +column) times 4 KiB, plus its offset in the tile. The runtime copies the +visible 16-byte blocks to their desktop footprint rows before uploading them. +Ordinary placed R8 views and the XG BC7 layout probe retain their existing +behavior. The first publication at an address of such a view with a partial +tile logs its size, format, whether the XG stand-in had announced a copy there, +and how many of the first and last 4 KiB are non-zero, with a hash. Keep the complete tiled guest allocation readable for the view's sampling lifetime. The producer stores the view identity in the title heap. The @@ -10920,6 +10927,16 @@ RTX 4060 Laptop (32.0.16.1656) and WARP (10.0.26100.9818) each matched 65,536 RGBA pixels per format. The swapped-tile control changed 16,384 of 16,384 pixels for BC3 and 15,360 for BC7. +The same test draws a 100 by 68 texture of each format from the same four +tiles, whose last tile column and row are partial. The padding blocks hold +distinct valid blocks; texels outside the texture must load as zero. A +reference that uses the tile pitch of the unpadded width (one tile instead of +two) differs from the image in 1,600 pixels for BC3 and 1,500 for BC7, and a +98 by 68 description (not whole blocks) must be refused. Restoring the +64-texel check fails the 100 by 68 BC3 case at its first texel row. On +Windows 11 build 26300 with the debug layer off, RTX 5090 (32.0.16.2135) and +WARP (10.0.26100.10068) matched all 65,536 RGBA pixels per case. + ```powershell cmake --build --preset release --target startup_stream_consumer startup_stub_consumer d3d12_xs_shim ctest --preset release --no-tests=error --output-on-failure -R '^(startup_address_(bc_view|srv)_(hardware|warp)|startup_xg_bc7_layout|bc7_tile_swizzle)$' diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index a0c7eca7ef..f712dd1be0 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -19,7 +19,30 @@ bool tiled_bc_srv(const D3D12_RESOURCE_DESC& r) static_cast(r.Layout) == 0x105 && r.Alignment == 0 && (r.Flags == D3D12_RESOURCE_FLAG_NONE || static_cast(r.Flags) == 0x300000) && r.Width <= D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION && r.Height <= D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION && - r.Width % 64 == 0 && r.Height % 64 == 0; + // A single mip pads its pitch and height to whole 4 KiB tiles (Bc16Surface); the desktop + // texture needs whole 4x4 blocks. + r.Width && r.Height && r.Width % 4 == 0 && r.Height % 4 == 0; +} + +// Content summary of a published view whose last tile row or column is partial: a title run can tell +// console-tiled data from zeros or rows written by the XG stand-in (which announces its copies). +void log_partial_tile_view(const D3D12_RESOURCE_DESC& r, u64 address, bool announced, bool published) +{ + static LONG lines; + if (!first_calls(lines, 16)) return; + const auto bytes = x2w_startup::Bc16Surface(r).size(); + const auto* data = reinterpret_cast(address); + const bool readable = x2w_local::readable(data, bytes); + const auto nonzero = [&](u64 from) { + unsigned count = 0; + for (u64 i = from; i < from + 4096; ++i) count += data[i] != 0; + return count; + }; + log("x2w-d3d12 Device::slot70 partial-tile BC view %llux%u format %d layout 0x%x: %s, XG-announced write %d, " + "%llu tiled bytes%s nonzero first/last 4 KiB %u/%u hash %016llx", r.Width, r.Height, r.Format, + static_cast(r.Layout), published ? "published" : "texture_unavailable", announced, bytes, + readable ? "," : " unreadable,", readable ? nonzero(0) : 0, readable ? nonzero(bytes - 4096) : 0, + readable ? hash_memory(data, static_cast(bytes)) : 0); } bool supports_memory_srv(const D3D12_RESOURCE_DESC& r, const D3D12_SHADER_RESOURCE_VIEW_DESC& v) @@ -230,8 +253,20 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou view_slot<18, 2>(self, reinterpret_cast(buffer), reinterpret_cast(&adjusted), destination, 0); return; } + // Before the first publication at an address, only an XG stand-in copy leaves a write record there. + bool first = false, announced = false; + if (tiled_bc_srv(*resource) && (resource->Width % 64 || resource->Height % 64)) { + { + std::lock_guard lock(tiled_srv_mutex); + first = !tiled_srv_addresses.count(input->address); + } + AcquireSRWLockShared(&written_lock); + announced = written_textures().count(input->address) != 0; + ReleaseSRWLockShared(&written_lock); + } Wrapper* texture = (tiled_bc_srv(*resource) || x2w_startup::bc64::supports(*resource)) ? publish_tiled_texture(self, input->address, *resource) : existing_memory_texture(input->address, *resource); + if (first) log_partial_tile_view(*resource, input->address, announced, texture != nullptr); if (!texture) { if (first_calls(refused, 8)) log_refused_memory_srv("unavailable texture at the supplied base and layout", "texture_unavailable", resource, input, destination); diff --git a/tests/startup_address_bc_view_check.py b/tests/startup_address_bc_view_check.py index d2a3548dcc..a41357a0cb 100644 --- a/tests/startup_address_bc_view_check.py +++ b/tests/startup_address_bc_view_check.py @@ -1,5 +1,6 @@ """Draw 2×2 4 KiB tiled BC3/BC7 textures from guest addresses, including swapped tiles.""" from pathlib import Path +import re import struct import sys import tempfile @@ -35,14 +36,35 @@ def make_surface(encode): return tiled, rgba -def check(directory, consumer, backend, fmt): +def source_block(x, y, tile_pitch, swapped): + """The block of the 2x2 tile grid that a detile with this tile pitch shows at block (x, y).""" + tile = (y // 16) * tile_pitch + x // 16 + tile ^= swapped + return (tile % 2) * 16 + x % 16, (tile // 2) * 16 + y % 16 + + +def expected_panels(rgba, width, height, tile_pitch): + """Four panels of 128x128 texels: views of the first, swapped, first (kept) and first backing.""" + image = bytearray(4 * WIDTH * 4 * HEIGHT) + for panel, swapped in enumerate((0, 1, 1, 0)): + for y in range(height): + for x in range(width): + bx, by = source_block(x // 4, y // 4, tile_pitch, swapped) + start = 4 * ((by * 4 + y % 4) * 128 + bx * 4 + x % 4) + pos = 4 * (y * 4 * WIDTH + panel * WIDTH + x) + image[pos:pos + 4] = rgba[start:start + 4] + return bytes(image) # texels outside the texture load as zero + + +def check(directory, consumer, backend, fmt, width, height): tiled, rgba = make_surface(bc3_block if fmt == "BC3" else bc7_block) source = directory / "tiled.bin" source.write_bytes(tiled) hlsl = ("Texture2D t : register(t0);\n" "float4 main(float4 p : SV_Position) : SV_Target {\n" f" return t.Load(int3(uint(p.x) % {WIDTH}, uint(p.y), 0));\n}}\n") - modes = {"X2W_TEST_BC_VIEW": str(source)} + trap_log = directory / "trap.log" + modes = {"X2W_TEST_BC_VIEW": str(source), "X2W_TEST_BC_SIZE": f"{width}x{height}", "X2W_TRAP_LOG": str(trap_log)} if fmt == "BC3": modes["X2W_TEST_BC3"] = "1" actual = draw_texture(directory, consumer, backend, hlsl, modes) @@ -50,27 +72,41 @@ def check(directory, consumer, backend, fmt): return 77 if len(actual) != WIDTH * 4 * HEIGHT * 4: raise AssertionError(f"{fmt} readback length {len(actual)}") - different = 0 - for y in range(HEIGHT): - for x in range(WIDTH): - normal = 4 * (y * 128 + x) - swapped = 4 * (y * 128 + (x ^ 64)) - different += rgba[normal:normal + 4] != rgba[swapped:swapped + 4] - for panel, source_index in enumerate((normal, swapped, swapped, normal)): - pos = 4 * ((y * 4 + panel) * WIDTH + x) - pixel = actual[pos:pos + 4] - expect = rgba[source_index:source_index + 4] - if pixel != expect: - raise AssertionError(f"{fmt} panel {panel} ({x},{y}) actual {pixel.hex()} expected {expect.hex()}") - if different == 0: + expected = expected_panels(rgba, width, height, 2) + for pos in range(0, len(expected), 4): + if actual[pos:pos + 4] != expected[pos:pos + 4]: + y, x = divmod(pos // 4, 4 * WIDTH) + raise AssertionError(f"{fmt} {width}x{height} panel {x // WIDTH} ({x % WIDTH},{y}) " + f"actual {actual[pos:pos + 4].hex()} expected {expected[pos:pos + 4].hex()}") + # Controls: exchanged tiles, and for a partial last tile column the tile pitch of the unpadded width. + swapped = sum(expected[p:p + 4] != expected[p + 4 * WIDTH:p + 4 * WIDTH + 4] + for p in range(0, len(expected), 4) if (p // 4) % (4 * WIDTH) < WIDTH) + if swapped == 0: raise AssertionError("Swapping the tiles did not change the reference image") - print(f"{backend} {fmt}: {4 * WIDTH * HEIGHT} RGBA pixels exact; exchanged tiles differ in {different}/{WIDTH * HEIGHT}") + columns = width // 4 + unpadded = 0 + if columns % 16: + wrong = expected_panels(rgba, width, height, columns // 16) + unpadded = sum(actual[p:p + 4] != wrong[p:p + 4] for p in range(0, len(actual), 4)) + if unpadded == 0: + raise AssertionError("The unpadded tile pitch did not change the image") + log = trap_log.read_text(errors="replace") + refusal = (rf"Device::slot70 unsupported memory SRV arguments: reason=unsupported_description " + rf"res\{{dim=3 fmt={77 if fmt == 'BC3' else 98} size={width - 2}x{height}x1 ") + if not re.search(refusal, log): + raise AssertionError(f"{fmt}: no refusal for {width - 2}x{height} (not whole blocks)") + partial = re.findall(r"partial-tile BC view (\d+)x(\d+) .*: published, XG-announced write 0", log) + # One line per first publication at an address: the first backing and the exchanged one. + if partial != ([] if width % 64 == 0 and height % 64 == 0 else [(str(width), str(height))] * 2): + raise AssertionError(f"{fmt}: partial-tile lines {partial}") + print(f"{backend} {fmt} {width}x{height}: {4 * WIDTH * HEIGHT} RGBA pixels exact; exchanged tiles differ in " + f"{swapped} texels; unpadded tile pitch differs in {unpadded}; {width - 2}x{height} refused") return 0 if __name__ == "__main__": - for fmt in ("BC3", "BC7"): + for fmt, width, height in (("BC3", 128, 128), ("BC7", 128, 128), ("BC3", 100, 68), ("BC7", 100, 68)): with tempfile.TemporaryDirectory() as temporary: - result = check(Path(temporary), *sys.argv[1:], fmt) + result = check(Path(temporary), *sys.argv[1:], fmt, width, height) if result: sys.exit(result) diff --git a/tests/startup_address_srv.cpp b/tests/startup_address_srv.cpp index 5ebce1c274..2f93137e93 100644 --- a/tests/startup_address_srv.cpp +++ b/tests/startup_address_srv.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -139,7 +140,13 @@ int draw_bc_address_srvs(void* guest, void* list) const DWORD length = GetEnvironmentVariableW(L"X2W_TEST_BC_VIEW", path, MAX_PATH); if (!length || length >= MAX_PATH) return 43; const bool bc3 = GetEnvironmentVariableW(L"X2W_TEST_BC3", nullptr, 0) != 0; - constexpr UINT tile = 4096, width = 128, height = 128; + // X2W_TEST_BC_SIZE=x (default 128x128) keeps the 2x2 tiles; a smaller size leaves the + // last tile column and row partial, so the padding blocks must stay outside the texture. + constexpr UINT tile = 4096, panel = 128; + UINT width = panel, height = panel; + wchar_t size[16]{}; + if (GetEnvironmentVariableW(L"X2W_TEST_BC_SIZE", size, 16) && + (swscanf_s(size, L"%ux%u", &width, &height) != 2 || width > panel || height > panel)) return 43; alignas(4096) static std::array, 2> backing{}; std::ifstream input(path, std::ios::binary); if (!input.read(backing[0].data(), 4 * tile) || input.peek() != std::char_traits::eof()) return 43; @@ -159,10 +166,13 @@ int draw_bc_address_srvs(void* guest, void* list) for (LONG i = 0; i < 4; ++i) { v.address = addresses[i]; invoke(guest, 70, ptr(&desc), ptr(&v), cpu); - const D3D12_RECT region{i * LONG(width), 0, (i + 1) * LONG(width), LONG(height)}; + const D3D12_RECT region{i * LONG(panel), 0, (i + 1) * LONG(panel), LONG(panel)}; invoke(list, 22, 1, ptr(®ion)); invoke(list, 12, 3, 1); } + // Not whole 4x4 blocks: refused (the check reads the log), with nothing drawn after it. + desc.Width -= 2; + invoke(guest, 70, ptr(&desc), ptr(&v), cpu); return 0; } From 40553329c582f51ff8a2ac9732f7f1327b004483 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Tue, 24 Nov 2026 20:51:18 +0100 Subject: [PATCH 2/8] startup: log up to 64 partial-tile BC views and name the addrlib revision read Raise the cap on the partial-tile log line from 16 to 64 so that a run publishing many such views still logs the later ones. Co-Authored-By: Claude Opus 5.5 --- docs/experiments.md | 4 +++- src/startup_stubs/d3d12_memory_srv.inl | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 40c5e40f49..e831c701ec 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -10902,7 +10902,9 @@ published through device slot 70 without a prior resource object. The tested layout is 0x105, 4 KiB standard non-XOR. Width and height must be whole 4x4 blocks, with one mip, one slice, and one sample. Each tile holds 16 by 16 compressed blocks. A partial last tile column or row is padded to a whole tile, -as AMD addrlib does for a single-mip thin surface: `ComputeSurfaceInfoMacroTiled` +as AMD addrlib does for a single-mip thin surface (read in the GPUOpen PAL copy +of `gfx10addrlib.cpp` at `c5e80007`, not the Mesa revision pinned for the +optional addrlib tests): `ComputeSurfaceInfoMacroTiled` aligns pitch and height to the block size and places mip 0 at offset 0, and the address of a block is its tile index (row times tiles per padded pitch, plus column) times 4 KiB, plus its offset in the tile. The runtime copies the diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index f712dd1be0..3ef205008f 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -29,7 +29,7 @@ bool tiled_bc_srv(const D3D12_RESOURCE_DESC& r) void log_partial_tile_view(const D3D12_RESOURCE_DESC& r, u64 address, bool announced, bool published) { static LONG lines; - if (!first_calls(lines, 16)) return; + if (!first_calls(lines, 64)) return; const auto bytes = x2w_startup::Bc16Surface(r).size(); const auto* data = reinterpret_cast(address); const bool readable = x2w_local::readable(data, bytes); From 00e95930db37b7fbddefdd7a53696a9233c6176d Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Tue, 24 Nov 2026 23:16:40 +0100 Subject: [PATCH 3/8] tests: give startup_address_bc_view 180 s for its four cases The partial-tile cases double the draws. On a loaded machine the hardware run took 86-92 s against the 90 s limit (6-11 s when idle). Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c662aa936..8f6dcad374 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2286,7 +2286,7 @@ if(EXISTS "${INTAKE_PYTHON}") add_test(NAME startup_address_bc_view_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_bc_view_check.py" "$" "${backend}") - set_tests_properties(startup_address_bc_view_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) + set_tests_properties(startup_address_bc_view_${backend} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77) add_test(NAME startup_xg_bc_chain_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_xg_bc_chain_check.py" "$" "$" "${backend}") From 9d47e660be5b130ecffec469287ce9b187958f3f Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Tue, 24 Nov 2026 23:54:56 +0100 Subject: [PATCH 4/8] startup: keep partial-tile BC views refused over XG rows or past the XG size A partial-tile BC3/BC7 view at layout 0x105 now stays refused when the XG stand-in announced a copy to its address before the first publication there (that copy wrote desktop-footprint rows), or when its padded tiles are larger than the size the stand-in reports for the description (the desktop footprint rounded to 64 KiB), since those bytes may belong to something else. The log line names the reason, and gives the content summary only for a published view. startup_address_bc_view asserts the tiled byte count and adds refusals for a height that is not whole blocks, a 1028x1028 description and a view over an announced copy. The docs cite the pinned addrlib revision and list startup_bc7_4k_s with the reproduction tests. Co-Authored-By: Claude Opus 5.5 --- docs/experiments.md | 50 +++++++++++++++--------- src/startup_stubs/d3d12_memory_srv.inl | 54 ++++++++++++++++++-------- tests/startup_address_bc_view_check.py | 27 ++++++++----- tests/startup_address_srv.cpp | 25 ++++++++++-- 4 files changed, 108 insertions(+), 48 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index d557dc70d1..a41c54b248 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11600,17 +11600,25 @@ published through device slot 70 without a prior resource object. The tested layout is 0x105, 4 KiB standard non-XOR. Width and height must be whole 4x4 blocks, with one mip, one slice, and one sample. Each tile holds 16 by 16 compressed blocks. A partial last tile column or row is padded to a whole tile, -as AMD addrlib does for a single-mip thin surface (read in the GPUOpen PAL copy -of `gfx10addrlib.cpp` at `c5e80007`, not the Mesa revision pinned for the -optional addrlib tests): `ComputeSurfaceInfoMacroTiled` -aligns pitch and height to the block size and places mip 0 at offset 0, and the -address of a block is its tile index (row times tiles per padded pitch, plus -column) times 4 KiB, plus its offset in the tile. The runtime copies the -visible 16-byte blocks to their desktop footprint rows before uploading them. -Ordinary placed R8 views and the XG BC7 layout probe retain their existing -behavior. The first publication at an address of such a view with a partial -tile logs its size, format, whether the XG stand-in had announced a copy there, -and how many of the first and last 4 KiB are non-zero, with a hash. +as AMD addrlib does for a single-mip thin surface (Mesa +`5c142e46f3f6e752ed745fd48912ebb8fad67145`, `src/amd/addrlib/src/gfx10/gfx10addrlib.cpp`): +`ComputeSurfaceInfoMacroTiled` aligns pitch and height to the block size, with +no mip tail for one mip, and `ComputeSurfaceAddrFromCoordMacroTiled` takes a +block's tile index as its block row times the tiles per padded pitch, plus its +block column. The runtime copies the visible 16-byte blocks to their desktop +footprint rows before uploading them. Ordinary placed R8 views and the XG BC7 +layout probe retain their existing behavior. + +A view with a partial last tile stays refused in two cases. The first is when +the XG stand-in announced a copy to that address before the view was first +published there, because that copy wrote desktop-footprint rows, not tiles. +The second is when the padded tiles are larger than the size the XG stand-in +reports for the description (the desktop footprint rounded to 64 KiB): the +bytes past that size may belong to something else, and a readability probe +cannot tell. For example, 1028 by 1028 pads to 1,183,744 bytes against a +reported 1,179,648. The first publication at an address of a partial-tile view +logs its size, format, tiled byte count, and how many of the first and last +4 KiB are non-zero, with a hash; a refused one logs its reason instead. Keep the complete tiled guest allocation readable for the view's sampling lifetime. The producer stores the view identity in the title heap. The @@ -11629,17 +11637,21 @@ of 16,384 pixels for BC3 and 15,360 for BC7. The same test draws a 100 by 68 texture of each format from the same four tiles, whose last tile column and row are partial. The padding blocks hold -distinct valid blocks; texels outside the texture must load as zero. A -reference that uses the tile pitch of the unpadded width (one tile instead of -two) differs from the image in 1,600 pixels for BC3 and 1,500 for BC7, and a -98 by 68 description (not whole blocks) must be refused. Restoring the -64-texel check fails the 100 by 68 BC3 case at its first texel row. On -Windows 11 build 26300 with the debug layer off, RTX 5090 (32.0.16.2135) and -WARP (10.0.26100.10068) matched all 65,536 RGBA pixels per case. +distinct valid blocks; texels outside the texture must load as zero, and the +log must report 16,384 tiled bytes. A reference that uses the tile pitch of the +unpadded width (one tile instead of two) differs from the image in 1,600 pixels +for BC3 and 1,500 for BC7. The test also requires refusals for 98 by 68 and +100 by 66 (not whole blocks), 1028 by 1028 (past the reported size) and a +100 by 68 view over memory announced as an XG copy. Each of these changes makes +the test fail: restoring the 64-texel check, dropping the height check, +dropping the announced-copy refusal, or rounding the tile rows down in +`Bc16Surface`. The last also fails `startup_bc7_4k_s`. On Windows 11 build +26300 with the debug layer off, RTX 5090 (32.0.16.2135) and WARP +(10.0.26100.10068) matched all 65,536 RGBA pixels per case. ```powershell cmake --build --preset release --target startup_stream_consumer startup_stub_consumer d3d12_xs_shim -ctest --preset release --no-tests=error --output-on-failure -R '^(startup_address_(bc_view|srv)_(hardware|warp)|startup_xg_bc7_layout|bc7_tile_swizzle)$' +ctest --preset release --no-tests=error --output-on-failure -R '^(startup_address_(bc_view|srv)_(hardware|warp)|startup_xg_bc7_layout|startup_bc7_4k_s|bc7_tile_swizzle)$' ``` ### Six BC5/BC7 mips on a 64 KiB standard layout diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index 3ef205008f..5573ed6f28 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -24,25 +24,37 @@ bool tiled_bc_srv(const D3D12_RESOURCE_DESC& r) r.Width && r.Height && r.Width % 4 == 0 && r.Height % 4 == 0; } -// Content summary of a published view whose last tile row or column is partial: a title run can tell -// console-tiled data from zeros or rows written by the XG stand-in (which announces its copies). -void log_partial_tile_view(const D3D12_RESOURCE_DESC& r, u64 address, bool announced, bool published) +// One line for the first publication of a view whose last tile row or column is partial. A published +// view gets a content summary, so a title run can tell console-tiled data from zeros; a held one names +// why it stays refused. +void log_partial_tile_view(const D3D12_RESOURCE_DESC& r, u64 address, const char* held, bool published) { - static LONG lines; - if (!first_calls(lines, 64)) return; + static LONG lines, unpublished; const auto bytes = x2w_startup::Bc16Surface(r).size(); + if (!published) { + if (first_calls(unpublished, 8)) + log("x2w-d3d12 Device::slot70 partial-tile BC view %llux%u format %d layout 0x%x: %s, %llu tiled bytes", + r.Width, r.Height, r.Format, static_cast(r.Layout), held ? held : "texture_unavailable", bytes); + return; + } + if (!first_calls(lines, 64)) return; const auto* data = reinterpret_cast(address); - const bool readable = x2w_local::readable(data, bytes); const auto nonzero = [&](u64 from) { unsigned count = 0; for (u64 i = from; i < from + 4096; ++i) count += data[i] != 0; return count; }; - log("x2w-d3d12 Device::slot70 partial-tile BC view %llux%u format %d layout 0x%x: %s, XG-announced write %d, " - "%llu tiled bytes%s nonzero first/last 4 KiB %u/%u hash %016llx", r.Width, r.Height, r.Format, - static_cast(r.Layout), published ? "published" : "texture_unavailable", announced, bytes, - readable ? "," : " unreadable,", readable ? nonzero(0) : 0, readable ? nonzero(bytes - 4096) : 0, - readable ? hash_memory(data, static_cast(bytes)) : 0); + // publish_tiled_texture already found the whole range readable. + log("x2w-d3d12 Device::slot70 partial-tile BC view %llux%u format %d layout 0x%x: published, %llu tiled bytes, " + "nonzero first/last 4 KiB %u/%u hash %016llx", r.Width, r.Height, r.Format, static_cast(r.Layout), + bytes, nonzero(0), nonzero(bytes - 4096), hash_memory(data, static_cast(bytes))); +} + +// The size the XG stand-in reports for a description: desktop copyable footprints rounded to 64 KiB. +u64 xg_reported_bytes(Wrapper* device, const D3D12_RESOURCE_DESC& desc) +{ + const x2w_startup::SliceFootprints plan(device_of(device), sanitized(desc), 1); + return plan.size == UINT64_MAX ? 0 : (plan.size + 0xFFFF) & ~u64{0xFFFF}; } bool supports_memory_srv(const D3D12_RESOURCE_DESC& r, const D3D12_SHADER_RESOURCE_VIEW_DESC& v) @@ -253,23 +265,31 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou view_slot<18, 2>(self, reinterpret_cast(buffer), reinterpret_cast(&adjusted), destination, 0); return; } - // Before the first publication at an address, only an XG stand-in copy leaves a write record there. - bool first = false, announced = false; + // A partial-tile view stays refused when the XG stand-in copied desktop-footprint rows to the address + // (before the first publication there, only such a copy leaves a write record), or when its padded + // tiles run past the size the stand-in reported, which the title may not own. + bool first = false; + const char* held = nullptr; if (tiled_bc_srv(*resource) && (resource->Width % 64 || resource->Height % 64)) { { std::lock_guard lock(tiled_srv_mutex); first = !tiled_srv_addresses.count(input->address); } AcquireSRWLockShared(&written_lock); - announced = written_textures().count(input->address) != 0; + const bool announced = written_textures().count(input->address) != 0; ReleaseSRWLockShared(&written_lock); + if (first && announced) held = "partial_tile_xg_rows"; + else if (x2w_startup::Bc16Surface(*resource).size() > xg_reported_bytes(self, *resource)) + held = "partial_tile_beyond_xg_size"; } - Wrapper* texture = (tiled_bc_srv(*resource) || x2w_startup::bc64::supports(*resource)) ? publish_tiled_texture(self, input->address, *resource) + Wrapper* texture = held ? nullptr + : (tiled_bc_srv(*resource) || x2w_startup::bc64::supports(*resource)) ? publish_tiled_texture(self, input->address, *resource) : existing_memory_texture(input->address, *resource); - if (first) log_partial_tile_view(*resource, input->address, announced, texture != nullptr); + if (first) log_partial_tile_view(*resource, input->address, held, texture != nullptr); if (!texture) { if (first_calls(refused, 8)) - log_refused_memory_srv("unavailable texture at the supplied base and layout", "texture_unavailable", resource, input, destination); + log_refused_memory_srv("unavailable texture at the supplied base and layout", held ? held : "texture_unavailable", + resource, input, destination); return; } view_slot<18, 2>(self, reinterpret_cast(texture), reinterpret_cast(&input->view), destination, 0); diff --git a/tests/startup_address_bc_view_check.py b/tests/startup_address_bc_view_check.py index a41357a0cb..f7828f82e7 100644 --- a/tests/startup_address_bc_view_check.py +++ b/tests/startup_address_bc_view_check.py @@ -91,16 +91,25 @@ def check(directory, consumer, backend, fmt, width, height): if unpadded == 0: raise AssertionError("The unpadded tile pitch did not change the image") log = trap_log.read_text(errors="replace") - refusal = (rf"Device::slot70 unsupported memory SRV arguments: reason=unsupported_description " - rf"res\{{dim=3 fmt={77 if fmt == 'BC3' else 98} size={width - 2}x{height}x1 ") - if not re.search(refusal, log): - raise AssertionError(f"{fmt}: no refusal for {width - 2}x{height} (not whole blocks)") - partial = re.findall(r"partial-tile BC view (\d+)x(\d+) .*: published, XG-announced write 0", log) - # One line per first publication at an address: the first backing and the exchanged one. - if partial != ([] if width % 64 == 0 and height % 64 == 0 else [(str(width), str(height))] * 2): - raise AssertionError(f"{fmt}: partial-tile lines {partial}") + code = 77 if fmt == "BC3" else 98 + partial = width % 64 or height % 64 + tiled_bytes = 16384 # 2 x 2 tiles of 4 KiB, partial or not + refusals = [("unsupported_description", width - 2, height), ("unsupported_description", width, height - 2), + ("partial_tile_beyond_xg_size", 1028, 1028)] + if partial: + refusals.append(("partial_tile_xg_rows", width, height)) + for reason, w, h in refusals: + if not re.search(rf"Device::slot70 .*: reason={reason} res\{{dim=3 fmt={code} size={w}x{h}x1 ", log): + raise AssertionError(f"{fmt}: no {reason} refusal for {w}x{h}") + lines = re.findall(r"partial-tile BC view (\d+x\d+) format \d+ layout 0x105: (\w+), (\d+) tiled bytes", log) + # One line per first publication at an address (the first backing and the exchanged one), and one + # for the announced backing, which stays refused. + view = f"{width}x{height}" + expected = [(view, "published", str(tiled_bytes))] * 2 + [(view, "partial_tile_xg_rows", str(tiled_bytes))] + if lines != (expected if partial else []): + raise AssertionError(f"{fmt}: partial-tile lines {lines}, expected {expected if partial else []}") print(f"{backend} {fmt} {width}x{height}: {4 * WIDTH * HEIGHT} RGBA pixels exact; exchanged tiles differ in " - f"{swapped} texels; unpadded tile pitch differs in {unpadded}; {width - 2}x{height} refused") + f"{swapped} texels; unpadded tile pitch differs in {unpadded}; {len(refusals)} refusals as expected") return 0 diff --git a/tests/startup_address_srv.cpp b/tests/startup_address_srv.cpp index 2f93137e93..b03018cf18 100644 --- a/tests/startup_address_srv.cpp +++ b/tests/startup_address_srv.cpp @@ -9,6 +9,7 @@ using Word = std::uint64_t; Word invoke(void*, unsigned, Word = 0, Word = 0, Word = 0, Word = 0, Word = 0, Word = 0, Word = 0, Word = 0); +extern "C" __declspec(dllimport) void x2w_d3d12_texture_written(void* base, unsigned long long bytes); namespace { template Word ptr(T* value) { return reinterpret_cast(value); } struct ViewAt { @@ -170,9 +171,27 @@ int draw_bc_address_srvs(void* guest, void* list) invoke(list, 22, 1, ptr(®ion)); invoke(list, 12, 3, 1); } - // Not whole 4x4 blocks: refused (the check reads the log), with nothing drawn after it. - desc.Width -= 2; - invoke(guest, 70, ptr(&desc), ptr(&v), cpu); + // Refused, with nothing drawn after them (the check reads the log): not whole 4x4 blocks in either + // direction, and padded tiles past the size XG reports (1028 x 1028 pads to 1,183,744 bytes; the + // footprint rounded to 64 KiB is 1,179,648). + const auto refused = [&](UINT64 w, UINT h) { + auto shape = desc; + shape.Width = w; + shape.Height = h; + v.address = ptr(backing[0].data()); + invoke(guest, 70, ptr(&shape), ptr(&v), cpu); + }; + refused(width - 2, height); + refused(width, height - 2); + refused(1028, 1028); + // A partial-tile view over memory the XG stand-in announced as written (desktop-footprint rows) stays + // refused. A full-tile view is not affected, so only the partial size is published here. + if (width % 64 || height % 64) { + alignas(4096) static std::array announced{}; + x2w_d3d12_texture_written(announced.data(), announced.size()); + v.address = ptr(announced.data()); + invoke(guest, 70, ptr(&desc), ptr(&v), cpu); + } return 0; } From 70bc97fc61299c55957cbe7cd58d3d7ea0dba3bf Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 00:32:57 +0100 Subject: [PATCH 5/8] tests: translate the BC view pipeline once per run, keep the 90 s limit startup_address_bc_view translated the same pipeline (gfx2hlsl and FXC) for every case, which dominated its run time; with four cases it came close to the 90 s limit on a loaded machine. Translate once per run and draw every case from that cache, and restore the 90 s limit. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 2 +- tests/startup_address_bc_view_check.py | 46 ++++++++++++++++++++------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3512c8bf85..68ce26e011 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2366,7 +2366,7 @@ if(EXISTS "${INTAKE_PYTHON}") add_test(NAME startup_address_bc_view_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_bc_view_check.py" "$" "${backend}") - set_tests_properties(startup_address_bc_view_${backend} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77) + set_tests_properties(startup_address_bc_view_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) add_test(NAME startup_xg_bc_chain_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_xg_bc_chain_check.py" "$" "$" "${backend}") diff --git a/tests/startup_address_bc_view_check.py b/tests/startup_address_bc_view_check.py index f7828f82e7..07e889a077 100644 --- a/tests/startup_address_bc_view_check.py +++ b/tests/startup_address_bc_view_check.py @@ -2,14 +2,18 @@ from pathlib import Path import re import struct +import subprocess import sys import tempfile from bc7_draw_check import make_block as bc7_block from bc7_tiles_check import block_index -from startup_address_srv_check import draw_texture +from startup_address_srv_check import prepare_draw_cache WIDTH, HEIGHT = 128, 128 +HLSL = ("Texture2D t : register(t0);\n" + "float4 main(float4 p : SV_Position) : SV_Target {\n" + f" return t.Load(int3(uint(p.x) % {WIDTH}, uint(p.y), 0));\n}}\n") def bc3_block(x, y): @@ -56,18 +60,35 @@ def expected_panels(rgba, width, height, tile_pitch): return bytes(image) # texels outside the texture load as zero -def check(directory, consumer, backend, fmt, width, height): +class Drawer: + """Translates the pipeline once, then draws each case with the same shader cache.""" + + def __init__(self, directory, consumer, backend): + self.directory, self.consumer, self.backend, self.prepared = directory, consumer, backend, None + + def __call__(self, modes): + if self.prepared is None: + self.prepared = prepare_draw_cache(self.directory, self.consumer, HLSL, modes, "srv 0 tex2d table 0 0") + env, args, output = self.prepared + env = {key: value for key, value in env.items() if key != "X2W_TEST_BC3"} # the consumer tests presence + result = subprocess.run([*args, self.backend, output], env={**env, **modes}, capture_output=True, text=True) + print(result.stdout, end="") + if result.returncode == 77: + return None + if result.returncode: + raise RuntimeError(f"consumer failed ({result.returncode})\n{result.stderr[-5000:]}") + return output.read_bytes() + + +def check(directory, draw, backend, fmt, width, height): tiled, rgba = make_surface(bc3_block if fmt == "BC3" else bc7_block) source = directory / "tiled.bin" source.write_bytes(tiled) - hlsl = ("Texture2D t : register(t0);\n" - "float4 main(float4 p : SV_Position) : SV_Target {\n" - f" return t.Load(int3(uint(p.x) % {WIDTH}, uint(p.y), 0));\n}}\n") trap_log = directory / "trap.log" modes = {"X2W_TEST_BC_VIEW": str(source), "X2W_TEST_BC_SIZE": f"{width}x{height}", "X2W_TRAP_LOG": str(trap_log)} if fmt == "BC3": modes["X2W_TEST_BC3"] = "1" - actual = draw_texture(directory, consumer, backend, hlsl, modes) + actual = draw(modes) if actual is None: return 77 if len(actual) != WIDTH * 4 * HEIGHT * 4: @@ -114,8 +135,11 @@ def check(directory, consumer, backend, fmt, width, height): if __name__ == "__main__": - for fmt, width, height in (("BC3", 128, 128), ("BC7", 128, 128), ("BC3", 100, 68), ("BC7", 100, 68)): - with tempfile.TemporaryDirectory() as temporary: - result = check(Path(temporary), *sys.argv[1:], fmt, width, height) - if result: - sys.exit(result) + consumer, backend = sys.argv[1:3] + with tempfile.TemporaryDirectory() as shared: + draw = Drawer(Path(shared), consumer, backend) + for fmt, width, height in (("BC3", 128, 128), ("BC7", 128, 128), ("BC3", 100, 68), ("BC7", 100, 68)): + with tempfile.TemporaryDirectory() as temporary: + result = check(Path(temporary), draw, backend, fmt, width, height) + if result: + sys.exit(result) From b52f56af820fde42b62e90ccdc93ed00296c2140 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 01:17:09 +0100 Subject: [PATCH 6/8] startup: publish partial-tile BC views over opt-in tiled BC7 copies; test boundaries The announced-copy refusal now applies only to XG copies of desktop rows: the opt-in BC7 4 KiB copy announces tiled blocks (with their size), which detile correctly. startup_address_bc_view adds a 256x196 view (only the height is partial; padded size equal to the reported 64 KiB) that must be published, and a 100x68 view over an opt-in BC7 copy that must be published. Each case starts with no previous pixel output. Co-Authored-By: Claude Opus 5.5 --- docs/experiments.md | 15 +++++++++++---- src/startup_stubs/d3d12_memory_srv.inl | 6 ++++-- tests/startup_address_bc_view_check.py | 15 ++++++++++----- tests/startup_address_srv.cpp | 23 +++++++++++++++++------ 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 5a41000b4d..b99a797cf3 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11729,6 +11729,9 @@ layout probe retain their existing behavior. A view with a partial last tile stays refused in two cases. The first is when the XG stand-in announced a copy to that address before the view was first published there, because that copy wrote desktop-footprint rows, not tiles. +A copy from the opt-in BC7 4 KiB layout writes tiles and does not hold the +view. The record of a copy is never cleared, so an address once written with +rows keeps its partial-tile views refused, as all of them were before. The second is when the padded tiles are larger than the size the XG stand-in reports for the description (the desktop footprint rounded to 64 KiB): the bytes past that size may belong to something else, and a readability probe @@ -11759,10 +11762,14 @@ log must report 16,384 tiled bytes. A reference that uses the tile pitch of the unpadded width (one tile instead of two) differs from the image in 1,600 pixels for BC3 and 1,500 for BC7. The test also requires refusals for 98 by 68 and 100 by 66 (not whole blocks), 1028 by 1028 (past the reported size) and a -100 by 68 view over memory announced as an XG copy. Each of these changes makes -the test fail: restoring the 64-texel check, dropping the height check, -dropping the announced-copy refusal, or rounding the tile rows down in -`Bc16Surface`. The last also fails `startup_bc7_4k_s`. On Windows 11 build +100 by 68 view over memory announced as an XG copy. It requires publication of +a 256 by 196 view, whose padded size equals the reported 64 KiB and whose width +is whole tiles, and of a 100 by 68 view over memory announced by the opt-in BC7 +copy. Each of these changes makes the test fail: restoring the 64-texel check, +dropping the height check, requiring both dimensions to be partial, refusing at +an equal size, dropping the announced-copy refusal, ignoring which copy was +announced, or rounding the tile rows down in `Bc16Surface`. The last also fails +`startup_bc7_4k_s`. On Windows 11 build 26300 with the debug layer off, RTX 5090 (32.0.16.2135) and WARP (10.0.26100.10068) matched all 65,536 RGBA pixels per case. diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index 5573ed6f28..4f600a1ae0 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -276,9 +276,11 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou first = !tiled_srv_addresses.count(input->address); } AcquireSRWLockShared(&written_lock); - const bool announced = written_textures().count(input->address) != 0; + const auto found = written_textures().find(input->address); + // The opt-in BC7 4 KiB copy announces tiled blocks (with their size); a plain XG copy, rows. + const bool rows = found != written_textures().end() && !found->second.bc7_width; ReleaseSRWLockShared(&written_lock); - if (first && announced) held = "partial_tile_xg_rows"; + if (first && rows) held = "partial_tile_xg_rows"; else if (x2w_startup::Bc16Surface(*resource).size() > xg_reported_bytes(self, *resource)) held = "partial_tile_beyond_xg_size"; } diff --git a/tests/startup_address_bc_view_check.py b/tests/startup_address_bc_view_check.py index 07e889a077..717e25fd9b 100644 --- a/tests/startup_address_bc_view_check.py +++ b/tests/startup_address_bc_view_check.py @@ -70,6 +70,7 @@ def __call__(self, modes): if self.prepared is None: self.prepared = prepare_draw_cache(self.directory, self.consumer, HLSL, modes, "srv 0 tex2d table 0 0") env, args, output = self.prepared + output.unlink(missing_ok=True) # no case reads the previous case's pixels env = {key: value for key, value in env.items() if key != "X2W_TEST_BC3"} # the consumer tests presence result = subprocess.run([*args, self.backend, output], env={**env, **modes}, capture_output=True, text=True) print(result.stdout, end="") @@ -123,12 +124,16 @@ def check(directory, draw, backend, fmt, width, height): if not re.search(rf"Device::slot70 .*: reason={reason} res\{{dim=3 fmt={code} size={w}x{h}x1 ", log): raise AssertionError(f"{fmt}: no {reason} refusal for {w}x{h}") lines = re.findall(r"partial-tile BC view (\d+x\d+) format \d+ layout 0x105: (\w+), (\d+) tiled bytes", log) - # One line per first publication at an address (the first backing and the exchanged one), and one - # for the announced backing, which stays refused. + # One line per first publication at an address: the first backing and the exchanged one, the 256x196 + # boundary (padded size equal to the reported size), then the backing announced as XG rows (refused) + # and the one announced as opt-in tiled BC7 blocks (published). view = f"{width}x{height}" - expected = [(view, "published", str(tiled_bytes))] * 2 + [(view, "partial_tile_xg_rows", str(tiled_bytes))] - if lines != (expected if partial else []): - raise AssertionError(f"{fmt}: partial-tile lines {lines}, expected {expected if partial else []}") + boundary = [("256x196", "published", "65536")] + expected = ([(view, "published", str(tiled_bytes))] * 2 + boundary + + [(view, "partial_tile_xg_rows", str(tiled_bytes)), (view, "published", str(tiled_bytes))] + if partial else boundary) + if lines != expected: + raise AssertionError(f"{fmt}: partial-tile lines {lines}, expected {expected}") print(f"{backend} {fmt} {width}x{height}: {4 * WIDTH * HEIGHT} RGBA pixels exact; exchanged tiles differ in " f"{swapped} texels; unpadded tile pitch differs in {unpadded}; {len(refusals)} refusals as expected") return 0 diff --git a/tests/startup_address_srv.cpp b/tests/startup_address_srv.cpp index b03018cf18..2b41e8d53f 100644 --- a/tests/startup_address_srv.cpp +++ b/tests/startup_address_srv.cpp @@ -10,6 +10,7 @@ using Word = std::uint64_t; Word invoke(void*, unsigned, Word = 0, Word = 0, Word = 0, Word = 0, Word = 0, Word = 0, Word = 0, Word = 0); extern "C" __declspec(dllimport) void x2w_d3d12_texture_written(void* base, unsigned long long bytes); +extern "C" __declspec(dllimport) void x2w_d3d12_bc7_4k_s_written(void* base, UINT width, UINT height); namespace { template Word ptr(T* value) { return reinterpret_cast(value); } struct ViewAt { @@ -184,13 +185,23 @@ int draw_bc_address_srvs(void* guest, void* list) refused(width - 2, height); refused(width, height - 2); refused(1028, 1028); - // A partial-tile view over memory the XG stand-in announced as written (desktop-footprint rows) stays - // refused. A full-tile view is not affected, so only the partial size is published here. + // Published at the boundary: 256 x 196 (only the height partial) pads to 64 KiB, exactly the reported size. + alignas(4096) static std::array boundary{}; + auto exact = desc; + exact.Width = 256; + exact.Height = 196; + v.address = ptr(boundary.data()); + invoke(guest, 70, ptr(&exact), ptr(&v), cpu); + // A partial-tile view over memory the XG stand-in announced as copied rows stays refused; over blocks + // its opt-in BC7 4 KiB copy announced as tiled, it is published. Only partial sizes are affected. if (width % 64 || height % 64) { - alignas(4096) static std::array announced{}; - x2w_d3d12_texture_written(announced.data(), announced.size()); - v.address = ptr(announced.data()); - invoke(guest, 70, ptr(&desc), ptr(&v), cpu); + alignas(4096) static std::array, 2> announced{}; + x2w_d3d12_texture_written(announced[0].data(), announced[0].size()); + x2w_d3d12_bc7_4k_s_written(announced[1].data(), width, height); + for (const auto& backing : announced) { + v.address = ptr(backing.data()); + invoke(guest, 70, ptr(&desc), ptr(&v), cpu); + } } return 0; } From fc958fb5566c9d08d458e69f52c347d290f06181 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 01:58:57 +0100 Subject: [PATCH 7/8] startup: refuse partial-tile BC views after the latest XG row copy, not only before the first The refusal over desktop-footprint rows was gated on the address never having been published, so a valid publication followed by an XG row copy and a second slot 70 request published the old tiled resource again and read the rows as tiles. The write record now keeps whether the last XG copy wrote desktop rows or opt-in BC7 tiles, and the refusal follows it. startup_address_bc_view adds that sequence: the second request must be refused and leave the destination naming the view it held before. Co-Authored-By: Claude Opus 5.5 --- docs/experiments.md | 14 ++++++++------ src/startup_stubs/d3d12_memory_srv.inl | 10 ++++------ src/startup_stubs/d3d12_xs_device.cpp | 2 ++ tests/startup_address_bc_view_check.py | 12 ++++++++---- tests/startup_address_srv.cpp | 13 +++++++++++++ 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index e4f1db7624..18aa924652 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11802,11 +11802,11 @@ footprint rows before uploading them. Ordinary placed R8 views and the XG BC7 layout probe retain their existing behavior. A view with a partial last tile stays refused in two cases. The first is when -the XG stand-in announced a copy to that address before the view was first -published there, because that copy wrote desktop-footprint rows, not tiles. -A copy from the opt-in BC7 4 KiB layout writes tiles and does not hold the -view. The record of a copy is never cleared, so an address once written with -rows keeps its partial-tile views refused, as all of them were before. +the last copy the XG stand-in announced to that address wrote desktop-footprint +rows, not tiles, whether that copy came before the first publication there or +after it. A copy from the opt-in BC7 4 KiB layout writes tiles and does not hold +the view. A view published before such a row copy keeps its resource and reads +the address as tiles; only new requests are refused. The second is when the padded tiles are larger than the size the XG stand-in reports for the description (the desktop footprint rounded to 64 KiB): the bytes past that size may belong to something else, and a readability probe @@ -11837,7 +11837,9 @@ log must report 16,384 tiled bytes. A reference that uses the tile pitch of the unpadded width (one tile instead of two) differs from the image in 1,600 pixels for BC3 and 1,500 for BC7. The test also requires refusals for 98 by 68 and 100 by 66 (not whole blocks), 1028 by 1028 (past the reported size) and a -100 by 68 view over memory announced as an XG copy. It requires publication of +100 by 68 view over memory announced as an XG copy, and a second request for a +100 by 68 address after a row copy over its published view; that request must +leave the destination descriptor naming the view it held. It requires publication of a 256 by 196 view, whose padded size equals the reported 64 KiB and whose width is whole tiles, and of a 100 by 68 view over memory announced by the opt-in BC7 copy. Each of these changes makes the test fail: restoring the 64-texel check, diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index 4f600a1ae0..b5719314d6 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -265,9 +265,8 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou view_slot<18, 2>(self, reinterpret_cast(buffer), reinterpret_cast(&adjusted), destination, 0); return; } - // A partial-tile view stays refused when the XG stand-in copied desktop-footprint rows to the address - // (before the first publication there, only such a copy leaves a write record), or when its padded - // tiles run past the size the stand-in reported, which the title may not own. + // A partial-tile view stays refused when the last XG stand-in copy to the address wrote desktop-footprint + // rows, or when its padded tiles run past the size the stand-in reported, which the title may not own. bool first = false; const char* held = nullptr; if (tiled_bc_srv(*resource) && (resource->Width % 64 || resource->Height % 64)) { @@ -277,10 +276,9 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou } AcquireSRWLockShared(&written_lock); const auto found = written_textures().find(input->address); - // The opt-in BC7 4 KiB copy announces tiled blocks (with their size); a plain XG copy, rows. - const bool rows = found != written_textures().end() && !found->second.bc7_width; + const bool rows = found != written_textures().end() && found->second.desktop_rows; ReleaseSRWLockShared(&written_lock); - if (first && rows) held = "partial_tile_xg_rows"; + if (rows) held = "partial_tile_xg_rows"; else if (x2w_startup::Bc16Surface(*resource).size() > xg_reported_bytes(self, *resource)) held = "partial_tile_beyond_xg_size"; } diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 23966aeb7c..447a609bf9 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -2384,6 +2384,7 @@ extern SRWLOCK written_lock; struct TextureWrite { u64 generation = 0; UINT bc7_width = 0, bc7_height = 0; + bool desktop_rows = false; // the last XG copy wrote desktop-footprint rows (not the opt-in BC7 tiles) }; std::unordered_map& written_textures(); ID3D12Resource* sampled_address_texture(Wrapper* resource); @@ -3816,6 +3817,7 @@ void note_texture_write(void* base, UINT bc7_width, UINT bc7_height) ++written.generation; written.bc7_width = bc7_width; written.bc7_height = bc7_height; + written.desktop_rows = !bc7_width; ReleaseSRWLockExclusive(&written_lock); } } // namespace diff --git a/tests/startup_address_bc_view_check.py b/tests/startup_address_bc_view_check.py index 717e25fd9b..def41aa201 100644 --- a/tests/startup_address_bc_view_check.py +++ b/tests/startup_address_bc_view_check.py @@ -125,13 +125,17 @@ def check(directory, draw, backend, fmt, width, height): raise AssertionError(f"{fmt}: no {reason} refusal for {w}x{h}") lines = re.findall(r"partial-tile BC view (\d+x\d+) format \d+ layout 0x105: (\w+), (\d+) tiled bytes", log) # One line per first publication at an address: the first backing and the exchanged one, the 256x196 - # boundary (padded size equal to the reported size), then the backing announced as XG rows (refused) - # and the one announced as opt-in tiled BC7 blocks (published). + # boundary (padded size equal to the reported size), the backing announced as XG rows (refused), the + # one announced as opt-in tiled BC7 blocks, and the one published before an XG row copy over it. view = f"{width}x{height}" boundary = [("256x196", "published", "65536")] - expected = ([(view, "published", str(tiled_bytes))] * 2 + boundary + - [(view, "partial_tile_xg_rows", str(tiled_bytes)), (view, "published", str(tiled_bytes))] + published = (view, "published", str(tiled_bytes)) + expected = ([published] * 2 + boundary + [(view, "partial_tile_xg_rows", str(tiled_bytes)), published, published] if partial else boundary) + # Rows announced before the first publication, and rows copied over a published view: both refused. + held = re.findall(rf"reason=partial_tile_xg_rows res\{{dim=3 fmt={code} size={view}x1 ", log) + if len(held) != (2 if partial else 0): + raise AssertionError(f"{fmt}: {len(held)} partial_tile_xg_rows refusals for {view}") if lines != expected: raise AssertionError(f"{fmt}: partial-tile lines {lines}, expected {expected}") print(f"{backend} {fmt} {width}x{height}: {4 * WIDTH * HEIGHT} RGBA pixels exact; exchanged tiles differ in " diff --git a/tests/startup_address_srv.cpp b/tests/startup_address_srv.cpp index 2b41e8d53f..83fb85561e 100644 --- a/tests/startup_address_srv.cpp +++ b/tests/startup_address_srv.cpp @@ -202,6 +202,19 @@ int draw_bc_address_srvs(void* guest, void* list) v.address = ptr(backing.data()); invoke(guest, 70, ptr(&desc), ptr(&v), cpu); } + // Published first, then overwritten by an XG row copy: a second request is refused and leaves the + // destination naming the view it held before (the first backing's). + alignas(4096) static std::array reused{}; + v.address = ptr(reused.data()); + invoke(guest, 70, ptr(&desc), ptr(&v), cpu); + v.address = ptr(backing[0].data()); + invoke(guest, 70, ptr(&desc), ptr(&v), cpu); + v.address = ptr(reused.data()); + std::array written{}; + std::memcpy(written.data(), reinterpret_cast(cpu), written.size()); + x2w_d3d12_texture_written(reused.data(), reused.size()); + invoke(guest, 70, ptr(&desc), ptr(&v), cpu); + if (std::memcmp(written.data(), reinterpret_cast(cpu), written.size())) return 44; } return 0; } From a289585b120843d7488527bfe706f4deef39022a Mon Sep 17 00:00:00 2001 From: Royoo <3230853833@qq.com> Date: Wed, 25 Nov 2026 09:18:07 +0800 Subject: [PATCH 8/8] startup: reject row writes when sampling an existing partial BC view --- docs/experiments.md | 8 +++++--- src/startup_stubs/d3d12_memory_srv.inl | 26 ++++++++++++++++++++------ tests/startup_address_bc_view_check.py | 4 +++- tests/startup_address_srv.cpp | 9 ++++++--- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 18aa924652..c9f3e59207 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11805,8 +11805,9 @@ A view with a partial last tile stays refused in two cases. The first is when the last copy the XG stand-in announced to that address wrote desktop-footprint rows, not tiles, whether that copy came before the first publication there or after it. A copy from the opt-in BC7 4 KiB layout writes tiles and does not hold -the view. A view published before such a row copy keeps its resource and reads -the address as tiles; only new requests are refused. +the view. A row copy leaves previously published descriptor bytes intact, +and the translated sampler binds a null texture while the last write is a row +copy. It can sample the view again after a tiled write is announced. The second is when the padded tiles are larger than the size the XG stand-in reports for the description (the desktop footprint rounded to 64 KiB): the bytes past that size may belong to something else, and a readability probe @@ -11839,7 +11840,8 @@ for BC3 and 1,500 for BC7. The test also requires refusals for 98 by 68 and 100 by 66 (not whole blocks), 1028 by 1028 (past the reported size) and a 100 by 68 view over memory announced as an XG copy, and a second request for a 100 by 68 address after a row copy over its published view; that request must -leave the destination descriptor naming the view it held. It requires publication of +leave the destination descriptor naming the view it held. A draw through the +already-published token after a row write must read zero. The test requires publication of a 256 by 196 view, whose padded size equals the reported 64 KiB and whose width is whole tiles, and of a 100 by 68 view over memory announced by the opt-in BC7 copy. Each of these changes makes the test fail: restoring the 64-texel check, diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index b5719314d6..6ddf9b9670 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -168,6 +168,20 @@ struct TiledAddressTexture { std::unordered_map tiled_srv_textures; std::unordered_multimap tiled_srv_addresses; +bool partial_tiled_bc(const D3D12_RESOURCE_DESC& desc) +{ + return tiled_bc_srv(desc) && (desc.Width % 64 || desc.Height % 64); +} + +bool latest_texture_write_is_rows(u64 address) +{ + AcquireSRWLockShared(&written_lock); + const auto found = written_textures().find(address); + const bool rows = found != written_textures().end() && found->second.desktop_rows; + ReleaseSRWLockShared(&written_lock); + return rows; +} + Wrapper* publish_tiled_texture(Wrapper* device, u64 address, const D3D12_RESOURCE_DESC& desc) { const auto bytes = x2w_startup::bc64::supports(desc) ? x2w_startup::bc64::Chain(desc).bytes() : x2w_startup::Bc16Surface(desc).size(); @@ -199,6 +213,10 @@ ID3D12Resource* sampled_address_texture(Wrapper* resource) if (!resource->own().tiled_address_srv) return static_cast(resource->own().real); std::lock_guard lock(tiled_srv_mutex); auto& sampled = tiled_srv_textures.at(resource); + // An already-published token can outlive a write in a different layout. + if (partial_tiled_bc(sampled.desc) && + latest_texture_write_is_rows(reinterpret_cast(resource->own().title_memory))) + return nullptr; if (!sampled.desktop) { const auto desc = sanitized(sampled.desc); const D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_DEFAULT}; @@ -269,16 +287,12 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou // rows, or when its padded tiles run past the size the stand-in reported, which the title may not own. bool first = false; const char* held = nullptr; - if (tiled_bc_srv(*resource) && (resource->Width % 64 || resource->Height % 64)) { + if (partial_tiled_bc(*resource)) { { std::lock_guard lock(tiled_srv_mutex); first = !tiled_srv_addresses.count(input->address); } - AcquireSRWLockShared(&written_lock); - const auto found = written_textures().find(input->address); - const bool rows = found != written_textures().end() && found->second.desktop_rows; - ReleaseSRWLockShared(&written_lock); - if (rows) held = "partial_tile_xg_rows"; + if (latest_texture_write_is_rows(input->address)) held = "partial_tile_xg_rows"; else if (x2w_startup::Bc16Surface(*resource).size() > xg_reported_bytes(self, *resource)) held = "partial_tile_beyond_xg_size"; } diff --git a/tests/startup_address_bc_view_check.py b/tests/startup_address_bc_view_check.py index def41aa201..6613a23a3d 100644 --- a/tests/startup_address_bc_view_check.py +++ b/tests/startup_address_bc_view_check.py @@ -48,9 +48,11 @@ def source_block(x, y, tile_pitch, swapped): def expected_panels(rgba, width, height, tile_pitch): - """Four panels of 128x128 texels: views of the first, swapped, first (kept) and first backing.""" + """Four panels of 128x128 texels. A partial view with a later row write reads as null.""" image = bytearray(4 * WIDTH * 4 * HEIGHT) for panel, swapped in enumerate((0, 1, 1, 0)): + if panel == 3 and (width % 64 or height % 64): + continue for y in range(height): for x in range(width): bx, by = source_block(x // 4, y // 4, tile_pitch, swapped) diff --git a/tests/startup_address_srv.cpp b/tests/startup_address_srv.cpp index 83fb85561e..1042acec09 100644 --- a/tests/startup_address_srv.cpp +++ b/tests/startup_address_srv.cpp @@ -168,6 +168,9 @@ int draw_bc_address_srvs(void* guest, void* list) for (LONG i = 0; i < 4; ++i) { v.address = addresses[i]; invoke(guest, 70, ptr(&desc), ptr(&v), cpu); + // Keep the already-published descriptor while the backing changes to a row layout. + if (i == 3 && (width % 64 || height % 64)) + x2w_d3d12_texture_written(backing[0].data(), backing[0].size()); const D3D12_RECT region{i * LONG(panel), 0, (i + 1) * LONG(panel), LONG(panel)}; invoke(list, 22, 1, ptr(®ion)); invoke(list, 12, 3, 1); @@ -179,7 +182,7 @@ int draw_bc_address_srvs(void* guest, void* list) auto shape = desc; shape.Width = w; shape.Height = h; - v.address = ptr(backing[0].data()); + v.address = ptr(backing[1].data()); invoke(guest, 70, ptr(&shape), ptr(&v), cpu); }; refused(width - 2, height); @@ -203,11 +206,11 @@ int draw_bc_address_srvs(void* guest, void* list) invoke(guest, 70, ptr(&desc), ptr(&v), cpu); } // Published first, then overwritten by an XG row copy: a second request is refused and leaves the - // destination naming the view it held before (the first backing's). + // destination naming the view it held before (the second backing's). alignas(4096) static std::array reused{}; v.address = ptr(reused.data()); invoke(guest, 70, ptr(&desc), ptr(&v), cpu); - v.address = ptr(backing[0].data()); + v.address = ptr(backing[1].data()); invoke(guest, 70, ptr(&desc), ptr(&v), cpu); v.address = ptr(reused.data()); std::array written{};