Obtaining OpenGL Buffers Using OpenCL

Is it possible to get the values โ€‹โ€‹of OpenGL buffers using OpenCL? For example, write a program that creates an OpenGL context with buffers, and another that executes OpenCL code? I don't want the first code of the program to change.

+3


source to share


1 answer


There is an additional OpenCL extension cl_khr_gl_sharing

, which allows an implementation to share certain OpenGL objects (buffers and textures) with OpenCL objects (buffers and images). This extension is supported by most of the available OpenCL graphics solutions. Additional API functions defined by the extension are available in the header file CL/cl_gl.h

.

You can check if your device and platform supports this extension by calling clGetDeviceInfo

with an argument CL_DEVICE_EXTENSIONS

and checking the resulting string to see if it is present cl_khr_gl_sharing

.

During OpenCL initialization, you need to create an OpenCL context in a specific way to allow objects to be shared with OpenGL. This is OS dependent, but in all cases involves creating a set of context properties that you pass to clCreateContext

. Here are some examples:


OS X

CGLContextObj     CGLGetCurrentContext(void);
CGLShareGroupObj  CGLGetShareGroup(CGLContextObj);

CGLContextObj     kCGLContext     = CGLGetCurrentContext();
CGLShareGroupObj  kCGLShareGroup  = CGLGetShareGroup(kCGLContext);

cl_context_properties properties[] =
{
  CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
  (cl_context_properties) kCGLShareGroup,
  0
};

      


Linux (with GLX)

cl_context_properties properties[] =
{
  CL_GL_CONTEXT_KHR,   (cl_context_properties)glXGetCurrentContext(),
  CL_GLX_DISPLAY_KHR,  (cl_context_properties)glXGetCurrentDisplay(),
  CL_CONTEXT_PLATFORM, (cl_context_properties)platform, // OpenCL platform object
  0
};

      


Windows (WGL)

cl_context_properties properties[] =
{
  CL_GL_CONTEXT_KHR,   (cl_context_properties)wglGetCurrentContext(),
  CL_WGL_HDC_KHR,      (cl_context_properties)wglGetCurrentDC(),
  CL_CONTEXT_PLATFORM, (cl_context_properties)platform, // OpenCL platform object
  0
};

      


Android (with EGL)

cl_context_properties properties[] =
{
  CL_GL_CONTEXT_KHR,   (cl_context_properties)eglGetCurrentContext(),
  CL_EGL_DISPLAY_KHR,  (cl_context_properties)eglGetCurrentDisplay(),
  CL_CONTEXT_PLATFORM, (cl_context_properties)platform, // OpenCL platform object
  0
};

      




Once the properties have been configured, create a context like this:

context = clCreateContext(properties, 1, &device, NULL, NULL, &err);

      


The next thing to do is create your OpenCL objects from existing OpenGL objects. Nothing about OpenGL should change. Given an existing named OpenGL buffer bufferGL

, you can create an OpenCL buffer object from it as follows:

bufferCL = clCreateFromGLBuffer(context, CL_MEM_READ_WRITE, bufferGL, &err);

      

Or you can create an OpenCL image from an OpenGL texture:

imageCL = clCreateFromGLTexture2D(context, CL_MEM_WRITE_ONLY,
                                  GL_TEXTURE_2D, 0, textureGL, &err);

      


All that's left is to queue up your OpenCL kernels that work with these shared CL / GL objects. You need to insert a few extra commands on each side of your kernel to tell the implementation that you want to transfer ownership of objects to / from OpenCL. The general sequence of operations to be performed is as follows:

// Flush GL queue                                
glFlush();

// Acquire shared objects
err = clEnqueueAcquireGLObjects(queue, 1, &bufferCL, 0, NULL, NULL);

// Enqueue OpenCL commands to operate on objects (kernels, read/write commands, etc)

// Release shared objects                                                          
err = clEnqueueReleaseGLObjects(queue, 1, &bufferCL, 0, NULL, NULL);
checkError(err, "releasing GL objects");

// Flush CL queue                                                           
err = clFinish(queue);

      


There are some code examples out there that demonstrate OpenCL / OpenGL compatibility, which would be useful to look at in a full application:

+8


source







All Articles