UniformsGL 3.1

glGetUniformBlockIndex

Finds the index of a named uniform block inside a program.

Part of the family glGet*

Signature

GLuint glGetUniformBlockIndex(GLuint program, const GLchar *uniformBlockName);

How it works

A uniform block groups uniforms whose values come from a buffer (a UBO) instead of glUniform* calls. To connect a block to a binding point on older GLSL, you first need its index — that is what this returns. Then glUniformBlockBinding connects index → binding point.

Parameters

programGLuint

Linked program.

uniformBlockNameconst GLchar *

Block name as declared in GLSL (the name before the braces).

Return value

The block index, or GL_INVALID_INDEX if not found.

Tips & pitfalls

  • With GLSL 4.20+, layout(std140, binding = N) sets the binding in the shader and both calls become unnecessary.

Example

example.cpp
1GLuint idx = glGetUniformBlockIndex(program, "Matrices");
2glUniformBlockBinding(program, idx, 0);
Official Khronos documentation