UBO 固定位置不工作

UBO fixed location not working

我的片段着色器有一个神秘的问题。

如果我指定 UBO 的位置​​,它不会显示任何内容。没有 location=0 的相同着色器工作正常 - 在这种情况下它的位置为 0,如 glGetUniformBlockIndex(hProgram, "color") returns。用location=0它returns-1;

#version 440

layout(location = 0) in vec2 uv;
layout(location = 4) uniform sampler2D tex;
layout(std140, binding=3, location=0) uniform color{
  vec4 inColor;
};

layout(location = 0) out vec4 outColor;

void main(){
  outColor = inColor * texture2D(tex, uv);
}

这根本不是有效的 GLSL。 location 限定符不能应用于统一块。

GLSL 4.40 specification 的第 4.4 "layout qualifiers" 节中的 table 列出了 location= 限定符仅对独立变量有效,但对块或块成员无效uniform 情况(在 in/out 接口块中是允许的,但那是完全不同的故事)。

实际上,UBO 块的 location 甚至 应该 是什么意思完全不清楚。 UBO 没有位置,其中的变量也没有。这些变量仅通过它们在缓冲区内的字节偏移量来寻址,这是由实现或您选择的布局规则(如 std140)定义的。

另请参阅 GL_ARB_uniform_buffer_object specification

中的第 1 期

(1) How are offsets to elements in a uniform buffer correlated to uniform locations?

Resolved: Traditional uniform locations were used in the glUniform API to access the private uniform storage. This API does not allow the use of glUniform to update uniforms stored in uniform blocks. Instead it uses the various means to update buffer objects, and exposes the byte offsets of the uniforms in the buffer object. So, in short, uniform locations and uniform offsets are similar concepts but unrelated.

总而言之:您的着色器甚至不应该使用其中的 location 限定符进行编译。而且它可能不会。