summaryrefslogtreecommitdiffstats
path: root/balls.c
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-10-26 18:50:45 -0400
committerSam Anthony <sam@samanthony.xyz>2024-10-26 18:50:45 -0400
commitda60f356e3c6404672cbc35b85b309d21fca896e (patch)
tree6a3047ea3890948763d4afbf352cefed413aaab8 /balls.c
parent2e8af6099010dc3b525cfd6517b8754bca9e99b9 (diff)
downloadballs-da60f356e3c6404672cbc35b85b309d21fca896e.zip
dummy opencl kernel
Diffstat (limited to 'balls.c')
-rw-r--r--balls.c91
1 files changed, 82 insertions, 9 deletions
diff --git a/balls.c b/balls.c
index af4f9d5..ecb649e 100644
--- a/balls.c
+++ b/balls.c
@@ -1,5 +1,6 @@
#define CL_TARGET_OPENCL_VERSION 110
+#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/glx.h>
@@ -7,21 +8,24 @@
#include "sysfatal.h"
-#define PROG "balls.cl"
-#define KERNEL "balls"
+#define PROG_FILE "balls.cl"
+#define KERNEL_FUNC "balls"
enum { WIDTH = 640, HEIGHT = 480 };
void initGL(int argc, char *argv[]);
-cl_context initCL(void);
+void initCL(void);
+char *readFile(const char *filename, size_t *size);
+
+static cl_context context;
+static cl_command_queue queue;
+static cl_kernel kernel;
int
main(int argc, char *argv[]) {
- cl_context context;
-
initGL(argc, argv);
- context = initCL();
+ initCL();
clReleaseContext(context);
}
@@ -35,16 +39,20 @@ initGL(int argc, char *argv[]) {
glClearColor(1, 1, 1, 1);
}
-cl_context
+void
initCL(void) {
cl_platform_id platform;
cl_device_id device;
- cl_context context;
cl_int err;
+ cl_program prog;
+ char *progBuf, *progLog;
+ size_t progSize, logSize;
+ /* Get platform. */
if (clGetPlatformIDs(1, &platform, NULL) < 0)
sysfatal("No OpenCL platform available.\n");
+ /* Configure properties for OpenGL interoperability. */
#ifdef WINDOWS
cl_context_properties properties[] = {
CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(),
@@ -61,12 +69,77 @@ initCL(void) {
};
#endif
+ /* Get GPU device. */
if (clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL) < 0)
sysfatal("No GPUs available.\n");
+ /* Create context. */
context = clCreateContext(properties, 1, &device, NULL, NULL, &err);
if (err < 0)
sysfatal("Failed to create context.\n");
- return context;
+ /* Create program from file. */
+ progBuf = readFile(PROG_FILE, &progSize);
+ prog = clCreateProgramWithSource(context, 1, (const char **) &progBuf, &progSize, &err);
+ if (err < 0)
+ sysfatal("Failed to create program.\n");
+ free(progBuf);
+
+ /* Build program. */
+ err = clBuildProgram(prog, 0, NULL, NULL, NULL, NULL);
+ if (err < 0) {
+ /* Print build log. */
+ clGetProgramBuildInfo(prog, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
+ progLog = malloc(logSize + 1);
+ progLog[logSize] = '\0';
+ clGetProgramBuildInfo(prog, device, CL_PROGRAM_BUILD_LOG, logSize+1, progLog, NULL);
+ fprintf(stderr, "%s\n", progLog);
+ free(progLog);
+ exit(1);
+ }
+
+ /* Create command queue. */
+ queue = clCreateCommandQueue(context, device, 0, &err);
+ if (err < 0)
+ sysfatal("Failed to create command queue.\n");
+
+ /* Create kernel. */
+ kernel = clCreateKernel(prog, KERNEL_FUNC, &err);
+ if (err < 0)
+ sysfatal("Failed to create kernel: %d\n", err);
+}
+
+char *
+readFile(const char *filename, size_t *size) {
+ FILE *f;
+ char *buf;
+
+ if ((f = fopen(filename, "r")) == NULL)
+ sysfatal("Failed to open file '%s'\n", filename);
+ fseek(f, 0, SEEK_END);
+ *size = ftell(f);
+ if ((buf = malloc((*size + 1) * sizeof(char))) == NULL) {
+ fclose(f);
+ sysfatal("Failed to allocate file buffer for '%s'\n", filename);
+ }
+ rewind(f);
+ fread(buf, sizeof(char), *size, f);
+ buf[*size] = '\0';
+ fclose(f);
+ return buf;
+}
+
+void
+configSharedData(void) {
+
+}
+
+void
+execKernel(void) {
+
+}
+
+void
+display(void) {
+
}