Recursive AI

Dispose a WebGPU buffer before submit in C#

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

Record the copy, dispose its source, then submit. This uses OneGpu 1.2.0 in a Blazor WebAssembly app in Chrome with WebGPU.

OneGpu defers destruction until the submitted commands have run, but WebGPU still rejects the submission if you call GPUBuffer.destroy() directly from JavaScript before submitting.

Terminal
dotnet new blazorwasm -o DisposeDemo
cd DisposeDemo
dotnet add package RecursiveAI.OneGpu.WebGpu

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

C#
using RecursiveAI.OneGpu.WebGpu;
// Inside an async component method:
using var gpu =
    await WebGpu.CreateDeviceAsync();
using var target =
    gpu.CreateBuffer<uint>(4);
var source =
    gpu.CreateBuffer<uint>([3, 5, 7, 11]);
var commands = gpu.BeginCommands();
commands.CopyBuffer(source.Buffer, 0,
    target.Buffer, 0, source.SizeInBytes);
source.Dispose();
gpu.Submit(commands);

The example is the full program below: unzip it and dotnet run.

Full program

Open the address printed by dotnet run in Chrome. For an existing Blazor WebAssembly app, replace Pages/Home.razor with the component below. On the first render it records and submits the copy, then reads back the destination and displays the values.

@page "/"
@using RecursiveAI.OneGpu.WebGpu
 
<pre id="result">@result</pre>
 
@code {
    private string result = "Copying…";
 
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (!firstRender) return;
 
        try
        {
            using var gpu = await WebGpu.CreateDeviceAsync();
            using var target = gpu.CreateBuffer<uint>(4);
            var commands = gpu.BeginCommands();
            using (var source = gpu.CreateBuffer<uint>([3, 5, 7, 11]))
            {
                commands.CopyBuffer(source.Buffer, 0, target.Buffer, 0, source.SizeInBytes);
            } // Disposed after recording, before submission.
 
            gpu.Submit(commands);
            var values = new uint[4];
            await target.ReadAsync(values);
            result = $"Copied: {string.Join(", ", values)}";
        }
        catch (Exception error)
        {
            result = $"WebGPU error: {error.Message}";
        }
        StateHasChanged();
    }
}
Text
Copied: 3, 5, 7, 11

If Chrome can’t provide a WebGPU adapter, device creation fails and the page displays the error instead of the copied values.

Variations

Copy only the middle two values. In Pages/Home.razor, allocate target with gpu.CreateBuffer<uint>(2) and set values to new uint[2]. Replace the copy call inside the source buffer’s using block. Offsets and lengths are in bytes; one uint is four bytes.

C#
commands.CopyBuffer(source.Buffer, 4,
    target.Buffer, 0, 8);
Text
Copied: 5, 7

The short answer

With OneGpu’s WebGPU backend in 1.2.0, record the last command that uses the source buffer, dispose the C# buffer, then submit the encoder. OneGpu defers destruction so that WebGPU can still execute the recorded copy. You can’t use the disposed wrapper to record more work. Keep the destination buffer alive until ReadAsync completes if you need the result on the CPU.

Why does WebGPU say a buffer was used in submit while destroyed?

Recording a copy doesn’t run it. The browser’s queue needs both buffers to be valid when you submit the commands. If you use the browser’s raw GPUBuffer.destroy() before submission, the WebGPU submit rules reject the destroyed buffer. The same rules let the browser keep memory alive for work that was already submitted when a buffer is destroyed.

OneGpu’s Dispose() is a different call. When this example leaves the inner using block, it disposes the C# wrapper while the encoder is still open. The backend puts the destruction operation aside. On Submit, it sends the recorded copy and the queue submission to the browser before the destruction operation. That ordering is what OneGpu 1.1.2 fixed. It applies to the backend’s managed resources, not a separate GPUBuffer.destroy() call made from JavaScript.

When should I dispose a WebGPU buffer in .NET?

Dispose a temporary source once you’ve recorded its last use. In the example, only the copy refers to source, so its scope can end before Submit. If a later pass still needs to bind or copy from the same buffer, leave its scope open until you’ve recorded that pass too. Deferring destruction does not make a disposed wrapper usable again.

For target, the last use is not the copy. The full component also downloads its bytes, so target stays in scope until the awaited read finishes. Submit hands work to the browser’s queue; it doesn’t wait for the copy to complete. Use asynchronous readback in the browser rather than trying to make submission block. If the destination only feeds another GPU command, you don’t need to read it back to the CPU.

All onegpu recipes