Recursive AI

Copy a GPU texture without CPU readback in C#

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

Call CopyTexture(source, target) on matching textures. Requires Vulkan 1.3 on Windows or Linux.

Terminal
dotnet add package RecursiveAI.OneGpu.Backends

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneGpu;
 
using var gpu = GpuBackends.CreateDevice();
var usage = TextureUsage.CopySource
          | TextureUsage.CopyDestination;
var desc = new TextureDesc(
    2, 1, GpuFormat.Rgba8Unorm, usage);
using var source = gpu.CreateTexture(desc);
using var target = gpu.CreateTexture(desc);
byte[] pixels =
    [255, 0, 0, 255, 0, 255, 0, 255];
gpu.WriteTexture(source, 0, 0, pixels);
var commands = gpu.BeginCommands();
commands.CopyTexture(source, target);
gpu.Submit(commands);
 
var result = new byte[8];
// optional: read back only to print the result
await gpu.ReadTextureAsync(
    target, 0, 0, result);
Console.WriteLine(Convert.ToHexString(result));

Copies a red and a green pixel. The optional readback prints their RGBA bytes in hex:

Text
FF0000FF00FF00FF

On Vulkan, incompatible textures or missing copy usage flags throw ArgumentException while recording the copy.

Unzip the example, then dotnet run.

Variations

Copy two mip levels (the full image and its smaller level). Replace the descriptor in the snippet at the top:

C#
var desc = new TextureDesc(
    2, 1, GpuFormat.Rgba8Unorm, usage)
    { MipLevels = 2 };

After uploading level 0, upload one blue pixel to level 1:

C#
gpu.WriteTexture(
    source, 1, 0, [0, 0, 255, 255]);

Keep the same CopyTexture call. After printing the original result, read level 1 of the target:

C#
var mip = new byte[4];
await gpu.ReadTextureAsync(target, 1, 0, mip);
Console.WriteLine(Convert.ToHexString(mip));
Text
FF0000FF00FF00FF
0000FFFF

The short answer

Record CopyTexture(source, target) outside a render or compute pass and submit the commands. The source needs TextureUsage.CopySource, and the target needs TextureUsage.CopyDestination. On Vulkan, CopyTexture does not copy between devices. Create both textures on the device that records the copy. On Vulkan, the encoder records a GPU image copy, not a download and re-upload.

How do you copy a texture without reading pixels back to the CPU?

Start with pixels already in a GPU texture and use the GPU copy command. The sample uploads a tiny source image with WriteTexture so you can run it without an asset file. If the source was already filled by an earlier GPU pass, there’s no upload to perform. End that pass, encode the copy, then use the destination in the next GPU operation. Give the destination whatever usage that operation needs in addition to CopyDestination.

The sample’s final ReadTextureAsync is deliberately separate. It copies the destination’s first mip and first layer back so C# can print FF0000FF and 00FF00FF. Omit that readback and the print if the next consumer is another GPU pass. Submit sends the work to the GPU; it isn’t a request to return pixels to C#.

Can you copy a GPU texture to a different size or format?

Not with OneGpu’s CopyTexture. The source and target must match in width, height, depth, texture dimension, format, mip count, and array-layer count. Both must have a sample count of one. This is a copy, not a resize, format conversion, or multisample resolve. Reusing the same descriptor as the example does is a simple way to match the required properties; the two textures don’t otherwise need identical usage flags.

The command copies the whole texture, including all mip levels and array layers. It doesn’t generate missing mip content: the variation writes a blue pixel to the smaller source level so you can see that level is copied as-is. There’s no rectangle parameter on CopyTexture. The texture-region recipe covers a different task: uploading only part of a texture from CPU memory.

All onegpu recipes