The idea: one draw call, eight stages

A program asks the GPU to draw a triangle with one call, for example glDrawArrays(GL_TRIANGLES, 0, 3). The GPU then runs the three vertices through a fixed chain of stages called the graphics pipeline. Two stages run code you write: the vertex shader and the fragment shader. The others are built into the hardware and you only switch them on, off or set a mode.

Eight boxes in a row: vertex shader and fragment shader in green (programmable), assembly, clipping and divide, rasterizer, depth test, blending and framebuffer in grey (fixed function). Under the arrows: 3 vertices, 3 clip-space vertices, 1 triangle, window-space triangle, fragments, coloured fragments, surviving fragments, pixels
Vertices go in at the left and pixels come out at the right; only the two green stages run your code.

Each stage has its own page on this site: the transforms on Perspective Projection, the pixel test on Rasterization, the lighting a fragment shader computes on Phong Shading. This page shows how they fit together. The screen is a tiny 16 × 12 framebuffer, so every fragment and every depth value is on the canvas.

stagerunsinput → output
vertex shaderyour code, once per vertexvertex → clip-space position
primitive assemblyfixed3 vertices → 1 triangle
clipping, ÷ w, viewportfixedtriangle → 0, 1 or more triangles in pixels
rasterizationfixedtriangle → fragments (one per covered pixel)
fragment shaderyour code, once per fragmentfragment → colour
depth testfixed, on or offkeep or drop the fragment
blendingfixed, on or offmix with the colour already there
framebuffermemorycolour buffer + depth buffer

The vertex shader: model, view, projection

A vertex starts in object space, the coordinates the modeller used. The vertex shader multiplies it by three matrices: M (model) places the object in the world, V (view) moves the world so the camera sits at the origin looking down −z, and P (projection) turns the view volume into clip space. In GLSL it is one line:

gl_Position = P * V * M * vec4(position, 1.0);

On the page M is a translation, the camera is at (0, 0, 2), and P is a perspective matrix with a 90° field of view, aspect 16 / 12, near plane 1 and far plane 3. Written out it gives xc = 0.75·x, yc = y, zc = −2·z − 3 and wc = −z. For vertex v0 of triangle A: object (−3, 1.4, 0) → world (−2, 1.4, 0) → eye (−2, 1.4, −2) → clip (−1.5, 1.4, 1, 2). (Demo 1: one triangle through every stage)

The shader runs once per vertex and every run is independent, so a GPU runs thousands of them at once (see GPU SIMT and warps). It can also pass extra values, called varyings (colour, normal, texture coordinates), on to the fragment shader.

Clipping before the divide

In clip space a point is inside the view volume when −w ≤ x ≤ w, −w ≤ y ≤ w and −w ≤ z ≤ w. The clipper checks this before dividing by w, and for a good reason: a point level with the camera has w = 0, and a point behind it has w < 0. Dividing would blow up or flip it to the wrong side of the screen.

Triangle C has c2 at eye z = 0, so its clip position is (0.3, 2.4, −3, 0). It fails the near plane (z < −w). The clipper cuts both edges that leave the volume. For edge c0–c2 the distance to the near plane, z + w, is 3 at c0 and −3 at c2, so the cut is at t = 3 / (3 − (−3)) = 0.5: a new vertex at clip (0.75, 0.4, −1, 1). The kept part has four corners, c0, c1, p4, p3, and is drawn as a fan of two triangles. (Demo 8: clipping at the near plane)

Left: side view of the camera, the near plane at z = −1 and triangle C whose top corner c2 sits level with the camera; the part between the camera and the near plane is cut off. Right: on the screen the kept part is a four-cornered polygon c0, c1, p4, p3 split by one diagonal into two triangles
A triangle that pokes through the near plane loses its tip; the four-cornered rest becomes two triangles.

After clipping, every vertex has w > 0. The perspective divide gives normalized device coordinates (NDC) in −1…1, and the viewport transform maps them to pixels: xwin = (x + 1)·8, ywin = (1 − y)·6 and depth = (z + 1) / 2, a number from 0 (near plane) to 1 (far plane).

Rasterize, then shade each fragment

The rasterizer finds the pixel centres inside the triangle and makes one fragment for each, with a depth mixed from the three vertex depths (how: Rasterization). Triangle A gives 48 fragments with depths from 0.51 to 0.84; the canvas prints each one in its pixel. Then the fragment shader runs once per fragment and returns a colour. On the page it returns the triangle's colour; a real one would compute lighting or read a texture here.

Fragments are not pixels yet. A fragment is a candidate: the next two stages decide whether it reaches the framebuffer and how.

A second triangle: the depth test

The depth buffer keeps one depth per pixel, cleared to 1. With GL_DEPTH_TEST on and the default GL_LESS, a fragment survives only if its depth is less than the stored one, and then its depth is stored. Triangle B sits at depth 0.25, in front of A. It has 31 fragments and overlaps A in 15 pixels.

draw orderdepth testsecond draw: pass / failoverlap shows
A, then BonB: 31 / 0B ✓
B, then AonA: 33 / 15B ✓ (same picture)
B, then AoffA: 48 / 0A ✗ (the farther one)

(Demo 2, Demo 3 and Demo 4.) In Demo 3 the 15 hidden fragments of A were still run through the fragment shader and then thrown away. GPUs avoid that waste with early-z: when the shader does not change the depth, the test runs before the shader. Drawing opaque objects roughly front to back makes it pay off.

Glass: blending and why order matters again

With blending on (glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)) the new colour is mixed with the colour already in the pixel:

new = α·src + (1 − α)·dst

The glass version of B is blue (66, 165, 245) with α = 0.5. Over the orange of A (255, 167, 38) it gives (161, 166, 142); over the white background it gives (161, 210, 250). (Demo 7: one fragment of the glass, blended)

Now the order matters even with the depth test on. Draw A first and the glass second, and the overlap shows A through the glass. Draw the glass first, and it writes depth 0.25 into the overlap; A's 15 fragments there then fail the depth test, so A vanishes behind a pane that should be see-through. (Demo 5 and Demo 6.)

Two framebuffers side by side. Left, A drawn first and the glass second: the overlap is a mix of blue and orange, correct. Right, the glass drawn first: the overlap is light blue over white because A's fragments failed the depth test there, wrong
Opaque first, glass last: drawn the other way, the glass's depth hides what is behind it.

The usual recipe: draw all opaque objects first with depth writes on, then draw the transparent ones sorted far to near, with the depth test on but depth writes off (glDepthMask(GL_FALSE)).

See also Rasterization: Edge Functions and the Z-Buffer for the pixel test, Perspective Projection for the divide by depth, Phong Shading for what a fragment shader computes, and Ray Tracing vs Rasterization for the other way to make an image.

See also Texture Mapping: UV, Bilinear, Mipmaps and Wrap for what a texture read in the fragment shader does: nearest or bilinear lookups, mipmap levels and wrap modes.

See also Shadow Mapping, which runs the pipeline twice: once from the light to store depths, then from the camera to test each point against them.

See also Polygon Clipping: Outcodes, Sutherland–Hodgman and Back Faces, which clips a polygon against one window edge at a time with the same four inside/outside cases and shows back-face culling by winding order.

What the page leaves out

Index buffers and vertex caches, back-face culling (off by default in OpenGL; all triangles here face the camera), tessellation and geometry shaders, perspective-correct interpolation of varyings, textures and mipmaps, the stencil test and scissor test, anti-aliasing with several samples per pixel, guard-band clipping (GPUs only really clip at the near and far planes), tile-based GPUs on phones, and double buffering of the framebuffer.

On the mind map

Open the Geometric Algorithms mind mapGeometricAlgorithmsTransform matrices: multiply each vertex by a matrixTransform matricesCoordinate frames: same point, different framesCoordinate framesHulls and curves: cross products and lerpsHulls and curvesRendering: which pixel shows whatRenderingFourier and DCT: a signal as a sum of wavesFourier and DCTTriangulations: mesh a point setTriangulationsSegments and polygons: lines, crossings, regionsSegments and polygonsRasterizationRasterizationthree edge tests per pixel, z-bufferGraphics PipelineClip before ÷w; opaque first, glass lastPolygon ClippingPolygon Clippingone window edge at a time: 0, 1 or 2 pointsTexture Mapping & MipmapsTexture Mapping & Mipmapsλ = log2 ρ picks the mipmap levelShadow MappingShadow MappingCompare depth with the light's mapRay tracingRay tracingone ray per pixel, nearest hit winsBresenham lineBresenham lineinteger error term picks the next pixelPhong shadingPhong shadingambient + diffuse N·L + specular (R·V)ⁿOpen the Geometric Algorithms mind mapGeometric Algorithmswhich pixel shows whatRenderingRasterizationRasterizationthree edge tests per pixel, z-bufferGraphics PipelineClip before ÷w; opaque first, glass lastPolygon ClippingPolygon Clippingone window edge at a time: 0, 1 or 2 pointsTexture Mapping & MipmapsTexture Mapping & Mipmapsλ = log2 ρ picks the mipmap levelShadow MappingShadow MappingCompare depth with the light's mapRay tracingRay tracingone ray per pixel, nearest hit winsBresenham lineBresenham lineinteger error term picks the next pixelPhong shadingPhong shadingambient + diffuse N·L + specular (R·V)ⁿ
This page on the Geometric Algorithms mind map · 25 pages in 7 branches · open it to see the whole map and test yourself in recall mode.