PDA

View Full Version : Optimizing glDrawElements



bhatiaharsh
03-26-2011, 01:10 PM
Hi,

I am using glDrawElements to highlight some special triangles in my mesh. My mesh contains about 720000 triangles. While after some computations, I want to highlight about 50000 of these triangles.

Earlier, I was maintaining a special array of vertices (corresponding to these special triangles), and rendering them as triangles using glDrawArrays. And I realized I was eating up a lot of memory to duplicate the vertices.

So, I tried switching it to glDrawElements. Now I am maintaining indices of the vertices corresponding to the special triangles, and rendering them using the vertices of the mesh.

My Question is, which of them is faster ?

1. If I use glDrawArrays, I spend extra memory to duplicate the vertices. But I only pass the vertices that are needed for the special triangles.

2. If I use glDrawElements, instead of 3 doubles (representing a vertex), I only need 1 integer for its index. But then, I have to pass this enormous sized mesh to openGl which contains mostly unused vertices.

I would like to save the memory unless the openGL call to glDrawElement is way too slower than glDrawArrays.

Any ideas ?

malexander
03-27-2011, 08:56 AM
2. If I use glDrawElements, instead of 3 doubles (representing a vertex), I only need 1 integer for its index. But then, I have to pass this enormous sized mesh to openGl which contains mostly unused vertices.

But you've already passed this large array to the GPU when you drew the large mesh, assuming you're using VBOs (which you should be). So you'll only be passing the new index array, which is very small, and using to index into the large array which is residing on the GPU.

BTW, if you are using doubles to pass the position, and aren't using the GL_ARB_vertex_attrib_64bit extension, then they are just being converted to 32b floats by the driver before being sent off to the GPU. If you are using the extension, be aware that you'll be losing 50% to 92% of your vertex shader performance, depending on the GPU.

So, in summary, use glDrawElements() with VBOs.

bhatiaharsh
03-28-2011, 10:36 AM
Yup. Thanks for the advise. I went for glDrawElements, and checked the timings. It seemed faster. :-)

Groovounet
03-29-2011, 06:13 AM
glDrawElements reduces the memory size of the vertex data and allows the use of GPU vertex cache... There is few good reasons to use glDrawArrays.