Reputation: 1155
I recently read (for the first time) that passing an array with texture coordinates to the fragment shader for multiple lookups of a sampler is way quicker than calculating them in the fragment shader, because opengl can then prefetch these pixels. Is that true ? If so, what are the limitations ? Do I have to tell opengl that these coordinates will be used a texCoords ?
Upvotes: 1
Views: 1615
Reputation: 2917
As with most performance questions, the answer is dependent on the OpenGL implementation. I know of at least one implementation which does have this optimization and one which doesn't.
In the implementations I know anything about, the prefetch happens only if you use a fragment shader input (i.e. a variable declared with varying
or in
) directly as an argument to a texture function. So:
in vec2 tex_coord;
main()
{
gl_FragColor = texture( diffuse_texture, tex_coord );
}
would qualify, but:
gl_FragColor = texture( diffuse_texture, 0.5 * tex_coord + 0.5 );
would not. Note that in this case the calculation could easily be done in the vertex shader. Also the compiler might be smart enough to calculate the texture coordinate early. The case most likely to hurt performance is where one set of texture coordinates is dependent on the result of another texture look-up:
vec2 tex_coord2 = texture( offset_texture, tex_coord ).rg;
gl_FragColor = texture( diffuse_texture, tex_coord2 );
Now the second texture look up can't happen until the first one completes.
You don't have to tell OpenGL that the coordinates will be used as texture coordinates.
Upvotes: 0