From 5ec780c3e7c279310c667a9dc05ba54e6f1284a8 Mon Sep 17 00:00:00 2001 From: ismailchaka1 Date: Sun, 22 Nov 2026 03:02:26 +0000 Subject: [PATCH] Runtime: order back-to-back copies into an image already in the transfer layout D3D12 orders consecutive copies and resolves on a resource that stays in COPY_DEST or COPY_SOURCE without a barrier from the application; Vulkan needs one. detail::copy_layout emitted a barrier only when the layout changed, so a second copy into an image the first had already left in TRANSFER_DST_OPTIMAL was unordered with it: synchronization validation reported 10 WRITE_AFTER_WRITE hazards in d3d12_vulkan_device_test (@guimsolutions's finding in #634). When a piece needs no transition, copy_layout now adds a memory barrier from earlier transfer writes to the new transfer access (transfer stage only). Copies and resolves both go through it. With VK_LAYER_VALIDATE_SYNC=1: d3d12_vulkan_device_test goes from 10 hazards (exit 1) to 0; the descriptor, method-variants, sampler2 and replay tests report none either. --- runtime/src/vulkan_replay.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 9b470e6b2..38e21a85e 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -1080,10 +1080,25 @@ inline VkImageLayout copy_layout(const ReplayFunctions& vk, VkCommandBuffer comm const VkImageLayout target = image_layout(resource, wanted); auto pieces = current(tracker, id, range, target); initialize(vk, command_buffer, resource, id, pieces, target, tracker); - if (pieces.size() == 1 && (pieces[0].layout == target || pieces[0].layout == VK_IMAGE_LAYOUT_GENERAL)) + // 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. A piece already in the transfer layout gets no layout + // transition, so an earlier transfer write to it is ordered here instead (write-after-write and read-after-write, + // which synchronization validation reports otherwise). + const auto after_transfer_writes = [&] { + memory_barrier(vk, command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, access, + VK_PIPELINE_STAGE_TRANSFER_BIT); + }; + if (pieces.size() == 1 && (pieces[0].layout == target || pieces[0].layout == VK_IMAGE_LAYOUT_GENERAL)) { + after_transfer_writes(); return pieces[0].layout; + } + bool ordered = false; for (const auto& piece : pieces) { - if (piece.layout == target) continue; + if (piece.layout == target) { + if (!ordered) after_transfer_writes(); + ordered = true; + continue; + } image_barrier(vk, command_buffer, resource, piece.range, piece.layout, target, VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, access, VK_PIPELINE_STAGE_TRANSFER_BIT);