GL ARB vertex buffer object
This extension provides a mechanism for storing vertex array data in "fast" memory (video or AGP), thereby allowing for significant increases in vertex throughput between the application and the GPU. Similar functionality has since long been exposed by the GL_NV_vertex_array_range and GL_ATI_vertex_array_object extensions. GL_ARB_vertex_buffer_object supersedes these extensions with a new vendor-independent mechanism.
Chuncks of vertex array data are encapsulated in a vertex buffer objects (VBO). A VBO is an opaque handle. Much like a texture object or display list, you can associate data with a VBO but the actual storage location of the data is hidden from you. The API for creating a VBO is very similar to the texture object APIs. A VBO is created using glGenBuffers() and destroyed using glDeleteBuffers(). Before rendering from a VBO, it has to be made active using glBindBuffer().
Data can be written to a VBO in two ways. The first way is through the GL, using the glBufferData() or glBufferSubData() functions. These functions are conceptually similar to glTexImage*() and glTexSubImage*(). The second option is to obtain a direct pointer to the VBO data and to write the data to this memory area yourself. This technique is called "mapping a buffer". The application calls glMapBuffer() to obtain a pointer to the VBO, then writes data to it, and finishes by calling glUnmapBuffer(). When calling glMapBuffer(), you have to specify the desired access mode that you would like for the data. This can be GL_READ_ONLY, GL_WRITE_ONLY or GL_READ_WRITE. The meanings of these are self-explanatory. Obviously you should not attempt to read from a write-only mapping or vice versa.
It's important to note that a VBO cannot be rendered from while it is mapped. This restriction exists so that drivers may be free to move the data around as they see fit in order to provide the best performance. This also means that mapping and unmapping the same buffer twice will not necessarily give you back the same pointer. You should never store the pointer to a buffer after you've unmapped that buffer, and you should never pass it to the GL (e.g. to glVertexPointer() or related functions).
Also note that it's not possible to map a buffer until memory has been allocated for it. To do so, you have to use glBufferData() at least once. If you don't have any data available at buffer creation time, you can pass a null pointer to allocate uninitialized memory. You can then fill this memory later by mapping the buffer or by using glBufferSubData().
When allocating VBOs, you have to specify your intended usage for them. The first thing you have to specify is what kind of data you will store in the buffer -- this can be either vertex data or element data (i.e. indices). You do this by setting the target parameter of glBufferData() to GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER, respectively. The same target also has to be specified when binding or mapping the buffer. The second thing you have to specify is the access pattern you will use for your buffer.
The access pattern is specified as a combination of two settings. The first is how often you intend to modify the data. The three possible settings are:
- STATIC: You will specify the data only once, then use it many times without modifying it.
- STREAM: You will modify the data once, then use it once, and repeat this process many times.
- DYNAMIC: You will specify or modify the data repeatedly, and use it repeatedly after each time you do this.
The second setting indicates what the source and destination of the data will be. There are again three possibilities:
- DRAW: The data is generated by the application and passed to the GL for rendering.
- COPY: The data is generated by the GL, and copied into the VBO to be used for rendering.
- READ: The data is generated by the GL, and read back by the application. It is not used by the GL.
The usage parameter of glBufferData() is a combination of these two settings, as shown in this table:
Rendering from a VBO is like rendering from a normal vertex array, and can be done using any of the relevant OpenGL functions such as glDrawArrays(), glDrawElements() or glDrawRangeElements(). There are however two important steps to take. The first is that you have to make your VBO active by calling glBindBuffer(), taking care to bind the buffer to the correct target. The second is that you have to convert your vertex array pointers (glVertexPointer() and related) from direct pointers (which you don't have in the case of a VBO) to pointers that are relative to the start of your VBO. In other words, a pointer to the first byte in the VBO would now become a null pointer.
As mentioned in the introduction, GL_ARB_vertex_buffer_object offers a vendor-independent alternative to GL_NV_vertex_array_range and GL_ATI_vertex_array_object. These extensions exist because the standard OpenGL vertex array functionality requires vertex array data to reside in system memory. This makes it hard to obtain good vertex throughput from the application to the GPU. By enabling the application to store data directly in graphics memory (video or AGP), the GPU can get much faster access to it. Display lists can serve the same purpose, but the compilation step may take too much time for applications that deal with dynamic data, and the memory overhead is unknown to the developer.
Tips and tricks
- Mapping a buffer may cause your data to be lost.
- Array pointers are only treated as offsets if a buffer object is bound. Binding 0 returns to normal behaviour (i.e. direct pointers).
- Multiple buffers can be mapped simultaneously.
- All vertex attributes do not necessarily need to be in the same buffer object.