Uniform block index in different shaders

泄露秘密 提交于 2019-12-11 10:30:10

问题


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?


回答1:


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;
};


来源:https://stackoverflow.com/questions/33693916/uniform-block-index-in-different-shaders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!