GLSL 在程序之间共享制服#130

Ban*_*urt 3 opengl legacy glsl

我目前正在尝试弄清楚如何在旧 GLSL 中的着色器之间共享统一。在制服前面粘贴“共享”无法编译。 编辑:我知道制服的范围是一个程序。modeliew-projection-matrix 就是一个例子。人们不想为每个程序单独设置它,但只设置一次。

有办法做到吗?

这是(顶点)着色器代码:

#version 130
in vec4 position;
in float size;
in vec4 incol;
out vec4 color;
shared uniform ivec4 relWorldOffset;
uniform vec4[14] cubestrip;
uint cubeindex;
void main()
{
  gl_Position = gl_ModelViewProjectionMatrix
   * (cubestrip[cubeindex] * size
   + relWorldOffset + position);
  cubeindex++;
  color = incol;
  cubeindex %= 14U;
Run Code Online (Sandbox Code Playgroud)

这是错误:

0:6(1): error: syntax error, unexpected NEW_IDENTIFIER, expecting $end
Run Code Online (Sandbox Code Playgroud)

Art*_*nen 5

GLSL 中没有shared这样的关键字。您可能正在寻找统一块统一缓冲区对象(UBO)。根据OpenGL wiki,它们需要 OpenGL 3.1 版本(因此#version 140需要 GLSL 或更高版本)。如果这不是问题,GLSL 语法将如下所示:

uniform MatrixBlock
{
    mat4 projection;
    mat4 modelview;
};
Run Code Online (Sandbox Code Playgroud)

另外,请查看本教程GLSL 1.40 规范(第 4.3.5.1 章)以获取更多指导。

(编辑:实际上,shared关键字是在最新的 OpenGL 版本中定义的,但它仅用作计算着色器中的布局限定符,以使变量在工作组内共享。)

  • @Banyoghurt:事实并非您使用的是英特尔硬件,您必须这样做。这实际上是因为 GLSL 1.30 中没有定义统一缓冲区对象。当您告诉兼容的 GLSL 编译器您正在编译 GLSL 1.30 着色器时,如果没有“#extension ...”指令,兼容的 GLSL 编译器将不允许您使用它们。有些供应商比其他供应商更合规,但这并不仅仅因为您使用英特尔硬件而发生。 (4认同)