Intrinsics2 min read

ddx_coarse

Computes a coarse partial derivative in screen-space x.

Reading Time
2 min
Word Count
185
Sections
11
Try It Live

Test ddx_coarse in a live shader

Open the playground, start from a visual preset, and wire ddx_coarse into the fragment stage to see how it behaves with real values.

Open Playground

Live Demo

The ddx_coarse function computes a lower-precision partial derivative in the x direction, potentially shared across larger pixel groups for better performance.

Signature

bwsl
ddx_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 horizontal direction.

Example

bwsl
pipeline FastMipSelect {
@fragment_only
fragment {
// Use coarse derivatives for mip level selection (quality not critical)
float2 dUVdx = ddx_coarse(input.uv * textureSize);
float2 dUVdy = ddy_coarse(input.uv * textureSize);
float maxDeriv = max(dot(dUVdx, dUVdx), dot(dUVdy, dUVdy));
float mipLevel = 0.5 * log2(maxDeriv);
float4 color = sample_lod(albedoTex, input.uv, mipLevel);
output.color = color;
}
}

Common Use Cases

LOD Calculation

bwsl
// Mip level doesn't need per-pixel precision
float2 dx = ddx_coarse(uv) * texSize;
float2 dy = ddy_coarse(uv) * texSize;
float lod = 0.5 * log2(max(dot(dx, dx), dot(dy, dy)));

Fog/Distance Effects

bwsl
// Coarse derivatives are sufficient for large-scale effects
float depthGrad = ddx_coarse(linearDepth);
float fogVariation = depthGrad * 0.1;

Low-Frequency Effects

bwsl
// Effects that don't need pixel-level precision
float3 posGrad = ddx_coarse(worldPos);
float terrainSlope = length(posGrad);

Performance-Critical Paths

bwsl
// When you have many derivative calculations
float d1 = ddx_coarse(value1);
float d2 = ddx_coarse(value2);
float d3 = ddx_coarse(value3);
// Combined result used for coarse effect

When to Use

Use coarse derivatives when:

  • Calculating LOD/mip levels
  • Large-scale effects (fog, distance fade)
  • Performance is critical and precision isn't
  • Computing many derivatives in the same shader

Coarse vs Fine

On many GPUs, coarse derivatives can be faster because they're computed once per 2x2 quad rather than approximated per-pixel. The difference may be minimal on modern hardware.

Compiled Output

When compiled to GLSL:

Uses the target backend's native coarse x-derivative operation when derivative control is available.

When compiled to HLSL:

hlsl
ddx_coarse(x)

When compiled to SPIR-V:

Uses OpDPdxCoarse instruction.

See Also