Skip to content

d3d12: WriteToSubresource/ReadFromSubresource check a BC box against the block-padded mip - #9162

Open
haayden wants to merge 1 commit into
ItsNotRexed:mainfrom
haayden:fix/bc-subresource-box
Open

haayden wants to merge 1 commit into
ItsNotRexed:mainfrom
haayden:fix/bc-subresource-box

Conversation

@haayden

@haayden haayden commented Nov 23, 2026

Copy link
Copy Markdown

Claimed in the coordination issue: #9065 (comment)

What and why

Resource::region() (runtime/shims/d3d12_vulkan_device.cpp), used by WriteToSubresource and ReadFromSubresource, checked a block-compressed box against the unpadded mip size. It also allowed a right or bottom edge that isn't a whole block when that edge equalled the mip width or height. The host runtime does the reverse: every edge of a caller's box must be a whole block, and the box may reach the block-padded edge of the mip.

BC1 12x12 with 3 mips (mip 1 is 6x6, mip 2 is 3x3). The NVIDIA host runtime and WARP agree with each other:

Box Host / WARP This layer on main
(0,0)-(8,8) on 6x6 S_OK E_INVALIDARG
(4,0)-(8,4) on 6x6 S_OK E_INVALIDARG
(0,0)-(4,4) on 3x3 S_OK E_INVALIDARG
(0,0)-(6,6), (4,4)-(6,6) on 6x6 E_INVALIDARG S_OK
(0,0)-(3,3) on 3x3 E_INVALIDARG S_OK

The same pattern shows on BC7 10x6 and on a 3D BC1 12x10 with depth. The fix checks the box against the padded size, requires whole blocks on every edge of a caller-supplied box, and then clamps right and bottom to the mip edge for the Vulkan copy. A null box is unchanged.

#8777 fixes block-compressed bounds only in the GPU footprint copy path (vulkan_replay.h). This is the CPU path.

Tested

Windows 11, RTX 5070 Ti (NVIDIA 621.14), MSVC 14.44, RelWithDebInfo, on main 867d922. No game files.

  • New d3d12_subresource_probe_test runs the same writes and reads on the Vulkan device and on the host device, then compares every HRESULT and a hash of every block row read back. PROBE_WARP=1 uses WARP as the host instead. It skips (77) when either device is missing.
    • main: subresource probe: 3 cases differ, exit 1.
    • this PR: 0 cases differ against the NVIDIA host and against WARP, exit 0.
  • ctest -R "d3d12_subresource|resource_detile|d3d12_placed|d3d12_existing_heap|d3d12_buffer_tiles|d3d12_texture_tiles|d3d12_noop_copy": 8/8 pass, including the existing d3d12_subresource_copy (host-copy and staging paths).
  • No Vulkan validation layer is installed on this machine, so there was no LEONIDA_VK_VALIDATION=1 run.

AI-assisted: yes

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a, no translator change)

…the block-padded mip

Resource::region() checked a block-compressed box against the unpadded mip size and let a right or bottom edge
that isn't a whole block through when it equalled the mip edge. The host runtime and WARP do the reverse: every
edge of a caller's box is in whole blocks and may reach the block-padded edge (0,0)-(8,8) on a 6x6 BC1 mip, while
(0,0)-(6,6) is refused. The box is now checked against the padded size, and right/bottom are clamped to the mip
edge for the copy.

d3d12_subresource_probe_test runs the same writes and reads on the Vulkan device and on the host (PROBE_WARP=1:
WARP) and compares every HRESULT and a hash of every block row: 3 cases differ on main, 0 with the fix.

AI-assisted: yes

@abnalstlll-byte abnalstlll-byte 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.

Independent Windows review (commit caa1b6be3f19c1be42336c1d88583076226b0280)

What changed:

  • runtime/shims/d3d12_vulkan_device.cpp:
    • In Resource::region(), validates block-compressed (BC) copy boxes against block-padded mip dimensions (width + k - 1) / k * k.
    • Rejects partial-block unaligned box boundaries with E_INVALIDARG, matching D3D12/WARP host behavior, while safely clamping underlying Vulkan copy extents to unpadded mip limits.
  • runtime/test/d3d12_subresource_probe_test.cpp:
    • 155-line test suite verifying odd-sized BC1/BC7 mip boundaries against native D3D12 behavior.

Verification:

  • Verified on Windows host: matches D3D12 specification.
  • Clean diff: no game files.

Ready for merge.

@smowlyy

smowlyy commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Verified head caa1b6be3 merged onto main (867d922f0) on native Windows 11 (10.0.26300.9974, MSVC 19.44, RelWithDebInfo, Vulkan SDK 1.4.363.0) across both GPUs (both VK_EXT_host_image_copy and staging-submit paths) with LEONIDA_VK_VALIDATION=1:

  • python -I scripts/check_no_game_data.py on all 3 changed files: clean (exit 0)
  • cmake --build runtime/build --target d3d12_subresource_probe_test --parallel: built cleanly
  • LEONIDA_VK_VALIDATION=1 .\runtime\build\d3d12_subresource_probe_test.exe on NVIDIA GeForce RTX 5060 Ti (driver 610.88, Vulkan 1.4.341, host image copy): passed, 0 validation errors (subresource probe: 0 cases differ)
  • LEONIDA_VK_VALIDATION=1 LEONIDA_VK_DEVICE=Radeon .\runtime\build\d3d12_subresource_probe_test.exe on AMD Radeon(TM) Graphics (driver 24.10.24, Vulkan 1.3.280, staging submit): passed, 0 validation errors (subresource probe: 0 cases differ)

AI-assisted.

@thehollowedone

Copy link
Copy Markdown

Additional padded-box readback for caa1b6be on native Windows 11, RTX 3080 Ti (621.35), MSVC 19.44, Vulkan SDK 1.4.363.

I ran the three stock valid BC1 boxes plus two valid BC7 edge boxes, one on a 10×6 mip padded to 12×8 and one on a 5×3 mip padded to 8×4. With both native D3D12 and Microsoft WARP as references, all five writes and immediate reads returned S_OK. All 80 bytes read back from the boxes matched the input, and all 848 whole-subresource bytes matched each reference, including the initialized regions outside the boxes.

Both focused runs passed with zero skips or Vulkan core or synchronization validation messages. Only the temporary fixture changed to add the BC7 cases and compare bytes directly. Synthetic inputs only. AI-assisted.

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

AI-assisted review and test.

Independent Windows MSVC + Vulkan validation verification of caa1b6be3 on Windows 11 x64 (es-ES), MSVC 19.44.35230.0 (RelWithDebInfo, -DLEONIDA_REQUIRE_VULKAN=ON), NVIDIA GeForce RTX 3060 (driver 621.24, Vulkan SDK 1.4.363.0 with LEONIDA_VK_VALIDATION=1), no game files used: pass.

  • Merge & hygiene: Merges cleanly with upstream/main (867d922f0, git merge-tree exit 0); python -I scripts/check_no_game_data.py clean; python -I tests/test_ctest_registration.py 4/4 OK.
  • Tests (LEONIDA_VK_VALIDATION=1):
    • d3d12_subresource_probe_test vs NVIDIA GeForce RTX 3060 host: pass, 0 cases differ, 0 Vulkan validation errors.
    • PROBE_WARP=1 d3d12_subresource_probe_test vs WARP: pass, 0 cases differ, 0 Vulkan validation errors.
    • d3d12_subresource_copy_test (host image copy and staging copy paths): pass, 109 steps compared, 0 mismatches on both paths, 0 Vulkan validation errors.
  • Negative control: Reverting runtime/shims/d3d12_vulkan_device.cpp to upstream/main causes d3d12_subresource_probe_test to fail with subresource probe: 3 cases differ (exit 1).
  • Minor non-blocking note: MSVC /W4 emits C4996: 'getenv' at runtime/test/d3d12_subresource_probe_test.cpp:105 (could use GetEnvironmentVariableA or _dupenv_s to keep the target warning-free).

@DaviDreamerz

Copy link
Copy Markdown

Tested caa1b6be3 on Windows 11 (build 26300), MSVC 14.44 RelWithDebInfo, GeForce RTX 4070 Ti SUPER, NVIDIA 621.24: pass.

  • d3d12_subresource_probe: 0 cases differ against the NVIDIA host, passed (1.2 s)
  • PROBE_WARP=1 (WARP as host): 0 cases differ on the same cases (R16 1D 77 x3, 3 mips and RGBA8 2D 13x7, 2 mips both match)
  • Negative control: with only runtime/shims/d3d12_vulkan_device.cpp from 867d922f0 in the same tree, the same test reports 3 cases differ
  • No game files (LEONIDA_GAME_DIR unset); synthetic only

AI-assisted: yes.

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3), AMD Windows driver 32.0.32029.2010, Windows 11, MSVC 14.51 Release, at caa1b6b. Full ctest -j 8, out of 240 tests:

No regression. The only failures are the same 5 as on main dde82f2 on this card (vk_queue_query_sync, d3d12_vulkan_device tiles == 1, d3d12_texture_tiles, d3d12_depth_resolve, d3d12_pipeline_library_host; tracked in #8426). Every test this PR adds or touches passes.

AI-assisted (Claude Code).

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.

7 participants