BuffersGL 3.0
glBindBufferRange
Connects only a slice of a buffer to a numbered binding point.
Part of the family glBind*
Signature
void glBindBufferRange(GLenum target, GLuint index, GLuint buffer, GLintptr offset, GLsizeiptr size);
How it works
Same idea as glBindBufferBase, but the shader only sees size bytes starting at offset. This lets one big buffer hold data for many objects, each draw pointing a binding at a different slice.
Parameters
targetGLenum
GL_UNIFORM_BUFFER, GL_SHADER_STORAGE_BUFFER, etc.
indexGLuint
Binding point number.
bufferGLuint
Buffer to attach.
offsetGLintptr
Start of the slice in bytes.
sizeGLsizeiptr
Size of the slice in bytes.
Tips & pitfalls
For uniform buffers, offset must be a multiple of GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT (often 256). Query it with
glGetIntegerv.
Errors
When the call is invalid it does nothing and sets an error, which glGetError returns:
- GL_INVALID_VALUE
- size ≤ 0, or offset is not correctly aligned.
Example
example.cpp
1GLint align = 0;
2glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &align);
3// Object i lives at i * align inside one big UBO
4glBindBufferRange(GL_UNIFORM_BUFFER, 1, uboObjects, i * align, sizeof(ObjectData));