textures: copy BC7 blocks into the opt-in tiled layout #1236

Merged
Jess-Doit merged 2 commits from upstream-pr/1243 into main 2026-11-24 05:44:45 +00:00
Jess-Doit commented 2026-11-24 05:03:51 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-24T05:03:51Z.


Change

Refs #1140. This follows #1218, which added the opt-in X2W_XG_BC7_4K_S_LAYOUT layout query for single-slice, single-mip BC7_UNORM 2D textures. CopyIntoSubresource (slot 8) refused that profile with E_NOTIMPL; it now writes tiled data.

  • Copy: the visible BC7 blocks are copied from pitched source rows into the reported GFX10 4 KiB standard tiled surface: 16×16 blocks per 4 KiB tile, block order y0 y1 x0 x1 y2 x2 y3 x3 within a tile, tiles in row-major order. src/startup_stubs/xg_xs.cpp now computes the layout query and the copy from one Bc7Surface description, so the reported span, pitch and padded extent and the copy offsets can't drift apart.
  • Padding: blocks outside the visible extent are left as the caller had them.
  • Refused with E_INVALIDARG, destination untouched:
    • a non-zero plane or subresource;
    • a row pitch shorter than one block row;
    • a non-zero slice pitch shorter than the source rows;
    • a source that overlaps the destination.
  • Render mode: with X2W_D3D12_RENDER=1 the copy still returns E_NOTIMPL. The draw uploader reads backing memory as desktop rows, so tiled bytes would draw wrong until a matching detiler lands. This slice is for CPU-side diagnostics.

The default (non-opt-in) copy path is unchanged. Doc fix on the way: the X2W_XG_BC7_4K_S_LAYOUT row in docs/startup-runtime.md had a stray indent that broke the switch table.

Fixture

tests/startup_xg_bc7_layout_check.py already checked the layout query for 64×64 and 81×69 against the independent tile oracle in tests/gfx10_texture_roundtrip_check.py. It now also checks the copy:

  • Input: tagged oracle blocks, laid out with 16 bytes of 0x5a filler after each block row, so the row pitch is wider than a block row. 81×69 also passes an exact slice pitch; 64×64 passes 0.
  • Destination: seeded with 0xa5 and surrounded by guard bytes. Padding blocks stay 0xa5, which differs from the oracle's own padding filler, so a copy that writes padding fails.
  • Expected result: the oracle's tiled surface byte for byte, with the guards intact and the source unchanged.
  • Refusals: the five cases above each return 0x80070057 and leave the destination byte-identical. With X2W_D3D12_RENDER=1, the copy returns 0x80004001 and also leaves the destination byte-identical.

Negative control: with the block offset replaced by row-major (linear) order, the check fails at the first mismatched block: BC7 81x69 tiled blocks, padding and guards: first difference at byte 0x26, got 01, expected 00.

Verification

Tested a2baada4 (on f4a6caf4) on Windows 10 22H2 19045.6456, MSVC x64. These tests use no GPU.

. .\tools\dev.ps1
foreach ($preset in 'dev', 'release') {
    cmake --preset $preset
    cmake --build --preset $preset -j 2
    ctest --preset $preset --no-tests=error -R '^(startup_xg.*|startup_stubs|xg_swizzle_.*|cpu_native_xg_swizzle|gfx10_texture_roundtrip.*)$' -V
}
Configuration Result Linear-order mutation
Debug 9/9 passed, 0 skipped startup_xg_bc7_layout failed as expected
Release 9/9 passed, 0 skipped startup_xg_bc7_layout failed as expected

The 9 tests are startup_xg_bc7_layout, startup_stubs, xg_swizzle_select, xg_swizzle_select_mutation, xg_swizzle_factory, xg_swizzle_factory_no_write, cpu_native_xg_swizzle, gfx10_texture_roundtrip and gfx10_texture_roundtrip_controls. After restoring the change, both configurations passed 9/9 again.

Fixture output, both configurations:

BC7 81x69: span=16384 offset=0 padded_blocks=32x32 pitch=512 tiled copy exact, refusal controls passed
BC7 64x64: span=4096 offset=0 padded_blocks=16x16 pitch=256 tiled copy exact, refusal controls passed

Still open in #1140:

  • a detiler for the draw upload path, so render mode can use this profile;
  • independent evidence for the native XG selector values;
  • mips, arrays and other formats.

AI-assisted (Codex, reviewed with Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6575028456 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1243). Originally posted by [Jess-Doit](https://github.com/Jess-Doit) on 2026-11-24T05:03:51Z. --- ## Change Refs #1140. This follows #1218, which added the opt-in `X2W_XG_BC7_4K_S_LAYOUT` layout query for single-slice, single-mip `BC7_UNORM` 2D textures. `CopyIntoSubresource` (slot 8) refused that profile with `E_NOTIMPL`; it now writes tiled data. - **Copy:** the visible BC7 blocks are copied from pitched source rows into the reported GFX10 4 KiB standard tiled surface: 16×16 blocks per 4 KiB tile, block order `y0 y1 x0 x1 y2 x2 y3 x3` within a tile, tiles in row-major order. `src/startup_stubs/xg_xs.cpp` now computes the layout query and the copy from one `Bc7Surface` description, so the reported span, pitch and padded extent and the copy offsets can't drift apart. - **Padding:** blocks outside the visible extent are left as the caller had them. - **Refused with `E_INVALIDARG`, destination untouched:** - a non-zero plane or subresource; - a row pitch shorter than one block row; - a non-zero slice pitch shorter than the source rows; - a source that overlaps the destination. - **Render mode:** with `X2W_D3D12_RENDER=1` the copy still returns `E_NOTIMPL`. The draw uploader reads backing memory as desktop rows, so tiled bytes would draw wrong until a matching detiler lands. This slice is for CPU-side diagnostics. The default (non-opt-in) copy path is unchanged. Doc fix on the way: the `X2W_XG_BC7_4K_S_LAYOUT` row in `docs/startup-runtime.md` had a stray indent that broke the switch table. ## Fixture `tests/startup_xg_bc7_layout_check.py` already checked the layout query for 64×64 and 81×69 against the independent tile oracle in `tests/gfx10_texture_roundtrip_check.py`. It now also checks the copy: - **Input:** tagged oracle blocks, laid out with 16 bytes of `0x5a` filler after each block row, so the row pitch is wider than a block row. 81×69 also passes an exact slice pitch; 64×64 passes 0. - **Destination:** seeded with `0xa5` and surrounded by guard bytes. Padding blocks stay `0xa5`, which differs from the oracle's own padding filler, so a copy that writes padding fails. - **Expected result:** the oracle's tiled surface byte for byte, with the guards intact and the source unchanged. - **Refusals:** the five cases above each return `0x80070057` and leave the destination byte-identical. With `X2W_D3D12_RENDER=1`, the copy returns `0x80004001` and also leaves the destination byte-identical. **Negative control:** with the block offset replaced by row-major (linear) order, the check fails at the first mismatched block: `BC7 81x69 tiled blocks, padding and guards: first difference at byte 0x26, got 01, expected 00`. ## Verification Tested `a2baada4` (on `f4a6caf4`) on Windows 10 22H2 `19045.6456`, MSVC x64. These tests use no GPU. ```powershell . .\tools\dev.ps1 foreach ($preset in 'dev', 'release') { cmake --preset $preset cmake --build --preset $preset -j 2 ctest --preset $preset --no-tests=error -R '^(startup_xg.*|startup_stubs|xg_swizzle_.*|cpu_native_xg_swizzle|gfx10_texture_roundtrip.*)$' -V } ``` | Configuration | Result | Linear-order mutation | |---|---|---| | Debug | 9/9 passed, 0 skipped | `startup_xg_bc7_layout` failed as expected | | Release | 9/9 passed, 0 skipped | `startup_xg_bc7_layout` failed as expected | The 9 tests are `startup_xg_bc7_layout`, `startup_stubs`, `xg_swizzle_select`, `xg_swizzle_select_mutation`, `xg_swizzle_factory`, `xg_swizzle_factory_no_write`, `cpu_native_xg_swizzle`, `gfx10_texture_roundtrip` and `gfx10_texture_roundtrip_controls`. After restoring the change, both configurations passed 9/9 again. Fixture output, both configurations: ```text BC7 81x69: span=16384 offset=0 padded_blocks=32x32 pitch=512 tiled copy exact, refusal controls passed BC7 64x64: span=4096 offset=0 padded_blocks=16x16 pitch=256 tiled copy exact, refusal controls passed ``` **Still open in #1140:** - a detiler for the draw upload path, so render mode can use this profile; - independent evidence for the native XG selector values; - mips, arrays and other formats. AI-assisted (Codex, reviewed with Claude Code).
upstream-sync closed this pull request 2026-11-24 06:47:13 +00:00
tedin7 commented 2026-11-24 06:47:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T05:07:52Z.

Review: COMMENTED


Reviewed PR #1243 at commit a2baada4 (a2baada46c3cb7439bb4ddc8c9f1ddbc876faa47).

Findings

  • Shared Bc7Surface & GFX10 4 KiB tiling (src/startup_stubs/xg_xs.cpp):
    • Unifying write_bc7_4k_s_layout and copy_bc7_4k_s under Bc7Surface keeps the reported span, padded_row_bytes(), padded extent, and block_offset(column, row) in sync.
    • block_offset bit-packing (y & 3) | ((x & 3) << 2) | ((y & 4) << 2) | ((x & 4) << 3) | ((y & 8) << 3) | ((x & 8) << 4) matches the GFX10 4 KiB standard non-XOR BC7 y0 y1 x0 x1 y2 x2 y3 x3 block order.
  • Bounds, pitch, padding, and overflow (copy_bc7_4k_s):
    • use_bc7_4k_s_layout bounds Width/Height to 1..16384 (D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION), so surface.rows >= 1 (surface.rows - 1 cannot underflow), surface.size() <= 256 MiB, and u64{surface.rows - 1} * row_pitch + row_bytes / u64{y} * row_pitch avoid 32-bit multiplication overflow.
    • row_pitch < row_bytes, non-zero slice_pitch < source_bytes, plane || subresource, and [dest, dest + surface.size()) vs [source, source + source_bytes) overlap all return E_INVALIDARG before touching dest; x2w_local::readable/writable guard bad pointers with E_FAIL.
    • Only visible (0..columns-1, 0..rows-1) blocks are copied; tile padding blocks are left untouched.
    • X2W_D3D12_RENDER=1 is checked first in computer_copy_into and returns E_NOTIMPL, leaving dest unchanged.
  • Compliance & Docs: AGENTS.md title-neutrality is preserved, and docs/startup-runtime.md fixes the X2W_XG_BC7_4K_S_LAYOUT=1 table row indentation and documents the CopyIntoSubresource diagnostic behavior.

Verification (Linux host)

Windows MSVC build and CTest targets (startup_xg_bc7_layout, etc., which require the compiled startup_stub_xg_xs DLL) were not run locally on this Linux environment.

Commands run on a2baada4:

  • git diff --check $(git merge-base origin/main HEAD)..HEAD -> exit 0 (clean)
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py -> passed (Self-test passed. Checked 16 Markdown files and 280 links., 0 fail, 0 skip)
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py -> passed (Self-test passed. Checked 1048 tracked source, script and build/CI files: no bidirectional controls., 0 fail, 0 skip)
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/form_coverage_check.py -> passed (form coverage checks passed, 0 fail, 0 skip)
  • /root/.local/share/x2w-rev-venv/bin/python -B -m py_compile tests/startup_xg_bc7_layout_check.py -> exit 0
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/gfx10_texture_roundtrip_check.py -> passed (Ran 6 tests in 0.006s, gfx10_texture_roundtrip all: PASS, 0 fail, 0 skip)
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6034010394 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1243#pullrequestreview-6034010394). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T05:07:52Z. **Review:** COMMENTED --- Reviewed PR #1243 at commit `a2baada4` (`a2baada46c3cb7439bb4ddc8c9f1ddbc876faa47`). ### Findings - **Shared `Bc7Surface` & GFX10 4 KiB tiling (`src/startup_stubs/xg_xs.cpp`)**: - Unifying `write_bc7_4k_s_layout` and `copy_bc7_4k_s` under `Bc7Surface` keeps the reported span, `padded_row_bytes()`, padded extent, and `block_offset(column, row)` in sync. - `block_offset` bit-packing `(y & 3) | ((x & 3) << 2) | ((y & 4) << 2) | ((x & 4) << 3) | ((y & 8) << 3) | ((x & 8) << 4)` matches the GFX10 4 KiB standard non-XOR BC7 `y0 y1 x0 x1 y2 x2 y3 x3` block order. - **Bounds, pitch, padding, and overflow (`copy_bc7_4k_s`)**: - `use_bc7_4k_s_layout` bounds `Width`/`Height` to `1..16384` (`D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION`), so `surface.rows >= 1` (`surface.rows - 1` cannot underflow), `surface.size() <= 256 MiB`, and `u64{surface.rows - 1} * row_pitch + row_bytes` / `u64{y} * row_pitch` avoid 32-bit multiplication overflow. - `row_pitch < row_bytes`, non-zero `slice_pitch < source_bytes`, `plane || subresource`, and `[dest, dest + surface.size())` vs `[source, source + source_bytes)` overlap all return `E_INVALIDARG` before touching `dest`; `x2w_local::readable`/`writable` guard bad pointers with `E_FAIL`. - Only visible `(0..columns-1, 0..rows-1)` blocks are copied; tile padding blocks are left untouched. - `X2W_D3D12_RENDER=1` is checked first in `computer_copy_into` and returns `E_NOTIMPL`, leaving `dest` unchanged. - **Compliance & Docs**: `AGENTS.md` title-neutrality is preserved, and `docs/startup-runtime.md` fixes the `X2W_XG_BC7_4K_S_LAYOUT=1` table row indentation and documents the `CopyIntoSubresource` diagnostic behavior. ### Verification (Linux host) Windows MSVC build and CTest targets (`startup_xg_bc7_layout`, etc., which require the compiled `startup_stub_xg_xs` DLL) were **not** run locally on this Linux environment. Commands run on `a2baada4`: - `git diff --check $(git merge-base origin/main HEAD)..HEAD` -> exit 0 (clean) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py` -> passed (`Self-test passed. Checked 16 Markdown files and 280 links.`, 0 fail, 0 skip) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py` -> passed (`Self-test passed. Checked 1048 tracked source, script and build/CI files: no bidirectional controls.`, 0 fail, 0 skip) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/form_coverage_check.py` -> passed (`form coverage checks passed`, 0 fail, 0 skip) - `/root/.local/share/x2w-rev-venv/bin/python -B -m py_compile tests/startup_xg_bc7_layout_check.py` -> exit 0 - `/root/.local/share/x2w-rev-venv/bin/python -B tests/gfx10_texture_roundtrip_check.py` -> passed (`Ran 6 tests in 0.006s`, `gfx10_texture_roundtrip all: PASS`, 0 fail, 0 skip)
tedin7 commented 2026-11-24 06:50:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T05:14:53Z.


Windows Server 2025 retest on a2baada46c3cb7439bb4ddc8c9f1ddbc876faa47 (startup_stub_xg_xs, startup_stub_consumer):

  • dev (Debug): ctest --preset dev -R '^(startup_xg_bc7_layout|startup_stubs)$' — 2/2 passed (startup_xg_bc7_layout 0.14s, startup_stubs 0.16s)
  • release (Release /O2): ctest --preset release -R '^(startup_xg_bc7_layout|startup_stubs)$' — 2/2 passed (startup_xg_bc7_layout 0.18s, startup_stubs 0.19s)
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7122766353 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1243#issuecomment-7122766353). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T05:14:53Z. --- Windows Server 2025 retest on `a2baada46c3cb7439bb4ddc8c9f1ddbc876faa47` (`startup_stub_xg_xs`, `startup_stub_consumer`): - `dev` (Debug): `ctest --preset dev -R '^(startup_xg_bc7_layout|startup_stubs)$'` — 2/2 passed (`startup_xg_bc7_layout` 0.14s, `startup_stubs` 0.16s) - `release` (Release /O2): `ctest --preset release -R '^(startup_xg_bc7_layout|startup_stubs)$'` — 2/2 passed (`startup_xg_bc7_layout` 0.18s, `startup_stubs` 0.19s)
Jess-Doit commented 2026-11-24 06:50:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-24T05:30:09Z.


Merged current main (7a4043e4) into the branch as c49b4c24 to clear a conflict with #1237 in docs/startup-runtime.md. I kept this PR's switch-table row and CopyIntoSubresource paragraph, and #1237's updated sampler-heap text. There is no code change from a2baada4.

Retested c49b4c24 on Windows 10 22H2 19045.6456, MSVC x64, with a full build per configuration:

cmake --build --preset $preset -j 2
ctest --preset $preset --no-tests=error -R '^(startup_xg.*|startup_stubs|xg_swizzle_.*|cpu_native_xg_swizzle|gfx10_texture_roundtrip.*)$' -V

Debug and Release each passed 9/9 with no skips. Thanks @tedin7 for the review.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7123007362 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1243#issuecomment-7123007362). Originally posted by [Jess-Doit](https://github.com/Jess-Doit) on 2026-11-24T05:30:09Z. --- Merged current main (`7a4043e4`) into the branch as `c49b4c24` to clear a conflict with #1237 in `docs/startup-runtime.md`. I kept this PR's switch-table row and `CopyIntoSubresource` paragraph, and #1237's updated sampler-heap text. There is no code change from `a2baada4`. Retested `c49b4c24` on Windows 10 22H2 `19045.6456`, MSVC x64, with a full build per configuration: ```powershell cmake --build --preset $preset -j 2 ctest --preset $preset --no-tests=error -R '^(startup_xg.*|startup_stubs|xg_swizzle_.*|cpu_native_xg_swizzle|gfx10_texture_roundtrip.*)$' -V ``` Debug and Release each passed 9/9 with no skips. Thanks @tedin7 for the review.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1236
No description provided.