Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Uniform confusion

  1. #1
    Member Regular Contributor
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    311

    Uniform confusion

    I use the folowing code to query all active uniforms in a program:

    Code :
    int activeUniforms;
    char uniformName[256];
     
    // get number of active uniforms
    glGetProgramiv( program->id, GL_ACTIVE_UNIFORMS, &activeUniforms );
     
    // save names, sizes and locations of all uniforms
    for( uint index = 0; index < activeUniforms; index++ )
    {
    	int type, location;
     
    	// get uniform's name and data type at given index
    	glGetActiveUniformName(	program->id, index, 256, 0, uniformName );
    	glGetActiveUniformsiv( program->id, 1, &amp;index, GL_UNIFORM_TYPE, &amp;type );
     
    	// get uniform's location
    	location = glGetUniformLocation( program->id, uniformName );
     
    	// store type size and location of uniform
    	program->uniforms[uniformName].first = datatypeSizes[type];
    	program->uniforms[uniformName].second = location;
    }
    This enumerates all active uniforms including those which a part of a uniform block. For those, however, a location of -1 is returned. Why? If a uniform is active, it has a location, even if it's part of a uniform block, doesn't it?

    For example, for this program:

    Code :
    vs:
     
    #version 150 core
     
    layout(location=0) in vec4 in_vertexPos;
     
    layout(std140) uniform Projection
    {
    	mat4 perspective;
    	mat4 orthographic;
    } projection;
     
    void main()
    {
    	gl_Position = projection.orthographic*vec4(in_vertexPos.xyz, 1.0);
    }
     
    fs:
     
    #version 150 core
     
    out vec4 out_Color;
     
    uniform vec4 color;
    uniform vec4 color2;
     
    void main()
    {
    	out_Color = color*color2;
    }
    these uniforms are listed:

    Code :
    Active uniforms
    ---------------
    Uniform: 'projection.orthographic' (size: 64, location: -1)
    Uniform: 'color' (size: 16, location: 0)
    Uniform: 'color2' (size: 16, location: 1)
    Uniform: 'projection.perspective' (size: 64, location: -1)

    Does that make sense?

    EDIT: Is glGetActiveUniformsiv even part of the core yet? Can't find it in either the 3.3 nor the 4.1 specs..?!

  2. #2
    Junior Member Regular Contributor
    Join Date
    Apr 2004
    Posts
    205

    Re: Uniform confusion

    uniforms in named blocks don't have locations, glGetUniformLocation returns -1 for them. This is so by the specification. Instead for them you can get the offset from the block start with glGetActiveUniformsiv with pname of GL_UNIFORM_OFFSET

    You better ask such questions in the "begginers" forum section,
    or even better just read the specification yourself

  3. #3
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    381

    Re: Uniform confusion

    glGetActiveUniformsiv is part of the GL_ARB_uniform_buffer_object extension + core OpenGL from version 3.1.

    If you're searching the spec for it, remember to remove the "gl" from the start, just search for "GetActiveUniformsiv"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •