All guides

texture2D vs texture vs texelFetch

texture2D is the ES 1.00 name for filtered 2D lookup. ES 3.00 uses texture overloads selected by sampler type. texelFetch reads an integer texel coordinate at a chosen mip level without bilinear filtering. Images often want interpolation, while cell states and precise data lookups often must not blend neighboring values.

Fix the problem and environment

texture2D is the ES 1.00 name for filtered 2D lookup. ES 3.00 uses texture overloads selected by sampler type. texelFetch reads an integer texel coordinate at a chosen mip level without bilinear filtering. Images often want interpolation, while cell states and precise data lookups often must not blend neighboring values.

The controlling code

// GLSL ES 1.00:
vec4 a=texture2D(image,uv);
// GLSL ES 3.00, compile separately:
vec4 b=texture(image,uv);
ivec2 size=textureSize(image,0);
ivec2 pixel=clamp(ivec2(uv*vec2(size)),ivec2(0),size-1);
vec4 c=texelFetch(image,pixel,0);

Verify in order

Quantize UV before filtered lookup in the pixelation case, then inspect integer addressing in the texelFetch reference. Move a sample from a texel center toward a boundary to compare interpolation.

Scope and common mistakes

The lines belong to two language versions, not an automatic compatibility layer. Out-of-range texelFetch coordinates are not made safe by ordinary texture wrap settings.

Continue experimenting

Reference

Official documentation

Run the related examples