Wasm Demos
Six programs written by hand in WebAssembly's assembly language and built with AsmSdk, running in this page. They use one core and no GPU, and you install nothing.
AsmSdk built these modules from
samples/WasmDemos (see
WebAssembly).
| Demo | What it does | Module |
|---|---|---|
| Life, 16 million cells | Conway's Life on a 4096 by 4096 board, 128 cells per instruction | life.wasm, 3.3 KB |
| Ray-marched scene | spheres and soft shadows, four rays per instruction, refining as you watch | ray.wasm, 4.9 KB |
| Particle swarm | 120,000, 240,000 or 500,000 particles following your pointer, four per instruction | swarm.wasm, 2.8 KB |
| Galaxy collision | two spiral galaxies, every star pulling on every other, one body against four per instruction | galaxy.wasm, 4.9 KB |
| Cloth you can tear | 128 by 80 points held by 20,352 springs, four springs per instruction, drawn by a rasterizer in the module | cloth.wasm, 7.6 KB |
| Fluid you stir | Stam's stable fluids on 480 by 270 cells with dye you pour, four cells per instruction | fluid.wasm, 7.0 KB |
Only WebAssembly
Each demo is a single .wasm.s file. The module steps its simulation, draws every pixel of its
frame, chooses its colours and decides what the pointer does. Sine and cosine are written there as
polynomials, because WebAssembly has no instruction for them. The page's script,
demo.js, does only what
a browser allows nothing else to do. It starts the module, passes it the pointer, the wheel, the
buttons and the theme, and copies its frame to the canvas.
All six modules share one interface, so the same script starts any of them:
| Export | What it does |
|---|---|
demo_init(width, height) |
sets the demo up and returns the address of its RGBA frame in the module's memory |
demo_theme(dark) |
chooses the light or dark colours (this site follows your system, or its theme switch) |
demo_pointer(x, y, buttons), demo_wheel(x, y, delta) |
the pointer and the wheel, in frame pixels |
demo_command(command) |
the controls under the demo: a button's number, or a choice's value (the swarm's particle count and the galaxy's stars are the count itself) |
demo_frame() |
one step and one drawn frame; returns the work done, for the figures |
The figures under each demo come from your browser. The script times demo_frame alone, on the
page's single thread, which is one core.
How they are checked
check.mjs runs
each module under Node and compares it with a reference written in JavaScript:
- Life: three generations of the whole board match Conway's rule cell by cell, including the edges that wrap around. A glider crosses the corner of the board, and drawing, panning and zoom are checked too.
- Particle swarm: one step of all 120,000 particles is compared, in single precision and in the module's order of operations. The two agree bit for bit, and again at 240,000 and 500,000 particles, where the check also confirms that the swarm stays, the newcomers start on the ring and each frame adds the density the count calls for.
- Ray-marched scene: the finished picture is compared, pixel by pixel, with a reference renderer that repeats every operation in single precision. They agree bit for bit, after a camera turn, a zoom, and in both themes as well.
- Galaxy collision: one step of all 2,056 bodies matches a single-precision reference bit for bit, and so does the first frame's drawing, pixel by pixel. The checks also cover the two disks each encounter sets up, the star counts, the view's drag and zoom, and the black holes.
- Cloth you can tear: a frame of the hanging sheet and a frame on the ball match the reference bit for bit, every coordinate and every spring. Pulling, cutting, tearing by itself, the wind, gravity and the drawing are checked too.
- Fluid you stir: frames match the reference bit for bit in every field, the dye and the drawing: a burst, strokes with either button, each viscosity, no curl and wild curl, and each set of colours, on the light theme and the dark. The brush, pause, clear and the fluid stirring itself are checked too.
AsmSdk's tests run the checks on every build. They also check that the modules this site serves are the same, byte for byte, as a fresh build of the samples.