GetUniformLocationARB failure

Hi guys,

I am writing some shader programs in GLSL on my nVidia 6600 using the latest nVidia drivers and the headers provided in the latest nVidia SDK (IDE = MS VS 7.0, aka 2002).

I encountered a very annoying problem in the pixel shader: I cannot obtain the location of some of the uniform variables defined in this shader (GetUniformLocationARB return -1), although I double-checked the names everywhere, and they do match. On the other hand, some other very similar variables work just fine?! It seems to affect mostly the variables declared as “uniform float”, although I am not so sure about that.

I am thinking that maybe there is a limit to the amount of global variables that can be instanciated in a pixel shader, and my problem comes from the fact that I reached this limit, as I have a fair amount of variables, arrays and structures declared. Is such a problem possible?

Otherwise, would some seasoned GLSL programmers be kind enough to indicate me some ideas explaining typical reasons for such a failure?

Many thanks

Alexis

Sounds like your shader doesn’t actually use some of the declared uniforms, so glGetUniformLocationARB will correctly return -1.

Thanks for the great answer. It indeed seems to due to the fact that the compiler thinks that the variables are not used. For instance, if I just change the last line of the pixel shader, from gl_FragColor = “(spec + diff + ambient) * textureColor” to “gl_FragColor = foo”, all the uniform variables used to perform the lighting computation (and obtain spec, diffuse and ambient) cannot be accessed (glUniformLocationARB return -1), although they are still used within the shader to perform the computation of spec, diffuse and ambient. So they are used within the shader, but they are useless as they dont help to determine gl_FragColor.

Could someone please explain me how the “compiler” decides which uniform variables is used or not? Or indicate me an article explaining this?

Many thanks

Alexis

Since shaders output is void (void main (void){…}), only outputs are varying variables (in case of vertex shader) and gl_FragXXXX (in case of fragment shader).

Compiler is smart enought to analize your shader code and if you have some expensive calculation which is not used in final result it will be discarded from compiled code. Additinaly, if some uniform variable are used in useless computation it will be discarded too.

Without such optimization, shaders will run much slower.

yooyo

Originally posted by nurbz:
Could someone please explain me how the “compiler” decides which uniform variables is used or not? Or indicate me an article explaining this?

In the case of a fragment shader that only writes to gl_FragColor, the compiler will not generate any instructions for code in the shader that does not contribute to the final value written to this output.

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