DEV Community

Cover image for C# Kernels in the Browser: High-Performance GPU Computing with Blazor and WebGPU
Todd Tanner
Todd Tanner

Posted on

C# Kernels in the Browser: High-Performance GPU Computing with Blazor and WebGPU

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:

  1. WebGPU Backend (Fastest): Uses C# kernels transpiled directly to WGSL for hardware-accelerated GPU compute.
  2. Wasm Backend (2nd Fastest): Leverages multiple Web Workers and C# transpiled to WebAssembly for high-speed parallel processing.
  3. Workers Backend: Utilizes multiple Web Workers with C# transpiled to JavaScript for broad parallel compatibility.
  4. 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.

Mandelbrot Fractal Explorer rendered with SpawnDev.ILGPU

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.

#dotnet #blazor #webgpu #programming #csharp

Top comments (0)