CL GL Context creation

Hi forum,

Is it mandatory to use

clGetGLContextInfoKHR_fn func = (clGetGLContextInfoKHR_fn)clGetExtensionFunctionAddress("clGetGLContextInfoKHR");

        //Find the CL capable devices int the current GL context
        errNum = func(contextProperties,CL_DEVICES_FOR_GL_CONTEXT_KHR,0,NULL,&deviceSize);

while creating the OpenCL context with GL-CL interoperability ?

I have seen many examples where they are creating without referring to the above function .

I tried to use the above and i am getting undefined behavior with GTX 560M on Linux system

Any thoughts ?


NVIDIA generally returns an empty list for CL_DEVICES_FOR_GL_CONTEXT_KHR, so this should be avoided.

There are (at least) three ways to create a shared context. Whichever method you use, the property list should contain CL_CONTEXT_PLATFORM and the properties for the OpenGL binding (CL_GL_CONTEXT_KHR and CL_WGL_HDC_KHR on Windows, CL_GLX_DISPLAY_KHR on Linux and so on).

  1. The easiest way is to call clGetGLContextInfoKHR with CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR. This returns a good device which you can use to create a shared context. However it returns only one device, so you can’t use this method to create a context on SLI GPUs.

  2. Use clCreateContextFromType(prop, CL_DEVICE_TYPE_GPU, …) to create a context on all the GPU devices of the platform. Then use clGetContextInfo to find which devices are used.

  3. Slow but flexible method: call clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, …) to get the list of devices of the platform. Filter this list with clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, …) to keep only devices with support of the cl_khr_gl_sharing extension, then create the context with clCreateContext.