Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawElements vs drawArrays in webgl

Does it make any sense to use drawElements instead of drawArrays if I'm not going to share any vertices?

If I understood correctly, with drawElements I have to use multiple drawcalls if the index array exceeds ~65k elements because of the uInt16 limitation of the indexbuffer ( in webgl ).

So could one say as a rule of thumb:

No shared vertices, use drawArrays because its just one big drawCall.

If shared vertices, use drawElements because GPU bandwith could be saved and this would result in better performance than drawArrays also if there are multiple drawcalls required?

like image 858
supernova Avatar asked Jan 21 '15 19:01

supernova


1 Answers

The advantage of glDrawElements() is in theory simple: you use fewer vertexes, and you get to use the post-T&L cache. The penalty is that you have to use an index array and cache locality might be worse.

Comparisons are complicated by the fact that the post-T&L cache will vary in size depending on (for example) how old your graphics card is. Newer cards might have huge caches, older cards might have smaller ones.

However, if you are not sharing any vertexes between primitives, then there is no advantage to glDrawElements() whatsoever, and you should use glDrawArrays().

In more complicated scenarios, when you are comparing the cost of additional draw calls (with glDrawElements()) against the cost of extra vertex data and more vertex shader invocations (with glDrawArrays()), I would want to profile.

like image 60
Dietrich Epp Avatar answered Oct 26 '22 13:10

Dietrich Epp