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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/experiments/startup-gfx10-linear.md
Original file line number Diff line number Diff line change
@@ -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=<mesa>/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.
7 changes: 4 additions & 3 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -3539,7 +3539,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
Expand All @@ -3556,8 +3557,8 @@ viewed as a Texture3D. addrlib lays a 3D resource in a standard or display mode
`GFX10_SW_4K_S3_PATINFO`, the block is 16x16x16, 8x16x16, 8x16x8, 8x8x8 or 4x8x8 elements for 1- to
16-byte elements, and blocks follow along x, then y, then a layer of blocks at a time along z, with
every side padded to whole blocks. Each depth layer is copied to its own footprint rows.
Anything else is refused as before and leaves the destination descriptor unchanged: layout `0` and
the linear mode `0x100` without a live texture, cube views, three-dimensional descriptions in any
Anything else is refused as before and leaves the destination descriptor unchanged: layout `0`
without a live texture, cube views, three-dimensional descriptions in any
other layout, with a mip chain or with a block-compressed format, multisampled textures, the XOR
swizzle modes and every other layout value. The texels must be readable and span
at most 64 MiB.
Expand Down
7 changes: 4 additions & 3 deletions src/startup_stubs/address_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
41 changes: 33 additions & 8 deletions src/startup_stubs/gfx10_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.

Expand Down Expand Up @@ -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) {
Expand All @@ -80,20 +85,22 @@ inline bool supports_volume(const D3D12_RESOURCE_DESC& d)
inline bool supports(const D3D12_RESOURCE_DESC& d)
{
if (d.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D) return supports_volume(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<std::uint64_t, max_mips> start{}; // byte offset of a mip in its slice
std::array<UINT, max_mips> blocks_wide{}, origin_x{}, origin_y{};
std::array<UINT, max_mips> row_pitch{}; // elements per row of a linear mip
std::array<UINT, 256> x_bits{}, y_bits{}; // in-block byte offset contributed by a coordinate
// A volume (supports_volume): its depth, the depth of a thick block, the in-block offset contributed by z,
// and the bytes of one layer of blocks. Its one slice holds every depth layer; a 2D texture keeps 1, 1, 0.
Expand All @@ -112,6 +119,25 @@ struct Layout {
return;
}
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;
Expand Down Expand Up @@ -147,8 +173,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<std::uint64_t, max_mips> size{};
Expand Down Expand Up @@ -243,6 +267,7 @@ struct Layout {
(std::uint64_t{y / block_height} * blocks_wide[0] + x / block_width) * block_bytes +
(x_bits[x % block_width] | y_bits[y % block_height] | z_bits[z % block_depth]);
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]);
Expand Down
21 changes: 21 additions & 0 deletions tests/cmake/startup_gfx10_linear.cmake
Original file line number Diff line number Diff line change
@@ -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"
"$<TARGET_FILE:startup_stream_consumer>" "${backend}" linear-unpadded)
set_tests_properties(startup_address_console_linear_unpadded_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
endforeach()
endif()
Loading
Loading