Introduction

Part 2 used ddx and ddy to pick mip levels and smooth edges. This post uses them on a pixel’s position to work out which way its triangle faces, giving flat shading with no extra data in the mesh, then rounds up a few more uses. It helps to have read Basic Lighting and The Cross Product.

Making a Normal from ddx and ddy

Lighting needs a normal: a direction pointing straight out of the surface. Usually a normal is stored with each corner (vertex) of the mesh and blended across each triangle, which makes the surface look smooth.

You can also make one from ddx and ddy. Pass the 3D position of each vertex (for example in world space) from the vertex shader to the fragment shader, so each pixel knows its own position on the triangle. ddx(position) is how that position changes one pixel to the right, and ddy(position) is how it changes one pixel down. All four pixels of a group are worked out on the same flat triangle, so both of those directions lie flat along the triangle. (Where a group hangs over the triangle’s edge, the pixels outside it are worked out as if the triangle carried on; these are the helper pixels covered in their own post.) The cross product of two directions (a standard maths function in every shading language) gives a direction at right angles to both, which is the triangle’s own normal, in the same space as the position:

float3 flatNormal = normalize(cross(ddx(position), ddy(position)));

Depending on your coordinate conventions, this can point into the surface instead of out of it. If it does, swap the two arguments or negate the result. The demo below simply flips it whenever it points away from the camera.

This gives a faceted, low-poly look with no extra data in the mesh at all. Try it below, and change the detail to see how it behaves with more or fewer triangles. The last two options show the direction of ddx and ddy of the position as colours. Each colour changes only slightly across a triangle (because of perspective, the direction of a one-pixel step varies a little across the triangle), but both directions always lie flat along the triangle, so the normal made from them is the same everywhere on it.

Smooth normals compared with flat normals made from ddx and ddy

Smooth normalsFlat, from ddx/ddy

Other Uses

  • Bump mapping without tangents: ddx and ddy of a height read from a texture, together with ddx and ddy of the position, are enough to tilt the normal and fake bumps, without the extra tangent directions (see Normal Mapping) normally stored in the mesh.
  • Cheap edge detection: a large ddx or ddy of depth or colour suggests an edge. Because of the fixed pairs, this only sees edges between the two pixels of a pair, so proper outlines usually use a separate pass instead.
  • Fading out detail: when ddx and ddy of a pattern’s coordinates are large, its details are smaller than a pixel, so you can fade them out before they shimmer.
Under the hood

There’s another way to get flat shading. GLSL’s flat qualifier (nointerpolation in HLSL) switches off blending for an output, so every pixel of a triangle gets the value from just one of its corners, the provoking vertex. But that needs a normal stored per face, so corners shared by several faces must be stored several times, making the mesh bigger. The ddx/ddy version needs no mesh changes and costs two derivatives and a cross product per pixel, which is cheap on modern graphics cards.

Questions You Might Have

Why would a game want flat shading?

Mostly for style: the faceted, low-poly look is popular in stylised games. It’s also handy for debugging, because it shows exactly where the triangles are, and for meshes that are generated on the fly, such as terrain, where computing smooth normals would take extra work.

Summary

  • ddx and ddy of the position are two directions lying flat along the triangle.
  • Their cross product is the triangle’s own normal, giving flat, faceted lighting with no extra mesh data.
  • Derivatives also help with bump mapping, edge detection and fading out detail before it shimmers.

All of these uses rely on the graphics card running fragment shaders in 2Ɨ2 groups, and that has a cost. Helper Pixels, in the GPU Performance series, covers the things that can catch you out and the hidden extra work involved.

Further Reading