This project implements Conway's Game of Life on the GPU using Metal compute shaders, and displays the result in a SwiftUI app through MTKView.
- Simulates a 2D cellular automaton (Conway's Life)
- Updates the grid with a Metal compute kernel
- Renders the current grid texture with a Metal render pipeline
- Lets you Play/Pause, Step, Randomize, Clear, and change simulation speed
Let each cell state be:
where 1 means alive and 0 means dead at generation t.
Neighbor count:
Transition rule:
This project uses toroidal boundaries (wrap-around edges):
where W is grid width and H is grid height.
- Seed or clear state textures using compute kernels:
seedRandom: writes random alive/dead valuesclearState: writes all dead cells
- For each generation:
stepLifereads fromcurrenttexture and writes next generation tonexttexture- texture indices are swapped (ping-pong buffering)
- Render pass:
lifeVertexoutputs a fullscreen quadlifeFragmentsamples current state texture and maps alive/dead to colors
The next generation must be computed from the unchanged previous generation. If we wrote updates into the same texture we are still reading from, results would be incorrect due to read/write hazards.
So we keep:
currentStateTexturefor readingnextStateTexturefor writing
Then swap them each step.
Simulation speed is controlled in generations per second (f).
A time accumulator advances with frame delta time. While accumulator is greater than Delta t_step, the code runs another generation.
This keeps simulation rate stable even if render FPS changes.
