Passes and feedback

glint · Examples · Tutorials · Gallery

Shaders that read pictures: a second pass, blurs (and why two passes beat one), bloom, feedback trails and tunnels, the Game of Life and reaction-diffusion.

A free interactive course of 8 steps in glint, a live shader editor in the browser: each step changes a running shader, and ticks itself when you've done its task.

1. A second pass

Everything so far drew straight to the screen. A pass draws into a picture instead (a texture), and later passes can read it. That opens up a whole class of effects: anything that needs to look at other pixels, which a single shader can't do.

This project has two passes (the tabs above the code). Pass 0, Scene, draws the shapes. Pass 1, Post, is the one you're in: it reads Scene's picture with texture(prevPass, uv) and shows it. The last pass is what ends up on screen.

Post can do anything with what it reads. Here's the simplest: flip every colour to its opposite.

Your turn: In Post, invert the picture: fragColor = vec4(1.0 - c.rgb, 1.0);

2. Blur: reading the neighbours

Blurring means each pixel becomes the average of the pixels around it. One texture read gets one pixel; a loop reads a square of them and averages.

texel = 1.0 / resolution is the size of one pixel in texture coordinates. The loop reads a 9 × 9 square, spaced RADIUS pixels apart, adds them up and divides by 81.

With RADIUS = 1 the square is only 9 pixels wide, barely a blur. Spacing the reads further apart blurs more for the same cost, but it skips the pixels in between: look closely and each light is a grid of copies, which turns blocky as the blur grows. Blur and glow (its own course) does big blurs that stay smooth.

Your turn: Blur more: const float RADIUS = 3.0;

3. Two passes are faster than one

The last blur read 81 pixels for every pixel. A blur 33 pixels wide that way would be 1089 reads each: far too slow. But a square blur can be done in two thin ones: blur along x into a picture, then blur that along y. The result is the same.

Cost: 33 reads in the first pass plus 33 in the second, 66 instead of 1089. That's why real blurs (and glows, and depth of field) are almost always separable, split into a horizontal and a vertical pass.

Both blur passes here use the same code, with a DIR saying which way to go. Right now both blur sideways, so the picture is only smeared horizontally.

These read every pixel with the same weight (a box), so a light comes out as a soft square. Weighting the reads by a bell curve (a Gaussian, used in the next step) makes it round.

Your turn: In the last pass, blur up and down: const vec2 DIR = vec2(0.0, 1.0);

4. Glow: bloom

Bright things in real cameras bleed light into their surroundings: bloom. It's three steps, and each is a pass. Bright: keep only the brightest parts of the scene (everything else black). Blur them a lot. Then add the blurred light back on top of the original scene.

The last pass needs two pictures: the blurred light (prevPass, the pass just before) and the original scene. Any earlier pass can be read by number: pass0 is the Scene.

The blur is two passes, x then y, each weighting its reads by a bell curve (a Gaussian), so the glow is round and smooth. Blur and glow (its own course) goes further: wider glows for less work, and why they turn blocky.

Right now the last pass shows only the scene. Add the glow.

Your turn: In Combine, add the glow: col += 1.5 * glow;

5. Feedback: a pass that remembers

A pass can also read its own picture from the previous frame: feedback. Whatever it drew last frame is still there to build on. That's memory, and it's where trails, smears and simulations come from.

Here: draw a moving dot, and also last frame's picture faded a little (* decay). The dot leaves a trail that fades out over time. Each frame keeps decay of the last, so after n frames a mark has decay^n left.

Worked example at 60 frames a second: with decay 0.9, a mark is down to 0.9^60 ≈ 0.002 after a second (gone). With 0.97 it's 0.97^60 ≈ 0.16: a long, lingering tail.

Your turn: Make the trails longer: float decay = 0.97;

6. Feedback that moves

Read the old frame from a slightly different place and everything drawn so far moves on every frame. Read it a little zoomed in and old marks drift outwards; turn it a little and they spiral. Small changes, repeated 60 times a second, make big shapes.

The reading position is the pixel's position, zoomed by 0.99 around the centre and turned by a small angle. The new frame adds a ring that changes colour, and feedback does the rest: an endless tunnel.

Read a bit further each frame and the motion is faster; flip the angle's sign and it spirals the other way.

Your turn: Add a twist: float turn = 0.02;

7. A simulation: the Game of Life

Feedback can hold any state, not just a picture. In Conway's Game of Life each pixel is a cell, alive (1) or dead (0). Every frame, each cell counts its 8 living neighbours, and two rules decide its next state: a living cell with 2 or 3 neighbours survives, and a dead cell with exactly 3 comes alive.

Pass 0 (Life, the one you're in) reads its own last frame with feedback, applies the rules, and writes the new state. On the first frames (frame < 2, and loading rewinds to frame 0) it fills itself with random cells instead. Pass 1 just colours the result.

Changing the rules changes the world. 'HighLife' adds one: a dead cell with 6 neighbours also comes alive, and patterns that copy themselves appear.

Your turn: Switch to HighLife: bool born = n == 3 || n == 6;

8. A simulation: reaction-diffusion

Two imaginary chemicals, A and B, spread out (diffuse) and react: B eats A to make more B, A is fed in steadily, and B slowly dies away. The Gray-Scott model, a few lines of arithmetic per pixel, grows patterns like coral, fingerprints, leopard spots and cell division.

Pass 0 keeps A in the red channel and B in green, reads its last frame through feedback, and steps the simulation 1 frame on. The blur-like sum is the diffusion (the Laplacian: how different a pixel is from its neighbours).

Two numbers choose the pattern: feed (how fast A arrives) and kill (how fast B dies). feed 0.055 / kill 0.062 grows mazes; 0.037 / 0.06 makes spots that split like cells.

Your turn: Grow dividing spots: const float feed = 0.037, kill = 0.06;

Other courses: Basics · Medium · Advanced · Maths for shaders · Raymarching, step by step · Matrices, from scratch · Sound you can see · Noise from scratch · Colour and light · Skies and reflections · Vertex shaders, step by step · Raymarching 2: glow, fog and light · Raymarching 3: worlds · Raymarching 4: modelling, materials and motion · Fractals, step by step · Vertex shaders 2: lines, particles and moving meshes · Fractals 2: further in · Vertex shaders 3: living geometry · Post-processing · Surfaces: from vertex to fragment · Blur and glow