Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glDrawElements vs glDrawArray efficiency

I'm making a game for iOS and Android. I've seen in a lot of places that drawing using indices is more efficient than just drawing triangles array.

The thing is that i'm using lossy compressed vertices (like the md2's file format) and it takes less than just the indices alone - Array: N * 3 (xyz) * 1 (uchar) + translate (12 bytes) + scale (12 bytes). Element: N * 3 (xyz) * 4 (uint) + Array / ~10

It seems like the array is even better choice than indexed and compressed element, altough apple' s OpenGL profiler tool says that I should use glDrawElements..

Does the OpenGL implementation prefer indexed array? or it's because that the indexed array contains less data than the regular uncompressed array?

p.s. I'm using OpenGL es 2.0 and the vertex shader is the one who decompresses the vertices.

like image 544
Amir Avatar asked Jul 13 '11 17:07

Amir


People also ask

Is glDrawElements faster than glDrawArrays?

Unless all of your polys have distinct vertices (pretty unlikely), glDrawElements will be faster than glDrawArray because glDrawElements will take advantage of the fact that repeated vertices will be loaded into the gpu cache, and will not have to be loaded more than once for each poly that that vertex is a part of.

What is the difference between glDrawArrays and glDrawElements?

With glDrawArrays, OpenGL pulls data from the enabled arrays in order, vertex 0, then vertex 1, then vertex 2, and so on. With glDrawElements, you provide a list of vertex numbers. OpenGL will go through the list of vertex numbers, pulling data for the specified vertices from the arrays.


2 Answers

Are you using Vertex Arrays or Vertex Buffer Objects? If you are using Vertex Arrays, then you might want to consider looking into glDrawRangeElements rather than glDrawElements.

Unless all of your polys have distinct vertices (pretty unlikely), glDrawElements will be faster than glDrawArray because glDrawElements will take advantage of the fact that repeated vertices will be loaded into the gpu cache, and will not have to be loaded more than once for each poly that that vertex is a part of. However, with glDrawArray, it will iterate through the array and not be able to associate similar vertices, so it will have to load them multiple times.

However, it always depends on your specific situation. Try using a profiler or getting a framerate using each of these methods. It shouldn't be too hard to switch between them. If one is clearly better, use that one. If neither is outstandingly better, then it probably doesn't matter too much.

Remember, premature optimization is the root of all evil. Good luck! :)

like image 136
Andrew Rasmussen Avatar answered Oct 25 '22 03:10

Andrew Rasmussen


Using glDrawElements() will save a substantial amount of memory and you can specify the array elements in any order.

Using glDrawArrays(), however, your only option is to iterate sequentially over the list.

You have to experiment.

like image 41
Wroclai Avatar answered Oct 25 '22 05:10

Wroclai