16 Oktober 2015
Posted by Shanee Nishry
Previously, we showed how you can use vertex layout qualifiers to increase the performance and determinism of your OpenGL application. In this post, we’ll show another useful technique that will help you produce increased performance and cleaner code when drawing objects.
Before drawing onto the screen, you need to bind your vertex data (e.g. positions, normals, UVs) to the corresponding vertex shader attributes. To do that, you need to bind the vertex buffer, enable the generic vertex attribute, and use glVertexAttribPointer to describe the layout of the buffer.
Therefore, a draw call might look like this:
const GLuint ATTRIBUTE_LOCATION_POSITIONS = 0; const GLuint ATTRIBUTE_LOCATION_TEXTUREUV = 1; const GLuint ATTRIBUTE_LOCATION_NORMALS = 2; // Bind shader program, uniforms and textures // ... // Bind the vertex buffer glBindBuffer( GL_ARRAY_BUFFER, vertex_buffer_object ); // Set the vertex attributes glEnableVertexAttribArray( ATTRIBUTE_LOCATION_POSITIONS ); glVertexAttribPointer( ATTRIBUTE_LOCATION_POSITIONS, 3, GL_FLOAT, GL_FALSE, 32, 0 ); glEnableVertexAttribArray( ATTRIBUTE_LOCATION_TEXTUREUV ); glVertexAttribPointer( ATTRIBUTE_LOCATION_TEXTUREUV, 2, GL_FLOAT, GL_FALSE, 32, 12 ); glEnableVertexAttribArray( ATTRIBUTE_LOCATION_NORMALS ); glVertexAttribPointer( ATTRIBUTE_LOCATION_NORMALS, 3, GL_FLOAT, GL_FALSE, 32, 20 ); // Draw elements glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_SHORT, 0 );
There are several reasons why we might not like this code very much. The first is that we need to cache the layout of the vertex buffer to enable and disable the right attributes before drawing. This means we are either hard-coding or saving some amount of data for a nearly meaningless task.
The second reason is performance. Having to tell the drivers which attributes to individually activate is suboptimal. It would be best if we could precompile this information and deliver it all at once.
Lastly, and purely for aesthetics, our draw call is cluttered by long boilerplate code. It would be nice to get rid of it.
Did you know there is another reason why someone might frown on this code? The code is making use of layout qualifiers which is great! But, since it’s already using OpenGL ES 3+, it would be even better if the code also used Geometry Instancing. By batching many instances of a mesh into a single draw call, you can really boost performance. |
So how can we improve on the above code?
If you are using OpenGL ES 3 or higher, you should use Vertex Array Objects (or "VAOs") to store your vertex attribute state.
Using a VAO allows the drivers to compile the vertex description format for repeated use. In addition, this frees you from having to cache the vertex format needed for glVertexAttribPointer, and it also results in less per-draw boilerplate code.
The first thing you need to do is create your VAO. This is created once per mesh, alongside the vertex buffer object and is done like this:
const GLuint ATTRIBUTE_LOCATION_POSITIONS = 0; const GLuint ATTRIBUTE_LOCATION_TEXTUREUV = 1; const GLuint ATTRIBUTE_LOCATION_NORMALS = 2; // Bind the vertex buffer object glBindBuffer( GL_ARRAY_BUFFER, vertex_buffer_object ); // Create a VAO GLuint vao; glGenVertexArrays( 1, &vao ); glBindVertexArray( vao ); // Set the vertex attributes as usual glEnableVertexAttribArray( ATTRIBUTE_LOCATION_POSITIONS ); glVertexAttribPointer( ATTRIBUTE_LOCATION_POSITIONS, 3, GL_FLOAT, GL_FALSE, 32, 0 ); glEnableVertexAttribArray( ATTRIBUTE_LOCATION_TEXTUREUV ); glVertexAttribPointer( ATTRIBUTE_LOCATION_TEXTUREUV, 2, GL_FLOAT, GL_FALSE, 32, 12 ); glEnableVertexAttribArray( ATTRIBUTE_LOCATION_NORMALS ); glVertexAttribPointer( ATTRIBUTE_LOCATION_NORMALS, 3, GL_FLOAT, GL_FALSE, 32, 20 ); // Unbind the VAO to avoid accidentally overwriting the state // Skip this if you are confident your code will not do so glBindVertexArray( 0 );
You have probably noticed that this is very similar to our previous code section except that we now have the addition of:
// Create a vertex array object GLuint vao; glGenVertexArrays( 1, &vao ); glBindVertexArray( vao );
These lines create and bind the VAO. All glEnableVertexAttribArray and glVertexAttribPointer calls after that are recorded in the currently bound VAO, and that greatly simplifies our per-draw procedure as all you need to do is use the newly created VAO.
The next time you want to draw using this mesh all you need to do is bind the VAO using glBindVertexArray.
// Bind shader program, uniforms and textures // ... // Bind Vertex Array Object glBindVertexArray( vao ); // Draw elements glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_SHORT, 0 );
You no longer need to go through all the vertex attributes. This makes your code cleaner, makes per-frame calls shorter and more efficient, and allows the drivers to optimize the binding stage to increase performance.
Did you notice we are no longer calling glBindBuffer? This is because calling glVertexAttribPointer while recording the VAO references the currently bound buffer even though the VAO does not record glBindBuffer calls on itself. |
Want to learn more how to improve your game performance? Check out our Game Performance article series. If you are building on Android you might also be interested in the Android Performance Patterns.