In a rasterizing GPU pipeline, what stages does one triangle of a character mesh pass through from its vertex buffer to the pixels it covers?
answer
- vertices first, pixels last
- programs at two ends, hardware between
- clip before dividing by w
- depth test can move before shading
basics
~20 sVertices are fetched and transformed by a vertex program, assembled into triangles, clipped, divided by w, mapped to the viewport and rasterized into fragments, which are depth-tested, shaded by a fragment program and blended into the colour target.
solid answer
~40 s**Input assembly** reads indices and vertex attributes and groups them into triangles, often reusing recently transformed vertices. The **vertex program** (programmable) outputs a clip-space position `(x, y, z, w)` per vertex; optional tessellation, geometry or mesh stages can sit here. Fixed-function hardware then assembles the triangle, optionally culls back faces, **clips** it against the view volume in homogeneous coordinates, performs the **perspective divide** to normalized device coordinates and the **viewport transform** to pixel coordinates plus a stored depth. The **rasterizer** finds the covered pixel samples and emits fragments with interpolated outputs. Each fragment is depth/stencil tested **early** when that is safe, runs the **fragment program** (programmable), is tested **late** if early testing was not possible, and the configurable **blend** stage combines it with the colour already in the target, in submission order.
go deeper
Recite the stages in order and mark each one programmable or fixed-function. Know what a fragment is and what the depth buffer is for.
Explain why clipping happens before the divide, what the viewport transform outputs, and when the depth test can run before the fragment program instead of after it.
Tie stages to costs and failures: which stage limits a dense mesh versus a full-screen effect, and how program-written depth or discards change early depth testing.
Weigh which work stays on the classic pipeline and which moves to compute or mesh-style front ends, and what tile-based versus immediate-mode hardware means for the frame's pass structure.
## The pipeline as a contract A rasterizing GPU turns a stream of triangles into pixels through a fixed sequence of **stages**. The major graphics APIs describe the same logical order, and the hardware promises that the image looks as if each triangle went through the stages one after another, in the order the draws were submitted. Following one triangle of a character mesh from its vertex buffer to the pixels it covers is the standard way to explain it. Two kinds of stage alternate: - **Programmable** stages run a small program you write, once per vertex or once per fragment. - **Fixed-function** stages are dedicated hardware you can only configure: you pick a mode, a comparison or a factor, but you cannot change what they do. ## Front end: from buffers to clip-space triangles 1. **Input assembly** reads the index buffer, fetches each referenced vertex's attributes (position, normal, UVs, skin weights) and groups indices into triangles. With indexed drawing, many GPUs reuse a vertex that was transformed recently instead of running its program again. 2. **Vertex processing** runs the **vertex program** once per vertex. Its essential output, or that of the last stage before clipping, is a position in **homogeneous clip space**, a 4-component `(x, y, z, w)`; everything else it outputs (normals, UVs) is carried along for interpolation. 3. **Optional geometry stages** can sit here: tessellation stages that subdivide patches, a geometry stage that emits primitives, or, on recent GPUs, task and mesh programs that replace input assembly and vertex processing with a compute-like front end. 4. **Primitive assembly** gathers the three transformed vertices into a triangle, and face culling can drop it if it faces away from the camera. ## The fixed-function middle 5. **Clipping** cuts the triangle against the view volume in clip space (`-w <= x <= w`, the same for `y`, and a depth range). Triangles fully outside are dropped; ones crossing the near plane are cut into a smaller polygon. This happens before the divide because a vertex behind the eye has `w <= 0`. 6. The **perspective divide** computes `x/w`, `y/w` and `z/w`, giving **normalized device coordinates (NDC)**. 7. The **viewport transform** scales NDC to pixel coordinates in the render target and maps NDC depth into the stored depth range. 8. **Rasterization** (triangle setup and traversal) finds which pixel samples the triangle covers and produces one **fragment** per covered pixel, carrying a depth and the interpolated vertex outputs. ## Back end: tests, fragment program, blending 9. **Early depth/stencil test**: when nothing the fragment program does could change the outcome, the hardware tests depth and stencil before shading and throws hidden fragments away without running the program. 10. The **fragment program** runs per fragment and computes a colour; it may also write its own depth or discard the fragment. 11. **Late depth/stencil test**: when early testing was not possible, the test runs now, after shading, where the specification places it. 12. **Blending** combines the fragment's colour with the value already in the colour target using configured factors and operators, and the result is written. With multisampling, coverage is tracked per sample. ## Programmable vs fixed-function at a glance | Stage | Kind | What you control | |---|---|---| | Input assembly | fixed | vertex layout, index format, primitive type | | Vertex program | programmable | the whole program | | Tessellation, geometry, mesh | programmable, optional | the program | | Clipping, divide, viewport | fixed | viewport rectangle, depth range | | Rasterization | fixed | fill and cull modes, sample count | | Depth/stencil test | fixed | comparison, write masks, stencil operations | | Fragment program | programmable | the whole program | | Blending | configurable | factors and operators per target | The fixed stages stay in hardware because they do the same simple work for every draw at enormous rates: dedicated circuits are far cheaper per operation than general shader cores, and they also enforce the in-order result. Some tile-based GPUs let a fragment program read the current pixel value, which makes blending effectively programmable there. ## Logical order versus what the hardware does The order above is a contract about results, not a schedule. GPUs run thousands of vertices and fragments in parallel, move the depth test before shading when it is safe, and tile-based GPUs first sort a render pass's triangles into screen tiles and then rasterize one tile at a time in on-chip memory. None of that is visible: for each pixel, depth tests and blends still take effect in the order the triangles were submitted. Interviewers use this walkthrough to see whether you know where later topics plug in: - the depth buffer's uneven precision comes from the divide and the viewport mapping; - early depth testing depends on what the fragment program does; - in-order blending is why transparent surfaces have to be sorted by the application; - the stencil test gives a cheap per-pixel mask beside the depth test.
- Why is the depth test placed after the fragment program in the specification but usually run before it?The logical model tests late so a program that writes its own depth or discards fragments still behaves correctly. Hardware runs the test early whenever nothing the program does could change the outcome, which skips shading for hidden fragments. When the program writes depth, discards or has side effects, part or all of the test moves back after shading.
- Why do clipping, rasterization, depth testing and blending stay fixed-function?They do the same simple arithmetic for every draw at billions of operations per second, where dedicated circuits are much cheaper than general shader cores, and they enforce the in-order result per pixel. You configure them with modes, comparisons and factors. Some tile-based GPUs expose the current pixel value to the fragment program, making blending programmable there.
- Where does a compute program fit relative to this pipeline?Outside it. A compute dispatch has no input assembly, rasterizer, depth test or blending; it runs a program over a grid of work items that read and write buffers and images. Renderers use it before draws (culling, skinning, particle updates) and after them (post-processing, lighting passes), synchronized with the graphics work.
A car assembly line: some stations are robots you can only configure (clipping, rasterization, depth testing, blending), others are crews following your written instructions (vertex and fragment programs). Many cars are worked on at once, yet they leave the line in the order they entered.
- The vertex program performs the perspective divide and outputs pixel coordinates.
- Depth testing always runs after the fragment program, so every hidden fragment is shaded.
- Blending is written as a separate program, just like the vertex and fragment programs.
- Fragments are blended in whatever order their programs finish, so blend order is undefined.
- Back-face culling is something the fragment program does by discarding pixels.