## 17.2. Clearing Images Inside A Render Pass Instance

To clear one or more regions of color and depth/stencil attachments inside a render pass instance, call:

void vkCmdClearAttachments(
VkCommandBuffer                             commandBuffer,
uint32_t                                    attachmentCount,
const VkClearAttachment*                    pAttachments,
uint32_t                                    rectCount,
const VkClearRect*                          pRects);

• commandBuffer is the command buffer into which the command will be recorded.
• attachmentCount is the number of entries in the pAttachments array.
• pAttachments is a pointer to an array of VkClearAttachment structures defining the attachments to clear and the clear values to use.
• rectCount is the number of entries in the pRects array.
• pRects points to an array of VkClearRect structures defining regions within each selected attachment to clear.

vkCmdClearAttachments can clear multiple regions of each attachment used in the current subpass of a render pass instance. This command must be called only inside a render pass instance, and implicitly selects the images to clear based on the current framebuffer attachments and the command parameters.

The VkClearRect structure is defined as:

typedef struct VkClearRect {
VkRect2D    rect;
uint32_t    baseArrayLayer;
uint32_t    layerCount;
} VkClearRect;

• rect is the two-dimensional region to be cleared.
• baseArrayLayer is the first layer to be cleared.
• layerCount is the number of layers to clear.

The layers $[baseArrayLayer, baseArrayLayer+layerCount)$ counting from the base layer of the attachment image view are cleared.

The VkClearAttachment structure is defined as:

typedef struct VkClearAttachment {
uint32_t              colorAttachment;
VkClearValue          clearValue;
} VkClearAttachment;

• aspectMask is a mask selecting the color, depth and/or stencil aspects of the attachment to be cleared. aspectMask can include VK_IMAGE_ASPECT_COLOR_BIT for color attachments, VK_IMAGE_ASPECT_DEPTH_BIT for depth/stencil attachments with a depth component, and VK_IMAGE_ASPECT_STENCIL_BIT for depth/stencil attachments with a stencil component. If the subpass’s depth/stencil attachment is VK_ATTACHMENT_UNUSED, then the clear has no effect.
• colorAttachment is only meaningful if VK_IMAGE_ASPECT_COLOR_BIT is set in aspectMask, in which case it is an index to the pColorAttachments array in the VkSubpassDescription structure of the current subpass which selects the color attachment to clear. If colorAttachment is VK_ATTACHMENT_UNUSED or is greater than or equal to VkSubpassDescription::colorAttachmentCount, then the clear has no effect.
• clearValue is the color or depth/stencil value to clear the attachment to, as described in Clear Values below.

No memory barriers are needed between vkCmdClearAttachments and preceding or subsequent draw or attachment clear commands in the same subpass.

The vkCmdClearAttachments command is not affected by the bound pipeline state.

Attachments can also be cleared at the beginning of a render pass instance by setting loadOp (or stencilLoadOp) of VkAttachmentDescription to VK_ATTACHMENT_LOAD_OP_CLEAR, as described for vkCreateRenderPass.