dFdx
Calculates the rate of change or gradient of a value in the screen-space X-direction. It answers, 'How much does this value change when moving one pixel to the right?', allowing a pixel's calculation to 'sense' its neighbors.
Core Advantages
Exposes the GPU's native, high-performance gradient calculation capabilities to the developer, solving a task that is nearly impossible to achieve in conventional shader programming. It is the cornerstone of high-quality procedural anti-aliasing and many advanced effects.
Common Uses
Calculating the gradient of procedural content to achieve smooth, anti-aliased edges.
Analyzing the rate of change of UV coordinates to dynamically adjust normal map strength or implement custom texture filtering, preventing distant aliasing.
Detecting the gradient of barycentric coordinates to dynamically draw wireframes or outlines on a model.
How to adjust
The output is proportional to the 'frequency' of the input. For example, applying dFdx to a high-frequency noise pattern (where the input value changes rapidly) yields a larger result than on a smooth pattern. This allows anti-aliasing strength to automatically adapt to content complexity. As the calculation is in screen space, the object's orientation significantly impacts the result; for a plane receding into the distance, the dFdx of its world position will increase with distance.
Code Examples
1// p can be UVs, world position, or any other value computed in the fragment shader
2const gradientX = dFdx( p );