https://github.com/lostbeard/spawndev.ilgpu.webgpu
WebGPU backend for ILGPU, enabling GPU compute in Blazor WebAssembly.
https://github.com/lostbeard/spawndev.ilgpu.webgpu
blazor blazor-webassembly gpgpu-computing ilgpu webgpu
Last synced: 19 days ago
JSON representation
WebGPU backend for ILGPU, enabling GPU compute in Blazor WebAssembly.
- Host: GitHub
- URL: https://github.com/lostbeard/spawndev.ilgpu.webgpu
- Owner: LostBeard
- License: mit
- Created: 2026-02-02T02:16:42.000Z (26 days ago)
- Default Branch: master
- Last Pushed: 2026-02-07T05:07:52.000Z (21 days ago)
- Last Synced: 2026-02-07T08:18:57.275Z (20 days ago)
- Topics: blazor, blazor-webassembly, gpgpu-computing, ilgpu, webgpu
- Language: C#
- Homepage: https://lostbeard.github.io/SpawnDev.ILGPU.WebGPU/
- Size: 63.6 MB
- Stars: 6
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# SpawnDev.ILGPU.WebGPU
[](https://www.nuget.org/packages/SpawnDev.ILGPU.WebGPU)
**Run [ILGPU](https://github.com/m4rs-mt/ILGPU) kernels directly in the browser using WebGPU!**
Write GPU compute shaders in C# and compile them to WGSL automatically.
## Features
- **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)
- **64-bit Emulation** - Support for `double` (f64) and `long` (i64) types via emulated WGSL logic
- **Blazor WebAssembly** - Seamless integration via [SpawnDev.BlazorJS](https://github.com/LostBeard/SpawnDev.BlazorJS)
- **Shared memory & atomics** - Supports static and dynamic workgroup shared memory, barriers, and atomic operations
- **No native dependencies** - Entirely written in C#
## Installation
```bash
dotnet add package SpawnDev.ILGPU.WebGPU
```
## Quick Start
```csharp
using 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(length);
// Load and execute kernel
var kernel = accelerator.LoadAutoGroupedStreamKernel, ArrayView, ArrayView>(VectorAddKernel);
kernel((Index1D)length, bufA.View, bufB.View, bufC.View);
// Wait for GPU to complete (async required in Blazor WASM)
await accelerator.SynchronizeAsync();
// Read back the results
var results = await bufC.CopyToHostAsync();
// Define the kernel
static void VectorAddKernel(Index1D index, ArrayView a, ArrayView b, ArrayView c)
{
c[index] = a[index] + b[index];
}
```
## Demo Application
The demo application is located in [SpawnDev.ILGPU.WebGPU.Demo](SpawnDev.ILGPU.WebGPU.Demo) and showcases:
- GPU compute tasks running in Blazor WebAssembly
- Interactive Mandelbrot renderer
- Comprehensive unit tests at `/tests`
- View [Live Demo](https://lostbeard.github.io/SpawnDev.ILGPU.WebGPU/)
### Running the Demo
```bash
cd SpawnDev.ILGPU.WebGPU.Demo
dotnet run
```
Navigate to `https://localhost:5181` in a WebGPU-capable browser (Chrome, Edge, or Firefox Nightly).
## Testing
### Browser Tests
Start the demo app and navigate to `/tests` to run the unit test suite.
### Automated Tests (Playwright)
```bash
# Windows
_test.bat
# Linux/macOS
./_test.sh
```
The PlaywrightTestRunner runs tests in a headless browser. To view the browser during tests, uncomment `Environment.SetEnvironmentVariable("HEADED", "1");` in `PlaywrightTestRunner/GlobalSetup.cs`.
## Test Coverage
**95 tests** covering all core ILGPU features supported by WebGPU.
### Coverage by Area
| Area | What's Tested | Status |
|------|---------------|--------|
| **Memory** | Allocation, transfer, copy, views | ✅ Complete |
| **Indexing** | 1D, 2D, 3D kernels, boundary conditions | ✅ Complete |
| **Arithmetic** | +, -, *, /, %, negation, complex expressions | ✅ Complete |
| **Bitwise** | AND, OR, XOR, NOT, shifts (<<, >>) | ✅ Complete |
| **Math Functions** | sin, cos, tan, exp, log, sqrt, pow, abs, min, max | ✅ Complete |
| **Trigonometric** | sin, cos, tan, asin, acos, atan, sinh, cosh, tanh | ✅ Complete |
| **Atomics** | Add, Min, Max, CompareExchange, Xor | ✅ Complete |
| **Control Flow** | if/else, loops, nested, short-circuit | ✅ Complete |
| **Structs** | Simple, nested, with arrays | ✅ Complete |
| **Type Casting** | float↔int, uint, mixed precision | ✅ Complete |
| **64-bit Emulation** | Support for `double` and `long` via Software Emulation | ✅ Complete |
| **GPU Patterns** | Stencil, reduction, matrix multiply, lerp, smoothstep | ✅ Complete |
| **Shared Memory** | Static and Dynamic workgroup memory, length extraction | ✅ Complete |
| **Synchronization** | Barriers, atomic reduction | ✅ Complete |
| **Special Values** | NaN, Infinity detection | ✅ Complete |
| **Scalability** | 65K+ elements, 1M element stress test | ✅ Complete |
### Not Supported (Hardware/Spec Limitations)
| Feature | Reason |
|---------|--------|
| **Subgroups/Warps** | Browser WebGPU extension not available |
## Browser Requirements
WebGPU is required. Supported browsers:
- Chrome 113+
- Edge 113+
- Firefox Nightly (with `dom.webgpu.enabled` flag)
## Configuration
### 64-bit Emulation
WebGPU hardware typically only supports 32-bit float and integer operations. SpawnDev.ILGPU.WebGPU provides software emulation for 64-bit types (`double`/f64 and `long`/i64) via the `WebGPUBackendOptions` configuration.
**Configure when creating the accelerator:**
```csharp
using SpawnDev.ILGPU.WebGPU.Backend;
// Create options with 64-bit emulation enabled
var options = new WebGPUBackendOptions { EnableF64Emulation = true };
// Pass options when creating the accelerator
using var accelerator = await device.CreateAcceleratorAsync(context, options);
```
Or use the context extension method:
```csharp
var options = new WebGPUBackendOptions { EnableF64Emulation = true };
using var accelerator = await context.CreateWebGPUAcceleratorAsync(0, options);
```
**Available Options:**
| Option | Default | Description |
|--------|---------|-------------|
| `EnableF64Emulation` | `false` | Enable 64-bit float (`double`) emulation via `vec2` |
| `EnableI64Emulation` | `false` | Enable 64-bit integer (`long`) emulation via `vec2` |
> **Note:** Use `WebGPUBackendOptions` when creating an accelerator to configure 64-bit emulation per-instance.
## Known Limitations
- Subgroups extension not available in all browsers
- **Deep Zoom Panning**: At extreme zoom levels (beyond ~10^6), panning may feel sluggish due to floating-point precision limitations. This is inherent to 64-bit double arithmetic and affects all Mandelbrot viewers. Advanced implementations use perturbation theory or arbitrary precision arithmetic to overcome this.
## Async Synchronization
In Blazor WebAssembly, the main thread cannot block. Use `SynchronizeAsync()` instead of `Synchronize()`:
```csharp
// ❌ 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.
## Blazor WebAssembly Configuration
When publishing your Blazor WebAssembly application, specific MSBuild properties are required in your `.csproj` to ensure ILGPU functions correctly in a production environment:
```xml
false
false
```
### Why are these required?
- **`PublishTrimmed = false`**: ILGPU uses reflection-like techniques to extract information from kernel methods at runtime. The standard .NET IL Linker (trimmer) may remove essential methods or metadata if it cannot statically determine their usage, leading to `MissingMethodException`.
- **`RunAOTCompilation = false`**: The ILGPU frontend performs dynamic analysis of IL code which is currently incompatible with Blazor WebAssembly AOT compilation.
## License
This project is licensed under the same terms as ILGPU. See [LICENSE](LICENSE) for details.
## Resources
- [ILGPU Documentation](https://ilgpu.net/)
- [WebGPU Specification](https://www.w3.org/TR/webgpu/)
- [SpawnDev.BlazorJS](https://github.com/LostBeard/SpawnDev.BlazorJS)