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
ddxandddyactually 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 muchvaluechanges when you move one pixel to the right.ddy(value)is how muchvaluechanges 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
ddxof 0 everywhere, because nothing changes. - A steady ramp gives the same
ddxeverywhere, and steeper ramps give bigger numbers. - A curve gives a
ddxthat 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 groupddy= 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
ddyis 0 everywhere. - Diagonal ramp. Now both
ddxandddyare 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.
ddxis 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 does | HLSL | GLSL | Metal |
|---|---|---|---|
| Change one pixel to the right | ddx | dFdx | dfdx |
| Change one pixel down* | ddy | dFdy | dfdy |
| abs(ddx) + abs(ddy) | fwidth | fwidth | fwidth |
| Fine: each row (or column) of a group gets its own answer | ddx_fine, ddy_fine | dFdxFine, dFdyFine | Not available |
| Coarse: one answer may be shared by the whole group | ddx_coarse, ddy_coarse | dFdxCoarse, dFdyCoarse | Not available |
| Read a texture with your own ddx/ddy | SampleGrad | textureGrad | sample 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.
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#
ddxandddytell 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#
- Microsoft, ddx, ddy and fwidth: the HLSL reference pages.
- Khronos, dFdx and dFdy: the GLSL reference page, including the fine and coarse versions.
- Fabian Giesen, A trip through the Graphics Pipeline, part 8: how graphics cards really run fragment shaders (Direct3Dâs pixel shaders) in 2Ă2 groups.