PDA

View Full Version : GetUniformLocationARB failure



nurbz
07-27-2005, 12:59 PM
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

jra101
07-27-2005, 01:46 PM
Sounds like your shader doesn't actually use some of the declared uniforms, so glGetUniformLocationARB will correctly return -1.

nurbz
07-28-2005, 12:40 AM
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

yooyo
07-28-2005, 02:57 AM
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

jra101
07-28-2005, 08:27 AM
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.