3

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
mado
  • 31
  • 2
  • Do you have some kind of thread synchronization? Because OpenGL API calls aren't thread safe. To me the context sharing seems correct. I also shared textures across multiple threads. I just hope you are not doing this because of performance reasons, because sharing OpenGL threads is actually a bad thing. – Tara Feb 07 '13 at 12:57
  • There is no special synchronization, only a flag which says that the thread should rerender screen with new - modified VBO. The only reason why I am doing this is because I want to see rendered the modified VBO as the processing thread is changing vertex positions. – mado Feb 11 '13 at 14:54
  • Are you sure that never two OpenGL API calls are being done at the same time from two different threads? Also, are you releasing the buffer from OpenCL, so OpenGL can use it again? – Tara Feb 16 '13 at 22:13
  • @mado: don't you see any error returned by `clCreateFromGLBuffer` ? – Fabien R Jun 01 '13 at 11:47

0 Answers0