Introduction#
In part 1 we saw that the graphics card’s job is to fill a grid of pixels with colours. But a game doesn’t hand it a grid of colours; it hands it a 3D world made of triangles. The series of steps that turns one into the other is called the graphics pipeline, and it’s the backbone of everything else on this site. This post covers:
- The six main stages of the pipeline, and what each one does
- Which stages you program yourself (shaders) and which are built into the hardware
- How often each shader actually runs, measured on your own graphics card
Every later post in this series zooms in on one of these stages, so think of this as the map.
Why a Pipeline?#
A factory production line splits a big job into small steps, with each worker doing one step over and over. While one car is being painted, the next is having its wheels fitted. A graphics card works the same way: each stage takes the output of the stage before it, and all stages work at once on different pieces of the frame.
Each stage also works on many things in parallel. A graphics card has thousands of small processors, and the work is arranged so that each vertex, and each pixel, can be handled independently of the others. That’s why a graphics card can colour millions of pixels in a few milliseconds when a normal processor would take far longer.
The Six Stages#
Step through the stages below, using the buttons or Next. The image is a tiny 24 Ă— 14 pixels so you can see every one, and it contains two triangles: an orange one in front and a blue one behind.
Two triangles going through the graphics pipeline, one stage at a time
Here is each stage in a little more detail.
- Vertex data. The game uploads its 3D models, called meshes, to the graphics card’s memory. A mesh is stored as a list of vertices: the corners of its triangles. Each vertex carries a position and usually more, such as a colour, or which point of an image should be stretched onto that corner. The next part, Vertices, Triangles and Meshes, covers this in detail.
- Vertex shader. A small program, written by the game’s developers, that runs once for each vertex. Its main job is to work out where the vertex ends up on screen, which usually involves some grids of numbers called matrices that move, turn and project things; parts 4 to 7 of this series explain them. It can also move vertices around to animate them.
- Primitive assembly. The vertices are joined up into triangles. Triangles completely outside the view are thrown away, triangles crossing the edge of the view are trimmed where necessary (clipped), and triangles facing away from the camera can be skipped too (culling).
- Rasterisation. For each triangle, the hardware works out which pixels it covers. Each covered pixel becomes a fragment: a candidate for that pixel, carrying values blended from the triangle’s three vertices.
- Fragment shader. Another small program written by the developers, which runs once for each fragment and works out its colour. This is where texturing and lighting usually happen. (Some graphics systems, such as Microsoft’s Direct3D, call it a pixel shader.)
- Output merging. Each fragment’s colour is tested and combined with what’s already in the framebuffer. The depth test throws away fragments hidden behind something nearer, and blending mixes colours for see-through surfaces.
A shader is just a program that runs on the graphics card as one of these stages. The name is historical: the first ones only worked out shading (light and dark), but today they do far more.
Only the vertex and fragment shaders are programs you write. Rasterisation, clipping and the depth test are fixed-function stages: built into the hardware, and only configurable through settings like “turn the depth test on”. Modern graphics cards also have a few optional extra programmable stages, which this series doesn’t need: the six stages above are the core that everything else builds on.
Who Tells the Graphics Card What to Do?#
The game runs on the computer’s main processor (the CPU). To draw something, it calls a graphics API (application programming interface): a set of functions provided for programs to use, such as Direct3D, Vulkan, Metal, OpenGL or, in a web browser, WebGL. It sets up which shaders, meshes and textures to use, then issues a draw call: “draw these triangles”.
The graphics card’s driver, software supplied by its manufacturer, translates those calls into commands the hardware understands and queues them up. The graphics card works through the queue on its own, which leaves the CPU free to get on with the game. A typical frame of a modern game might make thousands of draw calls.
How Often Does Each Shader Run?#
The vertex shader runs once per vertex, and the fragment shader runs once per fragment, roughly once per covered pixel. Those can be wildly different numbers. The demo below draws a sphere on your graphics card, then reads the image back to count exactly how many pixels it covers.
Counting vertex shader and fragment shader runs for a sphere
Try these:
- Move the sphere further away. The number of fragments drops quickly (it shrinks with the area on screen), but the vertex count doesn’t change at all: the vertex shader still has to process every vertex, however small the sphere looks.
- Turn up the detail while the sphere is small. The vertex shader now does more work than the fragment shader, which is why games swap distant objects for simpler versions, called levels of detail.
- Lower the resolution. Fragment work falls with the number of pixels. This is why lowering the resolution is the classic way to make a game run faster, and why games often draw at a lower resolution and then stretch the picture up to full size (upscaling).
The counts in the demo are close, but not exact. Vertices shared by several triangles are normally processed once and the result reused, but the graphics card only remembers a small number of recent results, so some vertices can run more than once. Fragment shaders always run in little 2Ă—2 squares of pixels, so pixels along a triangle’s edge often cost a few extra “helper” runs. And in a scene where objects overlap, a pixel can be coloured several times over before the nearest object wins, which is called overdraw. Graphics cards reduce that by checking, before running the fragment shader, whether the fragment is hidden behind something already drawn. The Helper Pixels post shows the 2Ă—2 groups in action.
Questions You Might Have#
Why triangles, and not squares or circles?
A triangle’s three corners always lie on one flat surface, so it can never be bent or twisted, and any shape can be built from enough of them. That keeps the hardware’s job of filling them in simple and very fast. Squares and circles are drawn as triangles too.
Summary#
- The graphics pipeline turns vertices into pixels in stages: vertex data, vertex shader, primitive assembly, rasterisation, fragment shader and output merging.
- The vertex shader runs once per vertex and places it on screen; the fragment shader runs once per covered pixel and colours it.
- The CPU drives it all with draw calls through a graphics API, and the driver turns them into hardware commands.
- Vertex work depends on mesh detail; fragment work depends on screen area and resolution.
Next, we’ll look at what goes in at the very start of the pipeline: vertices, triangles and meshes.
Further Reading#
- Fabian Giesen, A trip through the Graphics Pipeline 2011: the classic deep dive into what really happens at each stage, from the driver to the pixels.
- LearnOpenGL, Hello Triangle: the pipeline from a programmer’s point of view, with the code to draw your first triangle.
- WebGL2 Fundamentals, How It Works: how WebGL feeds vertex and fragment shaders.
- Microsoft, Graphics pipeline: Direct3D 11’s version of the pipeline, including the optional stages.