Search code examples
c++openglglslshaderglm-math

glsl vertex shader uniform variable not getting activated


I have been trying to pass a mat4 into VS. I am activating the shader program before passing the data:

renderProg.use();  // glUseProgram( m_programHandle );
glm::mat4 mv = view * model;
renderProg.setUniform("u_ModelViewMatrix", mv);
renderProg.setUniform("u_MVP", projection * mv);

But for some reason the uniform variable(u_ModelViewMatrix) is not being activated and returning -1 in the "Location" of glGetUniformLocation. Here is the VS code:

#version 460

layout (location = 0 ) in vec3 VertexPosition;    
uniform mat4 u_MVP;
uniform mat4 u_ModelViewMatrix;

out vec3 Position;    
void main()
{
    vec4 vp = vec4(VertexPosition, 1.0);
    Position = (u_ModelViewMatrix * vp).xyz;
    gl_Position = u_MVP * vp;
}

FS Code:

#version 460

in vec3 Position;

uniform vec4 Color;

layout( location = 0 ) out vec4 FragColor;

void main() {
  FragColor = Color;
}

Only one uniform is not being able to set "u_ModelViewMatrix", the other is being successfully accessed "u_MVP". Both uniforms use the same call for receiving data:

glUniformMatrix4fv(loc, 1, GL_FALSE, &m[0][0]);

Right after successful linking and compiling of the Vertex and the Fragment shader , if I am querying the active uniforms , its returning two location of uniforms , one is from FS "Color" the other is from VS "u_MVP"

for( int i = 0; i < nUniforms; ++i ) {
        glGetActiveUniform( m_programHandle, i, maxLen, &written, &size, &type, name );
        location = glGetUniformLocation(m_programHandle, name);
        qDebug() << "Location: " << location << " Name: " << name;
    }

Output: Location: 0 Name: Color Location: 1 Name: u_MVP

What am I doing wrong here?


Solution

  • The interface variabel Position is not used in the fragment shader. So the uniform variable u_ModelViewMatrix is not required. This uniform is "optimized out" by the linker and does not become an active program resource. Therefor you'll not get a uniform location for "u_ModelViewMatrix".