Skip to content

Commit e68f02e

Browse files
committed
CpuTextureManager: Use a single FBO for all textures
1 parent fd89971 commit e68f02e

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/cputexturemanager.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/cputexturemanager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class CpuTextureManager
4242
GLubyte **data,
4343
std::vector<QPoint> &points) const;
4444

45+
static inline GLuint m_fbo = 0; // single FBO for all texture managers
4546
std::unordered_map<GLuint, GLubyte *> m_textureData;
4647
std::unordered_map<GLuint, std::vector<QPoint>> m_convexHullPoints;
4748
};

test/texture/cputexturemanager_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ TEST_F(CpuTextureManagerTest, TextureDataAndHullPoints)
130130
ASSERT_EQ(hullPoints, refHullPoints2);
131131

132132
// Cleanup
133+
emit context.aboutToBeDestroyed();
133134
context.doneCurrent();
134135
}
135136

@@ -175,6 +176,7 @@ TEST_F(CpuTextureManagerTest, GetPointColor)
175176
// TODO: Test point transform (graphic effects that change shape)
176177

177178
// Cleanup
179+
emit context.aboutToBeDestroyed();
178180
context.doneCurrent();
179181
}
180182

@@ -219,5 +221,6 @@ TEST_F(CpuTextureManagerTest, TextureContainsPoint)
219221
// TODO: Test point transform (graphic effects that change shape)
220222

221223
// Cleanup
224+
emit context.aboutToBeDestroyed();
222225
context.doneCurrent();
223226
}

0 commit comments

Comments
 (0)