# Best way to clear a framebuffer image without drawing?

So I have a framebuffer image which later acts as input to a compute shader, and some frames there is geometry drawn to the framebuffer, but some frames this is not necessary.

The thing is, in my current process, when I don't draw anything to that framebuffer, the image contents are undefined which leads to incorrect output of my compute shader. So on frames where there is no geometry drawn, I would like to simply clear the framebuffer.

In OpenGL this would be easy: glClearColor(...) but so far I have not been able to get it working on Vulkan.

I have tried just doing an empty renderpass: i.e: vkCmdBeginRenderPass/vkCmdEndRenderpass with the correct clear values defined in the VkRenderPassBeginInfo struct.

I have also tried doing a renderpass with only vkCmdClearAttachments between the bein/end commands.

So far I haven't had any luck. What's the best way to accomplish this?

You are looking for vkCmdClearColorImage,

It does exactly what it says on the tin, it clears an image to a specified color. This command uses the Transfer stage as far as barriers are concerned. You will need to add one between the clear and the vkCmdDispatch.

There is also a vkCmdClearDepthStencilImage if you need it.

• Also, the image must be created with VK_IMAGE_USAGE_TRANSFER_DST_BIT, since clearing is a transfer operation. – Nicol Bolas Sep 5 '18 at 18:35
• @NicolBolas but that's not the case for implicit clears via the renderpass? That seems like an odd distinction. – Jherico Sep 5 '18 at 20:03
• @Jherico: Renderpass clears are often just changes to the render target caching system, so that cache-miss reads simply return the clear color, and writes of a partial cache-line fill in the other values with the clear color. So "clearing" is just a trick that never directly touches memory (and it's even moreso for TBR-based systems). When you clear an image directly, you're doing actual writes to the memory, rather than caching tricks or clearing tile storage. – Nicol Bolas Sep 5 '18 at 21:23