Search code examples
openglglsllegacy

GLSL share uniforms among programs #130


i'm currently trying to figure out, how I share a uniform between shaders in old GLSL. Sticking 'shared' in front of the uniform didn't compile. Edit: I know the scope of a uniform is one program. An example for this could be the modeliew-projection-matrix. One wouldn't want to set it for each program individually but only once.

Is there a way to do it?

This is the (vertex-)shader code:

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

This is the error:

0:6(1): error: syntax error, unexpected NEW_IDENTIFIER, expecting $end

Solution

  • There's no shared keyword like that in GLSL. You are probably looking for uniform blocks or uniform buffer objects (UBO). According to OpenGL wiki, they require OpenGL version 3.1 (so GLSL #version 140 or higher is required). If that's not a problem, the GLSL syntax would be as follows:

    uniform MatrixBlock
    {
        mat4 projection;
        mat4 modelview;
    };
    

    Also, take a look at this tutorial and GLSL 1.40 specification (chapter 4.3.5.1) for more pointers.

    (EDIT: Actually, shared keyword is defined in most recent OpenGL versions, but it is only used as a layout qualifier in compute shaders to make variable shared within a workgroup.)