diff options
author | Michael Bien <[email protected]> | 2010-01-26 00:40:22 +0100 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-01-26 00:40:22 +0100 |
commit | b0102cab5062eb8cb06329bff457d4dd5b9d0099 (patch) | |
tree | fdc39c89c6c55a7d41b7e4911a13c52b3a7f7d8b /src/com/mbien/opencl/CLGLContext.java | |
parent | 22a5f55a5d0e4215eacda6dbf01b34cec47a5bf0 (diff) |
implemented GL interop context creation on windows and mac.
added more factory methods to CLGLContext.
Diffstat (limited to 'src/com/mbien/opencl/CLGLContext.java')
-rw-r--r-- | src/com/mbien/opencl/CLGLContext.java | 117 |
1 files changed, 87 insertions, 30 deletions
diff --git a/src/com/mbien/opencl/CLGLContext.java b/src/com/mbien/opencl/CLGLContext.java index a76c15ab..a66772e7 100644 --- a/src/com/mbien/opencl/CLGLContext.java +++ b/src/com/mbien/opencl/CLGLContext.java @@ -13,66 +13,123 @@ import javax.media.opengl.GLContext; import static com.mbien.opencl.CLGLI.*; /** - * + * OpenCL Context supporting interoperablity between JOGL and JOCL. * @author Michael Bien */ public final class CLGLContext extends CLContext { - final long glContextID; + final long glID; private CLGLContext(long clContextID, long glContextID) { super(clContextID); - this.glContextID = glContextID; + this.glID = glContextID; } - public static CLGLContext create(GLContext glContext) { + /** + * Creates a shared context on all available devices (CL_DEVICE_TYPE_ALL). + */ + public static final CLGLContext create(GLContext glContext) { + return create(glContext, (CLPlatform)null, CLDevice.Type.ALL); + } + + /** + * Creates a shared context on the specified platform on all available devices (CL_DEVICE_TYPE_ALL). + */ + public static CLGLContext create(GLContext glContext, CLPlatform platform) { + return create(glContext, platform, CLDevice.Type.ALL); + } + + public static CLGLContext create(GLContext glContext, CLDevice.Type... deviceTypes) { + return create(glContext, null, deviceTypes); + } + /** + * Creates a shared context on the specified devices. + * The platform to be used is implementation dependent. + */ + public static final CLGLContext create(GLContext glContext, CLDevice... devices) { + return create(glContext, null, devices); + } + + /** + * Creates a shared context on the specified platform and with the specified + * device types. + */ + private static final CLGLContext create(GLContext glContext, CLPlatform platform, CLDevice.Type... deviceTypes) { -//UNIX -//cl_context_properties props[] = { -// CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), -// CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(), -// CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0}; + long[] glID = new long[1]; + PointerBuffer properties = setupContextProperties(glContext, platform, glID); + long clID = createContextFromType(properties, toDeviceBitmap(deviceTypes)); -//WIN32 -//cl_context_properties props[] = { -// CL_GL_CONTEXT_KHR, (cl_context_properties)TODO0, -// CL_WGL_HDC_KHR, (cl_context_properties)TODO 0, -// CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0}; + return new CLGLContext(clID, glID[0]); -//MACOSX -//cl_context_properties props[] = { -// CL_CGL_SHAREGROUP_KHR, (cl_context_properties)TODO 0, -// CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0}; + } - long glID = 0; + /** + * Creates a shared context on the specified platform and with the specified + * devices. + */ + private static final CLGLContext create(GLContext glContext, CLPlatform platform, CLDevice... devices) { + + long[] glID = new long[1]; + PointerBuffer properties = setupContextProperties(glContext, platform, glID); + long clID = createContext(properties, devices); + + return new CLGLContext(clID, glID[0]); + } + + + private static final PointerBuffer setupContextProperties(GLContext glContext, CLPlatform platform, long[] glID) { + + if(platform == null) { + platform = CLPlatform.getDefault(); + } + + if(platform == null) { + throw new RuntimeException("no OpenCL installation found"); + } GLContextImpl ctxImpl = (GLContextImpl)glContext; DefaultGraphicsConfiguration config = (DefaultGraphicsConfiguration)ctxImpl.getDrawableImpl() .getNativeWindow().getGraphicsConfiguration().getNativeGraphicsConfiguration(); - PointerBuffer properties = PointerBuffer.allocateDirect(5); + PointerBuffer properties = null; if(glContext instanceof X11GLXContext) { + properties = PointerBuffer.allocateDirect(7); long handle = config.getScreen().getDevice().getHandle(); - glID = ((X11GLXContext)glContext).getContext(); - properties.put(CLGLI.CL_GL_CONTEXT_KHR).put(glID) - .put(CLGLI.CL_GLX_DISPLAY_KHR).put(handle); + glID[0] = ((X11GLXContext)glContext).getContext(); + properties.put(CL_GL_CONTEXT_KHR).put(glID[0]) + .put(CL_GLX_DISPLAY_KHR).put(handle) + .put(CL_CONTEXT_PLATFORM).put(platform.ID); }else if(glContext instanceof WindowsWGLContext) { // TODO test on windows - throw new RuntimeException("cl-gl interoperability on windows not yet implemented"); + //WIN32 + //cl_context_properties props[] = { + // CL_GL_CONTEXT_KHR, (cl_context_properties)0, + // CL_WGL_HDC_KHR, (cl_context_properties)0, + // CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0}; + properties = PointerBuffer.allocateDirect(7); + long handle = config.getScreen().getDevice().getHandle(); + glID[0] = ((WindowsWGLContext)glContext).getHGLRC(); + properties.put(CL_GL_CONTEXT_KHR).put(glID[0]) + .put(CL_WGL_HDC_KHR).put(handle) + .put(CL_CONTEXT_PLATFORM).put(platform.ID); }else if(glContext instanceof MacOSXCGLContext) { // TODO test on mac - throw new RuntimeException("cl-gl interoperability on mac not yet implemented"); + //MACOSX + //cl_context_properties props[] = { + // CL_CGL_SHAREGROUP_KHR, (cl_context_properties)0, + // CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0}; + properties = PointerBuffer.allocateDirect(5); + glID[0] = ((MacOSXCGLContext)glContext).getCGLContext(); + properties.put(CL_CGL_SHAREGROUP_KHR).put(glID[0]) + .put(CL_CONTEXT_PLATFORM).put(platform.ID); }else{ throw new RuntimeException("unsupported GLContext: "+glContext); } - - properties.put(0).rewind(); // 0 terminated array - - long clID = createContextFromType(properties, CL_DEVICE_TYPE_ALL); - return new CLGLContext(clID, glID); + return properties.put(0).rewind(); // 0 terminated array } |