Fluid you stir
Jos Stam's stable fluids on a grid of 480 by 270 cells, drawn at 960 by 540. You stir it, pour dye into it and choose how thick it is. Each WebAssembly instruction of the stencil passes updates four cells.
Left alone for two seconds, the fluid stirs itself with bursts of dye. Honey smooths the flow into slow spirals, and wild curl feeds the small eddies.
The kernel
Every frame follows Stam's method:
- The stroke: the pointer's stroke pulls the velocity under the brush toward its own and pours dye.
- Viscosity, when chosen: the velocity diffuses implicitly, by Jacobi sweeps that stay stable however thick the fluid is.
- Vorticity confinement: the curl of each cell, and a push round each swirl's centre. It feeds back the eddies that a coarse grid smears out.
- The projection makes the flow incompressible: the divergence, then 30 Jacobi sweeps of the pressure, whose gradient is taken from the velocity.
- Advection: every cell is traced back along its velocity, and the velocity and the dye are sampled where the trace lands. This semi-Lagrangian step is stable at any speed.
The stencil passes (the curl, the divergence, the sweeps and the gradient) update four cells per
f32x4 instruction. Advection traces four cells at once too, then gathers each cell's samples:
u and v together with two 64-bit loads per row of the sample, and the dye's red, green and blue as
one f32x4. The module draws the frame at twice the grid, with even pixels on cells and odd ones
halfway between two. On either theme, dye whose strongest channel passes 255 is scaled back to it,
so heavy dye keeps its hue instead of turning white.
This is one pressure sweep for four cells:
local.get 2
local.get 1
i32.add
local.get 2
local.get 0
i32.add
local.tee 5
v128.load 1932 // left
local.get 5
v128.load 1940 // right
f32x4.add
local.get 5
v128.load 0 // above
local.get 5
v128.load 3872 // below
f32x4.add
f32x4.add
local.get 2
v128.load 3161600 // div
f32x4.sub
SPLAT 0.25
f32x4.mul
v128.store 0
The whole kernel, with the strokes, the drawing and the controls, is
fluid.wasm.s.
Under Node on an i9-13900K, a frame takes about 5.3 ms. Advection takes about 2 ms. The 30
pressure sweeps take about 1.3 ms, 45 µs a sweep, which is nearly 3 billion cell updates a second.
Drawing takes about 1.5 ms, and finding each cell's strongest channel and scaling it is 1 ms of
that. The figures under the demo are your browser's.
Build one yourself
dotnet new asmwasm -n Kernel # a WebAssembly library in LLVM's WebAssembly assembly
dotnet build Kernel # Kernel/bin/Debug/freestanding/browser-wasm/Kernel.wasm
node Kernel/run.mjs # calls it from Node; a page loads it the same way
The WebAssembly guide covers the language, the FUNCTION macros and the
traps. One trap this kernel met: a load's offset cannot be negative, so the row above a cell, 1,936
bytes back, cannot be an offset from the cell's own address. The loops keep one pointer per cell,
the label plus the cell's offset, and reach every field at a constant offset from it (u at
+2,048, v at +528,640). The fields start 2,048 bytes after their label, so even the row above is a
positive offset.