Results 1 to 3 of 3

Thread: OpenGL / OpenCL interop with shared contexes and multithread

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2

    OpenGL / OpenCL interop with shared contexes and multithread

    Hi,
    I am working on a project using OpenCL / OpenGL interoperability and multi-threading. Thread1 is used just for rendering of VBO and Thread2 is used for running OpenCL kernel which process geometry stored in VBO. The kernel is called several times and I want to visualize processed mesh after each iteration. Therefore I need two things - to share openGL contexes in Thread1 and Thread2 to share the VBO and to share OpenCL / OpenGL context. The first can be achieved using wglShareLists(HLRC2, HLRC2). The second step is to create OpenCL context using sharing OpenGL context. For this I have to use the context from Thread2 - processing thread.

    As far as I understand it, the order of the commands should be as follows:

    // create contexes

    Code :
    hlrc1 = wglCreateContext(m_hdc);
    hlrc2 = wglCreateContext(m_hdc);

    // share resources while they are not set as current for each thread

    Code :
    wglShareLists(hlrc1, hlrc2);

    // make hlrc1 current in thread1 and hlrc2 in thread2

    Code :
    wglMakeCurrent(m_hdc, hlrc1) / wglMakeCurrent(m_hdc, hlrc2)

    // and now set shared context for openCL

    Code :
    cl_context_properties properties[] = {
    CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(), // WGL   Context
    CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(), // WGL HDC
    CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, // OpenCL platform
    0   };
     
    cl_device_id devices[32]; size_t sizedev;
    clGetGLContextInfoKHR_fn clGetGLContextInfo = (clGetGLContextInfoKHR_fn)clGetExtensionFunctionAddressForPlatform(cpPlatform, "clGetGLContextInfoKHR");
     
    clGetGLContextInfo(properties, CL_DEVICES_FOR_GL_CONTEXT_KHR, 32 * sizeof(cl_device_id), devices, &sizedev);
     
    cl_uint countdev = (cl_uint)(sizedev / sizeof(cl_device_id));
    context = clCreateContext(properties, countdev, devices, NULL, 0, 0);

    // and then the shared interop memory object is created and passed as kernel argument in openCL

    Code :
    cl_mem vbo_cl = clCreateFromGLBuffer(context, CL_MEM_READ_WRITE, vboID, NULL);

    And here come the troubles. If the command wglShareLists(hlrc1, hlrc2) is called, shared VBO has only zeroes instead of vertex positions. If the command wglShareLists(hlrc1, hlrc2) is skipped, VBO has valid values, everything works fine between OpenGL / OpenCL interop, but I cant render the process, because the resorces between OpenGL contexes in Thread1 and Thread2 can't be shared.

    Has anyone tried something like this, is it possible? Or am I doing something in a wrong way? Thanks for any suggestions.

  2. #2
    Senior Member
    Join Date
    Oct 2012
    Posts
    104

    Re: OpenGL / OpenCL interop with shared contexes and multith

    If you fill your VBO in the first thread, you first have to call glFinish() to ensure that the data are correctly sent before using them in the second thread.

    Furthermore, you have to use clEnqueueAcquireGLObjects() on the OpenCL buffers in the second thread before being able to use them in any kernel.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    2

    Re: OpenGL / OpenCL interop with shared contexes and multith

    Yes, I am using both of these :

    Code :
    glFinish();
    clEnqueueAcquireGLObjects(commands, 1, &vbo_cl, 0,0,0);
    clEnqueueNDRangeKernel(commands, kernel, 1, NULL, &global, &local, 0, NULL, NULL);
    clEnqueueReleaseGLObjects(commands, 1, &vbo_cl, 0,0,0);

Similar Threads

  1. Replies: 13
    Last Post: 10-15-2012, 11:06 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
  •