Introduction

In ddx and ddy Explained we saw that the graphics card works out ddx and ddy by running fragment shaders (Direct3D’s pixel shaders) in 2×2 groups of neighbouring pixels and subtracting their values, and parts 2 and 3 of those posts, in the Shader Techniques series, showed what that makes possible. This post covers the catch: a few rules you need to follow, and the hidden extra work the 2×2 groups cause, called helper pixels. The demo at the end shows where helper pixels run, using your own graphics card.

Things to Watch Out For

  • Careful inside if statements. All four pixels in a group must reach ddx together, because they need each other’s values. If an if (or a loop) sends some pixels of a group one way and the rest another, the result is undefined: it may be garbage, and some compilers refuse to compile it. This applies to normal texture reads too, because they use ddx and ddy behind the scenes. Work out ddx and ddy (and do your texture reads) before the if, or use SampleGrad/textureGrad or SampleLevel/textureLod inside it.
  • They’re measured in pixels of the image being drawn. The same object gives different answers at a different resolution or distance. That’s usually exactly what you want, but remember it when you compare against fixed numbers.
  • Mainly in fragment shaders. Vertex shaders don’t run in 2Ă—2 groups, so ddx and ddy aren’t available there. (DirectX 12 with Shader Model 6.6, and Vulkan with an extension, also allow them in compute shaders (general-purpose programs that run on the graphics card without drawing anything), which then arrange their threads into 2Ă—2 groups.)
  • Answers are shared. As the demos in part 1 showed, both pixels in a pair get the same answer, and a coarse derivative can share one answer across all four. They’re an estimate, not an exact per-pixel value, and they can miss or misplace sudden changes, as the edge demos showed.
  • The 2Ă—2 groups cost extra work. The graphics card often has to run the fragment shader for pixels that are never drawn, so that the drawn ones have neighbours to subtract from. These are called helper pixels, and the next section covers them.

Helper Pixels

Because fragment shaders always run in 2×2 groups, the graphics card has a problem whenever only some of a group’s four pixels need drawing. The other pixels are still needed, because ddx and ddy need their values to subtract, so the graphics card runs the fragment shader for them anyway. These extra copies are called helper pixels (HLSL calls them helper lanes, and GLSL calls them helper invocations). A helper pixel runs the shader’s instructions like any other pixel, but everything it produces is thrown away: it never changes the image, and any writes it makes to memory are ignored.

Helper pixels turn up in more places than you might expect:

  • Along every edge of every triangle. Any 2Ă—2 group that a triangle’s edge cuts through is only partly covered, and the uncovered pixels in it run as helpers. This happens for big triangles too, but a big triangle also has lots of fully covered groups in its middle, so the extra work is small in comparison. The smaller or thinner a triangle is, the more of its groups sit on an edge. A triangle that covers a single pixel runs the shader four times, three of them as helpers.
  • Where triangles meet. A mesh is made of triangles that share edges, and the graphics card handles each triangle separately. A 2Ă—2 group split by a shared edge runs once for each triangle that touches it, so its four pixels can cost eight or more runs of the shader, and many of those are helpers.
  • Where a triangle is partly hidden. Graphics cards normally test depth before running the fragment shader (an early depth test), so pixels hidden behind something already drawn can be skipped. But if a group has some visible and some hidden pixels, the hidden ones still run, as helpers for the visible ones. (If a shader writes its own depth value, and in some cases if it uses discard, the depth test has to wait until after the shader, and hidden pixels then run in full.)
  • After discard. A shader can throw away its pixel part-way through with discard (or clip in HLSL), for example to cut out the see-through parts of a leaf texture. In DirectX, a discarded pixel carries on running as a helper, so that its neighbours’ ddx and ddy still work. Vulkan, and OpenGL with an extension, offer the same thing as demote. In plain OpenGL and WebGL, don’t rely on ddx and ddy after some pixels in a group have discarded.

Helper pixels take real time on the graphics card, which is one reason why lots of tiny or long, thin triangles are slow to draw. A shader can check whether it’s running as a helper with IsHelperLane() in HLSL (added in Shader Model 6.6), or gl_HelperInvocation in GLSL (OpenGL 4.5, OpenGL ES 3.1 and Vulkan). WebGL2 is based on OpenGL ES 3.0, so it has neither.

Seeing Helper Pixels

WebGL doesn’t let you look at helper pixels directly: everything they produce is thrown away, and a WebGL2 shader can’t ask “am I a helper?”. So the demo below works them out in two steps.

  1. It draws some triangles on your graphics card at a tiny 32×20 resolution, then reads back which pixels each triangle actually drew, after the depth test. Each triangle’s drawn pixels are shown in its own colour. Every other pixel in a 2×2 group that a triangle drew into must have run as a helper for that triangle. Those pixels are striped, and on wider screens a number shows how many times each one ran as a helper. A striped pixel can also have a colour: it was drawn by one triangle and ran as a helper for another.
  2. For proof, tick Show proof. Every copy of this demo’s shader, drawn pixel or helper, also works out whether its own pixel centre is inside its triangle: 1 if it is, 0 if it isn’t. A drawn pixel is always inside, so if its ddx or ddy of that number isn’t 0, the 0 must have come from a pixel in its group that is outside the triangle and really did run: a helper pixel. Those drawn pixels get a dot. If your graphics card uses coarse derivatives, a whole group shares one answer, so dots appear on whole groups. The proof can’t see helpers caused by the depth test, because those pixels are inside their triangle.

Where helper pixels run, worked out on your graphics card

Things to try:

  • One big triangle. Helpers run all the way along its edges, even though the triangle is large.
  • A mesh of triangles. Shrink the triangles with the slider and watch the extra work climb. Pixels along shared edges are drawn by one triangle and run as helpers for its neighbours.
  • Long, thin triangles. Almost every group they touch sits on an edge, so they’re very wasteful for the few pixels they draw.
  • A triangle partly hidden. The small front triangle is drawn first. When the big triangle behind it is drawn, its pixels hidden behind the front triangle are skipped, except in groups that also have visible pixels: those hidden pixels run as helpers, along the front triangle’s edges.

The demo assumes each triangle’s 2×2 groups are handled separately, which is the standard way graphics cards are described as working. Some graphics cards have tricks to cut down the waste, so real numbers can be lower.

Under the hood

Each helper pixel takes up a lane in a group of 32 or 64 shader copies, exactly like a real pixel (How GPUs Run Shaders), so a group full of partly covered 2×2 groups does much less useful work than its size suggests. Games keep an eye on this: Unreal Engine, for example, has a Quad Overdraw view that colours the screen by how many times each 2×2 group was shaded. It’s also one reason games swap distant objects for simpler versions with fewer, larger triangles (levels of detail): bigger triangles fill more of their 2×2 groups.

Questions You Might Have

Can I switch helper pixels off?

No. Graphics cards always shade pixels in 2×2 groups, because that’s how they work out ddx, ddy and the mipmap level for every texture read. What you can control is how many are wasted: fewer, larger triangles mean fewer partly covered groups.

Summary

  • Work out ddx, ddy and ordinary texture reads before any if that could split a 2Ă—2 group, or use the versions that take your own derivatives or mip level.
  • Derivatives are measured in pixels of the image being drawn, are mainly available in fragment shaders, and are shared estimates rather than exact per-pixel values.
  • Helper pixels are extra fragment shader runs for the uncovered pixels of partly covered 2Ă—2 groups. Their results are thrown away.
  • They appear along triangle edges, where triangles meet, where a triangle is partly hidden and after discard, so small and thin triangles cost the most.

Further Reading