glDeleteBuffers 比 glBufferData 慢

2022-01-09 00:00:00 opengl-es c++

我在我的 iOS/Android 游戏中遇到了一些性能问题,其中几个 VBO 必须不时更新.在对我的游戏进行分析后,结果表明 glDeleteBuffers() 每次 VBO 更新最多需要 7 毫秒.当帧通常只需要 4 毫秒来渲染时,这当然会导致打嗝.

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);
}

其中的偏移量是一个实例变量,基本上是新 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 ;)

推荐答案

glGenBuffersglDeleteBuffers 分别设计为仅在初始化和清理时运行.在运行时调用它们是不好的.

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_DRAWGL_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

相关文章