From afa0ce90451c4c939c91c9ef157e1d60e4906085 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 24 Aug 2021 15:09:57 +0200 Subject: [PATCH] rhi: gl: Allow passing in a custom shareContext MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We already honor AA_ShareOpenGLContexts and pass in the QGuiApp's context as the shareContext for the QRhi's QOpenGLContext. Extend this to also allow specifying a QOpenGLContext in the init params struct. A good example of this is the backingstore compositor that serves QQuickWidget and co. If one wanted to implement that with (an OpenGL-based) QRhi, instead of direct OpenGL calls, then the ability to create a QRhi that uses a context that shares resources with a given other context becomes essential. Pick-to: 6.2 Change-Id: I6bc5ff8e803d467f8795197ac1f12fdc0f73bbd1 Reviewed-by: Christian Strømme --- src/gui/rhi/qrhigles2.cpp | 15 ++++++++++++--- src/gui/rhi/qrhigles2_p.h | 1 + src/gui/rhi/qrhigles2_p_p.h | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/gui/rhi/qrhigles2.cpp b/src/gui/rhi/qrhigles2.cpp index 7650e852ff..523269740c 100644 --- a/src/gui/rhi/qrhigles2.cpp +++ b/src/gui/rhi/qrhigles2.cpp @@ -107,8 +107,9 @@ QT_BEGIN_NAMESPACE of the returned QOffscreenSurface is transferred to the caller and the QRhi will not destroy it. - \note QRhiSwapChain can only target QWindow instances that have their - surface type set to QSurface::OpenGLSurface. + \note With the OpenGL backend, QRhiSwapChain can only target QWindow + instances that have their surface type set to QSurface::OpenGLSurface or + QSurface::RasterGLSurface. \note \l window is optional. It is recommended to specify it whenever possible, in order to avoid problems on multi-adapter and multi-screen @@ -117,6 +118,10 @@ QT_BEGIN_NAMESPACE an invisible window on some platforms (for example, Windows) and that may trigger unexpected problems in some cases. + In case resource sharing with an existing QOpenGLContext is desired, \l + shareContext can be set to an existing QOpenGLContext. Alternatively, + Qt::AA_ShareOpenGLContexts is honored as well, when enabled. + \section2 Working with existing OpenGL contexts When interoperating with another graphics engine, it may be necessary to @@ -424,6 +429,7 @@ QRhiGles2::QRhiGles2(QRhiGles2InitParams *params, QRhiGles2NativeHandles *import requestedFormat = QRhiGles2InitParams::adjustedFormat(params->format); fallbackSurface = params->fallbackSurface; maybeWindow = params->window; // may be null + maybeShareContext = params->shareContext; // may be null importedContext = importDevice != nullptr; if (importedContext) { @@ -472,7 +478,10 @@ bool QRhiGles2::create(QRhi::Flags flags) if (!importedContext) { ctx = new QOpenGLContext; ctx->setFormat(requestedFormat); - if (QOpenGLContext *shareContext = qt_gl_global_share_context()) { + if (maybeShareContext) { + ctx->setShareContext(maybeShareContext); + ctx->setScreen(maybeShareContext->screen()); + } else if (QOpenGLContext *shareContext = qt_gl_global_share_context()) { ctx->setShareContext(shareContext); ctx->setScreen(shareContext->screen()); } diff --git a/src/gui/rhi/qrhigles2_p.h b/src/gui/rhi/qrhigles2_p.h index 7273b8ee9e..b948b42be5 100644 --- a/src/gui/rhi/qrhigles2_p.h +++ b/src/gui/rhi/qrhigles2_p.h @@ -67,6 +67,7 @@ struct Q_GUI_EXPORT QRhiGles2InitParams : public QRhiInitParams QSurfaceFormat format; QOffscreenSurface *fallbackSurface = nullptr; QWindow *window = nullptr; + QOpenGLContext *shareContext = nullptr; static QOffscreenSurface *newFallbackSurface(const QSurfaceFormat &format = QSurfaceFormat::defaultFormat()); static QSurfaceFormat adjustedFormat(const QSurfaceFormat &format = QSurfaceFormat::defaultFormat()); diff --git a/src/gui/rhi/qrhigles2_p_p.h b/src/gui/rhi/qrhigles2_p_p.h index 8a0e0ec2a1..d55a32fefe 100644 --- a/src/gui/rhi/qrhigles2_p_p.h +++ b/src/gui/rhi/qrhigles2_p_p.h @@ -897,6 +897,7 @@ public: QSurfaceFormat requestedFormat; QSurface *fallbackSurface = nullptr; QWindow *maybeWindow = nullptr; + QOpenGLContext *maybeShareContext = nullptr; mutable bool needsMakeCurrent = false; QOpenGLExtensions *f = nullptr; uint vao = 0;