diff options
Diffstat (limited to 'balls.c')
| -rw-r--r-- | balls.c | 72 |
1 files changed, 72 insertions, 0 deletions
@@ -0,0 +1,72 @@ +#define CL_TARGET_OPENCL_VERSION 110 + +#include <stdio.h> +#include <GL/glut.h> +#include <GL/glx.h> +#include <CL/cl_gl.h> + +#include "sysfatal.h" + +#define PROG "balls.cl" +#define KERNEL "balls" + +enum { WIDTH = 640, HEIGHT = 480 }; + +void initGL(int argc, char *argv[]); +cl_context initCL(void); + +int +main(int argc, char *argv[]) { + cl_context context; + + initGL(argc, argv); + + context = initCL(); + + clReleaseContext(context); +} + +void +initGL(int argc, char *argv[]) { + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); + glutInitWindowSize(WIDTH, HEIGHT); + glutCreateWindow("Balls"); + glClearColor(1, 1, 1, 1); +} + +cl_context +initCL(void) { + cl_platform_id platform; + cl_device_id device; + cl_context context; + cl_int err; + + if (clGetPlatformIDs(1, &platform, NULL) < 0) + sysfatal("No OpenCL platform available.\n"); + + #ifdef WINDOWS + cl_context_properties properties[] = { + CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(), + CL_GLX_DISPLAY_KHR, (cl_context_properties) wctlGetCurrentDC(), + CL_CONTEXT_PLATFORM, (cl_context_properties) platform, + 0 + }; + #else + cl_context_properties properties[] = { + CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext(), + CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(), + CL_CONTEXT_PLATFORM, (cl_context_properties) platform, + 0 + }; + #endif + + if (clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL) < 0) + sysfatal("No GPUs available.\n"); + + context = clCreateContext(properties, 1, &device, NULL, NULL, &err); + if (err < 0) + sysfatal("Failed to create context.\n"); + + return context; +} |