ILGPU in the Browser: High-Performance GPU Computing with Blazor WebAssembly
High-performance computing on the web has traditionally been limited to JavaScript or specialized C++ via WebAssembly. But what if you could write high-level C# kernels and run them directly on the GPU from your Blazor WASM app?
Iβve been working on SpawnDev.ILGPU, a library that brings the power of ILGPU to the web. It allows you to write C# code that compiles to shaders or runs across multiple web workers, giving Blazor developers a path to massive parallelism.
π The Four Execution Backends
SpawnDev.ILGPU provides a custom implementation for ILGPU that targets the browser's unique capabilities. It offers four distinct backends, allowing you to scale performance and compatibility:
- WebGPU Backend (Fastest): Uses C# kernels transpiled directly to WGSL for hardware-accelerated GPU compute.
- Wasm Backend (2nd Fastest): Leverages multiple Web Workers and C# transpiled to WebAssembly for high-speed parallel processing.
- Workers Backend: Utilizes multiple Web Workers with C# transpiled to JavaScript for broad parallel compatibility.
- CPU Backend: Runs the IL code synchronously on the Blazor WASM main thread. This is a blocking execution path intended for simple tasks or environments where workers aren't available.
By using SpawnDev.BlazorJS for the underlying JavaScript interop, the library handles the heavy lifting of memory management and kernel dispatching between .NET and the browser APIs.
π From Fractals to Distributed Compute
To showcase the library, I built a Fractal Explorer that renders the Mandelbrot set. By offloading the math to the GPU via the WebGPU backend, it achieves real-time zoom levels that standard Blazor loops simply cannot touch.
Iβm also applying this to:
- On-the-fly 2D to 3D video conversion: Using ILGPU kernels for temporal blending of depth maps.
- Compute Groups: A distributed "compute group" concept using WebRTC to connect multiple devices into a cluster directly in the browser.
π Check it out
This project is open source and Iβd love to hear from other .NET developers interested in high-performance web tech.
- GitHub: LostBeard/SpawnDev.ILGPU
- NuGet: SpawnDev.ILGPU
#dotnet #blazor #webgpu #programming #csharp

Top comments (0)