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.
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.
#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);
}pixel (12, 7) reads texel (12, 7), level 0, no interpolationThe 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.