Pass a rectangle and just its RGBA bytes to WriteTexture. Requires Vulkan 1.3 on Windows or Linux.
dotnet add package RecursiveAI.OneGpu.BackendsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using System; using RecursiveAI.OneGpu; using var gpu = GpuBackends.CreateDevice(); var usage = TextureUsage.CopyDestination | TextureUsage.CopySource; using var texture = gpu.CreateTexture(new( 4, 3, GpuFormat.Rgba8Unorm, usage)); var pixels = new byte[4 * 3 * 4]; gpu.WriteTexture(texture, 0, 0, pixels); byte[] patch = [255, 0, 0, 255, 0, 255, 0, 255]; var rect = TextureRegion.Rect( mip: 0, layer: 0, x: 1, y: 1, width: 2, height: 1); gpu.WriteTexture(texture, rect, patch); Console.WriteLine("Uploaded a 2 x 1 patch.");
Unzip the example, then dotnet run.
Prints:
Uploaded a 2 x 1 patch.The full program
The program starts with a zero-filled 4 × 3 texture and writes one red and one green pixel at (1, 1) and (2, 1). It reads back the whole texture and checks all four channels of every pixel.
using System; using RecursiveAI.OneGpu; using var gpu = GpuBackends.CreateDevice(); var usage = TextureUsage.CopyDestination | TextureUsage.CopySource; using var texture = gpu.CreateTexture(new( 4, 3, GpuFormat.Rgba8Unorm, usage)); var pixels = new byte[4 * 3 * 4]; gpu.WriteTexture(texture, 0, 0, pixels); byte[] patch = [255, 0, 0, 255, 0, 255, 0, 255]; var rect = TextureRegion.Rect( mip: 0, layer: 0, x: 1, y: 1, width: 2, height: 1); gpu.WriteTexture(texture, rect, patch); Console.WriteLine("Uploaded a 2 x 1 patch."); await gpu.ReadTextureAsync(texture, 0, 0, pixels); int changed = 0; for (int y = 0; y < 3; y++) for (int x = 0; x < 4; x++) { var actual = pixels.AsSpan((y * 4 + x) * 4, 4); ReadOnlySpan<byte> expected = y == 1 && x is 1 or 2 ? patch.AsSpan((x - 1) * 4, 4) : [0, 0, 0, 0]; if (!actual.SequenceEqual(expected)) throw new InvalidOperationException($"Wrong pixel at ({x}, {y})."); if (actual[3] != 0) changed++; } Console.WriteLine($"Changed pixels: {changed}"); Console.WriteLine($"Unchanged pixels: {12 - changed}"); Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}");
Output from dotnet run:
Uploaded a 2 x 1 patch. Changed pixels: 2 Unchanged pixels: 10 Vulkan: NVIDIA GeForce RTX 4070
A rectangle that extends past the mip’s edge throws ArgumentOutOfRangeException; a patch with the wrong byte count throws ArgumentException.
Variations
Move the patch to the top-left corner
Change the rectangle’s origin to (0, 0) and keep the same two RGBA pixels:
var rect = TextureRegion.Rect( mip: 0, layer: 0, x: 0, y: 0, width: 2, height: 1); gpu.WriteTexture(texture, rect, patch);
If you’re using the full program, also change the pixel comparison to y == 0 && x is 0 or 1 and use x * 4 as the byte offset within patch.
Related recipes
The short answer
Create the texture with TextureUsage.CopyDestination. Pass TextureRegion.Rect(mip, layer, x, y, width, height) and the rectangle’s packed bytes to gpu.WriteTexture. You’re updating a region of an existing texture, not creating a second texture. Add TextureUsage.CopySource if you need to copy the result back to the CPU as this example does.
Can you update a Vulkan texture without replacing it every frame?
Yes. Keep the texture and write each changed rectangle into it. x and y are measured in texels (texture pixels) at the selected mip level, not bytes or coordinates at the original size. The first two arguments select the mip and array layer; both are 0 for the base level of this regular 2D texture. The last two arguments describe the rectangle’s size, not its bottom-right corner. At (1, 1) with size 2 × 1, the updated columns are 1 and 2, both in row 1.
A newly created texture has undefined contents. Initialize it before you expect a partial write to leave a known background. The first WriteTexture in the example writes the full level, and the second queues the patch. OneGpu’s texture API queues uploads ahead of the next submitted commands; you don’t write a compute shader for this copy. The call replaces the RGBA bytes. It doesn’t blend the old and new pixels.
What row pitch does a Vulkan texture patch need?
Row pitch means the number of bytes between the starts of two rows. For this OneGpu overload, pass tightly packed rows of the patch, not rows with the pitch of the whole texture. For Rgba8Unorm, each texel is four bytes in red, green, blue, alpha order. A 2 × 1 patch needs eight bytes; a 2 × 2 patch needs sixteen, with the second row starting at byte eight. Don’t prepend bytes for the destination’s offset: TextureRegion.Rect carries the offset separately.
If the patch comes from a larger CPU image with padded rows, pack just the rectangle’s bytes from each row into a contiguous array before uploading. The full-texture readback has its own tight pitch: 4 × 4 = 16 bytes per row in this example. That’s why the verification loop finds a pixel at (x, y) at byte (y * 4 + x) * 4, but indexes the two-pixel patch relative to its own origin.