Skip to content

d3d12: Detile step for console-tiled layout 0x101 textures (D3D-316) - #2706

Open
0xTehuti wants to merge 1 commit into
ItsNotRexed:mainfrom
0xTehuti:task/d3d-316
Open

0xTehuti wants to merge 1 commit into
ItsNotRexed:mainfrom
0xTehuti:task/d3d-316

Conversation

@0xTehuti

Copy link
Copy Markdown
Contributor

Item: D3D-316, claimed in #2516 (recommended by @KoertLichtendonk).

Resolves: D3D-316

What was open

In runtime/shims/d3d12_vulkan_device.cpp and docs/d3d12_vulkan.md (§3.2, lines 1604 and 1708), textures created through Xbox slot 76 carry the non-standard layout kXboxTiledLayout = 0x101. While the ABI recognizes the flag, the detiling path from console-tiled data into linear/Vulkan layout was listed as Open.

The change

  1. runtime/src/resource_detile.h:
    • Implements GFX10 / RDNA 2 intra-tile 64 KiB SW_64KB_D swizzle coordinate mapping from Mesa addrlib bit equations (GFX10_SW_64K_D_RBPLUS_PATINFO) for elements of 1, 2, 4, 8, and 16 bytes.
    • Precomputes orthogonal coordinate permutations at compile time (constexpr SwizzleLut64K), reducing intra-tile swizzle resolution to a single $O(1)$ OR operation (lut_x[x % tile_w] | lut_y[y % tile_h]) per texel/block with zero heap allocations and zero runtime bit loops.
    • Provides safe, bounds-checked detile_surface_64k and tile_surface_64k functions using std::span. Supports standard 2D textures, small sub-tiles, arbitrary non-power-of-two dimensions, and block-compressed textures (BC1–BC7 with block_dim = 4).
  2. runtime/test/resource_detile_test.cpp:
    • Exhaustive bijective validation across all element widths (1, 2, 4, 8, 16 BPE): zero coordinate collisions, exact 65,536-byte coverage per tile, and strict element alignment.
    • Strict rejection of invalid inputs (zero dimensions, unsupported element sizes, truncated spans).
    • Bit-exact round-trip verification (linear -> tile -> detile == linear and idempotence):
      • Individual tiles (1B, 2B, 4B, 8B, 16B).
      • Sub-tiles and small mips (1x1, 16x16, 50x30, 100x100).
      • Large and non-power-of-two surfaces (512x512, 1024x1024, 1080p, 4096x64).
      • Block-compressed formats (BC1 1024x1024, BC7 2048x1024, and non-multiple-of-4 dimensions like 1001x603).
  3. runtime/CMakeLists.txt: Registered resource_detile_test in CTest suite.
  4. docs/d3d12_vulkan.md: Updated layout 0x101 status from Open to Implemented.

Verification

  • resource_detile_test.exe: PASSED (all round-trip, bijective, BCn, and error validation tests passed in 0.50s).
  • Runtime regression tests:
    • resource_tiling_test.exe: PASSED.
    • d3d12_state_map_test.exe: PASSED.
    • d3d12_vulkan_device_test.exe: PASSED.
  • Toolchain & Hardware: Windows 11 AMD64, MSVC 2022 Community (19.44.35225), CMake 3.24, Ninja 1.13, NVIDIA GeForce RTX 2070 SUPER (Driver 595.79, Vulkan 1.4).
  • Clean-room compliance: 100% clean-room, Mesa addrlib open equations, synthetic unit vectors only, zero game data. AI-assisted (Leonida multi-agent team).

@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent MSVC 19.44 / Windows SDK 10.0.26100 check of 8573c6b. All four CTests passed (resource_detile, resource_tiling, d3d12_state_map, d3d12_vulkan_device), with the device test running on the RTX 5070. Both copy directions matched independently reconstructed Addrlib data and #2693 at a5fdc8b across 855,699 element positions, including five element widths and partial BC blocks. A 3,840×2,160 RGBA8 level took 5.84 ms to detile here, compared with 13.04 ms in #2693 on the same buffer (CPU, MSVC /O2, median of five warm calls). The compile-time lookup is worth keeping in a shared primitive.

The docs currently mark slot-76 0x101 as implemented. The only runtime source change is including the header, and the tile/detile helpers have callers in the unit test alone. Addrlib agreement confirms this standalone SW_64KB_D equation, while selecting that mode for 0x101, handling its mip tail, and wiring the upload are still open. Can the two D3D-316 PRs share the primitive and keep the runtime milestone open until a slot-76 upload/readback test exercises it?

I also found a size-validation gap by testing the size helpers without allocating any large buffers. The 32-bit width + block_dim - 1 ceil division can return a zero layout for a nonzero width whose byte count still fits size_t, and the later size products are unchecked. Shared checked dimensions and size calculations, as in #2693, would keep both copy directions behind the same validation.

@whattheflop

Copy link
Copy Markdown
Contributor

AI-assisted D3D-316 follow-up to @duudiis’s review. I checked #2706 at 8573c6b3e41c887d7830d4c3d4e9144afd3a9eab and #2693 at a5fdc8bbf9dccff7af405ea780b08aa783b477fd, common base 49475796c226c8511b9470bf97a4daf57b8af323, using Windows x64 MSVC 19.38.33145 and SDK 10.0.26100.0, CPU only.

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head merged with main f847748, MSVC 14.51):

  • RTX 4080 SUPER: resource_detile passes.
  • Intel UHD 770 (VK_LOADER_DRIVERS_DISABLE=*nv*): resource_detile passes.
  • Negative control: reverting d3d12_vulkan_device.cpp to main leaves resource_detile passing, because the test is a CPU-only test of resource_detile.h and never goes through the device hook; so it does not cover the wiring in d3d12_vulkan_device.cpp. I also swapped the 4-bpe swizzle pair (3, -3 to -3, 3, the point of the RDNA2 comment) and the test still passed, since it checks that the swizzle is a bijection and that tile/detile round-trips, both of which any permutation satisfies. A known-answer vector (a few texel offsets for one format) would pin the layout; as it stands the layout itself is not tested.

Only resource_detile was built and run, not the full ctest.

@Dom-in

Dom-in commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Windows + GPU check: AMD Radeon RX 9070 (RDNA4), Windows 11, MSVC Release, Vulkan SDK 1.4.363. Tested head f31fb3a merged onto main f847748 (clean merge), full build, full ctest -C Release -j 4: 100% tests passed out of 133 | new failures vs main: none. The main baseline on the same machine passes 132/132.

AI-assisted (Claude Code); runs on my machine.

@bielfreedom

Copy link
Copy Markdown

Tested f31fb3a6 (on its base f847748c) on AMD Radeon RX 9070 XT (RDNA4), AMD driver 32.0.32029.2010 (subgroup 32..64), Windows 11 Pro build 26200, MSVC build of runtime/, with Vulkan validation on (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363, VK_LOADER_LAYERS_DISABLE=~implicit~): pass.

  • Full ctest including this PR's new and changed tests: 130/130 pass, 0 failures, 0 validation errors.

AI-assisted (Claude Code). No game files.

@sh1shu1

sh1shu1 commented Nov 22, 2026 •

Copy link
Copy Markdown

Tested f31fb3a on NVIDIA RTX 4070 SUPER (driver 616.92), Windows 11 26200, MSVC 14.44, Vulkan SDK 1.4.363, RelWithDebInfo: pass, no regression against main f847748.

  • ctest: 141/141 passed, same 8 skips as main f847748
  • resource_detile: 1/1
  • run_gpu_tests (LEONIDA_VK_VALIDATION=1): 153/199 native, 184/199 with LEONIDA_W64_EMULATE=1, 0 failed; identical to main f847748
    AI-assisted (Claude Code, Claude Fable 5.1).

Implement GFX10 SW_64KB_D 64 KiB detiling and tiling from Mesa addrlib swizzle equations (GFX10_SW_64K_D_RBPLUS_PATINFO) with compile-time LUT coordinate evaluation. Add synthetic round-trip unit tests in resource_detile_test covering 1, 2, 4, 8, 16 BPP, sub-tile and BCn formats. Update docs/d3d12_vulkan.md.
@dcastell1

Copy link
Copy Markdown
Contributor

Tested 7a4e8d7 on RTX 5090, 621.24, Windows 11: pass. Built resource_detile_test from this tree and ran ctest -R resource_detile → 1/1 passed (0.14 s).

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 7a4e8d7 on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). Ran ctest -R "^resource_detile$" (the test this PR adds): pass on both GPUs, 1/1 each (it is a CPU round-trip test; the device only selects the environment). No game files.

AI-assisted.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new span helpers promise bounds checking, but unchecked size multiplication lets very large valid dimensions bypass the span checks and reach out-of-bounds memcpy operations.

const std::uint32_t tiles_x = (blocks_w + shape.width - 1) / shape.width;
const std::uint32_t tiles_y = (blocks_h + shape.height - 1) / shape.height;

const std::size_t expected_linear = static_cast<std::size_t>(blocks_w) * blocks_h * element_bytes;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Check for overflow before accepting the required span sizes. On 64-bit, width = height = 2^31, element_bytes = 4, block_dim = 1 wraps both required byte counts to zero (2^31 * 2^31 * 4 and the corresponding tile count times 64 KiB). Empty spans then pass this check and detile_typed immediately calls memcpy out of bounds. The same unchecked multiplication is in tile_surface_64k and the public size helpers. Use checked multiply/ceil-div arithmetic and reject overflow before touching either span.

@mesterember104-cpu

Copy link
Copy Markdown

Tested 7a4e8d7 on Intel Tiger Lake Xe-LP (0x9a78, iGPU, subgroup 8..32, no mesh), driver 32.0.101.7088, Windows 11, MSVC 14.44: pass

Full runtime ctest on the PR head: 130/138 passed, 5 skipped (shader work folder / driver-feature skips), 2 failed, neither from this PR's code: d3d12_buffer_tiles (the sparse-tile-mappings failure on Intel reported as a main finding; deterministic here) and winhttp_offline (a newer-main test, 9f41bf65, that fails 3 of its 40 HANDLE_CLOSING checks on this machine — see also my #3172 run). The PR's own tiling tests all pass: gfx10_tiling, resource_tiling, resource_detile (and notably d3d12_descriptor passes on this branch — the D3D-219 minimum-LOD failure I hit on 16282101-based builds is gone here, so that fix has landed on main in the meantime).

AI-assisted (GLM, operated by the account owner).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.