nikitablack
nikitablack

Reputation: 4663

How to preserve attachment content between render passes

I have multiple "renderers" which should draw to the same attachment (swap chain image to be precise). I don't know the number of such renderers beforehand so I can't use subpasses. This is how I wanted to implement it:

VkCommandBuffer cb{...}; // get current "main" command buffer
for(auto r : renderers)
{
    VkRenderPassBeginInfo renderPassBeginInfo{get_render_pass_begin_info(...)};
    vkCmdBeginRenderPass(cb, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
    array<VkCommandBuffer, 2> buffs{r->getCommandBuffers()}; // renderer build two secondary command buffers...
    vkCmdExecuteCommands(cb, 1, buffs[0]); // first should be used in a render pass
    vkCmdEndRenderPass(cb);
    vkCmdExecuteCommands(cb, 1, buffs[1]); // second should be used ooutside of a render pass
}

The problem here is that each new call to vkCmdBeginRenderPass clears the target. This happens because the attachment was created with loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR because I need to clear it (but only once).

The solution in my case would be to move vkCmdBeginRenderPass and vkCmdEndRenderPass outside of the loop, but in this case, I need to "collect" all secondary command buffers that can't be used inside a render pass and execute them later.

But since the concept of render passes doesn't go into my head I wonder if there may be a way to keep the attachment's data between render passes?

Upvotes: 2

Views: 1418

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474536

You could stop clearing the attachments on load. Just manually clear them, either before the render pass begins or at the start of the first subpass.

That being said, render passes are not cheap, and this is really not the way to use them. The correct solution is to restructure your rendering code so that you only need a single render pass.

Upvotes: 2

Related Questions