retrieve opencl platform that match the opengl one


#1

Hello !

I have two gpu on my computer, one from my cpu and another one from my graphic card.

I am trying to use the opengl/opencl interop capabilities. But I am stuck at the creation of the opencl context, I don’t know how to identify which platform / device of the two one is used by opengl.

In the above code, which fonction should I use in the test “DEVICE MATCHING OPENGL ONE” to test if the device is the one used by OpenGL, or what should I do to test if the platform_id is the good one ?

sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 2;
sf::Window window(sf::VideoMode(2048, 1024), "GAME",
		sf::Style::Fullscreen, settings);
 
glewInit();
 
cl_platform_id platform_ids[16] = { NULL };
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
 
cl_platform_id platform_id = 0;
cl_int ret = clGetPlatformIDs(_countof(platform_ids), platform_ids,
			&ret_num_platforms);
 
size_t n = 0;
 
cl_context_properties props[] = { CL_GL_CONTEXT_KHR,
	(cl_context_properties) wglGetCurrentContext(), CL_WGL_HDC_KHR,
	(cl_context_properties) wglGetCurrentDC(), CL_CONTEXT_PLATFORM,
	(cl_context_properties) platform_id, 0 };
 
for (size_t i = 0; i < ret_num_platforms; ++i) {
	platform_id = platform_ids[i];
	cl_device_id curDevices_id[16];
 
    	ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU,
				_countof(curDevices_id), curDevices_id, &ret_num_devices);
 
	for (cl_uint nDevices = 0; nDevices < ret_num_devices; ++nDevices) {
		cl_device_id curDevice_id = curDevices_id[nDevices];
 
		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(clGLDevice), &clGLDevice, &n);
 
			if (DEVICE MATCHING OPENGL ONE) {
				device_id = clGLDevice;
			}
		}
	}
	if (device_id) {
		break;
	}
}
 
cl_context context = clCreateContext(props, 1, &device_id, NULL, NULL,
		&ret);

Thanks for your future help!


#2

I think your code is almost right.
I do the work this way:
1)Find all opencl device_ids.
2)For every platform looking for the OpenGL device, only one platform will return a none-zero gl_id.
3)compare gl_id with all device_ids, it is one of them.


#3

2)For every platform looking for the OpenGL device, only one platform will return a none-zero gl_id.
– Both of the two platforms return a different none-zero gl_id


#4

You can try to create a CL/GL shared context and if it works they are the same device. But if they are not we have seen crashes. So we at least don’t try across non-matching vendors. The way we do this: Get GL device name, look for certain strings to determine if AMD, NVIDIA, Intel. Do same for CL device. Only try CL/GL interop when same vendor.


#5

I debuged my program again, the CL/GL shared context returned a zero device at the Intel platform.
I think the Intel platform has an integrated graphics card, but in my case, It doesn’t return for the CL/GL shared context.
You told me a different case, I never think that will happen.