The GPU Pipeline
Before writing more shaders, it helps to know where they sit. A game does not hand the GPU "a character". It hands it buffers of numbers and a pipeline: a fixed sequence of stages, some programmable (the shaders) and some built into the hardware. Every draw call pushes data through that sequence.
From vertices to pixels
CPU (your program) GPU
─────────────────── ─────────────────────────────────────────────────────────────
vertex buffer ──┐ ┌──────────────┐ ┌─────────────┐ ┌───────────────┐
index buffer ──┼── draw call ──────────► │ input │──►│ VERTEX │──►│ primitive │
uniforms ──┤ │ assembly │ │ SHADER │ │ assembly, │
textures ──┘ └──────────────┘ │ (per vertex)│ │ clipping, │
└─────────────┘ │ viewport │
└──────┬────────┘
▼
framebuffer ◄── blending ◄── depth/stencil test ◄── FRAGMENT SHADER ◄── RASTERIZER
(on screen or (per pixel) (which pixels does
a render target) each triangle cover?)| Stage | Programmable? | What it does |
|---|---|---|
| Input assembly | no (configured) | reads vertex attributes (position, normal, UV, color) from buffers, using the index buffer to reuse shared vertices |
| Vertex shader | yes | runs once per vertex; must output a clip-space position (gl_Position) and any values to pass along |
| Primitive assembly, clipping | no | groups vertices into triangles, cuts away what is outside the view, divides by w (perspective divide) and maps to pixels (viewport transform) |
| Rasterizer | no (configured: culling, fill mode) | finds the pixels whose centers lie inside each triangle and interpolates the vertex outputs across it |
| Fragment shader | yes | runs once per covered pixel (a fragment); outputs a color (or several), may discard |
| Depth and stencil test | no (configured) | keeps the fragment only if it is closer than what is already there |
| Blending | no (configured) | combines the new color with the old one (opaque overwrite, alpha blend, additive) |
Newer pipelines add optional stages (tessellation, geometry shaders, mesh and task shaders) and a separate compute pipeline that has no rasterizer at all. This course focuses on the two stages every renderer has: vertex and fragment.
Buffers, uniforms and varyings
Shaders receive data in three ways, and mixing them up is the first source of shader bugs:
| Kind | GLSL ES 3.00 | Changes | Example |
|---|---|---|---|
| Attribute (vertex input) | in vec3 aPosition; in the vertex shader |
per vertex | position, normal, UV |
| Uniform | uniform mat4 uModel; |
per draw call | matrices, time, light direction, material color |
| Varying (interpolated) | out vec2 vUv; in the vertex shader, in vec2 vUv; in the fragment shader |
per fragment, blended from the 3 vertices | UV, world position, normal |
Textures are a fourth kind: a uniform sampler2D is a handle the shader reads with texture(tex, uv). The playgrounds in this course have none, so effects are built from math (noise, SDFs) instead of images.
The names in old GLSL (attribute, varying, gl_FragColor) were replaced by in/out in GLSL 1.30 and GLSL ES 3.00. You will still meet them in old tutorials and in WebGL 1 code.
A rasterizer you can read
The rasterizer is the part people find most magical, so here is one in JavaScript. For each pixel center it computes three edge functions: signed areas that say on which side of each triangle edge the point lies. If all three are positive, the pixel is inside. Divided by the triangle's total area, the same three numbers are the barycentric weights used to interpolate a varying, here a "brightness" that is 0, 4 and 9 at the three corners:
const W = 24, H = 10;
const A = { x: 2, y: 1, v: 0 }, B = { x: 22, y: 3, v: 4 }, C = { x: 8, y: 9.5, v: 9 };
// Twice the signed area of triangle (a, b, p): > 0 when p is left of the edge a→b.
const edge = (a, b, p) => (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
const area = edge(A, B, C);
let covered = 0;
for (let y = 0; y < H; y++) {
let row = '';
for (let x = 0; x < W; x++) {
const p = { x: x + 0.5, y: y + 0.5 }; // sample at the pixel center
const w0 = edge(B, C, p) / area; // weight of A
const w1 = edge(C, A, p) / area; // weight of B
const w2 = edge(A, B, p) / area; // weight of C
if (w0 >= 0 && w1 >= 0 && w2 >= 0) {
covered++;
row += Math.round(w0 * A.v + w1 * B.v + w2 * C.v); // the interpolated varying
} else row += '.';
}
console.log(row);
}
console.log(`fragment shader invocations: ${covered}`);........................
..11111.................
...22222222233333.......
....33333333444444444...
....444444445555555.....
.....555555566666.......
......66666667..........
.......77777............
.......888..............
........................
fragment shader invocations: 79
Every digit is one fragment-shader call, and its value came from interpolation, not from your code. Real GPUs do exactly this, in parallel blocks of pixels, with exact fill rules so that two triangles sharing an edge never both draw (or both skip) the pixels on it. They also interpolate perspective-correctly: weights are divided by each vertex's w and renormalized, so textures do not swim on surfaces seen at an angle.
The same idea inside a fragment shader
A fragment shader can run edge functions too. This one draws a triangle itself, with the three corner colors blended by the barycentric weights: the picture the hardware rasterizer would produce for a triangle with red, green and blue vertices.
float edgeFn(vec2 a, vec2 b, vec2 p) {
return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = fragCoord / iResolution.y; // y in 0..1, x scaled by aspect
float wobble = 0.05 * sin(iTime);
vec2 a = vec2(0.20, 0.10);
vec2 b = vec2(1.20 + wobble, 0.25);
vec2 c = vec2(0.55, 0.90 - wobble);
float area = edgeFn(a, b, c);
vec3 w = vec3(edgeFn(b, c, p), edgeFn(c, a, p), edgeFn(a, b, p)) / area;
vec3 background = vec3(0.08, 0.09, 0.12);
if (min(w.x, min(w.y, w.z)) < 0.0) {
fragColor = vec4(background, 1.0); // outside: no fragment would exist
return;
}
fragColor = vec4(w, 1.0); // red, green, blue corners blended
}Note: This is only a teaching trick. Real renderers never rasterize in a fragment shader; they send triangles and let the fixed-function hardware do it, which is far faster.
Why GPUs are fast, and what that costs you
A GPU runs shaders in groups of 32 or 64 invocations (a warp on NVIDIA, a wavefront on AMD, a SIMD group on Apple) that execute the same instruction at the same time on different data. Two consequences shape the rest of this course:
- Independence. A fragment cannot read the color another fragment is writing in the same pass. Effects that need neighbors (blur, outlines, bloom) render to a texture first and read it in a second pass. That is what render targets are for.
- Divergence. When invocations in one group take different sides of an
if, the group runs both sides and masks the results. Branches are not forbidden, but a branch that differs pixel to pixel can cost the sum of both paths. The Field Notes lesson goes deeper.
Fragments are also shaded in 2×2 quads, so the GPU can compare neighbors to compute derivatives (dFdx, dFdy, fwidth) and pick texture mip levels. Even a triangle covering a single pixel pays for four invocations.
Test yourself
Which stage decides which pixels a triangle covers?
- Vertex shader
- Rasterizer
- Fragment shader
- Blending
A value written by the vertex shader as out vec2 vUv arrives in the fragment shader how?
- Unchanged from the first vertex
- Interpolated across the triangle from its three vertices
- As a uniform
- Only if a texture is bound
Why does a blur need two passes instead of reading neighboring pixels directly?
- GLSL has no loops
- A fragment cannot read other fragments' outputs from the same pass
- Blur needs a vertex shader
- Uniforms are limited