---
title: "Avoid frame stalls from large billboard texture prep in C#"
url: https://recursiveai.org/onegame/cookbook/load-billboard-texture-without-stalling/
description: "Prepare large billboard textures on worker threads in OneGame, keep drawing while the pixels are prepared, then upload the texture on the render thread."
version: "2.7.0"
---

# Avoid frame stalls from large billboard texture prep in C#

**onegame** is a free, open-source game engine for .NET from Recursive AI. MIT or Apache-2.0, no editor required.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneGame.Render3D) · [GitHub](https://github.com/recursive-ai-oss/onegame) · [Docs](https://recursiveai.org/onegame/docs/) · [Download example (.zip)](https://recursiveai.org/onegame/cookbook/load-billboard-texture-without-stalling/load-billboard-texture-without-stalling.zip)

On desktop, only alpha premultiplication (scaling pixel colors by opacity) runs in the background; the GPU upload stays on the render thread. Neither call guarantees a hitch-free frame. The example uses Vulkan.

```shell
dotnet add package RecursiveAI.OneGame.Render3D
```

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

```csharp
// From your scene: renderer, batch.
// Decoded image: width, height, rgba.
var load = renderer
    .BeginCreateBillboardTexture(
        width, height, rgba);

// In the frame loop, on the render thread:
if (load.IsReady && batch.Texture is null)
    batch.Texture = load.Complete();
```

The billboard draws a soft round spot until the pixels are ready and uploaded, then draws your image.

Unzip [the example](https://recursiveai.org/onegame/cookbook/load-billboard-texture-without-stalling/load-billboard-texture-without-stalling.zip), then `dotnet run`.

## The full program

The example creates a 2,048 × 2,048 red image with a translucent border in memory. It draws a billboard offscreen before and after assigning the texture. No image file or window is needed. The example also installs the Vulkan backend:

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

```csharp
using System.Diagnostics;
using System.Numerics;
using OneGame.Render3D;
using RecursiveAI.OneGpu.Vulkan;

using var device = VulkanGpu.CreateDevice();
using var renderer = new Renderer3D(device);
using var target = new Offscreen3D(renderer, 64, 64);
var scene = new Scene3D { Samples = 1 };
scene.Camera = Camera3D.LookAt(new(0, 0, 3), Vector3.Zero, Vector3.UnitY);
scene.Post.Bloom = false;
scene.Post.ToneMapper = ToneMapper.None;
var batch = new BillboardBatch3D();
batch.Add(new Billboard3D { Size = Vector2.One, Color = Vector4.One });
scene.Billboards.Add(batch);

// Make a red image with a half-transparent border; no file is needed.
const int size = 2048;
var rgba = new byte[size * size * 4];
for (int y = 0; y < size; y++)
for (int x = 0; x < size; x++)
{
    int i = (y * size + x) * 4;
    rgba[i] = 255;
    rgba[i + 3] = (byte)(x < 256 || x >= size - 256 ||
                         y < 256 || y >= size - 256 ? 128 : 255);
}

byte[] before = target.Capture(scene); // Draws the default soft spot.
var load = renderer.BeginCreateBillboardTexture(size, size, rgba);
var timeout = Stopwatch.StartNew();
while (!load.IsReady)
{
    if (timeout.Elapsed > TimeSpan.FromSeconds(10))
        throw new TimeoutException("Billboard pixels were not ready.");
    target.Capture(scene); // Stand-in for drawing the next game frame.
}
using var texture = load.Complete();
batch.Texture = texture;
byte[] after = target.Capture(scene);
int center = (32 * target.Width + 32) * 4;
Console.WriteLine($"Backend: {device.Backend} ({device.Adapter.Name})");
Console.WriteLine($"Texture: {texture.Width} x {texture.Height}");
Console.WriteLine($"Billboard center changed to red: {before[center + 1] > after[center + 1] && after[center] > after[center + 1]}");
```

`dotnet run` printed this on a machine with an RTX 4070:

```text
Backend: Vulkan (NVIDIA GeForce RTX 4070)
Texture: 2048 x 2048
Billboard center changed to red: True
```

For a PNG, add `RecursiveAI.OneGame.Host` and call `var image = OneGame.Host.Png.Load("billboard.png");` with your own file. Pass `image.Width`, `image.Height`, and the `byte[]` in `image.Rgba` to `BeginCreateBillboardTexture`.

Pass straight-alpha sRGB RGBA bytes, top row first, and keep the buffer unchanged until `IsReady` is true. If the dimensions aren’t positive or the buffer length doesn’t match `width * height * 4`, the renderer rejects the input. Dispose the uploaded texture before disposing the renderer.

## Variations

**Upload immediately during a loading screen.** Replace the pending-load block in the full program with this single call. It prepares and uploads the same texture on the calling thread and produces the same output.

```csharp
using var texture = renderer
    .CreateBillboardTexture(size, size, rgba);
```

## Related recipes

- [Draw 2D sprites](https://recursiveai.org/onegame/cookbook/draw-sprites-2d/)
- [Add height fog to a 3D scene](https://recursiveai.org/onegame/cookbook/add-height-fog-to-3d-scene/)

## The short answer

Call `BeginCreateBillboardTexture` once with decoded RGBA pixels. Keep drawing while `IsReady` is false. When it’s true, call `Complete` on the thread that owns the renderer and assign the returned texture to `BillboardBatch3D.Texture`. On desktop, this moves alpha premultiplication off the render thread, not the upload. In a browser build, preparation happens inside `BeginCreateBillboardTexture`, so you don’t get the same background work there.

## Why can loading a large texture still cause a frame drop?

Loading an image file, decoding it into pixels, preparing those pixels for blending, and getting the result to the GPU are separate steps. `BeginCreateBillboardTexture` starts with pixels you already have; it doesn’t read or decode PNG or JPEG files for you. It splits the premultiplication work into bands of rows for thread-pool workers on desktop. Renderer setup still happens in the initial call, and `Complete` still creates and writes the GPU texture on the render thread. Neither call promises a hitch-free frame.

Don’t wrap the call to `Complete` in `Task.Run`, and don’t call it early to check whether the data is ready: it waits for the CPU work if you call it before `IsReady`. If the upload itself is the frame hitch you’re trying to avoid, give it a place in your loading screen or choose a smaller texture. If replacing an existing image, keep the old one assigned while the new one is pending; track the pending load separately instead of using an empty batch as the condition for swapping.

## What does premultiplied alpha mean?

Alpha is opacity. With straight alpha, a half-transparent red pixel still stores full red in its color channels. With premultiplied alpha, the red value is multiplied by that pixel’s opacity before blending. OneGame does that multiplication in linear light: it decodes each sRGB color channel, multiplies by alpha, then encodes the result back to sRGB. The alpha byte itself stays unchanged.

That distinction matters at the edge of a billboard, where filtering can combine fully transparent pixels with partly opaque ones. Let the renderer handle the conversion; passing pixels that are already premultiplied makes it apply alpha twice. The background and immediate paths use the same conversion and produce the same bytes.
