How interpolateAtCentroid Evaluates Inputs in GLSL

The GLSL built-in function interpolateAtCentroid allows fragment shaders to explicitly sample an interpolated input at the centroid of covered multisample anti-aliasing (MSAA) locations rather than at the default pixel center. This article explains how the function operates under multisampling, why it prevents visual artifacts along primitive edges, and the exact mechanics the GPU uses to compute these evaluated values.

The Problem with Default Pixel-Center Interpolation

In standard rasterization with multisample anti-aliasing (MSAA) enabled, fragment inputs are evaluated at the pixel center by default. However, when a triangle edge cuts across a pixel, the pixel center might not be covered by that primitive. If the GPU executes a fragment shader for the partially covered pixel, evaluating the input at an uncovered pixel center requires extrapolating values beyond the primitive boundary. This extrapolation frequently leads to graphical anomalies, such as texture coordinate bleeding, invalid normal calculations, or color banding along silhouette edges.

How interpolateAtCentroid Resolves Values

The interpolateAtCentroid function provides explicit programmatic control over the sample location without requiring the input variable to be declared globally with the centroid qualifier.

When interpolateAtCentroid(interpolant) is called:

  1. Determines Sample Coverage: The GPU checks the coverage mask for the current fragment to determine which multisample locations fall within the primitive boundaries.
  2. Locates the Centroid: The hardware computes a centroid point lying within the primitive's coverage area for that pixel. By specification, the centroid must fall within the intersection of the primitive and the pixel, bounded by the covered sample locations.
  3. Fallback to Pixel Center: If all multisample locations within the pixel are covered, or if the pixel center itself lies inside the primitive, the centroid evaluation typically defaults to the exact pixel center to maintain maximum mathematical stability.
  4. Applies Barycentric Coordinates: Once the centroid position is established, the GPU calculates perspective-correct barycentric coordinates for that specific offset and interpolates the requested attribute accordingly.

Behavior and Usage Details

The function accepts a floating-point scalar or vector input variable (float, vec2, vec3, or vec4) representing a smooth interpolated value passed from a prior shader stage (such as a vertex or geometry shader). It returns the exact same type, evaluated at the computed centroid.

Because the centroid always lies inside the primitive, interpolateAtCentroid guarantees that values remain strictly within the valid range defined by the primitive's vertices. This makes it an ideal solution for clamping texture coordinates and procedural patterns along silhouette edges while retaining standard pixel-center evaluation for other calculations within the same shader.