Search code examples
c++renderrenderervulkan

Describing attachments for multipass rendering


I am trying to create a deferred renderer in vulkan with one renderpass and two sub-passes. The first one should be the geometry pass to fill the G-Buffer and the second one should be the shading pass to apply the lighting. The problem now is that I can't wrap my head around how I should describe the dependencies between the subpasses in the VkRenderPassCreateInfo.

The VkRenderPassCreateInfo has a field:

pAttachments points to an array of attachmentCount number of VkAttachmentDescription structures describing properties of the attachments, or NULL if attachmentCount is zero.

But the VkSubpassDescription has a field:

pColorAttachments is an array of colorAttachmentCount VkAttachmentReference structures that lists which of the render pass’s attachments will be used as color attachments in the subpass, and what layout each attachment will be in during the subpass. Each element of the array corresponds to a fragment shader output location, i.e. if the shader declared an output variable layout(location=X) then it uses the attachment provided in pColorAttachments[X]

So to my understanding the pColorAttachments of the VkSubpassDescription declare the layout of the output of each pass. So in my example, the first subpass would have 4 outputs: 1 position, 1 normal, 1 specular and 1 depth attachment. And the second subpass would have 1 output: the color which is displayed on the screen. If that is true, what is the purpose of the pAttachments field in the VkRenderPassCreateInfo?


Solution

  • Render passes define attachments; subpasses reference them. That's why the struct is called: VkAttachmentReference; it references an attachment defined by the render pass. The render pass defines attachments with an array of VkAttachmentDescription objects. The VkAttachmentReference is just an index into that array.

    There is a hard limit on the number of attachments you can have in a render pass, as well as the number of attachments that an individual subpass can use.