How to make multiple QGLWidgets share the same QGLContext?



  • Maybe this is an old question, but I can not find any workable solution.
    I have tried the approach as follow according to the Qt document.
    Create a QGLContext and pass it to QGLWidget's constructor
    @
    QGLContext*glContext=new QGLContext (QGLFormat::defaultFormat());

    glContext->create();// create() always fail without painter device

    QGLWidget * glWidget=new QGLWidget(glContext,this);//OK, context becomes valid

    QGLWidget * glWidget1=new QGLWidget(glContext,this);
    @
    When passing the context to the second QGLWidget, error "QGLWidget::setContext: Context must refer to this widget" is produced.
    It seems a context can only be used by one widget.

    Some suggest that use the second widget as shared widget of first one.
    @
    QGLWidget * glWidget=new QGLWidget(QGLFormat::defaultFormat(),this);

    QGLWidget * glWidget1=new QGLWidget(QGLFormat::defaultFormat(),this,glWidget);
    @
    But the context is not shared , only display list and texture objects are shared. Right?
    Any workable solution?
    Thanks!



  • Hi

    [quote author="zhaopeng" date="1356013406"]

    Some suggest that use the second widget as shared widget of first one.
    [/quote]

    This solution is working. It's the one I always use.

    [quote author="zhaopeng" date="1356013406"]

    But the context is not shared , only display list and texture objects are shared.[/quote]

    What do you mean? Display lists and texture object are the only things "share-able" with OpenGL contexts. There is nothing more.



  • Thanks for your reply!

    [quote author="tilsitt" date="1356013956"]
    What do you mean? Display lists and texture object are the only things "share-able" with OpenGL contexts. There is nothing more.[/quote]

    I know the FBO can not be shared. How about VBO, VAO, UBO, Transform feedback buffer and so on? There are many kinds of resources of OpenGL. What I worry about is that some of them can not be shared and I have to make a big change of my program.



  • From "OpenGL 4.3 Core Profile specification":http://www.opengl.org/registry/doc/glspec43.core.20120806.pdf:

    bq. Objects that may be shared between contexts include buffer objects, program
    and shader objects, renderbuffer objects, sampler objects, sync objects, and texture
    objects (except for the texture objects named zero).
    Objects which contain references to other objects include framebuffer, program
    pipeline, query, transform feedback, and vertex array objects. Such objects are
    called container objects and are not shared.

    (chaper 5 page 47 about context sharing)

    So "true" objects like textures or buffer objects are shared. Containers that reference other objects (e.g. a FBO references a texture as color buffer) are not shared.



  • You mean the sharing of QGLContext follows the latest OpenGL specification, right? Now it's clear and I can rest to use it. Thanks a lot!



  • [quote author="zhaopeng" date="1356080913"]You mean the sharing of QGLContext follows the latest OpenGL specification, right?[/quote]

    Yes, that's what I mean.

    [quote author="zhaopeng" date="1356080913"]Thanks a lot![/quote]

    You're welcome ;)



  • Sorry, I got another question. Can the sharing relationship be propagated and commutated? For example
    @ QGLWidget * glWidget=new QGLWidget(QGLFormat::defaultFormat(),this);
    QGLWidget * glWidget1=new QGLWidget(QGLFormat::defaultFormat(),this,glWidget);
    QGLWidget * glWidget2=new QGLWidget(QGLFormat::defaultFormat(),this,glWidget1);@

    glWidget is the share widget of glWidget1. According to the Qt doc glWidget1 will share its OpenGL resources with glWidget, which can be accessed in glWidget. How about glWidget's OpenGL resources? Can they be accessed in glWidget1?
    Then glWidget1 is passed as share widget to glWidget2, so how about the relationship between glWidget and glWidget2.

    Thanks!



  • When a gl widget has another one as sharing gl widget, both of them are pointing to a unique set of share-able resources (it's like sharing smart pointers): glWidget, glWidget1 and glWidget2 all work on a unique set of resources. So, yes glWidget can "access" glWidget1 and glWidget2, glWidget1 can "access" glWidget and glWidget2 and glWidget2 can "access" glWidget and glWidget1. For what I can say, there is no limitation in the number of gl widget sharing the same resources.



  • [quote author="tilsitt" date="1356098680"]When a gl widget has another one as sharing gl widget, both of them are pointing to a unique set of share-able resources (it's like sharing smart pointers): glWidget, glWidget1 and glWidget2 all work on a unique set of resources. So, yes glWidget can "access" glWidget1 and glWidget2, glWidget1 can "access" glWidget and glWidget2 and glWidget2 can "access" glWidget and glWidget1. For what I can say, there is no limitation in the number of gl widget sharing the same resources.[/quote]

    Great! Thank you for being such a big help! :)



  • I have a problem in sharing a GLContext between two GLWidgets,
    I have Dual monitor system which has got NVidia Graphics Card, Monitors are configured as Screen 0 & Screen 1 ( Separate X Screens). and i am trying to create one GLwidget on Screen 0 and the second GLWidget on Screen 1, if Both GLWidgets are at same screen i am able to share the context but if i create a second GLWidget on Screen 1 context is not sharing. Advance Thanks for any help.



  • Are your two monitors connected to the same Nvidia card? I think the context can not be shared if each monitor has its own graphics card unless using SLI.



  • Hi zhaopeng!
    The two monitors are connected to same Nvidia Card and the both are configured as two screens.



  • Hi,
    It may be a big problem for me because I also have a plan to use dual monitor. I will give a try when another monitor is available and report the result.



  • Thank you very much for your time!!



  • @tilsitt you sure it can run right?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.