Skip to content

Commit

Permalink
Fixed multi-threading issues with RenderToImage
Browse files Browse the repository at this point in the history
  • Loading branch information
smistad committed Mar 7, 2024
1 parent 6b0ac55 commit 5362a07
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
29 changes: 16 additions & 13 deletions source/FAST/Visualization/RenderToImage/RenderToImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ RenderToImage::RenderToImage(Color bgcolor, int width, int height) {

if(QThread::currentThread() == QApplication::instance()->thread()) { // Is main thread?
// Main thread..
QGLWidget* widget = new QGLWidget;
QGLContext *context = new QGLContext(View::getGLFormat(), widget);
QGLContext *context = new QGLContext(View::getGLFormat(), fast::Window::getSecondaryGLContext()->device());
context->create(fast::Window::getSecondaryGLContext());
if(!context->isValid() || !context->isSharing()) {
throw Exception("The custom Qt GL context in fast::View is invalid!");
}
m_context = context;
} else {
// Computation thread
QGLWidget* widget = new QGLWidget;
QGLContext *context = new QGLContext(View::getGLFormat(), widget);
QGLContext *context = new QGLContext(View::getGLFormat(), fast::Window::getMainGLContext()->device());
context->create(fast::Window::getMainGLContext());
if(!context->isValid() || !context->isSharing()) {
throw Exception("The custom Qt GL context in fast::View is invalid!");
}
m_context = context;
}
m_context->makeCurrent();
initializeOpenGLFunctions();
}


Expand Down Expand Up @@ -82,8 +82,6 @@ std::vector<Renderer::pointer> RenderToImage::getRenderers() {

void RenderToImage::execute() {
m_context->makeCurrent();
initializeOpenGLFunctions();

bool doContinue = true;
for(auto renderer : getRenderers()) {
renderer->update(m_executeToken);
Expand All @@ -99,12 +97,11 @@ void RenderToImage::execute() {
recalculateCamera();
// Create framebuffer to render to
glGenFramebuffers(1, &m_FBO);
GLuint render_buf;
glGenRenderbuffers(1, &render_buf);
glBindRenderbuffer(GL_RENDERBUFFER, render_buf);
glGenRenderbuffers(1, &m_renderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_renderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, m_width, m_height);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_FBO);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, render_buf);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_renderBuffer);
if(!mNonVolumeRenderers.empty() && !mVolumeRenderers.empty()) {
glGenTextures(1, &m_textureColor);
glGenTextures(1, &m_textureDepth);
Expand All @@ -116,7 +113,6 @@ void RenderToImage::execute() {
// Assign textures to FBO
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_textureColor, 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_textureDepth, 0);

}
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
initializeGL();
Expand Down Expand Up @@ -443,8 +439,6 @@ void RenderToImage::initializeGL() {
initializeOpenGLFunctions();
glViewport(0, 0, m_width, m_height);
glEnable(GL_TEXTURE_2D);
// Enable transparency
glEnable(GL_BLEND);
if(mIsIn2DMode) {
glDisable(GL_DEPTH_TEST);
recalculateCamera();
Expand Down Expand Up @@ -542,6 +536,11 @@ void RenderToImage::reset() {
std::lock_guard<std::mutex> lock(m_mutex);
mNonVolumeRenderers.clear();
mVolumeRenderers.clear();
// Cleanup GL resources
glDeleteFramebuffers(1, &m_FBO);
glDeleteTextures(1, &m_textureColor);
glDeleteTextures(1, &m_textureDepth);
glDeleteRenderbuffers(1, &m_renderBuffer);
m_initialized = false;
}

Expand All @@ -563,4 +562,8 @@ void RenderToImage::setAutoUpdateCamera(bool autoUpdate) {
mAutoUpdateCamera = autoUpdate;
}

RenderToImage::~RenderToImage() {
reset();
}

}
2 changes: 2 additions & 0 deletions source/FAST/Visualization/RenderToImage/RenderToImage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ class FAST_EXPORT RenderToImage : public ProcessObject, protected QOpenGLFunctio
void set2DMode();
void set3DMode();
void setAutoUpdateCamera(bool autoUpdate);
~RenderToImage();
private:
void execute() override;
private:
uint m_FBO = 0;
uint m_textureColor = 0;
uint m_textureDepth = 0;
uint m_renderBuffer = 0;
std::vector<Renderer::pointer> mNonVolumeRenderers;
std::vector<Renderer::pointer> mVolumeRenderers;

Expand Down
2 changes: 0 additions & 2 deletions source/FAST/Visualization/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,6 @@ void View::initializeGL() {
initializeOpenGLFunctions();
glViewport(0, 0, this->width(), this->height());
glEnable(GL_TEXTURE_2D);
// Enable transparency
glEnable(GL_BLEND);
// Update all renderes, so that getBoundingBox works
std::vector<Renderer::pointer> renderers = getRenderers();
for(int i = 0; i < renderers.size(); i++) {
Expand Down

0 comments on commit 5362a07

Please sign in to comment.