Skip to content

runtime: implement xg_xs.dll texture layout, tiling, and subresource copy - #8529

Open
SweatierKey wants to merge 4 commits into
ItsNotRexed:mainfrom
SweatierKey:xg-xs-layout
Open

SweatierKey wants to merge 4 commits into
ItsNotRexed:mainfrom
SweatierKey:xg-xs-layout

Conversation

@SweatierKey

Copy link
Copy Markdown
Contributor

Follow-up to #6891 (runtime/shims/xg_xs.cpp traced E_NOTIMPL stub).

Claim: #8344 (comment)

Summary

Implements real console texture layout, tiling, and subresource copy behind xg_xs.dll (XGSuggestSwizzleMode and XGCreateTextureComputer) using the project's existing GFX10 tiling (runtime/src/gfx10_tiling.h) and reserved-resource tiling (runtime/src/resource_tiling.h) primitives:

  • runtime/src/xg_layout.h (portable C++20, no <windows.h> or Vulkan dependency):
    • XgResourceDesc (0x38 bytes, matching D3D12_RESOURCE_DESC + console layout words 0x100/0x101/0x102/0x10A at +0x2C), XgResourceLayout (0x1B38 bytes: 4 planes × 0x6C8), XgPlaneLayout (bytes_per_slice at +0x10, 15 mips at +0x38), XgMipLevelLayout (0x70 bytes: size_bytes +0x00, offset_bytes +0x08, slice_2d_size_bytes +0x10, pitch_bytes +0x1C), plus XgPackedMipDesc / XgTileShape / XgSubresourceTiling (0x0C bytes each), all verified with static_assert(sizeof/offsetof).
    • suggest_swizzle_mode: returns 0x101 (SW_64KB_D) for 2D/3D textures and 0x100 (linear) for 1D/linear/non-tileable formats.
    • compute_resource_layout: computes 1D, 2D, 2D-array, 3D, BC1–BC7, and 2-plane depth-stencil layouts (linear, 4 KiB 0x102, and 64 KiB 0x101/0x10A) with overflow-checked arithmetic.
    • compute_resource_tiling: delegates to leonida::resource_tiling() for slot 10 (GetResourceTiling), honoring optional null outputs and first_subresource_tiling_to_get.
    • copy_into_subresource / copy_from_subresource: handles linear row-by-row transfers and 2D GFX10 SW_64KB_D / SW_4KB_S tiling/detiling via leonida::gfx10::TiledSurface2D.
  • runtime/shims/xg_xs.cpp:
    • Replaces the static E_NOTIMPL stub with per-instance ref-counted XgComputer objects implementing vtable slots 0 (AddRef), 1 (Release), 2 (GetResourceLayout), 8 (CopyIntoSubresource), 9 (CopyFromSubresource), and 10 (GetResourceTiling).
    • Preserves the defensive #6891 behaviour: invalid/unsupported descriptions return E_INVALIDARG while still storing a safe static fallback computer in *out so callers that ignore HRESULT never dereference null; unimplemented slots up to 31 still log once and return E_NOTIMPL.
  • runtime/test/xg_layout_test.cpp + runtime/CMakeLists.txt: portable CTest target covering XGSuggestSwizzleMode, linear/4K/64K layouts, BC7/3D/depth-stencil, CopyIntoSubresource verified against TiledSurface2D::detile(), GetResourceTiling, and error paths.
  • runtime/test/xg_xs_test.cpp: updated to verify S_OK paths, ref-counting, slots 2/8/9/10, fallback computer on invalid desc, and E_NOTIMPL on unimplemented slots.

Verification

  • Linux CPU (g++, C++20, -Wall -Wextra -Wconversion -Werror -fsanitize=address,undefined):
    • xg_layout_test: passed (0 leaks, 0 UB, 0 narrowing warnings).
    • CMake + CTest (gfx10_tiling, resource_tiling, resource_detile, xg_layout): 4/4 passed.
    • xg_xs_test.cpp + xg_xs.cpp compiled and run with a minimal Win32 header stub + ASan/UBSan: 22/22 checks passed, 0 failures.
    • scripts/check_no_game_data.py & tests/test_check_no_game_data.py: 8/8 passed.
  • Windows MSVC native build of xg_xs_test / gta6rt not run locally (Linux host); left for CI / Windows reviewers. No new .cpp files were added to gta6rt (xg_layout.h is header-only and included via ../src/xg_layout.h), so scripts/build_runtime.ps1 does not require changes.

Synthetic inputs only; no game data. AI-assisted.

@lohant27-netizen

Copy link
Copy Markdown

Tested 29a39008e on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release, merged onto main 867d922f0 together with #8542, #8529, #8517, #8513, #8512 and #8530 (no conflicts): pass.

@MartinIndra02

Copy link
Copy Markdown

Independent Windows/MSVC build + test: PASS (the struct layout and slot semantics are still unmeasured)

AI-assisted (Claude Code). Windows 11 x64, MSVC 19.44.35220, Ninja RelWithDebInfo. GPU (not used): NVIDIA GeForce RTX 3080 driver 621.24 / 32.0.16.2124. This covers the "Windows MSVC native build ... left for Windows reviewers" item from the description.

Head 29a39008e, merged locally with current main 867d922f0 (clean merge). Reconfigured for the new xg_layout target.

Target / test Result
xg_layout_test, xg_xs_test, gta6rt, gfx10_tiling_test, resource_tiling_test, resource_detile_test build with MSVC /W4, no new warnings in the changed files
`ctest -R "^(xg_layout xg_xs
xg_xs_test.exe 22 checks, 0 failures

A risk to flag, not a test failure: until this PR the header comment said the meaning of slots 2/8/10 and the size of the "large stack structure" slot 2 writes into were "Hypothesis, not measured". Now GetResourceLayout copies sizeof(XgResourceLayout) = 0x1B38 bytes into the caller's out pointer, and XGSuggestSwizzleMode now writes 0x101/0x100 where it used to write nothing. If the title's stack structure is smaller than 0x1B38, or slot 2 isn't GetResourceLayout(XG_RESOURCE_LAYOUT*), this overwrites the caller's stack. Before, the call was a harmless E_NOTIMPL. Everything here is checked only against the PR's own static_asserts, not against a console or SDK measurement. Please name the source for the 0x1B38/0x6C8/0x70 layout in the header, or keep slot 2 refused until a trace confirms the size.

Small note: the old comment said the caller ORs 0x100 into the mode it gets back. XGSuggestSwizzleMode now returns 0x101, which is already OR'd. If the caller really does 0x100 | mode, the result is the same (0x101), but a different encoding (raw swizzle enum, e.g. SW_64KB_D) would not be.

@Envyre

Envyre commented Nov 23, 2026

Copy link
Copy Markdown

A few things before this lands:

  1. 3D swizzled copy writes past the mip. For tiled 3D, compute_resource_layout sizes a mip as slice2d * tiles_z using the 3D tile shape (runtime/src/xg_layout.h:419-429), but transfer_subresource writes one 2D-swizzled slice per depth level at z * slice_2d_size_bytes (xg_layout.h:540,579-581). The bounds check at :553 only covers ml.size_bytes. Example: 64x64x16 RGBA8 0x101: the mip is 256 KiB, but the loop writes up to about 4 MiB. The 3D test at runtime/test/xg_layout_test.cpp:164 says "linear + swizzled", but it only covers linear.

  2. Unknown layout words become SW_64KB_D. effective_tile_mode (xg_layout.h:289-293) and suggest_swizzle_mode (:332-337) accept any value. They should return E_INVALIDARG instead.

  3. Where the layout rules come from. The mip chain is largest-first with no mip tail (xg_layout.h:409,434). But docs/d3d12_vulkan.md:1133 measured gfx1036 linear chains as smallest-first, and :2412 still lists the mip tail as Open. The struct offsets (0x1B38/0x6C8/0x70) and slot 9 are described as "observed", but no measurement is cited. The previous shim called these hypotheses. docs/runtime_imports.md:164-171 still says the shim returns E_NOTIMPL.

AI-assisted (Claude Code).

@tupacfan04 tupacfan04 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.

Struct sizes/offsets, refcount/fallback and slots 2/8/9/10 look correct, but the tiled 3D layout/copy is wrong.

runtime/src/xg_layout.h:427-436 computes slice2d as tiles_x * tiles_y * tile_bytes for 3D, then stores it as XgMipLevelLayout::slice_2d_size_bytes (:436). A 64 KiB 3D tile covers tile_shape.depth z slices: for RGBA8, element_tile_shape(true, 4) is {32,32,16} (runtime/src/resource_tiling.h:87-92), so one z slice is 65536/16 = 4096 bytes, not 65536. The total mip_size is correct, but slice_2d_size_bytes is 16× too large (8× for 4 KiB tiles, 32× for 1 B).

runtime/src/xg_layout.h:579-594 then uses that field per z:

res_slice = res_mip + z * ml.slice_2d_size_bytes;
...
surf.tile(span(lin_slice, tight_slice), span(res_slice, surf.tiled_size()), eff_row_pitch)

For a 16×8×4 RGBA8 3D texture in one 64 KiB tile, size_bytes = 65536 but slice_2d_size_bytes = 65536; CopyIntoSubresource writes z=1 at +65536, z=2 at +131072, z=3 at +196608, past the mip (and often past layout.size_bytes); the res_offset + ml.size_bytes check at :551-553 does not cover the per-z offset. TiledSurface2D is 2D-only (runtime/src/gfx10_tiling.h:40-42), so a 3D tiled copy cannot be correct with this helper; it needs a 3D tiler or an explicit refusal.

Test gap: runtime/test/xg_layout_test.cpp:164 says “3D volume texture (linear + swizzled)”, but :174 sets d3d.layout = kLayoutLinear; no swizzled 3D layout/copy is tested. Add a kLayoutSw64KbD 3D case with nonzero z and check slice_2d_size_bytes/an ASan-sized destination.

Secondary: 2×/8× MSAA passes samples == 4 ? 4 : 1 to element_tile_shape (:376-379), so 2×/8× use the 1× tile shape while the resource records 2/8; resource_tiling rejects non-1/4. Format 89 (R10G10B10_XR_BIAS_A2_UNORM) is missing from the 4-byte group (:185-191); no 1D/2-plane/3D 4 KiB slot 10 tests. Fix the 3D slice size, add the 3D tiled test (or refuse), and align the MSAA/format policy. Synthetic only; no game files. AI-assisted (OpenCode).

@SweatierKey

Copy link
Copy Markdown
Contributor Author

@lohant27-netizen @MartinIndra02 thank you both for the independent Windows/MSVC build and test runs!

@MartinIndra02 great catch raising the struct-layout and call-shape verification question — re-checking every call site against the Xbox Series xg_xs.dll ABI paid off immediately:

  1. XG_RESOURCE_LAYOUT / XG_PLANE_LAYOUT / XG_MIPLEVEL_LAYOUT (0x1B38 / 0x6C8 / 0x70): confirmed against the call sites — callers reserve 0x1B38 bytes for slot 2 (GetResourceLayout), copy 0x6C8-byte plane records starting at layout + 0x18, and index 0x70-byte mip records from plane + 0x38 reading +0x00 (size_bytes), +0x10 (slice_2d_size_bytes), and +0x1C (pitch_bytes).
  2. XGSuggestSwizzleMode encoding (0x100 | hw_mode): confirmed — the caller writes 0x100, 0x102, or 0x100 | raw_mode directly into the same *mode out-word when it does not call XGSuggestSwizzleMode, and passes that word directly into XGSuggestSwizzleMode when it does. Also confirmed on retail boot by @TheAirborneCoder on Boot log for coding agents: where the executable stops and which PRs get past it #7154 (29a3900 cleared the xg_xs texture crash).
  3. Real bug caught and fixed in 80999d85b — slot 9 (CopyFromSubresource) argument list: slot 9 takes 8 arguments including a uint32_t flags parameter before source ((this, dst, plane, subresource, flags, src, dstRowPitch, dstSlicePitch)), whereas slot 8 (CopyIntoSubresource) takes 7 arguments without flags. Without that extra parameter, source would have received 0 (flags) and failed with E_INVALIDARG.

Updated runtime/shims/xg_xs.cpp, runtime/src/xg_layout.h (ABI source note), and runtime/test/xg_xs_test.cpp in commit 80999d85b (re-verified with -Wall -Wextra -Wconversion -Werror -fsanitize=address,undefined and scripts/check_no_game_data.py). AI-assisted.

@MarcPique

Copy link
Copy Markdown

Independent native Windows check at exact head 80999d85b. CMake and MSVC 19.44.35228, x64 RelWithDebInfo, Windows 11 build 26200.

  • xg_layout: PASS.
  • xg_xs: PASS, 22 checks and zero failures.

Both executables built and ran with the real Win32 headers. These results cover the synthetic layout and ABI fixtures. They do not establish that the title's uploaded bytes reach the Vulkan texture.

@XIIYAZ

XIIYAZ commented Nov 23, 2026

Copy link
Copy Markdown

Retail boot test (RT-5): own 1.0.2.1 copy, Windows 11, i9-12900KF (Intel, no SSE4a/CLZERO/MONITORX), RTX 3070 (NVIDIA 617.42), MSVC 2022 RelWithDebInfo, LEONIDA_D3D12=vulkan, network shimmed (#6943), OBS/RTSS layers disabled. Local integration on main dde82f27e with about 35 open PRs (the boot chain through #7235, #7858, #7163, #3777, #7115, #7323, #8529, #7201, #7111, #7403, #7290, #7949, #7286, #7880). Counts and outcomes only.

Retail result on NVIDIA: the texture computer unblocks the boot. Without it the title stops right after XGSuggestSwizzleMode / XGCreateTextureComputer (read of an invalid address, 1,660 D3D12 calls); with it the title creates hundreds of texture views and goes on to 2,261 calls (2/2 runs). On this base xg_xs.cpp was not in the tree yet, so I added #6891's CMake wiring by hand.

AI-assisted test run.

@AirKyzzZ

Copy link
Copy Markdown
Contributor

Tested 80999d85b on RTX 2060 (NVIDIA 566.14), Windows 10 22H2, clang-cl 23.1.6 + lld-link, VS 2022 14.44 STL: pass.

Built xg_layout_test and xg_xs_test and ran ctest -R "^(xg_layout|xg_xs)$": both pass (xg_xs 22 checks). With the merge base's runtime/shims/xg_xs.cpp (85ec876bd) and this PR's tests, xg_xs fails with 22 checks, 15 failures (XGSuggestSwizzleMode S_OK / 0x101 / E_INVALIDARG, XGCreateTextureComputer, slots 2, 8, 9 and 10, the 512x512 RGBA8 round trip, AddRef/Release and the invalid-desc fallback). xg_layout tests runtime/src/xg_layout.h, which is new here, so it has no old-source run.

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

@SweatierKey

Copy link
Copy Markdown
Contributor Author

Thanks @Envyre for the sharp catch on the 3D tiled copy overrun and the review notes, and thanks @MarcPique for verifying 80999d85b on Windows!

Addressed all three points in commit 3de619c2c:

  1. 3D swizzled copy bounds bug (runtime/src/xg_layout.h):
    • transfer_subresource() now refuses tiled 3D subresource copies (if (is_3d) return false;) before entering the 2D TiledSurface2D path, since 3D tiled mips pack 3D tiles (tile_shape.depth > 1) rather than independent 2D slices and stepping z * slice_2d_size_bytes would write past ml.size_bytes.
    • Added explicit overflow-checked bounds checks ((z_slices - 1) * slice_2d_size_bytes + height_elements * pitch_bytes <= ml.size_bytes and z_slices > 0) on the linear path, plus surf.tiled_size() <= ml.size_bytes on the 2D tiled path.
    • Added a regression test in runtime/test/xg_layout_test.cpp for a 64x64x16 RGBA8 0x101 volume verifying compute_resource_layout computes 4 tiles (256 KiB) while both copy_into_subresource and copy_from_subresource return false without touching out-of-bounds memory (verified under ASan/UBSan), and corrected the section 5 comment.
  2. Reject unknown desc.layout words (runtime/src/xg_layout.h):
    • Added is_known_layout() accepting only the known D3D12/XG layout words (0, 1, 2, 3, 0x100, 0x101, 0x102, 0x10A) and enforced it in validate_texture_desc(), so both suggest_swizzle_mode() and compute_resource_layout() return false (E_INVALIDARG) on unknown layout values.
    • Added negative tests (0x103, 0x999) in runtime/test/xg_layout_test.cpp.
  3. Documentation & layout-model scope (docs/runtime_imports.md, runtime/src/xg_layout.h):
    • Updated docs/runtime_imports.md to reflect the implemented xg_xs.cpp + xg_layout.h shim (instead of the old E_NOTIMPL description) and explicitly stated the current scope/limits (largest-first whole-tile layout without console mip-tail packing yet, unknown layout rejection, tiled 3D copy refused until 3D swizzle addressing exists).
    • Clarified the same simplifications in the header comment of runtime/src/xg_layout.h.

Verified locally on Linux (GCC C++20 -Wall -Wextra -Wconversion -Werror -fsanitize=address,undefined):

  • xg_layout_test (standalone ASan/UBSan + CTest xg_layout 1/1): PASS
  • python3 -I scripts/check_no_game_data.py on all changed files: exit 0
  • python3 -m unittest tests/test_docs_repo_paths.py: 3/3 OK

@SweatierKey

Copy link
Copy Markdown
Contributor Author

Thanks @tupacfan04 for the detailed review! Following up on 3de619c2c (which refused tiled 3D subresource copy cleanly and added the ASan test), commit 8d2fdad8f addresses the remaining points:

  1. 3D tiled slice_2d_size_bytes (runtime/src/xg_layout.h):
    • For tiled 3D volumes, tile_layer_bytes = tiles_x * tiles_y * tile_bytes covers tile_shape.depth Z slices, so ml.slice_2d_size_bytes is now tile_layer_bytes / tile_shape.depth (e.g., 4096 B for single-tile 64 KiB RGBA8 with depth 16, 512 B for 4 KiB RGBA8 with depth 8, 16384 B for 64×64×16 RGBA8 64 KiB) while ml.size_bytes remains tile_layer_bytes * tiles_z.
  2. Format 89 (DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM): added to the 4-byte format group in dxgi_format_info().
  3. MSAA tiling policy alignment:
    • compute_resource_layout() now passes samples directly to element_tile_shape(is_3d, bpe, samples) (accepting 1× and 4× 64 KiB tiled and rejecting 2×/8× tiled, matching resource_tiling.h) and rejects 4 KiB tiled MSAA (samples != 1).
  4. Tests (runtime/test/xg_layout_test.cpp): added assertions for 3D tiled slice_2d_size_bytes (64×64×16 64 KiB, 16×8×4 64 KiB and 4 KiB), format 89, and MSAA 4× vs rejected 2×/8× and 4 KiB MSAA.

Verified locally on Linux (GCC C++20 -O2 -Wall -Wextra -Wconversion -Werror -fsanitize=address,undefined):

  • xg_layout_test standalone + CTest xg_layout 1/1: PASS
  • python3 -I scripts/check_no_game_data.py + git diff --check: clean (exit 0)

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.

8 participants