Simulating epidemics in WebGPU
A few days ago, I simulated an epidemic with WebGL, using a texture for the state of a cellular automaton. However, I got some pretty weird effects. For example, the number of infected people never dropped to zero, despite the math clearly demanding that it should drop to zero.
I concluded that these weird effects were due to my sin of a texture to keep the state of the cells.
texture2d function lets you sample the color of a texture at a specific coordinate.
Not a pixel; a coordinate.
It does some clever stuff,
like mipmapping, “level of detail”, and interpolation.
For a cellular automaton,
I need a lower-level API,
to just access the value at a specific pixel in the texture.
As an extreme solution,
I’ve rewritten the epidemic simulation in WebGPU.
Earlier, I implemented Game of Life in WebGPU,
and I modified that for my epidemic simulation.
The API is much more pleasant:
instead of the voodoo
texture2d function to access pixels of a texture,
I can instead directly index into an array of cells.
Gladly, the weird effects are not present in the WebGPU implementation!
Below you should see the simulation -- but probably only if you’re running Google Chrome Canary, and you’ve enabled the “Unsafe WebGPU” experiment.