Skip to content

Compute & Particles

A hundred thousand particles moved entirely by the GPU, with no per-particle work on the CPU at all.

What you should come away with: What a compute shader is for, and the kind of problem that leaves WebGL behind entirely.

Assumes: Coordinate Spaces. It will still make sense without it, but that one comes first.

Why this API: WebGL has no compute shaders. This lab cannot be built on it — not slowly, not with a workaround. It is the clearest case for choosing WebGPU.

Checking for WebGPU…

What the GPU is doing

particles
60,000
workgroups
938
invocations
60,032
bytes from CPU
48

938 workgroups of 64, dispatched once per frame, each invocation owning one particle. The equivalent in WebGL means encoding positions into a floating-point texture, stepping them in a fragment shader, ping-ponging between two framebuffers and reading them back as vertices — a well-known trick, and a workaround for a missing stage rather than a way of expressing the problem.

Where that leaves you

You can now say what a compute shader is handed — an index and a buffer, with no vertex to place and no pixel to colour — and work out from a particle count how many workgroups a dispatch needs and why the shader still has to check the count itself.

This did not teach you what happens when invocations have to talk to one another. Every particle here reads and writes its own slot and nobody else’s, so workgroup memory, barriers and atomics never come up — and they are where compute actually gets hard, in a sort or a collision grid rather than in a field of independent particles.