OpenGL VAO 最佳实践 [英] OpenGL VAO best practices

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

问题描述

我面临一个我认为依赖于 VAO 的问题,但我不确定..

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

我不确定 VAO 的正确用法,我过去在 GL 初始化期间所做的很简单

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)

后面跟着一个

glBindVertexArray(vao)

后来,在我的绘图管道中,我只是调用了glBindBuffer()、glVertexAttribPointer()、glEnableVertexAttribArray()等等......而不关心最初绑定的VAO

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

这是正确的做法吗?

推荐答案

VAO 在绑定方式上与 VBO 和纹理类似.将单个 VAO 绑定到整个程序长度不会产生任何性能优势,因为您可能只是在没有 VAO 的情况下进行渲染.事实上,它可能会更慢,具体取决于实现在绘制顶点属性设置时拦截它们的方式.

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.

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

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

这避免了绑定/解除绑定缓冲区和传递每个顶点属性的所有设置的混乱,并仅用一个方法调用替换它,绑定一个 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天全站免登陆