Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

WGL Window on non-GUI thread



  • Hi guys,

    I started out trying to create an OpenGL render window on a non-GUI thread using QT's OpenGL resources as explained in this thread:

    https://forum.qt.io/topic/123151/qwindow-on-a-non-gui-thread

    My reason for attempting to do this is because my GUI thread is pretty busy with a lot of events and widgets and this causes stuttering and dropped frames when I use a QWindow.

    At the moment, I've gotten as far as creating a native WGL context / window which I used to create a QOpenGLContext:
    Here is a github link to the project: https://github.com/rtavakko/WinGL

    void OpenGLNativeRenderWindow::showNative()
    {
        //Create native window and context
        createNative();
    
        //Allocate memory for the context object and prepare to create it
        openGLContext = new QOpenGLContext(this);
    
        QWGLNativeContext nativeContext(hglrc,hwnd);
        openGLContext->setNativeHandle(QVariant::fromValue(nativeContext));
    
        openGLContext->setFormat(openGLFormat);
        if(sharedOpenGLContext)
            openGLContext->setShareContext(sharedOpenGLContext);
    
        //Make sure the context is created & is sharing resources with the shared context
        bool contextCreated = openGLContext->create();
        assert(contextCreated);
    
        if(sharedOpenGLContext)
        {
            bool sharing = QOpenGLContext::areSharing(openGLContext,sharedOpenGLContext);
            assert(sharing);
        }
    
        //Resize / show window
        resize(renderSpecs.frameType.width,renderSpecs.frameType.height);
        visible = !ShowWindow(hwnd,SW_SHOWNORMAL);
    }
    

    All the resources, including the native window, are created without issues and I'm able to make the QT context current on a separate thread and OpenGL rendering also produces no errors but Hello Triangle doesn't render on my window. It seems that the native WGL context and the QT context are still separate entities.

    If I make direct WGL calls to make the context current / swap buffers, I seem to be able to make OpenGL calls on the native window but it still seems I can't access the resources the QT context renders (e.g. FBO output texture).

    Any thoughts on what could be wrong?

    Cheers!