Sign in to follow this  

glGetUniformLocation always returns -1

This topic is 3295 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I'm working on some shadow mapping in glsl, so I need to pass in 4 matrices to my vert shader: light's view / projection, camera's inverse view, bias matrix. So in my shader, I have 4 uniform mat4. In my c++ code, I try to get the location of each of the 4 mat4, but glGetUniformLocation returns -1 for everything. The programID I pass into glGetUniformLocation is valid, also. Here is the c++ code calling glGetUniformLocation:
void RenderShawdowedScenery()
{
	g_shadowMapShader->UseShader(true);

	// Here is where we set the mode and function for shadow mapping with shadow2DProj().
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

	GLfloat bias[] = { 0.5, 0.0, 0.0, 0.0, 
					   0.0, 0.5, 0.0, 0.0,
					   0.0, 0.0, 0.5, 0.0,
					   0.5, 0.5, 0.5, 1.0 };

	GLint progID = g_shadowMapShader->ProgramId();

	GLint loc;

	loc = glGetUniformLocation(progID, "lightViewMat");
		  glUniformMatrix4fv(loc, 1, false, g_lightModelViewMat);

	loc = glGetUniformLocation(progID, "lightProjMat");
		  glUniformMatrix4fv(loc, 1, false, g_lightProjMat);

	loc = glGetUniformLocation(progID, "camInvViewMat");
		  glUniformMatrix4fv(loc, 1, false, g_camInvModelView);

        loc = glGetUniformLocation(progID, "biasMat");
		  glUniformMatrix4fv(loc, 1, false, bias);

	Enable2DTexture(0, g_shadowMapTexID);
	
	for(int i = 0; i < (int)g_shapes.size(); i++)
		g_shapes[i].Render();

	Disable2DTexture(0);
	
	g_shadowMapShader->UseShader(false);
}

And here is the vertex shader code:

uniform mat4 lightViewMat;
uniform mat4 lightProjMat;
uniform mat4 camInvViewMat;
uniform mat4 biasMat;

varying vec4 TC;
varying vec4 Vl;

// For per-pixel lighting
varying vec3 normal;
varying vec4 vertex;

void main(void)
{             
	// For Shadow Mapping
	vec4 Vv = gl_ModelViewMatrix * gl_Vertex;
	
	Vl = lightViewMat * camInvViewMat * Vv;
	TC = biasMat      * lightProjMat  * Vl;
	
	// For Lighting
	normal = normalize(gl_NormalMatrix * gl_Normal);
	vertex = gl_ModelViewMatrix * gl_Vertex;
	gl_TexCoord[0] = gl_MultiTexCoord0;
	
	gl_Position = ftransform();
}


Any ideas on why glGetUniformLocation would be returning -1?

Share this post


Link to post
Share on other sites
Quote:
Original post by diginerd
Any ideas on why glGetUniformLocation would be returning -1?

Because half of your shader doesn't do anything, and is optimized away by the compiler. The uniforms don't exist anymore after compilation (because they aren't used) and will therefore not be found by glGetUniformLocation. Unless you are accessing them by a pixel shader. But since you didn't post any, my first assumption would be this.

Share this post


Link to post
Share on other sites
Listen to Yann ^^

The gpu will optmize your code greatly, discarding unused values. Also if the uniform's value is changed before it is used (see below) the compiler will discard its uniform status.


uniform vec2 value;

void main(){
//without calling value first, the compiler will strip away its
//uniform, it wouldnt matter what you set it to as it would always
//be set to 1.0,0.0 before use
value = vec2( 1.0, 0.0 );
...
}



( NOTE: I have never used a ATI card, cant say about there compiler.. Infact I've never used anything but a 6150LE for shader programming which uses nvidia's CG compiler.. )

Share this post


Link to post
Share on other sites
First off, thanks for the quick responses.

In the vertex shader, I do use all of the uniforms. I don't use them in the pixel shader because I don't need them in there. I also don't change the uniforms inside the shader, so they shouldn't be optimized out. How should I be using them to make sure they're not optimized out?

Share this post


Link to post
Share on other sites
uniforms are not shared between shaders, so remove them from the fragment shader where they are not needed. I have never done thing so im not sure what truly happens, but I guess opengl assumes you mean the ones that are in the fragment shader and does not even look into the vertex...

For further refence if you need a value shared between the two shaders, vertex and fragment. use 'varying' or 'attribute'..

Also it might be a good idea to pick up a copy of the orange book ( OpenGL Shading Language second edition ). Its a great read ^_^

Share this post


Link to post
Share on other sites

This topic is 3295 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this