Sharing OpenGL object with OpenCL and use CPU device ?

Is it possible to share OpenGL objects with OpenCL and use OpenCL CPU device if the GPU is not supported by OpenCL ?

I don’t think so, mainly because if I create a OpenCL context that shares the CPU isn’t given as a compute device. That’s on a Snow Leopard Mac with a NV8600.

I assume you’d be unable to create a shared OpenCL context on a machine that had an unsupported GPU, but I’ve yet to try it as I haven’t updated my older ATI based machine yet to try anything.

This will depend entirely on the system you’re using as the CL/GL interop is up to the vendor.

On SnowLeopard I believe you can do this if you create your context in a special manner. (You need to specify the Apple CL/GL sharing extension property and then pass in the CPU device explicitly, or something, but I don’t remember the details.) Take a look at Apple’s documentation and see if they have an example. It might even be documented in the header file.

I have found the solution under Snow Leopard

CGLContextObj kCGLContext = [openglContext CGLContextObj];
CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);

// Create a context from a CGL share group
cl_context_properties properties[] = { CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup, 0 };

context = clCreateContext(properties, 0, 0, 0, 0, 0);
if (!context)
NSLog(@"Error: Failed to create a compute context!
return NO;

size_t returned_size;
unsigned int device_count;
cl_device_id device_ids[16];

err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(device_ids), device_ids, &returned_size);
if (err)
NSLog(@"Error: Failed to retrieve compute devices for context!
return NO;

One other information.
It seem’s that when you try to share an OpenGL context with an OpenCL context, only GPU are returned.

I don’t have found any solution to use CPU device in this case.

Try getting the CPU device and passing it in when you call clCreateContext with the properties. I believe this will add the CPU device to your context along with the appropriate GPU device, but it’s been a while since I tried this.