# GLSL: Why can only some of the matrices be found? [closed]

I've been learning GLSL and to start I've been trying to replicate the fixed-function pipeline functionality with it, for 2D graphics. I've done it successfully in most things, so far, but I came into a weird problem. First of all, here are my shaders:

static const char gVertexSource[] =
"#version 120\n"
"in vec4 vertex;\n"
"in vec4 color;\n"
"in vec2 texCoord;\n"
"uniform mat4 projection = mat4(1);\n"
"uniform mat4 model = mat4(1);\n"
"uniform mat4 view = mat4(1);\n"
"varying vec4 outColor;\n"
"varying vec2 texUV;\n"
"void main() {\n"
"  gl_Position = projection * view * model * vertex;\n"
"  outColor = color;\n"
"  texUV = texCoord;\n"
"}\n";

static const char gFragmentSource[] =
"#version 120\n"
"uniform int textured = 1;\n"
"uniform sampler2D texture;\n"
"varying vec4 outColor;\n"
"varying vec2 texUV;\n"
"void main() {\n"
"   float ftextured = textured;\n"
"   gl_FragColor = (texture2D(texture, texUV) + vec4(abs(1 - ftextured)) ) * outColor;\n"
"}\n";


It compiles fine and works fine, but something is wrong. This shader allows me to pass geometry with texcoords and colors and texture the final fragment while combining it with the color. In the projection matrix I set an orthographic camera which works perfectly. As you can see they are all default initialized to the identity, so even if i dont set the other matrices nothing happens.

So far so good. After this, i wanted to set the model mat4 with the actual transform of an object so it scales and re-positions within the screen. For my surprise, glGetUniformLocation() always returns -1 for the model uniform, even though it works fine for projection. So I tried to set the view matrix instead of the model matrix, which would end up in the same final result, and again, for my surprise, it worked.

I figured it could be something related to the order of declaration / order of multiplication or even the name of the uniform, so i tried many combinations always with the same result. Also tought I could be running out of uniform space (unlikely with only the basic matrices and my high end system), so I tried adding another mat4 uniform and that one could also be found correctly..

I really dont get what I could be doing wrong and I tried most things to debug this problem.. Any ideas on what is wrong with this shader?

• Have you tried changing the name of the variable? Also, using something like gDEBugger could help you track this down. – MichaelHouse Apr 11 '13 at 5:23
• The model uniform of this shader works fine for me. Can you also post the code for creating the shader and querying the uniform location? – msell Apr 11 '13 at 5:37

Uniform variables that do not contribute to the final result in the shader are optimized away by the compiler. So when you declare uniform mat4 and it does not contribute to the final result of the program like gl_FragColor (even if its used in the calculations ), the shader compiler will remove it from the program and always return -1 when you query its location.

The other reasons could be

• you are not binding the correct shader program (using glUseProgram before using glGetUniformLocation).
• if you misspell the variable name to glGetUniformLocation.

You can also debug it:

• Query all your uniform variable names using glGetActiveUniform.
• check if openGL generate any errors, I recommend gDebugger.
• I don't see how this could be the case, after all the model matrix contributes to gl_Position, unless I missed something. – maul Apr 11 '13 at 8:33
• I queried all uniforms and all of them exist. "model" is returned from glGetActiveUniform, but i get a -1 in glGetUniformLocation() right after. – Grimshaw Apr 11 '13 at 12:23

I found the problem, it was a very silly thing as expected, sorry to waste your time.

Turns out the model uniform was at location 0, and i was checking the location's validity with (location > 0) instead of (location != -1).

Thanks everyone.