summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Pouzenc <ludovic@pouzenc.fr>2013-03-02 20:20:32 +0000
committerLudovic Pouzenc <ludovic@pouzenc.fr>2013-03-02 20:20:32 +0000
commit8c587d2243884d85bbe5fadac64258ac246617b4 (patch)
treee944cd0223fef39a062a8abdf3ab0db8c621f60f
parent5fdcb39c0171c48207063a81ddbcaa553f1b2969 (diff)
download2013-gpudataviz-8c587d2243884d85bbe5fadac64258ac246617b4.tar.gz
2013-gpudataviz-8c587d2243884d85bbe5fadac64258ac246617b4.tar.bz2
2013-gpudataviz-8c587d2243884d85bbe5fadac64258ac246617b4.zip
Version C++ qui arrive a initialiser OpenCL sans trop de merdouilles specifiques.
Un dur labeur... Reste à retourner les pointeurs où il faut, faire la partie VBO, l'affichage du VBO et lancer un premier kernel git-svn-id: file:///var/svn/2013-gpudataviz/trunk@16 371a6b4a-a258-45f8-9dcc-bdd82ce0ac9d
-rw-r--r--src/boring_parts.cc188
-rw-r--r--src/boring_parts.h6
-rw-r--r--src/gpudataviz.cc36
3 files changed, 129 insertions, 101 deletions
diff --git a/src/boring_parts.cc b/src/boring_parts.cc
index bc25f8f..27ead8b 100644
--- a/src/boring_parts.cc
+++ b/src/boring_parts.cc
@@ -1,7 +1,5 @@
#include "boring_parts.h"
-
-
// TODO : only need OpenGL things, not GTK ones for now
//#include "gtk_includes.h"
@@ -20,22 +18,52 @@
} \
} while(0)
-int initLibs() {
-
-#ifdef HAS_OPENCL
- RETURN_IF_FAIL( initOpenCL() );
-#endif /*HAS_OPENCL*/
- return 0;
-}
+/* From http://stackoverflow.com/questions/4317062/opengl-how-to-check-if-the-user-supports-glgenbuffers
+#ifndef STRINGIFY
+ #define STRINGIFY(x) #x
+#endif
+#ifdef WIN32
+ #include <windows.h>
+ #define glGetProcAddress(a) wglGetProcAddress(a)
+#endif
+#ifdef X11
+ #define glGetProcAddress(a) glXGetProcAddress ( \
+ reinterpret_cast<const unsigned char*>(a) \
+ )
+#endif
+
+#ifndef GetExtension
+ #define GetExtension(Type, ExtenName) \
+ ExtenName = (Type) \
+ glGetProcAddress(STRINGIFY(ExtenName)); \
+ if(!ExtenName) \
+ { \
+ std:cout << "Your Computer Does Not " \
+ << "Support GL Extension: " \
+ << STRINGIFY(ExtenName) \
+ << std::endl; \
+ exit(1); \
+ } \
+ else \
+ { \
+ std::cout << "Loaded Extension: " \
+ << STRINGIFY(ExtenName) \
+ << std::endl; \
+ }
+#endif
+*/
#ifdef HAS_OPENCL
-int initOpenCL() {
+int initOpenCL(intptr_t gl_display, intptr_t gl_context, intptr_t gl_vbo) {
cl_uint id, numPlatforms, numDevices;
char pbuf[100];
std::string dTypeStr;
cl_platform_id *platforms, platform;
- cl_device_id *devices, device;
+ cl_device_id /* *devices, */device;
+ cl_context cl_ctx;
+ cl_command_queue cl_commandQueue;
+ bool usableDeviceFound=false;
// Get platform count
CL_RETURN_VAL_IF_FAIL(1,
@@ -55,97 +83,77 @@ int initOpenCL() {
// Enumerate platforms and grab informations
for(id=0;id<numPlatforms;id++) {
+ cl_int res;
+ platform=platforms[id];
+
CL_RETURN_VAL_IF_FAIL(4,
- clGetPlatformInfo(platforms[id], CL_PLATFORM_VENDOR, sizeof(pbuf), pbuf, NULL)
+ clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, sizeof(pbuf), pbuf, NULL)
);
std::cout << "Platform " << id << " : " << pbuf << std::endl;
- }
-
- // Take the first one FIXME : do more !
- platform = platforms[0];
-
- // Get device count
- CL_RETURN_VAL_IF_FAIL(11,
- clGetDeviceIDs(platform,CL_DEVICE_TYPE_ALL,0,NULL,&numDevices)
- );
-
- std::cout << "\tDetected " << numDevices << " device(s) for platform " << platform << std::endl;
- if ( ! ( numDevices > 0 ) ) return 12;
-
- // Allocate room for all devices IDs
- devices = new cl_device_id[numDevices];
-
- // Get devices IDs
- CL_RETURN_VAL_IF_FAIL(13,
- clGetDeviceIDs(platform,CL_DEVICE_TYPE_ALL,numDevices,devices,&numDevices)
- );
- // Enumerate devices and grab informations
- for(id=0;id<numDevices;id++) {
- cl_device_type dType;
-
- CL_RETURN_VAL_IF_FAIL(14,
- clGetDeviceInfo(devices[id], CL_DEVICE_TYPE, sizeof(dType), &dType, NULL)
- );
-
- switch(dType) {
- case CL_DEVICE_TYPE_GPU: dTypeStr="gpu"; break;
- case CL_DEVICE_TYPE_CPU: dTypeStr="cpu"; break;
- default: dTypeStr="(not supported)"; break;
+ // Dynamically get the function pointer for clGetGLConetextInfoKHR
+ clGetGLContextInfoKHR_fn clGetGLContextInfoKHR_proc = (clGetGLContextInfoKHR_fn) clGetExtensionFunctionAddressForPlatform(platform, "clGetGLContextInfoKHR");
+ if (!clGetGLContextInfoKHR_proc) {
+ std::cerr << "clGetExtensionFunctionAddressForPlatform(platform, clGetGLContextInfoKHR) failed" << std::endl;
+ continue;
}
- std::cout << "\tDevice " << id << " type is " << dTypeStr << std::endl;
- }
- // Take the first one FIXME : do more !
- device=devices[0];
- std::cout << "\tWill use device 0 (" << device << ")" << std::endl;
-
- delete [] devices;
- delete [] platforms;
-
-/*
- if (!clGetGLContextInfoKHR)
- {
- clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn) clGetExtensionFunctionAddressForPlatform(platform, "clGetGLContextInfoKHR");
- if (!clGetGLContextInfoKHR)
- {
- std::cout << "Failed to query proc address for clGetGLContextInfoKHR";
+ // Try to get the device corresponding to the GL context/display on this platform
+ cl_context_properties cpsGL[] = {
+ CL_CONTEXT_PLATFORM, (cl_context_properties)platform,
+ CL_GLX_DISPLAY_KHR, gl_display,
+ CL_GL_CONTEXT_KHR, gl_context,
+ 0
+ };
+
+ std::cout << "cl_context_properties :" << std::endl;
+ std::cout << "\tCL_CONTEXT_PLATFORM :" << (void *)cpsGL[1] << std::endl;
+ std::cout << "\tCL_GLX_DISPLAY_KHR :" << (void *)cpsGL[3] << std::endl;
+ std::cout << "\tCL_GL_CONTEXT_KHR :" << (void *)cpsGL[5] << std::endl;
+
+ size_t 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)) {
+ std::cerr << "clGetGLContextInfoKHR_proc(cpsGL,CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,0,...) failed" << std::endl;
+ std::cerr << " (return code : " << res << ")" << std::endl;
+ continue;
+ }
+
+ device=0;
+ res=clGetGLContextInfoKHR_proc(cpsGL,CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,deviceSize,&device,NULL);
+ if ( res!=CL_SUCCESS || device==0 ) {
+ std::cerr << "clGetGLContextInfoKHR_proc(cpsGL,CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,...) failed" << std::endl;
+ std::cerr << " (return code : " << res << ")" << std::endl;
+ continue;
}
- }
-
- std::cout << "glXCreateContextAttribsARB "
- << (void*) glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB")
- << std::endl;
- GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC)
- glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
+ std::cout << "cl_device :" << (void *)device << std::endl;
- int attribs[] = {
- GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
- GLX_CONTEXT_MINOR_VERSION_ARB, 0,
- 0
- };
-
- GLXContext ctx = glXCreateContextAttribsARB(displayName, *fbc, 0, true, attribs);
-*/
-
-
-/*
- Ici, le contexte OpenGL du widget MyGTKGLSceneWidget est nécessaire.
+ cl_ctx = clCreateContext(cpsGL,1,&device,0,0,&res);
+ if ( res!=CL_SUCCESS ) {
+ std::cerr << "clCreateContext() failed" << std::endl;
+ std::cerr << " (return code : " << res << ")" << std::endl;
+ continue;
+ }
- http://developer.gnome.org/gtkglext/stable/gtkglext-gdkglcontext.html
- glXMakeCurrent (displayName, win, ctx);
- */
+ cl_commandQueue = clCreateCommandQueue(cl_ctx,device,0,&res);
+ if ( res!=CL_SUCCESS ) {
+ std::cerr << "clCreateCommandQueue() failed" << std::endl;
+ std::cerr << " (return code : " << res << ")" << std::endl;
+ continue;
+ }
- GLXContext gGlCtx = glXGetCurrentContext();
- std::cout << "gGlCtx == " << gGlCtx << std::endl;
- cl_context_properties cpsGL[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform,
- CL_GLX_DISPLAY_KHR, (intptr_t) glXGetCurrentDisplay(),
- CL_GL_CONTEXT_KHR, (intptr_t) gGlCtx, 0
- };
+ usableDeviceFound=true;
+ break;
+ }
- std::cout << "OpenCL initialization done." << std::endl;
- return 0;
+ if (! usableDeviceFound) {
+ std::cerr << "No OpenCL device has been successfully initialized" << std::endl;
+ return 1;
+ }
+ std::cout << "OpenCL initialization done." << std::endl;
+ return 0;
}
#endif /*HAS_OPENCL*/
diff --git a/src/boring_parts.h b/src/boring_parts.h
index 54fd7b3..b4cd58d 100644
--- a/src/boring_parts.h
+++ b/src/boring_parts.h
@@ -2,18 +2,18 @@
#include <gtkmm.h> /* Must included before X11/Xlib.h */
//#include <GL/glew.h> /* Seems included by GL/glxew.h */
-#include <GL/glxew.h> /* For GLXContext, must appear before gl.h, includes X11/Xlib.h */
+//#include <GL/glxew.h> /* For GLXContext, must appear before gl.h, includes X11/Xlib.h */
#include <gtkglmm.h> /* Includes a GL/gl.h (the right one ??) */
//#include <GL/gl.h>
-//#include <GL/glu.h>
+#include <GL/glu.h>
//#include <CL/cl_gl.h>
int initLibs();
#ifdef HAS_OPENCL
#include <CL/opencl.h>
-int initOpenCL();
+int initOpenCL(intptr_t gl_display, intptr_t gl_context, intptr_t gl_vbo);
#else
// Quick and dirty cl_float4 replacement
typedef union {
diff --git a/src/gpudataviz.cc b/src/gpudataviz.cc
index e6e4ca5..fbc1852 100644
--- a/src/gpudataviz.cc
+++ b/src/gpudataviz.cc
@@ -7,6 +7,8 @@
#include "gtk_win_main.h"
#include "my_gtk_gl_scene_widget.h"
+#include <gdk/x11/gdkglx.h> // X11 specific
+
// Macro to make things readable in main() function
#define EXIT_IF_FAIL(val, expr) do { \
if ( ! (expr) ) { \
@@ -39,18 +41,15 @@ int main(int argc, char* argv[]) {
Glib::RefPtr<Gdk::GL::Config> glconfig;
EXIT_IF_FAIL(2, glconfig=Gdk::GL::Config::create(glMode) );
- // Initialize OpenCL (if available)
- EXIT_IF_FAIL(3, initLibs()==0 ); // See boring_parts.cc
-
- // Initialize host work memory (array for all the vertex coordinates computed by OpenCL and displayed by OpenGL)
- cl_float4 *hostWorkMem = (cl_float4 *) calloc(meshWidth * meshHeight, sizeof(cl_float4));
- EXIT_IF_FAIL(4, hostWorkMem);
-
// Initialize the OpenGL scene
MyGTKGLSceneWidget glScene(glconfig);
- // Instantiate and run the GTK app
+ // Instantiate the GTK app
GTKWinMain gtkwinmain(glScene);
+
+ // Initialize OpenCL (only after the MyGTKGLSceneWidget realize)
+ //EXIT_IF_FAIL(3, initLibs()==0 ); // See boring_parts.cc
+
gtkKit.run(gtkwinmain);
return 0;
@@ -88,6 +87,27 @@ void MyGTKGLSceneWidget::on_realize() {
return;
}
+ // TODO : GL_ARB_vertex_buffer_object
+ intptr_t gl_vbo=0;
+
+#ifdef HAS_OPENCL
+ static bool isOpenCLInitialized=false;
+
+ if (! isOpenCLInitialized) {
+// #ifdef X11
+ intptr_t gl_context = (intptr_t)glXGetCurrentContext();
+ intptr_t gl_display = (intptr_t)glXGetCurrentDisplay();
+
+ std::cerr << "DEBUG : begin initOpenCL()" << std::endl;
+ initOpenCL(gl_display, gl_context, gl_vbo); /* See boring_parts.cc */
+ isOpenCLInitialized=true;
+// #else
+// #error initOpenCL works only for X11 systems for now
+// #endif
+ }
+#endif
+
+
// Programmatically create rendering lists : opengl will able to replay that efficiently
GLUquadricObj* qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj, GLU_FILL);