Search code examples
vulkan

Why do I need srcStageMask in VkSubpassDependency?


I'm learning Vulkan from the tutorial: https://vulkan-tutorial.com/Drawing_a_triangle/Drawing/Rendering_and_presentation

VkSubpassDependency dependency{};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;

dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;

dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT

I don't understand why srcStageMask is needed. I understand that the dstStageMask stages are executed only after the srcStageMask stages.

But I don't understand what VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT means in srcStageMask. Are these the stages that srcSubpass waits for before launching itself? Can you please explain it to me?


Solution

  • Because you want Vulkan to know how early it can start doing its work. The driver is going to create an implicit pipeline barrier between the subpasses, or between this subpass and any other renderpasses, if there are no prior subpasses.

    So the values you place here are basically equivalent to ones you would provide to vkCmdPipelineBarrier2 as part of the VkImageMemoryBarrier2 structure in the VkDependencyInfo parameter.

    Every image and memory barrier needs two scopes, the source and destination scope, as explained here

    In your specific case the tutorial explicitly says

    The next two fields specify the operations to wait on and the stages in which these operations occur. We need to wait for the swap chain to finish reading from the image before we can access it. This can be accomplished by waiting on the color attachment output stage itself.

    So in this case, the srcStageMask is saying that the work being protected here must wait on the VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT of the previous usage of the image, because it's a swap chain image that gets re-used over and over and you don't want to be writing to it while the prior frame that used it might still be reading from it.