summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore6
-rw-r--r--Makefile17
-rw-r--r--balls.c72
-rw-r--r--sysfatal.c15
-rw-r--r--sysfatal.h1
5 files changed, 111 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7615922
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+*.o
+balls
+*.pdf
+*.png
+*.aux
+*.log
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..ec42712
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,17 @@
+CC = gcc
+CFLAGS = -std=c99 -Wall -pedantic -Wno-deprecated-declarations
+LDFLAGS = -lOpenCL -lglut -lGLU -lGL -lGLX
+
+SRC = balls.c sysfatal.c
+OBJ = ${SRC:.c=.o}
+
+balls: ${OBJ}
+ ${CC} -o $@ ${LDFLAGS} $^
+
+%.o: %.c
+ ${CC} -c ${CFLAGS} $<
+
+clean:
+ rm -f *.o balls
+
+${OBJ}: sysfatal.h
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;
+}
diff --git a/sysfatal.c b/sysfatal.c
new file mode 100644
index 0000000..9a63264
--- /dev/null
+++ b/sysfatal.c
@@ -0,0 +1,15 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include "sysfatal.h"
+
+void
+sysfatal(const char * format, ...) {
+ va_list args;
+
+ va_start(args, format);
+ vfprintf(stderr, format, args);
+ va_end(args);
+ fflush(NULL);
+ exit(1);
+}
diff --git a/sysfatal.h b/sysfatal.h
new file mode 100644
index 0000000..d56ec5d
--- /dev/null
+++ b/sysfatal.h
@@ -0,0 +1 @@
+void sysfatal(const char *format, ...);