From 829e385a32cead5011b0cae20b4da86b153b421b Mon Sep 17 00:00:00 2001 From: Erik Smistad Date: Tue, 15 Aug 2023 13:21:10 +0200 Subject: [PATCH] GL context needed in Image when deleting GL texture. This may not always be the case. Added some checks, and allowed renderer to delete it instead. --- source/FAST/Data/Image.cpp | 7 ++++++- source/FAST/Visualization/ImageRenderer/ImageRenderer.cpp | 4 ++-- .../SegmentationRenderer/SegmentationRenderer.cpp | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/source/FAST/Data/Image.cpp b/source/FAST/Data/Image.cpp index cf72b474f..e0e2628ea 100644 --- a/source/FAST/Data/Image.cpp +++ b/source/FAST/Data/Image.cpp @@ -738,7 +738,12 @@ void Image::freeAll() { this->free(Host::getInstance()); } if(m_GLtextureID > 0) { - glDeleteTextures(1, &m_GLtextureID); + // We have to have current GL context to delete it + if(QGLContext::currentContext() == nullptr) { + reportWarning() << "Unable to delete texture because no OpenGL context was current" << reportEnd(); + } else { + glDeleteTextures(1, &m_GLtextureID); + } m_GLtextureID = 0; m_GLtextureUpToDate = false; } diff --git a/source/FAST/Visualization/ImageRenderer/ImageRenderer.cpp b/source/FAST/Visualization/ImageRenderer/ImageRenderer.cpp index 5b1df8a88..d209d2974 100644 --- a/source/FAST/Visualization/ImageRenderer/ImageRenderer.cpp +++ b/source/FAST/Visualization/ImageRenderer/ImageRenderer.cpp @@ -106,7 +106,7 @@ void ImageRenderer::draw(Matrix4f perspectiveMatrix, Matrix4f viewingMatrix, flo glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h); //if(w != input->getWidth() && h != input->getHeight()) { // Remove old texture - //glDeleteTextures(1, &mTexturesToRender[inputNr]); // We cannot delete the texture, because it belongs to the Image object. + glDeleteTextures(1, &mTexturesToRender[inputNr]); mTexturesToRender.erase(inputNr); glDeleteVertexArrays(1, &mVAO[inputNr]); mVAO.erase(inputNr); @@ -117,7 +117,7 @@ void ImageRenderer::draw(Matrix4f perspectiveMatrix, Matrix4f viewingMatrix, flo //} } - auto access = input->getOpenGLTextureAccess(ACCESS_READ, device); + auto access = input->getOpenGLTextureAccess(ACCESS_READ, device, false, true); auto textureID = access->get(); mTexturesToRender[inputNr] = textureID; diff --git a/source/FAST/Visualization/SegmentationRenderer/SegmentationRenderer.cpp b/source/FAST/Visualization/SegmentationRenderer/SegmentationRenderer.cpp index 3cb15300a..bccc95685 100644 --- a/source/FAST/Visualization/SegmentationRenderer/SegmentationRenderer.cpp +++ b/source/FAST/Visualization/SegmentationRenderer/SegmentationRenderer.cpp @@ -95,7 +95,7 @@ void SegmentationRenderer::drawNormal(std::unordered_map 0) { // Delete old texture - //glDeleteTextures(1, &mTexturesToRender[inputNr]); // We cannot delete the texture, because it belongs to the Image object. + glDeleteTextures(1, &mTexturesToRender[inputNr]); mTexturesToRender.erase(inputNr); glDeleteVertexArrays(1, &mVAO[inputNr]); mVAO.erase(inputNr); @@ -105,7 +105,7 @@ void SegmentationRenderer::drawNormal(std::unordered_mapgetOpenGLTextureAccess(ACCESS_READ, device); + auto access = input->getOpenGLTextureAccess(ACCESS_READ, device, false, true); auto textureID = access->get(); mTexturesToRender[inputNr] = textureID; mImageUsed[inputNr] = input;