From aa769549e1268c67f3dbd62191a4071aead0f877 Mon Sep 17 00:00:00 2001
From: Ludovic Pouzenc <ludovic@pouzenc.fr>
Date: Sun, 17 Feb 2013 12:30:56 +0000
Subject: Ajout init openCL, debut binding buffers (mais l'extension pour les
 VBO n'est pas active !)

git-svn-id: file:///var/svn/2013-gpudataviz/trunk@11 371a6b4a-a258-45f8-9dcc-bdd82ce0ac9d
---
 tests/poc_c/compil.sh |   9 +++-
 tests/poc_c/main.c    | 112 +++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 100 insertions(+), 21 deletions(-)

(limited to 'tests/poc_c')

diff --git a/tests/poc_c/compil.sh b/tests/poc_c/compil.sh
index f636aaf..d64cc67 100755
--- a/tests/poc_c/compil.sh
+++ b/tests/poc_c/compil.sh
@@ -1,10 +1,15 @@
 #!/bin/bash -e
+AMDAPP_PATH=/opt/AMDAPP
+
 CFLAGS="-Wall -g"
 LDFLAGS="-Wall -g"
 
-CFLAGS="$CFLAGS $(pkg-config --cflags gtkglextmm-1.2)"
-LIBS="$(pkg-config --libs gtkglextmm-1.2)"
+CFLAGS="$CFLAGS $(pkg-config --cflags gtkglextmm-1.2) -I$AMDAPP_PATH/include"
+LIBS="$(pkg-config --libs gtkglextmm-1.2) -lGLEW -lOpenCL -L$AMDAPP_PATH/lib/x86_64"
 
+echo "Compiling..."
 gcc $CFLAGS -c main.c -o main.o
+
+echo "Linking..."
 gcc $LDFLAGS -o main main.o $LIBS
 
diff --git a/tests/poc_c/main.c b/tests/poc_c/main.c
index 2b86f0f..4e2be04 100644
--- a/tests/poc_c/main.c
+++ b/tests/poc_c/main.c
@@ -3,9 +3,12 @@
 #include <gtk/gtk.h>
 #include <gtk/gtkgl.h>
 
-#include <GL/gl.h>
+/*#include <GL/gl.h>*/
+#include <GL/glew.h> // For VBO OpenGL extensions
 #include <GL/glu.h>
 
+#include <CL/opencl.h>
+
 #include <gdk/x11/gdkglx.h> // X11 specific
 
 gboolean gl_area_on_draw (GtkObject* area, GdkEventExpose* event, gpointer data) {
@@ -18,8 +21,6 @@ gboolean gl_area_on_draw (GtkObject* area, GdkEventExpose* event, gpointer data)
 		return FALSE;
 	}
 
-	printf("on_draw_glx_current_context==%p\n", (void *)glXGetCurrentContext());
-
 	GtkAllocation allocation;
 	gtk_widget_get_allocation (GTK_WIDGET (area), &allocation);
 	GLdouble viewport_width = (GLdouble) allocation.width;
@@ -83,6 +84,12 @@ int main(int argc, char *argv[]) {
 	Display *glx_display; // X11 specific
 	gboolean res;
 
+	glewInit();
+	if ( !glewIsSupported("GL_ARB_vertex_buffer_object") ) {
+		fputs ("OpenGL extension GL_ARB_vertex_buffer_object is mandatory for this application\n", stderr);
+		return EXIT_FAILURE;
+	}
+
 	gtk_init(&argc, &argv);
 	if (gdk_gl_init_check(&argc, &argv) == FALSE ) {
 		fputs ("Failed to initialize GDKGLExt.\n", stderr);
@@ -144,34 +151,101 @@ int main(int argc, char *argv[]) {
 
 	printf("glx_current_context==%p\n", (void *)glx_context);
 	printf("glx_current_display==%p\n", (void *)glx_display);
+	
+	cl_context_properties cpsGL[] = {
+		/* CL_CONTEXT_PLATFORM value to be filled later */
+		CL_CONTEXT_PLATFORM, (cl_context_properties) NULL,
+		CL_GLX_DISPLAY_KHR, (intptr_t) glx_display,
+		CL_GL_CONTEXT_KHR, (intptr_t) glx_context, 0
+	};
 	/* END : X11 specific */
 	gdk_gl_drawable_gl_end (drawable);
 
-	//TODO OpenCL init (platform pointer). In a while loop for trying all ?
 
-	/* BEGIN : X11 specific */
-	cl_context_properties cpsGL[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform,
-		CL_GLX_DISPLAY_KHR, (intptr_t) glx_display,
-		CL_GL_CONTEXT_KHR, (intptr_t) glx_context, 0
-	};
+	cl_uint i, plat_count;
+	cl_platform_id *plat_ids, plat_id;
+	char pbuf[100];
+	size_t deviceSize;
+	cl_device_id device;
+	cl_context cl_ctx;
+	cl_command_queue commandQueue;
 
-	/*
-	if (!clGetGLContextInfoKHR)
-	{
-		clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn) clGetExtensionFunctionAddressForPlatform(platform, "clGetGLContextInfoKHR");
-		if (!clGetGLContextInfoKHR)
-		{
-			std::cout << "Failed to query proc address for clGetGLContextInfoKHR";
-		}
-	}
+	typedef CL_API_ENTRY cl_int (CL_API_CALL *clGetGLContextInfoKHR_fn)(
+			const cl_context_properties *properties,
+			cl_gl_context_info param_name,
+			size_t param_value_size,
+			void *param_value,
+			size_t *param_value_size_ret);
 
+	clGetGLContextInfoKHR_fn clGetGLContextInfoKHR_proc;
 
-*/
+	clGetPlatformIDs(0, NULL, &plat_count); // Get platform count
+	if ( ! ( plat_count > 0 ) ) {
+		fputs ("Failed to find an OpenCL platform\n", stderr);
+		return EXIT_FAILURE;
+	}
+	plat_ids=malloc(sizeof(cl_platform_id *) * plat_count);
+	clGetPlatformIDs(plat_count, plat_ids, &plat_count); // Get platform IDs
+
+        for(i=0;i<plat_count;i++) {
+		plat_id=plat_ids[i];
+
+		clGetPlatformInfo(plat_id, CL_PLATFORM_VENDOR, sizeof(pbuf), pbuf, NULL);
+		printf("Plaform %i (id %p) : VENDOR : '%s'\n", i, (void *) plat_id, pbuf);
+
+		/* CL_CONTEXT_PLATFORM value filled now */
+		cpsGL[1]=(cl_context_properties) plat_id;
+
+		// TODO : use the clGetGLContextInfoKHR() normally when available
+		clGetGLContextInfoKHR_proc = (clGetGLContextInfoKHR_fn) clGetExtensionFunctionAddressForPlatform(plat_id, "clGetGLContextInfoKHR");
+		 if (!clGetGLContextInfoKHR_proc) {
+			 fputs ("Failed to query proc address of clGetGLContextInfoKHR for this platform\n", stderr);
+			 continue;
+		 }
+
+		deviceSize=0;
+		// get deviceSize (should be 1*sizeof(cl_device_id) with CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR)
+		res=clGetGLContextInfoKHR_proc(cpsGL,CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,0,NULL,&deviceSize);
+		if ( res!=CL_SUCCESS || deviceSize!=1*sizeof(cl_device_id)) {
+			fputs ("Failed to get CL_CURRENT_DEVICE_FOR_GL_CONTEXT deviceSize\n", stderr);
+			continue;
+		}
 
+		device=0;
+		res=clGetGLContextInfoKHR_proc(cpsGL,CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,deviceSize,&device,NULL);
+		if ( res!=CL_SUCCESS || device==0 ) {
+			fputs ("Failed to get CL_CURRENT_DEVICE_FOR_GL_CONTEXT device\n", stderr);
+			continue;
+		}
 
+		//TODO : implement selection if multiple devices are usable (instead of taking the first one)
+		break;
+	}
+	printf("cl_device==%p\n", (void *)device);
+
+	cl_ctx = clCreateContext(cpsGL,1,&device,0,0,&res);
+	commandQueue = clCreateCommandQueue(cl_ctx,device,0,&res);
+
+	GLuint gl_vbo;
+	unsigned int meshWidth=2, meshHeight=2;
+	GLsizeiptr gl_vbo_data_size = meshWidth * meshHeight * sizeof(cl_float4);
+	float gl_vertex_default_pos[16] = { // to be removed
+		 0.5, -0.5, 0.0, 1.0,
+		 0.5,  0.5, 0.0, 1.0,
+		-0.5, -0.5, 0.0, 1.0,
+		-0.5, -0.5, 0.0, 1.0
+	};
 
+	res = gdk_gl_drawable_gl_begin(drawable, gl_context);
+	glGenBuffers(1, &gl_vbo);
+	glBindBuffer(GL_ARRAY_BUFFER, gl_vbo);
+	glBufferData(GL_ARRAY_BUFFER, gl_vbo_data_size, NULL, GL_STREAM_DRAW /*GL_DYNAMIC_DRAW vu dans SimpleGL de ATI*/);
 
+	// to be removed : default values
+	glBufferSubData(GL_ARRAY_BUFFER,0, gl_vbo_data_size, gl_vertex_default_pos);
 
+	glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind buffer (no more current buffer)
+	gdk_gl_drawable_gl_end (drawable);
 
 	gtk_main();
 	return EXIT_SUCCESS;
-- 
cgit v1.2.3