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

Closed
github-bot wants to merge 1 commit from gh-pr/354/head into gh-pr/354/base
Owner

@⁠natterman12 on GitHub · 2026-11-24 03:50 UTC · original
vulkan/order-back-to-back-buffer-copies → main · +47 −4 · 3 files · 1 commits · closed without merging

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 d478a92d 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 d478a92d) 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

**@⁠natterman12** on GitHub · 2026-11-24 03:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/354) `vulkan/order-back-to-back-buffer-copies` → `main` · +47 −4 · 3 files · 1 commits · closed without merging 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` d478a92d 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` d478a92d) | 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 - [x] 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](https://claude.com/claude-code)
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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:55:06 +00:00
github-bot closed this pull request 2026-11-26 08:55:06 +00:00
Author
Owner

@⁠Sigtryggur-Mani-Gudmundsson on GitHub · 2026-11-24 04:05 UTC · original

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.

**@⁠Sigtryggur-Mani-Gudmundsson** on GitHub · 2026-11-24 04:05 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/354#issuecomment-7121703185) 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](https://git.gtahub.st/github/ubiquitarianism--leonida/pulls/314). 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](https://github.com/Sigtryggur-Mani-Gudmundsson/graphics-runtime-patches/blob/92c7fb79a968d235756a8985352a0e7b5577c7f6/tests/copy-queue-sync/README.md).

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.