How to make array of sampler2D uniforms?

Please help! Is there any workaround for doing this:

uniform sampler2D myTexture[8];

it does not work: compiling error appear.
It is some kind of restriction?

As far as I am aware, yes that is a restriction as you cannot index uniforms.

(in theory however, it should work as long as it could be determined at compile time what the indices were)

You could use dynamic branchig to perform binary search (we’re searching for the proper texture2D instruction):

if (image < 4)
  if (image < 2)
    if (image == 0) color = texture2D(myTexture0, texcoord);
    else color = texture2D(myTexture1, texcoord);
  else
    if (image == 2) color = texture2D(myTexture2, texcoord);
    else color = texture2D(myTexture3, texcoord);
else
  if (image < 6)
    if (image == 4) color = texture2D(myTexture4, texcoord);
    else color = texture2D(myTexture5, texcoord);
  else
    if (image == 6) color = texture2D(myTexture6, texcoord);
    else color = texture2D(myTexture7, texcoord);

Should work fine on shader model 3.0 GPU’s but will be a bit slow on older GPU’s.

Another solution is something like this:

uniform sampler3D myTexture;

This however, limits you to use of the same set of textures every time (unless you will dynamically change slices of 3D texture by using glCopyTexSubImage2D or glTexSubImage2D). You could put all textures in one 3D texture though. There are two more limitations here: No mipmaps and equal texture sizes.

And the third solution is to use texture arrays available on GeForce 8800.

>>And the third solution is to use texture arrays available on GeForce 8800.
But one limitation will stay: textures must have equal sizes

As far as I am aware, yes that is a restriction as you cannot index uniforms.
There is no such restriction.
Like you said: “it should(will) work as long as it could be determined at compile time what the indices were”

So, I guess, author is trying to select texture, using varying from vertex shader/uniform from program

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.