Skip to content
systemcoding.dev
GPU

Fetch exact texels with texelFetch

Fetch exact texels with texelFetch, which takes integer coordinates and a mip level and skips filtering, in GLSL 1.30 and later.

1 min read
Summarize in AI

texture() takes normalised coordinates and filters. When you want the stored value at an exact pixel, such as a lookup table or a G-buffer, texelFetch takes integers and does neither.

fetch.fragglsl
#version 330 core
out vec4 frag;
uniform sampler2D u_gbuffer;

void main() {
    ivec2 p = ivec2(gl_FragCoord.xy);
    vec4 t = texelFetch(u_gbuffer, p, 0);
    frag = vec4(t.rgb, 1.0);
}
Text
pixel (12, 7) reads texel (12, 7), level 0, no interpolation

The third argument is the mip level, and the coordinates are not wrapped or clamped by the sampler state: a position outside the texture gives undefined results, per the texelFetch page. That makes it the right tool for per-pixel passes, and often cheaper because no filter is applied.

Because filtering is ignored, the sampler's wrap and filter settings have no effect. Check textureSize before indexing near an edge.

Share

More snippets

GPU

List platforms with clGetPlatformIDs

Call clGetPlatformIDs twice, once for the count and once for the handles, then print each platform name with clGetPlatformInfo in OpenCL 3.0 C.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.