McLovin
McLovin

Reputation: 3415

Uniform block index in different shaders

Looking at uniform_buffer_object specs, there is no guarantee that a certain uniform block that is defined the same way in multiple shader programs will have the same index returned by glGetUniformBlockIndex(). That means I have to call glBindBufferBase() to assign the UBO the relevant index every time I switch the shader program.

However, from some testing, it seems like a uniform block does have the same index in different shader programs, even when the uniform blocks are declared in different orders. Have a look at these two vertex shaders: one, two. (note I used the values of v1, v2.. etc to avoid inactive uniform elimination)

When querying the indices like this:

std::cout << glGetUniformBlockIndex(prog1, "var1") << "\n";
std::cout << glGetUniformBlockIndex(prog2, "var1") << "\n";

I get the same index value. I get the same results when querying "var2", "var3" and so on.

Is this intended? Why does it happen? Can I rely on this always happening?

Upvotes: 1

Views: 659

Answers (1)

ratchet freak
ratchet freak

Reputation: 48226

After you get the index you should set the binding point:

glUniformBlockBinding(prog1, glGetUniformBlockIndex(prog1, "var1"), 1);
glUniformBlockBinding(prog2, glGetUniformBlockIndex(prog2, "var1"), 1);

Now 1 is the binding point of var1 in each program.

You can also set the binding in glsl explicitly:

layout(binding = 3) uniform MatrixBlock
{
  mat4 projection;
  mat4 modelview;
};

Upvotes: 3

Related Questions