# HG changeset patch # User John Tsiombikas # Date 1282957276 -3600 # Node ID 1169f3d04135283a3de8638985f84deaac1dc7da # Parent 057b8575a1c1db3368c78e8a0c9377e4ddab202f added CL/GL interop support for macosx (compiles) and windows (untested) diff -r 057b8575a1c1 -r 1169f3d04135 Makefile --- a/Makefile Fri Aug 27 20:39:55 2010 +0100 +++ b/Makefile Sat Aug 28 02:01:16 2010 +0100 @@ -13,7 +13,6 @@ else libgl = -lGL -lglut libcl = -lOpenCL - def = -DCLGL_INTEROP endif diff -r 057b8575a1c1 -r 1169f3d04135 src/ocl.cc --- a/src/ocl.cc Fri Aug 27 20:39:55 2010 +0100 +++ b/src/ocl.cc Sat Aug 28 02:01:16 2010 +0100 @@ -21,6 +21,10 @@ #include #endif +#ifdef __APPLE__ +#include +#endif + struct device_info { cl_device_id id; @@ -33,10 +37,14 @@ size_t work_group_size; unsigned long mem_size; + + char *extensions; + bool gl_sharing; }; static int select_device(struct device_info *di, int (*devcmp)(struct device_info*, struct device_info*)); static int get_dev_info(cl_device_id dev, struct device_info *di); +static void destroy_dev_info(struct device_info *di); static int devcmp(struct device_info *a, struct device_info *b); static const char *devtypestr(cl_device_type type); static void print_memsize(FILE *out, unsigned long memsz); @@ -53,13 +61,25 @@ return false; } + + #ifndef CLGL_INTEROP cl_context_properties *prop = 0; - #else #if defined(__APPLE__) -#error "CL/GL context sharing not implemented on MacOSX yet" + CGLContextObj glctx = CGLGetCurrentContext(); + CGLShareGroupObj sgrp = CGLGetShareGroup(glctx); + + cl_context_properties prop[] = { +#ifdef CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE + CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)sgrp, +#else + CL_GL_CONTEXT_KHR, (cl_context_properties)glctx, + CL_CGL_SHAREGROUP_KHR, (cl_context_properties)sgrp, +#endif + 0 + }; #elif defined(unix) || defined(__unix__) Display *dpy = glXGetCurrentDisplay(); GLXContext glctx = glXGetCurrentContext(); @@ -72,7 +92,14 @@ 0 }; #elif defined(WIN32) || defined(__WIN32__) -#error "CL/GL context sharing not implemented on windows yet" + HGLRC glctx = wglGetCurrentContext(); + HDC dc = wglGetCurrentDC(); + + cl_context_properties prop[] = { + CL_GL_CONTEXT_KHR, (cl_context_properties)glctx, + CL_WGL_HDC_KHR, (cl_context_properties)dc, + 0 + }; #else #error "unknown or unsupported platform" #endif @@ -631,7 +658,7 @@ struct device_info di; if(get_dev_info(dev[i], &di) == -1) { - free(dev_inf->work_item_sizes); + destroy_dev_info(&di); return -1; } @@ -654,6 +681,8 @@ print_memsize(stdout, di.mem_size); putchar('\n'); + printf("extensions: %s\n", di.extensions); + if(devcmp(&di, dev_inf) > 0) { free(dev_inf->work_item_sizes); memcpy(dev_inf, &di, sizeof di); @@ -673,7 +702,6 @@ { di->id = dev; - clGetDeviceInfo(dev, CL_DEVICE_TYPE, sizeof di->type, &di->type, 0); clGetDeviceInfo(dev, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof di->units, &di->units, 0); clGetDeviceInfo(dev, CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof di->clock, &di->clock, 0); @@ -685,9 +713,28 @@ clGetDeviceInfo(dev, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof di->work_group_size, &di->work_group_size, 0); clGetDeviceInfo(dev, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof di->mem_size, &di->mem_size, 0); + size_t ext_str_len; + clGetDeviceInfo(dev, CL_DEVICE_EXTENSIONS, 0, 0, &ext_str_len); + + di->extensions = new char[ext_str_len + 1]; + clGetDeviceInfo(dev, CL_DEVICE_EXTENSIONS, ext_str_len, di->extensions, 0); + di->extensions[ext_str_len] = 0; + + if(strstr(di->extensions, "cl_khr_gl_sharing") || strstr(di->extensions, "cl_APPLE_gl_sharing")) { + di->gl_sharing = true; + } else { + di->gl_sharing = false; + } + return 0; } +static void destroy_dev_info(struct device_info *di) +{ + delete [] di->work_item_sizes; + delete [] di->extensions; +} + static int devcmp(struct device_info *a, struct device_info *b) { unsigned int aval = a->units * a->clock;