Skip to content

Runtime: stage block-compressed copies one vkCmdCopyImage cannot express - #860

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
adonizm:d3d12-bc-staging-copy
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
adonizm:d3d12-bc-staging-copy

Conversation

@adonizm

@adonizm adonizm commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Item: new D3D item, claimed in #634 (no ID assigned yet). Rebased onto main at 4947579 (round 39); no Depends on.

What

CopyTextureRegion between two block-compressed textures copies whole blocks on the PC runtime. When a mip edge falls inside a block on one side only (a 4x4 box into a 3x3 mip, a 6x6 mip into a 12x12 one), no single vkCmdCopyImage extent is a block multiple or reaches the edge of both images, so the layer refused the copy with invalid_copy. That was the known gap in docs/d3d12_vulkan.md §3.2, and the 8 BC mixed-mip cases d3d12_copy_compare printed as known differences.

Such a copy now goes through the transfer scratch that depth <-> colour copies already use (vulkan_scratch.h, ReplayFunctions::copy_scratch, Prepared::staged): vkCmdCopyImageToBuffer with the source's own cut extent into whole blocks, then vkCmdCopyBufferToImage with the destination's. The box fits the source's padded mip, so the scratch holds every block the destination reads.

  • prepare_texture_copy describes the two regions (staged_reads/staged_writes); prepare takes the scratch. Without copy_scratch the copy is refused as before.
  • No new emit code: the existing staged path is used, and it calls the destination's copy_layout after the scratch write, so every earlier transfer write is ordered before the destination's. That covers the write-after-write @Vonsek found in the first version.
  • Copies that already worked (one extent fits both images) take the same vkCmdCopyImage path as before.
  • Compared with the first version (one vkAllocateMemory per staged copy, owned by the queue), this uses the shared, reused scratch chunks, which also answers @Parakeet-Wheat's allocation-count point.

Tests

  • d3d12_copy_compare: the 8 former known differences (4 shapes, BC1 and BC7) must now match the host byte for byte, plus two more shapes: the box 4..8 of a 6x6 mip into the 3x3 mip, and the 3x3 mip into the 6x6 mip at (4, 4). The three shapes that don't fit are still refused by both sides.
  • d3d12_state_map_test: the staged regions on the CPU (extents, offsets, row length), a copy that stays direct, and refusals that staging doesn't change.

Measured at cde85e2, Windows 11, MSVC 14.50, driver 621.24:

Vulkan device (LEONIDA_VK_DEVICE) Host D3D12 adapter Result
NVIDIA GeForce RTX 5070 Ti RTX 5070 Ti ctest 107/107 apart from xbox_present (below); copy comparison 0 of 75 differ, 18/18 BC mixed-mip matches
NVIDIA GeForce RTX 3060 RTX 3060 d3d12_vulkan_device_test exit 0, 0 of 75 differ
AMD Radeon(TM) Graphics iGPU (0x1002:0x164e, Vulkan driver 2.0.353) the same iGPU d3d12_vulkan_device_test exit 0, 0 of 75 differ

xbox_present fails on this machine's real-time pacing check (30 frames at 60 Hz in about 7.3 s, limit 5 s). It fails the same way on plain main 4947579 here, so it isn't this PR.

Not run here: LEONIDA_VK_VALIDATION=1 (no Vulkan SDK on this machine). @Vonsek, @Parakeet-Wheat and @quackottu validated the first version (fc60aa6); this head is a rework onto main's scratch path, so a validation run at cde85e2 would be welcome.

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

🤖 Generated with Claude Code

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

The requested validation run found two synchronization errors at e7ce5659adde5b540392c09ddf004162e7c6dd8a. On RTX 5080 the stock d3d12_vulkan_device_test matched all 75 native copy cases, then exited 1 with two vkCmdCopyBufferToImage(): WRITE_AFTER_WRITE reports. I used LEONIDA_VK_VALIDATION=1 with Khronos 1.4.350.

I ran the individual mixed-mip cases on both RTX 5080 and Radeon Graphics. Each adapter reported one error for BC1 and one for BC7 on the same-resource copy mip 3 (3×3) → mip 2 (6×6), destination (4,4). The reported destination extent was 2×2. The mip 0 box 4×4 → mip 3 control produced zero errors for both formats. The unmodified Radeon stock executable stops earlier at its OPTIONS19 comparison, so the Radeon results are from the isolated cases.

A one-line local trial adding VK_ACCESS_TRANSFER_WRITE_BIT alongside VK_ACCESS_TRANSFER_READ_BIT in the destination access mask of the barrier between the two staged commands reduced those two errors to zero on each adapter. The full RTX stock executable then exited 0, still with 0/75 copy differences. The PR branch was unchanged.

I also tested whether the positions outside a 3×3 logical mip survive a round trip. Two BC1 copies in one command list took a patterned 4×4 block from an 8×8 image into a second texture's 3×3 mip, then copied it back to (4,4) of the first image. The public head's Vulkan path on both GPUs and native D3D12 on RTX, Radeon and WARP matched all 64 expected RGBA texels, including all seven positions in the block's last row or column. Omitting only the return copy changed precisely 16/64 texels, including those seven, on every path.

AI-assisted independent verification with source-built fixtures.

@Parakeet-Wheat

Copy link
Copy Markdown
Contributor

Tested e7ce565 on an NVIDIA GeForce RTX 3070, driver 610.88, Windows 11 (26300), MSVC 19.44, Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1: same result as @Vonsek, found independently.

  • Build passes, ctest 26/26. d3d12_state_map_test, d3d12_com_test and vk_command_replay_test (A to F) pass with no validation messages.
  • d3d12_vulkan_device_test: 0 of 75 copy cases differ from the host (the 8 former known differences and the 2 new shapes all match), then exit 1 on the same two WRITE_AFTER_WRITE reports: mip 3 (3×3) into mip 2 at (4,4), BC1 and BC7.
  • With the same one-line change, VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT as the destination access of the barrier between vkCmdCopyImageToBuffer and vkCmdCopyBufferToImage (vulkan_replay.h:1738), the test exits 0 with 0 hazards and still 0 of 75 differences. That's the fix confirmed on a third GPU.

A minor point, not blocking: each staged copy gets its own vkAllocateMemory. That's fine for these rare edge cases, but drivers cap the allocation count (maxMemoryAllocationCount, often 4096), so a shared staging pool would be worth it if a game ever stages many copies per frame.

AI-assisted (Claude Code).

adonizm added a commit to adonizm/Leonida that referenced this pull request Nov 22, 2026
…tion write

The barrier between the two halves of a staged block-compressed copy only
made the buffer's write visible to its read. Synchronization validation
reported a write-after-write on vkCmdCopyBufferToImage for the 3x3 mip into
the 6x6 mip at (4, 4), BC1 and BC7, on an RTX 5080 and a Radeon iGPU. With
TRANSFER_WRITE on the destination side the second command is ordered after
every earlier transfer write, and the reports go away on both GPUs. Found and
checked by @Vonsek on ItsNotRexed#860.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adonizm

adonizm commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Vonsek, that's exactly the validation run I couldn't do here (no Vulkan SDK on this machine).

Taken as you tested it: fc60aa6 adds VK_ACCESS_TRANSFER_WRITE_BIT to the destination side of the barrier between vkCmdCopyImageToBuffer and vkCmdCopyBufferToImage, so the destination write is ordered after every earlier transfer write and not only after the staging buffer's own write. The comment there credits your report.

Re-run at fc60aa6 without the validation layer (I still can't run it): ctest 32/32 and d3d12_vulkan_device_test exit 0 with 0/75 copy differences (18/18 BC mixed-mip matches), on an RTX 5070 Ti against its own host and on an RTX 3060 against its own host (driver 621.24, Windows 11). Your fixture showing the positions outside the 3x3 mip survive the round trip is good to have too. Thanks for that.

A validation run at fc60aa6 on either of your GPUs would confirm it from the real head.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Confirmed at the published fc60aa6ddfd57672ce5b0857712f85b60f1cbb53 with Khronos synchronization validation 1.4.350. For the same-resource BC1 and BC7 mip 3 → mip 2 copies at (4,4), the previous two WRITE_AFTER_WRITE reports fell to zero on both RTX 5080 and Radeon Graphics. The 4×4 → 3×3 comparison also passed on each, and the sampled BC1 round trip still matched 64/64 RGBA texels. Omitting the return copy still changed exactly 16, including the seven positions outside the intermediate mip's logical 3×3 footprint.

The full unmodified RTX d3d12_vulkan_device_test exited 0 with 0/75 native differences. The unmodified Radeon executable stopped before the copy cases at the existing OPTIONS19 sample-count comparison (0xf vs 0x1d), so I ran those copy cases separately there. AI-assisted retest, synthetic inputs only.

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

Tested fc60aa6 on NVIDIA GeForce RTX 5070, driver 621.24, Windows 11 Pro (26300), VS 2022 Build Tools, Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1 and LEONIDA_D3D12=vulkan: pass

  • ctest -C RelWithDebInfo: 100% tests passed, 0 tests failed out of 32
  • d3d12_state_map_test (changed here): exit=0 validation=0 | state map, enhanced barriers, subresource decoding, layout tracking and BAR detection OK
  • 33 test programs in the build pass with 0 validation errors
  • d3d12_clear_test fails (R8G8B8A8_UNORM/SNORM 0.5 ties and a D16 clear byte differ from WARP) exactly as on main 4a36bb1 here, so not from this PR; #521 addresses it

Implicit Vulkan layers (overlays) were off through an empty VK_IMPLICIT_LAYER_PATH.

CopyTextureRegion between two block-compressed textures copies whole blocks
on the PC runtime. When a mip edge falls inside a block on one side only (a
4x4 box into a 3x3 mip, a 6x6 mip into a 12x12 one), no single Vulkan extent
is a block multiple or reaches the edge of both images, so the layer refused
the copy with invalid_copy (docs/d3d12_vulkan.md, section 3.2 known gaps).

Such a copy now goes through the transfer scratch that depth <-> colour
copies already use (vulkan_scratch.h): vkCmdCopyImageToBuffer with the
source's own cut extent into whole blocks, then vkCmdCopyBufferToImage with
the destination's. The box fits the source's padded mip, so the scratch
holds every block the destination reads. The staged emit path calls the
destination's copy_layout after the scratch write, so every earlier transfer
write is ordered before the destination's (the write-after-write @Vonsek
found under synchronization validation in the first version of ItsNotRexed#860).

- d3d12_copy_compare: the 8 BC mixed-mip cases that were printed as known
  differences must now match the host; two more shapes (a source edge inside
  the block, a destination offset to the edge).
- d3d12_state_map_test checks the staged regions on the CPU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adonizm
adonizm force-pushed the d3d12-bc-staging-copy branch from fc60aa6 to cde85e2 Compare November 22, 2026 17:32
@adonizm

adonizm commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto round 39 (main 4947579) and reworked, now at cde85e2 (force-pushed; fc60aa6 is the old head).

main now has transfer scratch for staged depth <-> colour copies (vulkan_scratch.h, copy_scratch, Prepared::staged), so the BC case uses that instead of my queue-owned buffers:

  • no new emit code: the existing staged path calls the destination's copy_layout after the scratch write, so earlier transfer writes are ordered before the destination's. That's the ordering @Vonsek's one-line fix gave;
  • shared, reused scratch chunks instead of one vkAllocateMemory per copy, which covers @Parakeet-Wheat's allocation-count point.

Same tests: 0 of 75 copy differences (18/18 BC mixed-mip matches) on an RTX 5070 Ti, an RTX 3060 and an AMD Radeon iGPU, each against its own host. Since the code changed, the earlier validation results apply to fc60aa6, not to this head. A LEONIDA_VK_VALIDATION=1 run at cde85e2 would be appreciated (I still can't run it here). Thanks all for the testing so far.

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.

5 participants