---
title: "Copy part of a GPU buffer in C#"
url: https://recursiveai.org/onegpu/cookbook/copy-range-between-gpu-buffers/
description: "Copy a byte range between GPU buffers in C# with OneGpu and Vulkan. Set the source offset, destination offset, and byte count without CPU staging."
version: "1.1.0"
---

# Copy part of a GPU buffer 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/copy-range-between-gpu-buffers/copy-range-between-gpu-buffers.zip)

Pass two byte offsets and a byte count to `CopyBuffer`. Requires Vulkan 1.3 on Windows or Linux.

`RecursiveAI.OneGpu.Backends` includes Vulkan, Metal, and browser WebGPU; this recipe uses Vulkan.

```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 RecursiveAI.OneGpu;

using var gpu = GpuBackends.CreateDevice();
using var source =
    gpu.CreateBuffer<int>([10, 20, 30, 40]);
using var target =
    gpu.CreateBuffer<int>([-1, -1, -1, -1]);
var commands = gpu.BeginCommands();
// byte offsets and count
commands.CopyBuffer(
    source.Buffer, 4, target.Buffer, 8, 8);
gpu.Submit(commands);

var result = new int[4];
await target.ReadAsync(result);
Console.WriteLine(string.Join(", ", result));
```

Copies `20` and `30` into the last two slots and prints the destination:

```text
-1, -1, 20, 30
```

On Vulkan, a range that runs past either buffer throws `ArgumentOutOfRangeException` while recording the copy.

Unzip [the example](https://recursiveai.org/onegpu/cookbook/copy-range-between-gpu-buffers/copy-range-between-gpu-buffers.zip), then `dotnet run`.

## Variations

Pack the same two values at the start of the destination. Replace the `CopyBuffer` call with:

```csharp
commands.CopyBuffer(
    source.Buffer, 4, target.Buffer, 0, 8);
```

```text
20, 30, -1, -1
```

## Related recipes

- [Read back a GPU buffer in C#](https://recursiveai.org/onegpu/cookbook/read-back-a-gpu-buffer/)
- [Copy a GPU texture without CPU readback](https://recursiveai.org/onegpu/cookbook/copy-gpu-texture-to-another-texture/)

## The short answer

Call `CopyBuffer` on a command encoder with the source buffer, its byte offset, the destination buffer, its byte offset, and the byte count. Then submit the encoder. On Vulkan, OneGpu records a device-side copy; there’s no managed array between the two buffers. The readback at the end of the sample is just for printing.

## Are GPU buffer copy offsets measured in bytes or elements?

They’re measured in bytes, even if you created the buffers with `CreateBuffer<int>`. The typed wrapper counts `int` elements, but the encoder takes its underlying `.Buffer`. An `int` takes four bytes, so source offset `4` skips `10`, size `8` covers `20` and `30`, and destination offset `8` starts the write at index `2`. `CopyBuffer` doesn’t interpret or convert the bytes.

The two buffers don’t have to be the same size. What matters is that the selected range fits the source and destination independently. In the example, the first two destination slots stay `-1` because the copy never touches those bytes. The variation changes only the destination offset, so the last two slots stay untouched instead.

## What usage flags does a GPU buffer copy need?

The source needs `BufferUsage.CopySource` and the destination needs `BufferUsage.CopyDestination`. The typed-buffer overload used above supplies both by default. If you create a buffer with explicit flags, keep the copy flag it needs alongside any flags needed for the next operation. For example, a copied range that will become a shader input also needs the usage appropriate to that binding. A missing copy flag throws `ArgumentException` on Vulkan.

Record the copy outside a compute or render pass. If a shader produces the source bytes, end its pass before recording the copy. On Vulkan, the encoder sets up the transfer access for both buffers. If another GPU pass is the only consumer, you can leave the result on the GPU instead of calling `ReadAsync`; if C# needs the bytes, await readback after submitting the copy. See the [compute surface](https://recursiveai.org/onegpu/docs/compute/) for pass ordering and readback.
