summaryrefslogtreecommitdiffstats
path: root/balls.c
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-10-26 18:01:51 -0400
committerSam Anthony <sam@samanthony.xyz>2024-10-26 18:01:51 -0400
commit2e8af6099010dc3b525cfd6517b8754bca9e99b9 (patch)
tree2406ab0893cb81b4d7a94fc6352741014875b6e4 /balls.c
downloadballs-2e8af6099010dc3b525cfd6517b8754bca9e99b9.zip
initCL()
Diffstat (limited to 'balls.c')
-rw-r--r--balls.c72
1 files changed, 72 insertions, 0 deletions
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 <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;
+}