Understanding texelFetchOffset in GLSL

In OpenGL Shading Language (GLSL), texelFetchOffset is a built-in function designed to read raw, unfiltered texel data from a specific integer coordinate while applying a fixed, compile-time constant offset. Unlike standard texture sampling functions, it bypasses normalized texture coordinates, wrapping modes, and filtering methods, making it essential for tasks that require direct pixel-level access with minimal computational overhead.

Direct Texel Access Without Filtering

Standard sampling functions like texture() map floating-point coordinates from [0.0, 1.0] across a texture surface and perform interpolation (such as bilinear or trilinear filtering). In contrast, texelFetchOffset operates in integer space:

gvec4 texelFetchOffset(gsampler sampler, ivec P, int lod, ivec offset);
  • sampler: The texture sampler (e.g., sampler2D, sampler2DArray).
  • P: The non-normalized integer coordinates corresponding directly to pixel indices (e.g., ivec2(x, y)).
  • lod: The specific Level of Detail (mipmap level) to read from.
  • offset: A constant integer vector added to P before sampling.

Because it retrieves the exact texel stored at the computed coordinate, it does not blend adjacent pixels or account for texture wrapping; any coordinates outside texture bounds yield undefined results.

The Advantage of a Fixed Offset

The primary purpose of having an explicit offset parameter—rather than manually adding values via texelFetch(sampler, P + offset, lod)—is hardware optimization.

  1. Hardware-Level Addressing: Modern graphics processing units (GPUs) feature dedicated texture-mapping units (TMUs) that support base-plus-offset address generation in a single instruction. Using texelFetchOffset maps directly to these hardware instructions, eliminating the need for an extra Arithmetic Logic Unit (ALU) operation to calculate the new coordinate.
  2. Compile-Time Constraint: The offset argument must be an integer constant expression known at compile time. This guarantees that the GPU compiler can bake the offset directly into the sampling instruction rather than dynamically calculating memory addresses at runtime.

Hardware Limitations

Because the offset is embedded into the instruction encoding, its range is strictly constrained by hardware limits defined in GLSL:

  • gl_MinProgramTexelOffset (typically -8)
  • gl_MaxProgramTexelOffset (typically 7)

If an offset outside this implementation-dependent range is required, or if the offset depends on dynamic runtime variables, standard coordinate addition with texelFetch must be used instead.

Common Use Cases

  • Convolution Kernels: Ideal for image processing passes such as Gaussian blurs, sharpening, edge detection (e.g., Sobel filters), where neighboring texels at relative positions (such as -1, 0, and +1) are collected and weighted.
  • Deferred Shading and G-Buffers: Accessing precise geometric or material properties stored in non-interpolated full-screen render targets.
  • Post-Processing: Algorithms like Screen Space Ambient Occlusion (SSAO) or depth-based outline generation that require immediate structural analysis of neighboring pixels.