When using textures in GLSL, it is best to calculate the final texture coordinates in the vertex shader and hand them over to the fragment shader using varying
s. Example with a simple flip in the y coordinate:
// Vertex shader
attribute vec2 texture;
varying highp vec2 texCoord;
// ...
void main() {
texCoord = vec2(texture.x, 1.0-texture.y);
// ...
}
// Fragment shader
varying highp vec2 textureCoordinates;
uniform sampler2D tex;
// ...
void main() {
highp vec4 texColor = texture2D(tex, texCoord);
// ...
}
If the flip in the y coordinate, or an even simpler operation like adding vec2(0.5)
to the texture coordinate is performed in the fragment shader the texture access is much slower. Why?
As a note, e.g. blending two textures, using a weighted sum of them, is much cheaper in terms of time and also needs to be done for each pixel, so the computation of the texture coordinate itself does not seem to be that costly.