Search code examples
androidiosopengl-eswebgl2

What is an "active" uniform block in OpenGL


The OpenGL documentation is not very clear about what is an active uniform block (versus those are not active). So my understanding is it is about them being referenced/used in a shader program... or is it!

Consider the following shader program:

Vertex Shader

#version 300 es
precision highp float;

layout(std140) uniform globals
{
    mat4 world_view_proj;
};

layout(std140) uniform foo
{
    vec3 bar;
};

layout(std140) uniform dog
{
    vec3 cat;
};

layout(location = 0) in vec4 position0;
layout(location = 4) in lowp vec4 color0;
out lowp vec4 v_color;

void main ( )
{
    v_color = color0;
    gl_Position = position0 * world_view_proj;
}

Fragment Shader

#version 300 es
precision highp float;

in lowp vec4 v_color;
layout(location = 0) out lowp vec4 frag_color;

void main()
{
    frag_color = v_color;
}

Even though I don't use foo or dog uniform blocks, a query to get the number of active uniform blocks...

GLint count = 0;
glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &count);
for (auto i = 0; i < count; ++i) {
    ...
}

...always returns 3 for this shader program. I get the same result whether I run on Android, iOS or WebGL2. I haven't tested on desktop OpenGL (versus OpenGL ES 3.0) but I don't expect a different result (but I guess it could?).

  1. So what does it means for a uniform block to be active then?
  2. And is there a way to only retrieve used uniform blocks (skip unused ones)?

Thanks!


Solution

  • A uniform block that is not in use may be optimized out by the implementation. But it may not as well; that's all up to the implementation. OpenGL does not require implementations to optimize out unused uniforms or blocks; it simply allows for the possibility.

    There is no way to guaranteeably fetch which uniform blocks are in use.