how to use clGetGLContextInfoKHR correctly?

Hello.

First of all there is a bug in documentation - https://www.khronos.org/registry/OpenCL/sdk/1.1/docs/man/xhtml/clGetGLContextInfoKHR.html

Back to my question.

I’m trying to get opencl device which corresponds to current opengl context.

I have following code:


        for(auto& platform : platforms)
        {
            clGetGLContextInfoKHR_fn pclGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn)
                clGetExtensionFunctionAddressForPlatform(platform(), "clGetGLContextInfoKHR");

            if (!pclGetGLContextInfoKHR)
                continue;

            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(),
                0
            };

            cl_device_id device_id;
            size_t value_size_ret;
            if (CL_SUCCESS != pclGetGLContextInfoKHR(properties, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR
                        , sizeof(cl_device_id), &device_id, &value_size_ret))
                continue;

            if (0 == value_size_ret)
                continue;

            return std::unique_ptr<cl::Device>(new cl::Device(device_id));
        }
        return nullptr;

I have Intel and nvidia platforms. Current OpenGL context is nvidia but returned device is Intel. Is it a bug of drivers or am I doing it incorrectly?

P.S. Sometime ago it worked ok. The problem appeared when I install latest nvidia drivers.

Hi OmegaDoom,
you must choice the device that is associated for GL context from full device list.
Try use the sample code, but there has no lists of devices, we take the first available device.

	for (size_t i = 0; i < ret_num_platforms; ++i) {
		platform_id = platform_ids[i];
		cl_device_id curDevice_id = 0;
		ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &curDevice_id, &ret_num_devices);
		if (curDevice_id) {
			clGetGLContextInfoKHR_fn clGetGLContextInfo = reinterpret_cast<clGetGLContextInfoKHR_fn>(clGetExtensionFunctionAddressForPlatform(platform_id, "clGetGLContextInfoKHR"));
			if (clGetGLContextInfo) {
				cl_device_id clGLDevice = 0;
				props[5] = reinterpret_cast<cl_context_properties>(platform_id);
				clGetGLContextInfo(props, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR, sizeof(cl_uint), &clGLDevice, &n);
				if (clGLDevice == curDevice_id) {
					device_id = clGLDevice;
				}
			}
		}
		if (device_id) {
			break;
		}
	}

the same problem are described:
https://www.gamedev.net/forums/topic/699479-clcreatefromglbuffer-crash/
https://forums.khronos.org/showthread.php/13908-clCreateFromGLBuffer-crash

Hello Andrey. Thank you for reply.

I have tried you example(testOpenCL.7z.). it doesn’t work too. OpenGL context is NVidia but OpenCL is Intel which results to segfault when clCreateFromGLBuffer is called. Exact problem like in my example.
And i think your example is not 100% correct. According to documentation we need to check returned size of clGetGLContextInfo but I think your way should work too.

Hi OmegaDoom, On my system i have 2 GPU, nVidia and AMD,
Only nVidia has OpenGL Context. In tis case clGetGLContextInfo returns nVidia device_id.

Hi Andrey.
I just want to be sure if my approach is correct(it should be correct). Sometime ago this code worked ok. I started experience this problem after drivers update.
Lately I have a lot of problems related to drivers. For example latest intel driver crashes opengl applications on my system. Previous one crashes qt. I installed old Intel one, latest Nvidia and now I have this problem. :frowning:

Hi OmegaDoo, I think you have a local problem on driver side on your system. I had the same problem. Try to reinstall all drivers.

Thank you for your help.
Yes, it was a problem of my environment.