In a rasterizing GPU pipeline, how often do vertex, fragment and compute programs run, and what data does each one receive?
answer
- count invocations, not instructions
- vertices versus covered pixels
- rasterizer blends three vertex outputs
- per draw, per instance, per vertex
- compute sizes its own grid
basics
~20 sA vertex program runs about once per vertex, a fragment program about once per covered pixel, and a compute program once per thread of its dispatch. Fragments receive vertex outputs interpolated across the triangle, plus per-draw constants and resource reads.
solid answer
~50 sThe **vertex program** runs roughly once per vertex a draw references; indexed meshes often reuse recently transformed vertices, so the exact count is not guaranteed. It reads per-vertex attributes, per-instance data that advances once per instance, and constant data shared by the whole draw. The rasterizer **interpolates** its outputs across each triangle (perspective-correct by default, or flat from one vertex), and the **fragment program** runs once per covered pixel (per sample only when per-sample shading is requested) on those values, plus constants and texture or buffer reads. A **compute program** skips the rasterizer: it runs once per thread of a grid you size, grouped into workgroups that share on-chip memory, and finds its data by thread index. A 1920x1080 frame has about two million pixels while a character has tens of thousands of vertices, so per-fragment work usually dominates.
go deeper
Recall the three invocation rates (per vertex, per covered pixel, per compute thread) and the data rates (per draw, per instance, per vertex). Saying which program runs most often is the core of the answer.
Explain perspective-correct interpolation and the flat and screen-linear alternatives, and why vertex reuse makes the vertex count only approximate. Turn invocation counts into a cost estimate.
Use the frequencies to move work: hoist per-draw values out of programs, push linear quantities to the vertex stage, and say when interpolation would break a non-linear result. Mention helper lanes and overdraw inflating fragment counts.
Frame frequency as a budgeting tool: per-pixel work scales with resolution and overdraw, per-vertex work with geometry density. Set team conventions for which data lives per draw, per instance and per vertex.
## Three programs, three frequencies A GPU runs small programs at different points of a frame, and the first thing to know about each is **how many times it runs**. Cost is roughly instructions per invocation times invocations, so a cheap program that runs millions of times can outweigh an expensive one that runs thousands of times. | Program | Runs once per | Main inputs | Typical count | |---|---|---|---| | Vertex | vertex referenced by a draw (with reuse) | per-vertex attributes, per-instance data, per-draw constants | thousands to millions per frame | | Fragment | covered pixel, or covered sample with per-sample shading | interpolated vertex outputs, constants, texture and buffer reads | millions per frame (2,073,600 pixels at 1920x1080 before overdraw) | | Compute | thread of a dispatch grid | thread index, constants, buffers and textures | whatever the dispatch requests | The vertex count is approximate on purpose. With an index buffer, a vertex shared by several triangles can be served from a small cache of recently transformed results instead of running again, but no API promises how much reuse you get. ## Data at three frequencies Every program reads data that changes at some rate, and choosing that rate is a cost decision: - **Per-draw constant data** is identical for every invocation of a draw: the camera's matrices, the sun's direction, a material's tint. All lanes read the same value, which is cheap. - **Per-instance data** steps once per copy in an instanced draw: a tree's world transform or its colour variation. Every vertex of one instance sees the same value. - **Per-vertex attributes** live in vertex buffers, one entry per vertex: position, normal, texture coordinates, skin weights. - **Resource reads** are textures and buffers indexed by a computed address. Any stage can issue them, and each one waits on memory. Storing a per-draw value per vertex wastes memory and bandwidth; computing a per-draw value inside every fragment wastes arithmetic millions of times over. ## From vertex outputs to fragment inputs A vertex program writes a clip-space position and any number of extra outputs. After clipping and the viewport transform, the rasterizer finds which pixels a triangle covers and their **barycentric weights** `b0, b1, b2`, then builds each fragment's inputs in one of three ways: 1. **Perspective-correct** (the default): interpolate `a/w` and `1/w` linearly in screen space, then divide: `a = (b0*a0/w0 + b1*a1/w1 + b2*a2/w2) / (b0/w0 + b1/w1 + b2/w2)`, where `w` is each vertex's clip-space w. This keeps texture coordinates correct on a floor seen at an angle. 2. **Screen-linear**: use the barycentric weights directly and ignore `w`. It suits values that really are linear on screen, and distorts anything attached to a surface under perspective. 3. **Flat**: copy one vertex's value, chosen by a fixed convention, to every fragment of the triangle. Use it for identifiers and bit masks that must not be blended. Interpolation is linear in these spaces, so a quantity that is not linear across the triangle, such as a normalised direction, arrives only approximately. Interpolated normals need renormalising, and where lighting should be evaluated is a separate decision. ## What a fragment program does with its inputs The fragment program runs on those interpolated values, reads constants, samples textures and writes one or more colours and, optionally, depth. Its invocation count is higher than the visible pixel count for two reasons: **overdraw**, when a surface is shaded and later covered by a nearer one, and **helper lanes**, the extra invocations that fill out 2x2 pixel quads at triangle edges so that screen-space derivatives can be taken. With multisampling, the program normally still runs once per pixel per triangle and its result is copied to the covered samples. ## Compute invocations A compute program has no rasterizer in front of it: - It runs over a grid of **workgroups** whose size the programmer picks; a size that is a multiple of the GPU's SIMD width (typically 32 or 64 lanes, depending on the GPU) avoids idle lanes. - Threads in one workgroup share fast on-chip memory and can synchronise with each other. - It receives no interpolated inputs; it finds its data from its thread and group indices, and texture reads usually pass an explicit level of detail. - It produces results only by writing to buffers or textures. ## Why the frequencies decide cost 1. **Count invocations first.** Per-draw work runs once, per-vertex work once per vertex, per-fragment work once per covered pixel times overdraw. 2. **Move work up a frequency when the value allows it.** A value that is linear across a triangle, such as a scrolled texture coordinate, can be computed per vertex and interpolated. A value constant for the draw can be computed once on the CPU. 3. **Check non-linear work before moving it.** A specular highlight computed per vertex disappears between vertices; a direction must be renormalised after interpolation. 4. **Count outputs too.** Every extra vertex output is data the GPU must store and set up for interpolation. As a worked example, a character of 30,000 vertices covering 200,000 pixels pays about 200,000 x 20 = 4,000,000 instructions per frame for a 20-instruction texture-coordinate transform run per fragment, against 30,000 x 20 = 600,000 run per vertex: moving it saves about 3.4 million instructions per frame for that one draw, ignoring vertex reuse and helper lanes.
- Why does screen-linear interpolation distort a texture on a floor seen at a grazing angle?Under perspective, equal steps across the screen cover more and more of the floor as it recedes. Interpolating texture coordinates linearly in screen space spreads them evenly on screen, so the texture bends along each triangle's diagonal. Interpolating `u/w` and `1/w`, which are linear in screen space, and dividing restores the correct value.
- When would you pass a value flat rather than interpolated?When it is an identifier or a choice rather than a quantity: a material index, an instance ID, a bit mask of features. Interpolating such a value between vertices produces numbers that belong to none of them. Flat passes one vertex's value, chosen by a fixed convention, to every fragment of the triangle.
- How does instancing change what the vertex program reads?One draw renders many copies of a mesh. Per-vertex attributes step once per vertex as usual, while per-instance data steps once per copy, fetched by instance index or supplied as a stream that advances per instance. The program combines them, for example transforming each vertex by its instance's world matrix, so thousands of trees need one draw and one copy of the mesh.
- The vertex program runs once per pixel to place it on screen.
- The fragment program runs once per triangle, not once per covered pixel.
- Vertex outputs reach fragments as a plain average of the three vertices.
- Per-instance data must be copied into every vertex of each instance.
- A compute program receives interpolated inputs just like a fragment program.