---
title: "Clear an offscreen Vulkan texture to a color in C#"
url: https://recursiveai.org/onegpu/cookbook/clear-a-vulkan-render-target-texture/
description: "Clear one layer of an offscreen Vulkan texture to a color in C#. Use a clear-only render pass with no shader or window, then read the RGBA bytes."
version: "1.1.0"
---

# Clear an offscreen Vulkan texture to a color in C#

**OneGpu** is a free, open-source GPU compute library for .NET from Recursive AI. MIT or Apache-2.0.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneGpu.Backends) · [GitHub](https://github.com/recursive-ai-oss/onegpu-csharp) · [Docs](https://recursiveai.org/onegpu/docs/) · [Download example (.zip)](https://recursiveai.org/onegpu/cookbook/clear-a-vulkan-render-target-texture/clear-a-vulkan-render-target-texture.zip)

Use `ColorAttachment.Clear` in a render pass with no draw calls. Requires Vulkan 1.3, but no window or shader.

```shell
dotnet add package RecursiveAI.OneGpu.Backends
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using System;
using RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Vulkan;

using var gpu = VulkanGpu.CreateDevice(
    new() { Adapter = "discrete" });
var usage = TextureUsage.RenderTarget
          | TextureUsage.CopySource;
using var texture = gpu.CreateTexture(
    new TextureDesc(2, 2,
        GpuFormat.Rgba8Unorm, usage));
// r, g, b, a: opaque green.
var clear = ColorAttachment.Clear(
    texture, 0, 1, 0, 1);
var commands = gpu.BeginCommands();
commands.BeginRenderPass(clear).EndPass();
gpu.Submit(commands);

var rgba = new byte[16];
gpu.ReadTexture(texture, 0, 0, rgba);
Console.WriteLine(Convert.ToHexString(rgba));
```

[The example](https://recursiveai.org/onegpu/cookbook/clear-a-vulkan-render-target-texture/clear-a-vulkan-render-target-texture.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

Reads back four opaque green pixels as RGBA bytes in hex:

```text
00FF00FF00FF00FF00FF00FF00FF00FF
```

## The full program

The program also prints the adapter and the layer it read. The pass targets mip level `0`, the full-size image, and array layer `1`, the second layer.

```csharp
using System;
using RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Vulkan;

using var gpu = VulkanGpu.CreateDevice(
    new() { Adapter = "discrete" });
var usage = TextureUsage.RenderTarget
          | TextureUsage.CopySource;
using var texture = gpu.CreateTexture(
    new TextureDesc(2, 2,
        GpuFormat.Rgba8Unorm, usage)
    { ArrayLayers = 2 });
uint layer = args is ["--layer-zero"] ? 0u : 1u;
var clear = ColorAttachment.Clear(
    texture, 0, 1, 0, 1) with { Layer = layer };
var commands = gpu.BeginCommands();
commands.BeginRenderPass(clear).EndPass();
gpu.Submit(commands);

var rgba = new byte[2 * 2 * 4];
gpu.ReadTexture(texture, 0, layer, rgba);
Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}");
Console.WriteLine($"Layer {layer}: {Convert.ToHexString(rgba)}");
```

```text
Vulkan: NVIDIA GeForce RTX 4070
Layer 1: 00FF00FF00FF00FF00FF00FF00FF00FF
```

`VulkanGpu.CreateDevice` throws `GpuUnsupportedException` if `"discrete"` matches no adapter or Vulkan 1.3 support is missing; if only the discrete GPU is missing, pass `Adapter = null` to use the best available adapter (unless `ONEGPU_ADAPTER` overrides it).

On Vulkan, a texture without `TextureUsage.RenderTarget` is rejected when you begin the render pass.

## Variations

**Clear a layer of a texture array.** Replace the texture and attachment setup in the snippet at the top with this two-layer texture. Set `layer` to `1` for the second layer or `0` for the first:

```csharp
using var texture = gpu.CreateTexture(
    new TextureDesc(2, 2,
        GpuFormat.Rgba8Unorm, usage)
    { ArrayLayers = 2 });
uint layer = 1;
var clear = ColorAttachment.Clear(
    texture, 0, 1, 0, 1) with { Layer = layer };
```

After submitting, replace the readback line so it reads the layer you cleared:

```csharp
gpu.ReadTexture(texture, 0, layer, rgba);
```

The download clears layer `1` by default. Run `dotnet run -- --layer-zero` to clear the first layer instead:

```text
Vulkan: NVIDIA GeForce RTX 4070
Layer 0: 00FF00FF00FF00FF00FF00FF00FF00FF
```

## Related recipes

- [Copy a GPU texture without CPU readback](https://recursiveai.org/onegpu/cookbook/copy-gpu-texture-to-another-texture/)
- [Update part of a Vulkan texture](https://recursiveai.org/onegpu/cookbook/update-a-gpu-texture-region/)

## The short answer

Create a texture with `TextureUsage.RenderTarget`, then pass `ColorAttachment.Clear(texture, r, g, b, a)` to `BeginRenderPass`. Set `Layer` to select the array layer. End the pass without drawing and submit the commands. Add `TextureUsage.CopySource` only if you need to copy the result out, as this example does for readback.

## How do you clear a Vulkan framebuffer image without drawing?

Specify the clear as an attachment load operation. The [Vulkan clear commands reference](https://docs.vulkan.org/spec/latest/chapters/clears.html) distinguishes clearing at the start of a rendering pass from clearing an image outside a pass. Here `ColorAttachment.Clear` sets `LoadOp.Clear` at the start and `StoreOp.Store` at the end. No draw command is needed to produce the color, and the result is kept after `EndPass()` and submission. There’s no reason to create a graphics pipeline or connect the texture to a window just to set its initial color.

## Does clearing a color attachment clear every layer?

No. In this OneGpu pass, the attachment chooses one `Layer` and one `MipLevel`. The other layers aren’t initialized by this pass, and you shouldn’t expect zeroes if you read an untouched layer. To initialize multiple layers, record a clear for each one you’ll use. Read back the same mip level and layer that you cleared when checking the result.

For `Rgba8Unorm`, each channel has one byte. The clear takes normalized values, so `0, 1, 0, 1` becomes `00 FF 00 FF`: no red, full green, no blue, fully opaque. A 2 × 2 layer therefore needs 16 bytes for `ReadTexture`. The read blocks until submitted work finishes; omit it if the next consumer is another GPU operation and you don’t need the bytes on the CPU.
