opengl

What's the point of glMultiDrawElements?


I was calling glBindVertexArray and glDrawElements many times to draw VAO / VBO pairs and found that regardless of the size of the VBO / Indices for each mesh, calling glDrawElements too many times causes a large bottleneck, so I started looking into glMultiDrawElements.

I thought, cool, I'll just call that function instead of using a for loop! No, it seems I have to create a new merged VBO and Indices. Once more, from the few examples I was able to find online, it seems the indices reference the entire VBO. I was expecting multiple offsets of indices, each starting at 0 with the respective offset of the start of each mesh in the VBO. Now I have to add offsets to each index after the first mesh.

At this point, I might as well just make one big VAO / VBO / Indices for all the meshes and call glDrawElements a single time. If the meshes were identical then I would just use glDrawElementsInstanced.

So, my conclusion is that glMultiDrawElements is pointless, not to mention the atrocious indices parameter which could really be improved. What am I missing, what is the point of this function?


Solution

  • A drawing command can be given a base vertex index, per the function glDrawElementsBaseVertex. The "base vertex" is an index that is added to every index read from the index buffer before using it to fetch the vertices from the vertex buffer. This should allow you to not have to manually increment the indices yourself when shoving all of your meshes into the same buffer.

    There is a multidraw equivalent to this: glMultiDrawElementsBaseVertex, where each draw call in the multidraw operation is given its own base vertex.

    And of course the indirect rendering version of multidraw comes with base vertices as part of the multidraw data.