Intrinsics1 min read

ddy_coarse

Computes a coarse partial derivative in screen-space y.

Reading Time
1 min
Word Count
164
Sections
11
Try It Live

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 Playground

Live 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

bwsl
ddy_coarse :: (T x) -> T {...}

Where T can be float, float2, float3, or float4.

Parameters

ParameterTypeDescription
xTThe value to differentiate

Return Value

Returns the coarse rate of change of x in the vertical direction.

Example

bwsl
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

bwsl
// 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

bwsl
// Coarse mip selection
float2 dxUV = ddx_coarse(texCoord);
float2 dyUV = ddy_coarse(texCoord);
float maxChange = max(length(dxUV), length(dyUV));

Parallax Mapping LOD

bwsl
// 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

bwsl
// 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:

hlsl
ddy_coarse(x)

When compiled to SPIR-V:

Uses OpDPdyCoarse instruction.

See Also