5

In Vulkan, suppose I have two render passes:

  1. the first outputs to an image as its color attachment.
  2. the second samples from that same image from its fragment shaders (ie through a combined image sampler).

The render passes are submitted in that order.

What's the correct way to synchronize these two render passes so the second doesn't read before the first has written?

It is necessary and/or sufficient to create an image pipeline barrier on the image in between them?

Jesse Hall
  • 6,441
  • 23
  • 29
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319

1 Answers1

7

It is necessary and sufficient to have an image memory barrier between them:

  1. Execution barrier: You need to ensure the second renderpass fragment shaders (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) don't start executing before all of the first renderpass color attachment writes (VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT) complete.

  2. Memory barrier: You need to ensure the first renderpass color attachment writes (VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT) are available and visible to the second renderpass image sampling instructions (VK_ACCESS_SHADER_READ_BIT) .

  3. Layout transition: You need to change the layout of the image from VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL.

All of those can be done in a vkCmdPipelineBarrier with a single VkImageMemoryBarrier. Or you can use a VkSubpassDependency on one of the two renderpasses (with dstSubpass or srcSubpass set to VK_SUBPASS_EXTERNAL) and the attachment's oldLayout and newLayout fields.

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
Jesse Hall
  • 6,441
  • 23
  • 29