To answer your questions:
Normaly you clear when the frame begins, before you render something.
beginRenderpass can only be called by the primary commandbuffer, secondary commandbuffer must not invoke this call.
Basically you are starting a renderpass instance in the primary commandbuffer(only commandbuffer that you can submit to the queue),
This is what you will be doing on each frame.
but you can clear one or more regions of color and depth/stencil attachments inside a render pass instance, by calling
vkCmdClearAttachments regardless whether it is LOAD_OP_CLEAR / LOAD_OP_DONT_CARE,
or if you want to do it out side the renderpass you use vkCmdClearColor/DepthStencilImage.
this can be called by either the primary command buffer or secondary commandbuffer.
Tip: using LOAD_OP_DONT_CARE may optimized on some drivers, if you are sure that you will overwrite the entire the screen written by the previous frame.
so the drivers don't have to load/copy the memory from the presenation buffer for the current renderpass to clear it.
you can use vkCmdClearAttachment command in the secondary comandbuffer to clear any attachment.
But you cannot submit them by itself, it has to be put inside the primary commandbuffer.
So does that mean that every submitted command buffer starts a new renderpass and so clears the attachments?
is it a misunderstanding of vkCmdBeginRenderPass?
yes every submit commandbuffer starts the renderpass and clear the fbo attachments.
Does it not actually begin a new render pass when there is one running already?
Renderpass contains the executation order of the framebuffer. it has states.
the states are reused on each frame. you can use another renderpass (different state) with the same fbo.
so the first renderpass can clear it and the second renderpass don't clear at the beginning of the frame.
you cannot call another renderpass inside renderpass instance.
what you see below is invalid
Commandbuffer.beginRenderpass(renderpass1, fbo, ClearValues);
Commandbuffer.beginRenderpass(renderpass2, fbo, ClearValues);// ERROR
Commandbuffer.end;
you have to end the renderpass1 instance before you begin the second renderpass.
should be like this
Commandbuffer.beginRenderpass(renderpass1, fbo, ClearValues);
// draw scene
Commandbuffer.end;// renderpass is ended
Commandbuffer.beginRenderpass(renderpass2, fbo, ClearValues);
// draw full screen quad
Commandbuffer.end;
Commandbuffer.beginRenderpass(renderpass3, fbo, ClearValues);
// draw full screen quad
Commandbuffer.end;
let say that in the above example the fbo has 3 attachment. we are using 3 renderpass here.
First: Render the scene in to the attachment 1 using the renderpass1.
Second: Read from the attachment 1 and do a Vertical Blur and write into attachment 2 using renderpass2.
third: Read from the attachment 2 and do a Horinzontal blur and write in to the swapchain image using renderpass3.
(Note: for this particular techniqueue we cannnot use multiple subpass thats why I'm using 3 renderpasses for the same fbo.)