Direct State AccessGL 4.5

glVertexArrayBindingDivisor

Makes a vertex buffer binding advance once per instance instead of once per vertex (instancing).

Signature

void glVertexArrayBindingDivisor(GLuint vaobj, GLuint bindingindex, GLuint divisor);

How it works

With divisor 0 (the default) the binding moves to the next element for every vertex. With divisor 1 it moves once per instance, so every vertex of instance 5 reads element 5: one model matrix, colour or offset per copy. With 2 it moves every two instances, and so on.

It applies to a buffer binding point, not to an attribute: every attribute that reads from that binding (glVertexArrayAttribBinding) becomes per-instance.

Parameters

vaobjGLuint

The VAO.

bindingindexGLuint

The binding point (the one used in glVertexArrayVertexBuffer).

divisorGLuint

0 = per vertex, 1 = per instance, n = every n instances.

Tips & pitfalls

  • Direct State Access (GL 4.5): the object is passed as a parameter, so nothing needs to be bound and no global state is disturbed.

  • A mat4 attribute takes 4 locations (one vec4 each); give all 4 the same binding, and that binding the divisor.

Example

example.cpp
1// binding 0 = per-vertex mesh, binding 1 = per-instance offsets
2glVertexArrayVertexBuffer(vao, 1, offsetsVbo, 0, sizeof(glm::vec2));
3glVertexArrayAttribFormat(vao, 2, 2, GL_FLOAT, GL_FALSE, 0);
4glVertexArrayAttribBinding(vao, 2, 1);
5glEnableVertexArrayAttrib(vao, 2);
6glVertexArrayBindingDivisor(vao, 1, 1);
Official Khronos documentation