ddy_coarse
Computes a coarse partial derivative in screen-space y.
Test ddy_coarse in a live shader
Open the playground, start from a visual preset, and wire ddy_coarse into the fragment stage to see how it behaves with real values.
Open PlaygroundLive Demo
The ddy_coarse function computes a lower-precision partial derivative in the y direction, potentially shared across larger pixel groups for better performance.
Signature
ddy_coarse :: (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 coarse rate of change of x in the vertical direction.
Example
pipeline TerrainLOD {
@fragment_only
fragment {
// Calculate terrain detail level using coarse derivatives
float3 dPosDx = ddx_coarse(input.worldPos);
float3 dPosDy = ddy_coarse(input.worldPos);
// Larger derivatives = more distant = less detail needed
float screenSize = max(length(dPosDx), length(dPosDy));
float detailLevel = saturate(1.0 - screenSize * 0.1);
// Blend between detail and base textures
float4 detail = sample(detailTex, input.uv * 10.0);
float4 base = sample(baseTex, input.uv);
float4 color = lerp(base, detail, detailLevel);
output.color = color;
}
}Common Use Cases
Screen-Space Size
// Estimate how large something appears on screen
float3 dPdx = ddx_coarse(worldPos);
float3 dPdy = ddy_coarse(worldPos);
float screenCoverage = length(cross(dPdx, dPdy));
Texture Filtering
// Coarse mip selection
float2 dxUV = ddx_coarse(texCoord);
float2 dyUV = ddy_coarse(texCoord);
float maxChange = max(length(dxUV), length(dyUV));Parallax Mapping LOD
// Reduce parallax iterations when derivatives are large
float2 dUV = float2(ddx_coarse(uv.x), ddy_coarse(uv.y));
int iterations = int(lerp(16.0, 4.0, saturate(length(dUV) * 10.0)));
Distance-Based Effects
// Scale effects based on screen-space size
float derivMag = length(float2(ddx_coarse(depth), ddy_coarse(depth)));
float effectIntensity = 1.0 / (1.0 + derivMag * 100.0);
Matching Precision
When using coarse derivatives, use ddx_coarse and ddy_coarse together to maintain consistent precision across both axes.
Default Behavior
On some hardware, standard ddy may already use coarse precision. Using ddy_coarse explicitly documents your intent and guarantees the behavior.
Compiled Output
When compiled to GLSL:
Uses the target backend's native coarse y-derivative operation when derivative control is available.
When compiled to HLSL:
ddy_coarse(x)
When compiled to SPIR-V:
Uses OpDPdyCoarse instruction.
See Also
- ddy - Standard derivative in y
- ddy_fine - Fine derivative in y
- ddx_coarse - Coarse derivative in x