I’m having trouble to get this synchronization right.
First clear an image then I use it in a copy as a source, then as an attachment to draw to (with external subpass dependency)
vkCmdClearColorImage(..)
VkImageMemoryBarrier2 memoryBarrier = {
...
.srcStageMask = VK_PIPELINE_STAGE_2_CLEAR_BIT,
.srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
.dstStageMask = VK_PIPELINE_STAGE_2_COPY_BIT,
.dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT,
.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL };
vkCmdCopyImage(..) // as a source
VkAttachmentDescription2 attachement_info = {
...
.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE
.initialLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
}
VkMemoryBarrier2 subpass_memoryBarrier = {
...
.srcStageMask = VK_PIPELINE_STAGE_2_COPY_BIT,
.srcAccessMask = 0,
.dstStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT
.dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT
}
VkSubpassDependency2 info = {
...
.pNext = &subpass_memoryBarrier,
.srcSubpass = VK_SUBPASS_EXTERNAL,
.dstSubpass = 0,
.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT
};
vkCmdBeginRenderPass(..) // sync validation error when trying to record this command:
Validation Error: [ SYNC-HAZARD-READ-AFTER-WRITE ]
Object 0: handle = 0x4d6d9a000000010c, type = VK_OBJECT_TYPE_RENDER_PASS; | MessageID = 0xe4d96472 | vkCmdBeginRenderPass():
Hazard READ_AFTER_WRITE vs. layout transition in subpass 0 for attachment 0 aspect color during load with loadOp VK_ATTACHMENT_LOAD_OP_LOAD.