I’m moving my renderer from render passes to dynamic rendering and am seeing validation messages on Linux (Nvidia 550.78 to be specific) that I’m not seeing on Windows (AMD 24.3.1 to be specific). Here are a few lines that I’ve formatted for legibility:
[17:01:00.273][8314]: Swapchain image 0xcad092000000000d: 'ColorAttachmentOptimal'->'PresentSrcKHR'
[17:01:00.273][8314]:
Validation Error: [ SYNC-HAZARD-WRITE-AFTER-READ ]
Object 0: handle = 0x555555b41130, type = VK_OBJECT_TYPE_QUEUE; |
MessageID = 0x376bc9df |
vkQueueSubmit():
Hazard WRITE_AFTER_READ for entry 0,
VkCommandBuffer 0x5555582fad00[],
Submitted access info (submitted_usage: SYNC_IMAGE_LAYOUT_TRANSITION,
command: vkCmdPipelineBarrier,
seq_no: 1,
VkImage 0xcad092000000000d[],
reset_no: 30).
Access info (prior_usage: SYNC_PRESENT_ENGINE_SYNCVAL_PRESENT_ACQUIRE_READ_SYNCVAL,
read_barriers: VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT|VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT,
,
batch_tag: 677,
vkAcquireNextImageKHR aquire_tag:677
: VkSwapchainKHR 0xe88693000000000c[],
image_index: 0
image: VkImage 0xcad092000000000d[]).
[17:01:00.273][8314]:
Validation Error: [ SYNC-HAZARD-WRITE-AFTER-WRITE ]
Object 0: handle = 0x555555b41130, type = VK_OBJECT_TYPE_QUEUE; |
MessageID = 0x5c0ec5d6 |
vkQueueSubmit():
Hazard WRITE_AFTER_WRITE for entry 0,
VkCommandBuffer 0x5555582fad00[],
Submitted access info (submitted_usage: SYNC_IMAGE_LAYOUT_TRANSITION,
command: vkCmdPipelineBarrier,
seq_no: 2,
VkImage 0x2e2941000000001f[],
reset_no: 30).
Access info (prior_usage: SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
write_barriers: 0,
queue: VkQueue 0x555555b41130[],
submit: 88,
batch: 0,
batch_tag: 663,
command: vkCmdEndRenderingKHR,
command_buffer: VkCommandBuffer 0x555558313000[],
seq_no: 11,
reset_no: 28).
[17:01:00.289][8314]: Swapchain image 0x967dd1000000000e: 'Undefined'->'ColorAttachmentOptimal'
[17:01:00.289][8314]: Image 0x2e2941000000001f: 'Undefined'->'DepthAttachmentOptimal'
The first and last two lines are my own logging, while the “middle two lines” are broken out validation messages. It looks like there’s some formatting nonsense going on, but I preserved the missing information and was as consistent as I could be with my formatting.
Anyway, after I submit the command buffer that has the transition pipeline barrier in it, I get a write-after-read error. Here’s the code that actually controls when the barriers are created. Just before I call beginRenderingKHR()
, I transition the swapchain image and depth buffers:
auto &color_buffer = *Renderer::swapchain().images()[Renderer::image_index()];
color_buffer.transition_layout(Renderer::cmd_buffer(),
vk::ImageLayout::eUndefined,
vk::ImageLayout::eColorAttachmentOptimal);
_depth_buffer.transition_layout(Renderer::cmd_buffer(),
vk::ImageLayout::eUndefined,
vk::ImageLayout::eDepthAttachmentOptimal);
Then, just after calling endRenderingKHR()
I transition the color buffer:
auto &color_buffer = *Renderer::swapchain().images()[Renderer::image_index()];
color_buffer.transition_layout(Renderer::cmd_buffer(),
vk::ImageLayout::eColorAttachmentOptimal,
vk::ImageLayout::ePresentSrcKHR);
And here’s the image transition code (vkSwapchainImage
doesn’t apply to the depth buffer, but the logic is the same):
void vkSwapchainImage::transition_layout(vkCmdBuffer const &cmd_buffer,
vk::ImageLayout const old_layout,
vk::ImageLayout const new_layout)
{
BTX_TRACE("Swapchain image {}: '{:s}'->'{:s}'",
_handle,
vk::to_string(old_layout),
vk::to_string(new_layout));
vk::ImageMemoryBarrier barrier {
.srcAccessMask = { },
.dstAccessMask = { },
.oldLayout = old_layout,
.newLayout = new_layout,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = _handle,
.subresourceRange {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0u,
.levelCount = 1u,
.baseArrayLayer = 0u,
.layerCount = 1u,
}
};
vk::PipelineStageFlags src_stage = vk::PipelineStageFlagBits::eNone;
vk::PipelineStageFlags dst_stage = vk::PipelineStageFlagBits::eNone;
if(old_layout == vk::ImageLayout::eUndefined) {
barrier.srcAccessMask = vk::AccessFlagBits::eNone;
if(new_layout == vk::ImageLayout::eColorAttachmentOptimal) {
barrier.dstAccessMask = vk::AccessFlagBits::eColorAttachmentRead
| vk::AccessFlagBits::eColorAttachmentWrite;
src_stage = vk::PipelineStageFlagBits::eTopOfPipe;
dst_stage = vk::PipelineStageFlagBits::eColorAttachmentOutput;
}
else if(new_layout == vk::ImageLayout::eDepthAttachmentOptimal) {
barrier.dstAccessMask =
vk::AccessFlagBits::eDepthStencilAttachmentRead
| vk::AccessFlagBits::eDepthStencilAttachmentWrite;
src_stage = vk::PipelineStageFlagBits::eTopOfPipe;
dst_stage = vk::PipelineStageFlagBits::eEarlyFragmentTests
| vk::PipelineStageFlagBits::eLateFragmentTests;
}
else {
BTX_CRITICAL("Unsupported image layout transition");
return;
}
}
else if(old_layout == vk::ImageLayout::eColorAttachmentOptimal) {
barrier.srcAccessMask = vk::AccessFlagBits::eColorAttachmentRead
| vk::AccessFlagBits::eColorAttachmentWrite;
barrier.dstAccessMask = vk::AccessFlagBits::eNone;
src_stage = vk::PipelineStageFlagBits::eColorAttachmentOutput;
dst_stage = vk::PipelineStageFlagBits::eBottomOfPipe;
}
else {
BTX_CRITICAL("Unsupported image layout transition");
return;
}
cmd_buffer.native().pipelineBarrier(
src_stage, // Source stage
dst_stage, // Destination stage
{ }, // Dependency flags
nullptr, // Memory barriers
nullptr, // Buffer memory barriers
{ barrier } // Image memory barriers
);
_layout = barrier.newLayout;
}
I made an effort to copy the image transitions exactly from the official dynamic rendering example code, and they seem fine on Windows/AMD, but of course I’d like to understand either what’s incomplete or what’s wrong.
And of course any structural improvements for handling image transitions better would be very welcome. =)