π WebGPU the graphics card, as a general-purpose computer
Our WebGL cube used the GPU to draw. WebGPU β its successor β also lets pages use the GPU to compute: thousands of parallel cores running your kernel. This demo squares sixteen million numbers on the GPU and races the CPU doing the same, one at a time.
| Time | Spot-check: x[1,000,000]Β² | |
|---|---|---|
| CPU (one JS loop) | β | β |
| GPU (compute shader) | β | β |
The entire GPU program β WGSL, a shading language, inline in this page:
@group(0) @binding(0) var<storage, read_write> data: array<f32>;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) id: vec3u) {
data[id.x] = data[id.x] * data[id.x];
}