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 | |
parent | 22a5f55a5d0e4215eacda6dbf01b34cec47a5bf0 (diff) |
implemented GL interop context creation on windows and mac.
added more factory methods to CLGLContext.
Diffstat (limited to 'src')
-rw-r--r-- | src/com/mbien/opencl/CLContext.java | 50 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLGLContext.java | 117 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLPlatform.java | 10 |
3 files changed, 121 insertions, 56 deletions
diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java index ebd5821d..e75aaddd 100644 --- a/src/com/mbien/opencl/CLContext.java +++ b/src/com/mbien/opencl/CLContext.java @@ -1,5 +1,6 @@ package com.mbien.opencl; +import com.mbien.opencl.CLDevice.Type; import com.mbien.opencl.CLMemory.Mem; import com.mbien.opencl.CLSampler.AddressingMode; import com.mbien.opencl.CLSampler.FilteringMode; @@ -114,12 +115,7 @@ public class CLContext implements CLResource { */ private static final CLContext create(CLPlatform platform, CLDevice.Type... deviceTypes) { - long type = 0; - if(deviceTypes != null) { - for (int i = 0; i < deviceTypes.length; i++) { - type |= deviceTypes[i].TYPE; - } - } + long type = toDeviceBitmap(deviceTypes); PointerBuffer properties = setupContextProperties(platform); return new CLContext(createContextFromType(properties, type)); @@ -129,16 +125,10 @@ public class CLContext implements CLResource { * Creates a context on the specified platform and with the specified * devices. */ - private static final CLContext create(CLPlatform platform, CLDevice... devices) { - - long[] deviceIDs = new long[devices.length]; - - for (int i = 0; i < devices.length; i++) { - deviceIDs[i] = devices[i].ID; - } + private static final CLContext create(CLPlatform platform, CLDevice[] devices) { PointerBuffer properties = setupContextProperties(platform); - return new CLContext(createContext(properties, deviceIDs)); + return new CLContext(createContext(properties, devices)); } protected static final long createContextFromType(PointerBuffer properties, long deviceType) { @@ -151,14 +141,14 @@ public class CLContext implements CLResource { return context; } - protected static final long createContext(PointerBuffer properties, long[] devices) { + protected static final long createContext(PointerBuffer properties, CLDevice... devices) { IntBuffer status = BufferFactory.newDirectByteBuffer(4).asIntBuffer(); PointerBuffer pb = null; if(devices != null && devices.length != 0) { pb = PointerBuffer.allocateDirect(devices.length); for (int i = 0; i < devices.length; i++) { - pb.put(i, devices[i]); + pb.put(i, devices[i].ID); } } long context = CLPlatform.getLowLevelBinding().clCreateContext(properties, pb, null, null, status); @@ -171,18 +161,16 @@ public class CLContext implements CLResource { private static final PointerBuffer setupContextProperties(CLPlatform platform) { if(platform == null) { - CLPlatform[] platforms = CLPlatform.listCLPlatforms(); - if(platforms.length > 0) - platform = platforms[0]; + platform = CLPlatform.getDefault(); } - - PointerBuffer properties = null; - if(platform != null) { - properties = PointerBuffer.allocateDirect(3) - .put(CL.CL_CONTEXT_PLATFORM).put(platform.ID).put(0) // 0 terminated array - .rewind(); + + if(platform == null) { + throw new RuntimeException("no OpenCL installation found"); } - return properties; + + return PointerBuffer.allocateDirect(3).put(CL.CL_CONTEXT_PLATFORM) + .put(platform.ID).put(0) // 0 terminated array + .rewind(); } /** @@ -422,6 +410,16 @@ public class CLContext implements CLResource { return null; } + protected static long toDeviceBitmap(Type[] deviceTypes) { + long type = 0; + if (deviceTypes != null) { + for (int i = 0; i < deviceTypes.length; i++) { + type |= deviceTypes[i].TYPE; + } + } + return type; + } + @Override public String toString() { return "CLContext [id: " + ID 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 } diff --git a/src/com/mbien/opencl/CLPlatform.java b/src/com/mbien/opencl/CLPlatform.java index 80bd4d60..fc1b78f3 100644 --- a/src/com/mbien/opencl/CLPlatform.java +++ b/src/com/mbien/opencl/CLPlatform.java @@ -33,6 +33,16 @@ public final class CLPlatform { } /** + * Returns the default OpenCL platform or null when no platform found. + */ + public static CLPlatform getDefault() { + CLPlatform[] platforms = listCLPlatforms(); + if(platforms.length > 0) + return platforms[0]; + return null; + } + + /** * Lists all available OpenCL implementaitons. * @throws CLException if something went wrong initializing OpenCL */ |