7.1. Render Pass Creation

A render pass is created by calling:

 

VkResult vkCreateRenderPass(
    VkDevice                                    device,
    const VkRenderPassCreateInfo*               pCreateInfo,
    const VkAllocationCallbacks*                pAllocator,
    VkRenderPass*                               pRenderPass);

The VkRenderPassCreateInfo structure is defined as:

 

typedef struct VkRenderPassCreateInfo {
    VkStructureType                   sType;
    const void*                       pNext;
    VkRenderPassCreateFlags           flags;
    uint32_t                          attachmentCount;
    const VkAttachmentDescription*    pAttachments;
    uint32_t                          subpassCount;
    const VkSubpassDescription*       pSubpasses;
    uint32_t                          dependencyCount;
    const VkSubpassDependency*        pDependencies;
} VkRenderPassCreateInfo;

The VkAttachmentDescription structure is defined as:

 

typedef struct VkAttachmentDescription {
    VkAttachmentDescriptionFlags    flags;
    VkFormat                        format;
    VkSampleCountFlagBits           samples;
    VkAttachmentLoadOp              loadOp;
    VkAttachmentStoreOp             storeOp;
    VkAttachmentLoadOp              stencilLoadOp;
    VkAttachmentStoreOp             stencilStoreOp;
    VkImageLayout                   initialLayout;
    VkImageLayout                   finalLayout;
} VkAttachmentDescription;

 

typedef enum VkAttachmentDescriptionFlagBits {
    VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT = 0x00000001,
} VkAttachmentDescriptionFlagBits;

If the attachment uses a color format, then loadOp and storeOp are used, and stencilLoadOp and stencilStoreOp are ignored. If the format has depth and/or stencil components, loadOp and storeOp apply only to the depth data, while stencilLoadOp and stencilStoreOp define how the stencil data is handled.

During a render pass instance, input/color attachments with color formats that have a component size of 8, 16, or 32 bits must be represented in the attachment’s format throughout the instance. Attachments with other floating- or fixed-point color formats, or with depth components may be represented in a format with a precision higher than the attachment format, but must be represented with the same range. When such a component is loaded via the loadOp, it will be converted into an implementation-dependent format used by the render pass. Such components must be converted from the render pass format, to the format of the attachment, before they are stored or resolved at the end of a render pass instance via storeOp. Conversions occur as described in Numeric Representation and Computation and Fixed-Point Data Conversions.

If flags includes VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT, then the attachment is treated as if it shares physical memory with another attachment in the same render pass. This information limits the ability of the implementation to reorder certain operations (like layout transitions and the loadOp) such that it is not improperly reordered against other uses of the same physical memory via a different attachment. This is described in more detail below.

If a render pass uses multiple attachments that alias the same device memory, those attachments must each include the VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT bit in their attachment description flags. Attachments aliasing the same memory occurs in multiple ways:

Render passes must include subpass dependencies (either directly or via a subpass dependency chain) between any two subpasses that operate on the same attachment or aliasing attachments and those subpass dependencies must include execution and memory dependencies separating uses of the aliases, if at least one of those subpasses writes to one of the aliases. Those dependencies must not include the VK_DEPENDENCY_BY_REGION_BIT if the aliases are views of distinct image subresources which overlap in memory.

Multiple attachments that alias the same memory must not be used in a single subpass. A given attachment index must not be used multiple times in a single subpass, with one exception: two subpass attachments can use the same attachment index if at least one use is as an input attachment and neither use is as a resolve or preserve attachment. In other words, the same view can be used simultaneously as an input and color or depth/stencil attachment, but must not be used as multiple color or depth/stencil attachments nor as resolve or preserve attachments. This valid scenario is described in more detail below.

If a set of attachments alias each other, then all except the first to be used in the render pass must use an initialLayout of VK_IMAGE_LAYOUT_UNDEFINED, since the earlier uses of the other aliases make their contents undefined. Once an alias has been used and a different alias has been used after it, the first alias must not be used in any later subpasses. However, an application can assign the same image view to multiple aliasing attachment indices, which allows that image view to be used multiple times even if other aliases are used in between. Once an attachment needs the VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT bit, there should be no additional cost of introducing additional aliases, and using these additional aliases may allow more efficient clearing of the attachments on multiple uses via VK_ATTACHMENT_LOAD_OP_CLEAR.

[Note]Note

The exact set of attachment indices that alias with each other is not known until a framebuffer is created using the render pass, so the above conditions cannot be validated at render pass creation time.

The VkSubpassDescription structure is defined as:

 

typedef struct VkSubpassDescription {
    VkSubpassDescriptionFlags       flags;
    VkPipelineBindPoint             pipelineBindPoint;
    uint32_t                        inputAttachmentCount;
    const VkAttachmentReference*    pInputAttachments;
    uint32_t                        colorAttachmentCount;
    const VkAttachmentReference*    pColorAttachments;
    const VkAttachmentReference*    pResolveAttachments;
    const VkAttachmentReference*    pDepthStencilAttachment;
    uint32_t                        preserveAttachmentCount;
    const uint32_t*                 pPreserveAttachments;
} VkSubpassDescription;

The contents of an attachment within the render area become undefined at the start of a subpass S if all of the following conditions are true:

Once the contents of an attachment become undefined in subpass S, they remain undefined for subpasses in subpass dependency chains starting with subpass S until they are written again. However, they remain valid for subpasses in other subpass dependency chains starting with subpass S1 if those subpasses use or preserve the attachment.

The VkAttachmentReference structure is defined as:

 

typedef struct VkAttachmentReference {
    uint32_t         attachment;
    VkImageLayout    layout;
} VkAttachmentReference;

The VkSubpassDependency structure is defined as:

 

typedef struct VkSubpassDependency {
    uint32_t                srcSubpass;
    uint32_t                dstSubpass;
    VkPipelineStageFlags    srcStageMask;
    VkPipelineStageFlags    dstStageMask;
    VkAccessFlags           srcAccessMask;
    VkAccessFlags           dstAccessMask;
    VkDependencyFlags       dependencyFlags;
} VkSubpassDependency;

 

typedef enum VkDependencyFlagBits {
    VK_DEPENDENCY_BY_REGION_BIT = 0x00000001,
} VkDependencyFlagBits;

Each subpass dependency defines an execution and memory dependency between two sets of commands, with the second set depending on the first set. When srcSubpass does not equal dstSubpass then the first set of commands is:

While the corresponding second set of commands is:

When srcSubpass equals dstSubpass then the first set consists of commands in the subpass before a call to vkCmdPipelineBarrier and the second set consists of commands in the subpass following that same call as described in the Subpass Self-dependency section.

The srcStageMask, dstStageMask, srcAccessMask, dstAccessMask, and dependencyFlags parameters of the dependency are interpreted the same way as for other dependencies, as described in Synchronization and Cache Control.

Automatic image layout transitions between subpasses also interact with the subpass dependencies. If two subpasses are connected by a dependency and those two subpasses use the same attachment in a different layout, then the layout transition will occur after the memory accesses via srcAccessMask have completed in all pipeline stages included in srcStageMask in the source subpass, and before any memory accesses via dstAccessMask occur in any pipeline stages included in dstStageMask in the destination subpass.

The automatic image layout transitions from initialLayout to the first used layout (if it is different) are performed according to the following rules:

Similar rules apply for the transition to the finalLayout, using dependencies with dstSubpass equal to VK_SUBPASS_EXTERNAL

If an attachment specifies the VK_ATTACHMENT_LOAD_OP_CLEAR load operation, then it will logically be cleared at the start of the first subpass where it is used.

[Note]Note

Implementations may move clears earlier as long as it does not affect the operation of a render pass instance. For example, an implementation may choose to clear all attachments at the start of the render pass instance. If an attachment has the VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT flag set, then the clear must occur at the start of subpass where the attachment is first used, in order to preserve the operation of the render pass instance.

The first use of an attachment must not specify a layout equal to VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL or VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL if the attachment specifies that the loadOp is VK_ATTACHMENT_LOAD_OP_CLEAR. If a subpass uses the same attachment as both an input attachment and either a color attachment or a depth/stencil attachment, then both uses must observe the result of the clear.

Similarly, if an attachment specifies that the storeOp is VK_ATTACHMENT_STORE_OP_STORE, then it will logically be stored at the end of the last subpass where it is used.

[Note]Note

Implementations may move stores later as long as it does not affect the operation of a render pass instance. If an attachment has the VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT flag set, then the store must occur at the end of the highest numbered subpass that uses the attachment.

If an attachment is not used by any subpass, then the loadOp and the storeOp are ignored and the attachment’s memory contents will not be modified by execution of a render pass instance.

It will be common for a render pass to consist of a simple linear graph of dependencies, where subpass N depends on subpass N-1 for all N, and the operation of the memory barriers and layout transitions is fairly straightforward to reason about for those simple cases. But for more complex graphs, there are some rules that govern when there must be dependencies between subpasses.

As stated earlier, render passes must include subpass dependencies which (either directly or via a subpass dependency chain) separate any two subpasses that operate on the same attachment or aliasing attachments, if at least one of those subpasses writes to the attachment. If an image layout changes between those two subpasses, the implementation uses the stageMasks and accessMasks indicated by the subpass dependency as the masks that control when the layout transition must occur. If there is not a layout change on the attachment, or if an implementation treats the two layouts identically, then it may treat the dependency as a simple execution/memory barrier.

If two subpasses use the same attachment in different layouts but both uses are read-only (i.e. input attachment, or read-only depth/stencil attachment), the application does not need to express a dependency between the two subpasses. Implementations that treat the two layouts differently may deduce and insert a dependency between the subpasses, with the implementation choosing the appropriate stage masks and access masks based on whether the attachment is used as an input or depth/stencil attachment, and may insert the appropriate layout transition along with the execution/memory barrier. Implementations that treat the two layouts identically need not insert a barrier, and the two subpasses may execute simultaneously. The stage masks and access masks are chosen as follows:

where srcStageMask and srcAccessMask are taken based on usage in the source subpass and dstStageMask and dstAccessMask are taken based on usage in the destination subpass.

If a subpass uses the same attachment as both an input attachment and either a color attachment or a depth/stencil attachment, reads from the input attachment are not automatically coherent with writes through the color or depth/stencil attachment. In order to achieve well-defined results, one of two criteria must be satisfied. First, if the color components or depth/stencil components read by the input attachment are mutually exclusive with the components written by the color or depth/stencil attachment then there is no feedback loop and the reads and writes both function normally, with the reads observing values from the previous subpass(es) or from memory. This option requires the graphics pipelines used by the subpass to disable writes to color components that are read as inputs via the colorWriteMask, and to disable writes to depth/stencil components that are read as inputs via depthWriteEnable or stencilTestEnable.

Second, if the input attachment reads components that are written by the color or depth/stencil attachment, then there is a feedback loop and a pipeline barrier must be used between when the attachment is written and when it is subsequently read by later fragments. This pipeline barrier must follow the rules of a self-dependency as described in Subpass Self-dependency, where the barrier’s flags include:

A pipeline barrier is needed each time a fragment will read a particular (x,y,layer,sample) location if that location has been written since the most recent pipeline barrier, or since the start of the subpass if there have been no pipeline barriers since the start of the subpass.

An attachment used as both an input attachment and color attachment must be in the VK_IMAGE_LAYOUT_GENERAL layout. An attachment used as both an input attachment and depth/stencil attachment must be in either the VK_IMAGE_LAYOUT_GENERAL or VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL layout. Since an attachment in the VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL layout is read-only, this situation is not a feedback loop.

To destroy a render pass, call:

 

void vkDestroyRenderPass(
    VkDevice                                    device,
    VkRenderPass                                renderPass,
    const VkAllocationCallbacks*                pAllocator);