Skip to content

Runtime: order repeated Vulkan buffer transfer copies - #4475

Open
pmilewsskiedu-gif wants to merge 1 commit into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/d3d12-buffer-copy-waw-20261122
Open

pmilewsskiedu-gif wants to merge 1 commit into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/d3d12-buffer-copy-waw-20261122

Conversation

@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor

Claimed before production edits on coordination #4421: #4421 (comment)

Production correctness

On main 03b09a086592a197db64d91655c6e65c30764d96, Vulkan replay directly emits vkCmdCopyBuffer for successive D3D12 CopyBufferRegion / resource-buffer copies to the same destination without a transfer write dependency. Unlike copy_layout's repeated image copy path, overlapping Vulkan transfer writes remain unordered for synchronization validation despite D3D12 allowing multiple writes while the buffer remains COPY_DEST. This patch inserts a transfer WRITE->READ|WRITE pipeline memory dependency before the buffer-copy execution branch, covering prior transfer-produced sources as well as same-destination writes. No resource lifecycle, API recording, shader or aliasing changes.

Original-main RED / patched GREEN, identical test

A new Part J of runtime/test/vk_command_replay_test.cpp issues two synthetic 4096-byte buffer copies to identical destination offsets (0x11 first, 0x22 last) in one real Leonida command list, replays using Vulkan backend on actual RTX 4060 Ti with LEONIDA_VK_VALIDATION=1, reads back all final bytes and requires zero synchronization VUIDs. Detached original-main test-only worktree LeonidaBufferCopyWawBaseline20261122 retains unchanged production source at 03b09a08; SHA-256 of regression source in baseline equals patched 79661FF7E2B2A0DDE7B6F12896114F19BD2845EF870AC66023FBB05E8A7BF30E.

  • Baseline red: vkCmdCopyBuffer(): WRITE_AFTER_WRITE hazard, 1 sync validation error in Part J, executable exit 1; preceding Parts A–I remain green.
  • Patched green: Part J zero validation errors; readback verifies every output byte 0x22; executable exit 0. Explicit VkMemoryBarrier also synchronizes final GPU→host readback.
  • Native D3D12 WARP reference: a standalone synthetic d3d12_buffer_copy_order_test makes two unbarriered CopyBufferRegion writes to a DEFAULT/COPY_DEST buffer, transitions only before readback, and checks 4096 bytes of 0x22; native WARP Close/queue/fence/readback pass, 0 mismatches, exit 0.
  • MSVC 19.44 + Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0, RTX 4060 Ti validation enabled: six focused CTests command_list, vk_command_replay, vk_predicated_copy, vk_sync_validation, d3d12_buffer_copy_order, replay_copy_offset 6/6 PASS (3.06s). vk_command_replay and native WARP d3d12_buffer_copy_order each 10/10 consecutive repetitions PASS (2.61s combined). git diff --check clean.

Limits / checklist

  • Original production source and synthetic test fixtures only, no game assets, keys or decryption.
  • AI-assisted implementation, validation and review disclosed.

Raw Vulkan sync VUID proves the identified hazard; no game executable, authentic boot, framerate or proprietary resources tested. No full CTest suite or non-NVIDIA Vulkan device recheck yet. Separate from #4253 footprint-buffer support, #655/#770 placed-buffer ownership and earlier copy PRs. Base 03b09a08, head commit 4757ab515b12a4ffbec2a1e6fd83f375d1d3ed95. Please review, do not self-merge.

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

Built d3d12_buffer_copy_order_test and vk_command_replay_test at 4757ab51 and ran them on Windows 11: d3d12_buffer_copy_order_test -> exit 0, "native WARP: consecutive overlapping CopyBufferRegion writes, 0 byte mismatches"; ctest -R 'buffer_copy_order|vk_command_replay' -> 2/2 passed. No blocking issue.

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

Reviewed 4757ab5: prepare stores one buffer-copy region on the corresponding command and emit inserts the transfer write-to-read/write dependency before each CopyBufferRegion replay. The synthetic replay checks the later overlapping write wins with zero validation errors; native WARP confirms D3D12 ordering.

@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested 4757ab5 on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). ctest -R "command_list|vk_command_replay|vk_predicated_copy|vk_sync_validation|d3d12_buffer_copy_order|replay_copy_offset": 6 tests per GPU, 5 pass, 1 skipped (vk_sync_validation: LEONIDA_VK_VALIDATION not set), 0 failed. No Vulkan validation layer is installed on this machine, so the WRITE_AFTER_WRITE hazard that Part J targets could not be checked here; this run shows the new copy ordering does not break the replay and readback checks (Part J's byte readback passes). No game files.

AI-assisted.

@mrbean56

Copy link
Copy Markdown

Tested 4757ab515b on AMD Radeon RX 7800 XT (RADV NAVI32, Mesa 26.x, Vulkan 1.4.309), Linux, with VK_LAYER_KHRONOS_validation 1.4.309 installed: pass, and the negative control is red.

  • Built runtime/test/vk_command_replay_test.cpp (cmake -G Ninja -DCMAKE_BUILD_TYPE=Release, GCC) and ran it with LEONIDA_VK_VALIDATION=1 + VK_DRIVER_FILES=radeon_icd.json. All parts A–J pass; part J reports validation errors=0.
  • Negative control, same test binary source: restored main's runtime/src/vulkan_replay.h (03b09a08), rebuilt, reran. Part J reports validation errors=1; A–I are unchanged. So the new dependency is what the validation layer was missing, and the test measures it.
  • Placement check: each CopyBuffer region is its own prepared entry (p.buffer_copies.push_back in the per-command branch), so two CopyBufferRegion calls become two vkCmdCopyBuffers with this barrier between them; it is exactly the write→read|write transfer dependency D3D12's implicit ordering needs. The WriteBufferImmediate and AtomicCopyBuffer branches already carry their own full barriers.

One note, not a blocker: the barrier is emitted before every buffer-copy batch, including the first, and it is a global VkMemoryBarrier rather than a buffer-range one. Correct, but on a list with many small copies it is a transfer-stage stall each time; narrowing it to the ranges (or only when a prior copy touched the destination / produced the source) would be a later optimization.

No game data. AI-assisted.

@akimfomin

Copy link
Copy Markdown

Separate timing check for disjoint copies, where the new dependency is also emitted. Windows 11 28000.3501, RTX 4070 Ti SUPER / 617.14, 03b09a08 vs 4757ab51. Same portable probe against each revision of vulkan_replay.h, real replay CommandList and Vulkan timestamps around the copies. Each batch copied 16 MiB from a host-visible upload to a device-local buffer. Four interleaved process pairs, nine timed batches per size and process after warmup. Each process verified the entire readback after the timed region.

Copy regions per batch Main median GPU time PR median GPU time
16 1.270 ms 1.265 ms
1,024 1.191 ms 1.206 ms

All readbacks passed. The differences were small relative to run-to-run variation, so this check found no clear slowdown for this upload workload. It covers disjoint transfers; the overlap validation is covered by the other reports. No Vulkan validation layer in this timing run. Synthetic bytes only. AI-assisted (Codex).

@memorypressure

Copy link
Copy Markdown

FAIL to merge, verified: git merge-tree conflicts in runtime/CMakeLists.txt and runtime/test/vk_command_replay_test.cpp from main's round-43 churn, so a rebase is needed. The substance holds: the fix hunk in vulkan_replay.h auto-merges clean, and on current main the buffer-copy emit path really does lack the transfer-to-transfer barrier while the analogous UAV-clear path beside it already has one — the gap is real, so this wants a rebase rather than closing. The C++/WARP side cannot be built or run here, so the ordering fix stands with the pending checks and the Windows test. Checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only review).

@Kiril-Donec

Copy link
Copy Markdown

Tested 4757ab515 on Windows 11, NVIDIA GeForce RTX 4060 (driver 610.88), MSVC 14.51 (VS 2026), Vulkan SDK 1.4.363.0, LEONIDA_VK_VALIDATION=1: pass. vk_queue_test exits 0 with 0 validation messages. Control on main 0c6ff79: 10 WRITE_AFTER_WRITE hazards between vkCmdCopyBuffer submits, line 394: d.validation_errors() == 0, exit 1. This is the vk_queue_test part of TEST-1698; #5997 also makes it pass here. 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.

8 participants