@@ -164,15 +164,27 @@ bool CpuTextureManager::readTexture(
164164 QOpenGLFunctions glF;
165165 glF.initializeOpenGLFunctions ();
166166
167- // Create a FBO for the texture
168- unsigned int fbo;
169- glF.glGenFramebuffers (1 , &fbo);
170- glF.glBindFramebuffer (GL_FRAMEBUFFER, fbo);
167+ // Create global FBO
168+ if (m_fbo == 0 ) {
169+ glF.glGenFramebuffers (1 , &m_fbo);
170+
171+ QObject::connect (QOpenGLContext::currentContext (), &QOpenGLContext::aboutToBeDestroyed, []() {
172+ if (QOpenGLContext::currentContext ()) {
173+ QOpenGLFunctions glF;
174+ glF.initializeOpenGLFunctions ();
175+ glF.glDeleteFramebuffers (1 , &m_fbo);
176+ m_fbo = 0 ;
177+ }
178+ });
179+ }
180+
181+ // Bind the texture to the global FBO
182+ glF.glBindFramebuffer (GL_FRAMEBUFFER, m_fbo);
171183 glF.glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, handle, 0 );
172184
173185 if (glF.glCheckFramebufferStatus (GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
174186 qWarning () << " error: framebuffer incomplete (CpuTextureManager)" ;
175- glF.glDeleteFramebuffers ( 1 , &fbo );
187+ glF.glBindFramebuffer (GL_FRAMEBUFFER, 0 );
176188 return false ;
177189 }
178190
@@ -310,7 +322,6 @@ bool CpuTextureManager::readTexture(
310322
311323 // Cleanup
312324 glF.glBindFramebuffer (GL_FRAMEBUFFER, 0 );
313- glF.glDeleteFramebuffers (1 , &fbo);
314325
315326 return true ;
316327}
0 commit comments