1
\$\begingroup\$

In the vertex shader, I need to make a texture fetch, where the texture coordinate itself is read from some other texture.

vec2 uv = texture(someTexture,coords).xy;
vec4 val = texture(otherTexture,uv).xyzw;

As far as I know, the second sample has undefined results, because the value of uv is outside of uniform flow control.

Is there any way to efficiently (i.e., not copy back the contents of the texture and upload it as a uniform to the shader or something) do what I want to do?

\$\endgroup\$
1
\$\begingroup\$

Indirect texture lookups are supported by all hardware that implements GLSL (and even by GL's predecessor assembly shader language).

In early shader models there was a limit to the number of indirections you could perform (e.g. at most, you could do 4 indirect lookups in a single invocation) but modern hardware no longer suffers from any such limitation. This is even less of an issue since your question mentions this is a vertex shader that you are doing the texture lookups in. Vertex texture fetches are a Shader Model 3.0 feature.

As for doing this efficiently, that is a different story. Generally dependent lookups are not cache/pre-fetch friendly, but sometimes if the access pattern (e.g. monotonic coordinate change) is correct this is less of an issue.

An interesting point to note is that any time a texture coordinate is computed (as opposed to taken directly from a per-vertex / per-fragment input) while the shader is being invoked, it is technically a dependent lookup. You do not need to fetch something from another texture in order to create this situation (though this is most often what people associate with dependent).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.