Skip to content

vulkan_replay: order back-to-back buffer copies, as image copies already are (VK-BUFCOPY-ORDER) - #354

Closed
natterman12 wants to merge 1 commit into
ProjectLeonida:mainfrom
natterman12:vulkan/order-back-to-back-buffer-copies
Closed

natterman12 wants to merge 1 commit into
ProjectLeonida:mainfrom
natterman12:vulkan/order-back-to-back-buffer-copies

Conversation

@natterman12

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: VK-BUFCOPY-ORDER (#14)

What and why

D3D12 runs consecutive copies on a buffer in order with no barrier from the application: the buffer stays in COPY_DEST / COPY_SOURCE, and that holds within one list and across lists on a queue. Vulkan orders nothing unless a barrier says so.

The replay already handles this for images. copy_layout says: "D3D12 orders back-to-back copies and resolves on a resource that stays in COPY_DEST or COPY_SOURCE without a barrier from the application; Vulkan does not", and emits a memory barrier after earlier transfer writes. The plain buffer-to-buffer copy had no such barrier, so two copies into the same buffer are unordered as far as Vulkan is concerned.

Synchronization validation reports it. On main d478a92 with LEONIDA_VK_VALIDATION=1, vk_queue fails with ten of these from its backlog case (the same 16 MiB copy submitted repeatedly):

vkQueueSubmit(): pSubmits[0].pCommandBuffers[0] WRITE_AFTER_WRITE hazard detected. vkCmdCopyBuffer ... writes to
VkBuffer ..., which was previously written by another vkCmdCopyBuffer command ...

The change is one barrier ahead of a plain buffer copy: earlier TRANSFER_WRITE at the transfer stage, to this copy's TRANSFER_READ | TRANSFER_WRITE at the transfer stage, through the existing detail::memory_barrier (so it is restricted to the queue's stages like the others). It covers the read side too: a copy that reads what the copy before it wrote.

Cost: one transfer-to-transfer memory barrier per buffer copy, the same price image copies already pay.

Tests

  • replay_copy_offset_test (GPU-free, fake Vulkan): its barrier callback now records whether a transfer-write barrier came before each vkCmdCopyBuffer. It checks that for a single CopyResource, and for two overlapping CopyBufferRegion calls into the same buffer, where each copy needs its own barrier. Both checks fail without the change.
  • replay_query_edges_test: its fake Vulkan table set copy_buffer but no pipeline_barrier, so it crashed on the new call. It gets a no-op entry; nothing it asserts changes.

Tested

Windows 11 10.0.26300.9974, NVIDIA GeForce RTX 3080 Ti, driver 621.24, Vulkan SDK 1.4.363.0 (validation layer from the SDK), MSVC 19.44.35230. No game files. Everything recompiled for the "after" numbers.

before (main d478a92) after
vk_queue, LEONIDA_VK_VALIDATION=1 failed, 10 WRITE_AFTER_WRITE validation errors: 0, vk_queue_test: OK
replay_copy_offset passes (did not check ordering) passes with the two new checks
Full ctest, LEONIDA_VK_VALIDATION=1 6 failed of 290 5 failed of 290

The five that remain fail the same way without this change: d3d12_vulkan_device (#262 / #238), d3d12_execute_indirect_validation (#337 / #306), d3d12_depth_resolve (#66), and two that only fail under validation and that I am reporting separately in #14 rather than fixing here: d3d12_clear (VUID-VkClearDepthStencilValue-depth-00022) and d3d12_mesh (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076).

Not tested: other GPUs, Linux, and the full suite without validation on this branch (the validation run executes the same tests).

AI-assisted: yes (Claude Code)

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. (not applicable: no translator change)

🤖 Generated with Claude Code

…ady are

D3D12 runs consecutive copies on a buffer that stays in COPY_DEST or
COPY_SOURCE in order, with no barrier from the application, in one list or
across lists on a queue. Vulkan orders nothing without a barrier. The
replay emitted that barrier for image copies (copy_layout) but not for a
plain buffer-to-buffer copy, so two copies into the same buffer raced as
far as Vulkan is concerned: synchronization validation reports
WRITE_AFTER_WRITE on vk_queue's backlog case, ten times on an RTX 3080 Ti.

A plain buffer copy now gets a memory barrier from earlier transfer writes
to its transfer reads and writes.

replay_copy_offset checks the barrier ahead of one copy and ahead of each
of two back-to-back copies into the same buffer. replay_query_edges' fake
Vulkan table gains the barrier entry point the replay now calls.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Sigtryggur-Mani-Gudmundsson

Copy link
Copy Markdown

Independent verification on Linux with an RTX 4070 Ti (620.70.06). I reproduced the ten write-after-write errors in the 256-submit backlog case. With natterman12's buffer-copy barrier applied, that case passed. On transfer-only family 1, a copy followed by vkCmdUpdateBuffer or vkCmdFillBuffer also needed the copy-queue stage-filter correction in the PR 314 report.

I added a preparation check for the new barrier callback so a plain buffer copy reports unsupported when the callback is missing. Seven focused tests passed after applying both series to a clean base. In the full integrated Linux suite, 71 tests passed and the unconfigured shader-corpus test skipped. Patch and reproduction commands.

@natterman12 natterman12 closed this by deleting the head repository Nov 24, 2026
QemmHD pushed a commit that referenced this pull request Nov 25, 2026
…-TRANSFER-BARRIER)

Vulkan transfer writes can overlap a later read or write when D3D12 submits
plain buffer copies in sequence.

Emit the transfer memory dependency from natterman12's #354 (GPL-3.0) before
every plain buffer copy. Allow an explicit rollback for comparison, and
record the access masks in the synthetic replay fixture.

The base failed both copy-ordering assertions. The child passed both replay
fixtures, and the rollback control observed no ordering barrier.


AI-assisted (Codex and Claude Code agents).

Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

2 participants