OpenGL / OpenCL interacts with shared content and multithreading

I am working on a project using OpenCL / OpenGL compatibility and multithreading. Thread1 is used only for VBO rendering, while Thread2 is used to run the OpenCL core which handles geometry stored in VBO. The kernel is called multiple times and I want to render the rendered mesh after each iteration. So I need two things - share openGL content in Thread1 and Thread2 to share VBO and share OpenCL / OpenGL context. The first can be achieved with wglShareLists (HLRC2, HLRC2). The second step is to create an OpenCL context using the general OpenGL context. To do this, I have to use the Thread2 context - the processing thread.

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

// create context

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

      

// exchange resources until they are set as current for each thread

wglShareLists(hlrc1, hlrc2);

      

// make hlrc1 current on thread1 and hlrc2 on thread2

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

      

// and now set the general 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 a shared memory object interop is created and passed as a kernel argument to openCL

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

      

And then trouble comes. If the wglShareLists (hlrc1, hlrc2) command is called, the shared VBO only has zeros instead of vertex positions. If the wglShareLists (hlrc1, hlrc2) command is omitted, VBO has valid values, everything works fine between OpenGL / OpenCL interop, but I cannot display this process because resorptions between OpenGL content in Thread1 and Thread2 cannot be shared.

Has anyone tried something like this, is it possible? Or am I doing something wrong?

+3


source to share





All Articles