ddx_fine
Computes a fine-grained partial derivative in screen-space x.
Test ddx_fine in a live shader
Open the playground, start from a visual preset, and wire ddx_fine into the fragment stage to see how it behaves with real values.
Open PlaygroundLive Demo
The ddx_fine function computes a high-precision partial derivative in the x direction, using the minimum 2x2 quad for calculation rather than potentially larger blocks.
Signature
ddx_fine :: (T x) -> T {...}
Where T can be float, float2, float3, or float4.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | T | The value to differentiate |
Return Value
Returns the fine-grained rate of change of x in the horizontal direction.
Example
pipeline HighQualityAA {
@fragment_only
fragment {
// Use fine derivatives for better quality anti-aliasing
float sdf = signedDistanceFunction(input.uv);
// Fine derivatives give more accurate edge detection
float dx = ddx_fine(sdf);
float dy = ddy_fine(sdf);
float gradLength = length(float2(dx, dy));
// Anti-alias using gradient magnitude
float edgeWidth = gradLength * 1.5;
float alpha = 1.0 - smoothstep(-edgeWidth, edgeWidth, sdf);
output.color = float4(float3(1.0), alpha);
}
}Common Use Cases
High-Quality Text Rendering
// SDF text with fine AA
float distance = sample(sdfFont, uv).r;
float dx = ddx_fine(distance);
float dy = ddy_fine(distance);
float edgeWidth = length(float2(dx, dy)) * 0.707;
float alpha = smoothstep(0.5 - edgeWidth, 0.5 + edgeWidth, distance);Precise Edge Detection
// Accurate edge detection for effects
float edgeDx = ddx_fine(luminance);
float edgeDy = ddy_fine(luminance);
float edgeStrength = length(float2(edgeDx, edgeDy));
Normal Map Quality
// Higher quality mip level calculation
float2 dx = ddx_fine(uv);
float2 dy = ddy_fine(uv);
float mip = 0.5 * log2(max(dot(dx, dx), dot(dy, dy)));
Stippling/Hatching
// Fine derivatives for consistent stipple size
float pixelSize = length(float2(ddx_fine(uv.x), ddy_fine(uv.y)));
float stipple = fract(pattern / pixelSize);
Fine vs Coarse
ddx/ddy: Implementation-defined precision (may be coarse for performance)ddx_fine/ddy_fine: Maximum precision, computed per fragmentddx_coarse/ddy_coarse: Minimum precision, shared across quad
When to Use
Use fine derivatives when you need maximum quality for:
- Text/glyph rendering
- Signed distance field edges
- Anti-aliasing critical details
Use regular ddx/ddy for general use; the GPU may already use fine precision.
Compiled Output
When compiled to GLSL:
Uses the target backend's native fine x-derivative operation when derivative control is available.
When compiled to HLSL:
ddx_fine(x)
When compiled to SPIR-V:
Uses OpDPdxFine instruction.
See Also
- ddx - Standard derivative in x
- ddx_coarse - Coarse derivative in x
- ddy_fine - Fine derivative in y