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?