PDA

View Full Version : non-constant index in samplers



Prof. Crawfis
12-28-2005, 07:05 PM
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.

sqrt[-1]
12-28-2005, 09:20 PM
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)

2) Does assembly language have the same constraints?

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

3) Any chance this is going to change?

Leave this to someone who might know.

(As for spaces, use code blocks)

Prof. Crawfis
12-31-2005, 09:21 AM
Originally posted by sqrt[-1]:
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.
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.