Rasterization in the Graphics Pipeline
Abstract
Using OpenGL ES 2.0 as an example, this article explains primitive clipping and rasterization in the rendering pipeline, including how points, line segments, and polygons are rasterized.
Rasterization · Bresenham's line algorithm · Barycentric coordinates
Contents
Rasterization takes place after primitive assembly and before the fragment shader. In APIs such as Direct3D and OpenGL that have tessellation and geometry shaders, rasterization follows the geometry shader. This article covers OpenGL ES 2.0, which has neither stage, so rasterization follows primitive assembly.

Primitive Clipping
The output of primitive assembly is the input to rasterization. In the final step of primitive assembly, primitives are clipped. A point outside the near or far plane of the clipping volume is discarded. In clip space, the clipping volume is defined by $$ -w_c \leq x_c \leq w_c \\ -w_c \leq y_c \leq w_c \\ -w_c \leq z_c \leq w_c $$ When a line segment intersects the near or far clipping plane, clipping creates one or two new vertices. Each new vertex is assigned a parameter $0 \leq t \leq 1$, and its varying values are computed by linear interpolation. The interpolation happens in clip space, before division by $w_c$, so the resulting varying values are perspective-correct. A triangle with all its edges inside the clipping volume passes through unchanged. Otherwise it is clipped or discarded. Clipping may introduce vertices and turn the triangle into a polygon. See section 2.13, “Primitive Clipping,” of the OpenGL ES 2.0 specification for details.
After clipping, clip-space vertices undergo perspective division to get normalized device coordinates (NDC). A viewport transform converts those coordinates to window coordinates, which are sent to the rasterizer.
The rest of this article describes rasterization as specified in chapter 3 of the OpenGL ES 2.0 specification.
Rasterization
Rasterization converts a primitive into a two-dimensional image in two steps:
- Determine which squares in the window-coordinate grid are covered by the primitive.
- Assign a color and a depth to each covered square. The fragment shader produces the color.
Rasterization generates fragments. The fragment shader computes their colors, and rasterization determines their depth values. Extensions can allow the fragment shader to modify depth. The resulting fragments carry color, depth, and optionally stencil values into per-fragment operations, including the scissor, stencil, and depth tests, blending, and dithering.
A grid square and its parameters are called a fragment. Its signed $z$ value and varying values are collectively called its associated data. A fragment is located by its lower-left corner at an integer grid coordinate. Its center is offset from that corner by $(\frac{1}{2}, \frac{1}{2})$.
Point Rasterization
Rasterizing a point primitive produces a square of fragments centered at $(x_w, y_w)$. Its side length comes from the vertex shader output gl_PointSize and is limited to an implementation-defined range. The OpenGL ES specification leaves point size undefined when this value is zero or negative. In my tests, macOS drew a point of size 1 and Windows skipped the point.
All generated fragments have the same varying values. The built-in gl_PointCoord varies across the square. gl_PointCoord gives each fragment's coordinates $(s, t)$ within that square. The coordinates run from 0 to 1 from left to right and from top to bottom, respectively. Compute $(s, t)$ as follows:
$$ s = \frac{1}{2} + \frac{x_f + \frac{1}{2} - x_w}{\text{size}} \\ t = \frac{1}{2} - \frac{y_f + \frac{1}{2} - y_w}{\text{size}} $$
In this formula, size is the point size gl_PointSize. $(x_f, y_f)$ is the fragment's window position, and $(x_w, y_w)$ is the vertex's window position, which is the same for every fragment. For example, when $(x_w, y_w) = (200, 100), \text{size} = 50$, the ranges of $x_f$ and $y_f$ are $[175, 225]$ and $[75, 125]$, respectively. Here is what point rasterization looks like in WebGL:
The $(s,t)$ coordinates and adjustable point size make it possible to draw textured squares with point primitives, for example fixed- or variable-size billboards and particle systems.
Line Rasterization
Line segments come from a line strip, a line loop, or a sequence of independent lines.
When a line segment intersects the clipping volume, it is clipped to the volume boundary. Vertex attributes and varying values at the new endpoints are generated by linear interpolation before rasterization.
Use LineWidth to set the rasterized line width. Implementations define the supported range, which often contains only 1.
The rasterizer first classifies a line as x-major or y-major. A line is x-major if its slope is in $[-1,1]$. Otherwise it is y-major.
GL uses the “diamond-exit” rule to decide which fragments a line produces. For a fragment centered at window coordinates $(x_f,y_f)$, define a diamond-shaped region as the intersection of four half-planes:
$$ R_f = \{(x, y) \Big| | x-x_f | + |y-y_f| < 1/2 \} $$
A segment from $\mathbf{p}_a$ to $\mathbf{p}_b$ generates fragments whose $R_f$ it intersects. If $\mathbf{p}_b$ lies inside $R_f$, the segment skips the fragment corresponding to $\mathbf{p}_b$.
When $\mathbf{p}_a$ and $\mathbf{p}_b$ lie on fragment centers, the rule reduces to a slightly modified Bresenham algorithm. The line is half-open: the final fragment is skipped. When connected segments are rasterized, a shared endpoint is generated once. Standard Bresenham would generate it twice.
The demo uses the diamond-exit rule by default and allows arbitrary floating-point endpoints. You can switch to the Bresenham algorithm, which first rounds the endpoints to fragment centers:
Next we compute the associated data for each fragment. Let the fragment center be $\mathbf{p}_r = (x_d, y_d)$ and the endpoints be $\mathbf{p}_a = (x_a, y_a)$ and $\mathbf{p}_b = (x_b, y_b)$. Define
$$ t = \frac{(\mathbf{p}_r - \mathbf{p}_a) \cdot (\mathbf{p}_b - \mathbf{p}_a)}{||\mathbf{p}_b - \mathbf{p}_a||^2} $$
Then the fragment's associated value $f$ is
$$ f = \frac{(1-t) f_a / w_a + t f_b / w_b}{(1-t) / w_a + t / w_b} $$
Here $f_a$ and $f_b$ are the endpoint values, and $w_a$ and $w_b$ are the endpoints' clip-space $w$ coordinates. Depth (window $z$) uses linear interpolation:
$$ f = (1-t) f_a + t f_b $$
Polygon Rasterization
Polygons come from a triangle strip, a triangle fan, or a sequence of independent triangles. Like points and lines, their rasterization is controlled by a set of state variables.
First determine whether the polygon is front-facing or back-facing from the sign of its area in window coordinates:
$$ a = \frac{1}{2} \sum_{i=0}^{N-1} x^i_w y^{i \oplus 1}_w - x^{i \oplus 1}_w y^i_w $$
Here $x_w^i$ and $y_w^i$ are the window coordinates of vertex $i$ of an $n$-vertex polygon, and $i \oplus 1$ means $(i+1)$ modulo the vertex count. Counterclockwise winding gives a positive area and clockwise winding gives a negative area. With FrontFace set to CCW, use the sign as computed. With CW, reverse it first. Section 3.5.1 of the OpenGL ES 2.0 specification describes this sign reversal the other way around. Section 3.6.1 of the ES 3.0 specification corrects it. A positive sign after applying the winding setting means the polygon is front-facing. Otherwise it is back-facing. With counterclockwise vertices, FrontFace set to CCW keeps the positive sign. With CW the sign becomes negative, so counterclockwise polygons are back-facing.
Use CullFace to control which faces are culled.
The rule for generating polygon fragments is called point sampling.
After generating the fragments of a triangle, compute their associated data from barycentric coordinates. These are three numbers $a, b, c$ in $[0, 1]$ satisfying $a+b+c=1$. They can express any point inside the triangle or on its boundary:
$$ p = a p_a + b p_b + c p_c $$
Here $p_a$, $p_b$, and $p_c$ are the triangle vertices, and $a, b, c$ are given by:
$$ a = \frac{A(p p_b p_c)}{A(p_a p_b p_c)}, b = \frac{A(b p_a p_c)}{A(p_a p_b p_c)}, c = \frac{A(p p_a p_b)}{A(p_a p_b p_c)} $$
$A(lmn)$ denotes the area in window coordinates of the triangle with vertices $l, m, n$.
Let a vertex attribute, such as position, texture coordinates, or normal, have values $f_a, f_b, f_c$ at vertices $p_a, p_b, p_c$, respectively. Then the value $f$ at a fragment is:
$$ f = \frac{a f_a / w_a + b f_b / w_b + c f_c / w_c}{a / w_a + b / w_b + c / w_c} $$
Here $w_a, w_b, w_c$ are the clip-space $w$ components of $p_a, p_b, p_c$, respectively. Evaluate $a, b, c$ at the exact fragment center.
As with line rasterization, depth (window $z$) uses linear interpolation:
$$ f = a f_a + b f_b + c f_c $$
This demo simulates triangle rasterization with barycentric coordinates:
Drag the triangle vertices and make the triangle very thin. Gaps start to appear between the rasterized fragments. This is sub-pixel aliasing. Increasing the rasterization sample count with SSAA or MSAA can reduce it. Diving into Anti-Aliasing describes several kinds of aliasing and how to address them.
Depth Offset
You can configure polygon rasterization to compute a depth offset for every fragment. Set the parameters with PolygonOffset(float factor, float units) and activate the feature by calling Enable with POLYGON_OFFSET_FILL.
When rendering two overlapping surfaces, a depth offset can prevent z-fighting. It can also serve as a depth bias in shadow mapping.
Fragment depth is always clamped to $[0, 1]$, even after adding the offset.
Point, line, and polygon rasterization all sample the fragment center once, so all can produce aliasing. Some graphics APIs provide multisample anti-aliasing (MSAA) to reduce this.