I've been working on a Game Boy emulator using Qt and OpenGL. I'm pretty new to both Qt and OpenGL. It's in a pretty stable condition, and I removed what I thought was some wasteful code from the OpenGL routines. I have a routine that is called roughly 60 times every second by a Qt signal to update the OpenGL texture for the emulated screen. I changed this to just setting the texture's data with the new screen buffer. The problem I ran into is that leaving the wasteful code that destroys and creates a new texture somehow runs faster than just setting the new data. This is what I have:
void OpenGlWidget::updateEmulatedScreen(uint32_t *screenData) {
emulatedScreenTexture->destroy();
emulatedScreenTexture->create();
emulatedScreenTexture->setFormat(QOpenGLTexture::RGBA8_UNorm);
emulatedScreenTexture->setSize(EMULATED_SCREEN_RESOLUTION_X, EMULATED_SCREEN_RESOLUTION_Y);
emulatedScreenTexture->setMinMagFilters(QOpenGLTexture::Nearest, QOpenGLTexture::Nearest);
emulatedScreenTexture->setWrapMode(QOpenGLTexture::ClampToEdge);
emulatedScreenTexture->allocateStorage();
emulatedScreenTexture->setData(QOpenGLTexture::PixelFormat::RGBA, QOpenGLTexture::PixelType::UInt32_RGBA8, screenData);
this->update();
}
I changed it to this:
void OpenGlWidget::updateEmulatedScreen(uint32_t *screenData) {
emulatedScreenTexture->setData(QOpenGLTexture::PixelFormat::RGBA, QOpenGLTexture::PixelType::UInt32_RGBA8, screenData);
this->update();
}
To me, this seems a lot more efficient than destroying and creating a new texture each time, but it somehow slows the emulation down tremendously. I've tested multiple times without changing anything else, and adding the destroy/create speeds it back up every time. Am I missing something? Is it actually somehow worse to just set the data each time?