skip to content

Why do GPUs shade pixels in 2x2 quads, and how does that cause wrong-mip seams when a texture is sampled inside a divergent branch?

level: seniorimportance: should knowfreq 45%

answer

  1. differences between neighbouring lanes
  2. four lanes per 2x2 block
  3. helper lanes outside the triangle
  4. undefined in divergent control flow
  5. gradients before the branch

basics

~20 s

GPUs shade 2x2 quads so derivatives come from differencing neighbouring lanes, and texture sampling uses them to pick a mip. Inside a divergent branch a neighbour may skip the code, the derivative is undefined, and wrong-mip seams appear.

solid answer

~50 s

Fragments are launched in **2x2 quads** so the hardware can compute **screen-space derivatives** of any value by subtracting a horizontal or vertical neighbour's copy. Implicit texture sampling uses the derivatives of the coordinates to choose a mip level. Pixels of a quad that the triangle does not cover still run as **helper lanes**: they compute values for their neighbours' derivatives, but their writes are discarded. If a sample sits inside a branch that only some lanes of a quad take, the neighbour's value was never computed, the derivative is **undefined**, and thin seams or sparkles of the wrong mip appear along the branch boundary. The fix is to compute the coordinates and gradients in uniform control flow and sample with **explicit gradients** or an explicit level, or to make the condition uniform per quad. The same quads make tiny triangles expensive: a one-pixel triangle still runs four lanes.

code

pseudocode · 11 lines
pseudocode
// Broken: implicit gradients taken inside a per-pixel branch
if pixel_is_wet:
    ripple_uv = uv * ripple_scale + time_offset
    colour = colour + sample_implicit(ripple_tex, ripple_uv)

// Fixed: coordinates and gradients computed in uniform control flow
ripple_uv = uv * ripple_scale + time_offset
grad_x = screen_derivative_x(ripple_uv)
grad_y = screen_derivative_y(ripple_uv)
if pixel_is_wet:
    colour = colour + sample_with_gradients(ripple_tex, ripple_uv, grad_x, grad_y)

go deeper

for a junior

Recall that pixels are shaded in 2x2 groups and that this is how a program gets screen-space derivatives. Know that texture sampling uses those derivatives to pick a mip level.

for a middle

Explain helper lanes, what happens to their writes, and why a derivative needs the neighbour to run the same code. Describe the undefined result inside a divergent branch.

for a senior

Diagnose wrong-mip seams that follow a branch boundary and change between GPUs, and fix them with gradients computed in uniform control flow. Quantify quad overshading for tiny triangles.

for a principal

Treat quad efficiency as a content constraint: triangle density targets, level-of-detail policy and shading approaches that decouple shading from rasterized quads trade against each other. Set rules for where derivatives may be taken.

## Why fragments come in quads A fragment program often needs to know how fast a value changes across the screen. The GPU answers that cheaply by shading pixels in **2x2 quads** and letting each lane read its neighbours. The **screen-space derivative** of a value `v` in x is the difference between the value in the right-hand and left-hand pixel of the same row of the quad, and the derivative in y is the difference between the lower and upper pixel. Some programming models expose two precisions: - **Coarse** derivatives compute one x and one y difference for the whole quad. - **Fine** derivatives compute them per row and per column, so the two rows of a quad can differ. Which one an ordinary derivative request returns varies between GPUs and drivers. ## Helper lanes A triangle edge rarely aligns with quad boundaries, so many quads contain pixels the triangle does not cover. Those pixels still run as **helper lanes**: - Their inputs are evaluated on the triangle's plane, extended past its edge, so their values continue smoothly. - They execute the program so that covered neighbours can difference against them. - Their colour, depth and memory writes are thrown away. - They cost the same arithmetic as a real lane. ## What derivatives are used for - **Implicit mip and anisotropy selection.** A texture sample without an explicit level takes the derivatives of its coordinates to estimate the pixel's footprint in the texture; how that footprint becomes a level belongs with texture filtering. - **Procedural antialiasing.** The sum of the absolute x and y derivatives of a value tells the program how wide one pixel is in that value's units, so an edge can be smoothed over exactly one pixel. - **Face normals.** The cross product of the derivatives of an interpolated position is perpendicular to the triangle, which gives a flat-shaded normal without per-face data. ## Derivatives inside divergent control flow The failure in the question follows from the mechanism: 1. A branch condition differs between lanes of one quad, for example a per-pixel wet flag. 2. Only the lanes that take the branch compute the texture coordinate used inside it. 3. A lane that takes it differences its coordinate against a neighbour that never computed one, so the difference reads whatever that neighbour's register held. 4. The derivative comes out wildly large or near zero, the sampler picks a far too blurry or far too sharp level, and a seam or line of sparkles appears exactly along the branch boundary. 5. Programming models declare this result **undefined**, so it may look acceptable on one GPU or driver and break on the next. The fixes all make the derivative defined again: - Compute the coordinate and its gradients **before** the branch, in uniform control flow, and inside the branch sample with **explicit gradients**. - Sample with an **explicit level** when the right level is known. - Hoist the sample itself out of the branch and select the result afterwards, accepting the extra read. - Make the condition uniform per quad or per draw, so all four lanes always agree. ## Quad overshading on small triangles Quads also set a floor on fragment cost per triangle, because a triangle launches every quad it touches: | Triangle coverage | Quads launched | Useful lanes | Share of useful work | |---|---|---|---| | 1 pixel inside one quad | 1 | 1 of 4 | 25% | | 2-pixel sliver across two quads | 2 | 2 of 8 | 25% | | 10,000-pixel triangle | about 2,500 to 2,700, the extra ones partly covered at the edges | nearly all | above 90% | A mesh whose triangles shrink to a pixel or two on screen can therefore cost up to four fragment invocations per covered pixel. Keeping triangles above a few pixels through level-of-detail selection, avoiding long thin triangles, and shading approaches that first rasterize triangle IDs and shade each visible pixel once all recover the waste; those choices belong to culling and to the shading architecture, but the quad is why they pay. ## How to recognise these problems - Seams that follow a branch boundary rather than a texture or mesh boundary point to undefined derivatives. - A defect that changes with the GPU, the driver or the compiler's optimisation level points the same way. - A GPU capture that reports helper-lane or quad-efficiency figures shows overshading directly; a dense mesh with poor quad efficiency is a level-of-detail problem as much as a shading one.

  • Why do derivative bugs inside branches often look fine on one GPU and broken on another?
    The result is undefined, not reliably wrong. One compiler may hoist the coordinate computation out of the branch, or leave a register holding a value that happens to be close; another may not. Anything resting on undefined behaviour can change with a driver update, so the fix must make the gradients defined rather than make the picture look right today.
  • How can a fragment program get a flat-shaded normal without per-face normals in the mesh?
    Take the screen-space derivatives of the interpolated position in world or view space and cross them. Both difference vectors lie in the triangle's plane, so their cross product is the face normal, up to normalisation and a sign that depends on the handedness convention. Helper lanes lie on the plane extended past the edge, so the result holds at triangle borders too.
  • Why can very dense meshes cost more fragment work than their pixel count suggests?
    Each triangle launches whole quads, so a triangle covering one or two pixels runs four or eight lanes for one or two useful results. When most triangles are that small, fragment invocations approach four per covered pixel. Level-of-detail meshes that keep triangles above a few pixels, or approaches that shade each visible pixel once after rasterizing IDs, recover the waste.

  • Derivatives are computed symbolically by the compiler, so branches cannot affect them.
  • Helper lanes write their colour to the pixels outside the triangle.
  • A one-pixel triangle costs exactly one fragment invocation.
  • Wrong mips along a branch edge mean the texture's mip chain is corrupt.
  • Sampling inside a divergent branch is safe if it looks right on my GPU.