Non-constant index in samplers

I have a fragment shader where I select one of several texture units to sample. Unfortunately, using a non-constant to index the sampler array results in a compile time error (nVidia, but also I believe on ATI). Three questions:

  1. Is this in the official spec, or just a safety issue with current compilers?
  2. Does assembly language have the same constraints?
  3. Any chance this is going to change?

Currently, I have a massive if-else statement to test for which texture unit. What I would like is something like this (which 3DLabs GLSL Validator indicates is fine):
uniform sampler2D myTextures[4];
uniform float myValue;
void main( void ){
float fUnit = log2(myValue);
int unit = int( min(max(myValue,3),0) );
gl_FragColor = tex2D( myTextures[unit], gl_TexCoord[0].st );
}

Roger
p.s. How do you get extra white space in these posts? I lost the indenting above.

  1. Is this in the official spec, or just a safety issue with current compilers?

I can’t see it in the spec. The hardware probably just can’t support it. FYI: last time I even tried to use uniform sampler arrays on ATI (about a month ago) it crashed the driver. (even with a constant offset)

  1. Does assembly language have the same constraints?

Reasonably sure that no ASM version supports runtime determination of texture lookups.

  1. Any chance this is going to change?

Leave this to someone who might know.

(As for spaces, use code blocks)

Originally posted by sqrt[-1]:
[b]1) Is this in the official spec, or just a safety issue with current compilers?

I can’t see it in the spec. The hardware probably just can’t support it.
[/b]
I was surprised by this at first, indexing an array is pretty common. However the generated assembly language for nVidia looks like:

  TEX   R1, fragment.texcoord[2], texture[2], 2D;

which isn’t very assembly-language looking, so something very special with samplers is going on.

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