clCreateFromGLBuffer crash

Hello every one,

When I tried to use my vbo with opencl I get a crash when calling clCreateFromGLBuffer

Below a small code that reproduce the issue:


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_id = NULL;
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);

ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id,
        &ret_num_devices);

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 };

cl_context context = clCreateContext(props, 1, &device_id, NULL, NULL,
        &ret);

GLuint vboID_m = 0;
Creation of the vertex buffer object

glGenBuffers(1, &vboID_m);
glBindBuffer(GL_ARRAY_BUFFER, vboID_m);
{

    std::vector<float> tmp = { 0., 0., 0., 0., 0., 0., 0., 0., 0. };

    glBufferData(GL_ARRAY_BUFFER, (tmp.size()) * sizeof(float), 0,
    GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, tmp.size() * sizeof(float),
            tmp.data());
}
glBindBuffer(GL_ARRAY_BUFFER, 0);

glFlush();

cl_int status;
clCreateFromGLBuffer(context, CL_MEM_READ_WRITE, vboID_m, &status);
return 0;

Thanks for your help!

And Again:
Hi, sleeper

  1. wglGetCurrentContext() != NULL ? wglGetCurrentDC() != NULL ?

  2. Try to create OpenGL Context without SMFL(VERY BAD library) or try to use SDL :)))

  3. Can you attach your test project ? I can test him.

Thanks for your reply,

  1. wglGetCurrentContext() != NULL ? wglGetCurrentDC() != NULL ?

None of them are null.

  1. Try to create OpenGL Context without SMFL(VERY BAD library) or try to use SDL :)))

Will try.

  1. Can you attach your test project ? I can test him.

http://garcin.alex.free.fr/partage/test.rar

HI sleeper,

I have tested your project. On my system i have a few platforms and few GPU witch support OpenCL, so i added choice platform for GPU using clGetGLContextInfoKHR, I replace SFML to glut. I have no crash.

glutInit(&argc, argv);
	glutInitWindowSize(800, 600);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
	glutCreateWindow("Test OpenCL");


	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 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;
		}
	}

	cl_context context = clCreateContext(props, 1, &device_id, NULL, NULL,
			&ret);

	GLuint vboID_m = 0;

	glGenBuffers(1, &vboID_m);
	glBindBuffer(GL_ARRAY_BUFFER, vboID_m);

	{

		std::vector<float> tmp = { 0., 0., 0., 0., 0., 0., 0., 0., 0. };

		glBufferData(GL_ARRAY_BUFFER, (tmp.size()) * sizeof(float), 0,
		GL_STATIC_DRAW);
		glBufferSubData(GL_ARRAY_BUFFER, 0, tmp.size() * sizeof(float),
				tmp.data());
	}
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	cl_int status;

	glFinish();

	clCreateFromGLBuffer(context, CL_MEM_READ_ONLY, vboID_m, &status);
	std::cout << status << std::endl;

	return EXIT_SUCCESS;

I attached modified project: testOpenCL.7z.

Please let me know, why do you have error with using SFML(very bad library!) ?

When I run your code, ret_num_platform value is 2 and for each of them clGetGLContextInfo give a clGLDevice of 0 and so the device_id is not set :frowning:

Ok, please test new code testOpenCL.7z.

	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];
			char str[1024];
			size_t strSize = 0;
			ret = clGetDeviceInfo(curDevice_id, CL_DEVICE_NAME, sizeof(str), str, &strSize);
			fprintf(stderr, "OpenCL Device Name %s
", str);
			ret = clGetDeviceInfo(curDevice_id, CL_DEVICE_VENDOR, sizeof(str), str, &strSize);
			fprintf(stderr, "OpenCL Device Vendor %s
", str);
			ret = clGetDeviceInfo(curDevice_id, CL_DEVICE_EXTENSIONS, sizeof(str), str, &strSize);
			fprintf(stderr, "OpenCL Device Extensions %s
", str);
			bool supportGLSharing = strstr(str, "cl_khr_gl_sharing") != NULL;
			fprintf(stderr, "OpenCL Supports OpenCL/OpenGL sharing: %s
", supportGLSharing ? "true" : "false");
			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;
		}
	}

I think your device doesn’t support OpenCL/OpenGL Interop, please post OpenCL info.

I tested on 2 system:

Windows & X64 - single GPU AMD R7240

Windows 10 X64 - GPU 1: nVidia 610(Monitor uses for this GPU) , GPU 2 - AMD R7 350X

I have no problem on this systems.