GLSL interpolateAtOffset Sub-Pixel Evaluation

The interpolateAtOffset function in GLSL allows fragment shaders to explicitly sample varying attributes at custom, sub-pixel locations rather than standard pixel-center or MSAA sample positions. This article explains how the OpenGL Shading Language evaluates fragment attributes at specified screen-space offsets, how hardware uses barycentric coordinates and partial derivatives to compute these values, the coordinate systems involved, and the precision constraints to consider when implementing manual multisampling or anti-aliasing techniques.

Overview of interpolateAtOffset

In standard rasterization, the GPU evaluates fragment inputs (interpolants) at the center of the pixel grid coordinate \((x + 0.5, y + 0.5)\), or at specific sample locations when using multisample anti-aliasing (MSAA) with the sample qualifier. The built-in GLSL function:

genType interpolateAtOffset(genType interpolant, vec2 offset);

bypasses default evaluation points. It computes the value of interpolant at an arbitrary screen-space position defined by offset relative to the current pixel center.

Screen-Space Coordinate System and Offsets

The offset argument is a two-component floating-point vector (vec2) expressed in fractional pixel units:

  • vec2(0.0, 0.0) represents the exact center of the current pixel.
  • Values range from -0.5 to 0.5 along both the X and Y axes, mapping the interior boundaries of the pixel.
  • Positive X values shift the sampling position to the right, while positive Y values shift the sampling position vertically (direction depends on the active window coordinate convention, typically upward in standard OpenGL).

While hardware often accepts values outside the [-0.5, 0.5] range, doing so causes extrapolation beyond the current pixel's footprint, which can yield unexpected results near geometric edges or primitive boundaries.

Mathematical Evaluation and Hardware Implementation

When modern GPUs rasterize a primitive (such as a triangle), they set up plane equations for vertex attributes in screen space to perform perspective-correct interpolation.

  1. Barycentric Interpolation: Vertices define weights \((\lambda_0, \lambda_1, \lambda_2)\) that sum to 1 at any point on the triangle. Because of perspective projection, the GPU interpolates attribute values divided by the clip-space \(W\) coordinate (\(A/W\)) along with the reciprocal of \(W\) (\(1/W\)).
  2. Offset Application: When interpolateAtOffset is called with an offset vector \(\mathbf{\delta} = (\delta_x, \delta_y)\), the hardware shifts the evaluation coordinates: \[x' = x_{\text{center}} + \delta_x\] \[y' = y_{\text{center}} + \delta_y\]
  3. Plane Equation Re-evaluation: The GPU computes the perspective-correct attribute at the new screen-space coordinate \((x', y')\) using the pre-calculated screen-space derivatives: \[\frac{A}{W}(x', y') = \frac{A}{W}_{\text{center}} + \frac{\partial (A/W)}{\partial x} \delta_x + \frac{\partial (A/W)}{\partial y} \delta_y\] \[\frac{1}{W}(x', y') = \frac{1}{W}_{\text{center}} + \frac{\partial (1/W)}{\partial x} \delta_x + \frac{\partial (1/W)}{\partial y} \delta_y\]
  4. Final Reconstruction: The attribute value is recovered via division: \[A(x', y') = \frac{\frac{A}{W}(x', y')}{\frac{1}{W}(x', y')}\]

For attributes declared with the noperspective qualifier, the GPU skips division by \(W\) and evaluates the linear screen-space gradients directly.

Sub-Pixel Precision and Quantization

Hardware does not evaluate offsets using arbitrary continuous floating-point numbers. Instead, the offset values are quantized to a fixed-point sub-pixel grid determined by implementation-dependent hardware limits (typically defined by GL_SUBPIXEL_BITS, commonly 4 to 8 bits).

For a 4-bit sub-pixel precision grid, the hardware rounds the floating-point offset to the nearest multiple of \(1/16\) (\(0.0625\)). Consequently, offsets differing by less than the hardware step size evaluate to identical interpolated values.

Common Use Cases

  • Custom MSAA Resolves: Evaluating attributes at exact sample locations using programmable sample positions without requiring per-sample shading invocations.
  • Analytical Anti-Aliasing: Sampling geometry derivatives or textures at sub-pixel steps to estimate visibility gradients and smooth high-frequency geometric edges.
  • Screen-Space Decals and Reconstruction: Sampling surface parameters at offset positions to compute fine-grained screen-space surface gradients without relying exclusively on coarse \(2 \times 2\) pixel quad derivatives.