What is the right way to clear a framebuffer

I have a framebuffer that i render to it from more then 1 command buffer. I want to clear it.

I cant use the clear in VK_ATTACHMENT_LOAD_OP_CLEAR in the renderpass because every commandbuffer has a renderpass and this will cause the renderpasses to delete each other.

I wanted to use a renderpass with zero subpasses to clear the framebuffer and then start to render but the validation layer yells at me for having zero subpasses.

I wanted to use vkCmdClearColorImage but it only works with the layout:
VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR , VK_IMAGE_LAYOUT_GENERAL or VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
which means i need to change the image layouts 2 times, before and after, with a pipeline barrier just to clear the image.

My question is what is the right way to clear the frame buffer.

I didn’t quite get your explanation why you cannot use VK_ATTACHMENT_LOAD_OP_CLEAR on just your first render pass, but in any case you can use vkCmdClearAttachments to perform a clear with a recorded command.

1 Like

thank you.
you are right vkCmdClearAttachments is the right command for me. it does not need any special layout.

Why does each command buffer start its own renderpass? Odds are good that what you want to do is have these be secondary command buffers that get executed within the same renderpass instance.

In my program I have more then one command buffer that draw to the framebuffer. Every command buffer draw more then one object/model and every object/model has its own renderpass in the commandbuffer. correct me if im wrong but if I draw 2 objects/model in the same render pass it means that they will have the same attachments like textures which doesnt make cense. Is there a way to unite different objects to the same renderpass?

thank you.

No, framebuffer attachments are for writing output of a renderpass. Textures sampled by your models are (almost) never attachments of the framebuffer you are currently rendering to, because it is not allowed to render to a texture while at the same time sampling from the same texture (with one exception that I don’t think is relevant here). It is very much expected to render many models with different materials in the same render pass.

2 Likes

you are right, i mixed things up. thank you.