Run ILGPU kernels directly in the browser using WebGPU!
SpawnDev.ILGPU.WebGPU is a WebGPU backend for ILGPU that enables GPU-accelerated compute in Blazor WebAssembly applications. Write your GPU kernels once in C# and run them on any WebGPU-capable browser.
- ILGPU-compatible - Use familiar ILGPU APIs (
ArrayView,Index1D/2D/3D, math intrinsics, etc.) - WGSL transpilation - C# kernels are automatically compiled to WebGPU Shading Language (WGSL)
- Blazor WebAssembly - Seamless integration via SpawnDev.BlazorJS
- Shared memory & atomics - Supports workgroup shared memory, barriers, and atomic operations
- No native dependencies - Entirely written in C#
dotnet add package SpawnDev.ILGPU.WebGPUusing ILGPU;
using ILGPU.Runtime;
using SpawnDev.ILGPU.WebGPU;
// Initialize ILGPU context with WebGPU backend
var builder = Context.Create();
await builder.WebGPUAsync();
using var context = builder.ToContext();
// Get WebGPU device and create accelerator
var devices = context.GetWebGPUDevices();
var device = devices[0];
using var accelerator = await device.CreateAcceleratorAsync(context);
// Allocate buffers
int length = 64;
var a = Enumerable.Range(0, length).Select(i => (float)i).ToArray();
var b = Enumerable.Range(0, length).Select(i => (float)i * 2.0f).ToArray();
using var bufA = accelerator.Allocate1D(a);
using var bufB = accelerator.Allocate1D(b);
using var bufC = accelerator.Allocate1D<float>(length);
// Load and execute kernel
var kernel = accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<float>, ArrayView<float>, ArrayView<float>>(VectorAddKernel);
kernel((Index1D)length, bufA.View, bufB.View, bufC.View);
// Wait for GPU to complete (async required in Blazor WASM)
await accelerator.SynchronizeAsync();
// Define the kernel
static void VectorAddKernel(Index1D index, ArrayView<float> a, ArrayView<float> b, ArrayView<float> c)
{
c[index] = a[index] + b[index];
}The demo application is located in SpawnDev.ILGPU.WebGPU.Demo and showcases:
- GPU compute tasks running in Blazor WebAssembly
- Interactive Mandelbrot renderer
- Comprehensive unit tests at
/tests
cd SpawnDev.ILGPU.WebGPU.Demo
dotnet runNavigate to https://localhost:5181 in a WebGPU-capable browser (Chrome, Edge, or Firefox Nightly).
Start the demo app and navigate to /tests to run the unit test suite.
# Windows
_test.bat
# Linux/macOS
./_test.shThe PlaywrightTestRunner runs tests in a headless browser. To view the browser during tests, uncomment Environment.SetEnvironmentVariable("HEADED", "1"); in PlaywrightTestRunner/GlobalSetup.cs.
WebGPU is required. Supported browsers:
- Chrome 113+
- Edge 113+
- Firefox Nightly (with
dom.webgpu.enabledflag)
- Some advanced ILGPU features may not yet be supported
- Subgroups extension not available in all browsers
- Dynamic shared memory requires Pipeline Overridable Constants (not yet implemented)
In Blazor WebAssembly, the main thread cannot block. Use SynchronizeAsync() instead of Synchronize():
// ❌ Don't use - non-blocking in Blazor WASM
accelerator.Synchronize();
// ✅ Use async version
await accelerator.SynchronizeAsync();The standard Synchronize() method will log a warning and return immediately without waiting.
This project is licensed under the same terms as ILGPU. See LICENSE for details.