[Interest] How to share a native OpenGL context with Qt?

Marc Gilleron marc.gilleron at gmail.com
Thu Jun 25 14:50:51 CEST 2015

I managed to create our custom renderer by sharing the OpenGL context Qt
provides with qt_gl_global_share_context() and Qt::AA_ShareOpenGLContexts.
Using wglCreateContextAttribsARB() on our side, wharing worked without
errors, and it fixed some of our visual bugs.
We also made sure to call wglMakeCurrent() before we draw.

however I still get glitches: QML widget drawn in a half-quad, black
window, Qt window resizing our framebuffer, hover effects making our
renderer flicker... etc

I tried to launch gDebugger to see what's going on, and it seems not all of
Qt's contextes are shared.
Any idea how it can be solved?

2015-06-18 18:11 GMT+02:00 Marc Gilleron <marc.gilleron at gmail.com>:

> Actually, here is what we have:
> Our application has a main thread loop.
> It loads Qt dynamically (because we don't necessarily need it at compile
> time). Qt doesn't wraps our app.
> At some point in our main thread, we create a QApplication and widgets.
> Then, as we did before Qt integration, we load our OpenGL renderer. It
> creates its own context, may load resources at any time, and currently
> draws in its own non-Qt window.
> But, the simple fact we now have Qt Windows and non-Qt windows with OpenGL
> at the same time produces glitches in our renderer, and we're trying to
> solve it.
> (We do this to keep them independant from each other. In the future we may
> wrap our renderer window in a QWindow but at the moment we try to make it
> just work).
> Here is the kind of code I used to perform sharing, just after we create
> our custom OpenGL context:
>         // Get the main Qt context
>         Q_ASSERT(qGuiApp);
>         QOpenGLContext * qtContext = qt_gl_global_share_context();
>         if (qtContext == 0)
>         {
>             // Qt: Global OpenGL context is not available
>             return 0;
>         }
> #ifdef WIN32
>         // We get our renderer's OpenGL context
>         HGLRC hOurContext = /* get from somewhere */
>         HWND hOurWindow = /* get from somewhere */
>         QWGLNativeContext ourNativeContext(hOurContext, hOurWindow);
>         QOpenGLContext * ourContext = new QOpenGLContext;
>         ourContext->setNativeHandle(QVariant::fromValue(ourNativeContext));
>         ourContext->setShareContext(qtContext);
>         ourContext->create();
> #else
> However I didn't noticed any difference. Or is this the wrong way to share?
> Or maybe there is one more problem:
> we are still single-threaded, so we need to make sure the current OpenGL
> is the good one when we perform our calls.
> I said we cannot use the the approaches needing paintGL() and the like,
> because we would be outside of our main loop, and would lead to
> side-effects.
> I also wonder if we can put the Qt main loop in ours, because at the
> moment we have no clue when it updates...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150625/686a1bc3/attachment.html>

More information about the Interest mailing list