/gridded_automata

A library for visualizing discrete 2D cellular automata using compute shaders

Primary LanguageRust

Gridded Automata

This is a library for rendering discrete cellular automata using compute shaders. Users provide a state function (written in WGSL) that returns a cell’s new state based on its current state and that of its neighbors. The simulation speed, the way cells are colored, and the type of neighborhood can be tweaked through config options.

Automata cell data can be read from PGM image files, which can be in ASCII or binary format (P2 & P5 as described here). Data can also be read from any other type of image if the library’s from_image feature is enabled. Note that colors are assigned values in the order they are encountered. For this reason, it’s best to provide a "color legend" in the image’s topmost row, from left-to-right.

Examples

A number of classic automata are included as binaries, intended to showcase the library.
Run them with

cargo run --bin [name] [[args]]
Binaries
Name Arguments Description

cgol

Conway’s Game of Life

ww

Name of PGM file

Wire World

lant

Langton’s Ant

seeds

Seeds

bb

Brian’s Brain

Neighborhoods

The library distinguishes between two types of neighborhoods, Moore and Von-Neumann. These are represented by the same WGSL data type; all the same functions apply to them.

The State Function

Each generation, a 'state' function is called on each cell in the simulation. Here’s the one in seeds.wgsl:

fn main(neighborhood: Neighborhood, state: u32) -> u32 {
    if state == 0u && living(neighborhood) == 2u {
        return 1u;
    }

    return 0u;
}

All state functions must have this method signature. When compiling the compute shader, this function is placed after a header that contains a number of helper functions.

Here’s some of them…​
living(Neighborhood) → u32

Returns the number of non-zero cells in the neighborhood.

matching(Neighborhood, u32) → u32

Returns the number of cells with the given state in the neighborhood.

up(Neighborhood) → u32

Returns the cell above the current cell.

left(Neighborhood) → u32

Returns the cell to the left of the current cell.

right(Neighborhood) → u32

Returns the cell to the right of the current cell.

down(Neighborhood) → u32

Returns the cell beneath the current cell.

Images