Results 1 to 5 of 5

Thread: qt and gl/cl interop

  1. #1

    qt and gl/cl interop

    h?llo.
    I'm trying to use an interop with opengl and opencl in qt.
    this is my code:
    Code :
    	GLuint id = 0;
    	glGenBuffers(1, &id);
    	glBindBuffer(GL_ARRAY_BUFFER, id);
    	glBufferData(GL_ARRAY_BUFFER, lSize, NULL, GL_STATIC_DRAW);
     
    	int err;
    	cl::BufferGL buff;
    	buff= cl::BufferGL(context, CL_MEM_WRITE_ONLY, id, &err);
     
    	glBindBuffer(GL_ARRAY_BUFFER, 0);
    	vertexBufferObject = id;
    I get always the same error:
    -60: CL_INVALID_GL_OBJECT.
    i omitted the construction of the gl/cl context code, but I'm was try this without qt and all works fine.
    When i use the code in qt i get the error.
    What can be?
    the threads and contexts that are used in qt?
    How i can resolve?
    But I wish understand and use my own library, rather than use an already done library .
    thanks.

  2. #2
    Senior Member
    Join Date
    Dec 2011
    Posts
    152

    Re: qt and gl/cl interop

    Just guessing here, perhaps the Qt OpenGL context is different than the OpenGL context you are creating in your non-Qt example? Do you get any error when you create the OpenCL context from (or using) the OpenGL (in order to allow sharing)?

  3. #3

    Re: qt and gl/cl interop

    no,
    thanks

  4. #4

    Re: qt and gl/cl interop

    this is the create procedure:
    Code :
    bool QCLContextGL::create(const QCLPlatform &platform)
    {
        Q_D(QCLContextGL);
     
        // Bail out if the context already exists.
        if (isCreated())
            return true;
     
        // Bail out if we don't have an OpenGL context.
        if (!QGLContext::currentContext()) {
            qWarning() << "QCLContextGL::create: needs a current GL context";
            setLastError(CL_INVALID_CONTEXT);
            return false;
        }
     
        // Find the first gpu device.
        QList<QCLDevice> devices;
        cl_device_type deviceType = CL_DEVICE_TYPE_GPU;
        devices = QCLDevice::devices(QCLDevice::GPU, platform);
        if (devices.isEmpty()) {
            // Find the first default device.
            devices = QCLDevice::devices(QCLDevice::Default, platform);
            deviceType = CL_DEVICE_TYPE_DEFAULT;
        }
        if (devices.isEmpty()) {
            qWarning() << "QCLContextGL::create: no gpu devices found";
            setLastError(CL_DEVICE_NOT_FOUND);
            return false;
        }
        QCLDevice gpu = devices[0];
        QVarLengthArray<cl_device_id> devs;
        foreach (QCLDevice dev, devices)
            devs.append(dev.deviceId());
     
        // Add the platform identifier to the properties.
        QVarLengthArray<cl_context_properties> properties;
        properties.append(CL_CONTEXT_PLATFORM);
        properties.append(cl_context_properties(gpu.platform().platformId()));
     
        bool hasSharing = false;
    #ifndef QT_NO_CL_OPENGL
        // Determine what kind of OpenCL-OpenGL sharing we have and enable it.
    #if defined(__APPLE__) || defined(__MACOSX)
        bool appleSharing = gpu.hasExtension("cl_apple_gl_sharing");
        if (appleSharing) {
            CGLContextObj cglContext = CGLGetCurrentContext();
            CGLShareGroupObj cglShareGroup = CGLGetShareGroup(cglContext);
            properties.append(CL_CGL_SHAREGROUP_KHR);
            properties.append(cl_context_properties(cglShareGroup));
            hasSharing = true;
        }
    #else
        bool khrSharing = gpu.hasExtension("cl_khr_gl_sharing");
    #if defined(QT_OPENGL_ES_2) || defined(QT_OPENGL_ES)
        if (khrSharing) {
            properties.append(CL_EGL_DISPLAY_KHR);
            properties.append(cl_context_properties(eglGetCurrentDisplay()));
    #ifdef EGL_OPENGL_ES_API
            eglBindAPI(EGL_OPENGL_ES_API);
    #endif
            properties.append(CL_GL_CONTEXT_KHR);
            properties.append(cl_context_properties(eglGetCurrentContext()));
            hasSharing = true;
        }
    #elif defined(Q_WS_X11)
        if (khrSharing) {
            properties.append(CL_GLX_DISPLAY_KHR);
            properties.append(cl_context_properties(glXGetCurrentDisplay()));
            properties.append(CL_GL_CONTEXT_KHR);
            properties.append(cl_context_properties(glXGetCurrentContext()));
            hasSharing = true;
        }
    #else
        // Needs to be ported to other platforms.
        if (khrSharing)
            qWarning() << "QCLContextGL::create: do not know how to enable sharing";
    #endif
    #endif
    #endif // !QT_NO_CL_OPENGL
        properties.append(0);
     
    #ifndef QT_NO_CL_OPENGL
        // Query the actual OpenCL devices we should use with the OpenGL context.
        typedef cl_int (*q_PFNCLGETGLCONTEXTINFOKHR)
            (const cl_context_properties *, cl_uint, size_t, void *, size_t *);
        q_PFNCLGETGLCONTEXTINFOKHR getGLContextInfo =
            (q_PFNCLGETGLCONTEXTINFOKHR)clGetExtensionFunctionAddress
                ("clGetGLContextInfoKHR");
        if (getGLContextInfo && hasSharing) {
            size_t size;
            cl_device_id currentDev;
            if(getGLContextInfo(properties.data(),
                                CL_DEVICES_FOR_GL_CONTEXT_KHR,
                                0, 0, &size) == CL_SUCCESS && size > 0) {
                QVarLengthArray<cl_device_id> buf(size / sizeof(cl_device_id));
                getGLContextInfo(properties.data(),
                                 CL_DEVICES_FOR_GL_CONTEXT_KHR,
                                 size, buf.data(), 0);
                devs = buf;
                gpu = QCLDevice(devs[0]);
            }
            if (getGLContextInfo(properties.data(),
                                 CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,
                                 sizeof(currentDev), &currentDev, 0)
                    == CL_SUCCESS) {
                gpu = QCLDevice(currentDev);
            }
        }
    #endif
     
        // Create the OpenCL context.
        cl_context id;
        cl_int error;
        id = clCreateContext
            (properties.data(), devs.size(), devs.data(),
             qt_clgl_context_notify, 0, &error);
        if (!id && hasSharing) {
            // Try again without the sharing parameters.
            properties.resize(2);
            properties.append(0);
            hasSharing = false;
            id = clCreateContext
                (properties.data(), devs.size(), devs.data(),
                 qt_clgl_context_notify, 0, &error);
        }
        setLastError(error);
        if (id == 0) {
            qWarning() << "QCLContextGL::create:" << errorName(error);
            d->supportsSharing = false;
        } else {
            setContextId(id);
            clReleaseContext(id);   // setContextId() adds an extra reference.
            setDefaultDevice(gpu);
            d->supportsSharing = hasSharing;
        }
        return id != 0;
    }
    i see better and i have this warning:
    if (khrSharing)//is true
    qWarning() << "QCLContextGL::create: do not know how to enable sharing";
    and hasSharing is alwais false;
    thanks.

  5. #5
    Senior Member
    Join Date
    Dec 2011
    Posts
    152

    Re: qt and gl/cl interop

    I'm guessing you are on Windows. That code appears to support sharing under Mac OS X and X11 but not Windows. You'll need to add GL/CL sharing code appropriate for Windows. There are examples around, and it sounds like you have it working already in your non-Qt code (so copy from that).

Similar Threads

  1. gl-cl interop and depth buffer
    By whites11 in forum OpenCL
    Replies: 0
    Last Post: 05-11-2010, 12:01 PM
  2. OpenCL + OpenGL (interop)
    By Executor in forum OpenCL
    Replies: 2
    Last Post: 11-09-2009, 05:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •