glDeleteBuffers比glBufferData慢 [英] glDeleteBuffers slower than glBufferData
问题描述
我的iOS/Android游戏中存在一些性能问题,该游戏必须不时更新几个VBO.对游戏进行性能分析后,发现每次VBO更新glDeleteBuffers()最多需要7毫秒.当帧通常仅花费4 ms进行渲染时,这当然会导致打ic.
I'm having a bit of performance issue in my iOS/Android game where several VBO's have to be updated every once in a while. After profiling my game it turns out that glDeleteBuffers() takes up to 7ms per VBO update. This of course results in a hiccup when frames normally take only 4 ms to render.
这是我更新VBO的部分:
Here's the part where I update my VBO:
Chunk* chunk;
pthread_join(constructionThread, (void**)&chunk);
building = false;
if (vboID)
{
//takes 7 milliseconds
glDeleteBuffers(1, &vboID);
vboID = 0;
}
if (offset)
{
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
//takes about 1-2 milliseconds, which is acceptable
glBufferData(GL_ARRAY_BUFFER, offset * 4, constructionBuffer, GL_STATIC_DRAW);
}
其中offset是实例变量,基本上是新VBO的大小,这是相当可变的.我想vboID可以说明一切;)
where offset is an instance variable is basically the size of the new VBO, which is quite variable. vboID speaks for itself, I guess ;)
推荐答案
glGenBuffers
和glDeleteBuffers
分别设计为仅在初始化和清理时运行.在运行时调用它们是不好的.
glGenBuffers
and glDeleteBuffers
are designed to only be run on initialization and cleanup, respectively. Calling them during runtime is bad.
glBufferData
用新的数据集替换当前的缓冲区数据,这将自动更改缓冲区的大小.您可以安全地删除整个glGenBuffers
/glDeleteBuffers
东西,并将其移至初始化和清理中.
glBufferData
replaces the current buffer data with a new set of data, which automatically changes the size of the buffer. You can safely remove the whole glGenBuffers
/glDeleteBuffers
thing and move it into initialization and cleanup.
此外,您正在将缓冲区创建为静态缓冲区.这告诉OpenGL,您几乎永远都不会对其进行更改,因此它以一种在GPU上访问较快但从系统其余部分访问较慢的方式存储它.尝试将GL_STATIC_DRAW
更改为GL_DYNAMIC_DRAW
或GL_STREAM_DRAW
.此处的更多信息: http://www.opengl.org/wiki/Buffer_Objects#Buffer_Object_Usage
Additionally, you are creating the buffer as a static buffer. This is telling OpenGL that you will almost never change it so it stores it in a way that's quicker to access on the GPU but slower to access from the rest of the system. Try changing GL_STATIC_DRAW
to GL_DYNAMIC_DRAW
or GL_STREAM_DRAW
. More on this here: http://www.opengl.org/wiki/Buffer_Objects#Buffer_Object_Usage
这篇关于glDeleteBuffers比glBufferData慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!