xg_x: calculate HTILE plane for single-mip depth-bind textures and write correct plane usages #119

Closed
github-bot wants to merge 1 commit from gh-pr/119/head into gh-pr/119/base
Owner

@⁠adriaespii on GitHub · 2026-11-22 12:40 UTC · original
adriaespii:contrib/xg-x-htile-plane → main · +203 −24 · 3 files · 104 commits · closed without merging

Summary

This PR implements HTILE plane calculation and correct plane usage serialization for depth-bind textures in xg_x, resolving the depth buffer allocation size mismatch identified in docs/BLOCKERS.md:244.

Context & Problem

In docs/BLOCKERS.md:244:

xg_x HTILE plane missing (open, all depth-bind textures): for a depth-bind texture with one mip, the console layout starts with an HTILE plane (usage 4, align 0x800, bpe 0), then depth (7) and stencil (8). This applies to fmt 19/40/55 at 1x and MSAA. Ours omits it, so sizes come out short by the HTILE size: Trackmania's 1920x1080 2x fmt 19 is 0x13ec000 here against 0x140c000 on the console. Mip chains get no HTILE.

Before this fix:

  1. compute_with omitted the HTILE metadata plane when calculating depth layouts, resulting in depth buffers allocated by games (like Trackmania) falling short of console sizes.
  2. In write_layout, plane count and usages were hardcoded to video planar layouts (0x14 = 2, 0x18 = 5, 0x5e0 = 6), whereas console encodes each plane's usage at 0x18 + i * PLANE_PITCH (4 for HTILE, 7 for Depth, 8 for Stencil).

Key Changes

  1. HTILE Plane Calculation (crates/compat/xg_x/src/layout.rs):
    • Implemented pub fn htile_plane(d: &Desc) -> Plane:
      • Linear HTILE rule for single slices (align(pitch, 128) * align(height, 64) / 16).
      • Tiled HTILE rule for arrays (align(pitch, 512) * align(height, 512) / 16).
      • Aligned per-slice to 0x4000 blocks, with 2 KiB (0x800) base plane alignment and bpe 0.
  2. compute_depth and compute_depth_msaa:
    • Prepends the HTILE plane (usage 4) before the depth plane (usage 7) and stencil plane (usage 8).
    • Offsets depth and stencil planes after the HTILE plane, preserving exact plane alignments.
    • For Trackmania's 1920x1080 2x fmt 19 texture:
      • HTILE plane: 0x20000 bytes (usage 4, align 0x800, offset 0)
      • Depth plane: 0xff0000 bytes (usage 7, align 0x2000, offset 0x20000)
      • Stencil plane: 0x3fc000 bytes (usage 8, align 0x2000, offset 0x1010000)
      • Total size: 0x140c000 (exact console match; previously was 0x13ec000).
    • Supports plain depth formats without stencil (fmt 40 / DXGI_FORMAT_D32_FLOAT, fmt 55 / DXGI_FORMAT_D16_UNORM) as 2-plane layouts (HTILE + Depth).
  3. Plane Usages in write_layout:
    • Added usage: u32 to Plane.
    • Serialized active plane usages at 0x18 + i * PLANE_PITCH (matching offset 0x18 for plane 0, 0x5e0 for plane 1, 0xba8 for plane 2), correctly setting plane counts at 0x14.
  4. XGCreateTextureComputerWithPitch (crates/compat/xg_x/src/lib.rs):
    • Routes single-mip textures with BIND_DEPTH_STENCIL (flags & 2 != 0) and depth formats through layout::compute_depth.
  5. Unit Tests (crates/compat/xg_x/src/layout_oracle_tests.rs):
    • compute_depth_trackmania_matches_console_exact: verifies exact 0x140c000 layout, plane offsets, alignments, and serialized buffer layout.
    • compute_depth_array_uses_512_tiling_rule: verifies 512x512 tile HTILE rule on array depth buffers.
    • compute_depth_plain_formats: verifies 2-plane HTILE+Depth layouts for fmt 40 and fmt 55.
    • Updated depth_bind_planar_msaa_planes_match_console to compare direct 3-plane layouts against console oracle.

Verification

  • cargo check -p xg_x: Clean compilation (0 errors).
  • cargo test -p xg_x --lib compute_depth: 4/4 passed.
  • cargo test -p xg_x --lib -- --skip oracle --skip console --skip built_dll: 11/11 passed.
**@⁠adriaespii** on GitHub · 2026-11-22 12:40 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/119) `adriaespii:contrib/xg-x-htile-plane` → `main` · +203 −24 · 3 files · 104 commits · closed without merging ### Summary This PR implements HTILE plane calculation and correct plane usage serialization for depth-bind textures in `xg_x`, resolving the depth buffer allocation size mismatch identified in `docs/BLOCKERS.md:244`. ### Context & Problem In `docs/BLOCKERS.md:244`: > **xg_x HTILE plane missing (open, all depth-bind textures):** for a depth-bind texture with one mip, the console layout starts with an HTILE plane (usage 4, align 0x800, bpe 0), then depth (7) and stencil (8). This applies to fmt 19/40/55 at 1x and MSAA. Ours omits it, so sizes come out short by the HTILE size: Trackmania's 1920x1080 2x fmt 19 is 0x13ec000 here against 0x140c000 on the console. Mip chains get no HTILE. Before this fix: 1. `compute_with` omitted the HTILE metadata plane when calculating depth layouts, resulting in depth buffers allocated by games (like Trackmania) falling short of console sizes. 2. In `write_layout`, plane count and usages were hardcoded to video planar layouts (`0x14 = 2`, `0x18 = 5`, `0x5e0 = 6`), whereas console encodes each plane's usage at `0x18 + i * PLANE_PITCH` (4 for HTILE, 7 for Depth, 8 for Stencil). ### Key Changes 1. **HTILE Plane Calculation (`crates/compat/xg_x/src/layout.rs`)**: - Implemented `pub fn htile_plane(d: &Desc) -> Plane`: - Linear HTILE rule for single slices (`align(pitch, 128) * align(height, 64) / 16`). - Tiled HTILE rule for arrays (`align(pitch, 512) * align(height, 512) / 16`). - Aligned per-slice to `0x4000` blocks, with 2 KiB (`0x800`) base plane alignment and bpe 0. 2. **`compute_depth` and `compute_depth_msaa`**: - Prepends the HTILE plane (usage 4) before the depth plane (usage 7) and stencil plane (usage 8). - Offsets depth and stencil planes after the HTILE plane, preserving exact plane alignments. - For Trackmania's 1920x1080 2x fmt 19 texture: - HTILE plane: 0x20000 bytes (usage 4, align 0x800, offset 0) - Depth plane: 0xff0000 bytes (usage 7, align 0x2000, offset 0x20000) - Stencil plane: 0x3fc000 bytes (usage 8, align 0x2000, offset 0x1010000) - Total size: `0x140c000` (exact console match; previously was `0x13ec000`). - Supports plain depth formats without stencil (fmt 40 / `DXGI_FORMAT_D32_FLOAT`, fmt 55 / `DXGI_FORMAT_D16_UNORM`) as 2-plane layouts (HTILE + Depth). 3. **Plane Usages in `write_layout`**: - Added `usage: u32` to `Plane`. - Serialized active plane usages at `0x18 + i * PLANE_PITCH` (matching offset `0x18` for plane 0, `0x5e0` for plane 1, `0xba8` for plane 2), correctly setting plane counts at `0x14`. 4. **`XGCreateTextureComputerWithPitch` (`crates/compat/xg_x/src/lib.rs`)**: - Routes single-mip textures with `BIND_DEPTH_STENCIL` (`flags & 2 != 0`) and depth formats through `layout::compute_depth`. 5. **Unit Tests (`crates/compat/xg_x/src/layout_oracle_tests.rs`)**: - `compute_depth_trackmania_matches_console_exact`: verifies exact `0x140c000` layout, plane offsets, alignments, and serialized buffer layout. - `compute_depth_array_uses_512_tiling_rule`: verifies 512x512 tile HTILE rule on array depth buffers. - `compute_depth_plain_formats`: verifies 2-plane HTILE+Depth layouts for fmt 40 and fmt 55. - Updated `depth_bind_planar_msaa_planes_match_console` to compare direct 3-plane layouts against console oracle. ### Verification - `cargo check -p xg_x`: Clean compilation (0 errors). - `cargo test -p xg_x --lib compute_depth`: 4/4 passed. - `cargo test -p xg_x --lib -- --skip oracle --skip console --skip built_dll`: 11/11 passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:10:40 +00:00
github-bot closed this pull request 2026-11-26 06:10:40 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 13:13 UTC · original

Main's history was rewritten, which auto-closed this PR (sorry for the noise). A maintainer is checking this against the xg_x console oracle. No action needed for now. We will apply it with your authorship or comment with the exact change.

**@⁠omonkeyz** on GitHub · 2026-11-22 13:13 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/119#issuecomment-7079653821) Main's history was rewritten, which auto-closed this PR (sorry for the noise). A maintainer is checking this against the xg_x console oracle. No action needed for now. We will apply it with your authorship or comment with the exact change.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.