UniformsGL 3.1

glUniformBlockBinding

Connects a uniform block of a program to a numbered binding point.

Signature

void glUniformBlockBinding(GLuint program, GLuint uniformBlockIndex,
                           GLuint uniformBlockBinding);

How it works

Picture a row of numbered sockets. A buffer is plugged into socket N with glBindBufferBase(GL_UNIFORM_BUFFER, N, ubo). A program's block reads from socket N after glUniformBlockBinding(program, blockIndex, N). Many programs can read the same socket, so one camera UBO serves every shader.

Parameters

programGLuint

Linked program.

uniformBlockIndexGLuint
uniformBlockBindingGLuint

Binding point number (socket).

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GL_INVALID_VALUE
Index is not a valid block, or the binding is ≥ GL_MAX_UNIFORM_BUFFER_BINDINGS.

Example

example.cpp
1for (GLuint prog : { shaderRed, shaderGreen, shaderBlue }) {
2    GLuint idx = glGetUniformBlockIndex(prog, "Matrices");
3    glUniformBlockBinding(prog, idx, 0);
4}
5glBindBufferBase(GL_UNIFORM_BUFFER, 0, uboMatrices);
Official Khronos documentation