Tutorials / Community / Getting started

★ Staff pickIntermediate⏱ 40 minTallow 0.2

Build Conway's Game of Life with Compute Shader Graphs

Difficulty: Beginner–intermediate. Time: about 40 minutes. You need: Tallow 0.2 (this was checked step by step with v0.2.15-beta) and a graphics card that can run compute shaders (Vulkan, Direct3D 11/12 or Metal).

In this tutorial you build Conway's Game of Life with Tallow's Compute Graph, without writing shader code. Each pixel of a 256 × 256 texture is a cell: white is alive, black is dead. Every step, the graphics card works out the next generation of all 65,536 cells at once by looking at the one before.

The finished graph. The Preview in the corner is the simulation running.
The finished graph. The Preview in the corner is the simulation running.

What you'll learn: how a compute graph feeds its own result back in (Previous), how to count neighbours, how to use colour channels as plain numbers, how to build if/else logic out of Step nodes, and how to run the result on an object in your game.

Step 1: Create the Compute Graph

  1. Open your project in the Tallow editor.
  2. In the Project panel, click + and choose Compute Graph (visual compute shader).
  3. Name it ConwaysLife. It opens in the Graph tab.
  4. A new compute graph starts with an example (drifting ink trails). Click each example node and press Delete, until only Compute Output is left.

Compute Output → Result is the new colour of every pixel. Whatever you connect to it runs once per pixel, every step.

How the graph editor works (you'll do these four things the whole way through):

  • Add a node: right-click an empty part of the canvas and type its name, then pick it from the list.
  • Connect: drag from a dot on the right of a node (an output) to a dot on the left of another (an input). Alt+click a dot to disconnect it.
  • Type a value: click a node. Its inputs appear in a small panel at the top left of the canvas; type the value there (for example a Step's Edge).
  • Save: the Save button above the canvas saves and compiles the graph. The Preview in the bottom right corner, and the small picture inside each node, then show what it does.

Right-click the canvas and type to find a node.
Right-click the canvas and type to find a node.

Click a node to type its values in the panel at the top left.
Click a node to type its values in the panel at the top left.

The dots are coloured by what they carry, and only matching colours connect: green is a number, yellow a 3D vector, orange a 2D vector, pink a colour.

Step 2: Understand the rules

A cell's next state depends on whether it is alive now and how many of its eight neighbours are alive:

  • A living cell with two or three living neighbours stays alive.
  • A dead cell with exactly three living neighbours comes alive.
  • Every other cell is dead in the next generation.

Dead is 0 and alive is 1, kept in the texture's red channel. Written as one line:

next = (neighbours == 3) OR (alive AND neighbours == 2)

Shaders have no if in a graph, so you'll build this from Step (is a number at least this much?), Subtract, Multiply and Add.

The plan: first put random cells on the screen (Step 3), so you can see something. Then build the rules next to them (Steps 4 to 6), and finally add a switch between "random cells" and "the rules" (Step 7).

Step 3: Show random cells

This builds the chain that ends at Compute Output first. It is the part people find hardest, so it comes while the graph is still small. Lay these nodes out in a row along the bottom of the canvas, leaving a gap of about two nodes' width after the Step: in Step 7 a Blend node goes there.

Add these six nodes:

  • Pixel (Compute Input): where this pixel is, in whole pixels.
  • Noise (Shader Effects): a random number from 0 to 1 for each position.
  • Step (Shader Math): 1 if Value is at least Edge, otherwise 0.
  • Vector (Shader Values): makes one 3D vector from three numbers.
  • Color From Parts (Shader Values): makes a colour from an RGB vector and an alpha number.
  • Compute Output (you already have it).

Connect them like this:

FromToWhy
PixelNoise → UVEach cell gets its own random number.
NoiseStep → ValueClick the Step and set Edge to 0.76: about 24% of the cells start alive.
StepVector → X, Y and ZDrag from Step's output three times, once to each. The same number in all three makes red, green and blue all 0 (black) or all 1 (white).
VectorColor From Parts → RGBYellow dot to yellow dot. Then click Color From Parts and set Alpha to 1 (fully opaque).
Color From PartsCompute Output → ResultPink dot to pink dot.

Press Save. The Preview fills with random white dots on black, and stays still: for now every step just draws the same random cells again.

Step 3 done: Pixel → Noise → Step (Edge 0.76) → Vector (X, Y and Z) → Color From Parts (Alpha 1) → Compute Output, with a gap after the Step.
Step 3 done: Pixel → Noise → Step (Edge 0.76) → Vector (X, Y and Z) → Color From Parts (Alpha 1) → Compute Output, with a gap after the Step.

Why Vector and Color From Parts? Compute Output wants a colour (pink), but Step gives a number (green). A number can't plug into a colour, so you build one: three numbers make a vector (yellow), and a vector plus alpha makes a colour (pink).

Step 4: Read the previous generation

Now the rules, above the row you just made. Add:

  • Previous (Compute Input): this pixel's colour in the last generation.
  • Neighbours Sum (Compute Input): the colours of the eight pixels around it, added up.
  • Two Split Color nodes (Shader Vectors): split a colour into its R, G, B and A numbers.
  1. Connect Previous to the first Split Color's Color. Its R output is now "is this cell alive?" (0 or 1).
  2. Connect Neighbours Sum to the second Split Color's Color. Its R output is now "how many neighbours are alive?" (0 to 8): each living neighbour adds 1 to the red channel.

Press Save. The small pictures in Previous and Neighbours Sum now show the random cells and their neighbour counts (more white means more neighbours).

Step 4: Previous and Neighbours Sum, each into a Split Color.
Step 4: Previous and Neighbours Sum, each into a Split Color.

The graph reads every cell from the last generation and writes the next one into a separate texture, so all cells change at the same moment.

Step 5: Find "exactly two" and "exactly three" neighbours

Add three Step nodes. Connect the R of the Neighbours Sum Split Color to the Value of all three, then click each and set its Edge:

StepEdgeIt gives 1 when the count is
top1.52 or more
middle2.53 or more
bottom3.54 or more

Then add two Subtract nodes (Shader Math):

  • Exactly 2 (top Subtract): top Step to A, middle Step to B. "2 or more" minus "3 or more" is 1 only when the count is exactly 2.
  • Exactly 3 (bottom Subtract): middle Step to A, bottom Step to B. 1 only when the count is exactly 3.

The middle Step's output goes to both Subtracts: drag from it twice.

Step 5: three Steps (Edge 1.5, 2.5, 3.5) into two Subtracts.
Step 5: three Steps (Edge 1.5, 2.5, 3.5) into two Subtracts.

Step 6: Put the rule together

Add a Multiply and an Add (both Shader Math):

  1. The R of the Previous Split Color to Multiply → A.
  2. The Exactly 2 Subtract to Multiply → B. Multiply is now "alive AND exactly two".
  3. The Exactly 3 Subtract to Add → A.
  4. Multiply to Add → B.
next = Exactly3 + (alive × Exactly2)

With exactly three neighbours a cell is alive next time whatever it is now; with exactly two it stays alive only if it is alive now. Both can't be true at once, so the result is always 0 or 1. The Add node's output is the next generation.

Step 6: Multiply (alive × exactly 2) and Add (+ exactly 3).
Step 6: Multiply (alive × exactly 2) and Add (+ exactly 3).

The Preview doesn't change yet: Add isn't connected to anything that reaches Compute Output. That's the next step.

Step 7: Switch between random cells and the rules

You now have two answers for every cell: the random one along the bottom (Step 3) and the rules' one (Add, Step 6). A Blend node chooses between them, and a number called seed says which: 1 means "draw random cells", 0 means "apply the rules". It goes into the gap you left in Step 3.

  1. Add a Number Property (Shader Properties). Click it and set Name to seed and Default to 1. A property is a value you can change later without editing the graph, from the node or from a script.
  2. Add a Blend (Shader Math) in the gap between the noise Step and Vector.
  3. Alt+click Vector's X, Y and Z to take the noise Step off them.
  4. Connect the inputs of Blend:
    • Add (the rules) to Blend → A.
    • The noise Step (Edge 0.76) to Blend → B.
    • Number Property seed to Blend → Amount.
  5. Connect Blend to Vector's X, Y and Z (three drags, as in Step 3).
  6. Press Save.

The end of the graph close up: Blend into X, Y and Z of Vector, Vector into RGB, Color From Parts into Result.
The end of the graph close up: Blend into X, Y and Z of Vector, Vector into RGB, Color From Parts into Result.

Step 7 done: Add → Blend A, noise Step → Blend B, seed → Blend Amount.
Step 7 done: Add → Blend A, noise Step → Blend B, seed → Blend Amount.

How Blend chooses: Blend gives A when Amount is 0 and B when Amount is 1. So seed = 1 gives the random cells (B) and seed = 0 gives the rules (A). Use only 0 or 1: anything between mixes the two and makes grey cells.

Check your graph against the whole picture:

The whole graph. Top: the rules. Bottom: the random cells, the seed switch and the output.
The whole graph. Top: the rules. Bottom: the random cells, the seed switch and the output.

Previous       → Split Color R ───────────────────────────→ Multiply A
Neighbours Sum → Split Color R → Step 1.5 ─┐
                               → Step 2.5 ─┴→ Subtract (exactly 2) → Multiply B
                               → Step 2.5 ─┐
                               → Step 3.5 ─┴→ Subtract (exactly 3) → Add A
                                                         Multiply → Add B
Add (the rules)                        → Blend A
Pixel → Noise → Step 0.76 (random)     → Blend B
Number Property "seed"                 → Blend Amount
Blend → Vector X, Y, Z → Color From Parts RGB (Alpha 1) → Compute Output Result

Step 8: Watch it run

  1. With seed at 1, press Save. The Preview shows still, random cells.
  2. Click the Number Property, change Default to 0 and press Save again.
  3. The cells start following the rules: most die in the first moments, then blinkers, blocks and gliders are left moving around.

The Preview with seed 1 (left: random cells) and a few seconds after setting it to 0 (right: the Game of Life).
The Preview with seed 1 (left: random cells) and a few seconds after setting it to 0 (right: the Game of Life).

The Preview keeps its texture when you save, which is why this works: the random cells made with seed 1 are still there when the rules take over. It runs one generation per frame, so it moves fast. To start again, set seed to 1, Save, set it back to 0 and Save.

Saving also writes ConwaysLife.tcompute next to the graph (the Project panel doesn't show it). That file is what a game uses, in the next step.

Step 9: Show it in your game

  1. Make an object to show it on: GameObject → 3D Object → Cube, and scale it flat, for example Scale 4.5 4.5 0.05. Name it Life Board.
  2. In its Mesh Renderer, tick Unlit so the cells aren't shaded by the light.
  3. Add Component → Compute Effect. Set Shader to ConwaysLife.tcompute, Width and Height to 256, and untick Every Frame: a script decides when it steps, so you can choose the speed.
  4. In the Scripts folder, make a visual script (+ > Visual Script (Graph)) named LifeController and build the graph below.
  5. Add the Life Controller component to Life Board and press Play.

The LifeController graph. Delete the example nodes except On Update, and add two Float variables: stepInterval 0.12 (seconds between generations) and timer 0.

  1. Start: add On Start, then three nodes from Rendering > Compute Effect in a row: Set Float (Name seed, Value 1), Step, and Set Float (Name seed, Value 0). Leave their Compute Effect pins unlinked: that means this object's. This makes generation 0 from random cells, then switches to the rules.
  2. Every frame: after On Update, Set timer to Get timer + Delta Time (an Add).
  3. Then a While Loop (Flow Control) whose Condition is Greater Equal (Float): Get timer ≥ Get stepInterval.
  4. In its Body: Set timer to timer − stepInterval (a Subtract), then Compute Effect: Step.

On Start runs Set Float seed 1, Step, Set Float seed 0; On Update adds Delta Time to timer and, while timer is at least stepInterval, subtracts it and runs Step.
LifeController: seed once, then one generation every stepInterval seconds.

Show the Beef code
using System;
using Tallow;

namespace LifeTest;

[Component]
class LifeController : Script
{
    public float stepInterval = 0.12f;   // seconds between generations

    ComputeEffect mEffect;
    float mTimer;

    public override void Start()
    {
        mEffect = GetComponent<ComputeEffect>();
        if (!mEffect)
            return;

        mEffect.SetFloat("seed", 1);
        mEffect.Step();                  // generation 0: the random cells
        mEffect.SetFloat("seed", 0);     // from now on: the Game of Life rules
    }

    public override void Update(TickInput input, float deltaTime)
    {
        if (!mEffect)
            return;

        mTimer += deltaTime;
        while (mTimer >= stepInterval)
        {
            mTimer -= stepInterval;
            mEffect.Step();              // the GPU works out the next generation
        }
    }
}

The graph does what you did by hand in Step 8: one step with seed at 1, then seed 0 for every step after. It doesn't matter whether you saved the graph with seed at 0 or 1, because the script sets it. In Tallow 0.2 components like Compute Effect are handles, so they are checked with if (!mEffect), not == null.

Playing: the Life Board runs the Game of Life on the GPU, about eight generations a second.
Playing: the Life Board runs the Game of Life on the GPU, about eight generations a second.

Life Board in the Inspector, with the Life Controller.
Life Board in the Inspector, with the Life Controller.

The script only says when to step; every cell and every rule is still worked out on the graphics card.

Troubleshooting

  • The Preview is all black: nothing has made living cells yet. Set seed to 1 and Save, then 0 and Save. Check that Compute Output's Result is connected.
  • Random dots that never move: seed is still 1. Set it to 0 and Save.
  • Grey cells: seed is something other than 0 or 1, a Step edge is wrong (1.5, 2.5, 3.5 and 0.76), or Color From Parts' Alpha isn't 1.
  • Everything dies straight away: the two Subtracts are probably the wrong way round (A and B swapped), or Blend's A and B are swapped.
  • "Those pins have different types": the dot colours don't match, for example a number dragged straight to Compute Output. Go through Vector and Color From Parts (Step 3).
  • The Preview is too fast: it runs every frame. In a game, the script's stepInterval sets the speed.
  • Nothing on the object in Play: check the Compute Effect's Shader is ConwaysLife.tcompute, and that the graph was saved at least once. Compute shaders don't run on dedicated servers or on graphics backends without compute support.
  • Odd behaviour at the edges: this tutorial uses the texture's own edge behaviour and doesn't wrap the world round.

What you've learned

You built a GPU cellular automaton from nodes: Previous carries the last generation over, Neighbours Sum counts the living cells around each one, Step and Subtract turn counts into yes/no answers, Blend with a property switches between starting and running, and Compute Output writes the next generation. Try an Edge of 0.65 on the noise Step for more starting cells, or Width and Height of 512 on the Compute Effect.

Next: the same feedback works for ripples, spreading fire and other simulations. Read Materials, shaders and compute or download Tallow.