AnsweredAssumed Answered

OpenGL / OpenCL interop with shared contexes and multithreading

Question asked by mado on Feb 6, 2013
Latest reply on Jul 3, 2013 by himanshu.gautam

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

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

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

wglShareLists(hlrc1, hlrc2); 

// make hlrc1 current in thread1 and hlrc2 in thread2

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

// and now set shared context for openCL

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

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?

Outcomes