Provide OpenGl context to a OpenCL context

  • Hi!
    Im starting make some improvements using hardware optimizations and one of them involves share the OpenGl buffers with my OpenCL buffers.
    Everything is running fine, but to enable this specific feature i need inform to the OpenCL context what OpenGL context i am using.
    But the OpenCL context are prepeared to receive wgl contexts (hdc) and i dont know how can i provide my QOpenGL context and make OpenCL understand it.
    Anyone have an idea how i can enable GLCL sharing using Qt?

    Kind Regards.

  • @wwolff
    I find out there is a nativeHandle function on the QOpenGLContext object, but it returns a QVariant object.
    I will need typecast this return with the native type context?
    Any example of QVariant typecast?

    Kind Regards.

  • Lifetime Qt Champion


    Are you looking for qvariant_cast ?

  • @SGaist
    No, my doubt is becouse the OpenCL function request a native Handle of the window AND the native handle of the context...

  • Lifetime Qt Champion

    There was a module called QtOpenCL but it was for Qt 4 with this repository being a WIP to make it work with Qt 5.

    In any case, it could be an interesting starting point to help you use OpenCL with Qt

  • Hi!
    Yes i know there is a QtOpeCL Module , but what i want is get the native structures for my Rendering OpenGL Window and provide to my own OpenCL classes.
    Everything is running , including the OpenCL Kernels and etc...
    But i´m trying use the OpenGL->OpenCL interoperation to optimize some graphics algorithms and using Visual Studio are working fine too.

    Basically in windows i need 3 structures to enable this interoperation:

    HWND hWnd, 
    HDC     &hDC, 
    HGLRC &hRCe

    The First One is the structure create when we call the CreateWindow function to create a window.
    The secondOne is the structure where we store the Device Context retrieved by the GetDC(gHwnd); function.
    And Finally the Last store the OpenGL Context provided by the hRC = wglCreateContext(hDC); function.

    During This days i find out a private library in Qt called QPlatformNativeInterface , but i not found any documentation about it , but it seems i can get this informations from it.

    Any information of how i can get a HWND structure from a QOpenGLWindow and a QGLWidget will be much Appreciated, since from a HWND i can get the DC context and from the OpenGL context i can get the HGLRC.

    Otherwise, i find out too a object in the documentation called QWGLNativeContext defined in the Qt Platform Headers , and it seems this object can be typecasted in the nativeHandle method of the QOpenGLContext class , providing this two structures...

    I will do some more tests , but if this last object works i dont need use the private library to access it.

    Kind Regards.

  • Hi!

    I find in the documentation the following code:

    QVariant nativeHandle = context->nativeHandle();
    if (!nativeHandle.isNull() && nativeHandle.canConvert<QWGLNativeContext>()) {
        QWGLNativeContext nativeContext = nativeHandle.value<QWGLNativeContext>();
        HGLRC hglrc = nativeContext.context();

    If everything goes ok, this is exactly what i was looking for!

    Kind Regards.

  • Ok, i Almost there...

    Now i´m having problems with a function called GetDC(HWND) of windows.

    Basically this function get the device context needed...

    Everythig compiles , but when Qt compile the main module i receive a Link error becouse it not find the definition of this windows function.

    Any tip to solved it?

    Kind Regards.

  • Lifetime Qt Champion

    That's a function from the User32 library. You need to link against it.

Log in to reply

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