drawElements vs drawArrays

Started by
6 comments, last by Danny02 12 years, 10 months ago
So I was checking out c3dl and they exclusively use drawArrays in their engine. Until now I had been using drawElements and now I am questioning which is the better choice.

With drawArrays I would think that you're vertex/normal/texcoord buffers are going to be larger since you cant index into them and thus consume more memory. However with this you don't have to store an indices buffer at all which saves memory, where as with drawElements you have to store and bind an indices buffer everytime.

For example:
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, getIndexBuffer());
could be removed from the render calls all together when using drawArrays over drawElements. It may seem like a negligable difference but if you are drawing 50 objects 60 times a second thats 3000 bind and accessor calls saved per second.

On the otherhand if you're buffers have a lot of repeated data in them you would save memory with drawElements, as well as have a much better cache hit/miss ratio. The linear pattern of drawArrays wouldn't give you any extra cache hits.

I hope this isn't too advanced.

Could anyone share what they think is the most efficient/preferred format and for what reasons?

Thanks!

Advertisement
You can't compare them like that. You have already provided cases where both are more efficient than the other, so you have already, yourself, argued why one of them cannot be universally better than the other. It is entirely situational which one is better for a particular case, and even depends on other factors than the number of binds and the size of the arrays.
Agreed Bob.

As I'm reading up more on gpu caches I am starting to lean toward drawElements being the better choice for most situations. I'm thinking (and feel free to disagree) that the only time one would want to use drawArrays would be when drawing strips since there will be hardly any repeated data. Is that the general rule?
It is entirely situational which one is better for a particular case, and even depends on other factors than the number of binds and the size of the arrays.

Could you provide some more information on this please? Thanks =)

I'm thinking (and feel free to disagree) that the only time one would want to use drawArrays would be when drawing strips since there will be hardly any repeated data. Is that the general rule?

I disagree that it is the only time.


It is entirely situational which one is better for a particular case, and even depends on other factors than the number of binds and the size of the arrays.

Could you provide some more information on this please? Thanks =)

It all comes down to particular setups. Optimizing these things, from a theoretical side so to say, is not my strong side, but there are people around here that know this very well. There are always trade-offs between various parameters. One solution may improve performance at the cost of doubling the number of buffer binds; but if you're not bind-performance bound, then binding more buffers may not be a problem. If you're not vertex processing-performance bound, then using index arrays to save vertex processing is not going to help. If you're vertex processing-performance bound, then index arrays may help, as long as you don't start binding too many buffers because you now have an index array to bind as well.

Many of these points may, in practice, not have such big relative impact as I make it sound like, but it's all just examples of how all these parameters may interact, and how you have to consider the problem. The trade-offs could very well change just because you decided to increase the level of details in your scene by increasing the polygon count of your characters and objects, or if you decide to increase the number of objects.

It is very complex, and perhaps you can guess from experience if you're experienced enough, but profiling will often give you a better answer. And keep in mind that, for maximum performance, it is not a question of indexing or not indexing on an application level, but on a per object level. Some objects may benefit from indexing, and others may not.
In 99% of all cases, using indices is better in every aspect.

Only in some rar cases u can save a few bytes of memory but it will probably be slower.
  • With VAOs u always have only one bind, so no advantage for [color="#1C2837"]drawArrays
  • [color="#1C2837"]when using indices u get the speed up of the post transform cach
  • [color="#1C2837"]when u have a standart 32byte vertex layout (4*(3+3+2)) u need less space with indices, if more then 6,25% of your vertices got used more then once. (using usigned short indices)
[color="#1c2837"]
[color="#1c2837"]On of these rare cases would be rendering point spirits.

[color="#1c2837"]

[color="#1c2837"] As Brother Bob explained very well, optimizing your OpenGL code depends on a lot of factors.

Thanks for the great responses guys, really appreciate it.
On a somewhat similar matter, would anyone know ways to keep track of gpu memory/usage? Something similar to the task manager of the cpu but for the gpu would be very helpful for monitoring my code.

Thanks so much!

take a look at :
http://www.ozone3d.net/gpushark/

its from http://www.geeks3d.com/ which is also a very cool blog with interessting stuff(shader samples for example)

This topic is closed to new replies.

Advertisement