OpenGL VAO最佳做法 [英] OpenGL VAO best practices

查看:272
本文介绍了OpenGL VAO最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题是我认为取决于VAO,但是我不确定..

我不确定VAO的正确用法,我在GL初始化期间曾经做过的事情很简单

glGenVertexArrays(1,&vao)

后跟一个

glBindVertexArray(vao)

后来,在我的绘图管道中,我只调用了 glBindBuffer(),glVertexAttribPointer(),glEnableVertexAttribArray()等.

这是正确的做法吗?

解决方案

VAO的行为与VBO和纹理的绑定方式相似.在程序的整个长度上绑定一个VAO不会产生任何性能上的好处,因为您可能完全不使用VAO进行渲染.实际上,它可能会更慢,具体取决于实现在绘制时如何拦截顶点属性设置.

VAO的目的是在初始化期间运行一次绘制对象所需的所有方法,并消除主循环中所有额外的方法调用开销.关键是要有多个VAO,并在绘制时在它们之间切换.

按照最佳做法,这是组织代码的方法:

initialization:
    for each batch
        generate, store, and bind a VAO
        bind all the buffers needed for a draw call
        unbind the VAO

main loop/whenever you render:
    for each batch
        bind VAO
        glDrawArrays(...); or glDrawElements(...); etc.
    unbind VAO

这避免了绑定/取消绑定缓冲区的麻烦,并且避免了传递每个顶点属性的所有设置,并仅用一个方法调用(绑定VAO)来替换它.

Im facing an issue which I believe to be VAO-dependant, but Im not sure..

I am not sure about the correct usage of a VAO, what I used to do during GL initialization was a simple

glGenVertexArrays(1,&vao)

followed by a

glBindVertexArray(vao)

and later, in my drawing pipeline, I just called glBindBuffer(), glVertexAttribPointer(), glEnableVertexAttribArray() and so on.. without caring about the initally bound VAO

is this a correct practice?

解决方案

VAOs act similarly to VBOs and textures with regard to how they are bound. Having a single VAO bound for the entire length of your program will yield no performance benefits because you might as well just be rendering without VAOs at all. In fact it may be slower depending on how the implementation intercepts vertex attribute settings as they're being drawn.

The point of a VAO is to run all the methods necessary to draw an object once during initialization and cut out all the extra method call overhead during the main loop. The point is to have multiple VAOs and switch between them when drawing.

In terms of best practice, here's how you should organize your code:

initialization:
    for each batch
        generate, store, and bind a VAO
        bind all the buffers needed for a draw call
        unbind the VAO

main loop/whenever you render:
    for each batch
        bind VAO
        glDrawArrays(...); or glDrawElements(...); etc.
    unbind VAO

This avoids the mess of binding/unbinding buffers and passing all the settings for each vertex attribute and replaces it with just a single method call, binding a VAO.

这篇关于OpenGL VAO最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆