Use single vertex buffer or many?

给你一囗甜甜゛ 提交于 2019-12-19 03:56:36

问题


I'm implementing a 2D game with lots of independent rectangular game pieces of various dimensions. The dimensions of each piece do not change between frames. Most of the pieces will display an image and share the same fragment shader. I am new to WebGL and it is not clear to me what the best strategy is for managing vertex buffers in regard to performance for this situation.

Is it better to use a single vertex buffer (quad) to represent all of the game's pieces and then rescale those vertices in the vertex shader for each piece? Or, should I define a separate static vertex buffer for each piece?


回答1:


The GPU is a state machine, switching states is expensive(even more when done through WebGL because of the additional layer of checks introduced by the WebGL implementation) so binding vertex buffers is expensive.

Its good practice to reduce API calls to a minimum.

Even when having multiple distinct objects you still want to use a single vertex buffer and use the offset parameter of the drawArrays or drawElements methods.

Here is a list of API calls ordered by decreasing expensiveness(top is most expensive):

  • FrameBuffer
  • Program
  • Texture binds
  • Vertex format
  • Vertex bindings
  • Uniform updates

For more information on this you can watch this great talk Beyond Porting: How Modern OpenGL can Radically Reduce Driver Overhead by Cass Everitt and John McDonald, this is also where the list above comes from.

While these benchmarks were done on Nvidia hardware its a good guideline for AMD and Intel graphics hardware as well.



来源:https://stackoverflow.com/questions/26564104/use-single-vertex-buffer-or-many

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!