Use ColorAttachment.Clear in a render pass with no draw calls. Requires Vulkan 1.3, but no window or shader.
dotnet add package RecursiveAI.OneGpu.BackendsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 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:
00FF00FF00FF00FF00FF00FF00FF00FFThe 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.
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)}");
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:
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:
gpu.ReadTexture(texture, 0, layer, rgba);The download clears layer 1 by default. Run dotnet run -- --layer-zero to clear the first layer instead:
Vulkan: NVIDIA GeForce RTX 4070 Layer 0: 00FF00FF00FF00FF00FF00FF00FF00FF
Related recipes
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 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.