From 2e8af6099010dc3b525cfd6517b8754bca9e99b9 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sat, 26 Oct 2024 18:01:51 -0400 Subject: initCL() --- balls.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 balls.c (limited to 'balls.c') diff --git a/balls.c b/balls.c new file mode 100644 index 0000000..af4f9d5 --- /dev/null +++ b/balls.c @@ -0,0 +1,72 @@ +#define CL_TARGET_OPENCL_VERSION 110 + +#include +#include +#include +#include + +#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; +} -- cgit v1.2.3