summaryrefslogtreecommitdiff
path: root/src/boring_parts.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/boring_parts.cc')
-rw-r--r--src/boring_parts.cc188
1 files changed, 98 insertions, 90 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*/