From 80443a788123a91e2bec9cb2117d96a9504134c3 Mon Sep 17 00:00:00 2001 From: lewisalex86 Date: Thu, 26 Nov 2026 21:09:37 +0000 Subject: [PATCH] startup: read linear (0x100) textures in slot 70 Slot 70 refused a 2D texture whose Layout is the ADDR_SW_LINEAR word 0x100 because the extent of its rows was not established. Restate addrlib's Gfx10Lib::HwlComputeSurfaceInfoLinear in gfx10_layout.h: each mip's pitch is its rounded-up width aligned to 256 bytes, and the mips are packed from the smallest to the largest with no tail. Such textures then take the existing console-layout path. 1D and 3D linear textures stay refused. Tests: linear round trips, a hand-worked chain with a mip-order control, an optional comparison of every element address with addrlib, four exact linear draws in startup_address_console and an unpadded-pitch control. --- docs/experiments/startup-gfx10-linear.md | 57 +++++++++ docs/startup-runtime.md | 7 +- src/startup_stubs/address_texture.h | 7 +- src/startup_stubs/gfx10_layout.h | 41 +++++-- tests/cmake/startup_gfx10_linear.cmake | 21 ++++ tests/startup_address_console_check.py | 64 ++++++++-- tests/startup_gfx10_layout_check.cpp | 149 ++++++++++++++++++++++- 7 files changed, 315 insertions(+), 31 deletions(-) create mode 100644 docs/experiments/startup-gfx10-linear.md create mode 100644 tests/cmake/startup_gfx10_linear.cmake diff --git a/docs/experiments/startup-gfx10-linear.md b/docs/experiments/startup-gfx10-linear.md new file mode 100644 index 0000000000..330d225c6d --- /dev/null +++ b/docs/experiments/startup-gfx10-linear.md @@ -0,0 +1,57 @@ +# Slot-70 textures in the console linear layout (0x100) + +Device slot 70 publishes a texture by address. A description whose `Layout` is `0x100` (the swizzle +mode word for `ADDR_SW_LINEAR`) was refused with `reason=unsupported_description` unless it was a +single texel, because the extent of its rows was not established. This note establishes it from AMD +addrlib and reads such textures like the tiled console layouts. + +## Layout + +`Gfx10Lib::HwlComputeSurfaceInfoLinear` (Mesa 25.1.0, commit +[5c142e46](https://gitlab.freedesktop.org/mesa/mesa/-/blob/5c142e46f3f6e752ed745fd48912ebb8fad67145/src/amd/addrlib/src/gfx10/gfx10addrlib.cpp), +`src/amd/addrlib`, MIT) gives the linear mode: + +- The pitch of a mip is its width in elements, rounded up at each level (`GetMipSize`), then aligned + to `256 / element bytes` elements: 256 bytes. +- The mip rows are packed with no tail block. The mips run from the smallest to the largest, so the + largest mip comes last. +- A slice is the sum of its mips, and the array slices follow one another. + +`src/startup_stubs/gfx10_layout.h` restates this rule for two-dimensional textures. The rest of the +slot-70 console path is unchanged: view checks, the XG holds and the upload as desktop footprint rows. +One-dimensional and three-dimensional linear textures are still refused. + +## Results + +Build: `slot70/linear-layout` on main 6ee878d8, dev preset (debug layer on). Windows 11 Pro 10.0.26200, +NVIDIA GeForce RTX 5090 (driver 32.0.16.1088) and Microsoft WARP. + +| Test | Result | +| --- | --- | +| `startup_gfx10_layout` | PASS: 13 round trips (4 of them linear, including a 2-slice R8 chain and a 3-slice BC1 chain) and one hand-worked linear chain (a 100x10 RGBA16F texture with 3 mips: 32x3 elements at offset 0, 64x5 at 768, 128x10 at 3328, 13568 bytes a slice) | +| `startup_gfx10_layout_linear_order_control` | expected failure: with the largest mip first, all 6 offset and address checks fail | +| `startup_gfx10_layout_linear_addrlib` | PASS: 338,763 sizes and element addresses of 11 linear textures (R8 to RGBA32F, BC1, BC7, odd sizes, up to 9 mips and 3 slices) match `Addr2ComputeSurfaceInfo` and `Addr2ComputeSurfaceAddrFromCoord` with `ADDR_SW_LINEAR`; skips (77) without addrlib | +| `startup_address_console_{warp,hardware}` | PASS: 4 new linear draws are exact on all 65,536 pixels. They are RGBA8 100x40 (400-byte rows padded to 512); its 4-mip chain viewed at mip 0 and at mip 2; and BC1 200x96 (50 blocks padded to 64). The 7 earlier draws and the 3 held/late cases are unchanged | +| `startup_address_console_linear_unpadded_{warp,hardware}` | PASS (control): the same RGBA8 100x40 texture with rows written at 400 bytes is published, and 63,488 of 65,536 pixels differ | + +Without the `gfx10_layout.h` and `address_texture.h` change (main 6ee878d8), the first linear draw +reads the null view: pixel (0,0) is `00000000`, but `010300ff` was expected. There is also no +`published console layout` line in the log. + +Commands (PowerShell, at the repository root, after `. .\tools\dev.ps1`; addrlib fetched with +`tools/fetch_addrlib.py`): + +```powershell +cmake --preset dev -DXBOX2WINDOWS_ADDRLIB_DIR=/src/amd/addrlib +cmake --build --preset dev +ctest --preset dev --no-tests=error -R '^startup_gfx10_layout' +ctest --preset dev --no-tests=error -R '^startup_address_console_(warp|hardware|linear_unpadded_(warp|hardware))$' -V +``` + +## Open + +- The rule is addrlib's desktop GFX10 rule. That the console stores `0x100` textures with the same + 256-byte pitch is inferred from the layout word naming `ADDR_SW_LINEAR`; no console hardware + measurement backs it. +- One-dimensional linear textures need a 1D view in `address_view_fits` and a 1D desktop texture + for the upload. They are not covered here. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 886a6dc4fd..40b44cf97b 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -3335,7 +3335,8 @@ texture with that description exists at the address, its view is created as befo 70 already read from an address keep their own paths unchanged: the single texel, the `0x105` BC3/BC7 surface (partial last tiles included) and the `0x109` BC chains. For any other description with no live texture whose `Layout` is `0x101`, `0x102`, `0x105`, `0x106`, `0x109` or `0x10a` (256 B, -4 KiB and 64 KiB blocks in the standard and display orders, no XOR), the texels are read from the +4 KiB and 64 KiB blocks in the standard and display orders, no XOR) or the linear mode `0x100` (rows +whose pitch is rounded up to 256 bytes, the mips stored from the smallest up), the texels are read from the address in that console layout (`src/startup_stubs/address_texture.h`, device-free). A translated draw that samples the view copies each mip of each array slice from that layout into desktop footprint rows (`src/startup_stubs/gfx10_layout.h`: address equations, block sizes and mip-tail rules @@ -3344,8 +3345,8 @@ after AMD addrlib, Mesa 25.1.0, `src/amd/addrlib`, MIT). A 256 B layout is taken The description must be two-dimensional, at most 16384 texels a side, with 1 to 15 mips, one sample, no target, depth or unordered-access flag, and a format with a known element size (8- to 128-bit texels and BC1 to BC7, whose largest mip must then be whole 4x4 blocks). The view must use a supported typed format that matches the storage format or belongs to its typeless parent. The 2D or 2D-array view must fit the mip and slice ranges. A mismatched or interior view overlapping a live resource preserves the destination descriptor. -Anything else is refused as before and leaves the destination descriptor unchanged: layout `0` and -the linear mode `0x100` without a live texture, three-dimensional and cube views, multisampled +Anything else is refused as before and leaves the destination descriptor unchanged: layout `0` +without a live texture, three-dimensional and cube views, multisampled textures, the XOR swizzle modes and every other layout value. The texels must be readable and span at most 64 MiB. diff --git a/src/startup_stubs/address_texture.h b/src/startup_stubs/address_texture.h index 11d8337391..b5cdcc5150 100644 --- a/src/startup_stubs/address_texture.h +++ b/src/startup_stubs/address_texture.h @@ -5,9 +5,10 @@ // device, which of those descriptions the startup renderer reads from the address: a sampled-only, // two-dimensional, single-sample texture whose Layout field holds a console swizzle mode that // gfx10_layout.h addresses. Its texels are in that console layout. -// Everything else is refused here: the desktop's unknown layout and the console's linear mode (the -// extent of such rows is not established), other dimensions, multisampled textures, textures that -// are also targets, formats without a known element size and swizzle modes without an equation. +// The console's linear mode (0x100) is one of those layouts: rows with addrlib's 256-byte pitch. +// Everything else is refused here: the desktop's unknown layout, other dimensions, multisampled +// textures, textures that are also targets, formats without a known element size and swizzle modes +// without an equation. #include "startup_stubs/gfx10_layout.h" diff --git a/src/startup_stubs/gfx10_layout.h b/src/startup_stubs/gfx10_layout.h index 87deac0414..f088ca4bc3 100644 --- a/src/startup_stubs/gfx10_layout.h +++ b/src/startup_stubs/gfx10_layout.h @@ -5,8 +5,8 @@ // // A console resource description carries its swizzle mode in the Layout field as 0x100 + mode. The // modes handled here are 256 B, 4 KiB and 64 KiB blocks in the standard (S) and display (D) orders: -// 0x101, 0x102, 0x105, 0x106, 0x109 and 0x10a. The address equations, block dimensions and mip-tail -// rules follow AMD addrlib as Mesa carries it under src/amd/addrlib, at the revision +// 0x101, 0x102, 0x105, 0x106, 0x109 and 0x10a, and the linear mode 0x100. The address equations, +// block dimensions and mip-tail rules follow AMD addrlib as Mesa carries it under src/amd/addrlib, at the revision // tools/fetch_addrlib.py pins: Mesa 25.1.0, commit 5c142e46f3f6e752ed745fd48912ebb8fad67145. The three // files used carry this notice: // Copyright (C) 2007-2024 Advanced Micro Devices, Inc. All rights reserved. @@ -16,7 +16,9 @@ // - src/core/addrlib2.cpp: ComputeThinBlockDimension (a block is 2^ceil(n/2) by 2^floor(n/2) elements) // and GetMipTailDim (the tail holds mips up to half a block wide and one block high). // - src/gfx10/gfx10addrlib.cpp: ComputeSurfaceInfoMacroTiled (the tail block comes first in a slice, -// then the mips from the smallest to the largest; offsets and origins inside the tail block). +// then the mips from the smallest to the largest; offsets and origins inside the tail block), and +// HwlComputeSurfaceInfoLinear (ADDR_SW_LINEAR: each mip is rows of a pitch rounded up to 256 bytes, +// the mips stored from the smallest to the largest, with no tail). // The rules are restated here; no addrlib code is included. // Measurements are in elements: one texel, or one 4x4 block of a block-compressed format. @@ -55,6 +57,9 @@ inline UINT swizzle_mode(const D3D12_RESOURCE_DESC& d) return (layout & ~0xffu) == 0x100 ? layout & 0xff : ~0u; } +// ADDR_SW_LINEAR: rows, not blocks. +constexpr UINT linear_mode = 0; + inline UINT block_log2(UINT mode) { switch (mode) { @@ -68,20 +73,22 @@ inline UINT block_log2(UINT mode) // A two-dimensional, single-sample texture whose memory this header can address. inline bool supports(const D3D12_RESOURCE_DESC& d) { - const UINT log2 = block_log2(swizzle_mode(d)); - if (!log2 || d.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || !element_of(d.Format).bytes) return false; + const UINT mode = swizzle_mode(d), log2 = block_log2(mode); + if ((!log2 && mode != linear_mode) || d.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || !element_of(d.Format).bytes) return false; if (!d.Width || !d.Height || d.Width > 16384 || d.Height > 16384 || !d.DepthOrArraySize) return false; if (!d.MipLevels || d.MipLevels > max_mips || d.SampleDesc.Count != 1) return false; - return log2 >= 12 || d.MipLevels == 1; // a 256 B block has no mip tail here + return mode == linear_mode || log2 >= 12 || d.MipLevels == 1; // a 256 B block has no mip tail here } struct Layout { UINT bytes_per_element = 0, texels = 1, width = 0, height = 0, mips = 0, slices = 0; UINT block_bytes = 0, block_width = 0, block_height = 0; + bool is_linear = false; // rows of row_pitch[mip] elements; block_* then describe one 256-byte row step UINT first_tail = 0; // first mip inside the shared tail block; mips when there is none std::uint64_t slice_size = 0; std::array start{}; // byte offset of a mip in its slice std::array blocks_wide{}, origin_x{}, origin_y{}; + std::array row_pitch{}; // elements per row of a linear mip std::array x_bits{}, y_bits{}; // in-block byte offset contributed by a coordinate explicit Layout(const D3D12_RESOURCE_DESC& d) @@ -91,6 +98,25 @@ struct Layout { bytes_per_element = element.bytes; texels = element.texels; const UINT mode = swizzle_mode(d), log2 = block_log2(mode); + const auto shifted = [](UINT v, UINT mip) { return (std::max)(1u, (v + (1u << mip) - 1) >> mip); }; + const UINT columns0 = (width + texels - 1) / texels, rows0 = (height + texels - 1) / texels; + if (mode == linear_mode) { + // HwlComputeSurfaceInfoLinear: the pitch of each mip is its width (rounded up, GetMipSize) + // aligned to 256 bytes, and the smallest mip comes first. + is_linear = true; + first_tail = mips; + block_bytes = 256; + block_width = 256 / bytes_per_element; + block_height = 1; + std::uint64_t offset = 0; + for (UINT mip = mips; mip-- > 0;) { + row_pitch[mip] = (shifted(columns0, mip) + block_width - 1) / block_width * block_width; + start[mip] = offset; + offset += std::uint64_t{row_pitch[mip]} * shifted(rows0, mip) * bytes_per_element; + } + slice_size = offset; + return; + } UINT element_log2 = 0; while ((1u << element_log2) < bytes_per_element) ++element_log2; const UINT count_log2 = log2 - element_log2, width_log2 = (count_log2 + 1) / 2; @@ -126,8 +152,6 @@ struct Layout { } // Mip sizes for the layout round up at every level (addrlib GetMipSize). - const UINT columns0 = (width + texels - 1) / texels, rows0 = (height + texels - 1) / texels; - const auto shifted = [](UINT v, UINT mip) { return (std::max)(1u, (v + (1u << mip) - 1) >> mip); }; const UINT tail_mips = log2 - 4; // most mips one tail block holds, for 4 KiB and 64 KiB blocks first_tail = mips; std::array size{}; @@ -173,6 +197,7 @@ struct Layout { std::uint64_t address(UINT mip, UINT x, UINT y, UINT slice) const { const std::uint64_t base = slice * slice_size; + if (is_linear) return base + start[mip] + (std::uint64_t{y} * row_pitch[mip] + x) * bytes_per_element; if (mip >= first_tail) return base + (x_bits[(origin_x[mip] + x) & 255] | y_bits[(origin_y[mip] + y) & 255]); const std::uint64_t block = std::uint64_t{y / block_height} * blocks_wide[mip] + x / block_width; return base + start[mip] + block * block_bytes + (x_bits[x % block_width] | y_bits[y % block_height]); diff --git a/tests/cmake/startup_gfx10_linear.cmake b/tests/cmake/startup_gfx10_linear.cmake new file mode 100644 index 0000000000..9f68899b9e --- /dev/null +++ b/tests/cmake/startup_gfx10_linear.cmake @@ -0,0 +1,21 @@ +# The console linear mode (0x100) in src/startup_stubs/gfx10_layout.h. The mutated run expects the largest +# mip first and is a negative control; the addrlib run compares every element address of several linear +# textures with addrlib's SW_LINEAR (Mesa 25.1.0, src/amd/addrlib, MIT) and skips (77) without addrlib. +add_test(NAME startup_gfx10_layout_linear_order_control COMMAND startup_gfx10_layout_check --mutate linear-largest-first) +set_tests_properties(startup_gfx10_layout_linear_order_control PROPERTIES WILL_FAIL TRUE) +if(XBOX2WINDOWS_ADDRLIB_DIR) + target_link_libraries(startup_gfx10_layout_check PRIVATE xbw_addrlib) + target_compile_definitions(startup_gfx10_layout_check PRIVATE XBOX2WINDOWS_HAVE_ADDRLIB) +endif() +add_test(NAME startup_gfx10_layout_linear_addrlib COMMAND startup_gfx10_layout_check --addrlib) +set_tests_properties(startup_gfx10_layout_linear_addrlib PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77) +# The negative control for the linear cases of startup_address_console: rows written without the 256-byte +# pitch must not draw exactly. +if(STARTUP_FXC AND EXISTS "${INTAKE_PYTHON}" AND TARGET startup_stream_consumer) + foreach(backend IN ITEMS warp hardware) + add_test(NAME startup_address_console_linear_unpadded_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" + "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_console_check.py" + "$" "${backend}" linear-unpadded) + set_tests_properties(startup_address_console_linear_unpadded_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) + endforeach() +endif() diff --git a/tests/startup_address_console_check.py b/tests/startup_address_console_check.py index be74340afa..e451d3e45a 100644 --- a/tests/startup_address_console_check.py +++ b/tests/startup_address_console_check.py @@ -1,6 +1,6 @@ """Draw textures published by address (device slot 70) whose texels are in a console layout. - seven textures, each drawn once; every pixel of the 512x128 target is + eleven textures, each drawn once; every pixel of the 512x128 target is compared with the texel the view names; then two held descriptions and one texture whose address is announced as XG rows after it was published @@ -17,9 +17,12 @@ chunks at its fenced submit, so B draws payload 32; A submitted again afterwards would read reused chunks, so it is refused before the execute: the target, read back after that submit, still holds payload 32 on the right (not A's payload 31) + linear-unpadded the control described at check_linear_unpadded The backing bytes are built here, independently of src/startup_stubs/gfx10_layout.h, from the address -equations of AMD addrlib's GFX10 pattern tables, written out below. Reference: addrlib as Mesa carries it +equations of AMD addrlib's GFX10 pattern tables, written out below, and for the linear mode (0x100) from +HwlComputeSurfaceInfoLinear: rows whose pitch is the mip's rounded-up width aligned to 256 bytes, the mips +stored from the smallest to the largest. Reference: addrlib as Mesa carries it under src/amd/addrlib at the revision tools/fetch_addrlib.py pins (Mesa 25.1.0, commit 5c142e46f3f6e752ed745fd48912ebb8fad67145), files src/gfx10/gfx10SwizzlePattern.h, src/gfx10/gfx10addrlib.cpp and src/core/addrlib2.cpp, each with the notice "Copyright (C) 2007-2024 Advanced Micro Devices, Inc. All @@ -43,9 +46,22 @@ class Console: - def __init__(self, width, height, mips, element, texels, layout): + def __init__(self, width, height, mips, element, texels, layout, unpadded=False): self.width, self.height, self.mips, self.element, self.texels = width, height, mips, element, texels mode = layout & 0xff + columns, rows = -(-width // texels), -(-height // texels) + size = lambda v, mip: max(1, (v + (1 << mip) - 1) >> mip) + self.linear = mode == 0 + if self.linear: + # unpadded: rows exactly one mip wide, for the control at check_linear_unpadded. + align = 1 if unpadded else 256 // element + self.pitch, self.start, offset = {}, {}, 0 + for mip in range(mips - 1, -1, -1): + self.pitch[mip] = -(-size(columns, mip) // align) * align + self.start[mip] = offset + offset += self.pitch[mip] * size(rows, mip) * element + self.bytes = offset + return self.log2 = {1: 8, 2: 8, 5: 12, 6: 12, 9: 16, 10: 16}[mode] bits = (DISPLAY if mode in (2, 6, 10) else STANDARD)[element].split() x_next, y_next = (sum(b[0] == axis for b in bits) for axis in "XY") @@ -58,8 +74,6 @@ def __init__(self, width, height, mips, element, texels, layout): y_next += 1 self.bits = bits self.block_w, self.block_h = 1 << x_next, 1 << y_next - columns, rows = -(-width // texels), -(-height // texels) - size = lambda v, mip: max(1, (v + (1 << mip) - 1) >> mip) tail_mips = self.log2 - 4 self.first_tail = mips if mips > 1: @@ -96,6 +110,8 @@ def rows(self, mip): return -(-max(1, self.height >> mip) // self.texels) def address(self, mip, x, y): + if self.linear: + return self.start[mip] + (y * self.pitch[mip] + x) * self.element if mip >= self.first_tail: ox, oy = self.origin[mip] return self.in_block(ox + x, oy + y) @@ -124,15 +140,20 @@ def bc1_block(mip, x, y): ("RGBA8 256x256 chain 64 KiB display, tail mip 2", 256, 256, 4, RGBA8, 0x10a, 2), ("RGBA8 256x256 chain 64 KiB display, tail mip 3", 256, 256, 4, RGBA8, 0x10a, 3), ("RGBA8 40x24 256 B standard", 40, 24, 1, RGBA8, 0x101, 0), + # Linear: 100 texels are 400 bytes, so each row is padded to 512; 200 BC1 texels are 50 blocks, padded to 64. + ("RGBA8 100x40 linear", 100, 40, 1, RGBA8, 0x100, 0), + ("RGBA8 100x40 linear chain, mip 0", 100, 40, 4, RGBA8, 0x100, 0), + ("RGBA8 100x40 linear chain, mip 2", 100, 40, 4, RGBA8, 0x100, 2), + ("BC1 200x96 linear", 200, 96, 1, BC1, 0x100, 0), ] -def build(width, height, mips, fmt, layout): +def build(width, height, mips, fmt, layout, unpadded=False): """Backing bytes and, per mip, the RGBA texels a correct detile shows.""" texels = 4 if fmt == BC1 else 1 element = 8 if fmt == BC1 else 4 images = [] - console = Console(width, height, mips, element, texels, layout) + console = Console(width, height, mips, element, texels, layout, unpadded) backing = bytearray(console.bytes) seen = set() for mip in range(mips): @@ -173,9 +194,9 @@ def build(width, height, mips, fmt, layout): {"X2W_TEST_CONSOLE_XG_ROWS": "after"}) -def check(directory, consumer, backend, case, extra=None, held=None, late=False): +def check(directory, consumer, backend, case, extra=None, held=None, late=False, unpadded=False): name, width, height, mips, fmt, layout, viewed = case - backing, images = build(width, height, mips, fmt, layout) + backing, images = build(width, height, mips, fmt, layout, unpadded) source = directory / "texels.bin" source.write_bytes(backing) w, h = max(1, width >> viewed), max(1, height >> viewed) @@ -206,18 +227,33 @@ def check(directory, consumer, backend, case, extra=None, held=None, late=False) print(f"{backend} {name}: published, then {TARGET_W * TARGET_H} pixels read the null view") return 0 image = images[viewed] + wrong = 0 for y in range(TARGET_H): for x in range(TARGET_W): pixel = actual[4 * (y * TARGET_W + x):4 * (y * TARGET_W + x) + 4] expect = image[4 * ((y % h) * w + x % w):4 * ((y % h) * w + x % w) + 4] - if pixel != expect: + if pixel != expect and not unpadded: raise AssertionError(f"{name}: pixel ({x},{y}) actual {pixel.hex()} expected {expect.hex()}") + wrong += pixel != expect if "slot70 texture kind 1: published console layout" not in log: raise AssertionError(f"{name}: no 'published console layout' line in the log") + if unpadded: + if not wrong: + raise AssertionError(f"{name}: rows written without the 256-byte pitch drew exactly; the pitch is not checked") + print(f"{backend} {name}, rows without the 256-byte pitch: published, {wrong} of {TARGET_W * TARGET_H} pixels differ") + return 0 print(f"{backend} {name}: {TARGET_W * TARGET_H} RGBA pixels exact") return 0 +def check_linear_unpadded(directory, consumer, backend): + """The linear cases' negative control: the first linear texture with its rows written one mip wide + (400 bytes) instead of at the 256-byte pitch (512). The runtime reads the padded pitch, so the draw must + differ from the texels; an exact draw means the linear cases do not test the pitch.""" + case = next(c for c in CASES if c[5] == 0x100) + return check(directory, consumer, backend, case, unpadded=True) + + GATE_PAYLOADS = 18 KEEP_PAYLOADS = 32 # tests/startup_stream_draw.cpp, X2W_TEST_CONSOLE_KEEP @@ -330,9 +366,13 @@ def check_gate(directory, consumer, backend, mode): GATES = ("gate", "gate-ring", "gate-ring-reuse", "gate-ring-keep", "gate-ring-keep-release") if __name__ == "__main__": - if len(sys.argv) not in (3, 4) or (len(sys.argv) == 4 and sys.argv[3] not in GATES): - usage = "usage: startup_address_console_check.py [gate | gate-ring | gate-ring-reuse | gate-ring-keep | gate-ring-keep-release]\n" + modes = GATES + ("linear-unpadded",) + if len(sys.argv) not in (3, 4) or (len(sys.argv) == 4 and sys.argv[3] not in modes): + usage = "usage: startup_address_console_check.py [" + " | ".join(modes) + "]\n" sys.exit(usage + "\n\n".join(__doc__.split("\n\n")[:2])) + if len(sys.argv) == 4 and sys.argv[3] == "linear-unpadded": + with tempfile.TemporaryDirectory() as temporary: + sys.exit(check_linear_unpadded(Path(temporary), sys.argv[1], sys.argv[2])) if len(sys.argv) == 4: with tempfile.TemporaryDirectory() as temporary: sys.exit(check_gate(Path(temporary), sys.argv[1], sys.argv[2], sys.argv[3])) diff --git a/tests/startup_gfx10_layout_check.cpp b/tests/startup_gfx10_layout_check.cpp index 81a1ccc77b..72cfb94b67 100644 --- a/tests/startup_gfx10_layout_check.cpp +++ b/tests/startup_gfx10_layout_check.cpp @@ -3,6 +3,8 @@ // tables (written out below as the nibbles those rows name), for six swizzle modes and five element sizes; // - agreement with the three narrower layouts already in the tree (bc7_4k_s.h, bc64_chain.h, tile64_plan.h); // - the mip offsets docs/experiments.md records for a 1024x1024 six-mip BC7 chain; +// - the linear mode (0x100): mip offsets worked out by hand from addrlib's HwlComputeSurfaceInfoLinear, +// and, with --addrlib, every element address of several linear textures against addrlib itself; // - that every element of every subresource gets its own bytes inside the texture, and that a // tile followed by a detile returns the rows it started from. // @@ -167,6 +169,8 @@ void check_distinct_and_round_trip() {640, 360, 10, 1, DXGI_FORMAT_BC7_UNORM, 0x10a}, {257, 129, 9, 3, DXGI_FORMAT_R8G8B8A8_UNORM, 0x106}, {96, 40, 1, 1, DXGI_FORMAT_R16G16B16A16_FLOAT, 0x102}, {4, 4, 3, 1, DXGI_FORMAT_BC3_UNORM, 0x109}, {2048, 2048, 12, 1, DXGI_FORMAT_BC5_UNORM, 0x109}, + {100, 10, 3, 1, DXGI_FORMAT_R16G16B16A16_FLOAT, 0x100}, {257, 129, 9, 2, DXGI_FORMAT_R8_UNORM, 0x100}, + {256, 256, 7, 1, DXGI_FORMAT_R32_FLOAT, 0x100}, {64, 64, 4, 3, DXGI_FORMAT_BC1_UNORM, 0x100}, }; for (const Shape& s : shapes) { const D3D12_RESOURCE_DESC d = texture(s.width, s.height, s.mips, s.slices, s.format, s.layout); @@ -210,7 +214,8 @@ void check_refusals() }; D3D12_RESOURCE_DESC d = good; d.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; refused("desktop layout", d); - d = good; d.Layout = static_cast(0x100); refused("linear mode", d); + d = good; d.Layout = static_cast(0x100); d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D; + refused("linear volume", d); d = good; d.Layout = static_cast(0x115); refused("XOR mode", d); d = good; d.Layout = static_cast(0x209); refused("layout outside 0x1xx", d); d = good; d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D; refused("volume", d); @@ -222,6 +227,23 @@ void check_refusals() d = good; d.Layout = static_cast(0x101); d.MipLevels = 2; refused("256 B chain", d); } +// The linear mode by hand: a 100x10 R16G16B16A16 texture with three mips. Each mip's pitch is its +// rounded-up width aligned to 32 elements (256 bytes), and the mips are stored from the smallest up: +// mip 2 is 32x3 elements at 0, mip 1 64x5 at 768, mip 0 128x10 at 3328, 13568 bytes a slice. +// The mutated run expects the largest mip first and is a negative control. +void check_linear(bool largest_first) +{ + const Layout l(texture(100, 10, 3, 2, DXGI_FORMAT_R16G16B16A16_FLOAT, 0x100)); + const std::uint64_t start[3] = {largest_first ? 0u : 3328u, largest_first ? 10240u : 768u, largest_first ? 12800u : 0u}; + const UINT pitch[3] = {128, 64, 32}; + if (!l.is_linear || l.first_tail != 3 || l.slice_size != 13568 || l.bytes() != 2 * 13568) fail("linear: slice size"); + for (UINT mip = 0; mip < 3; ++mip) { + if (l.start[mip] != start[mip] || l.row_pitch[mip] != pitch[mip]) fail("linear: mip " + std::to_string(mip)); + const std::uint64_t want = 13568 + start[mip] + (2ull * pitch[mip] + 3) * 8; + if (l.address(mip, 3, 2, 1) != want) fail("linear: element (3, 2) of slice 1, mip " + std::to_string(mip)); + } +} + // Which slot-70 descriptions are taken, and how their texels are read (address_texture.h). void check_address_textures() { @@ -238,7 +260,7 @@ void check_address_textures() expect("not sampled", d, false); expect("64 KiB display R16 chain", texture(512, 512, 10, 1, DXGI_FORMAT_R16_UNORM, 0x10a), true); expect("desktop layout RGBA8", texture(1, 1, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, 0), false); - expect("linear mode R8", texture(256, 128, 1, 1, DXGI_FORMAT_R8_UNORM, 0x100), false); + expect("linear mode R8", texture(256, 128, 1, 1, DXGI_FORMAT_R8_UNORM, 0x100), true); expect("XOR mode", texture(256, 256, 1, 1, DXGI_FORMAT_BC7_UNORM, 0x119), false); expect("row-major desktop layout", texture(256, 256, 1, 1, DXGI_FORMAT_BC7_UNORM, 1), false); expect("unknown element size", texture(256, 256, 1, 1, DXGI_FORMAT_R32G32B32_FLOAT, 0), false); @@ -324,14 +346,131 @@ void check_address_textures() } } // namespace +#ifndef XBOX2WINDOWS_HAVE_ADDRLIB +int addrlib_linear_check() +{ + std::puts("startup_gfx10_layout --addrlib: SKIP (built without addrlib; run tools/fetch_addrlib.py and configure with " + "-DXBOX2WINDOWS_ADDRLIB_DIR=/src/amd/addrlib)"); + return 77; +} +#else +#include "addrinterface.h" + +namespace { +VOID* ADDR_API alloc_sys_mem(const ADDR_ALLOCSYSMEM_INPUT* in) { return std::malloc(in->sizeInBytes); } +ADDR_E_RETURNCODE ADDR_API free_sys_mem(const ADDR_FREESYSMEM_INPUT* in) +{ + std::free(in->pVirtAddr); + return ADDR_OK; +} +} // namespace + +// Every element of every subresource of several linear textures, and their sizes, against +// Addr2ComputeSurfaceAddrFromCoord and Addr2ComputeSurfaceInfo with ADDR_SW_LINEAR on Mesa's "navi21" +// (gfx10.3) configuration, the one xg_swizzle_select_check uses: family 0x8F, revision 0x28, +// GB_ADDR_CONFIG 0x444. Sizes are given to addrlib in elements, so a BC texture is its 4x4 blocks. +int addrlib_linear_check() +{ + ADDR_CREATE_INPUT create{}; + ADDR_CREATE_OUTPUT created{}; + create.size = sizeof create; + created.size = sizeof created; + create.chipEngine = 0x0000000D; // CIASICIDGFXENGINE_ARCTICISLAND + create.chipFamily = 0x8F; + create.chipRevision = 0x28; + create.callbacks.allocSysMem = alloc_sys_mem; + create.callbacks.freeSysMem = free_sys_mem; + create.regValue.gbAddrConfig = 0x40 | 4u | (4u << 8); + if (AddrCreate(&create, &created) != ADDR_OK) return 1; + struct Shape { UINT width, height, mips, slices; DXGI_FORMAT format; }; + const Shape shapes[] = { + {1, 1, 1, 1, DXGI_FORMAT_R8_UNORM}, {300, 7, 1, 1, DXGI_FORMAT_R8_UNORM}, + {257, 129, 9, 2, DXGI_FORMAT_R8_UNORM}, {100, 10, 3, 2, DXGI_FORMAT_R16G16B16A16_FLOAT}, + {1920, 64, 1, 1, DXGI_FORMAT_R16G16B16A16_FLOAT}, {256, 256, 7, 1, DXGI_FORMAT_R32_FLOAT}, + {33, 17, 6, 3, DXGI_FORMAT_R16_UNORM}, {200, 100, 8, 1, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB}, + {48, 40, 4, 2, DXGI_FORMAT_R32G32B32A32_FLOAT}, {64, 64, 4, 3, DXGI_FORMAT_BC1_UNORM}, + {128, 64, 6, 1, DXGI_FORMAT_BC7_UNORM}, + }; + unsigned compared = 0, mismatches = 0; + for (const Shape& s : shapes) { + const D3D12_RESOURCE_DESC d = texture(s.width, s.height, s.mips, s.slices, s.format, 0x100); + const std::string name = std::to_string(s.width) + "x" + std::to_string(s.height) + " format " + + std::to_string(s.format) + ", " + std::to_string(s.mips) + " mips"; + if (!x2w_startup::gfx10::supports(d)) { + fail(name + ": refused"); + ++mismatches; + continue; + } + const Layout l(d); + ADDR2_COMPUTE_SURFACE_INFO_INPUT info_in{}; + ADDR2_COMPUTE_SURFACE_INFO_OUTPUT info_out{}; + info_in.size = sizeof info_in; + info_out.size = sizeof info_out; + info_in.flags.texture = 1; + info_in.swizzleMode = ADDR_SW_LINEAR; + info_in.resourceType = ADDR_RSRC_TEX_2D; + info_in.bpp = l.bytes_per_element * 8; + info_in.width = (s.width + l.texels - 1) / l.texels; + info_in.height = (s.height + l.texels - 1) / l.texels; + info_in.numSlices = s.slices; + info_in.numMipLevels = s.mips; + info_in.numSamples = 1; + info_in.numFrags = 1; + ++compared; + if (Addr2ComputeSurfaceInfo(created.hLib, &info_in, &info_out) != ADDR_OK || info_out.surfSize != l.bytes() || + info_out.sliceSize != l.slice_size) { + fail(name + ": size " + std::to_string(l.bytes()) + ", addrlib " + std::to_string(info_out.surfSize)); + ++mismatches; + continue; + } + ADDR2_COMPUTE_SURFACE_ADDRFROMCOORD_INPUT in{}; + in.size = sizeof in; + in.flags.texture = 1; + in.swizzleMode = ADDR_SW_LINEAR; + in.resourceType = ADDR_RSRC_TEX_2D; + in.bpp = info_in.bpp; + in.unalignedWidth = info_in.width; + in.unalignedHeight = info_in.height; + in.numSlices = s.slices; + in.numMipLevels = s.mips; + in.numSamples = 1; + in.numFrags = 1; + unsigned wrong = 0; + for (in.slice = 0; in.slice < s.slices; ++in.slice) + for (in.mipId = 0; in.mipId < s.mips; ++in.mipId) + for (in.y = 0; in.y < l.rows(in.mipId); ++in.y) + for (in.x = 0; in.x < l.columns(in.mipId); ++in.x) { + ADDR2_COMPUTE_SURFACE_ADDRFROMCOORD_OUTPUT out{}; + out.size = sizeof out; + ++compared; + const std::uint64_t mine = l.address(in.mipId, in.x, in.y, in.slice); + if (Addr2ComputeSurfaceAddrFromCoord(created.hLib, &in, &out) != ADDR_OK || out.addr != mine) { + if (!wrong++) + fail(name + ": mip " + std::to_string(in.mipId) + " slice " + std::to_string(in.slice) + " (" + + std::to_string(in.x) + ", " + std::to_string(in.y) + ") at " + std::to_string(mine) + + ", addrlib " + std::to_string(out.addr)); + } + } + mismatches += wrong; + } + AddrDestroy(created.hLib); + std::printf("startup_gfx10_layout --addrlib: %u linear sizes and addresses compared, %u mismatches (Mesa 25.1.0 addrlib, " + "SW_LINEAR)\n", compared, mismatches); + return compared && !mismatches ? 0 : 1; +} +#endif + int main(int argc, char** argv) { + if (argc == 2 && std::string(argv[1]) == "--addrlib") return addrlib_linear_check(); const bool mutate = argc == 3 && std::string(argv[1]) == "--mutate" && std::string(argv[2]) == "display"; - if (argc != 1 && !mutate) { - std::printf("usage: startup_gfx10_layout_check [--mutate display]\n"); + const bool mutate_linear = argc == 3 && std::string(argv[1]) == "--mutate" && std::string(argv[2]) == "linear-largest-first"; + if (argc != 1 && !mutate && !mutate_linear) { + std::printf("usage: startup_gfx10_layout_check [--addrlib | --mutate display | --mutate linear-largest-first]\n"); return 2; } check_equations(mutate); + check_linear(mutate_linear); check_bc7_4k(); check_bc64(); check_tile64(); @@ -342,6 +481,6 @@ int main(int argc, char** argv) std::printf("startup_gfx10_layout: %d FAILED\n", failures); return 1; } - std::printf("startup_gfx10_layout: PASS (30 equations, 5 4 KiB surfaces, 8 64 KiB chains, 10 tile plans, 9 round trips, 11 refusals, 19 address descriptions, 27 views)\n"); + std::printf("startup_gfx10_layout: PASS (30 equations, 5 4 KiB surfaces, 8 64 KiB chains, 10 tile plans, 13 round trips, 1 linear chain, 11 refusals, 19 address descriptions, 27 views)\n"); return 0; }