Uniform block binding points act in exactly the same way as texture units.
In your shader, you refer to a texture sampler (e.g. a sampler2D
) and you associate this sampler to a texture unit by calling glUniform1i(samplerUniformIndex, textureUnitNumber);
. Before drawing with the shader, you then bind a texture to this textureUnitNumber
using a combination of glActiveTexture
(to set the active texture unit), and glBindTexture
to bind a texture to the active texture unit. So there are three concepts here:
- A binding point (the texture unit)
- The resource to be used (the texture)
- The thing using the resource (the sampler uniform in your shader)
Binding points for uniform blocks are the same. You have:
- A uniform block binding point
- A resource to be used. In this case this is the buffer object which stores the uniform block data.
- The thing using the resource, which is the uniform block in your shader.
We can compare how a texture and uniform buffer objects are used.
Associating the binding point with the thing using the resource:
Textures:
GLuint samplerUniformIndex;
glUseProgram(program);
samplerUniformIndex = glGetUniformLocation(program, "mysampler");
glUniform1i(samplerUniformIndex, textureUnitNumber);
Uniform buffers:
GLuint uniformBlockIndex = glGetUniformBlockIndex(program, "myblock");
glUniformBlockBinding(program, uniformBlockIndex, uniformBlockBinding);
Associating the resource with its binding point:
Textures:
glActiveTexture(textureUnitNumber);
glBindTexture(textureTarget, texture);
Uniform buffers:
glBindBufferBase(GL_UNIFORM_BUFFER, uniformBlockBinding, uniformBuffer);
Or you can use glBindBufferRange
to bind a specific range of the uniform buffer.