diff --git a/app/render/backend/opengl/openglshader.cpp b/app/render/backend/opengl/openglshader.cpp index 13d2d8fd1..0f17c5b12 100644 --- a/app/render/backend/opengl/openglshader.cpp +++ b/app/render/backend/opengl/openglshader.cpp @@ -230,3 +230,13 @@ QString OpenGLShader::CodeAlphaAssociate(const QString &function_name) " return vec4(col.rgb * col.a, col.a);\n" "}\n").arg(function_name); } + +void OpenGLShader::Lock() +{ + lock_.lock(); +} + +void OpenGLShader::Unlock() +{ + lock_.unlock(); +} diff --git a/app/render/backend/opengl/openglshader.h b/app/render/backend/opengl/openglshader.h index e9f491f4c..7f250e693 100644 --- a/app/render/backend/opengl/openglshader.h +++ b/app/render/backend/opengl/openglshader.h @@ -2,6 +2,7 @@ #define OPENGLSHADER_H #include +#include #include #include @@ -32,7 +33,11 @@ public: static QString CodeAlphaReassociate(const QString& function_name); static QString CodeAlphaAssociate(const QString& function_name); + void Lock(); + void Unlock(); + private: + QMutex lock_; }; #endif // OPENGLSHADER_H diff --git a/app/render/backend/opengl/openglworker.cpp b/app/render/backend/opengl/openglworker.cpp index 62bca4a94..f468baedc 100644 --- a/app/render/backend/opengl/openglworker.cpp +++ b/app/render/backend/opengl/openglworker.cpp @@ -157,6 +157,8 @@ void OpenGLWorker::RunNodeAccelerated(const Node *node, const NodeValueDatabase buffer_.Bind(); + // Lock the shader so no other thread interferes as we set parameters and draw (and we don't interfere with any others) + shader->Lock(); shader->bind(); unsigned int input_texture_count = 0; @@ -266,6 +268,11 @@ void OpenGLWorker::RunNodeAccelerated(const Node *node, const NodeValueDatabase // Blit this texture through this shader olive::gl::Blit(shader); + // Make sure all OpenGL functions are complete by this point before unlocking the shader (or another thread may + // change its parameters before our drawing in this thread is done) + functions_->glFinish(); + shader->Unlock(); + // Release any textures we bound before while (input_texture_count > 0) { input_texture_count--; @@ -281,8 +288,6 @@ void OpenGLWorker::RunNodeAccelerated(const Node *node, const NodeValueDatabase buffer_.Detach(); - functions_->glFinish(); - output_params->Push(NodeParam::kTexture, QVariant::fromValue(output)); }