<p>You are looking for <a href="https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#clears-outside" rel="nofollow noreferrer"><code>vkCmdClearColorImage</code>,</a> </p>

<p>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 <code>vkCmdDispatch</code>.</p>

<p>There is also a <code>vkCmdClearDepthStencilImage</code> if you need it.</p>

CC BY-SA 4.0
2018-09-05T18:35:42.577Z
Also, the image must be created with `VK_IMAGE_USAGE_TRANSFER_DST_BIT`, since clearing is a transfer operation.
Nicol Bolas
CC BY-SA 4.0
2018-09-05T20:03:05.123Z
@NicolBolas but that's not the case for implicit clears via the renderpass?  That seems like an odd distinction.
Jherico
CC BY-SA 4.0
2018-09-05T21:23:20.197Z
@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
CC BY-SA 4.0
2018-09-05T14:23:46.173Z
2018-09-05T16:13:01.913Z
2018-09-05T16:13:01.913Z
ratchet freak
<p>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.</p>

<p>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.</p>

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

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

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

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

<vulkan>
Best way to clear a framebuffer image without drawing?
sak
# Best way to clear a framebuffer image without drawing?

(Asked by: username_0 on 2018-09-05)

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?

**Tags:** vulkan


---

## 1 answers

### Answer by username_1 on 2018-09-05. Score: 5 | Accepted answer

You are looking for [`vkCmdClearColorImage`,](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#clears-outside)

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.

> **Comments:**
> **username_2**: Also, the image must be created with `VK_IMAGE_USAGE_TRANSFER_DST_BIT`, since clearing is a transfer operation.  
> **username_3**: @username_2 but that's not the case for implicit clears via the renderpass?  That seems like an odd distinction.  
> **username_2**: @username_3: 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.  

---

