The following code works fine (Windows 7, Nvidia GTX 750 Ti) with Nvidia 361.91 drivers (and earlier), but freezes with newer versions such as 364.72 and 368.69. GlFinish now blocks program execution only after calling clEnqueueReleaseGLObjects. Before blaming the drivers, I suspect that something is wrong with my OpenCL / OpenGL interaction, so here is the code for a small, solid program that reproduces the problem, with the problem at the very end:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <SDL.h>
#include <gl/glew.h>
#include <SDL_opengl.h>
#include <gl/glut.h>
#pragma comment (lib, "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v7.5\\lib\\x64\\OpenCL.lib")
#include <CL/cl.h>
#include <CL/cl_gl.h>
cl_int init_cl_context(cl_context *context, cl_command_queue *command_queue)
{
cl_int i, ret, pf_index=-1;
cl_platform_id platform_id[16];
cl_device_id device_id[16];
cl_uint ret_num_platforms;
cl_uint ret_num_devices;
ret = clGetPlatformIDs(sizeof(platform_id)/sizeof(*platform_id), platform_id, &ret_num_platforms);
for (i=0; i<ret_num_platforms; i++)
{
ret = clGetDeviceIDs(platform_id[i], CL_DEVICE_TYPE_GPU, sizeof(device_id)/sizeof(*device_id), device_id, &ret_num_devices);
if (ret_num_devices > 0)
{
pf_index = i;
break;
}
}
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_id[pf_index], 0 };
*context = clCreateContextFromType(properties, CL_DEVICE_TYPE_GPU, NULL, NULL, &ret);
*command_queue = clCreateCommandQueue(*context, device_id[0], 0*CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | 0*CL_QUEUE_PROFILING_ENABLE, &ret);
return ret;
}
int main(int argc, char *argv[])
{
cl_int ret=0;
int w = 800, h = 600;
SDL_Window *window;
SDL_Renderer *renderer;
cl_context context;
cl_command_queue command_queue;
cl_mem cltex;
uint32_t gltex;
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_AUDIO);
window = SDL_CreateWindow ("Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_OPENGL);
SDL_GetWindowSize(window, &w, &h);
SDL_GL_CreateContext(window);
glewExperimental = 1;
glewInit();
renderer = SDL_CreateRenderer(window, -1, 0*SDL_RENDERER_PRESENTVSYNC);
ret = init_cl_context(&context, &command_queue);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &gltex);
glBindTexture(GL_TEXTURE_2D, gltex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
cltex = clCreateFromGLTexture(context, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, gltex, &ret);
ret = clFinish(command_queue);
ret = clEnqueueReleaseGLObjects(command_queue, 1, &cltex, 0, 0, NULL);
clFinish(command_queue);
glFlush();
printf("This blocks the execution forever:\n");
glFinish();
printf("This never gets printed\n");
return 0;
}
( ) , , , , . , , , - . , ...