Stable fluids: blowing smoke on the GPU
Mon 23 Nov 2026, 03:21
A flame is the easiest fluid to fake and the hardest to make right. Fakes look like fakes because real smoke is incompressible: it can swirl, fold, and billow, but it can never pile density up out of nowhere, and it can never create a void. You can paint those shapes; you can't fake the pressure that produces them. So I implemented Jos Stam's 1999 "stable fluids" scheme — the algorithm behind a decade of game smoke — first in plain C to get the hero image, then as a set of GPU fragment shaders that run live.
The trick is a split. Each step I do three things.
- Advect. Bill every quantity (the velocity itself, and the dye that shows it) forward along the current velocity field. The standard way is semi-Lagrangian: for each cell, walk backwards along the velocity and sample bilinearly from where the stuff used to be. This is unconditionally stable — you can take huge time steps and the fluid just smears, it never explodes — which is Stam's "stable."
- Add forces. Buoyancy pushes the dye upward (
v += dt * buoy * density), and vorticity confinement re-injects the curl that the numerics keep grinding away. You compute the vorticityw = ∂v/∂x − ∂u/∂y, find the gradient of|w|, and push the velocity along it so eddies tighten instead of damping out. That's what makes smoke curl into billows instead of rising as a lazy column. - Project. Subtract the gradient of a pressure field solved from the velocity's divergence, so the result is divergence-free. This is the incompressibility, and it's the part that turns a straight jet of "smoke" into rolling, pairing vortices. I solve the Poisson equation by ~30 Jacobi iterations per frame.
A smoke plume: a short curling jet, then a tall turbulent column, then dye pooling under the closed ceiling
That last panel is the honest caveat: the domain is a closed box, so at high iteration counts the dye drifts up and pools against the top boundary — smoke in a sealed room, not an open sky. The C reference reflects the velocity at the walls (negate the normal component), which is what makes the column bounce and spread under the ceiling.
The live viewer puts velocity and dye in RGBA16F float textures and runs all nine passes as fragment shaders: curl, vorticity, buoyancy, divergence, a 30-tap Jacobi pressure solve, gradient-subtract, advect-velocity, advect-dye, and a tonemapped display (Reinhard x/(1+x) + gamma). Drag on the canvas to inject velocity and dye under your cursor and blow the smoke around; the sliders move buoyancy, vorticity, and the dissipation rates. It's the same equations as the C file, just spread across framebuffers, the pressure solver as a Jacobi sweep, and the boundary handling folded into the divergence shader's clamped edge samples.
A curling column of amber smoke rising against a dark field, breaking into vortices
The single most satisfying thing was seeing vorticity confinement pay for itself. Turn VORT to zero and the plume rises as a smooth, slightly boring cone. Turn it up and the column tears itself into that characteristic rolling, pairing, Kelvin-Helmholtz texture. The math for it is seven lines; the look of smoke is mostly those seven lines.