- OpenGL Graphics Primitives
- Data in OpenGL Buffers
- Vertex Specification
- OpenGL Drawing Commands
- Instanced Rendering
Instanced Rendering
Instancing, or instanced rendering, is a way of executing the same drawing commands many times in a row, with each producing a slightly different result. This can be a very efficient method of rendering a large amount of geometry with very few API calls. Several variants of already-familiar drawing functions exist to instruct OpenGL to execute the command multiple times. Further, various mechanisms are available in OpenGL to allow the shader to use the instance of the draw as an input, and to be given new values for vertex attributes per-instance rather than per-vertex. The simplest instanced rendering call is:
This is the instanced version of glDrawArrays(); note similarity of the two functions. The parameters of glDrawArraysInstanced() are identical to those of glDrawArrays(), with the addition of the primCount argument. This parameter specifies the count of the number of instances that are to be rendered. When this function is executed, OpenGL will essentially execute primCount copies of glDrawArrays(), with the mode, first, and count parameters passed through. There are *Instanced versions of several of the OpenGL drawing commands, including glDrawElementsInstanced() (for glDrawElements()) and glDrawElementsInstancedBaseVertex() (for glDrawElementsBaseVertex()). The glDrawElementsInstanced() function is defined as:
Again, note that the parameters to glDrawElementsInstanced() are identical to glDrawElements(), with the addition of primCount. Each time one of the instanced functions is called, OpenGL essentially runs the whole command as many times as is specified by the primCount parameter. This on its own is not terribly useful. However, there are two mechanisms provided by OpenGL that allow vertex attributes to be specified as instanced and to provide the vertex shader with the index of the current instance.
Instanced Vertex Attributes
Instanced vertex attributes behave similarly to regular vertex attributes. They are declared and used in exactly the same way inside the vertex shader. On the application side, they are also configured in the same way as regular vertex attributes. That is, they are backed by buffer objects, can be queried with glGetAttribLocation(), set up using glVertexAttribPointer(), and enabled and disabled using glEnableVertexAttribArray() and glDisableVertexAttribArray(). The important new function that allows a vertex attribute to become instanced is as follows:
The glVertexAttribDivisor() function controls the rate at which the vertex attribute is updated. index is the index of the vertex attribute whose divisor is to be set, and is the same as you would pass into glVertexAttribPointer() or glEnableVertexAttribArray(). By default, a new value of each enabled attribute is delivered to each vertex. Setting divisor to zero resets the attribute to this behavior and makes it a regular, noninstanced attribute. A nonzero value of divisor makes the attribute instanced and causes a new value to be fetched from the attribute array once every divisor instances rather than for every vertex. The index within the enabled vertex attribute array from which the attribute is taken is then , where instance is the current instance number and divisor is the value of divisor for the current attribute. For each of the instanced vertex attributes, the same value is delivered to the vertex shader for all vertices in the instance. If divisor is two, the value of the attribute is updated every second instance; if it is three then the attribute is updated every third instance, and so on. Consider the vertex attributes declared in Example 3.9, some of which will be configured as instanced.
Example 3.9. Vertex Shader Attributes for the Instancing Example
#version
410 core // "position" and "normal" are regular vertex attributeslayout
(location = 0)in vec4
position;layout
(location = 1)in vec3
normal; // Color is a per-instance attributelayout
(location = 2)in vec4
color; // model_matrix will be used as a per-instance transformation // matrix. Note that a mat4 consumes 4 consecutive locations, so // this will actually sit in locations, 3, 4, 5, and 6.layout
(location = 3)in mat4
model_matrix;
Note that in Example 3.9, there is nothing special about the declaration of the instanced vertex attributes color and model_matrix. Now consider the code shown in Example 3.10, which configures a subset of vertex attributes declared in Example 3.9 as instanced.
Example 3.10. Example Setup for Instanced Vertex Attributes
// Get the locations of the vertex attributes in "prog", which is // the (linked) program object that we're going to be rendering // with. Note that this isn't really necessary because we specified // locations for all the attributes in our vertex shader. This code // could be made more concise by assuming the vertex attributes are // where we asked the compiler to put them.int
position_loc = glGetAttribLocation(prog, "position");int
normal_loc = glGetAttribLocation(prog, "normal");int
color_loc = glGetAttribLocation(prog, "color");int
matrix_loc = glGetAttribLocation(prog, "model_matrix"); // Configure the regular vertex attribute arrays - // position and normal. glBindBuffer(GL_ARRAY_BUFFER, position_buffer); glVertexAttribPointer(position_loc, 4, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(position_loc); glBindBuffer(GL_ARRAY_BUFFER, normal_buffer); glVertexAttribPointer(normal_loc, 3, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(normal_loc); // Now we set up the color array. We want each instance of our // geometry to assume a different color, so we'll just pack colors // into a buffer object and make an instanced vertex attribute out // of it. glBindBuffer(GL_ARRAY_BUFFER, color_buffer); glVertexAttribPointer(color_loc, 4, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(color_loc); // This is the important bit... set the divisor for the color array // to 1 to get OpenGL to give us a new value of "color" per-instance // rather than per-vertex. glVertexAttribDivisor(color_loc, 1); // Likewise, we can do the same with the model matrix. Note that a // matrix input to the vertex shader consumes N consecutive input // locations, where N is the number of columns in the matrix. So... // we have four vertex attributes to set up. glBindBuffer(GL_ARRAY_BUFFER, model_matrix_buffer); // Loop over each column of the matrix...for
(int
i = 0; i < 4; i++) { // Set up the vertex attribute glVertexAttribPointer(matrix_loc + i, // Location 4, GL_FLOAT, GL_FALSE, // vec4sizeof
(mat4), // Stride (void
*)(sizeof
(vec4) * i)); // Start offset // Enable it glEnableVertexAttribArray(matrix_loc + i); // Make it instanced glVertexAttribDivisor(matrix_loc + i, 1); }
In Example 3.10, position and normal are regular, noninstanced vertex attributes. However, color is configured as an instanced vertex attribute with a divisor of one. This means that each instance will have a new value for the color attribute (which will be constant across all vertices in the instance). Further, the model_matrix attribute will also be made instanced to provide a new model transformation matrix for each instance. A mat4 attribute is consuming a consecutive location. Therefore, we loop over each column in the matrix and configure it separately. The remainder of the vertex shader is shown in Example 3.11.
Example 3.11. Instanced Attributes Example Vertex Shader
// The view matrix and the projection matrix are constant // across a drawuniform mat4
view_matrix;uniform mat4
projection_matrix; // The output of the vertex shader (matched to the // fragment shader)out
VERTEX { vec3 normal; vec4 color; } vertex; // Ok, go!void
main(void
) { // Construct a model-view matrix from the uniform view matrix // and the per-instance model matrix.mat4
model_view_matrix = view_matrix * model_matrix; // Transform position by the model-view matrix, then by the // projection matrix. gl_Position = projection_matrix * (model_view_matrix * position); // Transform the normal by the upper-left-3x3-submatrix of the // model-view matrix vertex.normal =mat3
(model_view_matrix) * normal; // Pass the per-instance color through to the fragment shader. vertex.color = color; }
The code to set the model matrices for the instances and then draw the instanced geometry using these shaders is shown in Example 3.12. Each instance has its own model matrix, whereas the view matrix (consisting of a rotation around the y axis followed by a translation in z) is common to all instances. The model matrices are written directly into the buffer by mapping it using glMapBuffer(). Each model matrix translates the object away from the origin and then rotates the translated model around the origin. The view and projection matrices are simply placed in uniform variables. Then, a single call to glDrawArraysInstanced() is used to draw all instances of the model.
Example 3.12. Instancing Example Drawing Code
// Map the buffer mat4 * matrices = (mat4 *)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); // Set model matrices for each instancefor
(n = 0; n < INSTANCE_COUNT; n++) {float
a = 50.0f *float
(n) / 4.0f;float
b = 50.0f *float
(n) / 5.0f;float
c = 50.0f *float
(n) / 6.0f; matrices[n] = rotation(a + t * 360.0f, 1.0f, 0.0f, 0.0f) * rotation(b + t * 360.0f, 0.0f, 1.0f, 0.0f) * rotation(c + t * 360.0f, 0.0f, 0.0f, 1.0f) * translation(10.0f + a, 40.0f + b, 50.0f + c); } // Done. Unmap the buffer. glUnmapBuffer(GL_ARRAY_BUFFER); // Activate instancing program glUseProgram(render_prog); // Set up the view and projection matrices mat4 view_matrix(translation(0.0f, 0.0f, -1500.0f) * rotation(t * 360.0f * 2.0f, 0.0f, 1.0f, 0.0f)); mat4 projection_matrix(frustum(-1.0f, 1.0f, -aspect, aspect, 1.0f, 5000.0f)); glUniformMatrix4fv(view_matrix_loc, 1, GL_FALSE, view_matrix); glUniformMatrix4fv(projection_matrix_loc, 1, GL_FALSE, projection_matrix); // Render INSTANCE_COUNT objects glDrawArraysInstanced(GL_TRIANGLES, 0, object_size, INSTANCE_COUNT);
The result of the program is shown in Figure 3.8. In this example, the constant INSTANCE_COUNT (which is referenced in the code of Examples 3.10 and 3.12) is 100. One hundred copies of the model are drawn, each with a different position and a different color. These models could very easily be trees in a forest, space ships in a fleet, or buildings in a city.
Figure 3.8. Result of rendering with instanced vertex attributes
There are some inefficiencies in the example shown in Examples 3.9 through 3.12. Work that will produce the same result across all of the vertices in an instance will still be performed per-vertex. Sometimes there are ways to get around this. For example, the computation of model_view_matrix will evaluate to the same matrix for all vertices within a single instance. Here, we could avoid this work by using a second instanced mat4 attribute to carry the per-instance model-view matrix. In other cases, it may not be possible to avoid this work, but it may be possible to move it into a geometry shader so that work is performed once per-primitive rather than once per-vertex, or perhaps use geometry shader instancing instead. Both of these techniques will be explained in Chapter 10.
Another example of a way to use instanced vertex attributes is to pack a set of textures into a 2D array texture and then pass the array slice to be used for each instance in an instanced vertex attribute. The vertex shader can then pass the instance’s slice into the fragment shader, which can then render each instance of the geometry with a different texture.
It is possible to internally add an offset to the indices used to fetch instanced vertex attributes from vertex buffers. Similar to the baseVertex parameter that is available through glDrawElementsBaseVertex(), the instance offset is exposed through an additional baseInstance parameter in some versions of the instanced drawing functions. The functions that take a baseInstance parameter are glDrawArraysInstancedBaseInstance(), glDrawElementsInstancedBaseInstance(), and glDrawElementsInstancedBaseVertexBaseInstance(). Their prototypes are as follows:
Using the Instance Counter in Shaders
In addition to instanced vertex attributes, the index of the current instance is available to the vertex shader in the built-in variable gl_InstanceID. This variable is implicitly declared as an integer. It starts counting from zero and counts up one each time an instance is rendered. gl_InstanceID is always present in the vertex shader, even when the current drawing command is not one of the instanced ones. In those cases, it will just be zero. The value in gl_InstanceID may be used to index into uniform arrays, perform texture lookups, as the input to an analytic function, or for any other purpose.
In the following example, the functionality of Examples 3.9 through 3.12 is replicated by using gl_InstanceID to index into texture buffer objects (TBOs) rather than through the use of instanced vertex attributes. Here, the vertex attributes of Example 3.9 are replaced with TBO lookups, and so are removed from the vertex attribute setup code. Instead, a first TBO containing color of each instance, and a second TBO containing the model matrices are created. The vertex attribute declaration and setup code are the same as in Examples 3.9 and 3.10 (with the omission of the color and model_matrix attributes, of course). As the instance’s color and model matrix is now explicitly fetched in the vertex shader, more code is added to the body of the vertex shader, which is shown in Example 3.13.
Example 3.13. gl_VertexID Example Vertex Shader
// The view matrix and the projection matrix are constant across a drawuniform mat4
view_matrix;uniform mat4
projection_matrix; // These are the TBOs that hold per-instance colors and per-instance // model matricesuniform samplerBuffer
color_tbo;uniform samplerBuffer
model_matrix_tbo; // The output of the vertex shader (matched to the fragment shader)out
VERTEX {vec3
normal;vec4
color; } vertex; // Ok, go!void
main(void
) { // Use gl_InstanceID to obtain the instance color from the color TBO vec4 color = texelFetch(color_tbo, gl_InstanceID); // Generating the model matrix is more complex because you can't // store mat4 data in a TBO. Instead, we need to store each // matrix as four vec4 variables and assemble the matrix in the // shader. First, fetch the four columns of the matrix // (remember, matrices are stored in memory in column-major // order).vec4
col1 = texelFetch(model_matrix_tbo, gl_InstanceID * 4);vec4
col2 = texelFetch(model_matrix_tbo, gl_InstanceID * 4 + 1);vec4
col3 = texelFetch(model_matrix_tbo, gl_InstanceID * 4 + 2);vec4
col4 = texelFetch(model_matrix_tbo, gl_InstanceID * 4 + 3); // Now assemble the four columns into a matrix.mat4
model_matrix = mat4(col1, col2, col3, col4); // Construct a model-view matrix from the uniform view matrix // and the per-instance model matrix.mat4
model_view_matrix = view_matrix * model_matrix; // Transform position by the model-view matrix, then by the // projection matrix. gl_Position = projection_matrix * (model_view_matrix * position); // Transform the normal by the upper-left-3x3-submatrix of the // model-view matrix vertex.normal =mat3
(model_view_matrix) * normal; // Pass the per-instance color through to the fragment shader. vertex.color = color; }
To drive the shader of Example 3.13, we need to create and initialize TBOs to back the color_tbo and model_matrix_tbo samplers rather than initializing the instanced vertex attributes. However, aside from the differences in setup code, the program is essentially unchanged.
Example 3.14 contains the code to set up the TBOs for use with the shader of Example 3.13.
Example 3.14. Example Setup for Instanced Vertex Attributes
// Get the locations of the vertex attributes in "prog", which is // the (linked) program object that we're going to be rendering // with. Note that this isn't really necessary because we specified // locations for all the attributes in our vertex shader. This code // could be made more concise by assuming the vertex attributes are // where we asked the compiler to put them.int
position_loc = glGetAttribLocation(prog, "position");int
normal_loc = glGetAttribLocation(prog, "normal"); // Configure the regular vertex attribute arrays - position and normal. glBindBuffer(GL_ARRAY_BUFFER, position_buffer); glVertexAttribPointer(position_loc, 4, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(position_loc); glBindBuffer(GL_ARRAY_BUFFER, normal_buffer); glVertexAttribPointer(normal_loc, 3, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(normal_loc); // Now set up the TBOs for the instance colors and model matrices... // First, create the TBO to store colors, bind a buffer to it and // initialize its format. The buffer has previously been created // and sized to store one vec4 per-instance. glGenTextures(1, &color_tbo); glBindTexture(GL_TEXTURE_BUFFER, color_tbo); glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, color_buffer); // Now do the same thing with a TBO for the model matrices. The // buffer object (model_matrix_buffer) has been created and sized // to store one mat4 per-instance. glGenTextures(1, &model_matrix_tbo); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_BUFFER, model_matrix_tbo); glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, model_matrix_buffer);
Note that the code in Example 3.14 is actually shorter and simpler than that in Example 3.10. This is because we have shifted the responsibility for fetching per-instance data from built-in OpenGL functionality to the shader writer. This can be seen in the increased complexity of Example 3.13 relative to Example 3.11. With this responsibility comes additional power and flexibility. For example, if the number of instances is small, it may be preferable to use a uniform array rather than a TBO for data storage, which may increase performance. Regardless, there are very few other changes that need to be made to the original example to move to using explicit fetches driven by gl_InstanceID. In fact, the rendering code of Example 3.12 is used intact to produce an identical result to the original program. The proof is in the screenshot (Figure 3.9).
Figure 3.9. Result of instanced rendering using gl_InstanceID
Instancing Redux
To use a instancing in your program
- Create some vertex shader inputs that you intend to be instanced.
- Set the vertex attribute divisors with glVertexAttribDivisor().
- Use the gl_InstanceID built-in variable in the vertex shader.
- Use the instanced versions of the rendering functions such as glDrawArraysInstanced()glDrawElementsInstanced(), or glDrawElementsInstancedBaseVertex().