ClearChain

OpenGL glTexSubImage2D very slow – a solution!

This article describes a possible reason glTexSubImage2D may be slow.

Recently I was working on a piece of code that updated a texture with a camera frame. The application was using the fixed function (non shaders) pipeline of OpenGL. Whilst I didn’t notice it at first, I soon found that the camera image was very delayed. For example the camera was capturing at 30 frames per second, but the screen update rate was only 6 frames per second.

After commenting out vast amounts of code, disabling functions the reason behind the slow frame rates was traced to the OpenGL call: glTexSubImage2D. The exact call that was being made was:

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,  mWidth, mHeight,  GL_RGB, GL_UNSIGNED_BYTE, image->getData());

Everything to this call looked normal. I tried a simple test program that used glTextSubImage2D and surprizingly it ran fast! So at this point I was a little confused. How could two very similar programs produce different results.
I kept searching. I tried Google searches found various comments but none of them seemed to work.

Eventually I looked at the Texture class I had been using and began commenting lines out of that. It was only when I found and commented out the line:

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE );

Things improved. This makes perfect sense as well. Generating a mipmap each frame is expensive. A quick fix and the application was running smoothly!

Exit mobile version