不同着色器中的均匀块索引 [英] Uniform block index in different shaders
问题描述
查看 uniform_buffer_object 规范,不能保证一定在多个着色器程序中以相同方式定义的统一块将具有glGetUniformBlockIndex()
返回的相同索引.这意味着每次我切换着色器程序时,都必须调用glBindBufferBase()
为UBO分配相关的索引.
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)
当查询这样的索引时:
std::cout << glGetUniformBlockIndex(prog1, "var1") << "\n";
std::cout << glGetUniformBlockIndex(prog2, "var1") << "\n";
我得到相同的索引值.查询"var2"
,"var3"
等时,我得到相同的结果.
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?
推荐答案
获得索引后,应设置绑定点:
After you get the index you should set the binding point:
glUniformBlockBinding(prog1, glGetUniformBlockIndex(prog1, "var1"), 1);
glUniformBlockBinding(prog2, glGetUniformBlockIndex(prog2, "var1"), 1);
现在1是每个程序中var1
的绑定点.
Now 1 is the binding point of var1
in each program.
您还可以在glsl中显式设置绑定:
You can also set the binding in glsl explicitly:
layout(binding = 3) uniform MatrixBlock
{
mat4 projection;
mat4 modelview;
};
这篇关于不同着色器中的均匀块索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!