Introduction#
After the vertex shader, the graphics card knows exactly where each triangle’s three corners land on screen. But the screen is a grid of pixels, and triangles don’t line up with it. Rasterisation is the step that decides which pixels each triangle covers, turning a smooth mathematical shape into a set of squares. (“Raster” is the old word for a grid of lines on a screen.) This post covers:
- The simple rule for deciding whether a pixel is covered
- Edge functions: the three tests that apply that rule
- Fill rules: what happens when a pixel lands exactly on an edge
- How real hardware makes all this fast
The Rule: Test the Pixel’s Centre#
A triangle’s edge can cut straight through a pixel, covering some of it and not the rest. But a pixel can only have one colour, so the rasteriser needs a yes-or-no answer. The standard rule is simple: a pixel is covered if its centre is inside the triangle. Pixel centres sit halfway across each pixel, at coordinates like (0.5, 0.5), (1.5, 0.5) and so on.
Drag the triangle’s corners below (or select the grid, press 1, 2 or 3 to pick a corner, and use the arrow keys). The orange pixels are the ones whose centres are inside. Hover over or tap a pixel to see the maths for it.
Rasterising a triangle onto a grid of pixels
Things to notice:
- A thin sliver of a triangle can miss every pixel centre and draw nothing at all, even though it’s visibly there. Squash the triangle flat and watch it vanish.
- The covered pixels form staircases along slanted edges. Raise Pixels across and the steps get smaller, but they never quite go away. This is aliasing, and the fix, anti-aliasing, has its own post.
- Some pixels that are almost half covered stay empty, while others that are barely more than half covered are filled. A yes-or-no test can’t do better.
Edge Functions: Three Tests per Pixel#
How does the graphics card know whether a point is inside a triangle? Each edge of a triangle is a straight line, and every line splits the screen into two halves. A point is inside the triangle if it’s on the inner side of all three edges.
To test which side a point is on, the rasteriser uses an edge function. For an edge from corner A to corner B, and a pixel centre P:
Here Px means P’s x coordinate, Ay means A’s y coordinate, and so on: it’s just two multiplications and a subtraction. The answer is positive on one side of the line, negative on the other and exactly zero on the line itself. (It’s a 2D version of the cross product.) Tick Show edge tests in the demo: pixels outside the triangle are shaded more darkly the more tests they pass, so you can see the three “inner sides” overlapping. Only pixels that pass all three are covered.
This is a lovely method for hardware, for two reasons. Each pixel can be tested independently, so thousands can be tested in parallel. And the edge function changes by a fixed amount from one pixel to the next, so stepping across the screen needs only an addition, not a multiplication.
The same three numbers, divided by the triangle’s total area, also tell you how close the pixel is to each corner. Those are the weights used to blend vertex values across the triangle, which is the subject of part 10.
Fill Rules: When a Centre Lands Exactly on an Edge#
Meshes are made of triangles that share edges. What if a pixel’s centre sits exactly on a shared edge? If both triangles claimed it, it would be drawn twice: harmless for a solid surface, but a see-through surface would be blended twice there and show a visible seam. If neither claimed it, there’d be a gap. Each such pixel must be drawn by exactly one of the two triangles.
In the demo below, the shared edge runs at exactly 45° through a line of pixel centres. Try each rule.
Two triangles sharing an edge that passes exactly through pixel centres
The top-left rule solves it. A pixel centre exactly on an edge counts as inside only if that edge is a top edge (perfectly horizontal, with the triangle below it) or a left edge (with the triangle to its right). Any edge shared by two triangles is a left edge for one of them and a right edge for the other (or top for one and bottom for the other), so exactly one of them claims the pixel.
Direct3D specifies the top-left rule exactly. OpenGL and Vulkan only require that a pixel on an edge shared by two triangles is drawn by exactly one of them, and in practice hardware uses a rule like this one.
Real rasterisers don’t test every pixel on the screen for every triangle. They first work out the triangle’s bounding box (the smallest rectangle that contains it), then test whole tiles of pixels (for example 8×8) at once, using the edge functions at the tile’s corners to reject tiles that are completely outside and accept tiles that are completely inside without testing each pixel. Only tiles that an edge passes through are tested pixel by pixel. Vertex positions are also rounded to a fine grid (in Direct3D 11, steps of 1/256 of a pixel), so the edge tests can use exact whole-number arithmetic, with no rounding errors. That makes the “exactly on the edge” case well defined, and guarantees two triangles sharing an edge compute the same edge, with no cracks between them. Finally, covered pixels are handed on to the fragment shader in 2×2 groups, for reasons covered later in the ddx and ddy posts.
Questions You Might Have#
What happens to a triangle smaller than a pixel?
If it doesn’t cover any pixel centre, it produces no fragments at all and simply vanishes. Huge numbers of tiny triangles are also slow, because the hardware works on pixels in small groups (Helper Pixels).
Summary#
- Rasterisation decides which pixels a triangle covers: a pixel is covered if its centre is inside.
- Inside means passing three edge function tests, one per edge, which are cheap and fully parallel.
- The yes-or-no test causes aliasing: staircase edges and vanishing slivers.
- Fill rules like the top-left rule make sure pixels on shared edges are drawn exactly once.
Next, part 10 looks at what happens to each covered pixel: how values are blended across the triangle, and how the fragment shader colours it.
Further Reading#
- Juan Pineda, A Parallel Algorithm for Polygon Rasterization (SIGGRAPH 1988): the original paper on edge functions.
- Fabian Giesen, Triangle rasterization in practice: edge functions, fill rules and fixed-point precision explained in depth.
- Scratchapixel, Rasterization: a Practical Implementation: building a rasteriser in code.
- Microsoft, Rasterization rules: Direct3D’s official top-left rule, with diagrams.