All projects

Enjambre

Artificial life on the GPU: thousands of particles that organize themselves.

Download for Linux 0.1.1
Other versions

Linux x86_64 · 23 MB · SHA-256Windows x86_64 · 7 MB · SHA-256release notes

See the repository

Filled attracts, hollow repels. Tap a cell: the row receives, the column exerts.

Thousands of colored particles that organize themselves into swarms, cells, rings and traveling waves from simple rules of attraction and repulsion. Nobody programs the patterns: they emerge. Underneath there are two different engines, eight shaders and a vertical video recorder.

Like plankton on the silk ribbon towed by research ships: scroll down the page and you drag it along.

The rule, and what comes out of it

Each particle has a hue on the color wheel and feels a force toward the others that depends on that hue and on distance. The whole configuration fits in a force matrix: who attracts whom, and how much.

Out of that come swarms, cells with a membrane, chains and traveling waves. None of those patterns is programmed —there is no code that draws a cell—, and that is exactly the point of the experiment.

Up close, they all repel: they do not pile up.Positive coefficient: attracts.Negative coefficient: repels.

Two engines for the same model

14,878 lines of Rust in 4 crates and 46 commits.

The sim crate is the CPU path: macroquad for rendering and rayon to parallelize the force computation between particles. It is the reference implementation, the one that says what the correct behavior is.

The sim-gpu crate is the rewrite on wgpu 24 and winit 0.30, with the whole simulation in compute shaders. That is where the number of particles stops being the limit. panel is the control interface in eframe/egui —a glass sheet with a fixed header and four tabs: Simulation, Look, Show and Output—, a separate process that talks to the simulation over IPC, and shared holds what is common: the model, the protocol, the audio and the video.

Keeping both paths is not indecision: the CPU one is the standard the GPU one is verified against.

The ribbon on this page runs that same CPU step, ported to JavaScript. The native program reaches tens of thousands of particles on the GPU.

The eight shaders

sim.wgsl
computes the simulation step
particles.wgsl
draws the result
grid.wgsl
solves the spatial neighborhood; without it the computation would be quadratic
params.wgsl
shares the configuration with the GPU
color.wgsl
turns hue into color
photo.wgsl
projects an image onto the particle field
post.wgsl
finishes with post-processing
glass.wgsl
blurs the scene behind the panel, outside the recording

Everything is written by hand in WGSL against wgpu, with bytemuck to pass structures between Rust and the GPU without odd copies and pollster to resolve the asynchronous initialization.

Music and video, with no native dependencies

The swarm reacts to music: shared/src/music.rs and shared/src/audio.rs do the analysis with realfft, a pure-Rust FFT —spectral flux, beat detection and frequency bands—. The choice is deliberate: it links nothing native, so it does not repeat the conflict with ALSA that ruled out cpal.

shared/src/video.rs decodes video by streaming, launching ffmpeg as a subprocess and consuming RGBA frames with backpressure: if the consumer falls behind, ffmpeg pauses. The recording comes out in 9:16 at 1080×1920, meant to be published as is.

Colored rosette structures generated by Enjambre in vertical format
Emergent rosettes: a crop from a recording of the program.

Stack

  • Rust
  • wgpu 24
  • WGSL
  • rayon
  • egui / eframe
  • realfft
  • ffmpeg

Let’s talk

Curious how something here is solved, or have a similar problem?