Introduction

Part 1 showed that ddx and ddy tell you how much a value changes from one pixel to the next. This post covers the two uses you’re most likely to meet: picking mipmap levels and smooth edges with fwidth. Part 3 adds flat shading and a few more.

Picking the Right Texture Size (Mipmaps)

This is the most common use. Every time a shader reads a texture with texture() in GLSL or Texture.Sample() in HLSL, the graphics card quietly works out ddx and ddy of the texture coordinate.

As Mipmaps and Trilinear Filtering showed, a distant texture shimmers unless the graphics card reads a smaller, pre-averaged copy of it (a mip level: level 0 is full size, each level after it half the width and height). To pick the level, it needs to know how many texels one pixel covers, and that’s what ddx and ddy of the texture coordinate tell it, once they’re multiplied by the texture’s size in texels.

It takes whichever is longer: the distance through the texture for one pixel step to the right (from ddx) or for one pixel step down (from ddy). If that distance is 1 texel or less, it uses level 0. Every time the distance doubles, it goes up one level: 2 texels gives level 1, and 4 texels gives level 2, a copy a quarter of the width and height, where one pixel step covers about one texel again. In-between distances give in-between levels such as 1.5, and trilinear filtering blends the two nearest levels. (This is the standard rule from the OpenGL specification. Graphics cards may approximate it slightly, and anisotropic filtering refines it for surfaces seen at a steep angle.)

In the demo below, the left half ignores ddx and ddy and always reads level 0. The right half reads the texture normally, with trilinear filtering. Watch the far distance, then hover over or tap the floor to see the numbers. Change Show to Mip level colours to see which level is used where (worked out in the shader with the rule above): red is level 0, each colour after it the next level, and colours in between a blend of two.

The mip level a texture read picks, with and without ddx and ddy

Ignoring ddx/ddyUsing ddx/ddy (normal)

Tilt the camera down and you see more of the floor close to the camera, where each pixel covers fewer texels, so more of the picture uses level 0. Add more texture repeats and each pixel covers more texels, so higher levels are used. To take control yourself, HLSL’s SampleGrad and GLSL’s textureGrad take your own ddx and ddy values, and HLSL’s SampleLevel and GLSL’s textureLod take a mip level directly.

Smooth Edges at Any Size with fwidth

Shaders often draw shapes with maths rather than textures: circles, lines, grids, text and user interface elements. A common technique works out the distance from the pixel’s centre to the shape’s edge, negative inside and positive outside (a signed distance; see Signed Distance Fields). The test “is the distance less than 0?” gives each pixel a yes-or-no answer, so edges come out jagged.

To make them smooth, you want to blend across the edge over about one pixel. But how much does the distance change over one pixel? That depends on how far you’re zoomed in, and it’s what ddx and ddy of the distance tell you. There’s a built-in helper for this called fwidth:

// What fwidth works out: a quick estimate of how much d changes over one pixel.
fwidth(d) == abs(ddx(d)) + abs(ddy(d))

// How much of the shape's colour to use for this pixel: 1 well inside, 0 well outside,
// and a blend across about one pixel at the edge.
float alpha = saturate(0.5 - d / fwidth(d));   // saturate keeps it between 0 and 1 (HLSL; in GLSL use clamp(
, 0.0, 1.0))

Adding the two together is quick but overestimates on diagonal edges, by up to about 1.4 times. length(float2(ddx(d), ddy(d))) gives a more exact answer for a little more work. Also make sure fwidth(d) can’t be 0 where you divide by it, for example with max(fwidth(d), 0.0001), or flat areas can give invalid results.

The demo draws a disc, a thin ring and a grid of thin lines. Turn up Pixel size to magnify the pixels, change the zoom, and compare the modes. A fixed blur looks fine at one zoom level only. The fwidth version stays crisp but smooth at every zoom, and lines thinner than a pixel stay unbroken instead of breaking up into dashes. They do look too bold when zoomed far out, because with this simple formula a pixel whose centre is on the line always gets at least half of the line’s colour, however thin the line is; the grid shader article in Further Reading shows how to fix that too.

Hard edges, a fixed blur and fwidth, at any zoom

Hard edgefwidth

Under the hood

When a shader reads a texture, the texture unit (the hardware that fetches and filters texels) is handed the texture coordinates of all four pixels in the 2×2 group at once. It works out the derivatives itself from their differences and picks the mip level, often once for the whole group. That’s why even a shader that never calls ddx still has to run in 2×2 groups: every normal texture read needs the neighbours. Reading with an explicit level, such as textureLod, doesn’t.

Questions You Might Have

Why not just use fwidth for every edge?

For shapes worked out in the shader, it’s usually the right tool. Edges of triangles are a different case: their pixels come from rasterisation, not from a distance the shader knows, so they need anti-aliasing instead.

Summary

  • Every ordinary texture read uses ddx and ddy of the texture coordinate to choose a mip level: the more texels one pixel step crosses, the smaller the copy it reads.
  • fwidth(d) is abs(ddx(d)) + abs(ddy(d)): roughly how much a value changes over one pixel.
  • Dividing a signed distance by fwidth blends edges over about one pixel, so shapes stay smooth at any zoom.

Next, part 3 builds a surface’s normal from ddx and ddy of its position: flat shading with no extra data.

Further Reading