What Does gl_DrawID Represent in Multi-Draw Indirect?

In OpenGL shader programming, the built-in GLSL variable gl_DrawID represents the zero-based index of the specific draw command currently being executed within a multi-draw call, such as glMultiDrawArraysIndirect or glMultiDrawElementsIndirect. When rendering scenes using indirect drawing, multiple distinct sub-commands are packed into a single buffer and submitted to the GPU together. gl_DrawID allows vertex shaders to identify exactly which batch or sub-draw is executing, enabling dynamic indexing into global descriptor arrays, material data, and transformation matrices for high-performance, single-call batch rendering.

Understanding Multi-Draw Indirect Rendering

Modern graphics rendering pipelines prioritize minimizing CPU-GPU synchronization bottlenecks. In traditional workflows, issuing individual draw calls like glDrawElements for hundreds or thousands of separate objects creates significant driver overhead.

Multi-Draw Indirect (MDI) resolves this bottleneck by allowing applications to specify an array of draw parameters directly within a GPU buffer:

The Role of gl_DrawID

Because indirect commands pack dozens or thousands of individual draw operations into a single dispatch, shaders require a way to differentiate between these draws. This is where gl_DrawID functions:

Practical Applications in Modern GLSL

The primary utility of gl_DrawID is indexing into unbounded arrays of resource data stored in Shader Storage Buffer Objects (SSBOs) or uniform buffers.

Per-Object Transformation Matrices

Instead of rebinding uniforms between draws, an application uploads all object transformation matrices into a single SSBO. Inside the vertex shader, gl_DrawID indexes directly into this buffer:

#version 460 core

layout(std430, binding = 0) buffer ObjectData {
    mat4 modelMatrices[];
};

layout(location = 0) in vec3 inPosition;

void main() {
    mat4 model = modelMatrices[gl_DrawID];
    gl_Position = model * vec4(inPosition, 1.0);
}

Material and Texture Indexing

In combination with bindless textures, gl_DrawID enables a single multi-draw dispatch to render objects with entirely different materials:

Differences Between gl_DrawID, gl_InstanceID, and gl_BaseInstance

Distinguishing gl_DrawID from instancing variables is critical when structuring multi-draw architectures:

Using gl_DrawID eliminates the need to manually encode draw offsets into unused vertex attributes or instance offsets, simplifying GPU-driven rendering pipelines and maximizing batching efficiency.