Qt World Summit: Register Today!

Can I share OpenGL Context

  • Dear all:

    I have a very silly question but that will help me a lot. I am making a software to display medical images and I need to have 4 widget to pain OpenGl stuff on then. The problem is that I need to use texture and this textures can be very big so I don't want to send the texture to the OpenGL Context for each widget, instead I would like to share the OpenGl Context in the 4 widget so they can share the the texture and any other objects there. So the question is can I share the OpenGlContext between different widgets?

    Thanks a lot for the help

  • Moderators

    The exact semantics depend on which widget you actually use but yes, you can share contexts without problem.

    With QGLWidget you can pass another QGLWidget as a parameter in the "constructor":http://qt-project.org/doc/qt-5/qglwidget.html#QGLWidget and it will create a shared context.

    In Qt 5.4 QOpenGLWidget context is implicitly shared with other contexts under the same window. You can also specify an application wide flag to make all contexts shared or explicitly share selected contexts with a OpenGlContext method. More details "in the docs":http://doc-snapshot.qt-project.org/qt5-5.4/qopenglwidget.html#context-sharing

    Apart from the portable Qt methods of sharing you can always resort to platform specific calls like WGL or GLX, but there's really no need to.

  • Dear Chris:

    Thanks very much for your help this is really what I need.

    Have a nice day

  • Hi Ernesto,
    I have the same problem as you faced. Have you solved this problem? I know that use the specific constructor of QGLWidget is helpful, but after geterating a new instance of QGLWidget by using that constructor, what should be done in the next?

    [quote author="epalmero" date="1417075809"]Dear Chris:

    Thanks very much for your help this is really what I need.

    Have a nice day

  • Hi Taigw

    When you use the share context then everytime you send a buffer in one Widget you don't need to send it in another one. I put you an example:

    Let's imaging you have 2 widgets and you want to draw the same triangle in both widget but with different color. What you do is that you send the geometry to the GPU using the QOpenGLBuffer objects in one of the Widget and you draw it using a color let's said red.

    After that you draw the second widget, if you will not share the context you will need to send the same triangle again to the GPU because the context does not know anything about that triangle. Now because you share the context you can simple use the QOpenGLBuffer that you have created in the first widget and draw the second triangle with the color that you want. You can do the same with all the object store in the Context like shaders, Buffers etc


Log in to reply