Skip to content

Commit

Permalink
[Metal] fix issues with OpenGL context thread affinity
Browse files Browse the repository at this point in the history
- The OpenGL context must be moved from the QSGRenderThread to the main thread for initialisation
- After initialisation the OpenGL context is then moved to the Ignition render thread

Signed-off-by: Rhys Mainwaring <[email protected]>
  • Loading branch information
srmainwaring committed Dec 4, 2021
1 parent 3790758 commit b96632f
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/plugins/minimal_scene/MinimalScene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -896,34 +896,29 @@ void RenderWindowItem::Ready()
this->dataPtr->renderThread->Surface()->create();
}

// Carry out any initialization before moving to Render thread
// Carry out initialization on main thread before moving to render thread
this->dataPtr->renderThread->Initialize();

this->dataPtr->renderThread->ignRenderer.textureSize =
QSize(std::max({this->width(), 1.0}), std::max({this->height(), 1.0}));

// Move to Render thread
if (this->dataPtr->graphicsAPI == rendering::GraphicsAPI::OPENGL)
{
// Move context to the render thread
this->dataPtr->renderThread->Context()->moveToThread(
this->dataPtr->renderThread);
}

this->dataPtr->renderThread->moveToThread(this->dataPtr->renderThread);

this->dataPtr->renderThread->ignRenderer.textureSize =
QSize(std::max({this->width(), 1.0}), std::max({this->height(), 1.0}));

this->connect(this, &QQuickItem::widthChanged,
this->dataPtr->renderThread, &RenderThread::SizeChanged);
this->connect(this, &QQuickItem::heightChanged,
this->dataPtr->renderThread, &RenderThread::SizeChanged);

// Running on Render thread
this->dataPtr->renderThread->start();

// Completed initialising
this->dataPtr->initializing = false;
this->dataPtr->initialized = true;

// Trigger update
this->update();
}

Expand Down Expand Up @@ -959,10 +954,14 @@ QSGNode *RenderWindowItem::updatePaintNode(QSGNode *_node,
this->dataPtr->renderThread->Context()->setShareContext(current);
this->dataPtr->renderThread->Context()->create();

// Initialize on main thread
QMetaObject::invokeMethod(this, "Ready", Qt::QueuedConnection);
// The slot "Ready" runs on the main thread, move the context to match
this->dataPtr->renderThread->Context()->moveToThread(
QApplication::instance()->thread());

current->makeCurrent(this->window());

// Initialize on main thread
QMetaObject::invokeMethod(this, "Ready", Qt::QueuedConnection);
}
else if (this->dataPtr->graphicsAPI == rendering::GraphicsAPI::METAL)
{
Expand Down

0 comments on commit b96632f

Please sign in to comment.