
Can't get uniform locations using GL.GetUniformLocation
Posted Thursday, 30 June, 2011 - 00:03 by slicedpan inHi, I am using the following code to load a vertex and fragment shader from files and then link them into a shader program.
The compiling/linking doesn't raise any errors, however I can't get the location of any of the uniforms defined in the shaders.
GL.CompileShader(vertexHandle); GL.CompileShader(fragHandle); _glHandle = GL.CreateProgram(); GL.AttachShader(_glHandle, vertexHandle); GL.AttachShader(_glHandle, fragHandle); GL.LinkProgram(_glHandle); Console.WriteLine(GL.GetProgramInfoLog(_glHandle)); //this does not generate any error message GL.UseProgram(_glHandle); int count; GL.GetProgram(_glHandle, ProgramParameter.ActiveUniforms, out count); Console.WriteLine(count.ToString() + " active uniforms"); int uniformLocation = GL.GetUniformLocation(_glHandle, "blah"); if (uniformLocation < 0) throw new Exception("couldn't get uniform location!"); //this exception gets thrown every time
The shader in question looks like this:
#version 330 layout(location = 0) in vec3 Position; layout(location = 1) in vec3 Normal; layout(location = 2) in vec2 TexCoord; uniform float blah; out vec3 out_Color; void main() { gl_Position = vec4(Position, 1); }


Comments
Re: Can't get uniform locations using GL.GetUniformLocation
Depending on the shader compiler the "blah" uniform may be optimized away so the location is not accessible.
Re: Can't get uniform locations using GL.GetUniformLocation
Unused uniforms will not get location assigned to them.
You can use following code to see what uniforms you have:
Re: Can't get uniform locations using GL.GetUniformLocation
Thanks guys!
It actually was being optimized away since I didn't actually use the uniform in the shaders.
Thanks for the code tksuoran, some of that might end up in my project!