Introduction

ddx and ddy turn up all over shader code, in anti-aliasing, lighting and more, and every ordinary texture read uses them behind the scenes. The documentation calls them “partial derivatives” (“partial” because each looks in only one direction), which sounds daunting, but the idea is simple. This post, the first of three, covers:

  • What ddx and ddy actually give you
  • How the graphics card works them out (it’s just a subtraction)
  • The difference between fine and coarse derivatives

Parts 2 and 3 show what they’re used for, and the Helper Pixels post covers the things that can catch you out. You don’t need any maths beyond subtraction.

ddx and ddy are the names in HLSL, Direct3D’s shading language. GLSL (used by OpenGL, WebGL and often Vulkan) calls them dFdx and dFdy, and Metal calls them dfdx and dfdy. This post uses ddx and ddy throughout. Everything here applies to all three, except which way “down” is (see the cheat sheet at the end).

What They Measure

This post builds on the fragment shader (Direct3D calls it a pixel shader): the small program that works out the colour of each pixel a triangle covers. The Graphics Pipeline shows where it runs, and Fragment Shaders shows what it does. Each copy normally knows only about its own pixel.

A derivative is the maths word for how fast something changes. ddx and ddy estimate it in the simplest way possible: they take a step of one pixel and measure how much a value changed.

  • ddx(value) is how much value changes when you move one pixel to the right.
  • ddy(value) is how much value changes when you move one pixel down (one pixel up in OpenGL and WebGL; the cheat sheet explains why).

You can pass in anything your shader has worked out: a texture coordinate, a position, a brightness, a distance.

The Idea: Compare with the Pixel Next Door

Here is a row of 16 pixels. Each has worked out a value between 0 (black) and 1 (white), shown as its colour and as a grey bar. The bars underneath show ddx, orange when the value goes up and blue when it goes down. This demo does its sums in JavaScript, the same way a graphics card does.

Try each pattern, move the Steepness slider, and hover over or tap a pixel to see the sum. Notice that:

  • A flat pattern gives a ddx of 0 everywhere, because nothing changes.
  • A steady ramp gives the same ddx everywhere, and steeper ramps give bigger numbers.
  • A curve gives a ddx that grows as the curve gets steeper.
  • A wave gives positive numbers where it rises and negative numbers where it falls.
  • A sudden edge gives 0 everywhere except at the one pair of pixels that sits across the edge.

ddx along a row of 16 pixels, worked out in pairs

The brackets under the pixels mark fixed pairs (pixels 0 and 1, 2 and 3, and so on). The graphics card subtracts the left pixel’s value from the right one’s and gives that one answer to both pixels, which is why the ddx bars come in matching twos. The next section explains why.

How the Graphics Card Works Them Out

How can a fragment shader know what’s happening next door? The graphics card never runs one on its own: it always runs them in 2×2 groups of four neighbouring pixels (usually called quads), lined up on a fixed grid across the image, and the four step through the shader together, one instruction at a time. When the shader reaches ddx(value), each pixel can read the values its three neighbours in the group have just worked out, and subtract:

  • ddx = the right pixel’s value − the left pixel’s value, in the same row of the group
  • ddy = the bottom pixel’s value − the top pixel’s value, in the same column of the group

The demo below runs on your own graphics card and shows a tiny image, 16 pixels wide and 10 tall, drawn large, with thick lines marking the 2×2 groups. Hover over or tap any pixel (or select the grid and use the arrow keys) to see its group and the two subtractions. Switch Show to ddx or ddy to colour every pixel by its answer: orange for positive, blue for negative and grey for zero.

ddx and ddy worked out by your graphics card, in 2×2 groups

Some things worth trying:

  • Ramp across with Show: ddy. The value doesn’t change going down, so ddy is 0 everywhere.
  • Diagonal ramp. Now both ddx and ddy are the same non-zero number.
  • Circle with Show: ddx. Left of the centre the distance shrinks as you move right (blue); right of the centre it grows (orange).
  • Sudden edge with Show: ddx. ddx is only non-zero for a pair with one pixel inside the disc and one outside. A pair entirely inside or outside sees no change at all, even right next to the edge: a side effect of working in fixed 2×2 groups.

So far, each row of a group works out its own ddx, and each column its own ddy: a fine derivative. Graphics cards may instead save effort by doing the subtraction for only one row (or column) and giving that answer to all four pixels: a coarse derivative. The demo tells you which your graphics card did. HLSL’s ddx_fine and ddx_coarse, and GLSL’s dFdxFine and dFdxCoarse, ask for one or the other. Plain ddx and dFdx promise neither, and even the coarse versions only allow the shortcut.

Cheat Sheet

What it doesHLSLGLSLMetal
Change one pixel to the rightddxdFdxdfdx
Change one pixel down*ddydFdydfdy
abs(ddx) + abs(ddy)fwidthfwidthfwidth
Fine: each row (or column) of a group gets its own answerddx_fine, ddy_finedFdxFine, dFdyFineNot available
Coarse: one answer may be shared by the whole groupddx_coarse, ddy_coarsedFdxCoarse, dFdyCoarseNot available
Read a texture with your own ddx/ddySampleGradtextureGradsample with gradient2d

* OpenGL and WebGL count pixel rows from the bottom by default, so there dFdy measures the change one pixel up. Direct3D, Metal and Vulkan count from the top. The fine and coarse GLSL versions need OpenGL 4.5 (or the ARB_derivative_control extension) or Vulkan; WebGL doesn’t have them.

Under the hood

Fragment shaders run in groups of 32 or 64 copies that execute each instruction together (How GPUs Run Shaders), with a quad’s four pixels side by side in the same group. Each copy keeps its values in registers, the processor’s fastest storage, and special instructions let a copy read a register from another copy in its quad. So a derivative needs no trip to memory: read the neighbour’s register and subtract. Newer shader languages let programmers use the same mechanism directly, for example HLSL’s QuadReadAcrossX (Shader Model 6.0) and Vulkan GLSL’s subgroupQuadSwapHorizontal.

Questions You Might Have

Can I use ddx and ddy in a vertex shader?

No. Only fragment shaders run in 2×2 groups of neighbouring pixels, so only they have neighbours to compare with. (Some newer APIs also allow derivatives in compute shaders, by arranging threads into the same 2×2 groups.)

Are ddx and ddy expensive?

The subtraction itself is about as cheap as an instruction gets. The real cost is that the graphics card must always shade whole 2×2 groups, even where a triangle covers only one pixel of a group. Helper Pixels measures that cost.

Summary

  • ddx and ddy tell you how much a value changes from one pixel to the next, to the right and down (up in OpenGL and WebGL).
  • The graphics card works them out by running fragment shaders in 2×2 groups and subtracting neighbours’ values, so each answer is shared by a pair of pixels.
  • Fine derivatives give each row or column of a group its own answer; coarse ones may share one answer across the group.

Next, part 2 puts them to work: picking mipmap levels, and smooth edges at any size with fwidth.

Further Reading