All projects

Real water, computed on the graphics card

Flow

A water simulation in the browser: up to 200,000 particles computed with MLS⁠-⁠MPM in WebGPU compute shaders and drawn as clear water. You can stir it, turn the tank, tilt your phone and open the hood to see the grid, the depth, the normals and the cost of every pass.

Role
Idea, physics, shaders, renderer, interface and measurements.
Stack
  • TypeScript
  • WebGPU
  • WGSL
  • WebGL2
  • React 19
  • Vite
  • shadcn/ui
  • Motion
  • Vitest
Live
flow-oktawian.vercel.appgithub.com/mauupa0/flow-mpm-fluid
Flow website

Problem

I wanted to see whether a browser can compute a real fluid, not an effect that imitates one, and prove it with numbers. On top of that the water has to look like water and leave the battery alone when it is still.

What I built

  • Seven compute passes per step: particles hand mass and momentum to a fixed grid, the grid solves pressure and walls, particles gather velocity back. No neighbour search, so the work per particle is constant.
  • Integer sums at a 2^18 scale, because WebGPU only has integer atomics. The result does not depend on thread order, so a step is repeatable.
  • A renderer without a mesh: sphere depth, a bilateral filter, thickness, then one pass with refraction, reflection and dye absorption. The tank and the table are computed in the shader, so the real ruler shows through the water.
  • The same algorithm in TypeScript as a CPU reference, and a WebGL2 fallback for browsers without WebGPU.
  • The page times its own GPU and picks 50k, 120k or 200k particles, and when the water is still it draws no frames at all.

By the numbers

particles at 144 fps on desktop
197,064
per physics step at 197k particles
0.89 ms
deliberately broken kernels caught by the test
9/9
browser checks, including a lost GPU device
54/54

Decisions that mattered

MLS⁠-⁠MPM over SPH

SPH looks up every particle's neighbours every step, and on a GPU that is the slow part. In MLS⁠-⁠MPM particles talk through a fixed grid and always touch 27 cells, so cost grows in a straight line and the benchmark explains it pass by pass.

A second engine to disagree with

A TypeScript reference computes the same particles as the GPU. After 48 steps the mean position difference is 4.1e-6 cells. Nine versions of the shader with one deliberate bug each fail the same test by a margin of at least 250 times.

Honest about chaos

After 3,000 steps individual CPU and GPU particles drift apart, because the flow is chaotic and the rounding order differs. The page says so and compares kinetic energy instead, which agrees to four digits for 2,000 steps.