skip to content

In the graphics pipeline, why are triangles clipped in homogeneous clip space before the perspective divide, and what do the divide and viewport transform produce?

level: middleimportance: should knowfreq 52%

answer

  1. w can be zero or negative
  2. linear in clip space, not after
  3. -w <= x, y <= w
  4. NDC, then pixels plus stored depth

basics

~20 s

Clipping runs before the divide because a vertex behind the eye has w <= 0, and dividing by it flips or explodes the position. The divide yields normalized device coordinates; the viewport transform maps them to pixel positions and stored depth.

solid answer

~50 s

The vertex program outputs `(x, y, z, w)`; for a perspective projection `w` is depth along the view direction. The view volume is linear: `-w <= x <= w`, `-w <= y <= w`, and `0 <= z <= w` or `-w <= z <= w` depending on the depth convention. A vertex behind the eye has `w < 0`, so dividing first would mirror it across the screen, and an edge crossing `w = 0` would map through infinity. Clipping at the near plane first leaves every `w` at least the near distance, and because the planes are linear, new vertices and their outputs interpolate exactly. The divide then gives NDC, with `x` and `y` in `[-1, 1]`; the viewport transform computes pixel `x0 + (x_ndc + 1) * width / 2`, the same for `y` (possibly flipped), and a depth inside the viewport's depth range.

code

pseudocode · 14 lines
pseudocode
// standard depth, clip-space convention 0 <= z <= w, so the near plane is z = 0
function clipAgainstNear(poly):          // poly: clip-space vertices carrying all outputs
    out = []
    for i in 0 .. poly.length - 1:
        a = poly[i]
        b = poly[(i + 1) mod poly.length]
        da = a.z                            // signed distance: >= 0 means inside
        db = b.z
        if da >= 0:
            out.append(a)
        if (da >= 0) != (db >= 0):          // the edge crosses the plane
            t = da / (da - db)
            out.append(lerp(a, b, t))       // position and every output, same t
    return out

go deeper

for a junior

Recall the order clip, divide, viewport, and that clipping keeps only what lies inside the view volume.

for a middle

Explain the w <= 0 problem, the clip-space inequalities, how a clipped edge creates a new vertex by interpolation, and what NDC and the viewport formula produce.

for a senior

Diagnose spikes or mirrored geometry near the camera, explain guard-band clipping, and reason about the depth range and y-flip conventions when porting between renderers.

for a principal

Discuss how clip-space and depth-range conventions are fixed across a codebase that targets several APIs, and what one consistent convention saves in projection, culling and post-processing code.

## Clip space and the view volume The vertex program outputs each vertex's position as a homogeneous 4-vector `(x, y, z, w)` in **clip space**. For a perspective projection, `w` holds the vertex's depth along the viewing direction, so it is positive for geometry in front of the eye. The visible region is described by linear inequalities in these four numbers: - `-w <= x <= w` and `-w <= y <= w` for the sides of the screen; - `0 <= z <= w` for depth in a 0-to-1 depth convention, or `-w <= z <= w` in a -1-to-1 convention; with standard (not reversed) depth the lower bound is the near plane and the upper bound the far plane. Because these bounds are linear, a point part-way along a straight edge between two vertices is a weighted average of the two in clip space, and every other vertex output (UVs, normals, colours) can be interpolated with the same weight. ## Why clipping has to come before the divide 1. A vertex behind the eye has `w < 0`. Dividing its `x` and `y` by a negative number flips their signs, so it lands on the opposite side of the screen from where its geometry really is. 2. A vertex on the eye plane has `w = 0`, and the divide is undefined. 3. An edge from a vertex in front of the eye to one behind it passes through `w = 0`. In screen space that edge runs out to infinity and comes back from the other side, so the rasterizer would fill a region the triangle never covered: the long spikes across the screen that appear when clipping is broken. Clipping against the near plane, which lies in front of the eye, cuts every triangle so that all its remaining vertices have `w` at least the near distance. After that the divide is safe. Picture a character walking past the camera: one triangle of the shoulder has a vertex behind the eye, and only the near-plane clip keeps it from smearing across the frame. ## How a triangle is clipped The classic method is **Sutherland-Hodgman** clipping: walk the polygon's edges against one plane at a time, keep the vertices on the inside, and wherever an edge crosses the plane emit a new vertex at the crossing. The signed distance to each plane is a linear expression, for example `w - x` for the right plane, or `z` for the near plane in a 0-to-1 convention. Clipping a convex polygon against one plane adds at most one vertex, so a triangle clipped against all six planes grows to at most nine vertices, which is then split into a fan of at most seven triangles. ## What GPUs actually clip Geometric clipping is expensive and rarely needed, so many GPUs avoid it for the sides of the screen: - They rasterize inside a **guard band**, a coordinate range much larger than the viewport, and reject pixels outside the viewport or scissor rectangle during rasterization. - They clip geometrically only triangles that cross the near plane or exceed the guard band; some designs also clip at the far plane, while others reject far fragments per pixel. - The image is identical either way; only the work differs. ## The perspective divide After clipping, the hardware divides by `w`: `x_ndc = x / w`, `y_ndc = y / w`, `z_ndc = z / w`. These are **normalized device coordinates (NDC)**: `x` and `y` lie in `[-1, 1]`, and depth in `[0, 1]` or `[-1, 1]` depending on the convention. The divide is what makes distant things small. It also makes stored depth non-linear in view distance, which is why depth precision is uneven. The hardware keeps `1/w` per vertex so that later interpolation of vertex outputs can be made perspective-correct. ## The viewport transform The **viewport transform** maps NDC onto the render target. For a 0-to-1 depth convention: | Output | Formula | |---|---| | pixel x | `x0 + (x_ndc + 1) * width / 2` | | pixel y | `y0 + (y_ndc + 1) * height / 2`, or the same with `y` flipped when row 0 is the top | | stored depth | `minDepth + z_ndc * (maxDepth - minDepth)` | Whether NDC `y = +1` lands on the top or bottom row differs between APIs, a common source of upside-down images when porting a renderer. The depth range is useful in its own right: setting `minDepth = maxDepth = 1` forces every fragment of a draw to the far plane, and giving a first-person weapon its own small slice at the front of the range keeps it from poking into walls.

  • What would you see if a renderer divided by w first and then rasterized a triangle with one vertex behind the camera?
    That vertex's negative w flips its x and y, so it projects to the opposite side of the screen, and the edges leading to it no longer follow the real triangle. The rasterizer fills a large wrong region, typically long spikes or a sheet stretching across the frame, flickering as the camera moves.
  • Why do many GPUs not clip against the left, right, top and bottom planes at all?
    Rasterizing in a guard band much larger than the viewport and rejecting pixels outside the viewport or scissor gives the same image without creating new vertices. Only triangles that cross the near plane, or are large enough to exceed the guard band, need real geometric clipping, which keeps the expensive path rare.
  • Why must a clipped vertex's attributes be interpolated in clip space rather than after the divide?
    Clip-space coordinates and vertex outputs vary linearly along an edge, so the crossing point and its UVs or normals come from one interpolation weight. After the divide, a fraction of the way along the screen-space edge is not the same fraction along the 3D edge, so a screen-space weight would give the new vertex the wrong depth and attributes.

  • Clipping happens after the perspective divide, in screen pixels.
  • Points behind the camera just project upside down and cause no harm.
  • Clipping a triangle against the view volume always yields one triangle.
  • Every GPU geometrically clips every triangle against all six planes.
  • The viewport transform is where the divide by w happens.