Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 50 ยท Ray Tracing and Rasterization Pipeline

The Rasterization Pipeline

Follow a triangle through the graphics pipeline: vertex shader, clipping, perspective divide, viewport, rasterization, fragment shader and depth test, with a live mini-GPU.

A ray tracer asks, for every pixel, "what is in this direction?". A rasterizer asks the opposite question: for every triangle, "which pixels does it cover?". The second question is much cheaper to answer and maps well onto parallel hardware, which is why every graphics card, phone and game console is built around it. The sequence of steps that turns triangles into pixels is the rasterization pipeline (or graphics pipeline).

You have already met almost every stage separately in this course: transformations, projection, clipping, triangle filling, the z-buffer and shading. This lesson shows how they connect, in the order the data flows, and then lets you run a miniature pipeline that you can inspect stage by stage. Understanding it makes WebGL, which comes next, much less mysterious.

The big picture

The pipeline has two halves. The first works on vertices and decides where triangles land on the screen. The second works on pixels (fragments) and decides what color each one gets.

flowchart LR
  A["Vertex data<br/>(positions, normals, uv)"] --> B["Vertex shader<br/>(programmable)"]
  B --> C["Primitive assembly<br/>(triangles)"]
  C --> D["Clipping"]
  D --> E["Perspective divide"]
  E --> F["Viewport transform"]
  F --> G["Face culling"]
  G --> H["Rasterization<br/>(find covered pixels)"]
  H --> I["Fragment shader<br/>(programmable)"]
  I --> J["Depth test and blending"]
  J --> K["Framebuffer"]

Two stages are programmable (shaders); the others are fixed-function hardware or configured with settings.

Let us walk through each stage, using the same terms that OpenGL, Vulkan, Direct3D and Metal use.

1. Vertex processing

Each vertex arrives with attributes: position, perhaps a normal, texture coordinates, a color. The vertex shader runs once per vertex. Its main job is to compute the vertex's clip-space position, by multiplying with the model, view and projection matrices from the earlier chapters:

TEXT
clipPosition = Projection * View * Model * position      (a 4D vector x, y, z, w)

It also outputs varyings (values to be interpolated across the triangle), such as a color, texture coordinates or a transformed normal. The vertex shader sees only one vertex at a time and cannot look at its neighbors.

2. Primitive assembly

The shaded vertices are grouped into primitives according to the index list: three at a time for triangles, two for lines, one for points. From here on we talk about triangles.

3. Clipping

The projection matrix maps the view volume to a box in clip space. A point is inside if:

TEXT
-w <= x <= w     -w <= y <= w     -w <= z <= w

Triangles fully inside pass through, triangles fully outside are discarded, and triangles that cross a plane are cut, using the polygon clipping you learned in Sutherland-Hodgman clipping. The cut may produce a polygon with more vertices, which is split back into triangles, and the varyings are interpolated at the new vertices.

Clipping happens before the perspective divide because after dividing by w, a point behind the camera (negative w) would flip to the wrong side of the screen. Clipping against the near plane is the one that truly matters. Most hardware skips the side planes and instead just limits the pixel search to the screen, using a large "guard band".

4. Perspective divide

Divide x, y and z by w. The result is normalized device coordinates (NDC), where everything visible lies in a cube from -1 to 1 on each axis (see projection). This single division is what makes distant objects smaller. The pipeline also keeps 1/w, which it needs later for correct interpolation.

5. Viewport transform

NDC to pixels: scale and shift into the window rectangle, flipping y because screen y points down (see window-to-viewport mapping):

TEXT
xScreen = (xNDC + 1) / 2 * width
yScreen = (1 - yNDC) / 2 * height
zDepth  = (zNDC + 1) / 2         // 0 = near, 1 = far

6. Culling

Triangles facing away from the camera can be dropped by the sign of their screen-space area, which is back-face culling in its cheapest form. For closed objects it removes about half of the triangles before any pixel work.

7. Rasterization

For each remaining triangle, find every pixel center it covers, using a bounding box and edge functions as in triangle rasterization. For each covered pixel, create a fragment: a candidate pixel with an interpolated depth and interpolated varyings.

The interpolation must be perspective-correct. Screen-space barycentric weights (b0, b1, b2) are linear in screen space, but attributes such as colors and texture coordinates are linear in world space, which differs under perspective. The fix is to interpolate attribute / w and 1 / w linearly, then divide:

TEXT
attr = (b0 * a0/w0 + b1 * a1/w1 + b2 * a2/w2) / (b0/w0 + b1/w1 + b2/w2)

Depth is the exception: NDC z is already linear in screen space, so z = b0*z0 + b1*z1 + b2*z2 works directly.

8. Fragment processing

The fragment shader runs once per fragment and outputs a color. It can sample textures, compute lighting per pixel (Phong shading), or do anything else you can express as a function of the interpolated varyings. This is where most of a modern GPU's time is spent.

9. Depth test and blending

Last come the per-fragment tests from the z-buffer lesson: if the fragment is nearer than the stored depth, write its depth and color; otherwise throw it away. Blending can instead mix the new color with the existing one to render transparency. The result lands in the framebuffer, ready to be shown.

A mini GPU you can inspect

The demo below implements all of these stages in about a hundred lines of JavaScript: a vertex "shader" function, near-plane clipping with varying interpolation, the perspective divide, the viewport transform, culling, a barycentric rasterizer with perspective-correct varyings, a choice of fragment "shaders" and a depth buffer. A cube with colored faces is drawn, and counters under the picture show how many triangles and fragments each stage produced.

Things to try:

  • Pull Camera distance down toward 2.3: the cube starts to cross the near plane, and the counter "after clipping" rises above the original 12 because clipped triangles are split.
  • Switch the fragment shader to Checkerboard (uv), then back: the texture coordinates are varyings, interpolated with perspective correction.
  • Turn off Face culling and the "culled" counter drops to zero while fragments double; with Depth test off as well, the picture only looks right by luck of drawing order.
  • Show the Wireframe to see the triangles that survived.
A mini GPU pipeline

A real GPU does the same work, but thousands of vertices and fragments are processed at once, and the shaders you write run on its many small cores.

A second look: where does the time go?

The pipeline is a conveyor belt, so the slowest stage sets the speed. Typical bottlenecks:

  • Vertex-bound: too many tiny triangles, each costing a vertex shader run and setup, but covering few pixels.
  • Fill-bound (fragment-bound): big triangles, expensive fragment shaders, or lots of overdraw (pixels shaded and then covered by nearer surfaces). Drawing front to back, or an early depth test, avoids most of that waste.
  • Bandwidth-bound: reading textures and writing the framebuffer move a lot of memory.

The counters in the demo hint at this: "fragments generated" is usually much larger than the number of pixels actually visible once culling and depth tests are off.

Rasterization versus ray tracing

Both produce images from the same scene, but organize the work differently.

Rasterization Ray tracing
Loops over Triangles, then covered pixels Pixels, then objects
Visibility Z-buffer, per fragment Nearest hit along the ray
Shadows, reflections Approximated (shadow maps, environment maps) Natural: more rays
Cost Roughly linear in triangles plus pixels Per-ray cost with acceleration structures (BVH)
Strength Very fast, predictable, real time Physically accurate effects, global illumination
Typical use Games, UI, CAD, the web Film, architecture, "ray tracing" modes in games

They are not rivals so much as partners: modern engines rasterize the main view and use ray tracing hardware for shadows, reflections and lighting, and offline renderers sometimes rasterize the first hit to save time. If you want to compare the two ideas in your own code, revisit ray tracing basics.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does clipping happen before the perspective divide?

    Dividing by a negative w mirrors points, so geometry crossing the camera plane must be cut while still in 4D clip space, where the test -w <= z is still meaningful.
  2. 2. Which two stages of the pipeline are programmable by the developer in WebGL and OpenGL?

    You supply a vertex shader (per vertex) and a fragment shader (per fragment). Clipping, the viewport transform, rasterization and the depth test are fixed-function.
  3. 3. What is a fragment?

    Rasterization generates one fragment per covered pixel. The fragment shader gives it a color, and the depth test decides whether it becomes part of the image.
  4. 4. Why must varyings such as texture coordinates be interpolated with attribute/w and 1/w?

    Attributes vary linearly in 3D space, not in screen space. Interpolating attribute/w and 1/w linearly and dividing gives the perspective-correct value.
  5. 5. What does back-face culling remove?

    It uses the sign of the projected triangle's area to find triangles facing away, which for closed solid objects cannot be visible anyway.

Summary

  • The pipeline turns triangles into pixels: vertex shader, primitive assembly, clipping, perspective divide, viewport transform, culling, rasterization, fragment shader, depth test and blending.
  • The vertex shader outputs a clip-space position and varyings; clipping uses the -w <= x, y, z <= w volume and happens before the divide.
  • The perspective divide gives NDC; the viewport transform maps NDC to pixels and flips y.
  • Rasterization creates fragments with interpolated depth (linear in screen space) and perspective-correct varyings (interpolate a/w and 1/w).
  • The fragment shader colors each fragment; the depth test decides which survive.
  • Rasterization loops over triangles and is fast; ray tracing loops over pixels and gets shadows and reflections naturally. Real-time engines mix the two.

Next, hand this pipeline over to real graphics hardware in WebGL Overview.