Menu

1 / 3

Your first GPU kernel in C#, in the browser and on the desktop

DEV Community: blazor·Todd Tanner·10 days ago
#mwA2Fpdt
#devtoblazor#devto#dev#app#kernel
Long article (1855 words) — no AI summary yet.
Reading 0:00
15s threshold

Live demo | Full example on GitHub

Some work is the same small calculation, repeated for every pixel or every item. A CPU does those a few at a time. A GPU does thousands at once.

Using the GPU usually means learning a second language for shaders, and a different one for the browser than for the desktop. With SpawnDev.ILGPU you write the calculation as a C# method. The same file runs in a Blazor app and in a desktop app.

We will draw a fractal, the Mandelbrot set, on the page and on the GPU, then run the same code in a console app. You need the .NET 10 SDK and a browser.

The kernel

A kernel is a method the GPU runs once for every item, in our case every pixel:

using ILGPU ; using ILGPU . Runtime ;

namespace GpuKernel ;

public static class Mandelbrot { /// <summary>The kernel: the GPU runs this once for every pixel.</summary> public static void Kernel ( Index2D pixel , ArrayView2D < uint , Stride2D . DenseX > image , int maxSteps ) => image [ pixel ] = Color ( pixel . X , pixel . Y , ( int ) image .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.