texelFetch() + modulo or texture() + GL_REPEAT


I need to use the texelFetch(…) function in my shader to get a single pixel of a sampler2D of size S. Unfortunately, when I compute the texture coordinate T, the result is not normalized between 0 and S-1 and I have to do a modulo to normalize it (T %= S). Then, I can use the texelFetch function.

The modulo operation is quite slow on GPU, especially because I do an intensive use of it ! So, a better idea was to set the GL_REPEAT flag to perform texture lookup with unnormalized texture coordinate. AFAIK, the texelFetch function does not support unnormalized texture coordinates.

So, I tried to use floating-point texture coordinates with classical “texture(…)” function to do a texture lookup. The conversion from integer coordinate to floating-point coordinate is done by :

fcoord = vec2(icoord+0.5) / S

where S is the size of the texture and icoord, the unormalized integer texture coordinate. The texture lookup can be done then using the floating-point coordinate with the “texture(…)” function.

The result is not identical between texelFetch(…) (i.e. integer texture lookup) and texture(…) (i.e. floating-point texture access).

Do you think the mapping from integer to floating-point texture coordinates is wrong ? Or, is there a way to use the GL_REPEAT from texelFetch (meaning without modulo).

Thanks a lot,

There can be rounding errors in case of the normalized texture coordinates (due to the normalization process) but those should not produce any visual artifacts if you use NEAREST filtering.