From e081f13ca50353a7a9c34438a2f81d38e03e88a7 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Mon, 8 Mar 2010 17:08:03 +0100 Subject: introduced gl package and moved all interoperability functionality into it. --- src/com/mbien/opencl/CLContext.java | 4 + src/com/mbien/opencl/CLGLBuffer.java | 64 ------ src/com/mbien/opencl/CLGLContext.java | 226 -------------------- src/com/mbien/opencl/CLGLImage2d.java | 60 ------ src/com/mbien/opencl/CLGLObject.java | 24 --- src/com/mbien/opencl/CLGLTexture.java | 23 --- src/com/mbien/opencl/CLGLTexture2d.java | 62 ------ src/com/mbien/opencl/CLGLTexture3d.java | 69 ------- src/com/mbien/opencl/CLImage.java | 6 +- src/com/mbien/opencl/CLInfoAccessor.java | 4 +- src/com/mbien/opencl/CLMemory.java | 6 +- src/com/mbien/opencl/gl/CLGLBuffer.java | 68 ++++++ src/com/mbien/opencl/gl/CLGLContext.java | 229 +++++++++++++++++++++ src/com/mbien/opencl/gl/CLGLImage2d.java | 65 ++++++ src/com/mbien/opencl/gl/CLGLObject.java | 24 +++ src/com/mbien/opencl/gl/CLGLTexture.java | 23 +++ src/com/mbien/opencl/gl/CLGLTexture2d.java | 65 ++++++ src/com/mbien/opencl/gl/CLGLTexture3d.java | 74 +++++++ .../mbien/opencl/util/CLBuildConfiguration.java | 8 +- 19 files changed, 568 insertions(+), 536 deletions(-) delete mode 100644 src/com/mbien/opencl/CLGLBuffer.java delete mode 100644 src/com/mbien/opencl/CLGLContext.java delete mode 100644 src/com/mbien/opencl/CLGLImage2d.java delete mode 100644 src/com/mbien/opencl/CLGLObject.java delete mode 100644 src/com/mbien/opencl/CLGLTexture.java delete mode 100644 src/com/mbien/opencl/CLGLTexture2d.java delete mode 100644 src/com/mbien/opencl/CLGLTexture3d.java create mode 100644 src/com/mbien/opencl/gl/CLGLBuffer.java create mode 100644 src/com/mbien/opencl/gl/CLGLContext.java create mode 100644 src/com/mbien/opencl/gl/CLGLImage2d.java create mode 100644 src/com/mbien/opencl/gl/CLGLObject.java create mode 100644 src/com/mbien/opencl/gl/CLGLTexture.java create mode 100644 src/com/mbien/opencl/gl/CLGLTexture2d.java create mode 100644 src/com/mbien/opencl/gl/CLGLTexture3d.java (limited to 'src/com') diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java index 6b09cc37..2698a934 100644 --- a/src/com/mbien/opencl/CLContext.java +++ b/src/com/mbien/opencl/CLContext.java @@ -382,6 +382,10 @@ public class CLContext extends CLObject implements CLResource { release(); } + protected void overrideContext(CLDevice device) { + device.setContext(this); + } + /** * Returns the CLPlatform this context is running on. */ diff --git a/src/com/mbien/opencl/CLGLBuffer.java b/src/com/mbien/opencl/CLGLBuffer.java deleted file mode 100644 index 8b4e5f56..00000000 --- a/src/com/mbien/opencl/CLGLBuffer.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.mbien.opencl; - -import com.mbien.opencl.CLMemory.GLObjectType; -import java.nio.Buffer; - - -/** - * Shared buffer between OpenGL and OpenCL contexts. - * @author Michael Bien - */ -public final class CLGLBuffer extends CLBuffer implements CLGLObject { - - - /** - * The OpenGL object handle. - */ - public final int GLID; - - private CLGLBuffer(CLContext context, B directBuffer, long id, int glObject) { - super(context, directBuffer, id); - this.GLID = glObject; - } - - - static CLGLBuffer create(CLContext context, B directBuffer, int flags, int glObject) { - checkBuffer(directBuffer, flags); - - CL cl = context.cl; - int[] result = new int[1]; - CLGLI clgli = (CLGLI)cl; - - long id = clgli.clCreateFromGLBuffer(context.ID, flags, glObject, result, 0); - - return new CLGLBuffer(context, directBuffer, id, glObject); - } - - static void checkBuffer(B directBuffer, int flags) throws IllegalArgumentException { - if (directBuffer != null && !directBuffer.isDirect()) { - throw new IllegalArgumentException("buffer is not a direct buffer"); - } - if (isHostPointerFlag(flags)) { - throw new IllegalArgumentException("CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR can not be used with OpenGL Buffers."); - } - } - - public int getGLObjectID() { - return GLID; - } - - public GLObjectType getGLObjectType() { - return GLObjectType.GL_OBJECT_BUFFER; - } - - @Override - public CLGLBuffer cloneWith(T directBuffer) { - return new CLGLBuffer(context, directBuffer, ID, GLID); - } - - @Override - public String toString() { - return "CLGLBuffer [id: " + ID+" glID: "+GLID+"]"; - } - -} diff --git a/src/com/mbien/opencl/CLGLContext.java b/src/com/mbien/opencl/CLGLContext.java deleted file mode 100644 index 5c7226c9..00000000 --- a/src/com/mbien/opencl/CLGLContext.java +++ /dev/null @@ -1,226 +0,0 @@ -package com.mbien.opencl; - -import java.nio.Buffer; -import com.mbien.opencl.CLMemory.Mem; -import com.sun.gluegen.runtime.PointerBuffer; -import com.sun.opengl.impl.GLContextImpl; -import com.sun.opengl.impl.macosx.cgl.MacOSXCGLContext; -import com.sun.opengl.impl.windows.wgl.WindowsWGLContext; -import com.sun.opengl.impl.x11.glx.X11GLXContext; -import javax.media.nativewindow.DefaultGraphicsConfiguration; -import javax.media.opengl.GLContext; - -import static com.mbien.opencl.CLGLI.*; - -/** - * OpenCL Context supporting JOGL-JOCL interoperablity. - * @author Michael Bien - */ -public final class CLGLContext extends CLContext { - - final long glID; - - private CLGLContext(CLPlatform platform, long clContextID, long glContextID) { - super(platform, clContextID); - this.glID = glContextID; - } - - /** - * Creates a shared context on all available devices (CL_DEVICE_TYPE_ALL). - */ - public static 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); - } - - /** - * Creates a shared context on the specified platform and with the specified - * device types. - */ - 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 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. - */ - public static CLGLContext create(GLContext glContext, CLPlatform platform, CLDevice.Type... deviceTypes) { - - if(platform == null) { - platform = CLPlatform.getDefault(); - } - - long[] glID = new long[1]; - PointerBuffer properties = setupContextProperties(platform, glContext, glID); - long clID = createContextFromType(properties, toDeviceBitmap(deviceTypes)); - - return new CLGLContext(platform, clID, glID[0]); - - } - - /** - * Creates a shared context on the specified platform and with the specified - * devices. - */ - public static CLGLContext create(GLContext glContext, CLPlatform platform, CLDevice... devices) { - - if(platform == null) { - platform = CLPlatform.getDefault(); - } - - long[] glID = new long[1]; - PointerBuffer properties = setupContextProperties(platform, glContext, glID); - long clID = createContext(properties, devices); - - CLGLContext context = new CLGLContext(platform, clID, glID[0]); - if(devices != null) { - for (int i = 0; i < devices.length; i++) { - devices[i].setContext(context); - } - } - return context; - } - - - private static PointerBuffer setupContextProperties(CLPlatform platform, GLContext glContext, long[] glID) { - - if(platform == null) { - throw new RuntimeException("no OpenCL installation found"); - } - - GLContextImpl ctxImpl = (GLContextImpl)glContext; - - DefaultGraphicsConfiguration config = (DefaultGraphicsConfiguration)ctxImpl.getDrawableImpl() - .getNativeWindow().getGraphicsConfiguration().getNativeGraphicsConfiguration(); - - PointerBuffer properties; - if(glContext instanceof X11GLXContext) { - properties = PointerBuffer.allocateDirect(7); - long handle = config.getScreen().getDevice().getHandle(); - 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 - //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 - //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); - } - - return properties.put(0).rewind(); // 0 terminated array - } - - // Buffers - public final CLGLBuffer createFromGLBuffer(int glBuffer, Mem... flags) { - return createFromGLBuffer(null, glBuffer, Mem.flagsToInt(flags)); - } - - public final CLGLBuffer createFromGLBuffer(int glBuffer, int flags) { - return createFromGLBuffer(null, glBuffer, flags); - } - - public final CLGLBuffer createFromGLBuffer(B directBuffer, int glBuffer, Mem... flags) { - return createFromGLBuffer(directBuffer, glBuffer, Mem.flagsToInt(flags)); - } - - public final CLGLBuffer createFromGLBuffer(B directBuffer, int glBuffer, int flags) { - CLGLBuffer buffer = CLGLBuffer.create(this, directBuffer, flags, glBuffer); - memoryObjects.add(buffer); - return buffer; - } - - // Renderbuffers - public final CLGLImage2d createFromGLRenderbuffer(int glBuffer, Mem... flags) { - return createFromGLRenderbuffer(null, glBuffer, Mem.flagsToInt(flags)); - } - - public final CLGLImage2d createFromGLRenderbuffer(int glBuffer, int flags) { - return createFromGLRenderbuffer(null, glBuffer, flags); - } - - public final CLGLImage2d createFromGLRenderbuffer(B directBuffer, int glBuffer, Mem... flags) { - return createFromGLRenderbuffer(directBuffer, glBuffer, Mem.flagsToInt(flags)); - } - - public final CLGLImage2d createFromGLRenderbuffer(B directBuffer, int glBuffer, int flags) { - CLGLImage2d buffer = CLGLImage2d.createFromGLRenderbuffer(this, directBuffer, flags, glBuffer); - memoryObjects.add(buffer); - return buffer; - } - - //2d Textures - public final CLGLTexture2d createFromGLTexture2d(int target, int texture, int mipmap, Mem... flags) { - return createFromGLTexture2d(null, target, texture, mipmap, Mem.flagsToInt(flags)); - } - - public final CLGLTexture2d createFromGLTexture2d(int target, int texture, int mipmap, int flags) { - return createFromGLTexture2d(null, target, texture, mipmap, flags); - } - - public final CLGLTexture2d createFromGLTexture2d(B directBuffer, int target, int texture, int mipmap, Mem... flags) { - return createFromGLTexture2d(directBuffer, target, texture, mipmap, Mem.flagsToInt(flags)); - } - - public final CLGLTexture2d createFromGLTexture2d(B directBuffer, int target, int texture, int mipmap, int flags) { - CLGLTexture2d buffer = CLGLTexture2d.createFromGLTexture2d(this, directBuffer, target, texture, mipmap, flags); - memoryObjects.add(buffer); - return buffer; - } - - //3d Textures - public final CLGLTexture3d createFromGLTexture3d(int target, int texture, int mipmap, Mem... flags) { - return createFromGLTexture3d(null, target, texture, mipmap, Mem.flagsToInt(flags)); - } - - public final CLGLTexture3d createFromGLTexture3d(int target, int texture, int mipmap, int flags) { - return createFromGLTexture3d(null, target, texture, mipmap, flags); - } - - public final CLGLTexture3d createFromGLTexture3d(B directBuffer, int target, int texture, int mipmap, Mem... flags) { - return createFromGLTexture3d(directBuffer, target, texture, mipmap, Mem.flagsToInt(flags)); - } - - public final CLGLTexture3d createFromGLTexture3d(B directBuffer, int target, int texture, int mipmap, int flags) { - CLGLTexture3d buffer = CLGLTexture3d.createFromGLTexture3d(this, directBuffer, target, texture, mipmap, flags); - memoryObjects.add(buffer); - return buffer; - } - - -} diff --git a/src/com/mbien/opencl/CLGLImage2d.java b/src/com/mbien/opencl/CLGLImage2d.java deleted file mode 100644 index dc64fc55..00000000 --- a/src/com/mbien/opencl/CLGLImage2d.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.mbien.opencl; - -import com.mbien.opencl.CLMemory.GLObjectType; -import com.mbien.opencl.impl.CLImageFormatImpl; -import java.nio.Buffer; - -import static com.mbien.opencl.CL.*; - -/** - * 2D OpenCL image representing an OpenGL renderbuffer. - * @author Michael Bien - */ -public class CLGLImage2d extends CLImage2d implements CLGLObject { - - /** - * The OpenGL object handle. - */ - public final int GLID; - - protected CLGLImage2d(CLContext context, B directBuffer, CLImageFormat format, CLImageInfoAccessor accessor, int width, int height, long id, int glid) { - super(context, directBuffer, format, accessor, width, height, id); - this.GLID = glid; - } - - static CLGLImage2d createFromGLRenderbuffer(CLContext context, B directBuffer, int flags, int glObject) { - - CLGLBuffer.checkBuffer(directBuffer, flags); - - CL cl = context.cl; - int[] result = new int[1]; - CLGLI clgli = (CLGLI)cl; - - long id = clgli.clCreateFromGLRenderbuffer(context.ID, flags, glObject, result, 0); - - return createImage(context, id, directBuffer, glObject); - } - - static CLGLImage2d createImage(CLContext context, long id, B directBuffer, int glObject) { - CLImageInfoAccessor accessor = new CLImageInfoAccessor(context.cl, id); - - CLImageFormat format = new CLImageFormat(); - accessor.getInfo(CL_IMAGE_FORMAT, CLImageFormatImpl.size(), format.getFormatImpl().getBuffer(), null); - - int width = (int)accessor.getLong(CL_IMAGE_WIDTH); - int height = (int)accessor.getLong(CL_IMAGE_HEIGHT); - - return new CLGLImage2d(context, directBuffer, format, accessor, width, height, id, glObject); - } - - @Override - public GLObjectType getGLObjectType() { - return GLObjectType.GL_OBJECT_RENDERBUFFER; - } - - @Override - public int getGLObjectID() { - return GLID; - } - -} diff --git a/src/com/mbien/opencl/CLGLObject.java b/src/com/mbien/opencl/CLGLObject.java deleted file mode 100644 index 044f2274..00000000 --- a/src/com/mbien/opencl/CLGLObject.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Created on Friday, February 26 2010 - */ -package com.mbien.opencl; - -import com.mbien.opencl.CLMemory.GLObjectType; - -/** - * - * @author Michael Bien - */ -interface CLGLObject { - - /** - * Returns the OpenGL object id of this shared object. - */ - public int getGLObjectID(); - - /** - * Returns the OpenGL buffer type of this shared object. - */ - public GLObjectType getGLObjectType(); - -} diff --git a/src/com/mbien/opencl/CLGLTexture.java b/src/com/mbien/opencl/CLGLTexture.java deleted file mode 100644 index 9290dc63..00000000 --- a/src/com/mbien/opencl/CLGLTexture.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Created on Friday, February 26 2010 - */ - -package com.mbien.opencl; - -/** - * - * @author Michael Bien - */ -interface CLGLTexture { - - /** - * Returns the OpenGL texture target of this texture. - */ - public int getTextureTarget(); - - /** - * Returns the OpenGL mipmap level of this texture. - */ - public int getMipMapLevel(); - -} diff --git a/src/com/mbien/opencl/CLGLTexture2d.java b/src/com/mbien/opencl/CLGLTexture2d.java deleted file mode 100644 index d23d7e3e..00000000 --- a/src/com/mbien/opencl/CLGLTexture2d.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.mbien.opencl; - -import com.mbien.opencl.CLImage.CLImageInfoAccessor; -import com.mbien.opencl.CLMemory.GLObjectType; -import com.mbien.opencl.impl.CLImageFormatImpl; -import java.nio.Buffer; - -import static com.mbien.opencl.CL.*; - -/** - * 2D OpenCL image representing an 2D OpenGL texture. - * @author Michael Bien - */ -public class CLGLTexture2d extends CLGLImage2d implements CLGLTexture { - - public final int target; - - public final int mipMapLevel; - - public CLGLTexture2d(CLContext context, B directBuffer, CLImageFormat format, CLImageInfoAccessor accessor, int target, int mipLevel, int width, int height, long id, int glid) { - super(context, directBuffer, format, accessor, width, height, id, glid); - this.target = target; - this.mipMapLevel = mipLevel; - } - - static CLGLTexture2d createFromGLTexture2d(CLContext context, B directBuffer, int target, int texture, int mipLevel, int flags) { - - CLGLBuffer.checkBuffer(directBuffer, flags); - - CL cl = context.cl; - int[] result = new int[1]; - CLGLI clgli = (CLGLI)cl; - - long id = clgli.clCreateFromGLTexture2D(context.ID, flags, target, mipLevel, texture, result, 0); - - CLImageInfoAccessor accessor = new CLImageInfoAccessor(cl, id); - - CLImageFormat format = new CLImageFormat(); - accessor.getInfo(CL_IMAGE_FORMAT, CLImageFormatImpl.size(), format.getFormatImpl().getBuffer(), null); - - int width = (int)accessor.getLong(CL_IMAGE_WIDTH); - int height = (int)accessor.getLong(CL_IMAGE_HEIGHT); - - return new CLGLTexture2d(context, directBuffer, format, accessor, target, mipLevel, width, height, id, width); - - } - - public int getTextureTarget() { - return target; - } - - public int getMipMapLevel() { - return mipMapLevel; - } - - @Override - public GLObjectType getGLObjectType() { - return GLObjectType.GL_OBJECT_TEXTURE2D; - } - - -} diff --git a/src/com/mbien/opencl/CLGLTexture3d.java b/src/com/mbien/opencl/CLGLTexture3d.java deleted file mode 100644 index 7bc08053..00000000 --- a/src/com/mbien/opencl/CLGLTexture3d.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.mbien.opencl; - -import com.mbien.opencl.CLMemory.GLObjectType; -import com.mbien.opencl.impl.CLImageFormatImpl; -import java.nio.Buffer; - -import static com.mbien.opencl.CL.*; - -/** - * 3D OpenCL image representing an 3D OpenGL texture. - * @author Michael Bien - */ -public class CLGLTexture3d extends CLImage3d implements CLGLObject, CLGLTexture { - - /** - * The OpenGL object handle. - */ - public final int GLID; - - public final int target; - - public final int mipMapLevel; - - private CLGLTexture3d(CLContext context, B directBuffer, CLImageFormat format, CLImageInfoAccessor accessor, int target, int mipLevel, int width, int height, int depth, long id, int glid) { - super(context, directBuffer, format, accessor, width, height, depth, id); - this.GLID = glid; - this.target = target; - this.mipMapLevel = mipLevel; - } - - static CLGLTexture3d createFromGLTexture3d(CLContext context, B directBuffer, int flags, int target, int mipLevel, int texture) { - - CLGLBuffer.checkBuffer(directBuffer, flags); - - CL cl = context.cl; - int[] result = new int[1]; - CLGLI clgli = (CLGLI)cl; - - long id = clgli.clCreateFromGLTexture3D(context.ID, flags, target, mipLevel, texture, result, 0); - - CLImageInfoAccessor accessor = new CLImageInfoAccessor(cl, id); - - CLImageFormat format = new CLImageFormat(); - accessor.getInfo(CL_IMAGE_FORMAT, CLImageFormatImpl.size(), format.getFormatImpl().getBuffer(), null); - - int width = (int)accessor.getLong(CL_IMAGE_WIDTH); - int height = (int)accessor.getLong(CL_IMAGE_HEIGHT); - int depth = (int)accessor.getLong(CL_IMAGE_DEPTH); - - return new CLGLTexture3d(context, directBuffer, format, accessor, target, mipLevel, width, height, depth, id, texture); - } - - public int getGLObjectID() { - return GLID; - } - - public int getTextureTarget() { - return target; - } - - public int getMipMapLevel() { - return mipMapLevel; - } - - public GLObjectType getGLObjectType() { - return GLObjectType.GL_OBJECT_TEXTURE3D; - } - -} diff --git a/src/com/mbien/opencl/CLImage.java b/src/com/mbien/opencl/CLImage.java index 1da1cc65..449ec703 100644 --- a/src/com/mbien/opencl/CLImage.java +++ b/src/com/mbien/opencl/CLImage.java @@ -30,6 +30,10 @@ public abstract class CLImage extends CLMemory { this.height = height; } + protected static CLImageFormat createUninitializedImageFormat() { + return new CLImageFormat(); + } + /** * Returns the image format descriptor specified when image was created. */ @@ -77,7 +81,7 @@ public abstract class CLImage extends CLMemory { this.id = id; } @Override - protected int getInfo(int name, long valueSize, Buffer value, PointerBuffer valueSizeRet) { + public int getInfo(int name, long valueSize, Buffer value, PointerBuffer valueSizeRet) { return cl.clGetImageInfo(id, name, valueSize, value, valueSizeRet); } } diff --git a/src/com/mbien/opencl/CLInfoAccessor.java b/src/com/mbien/opencl/CLInfoAccessor.java index 316c1257..554545b9 100644 --- a/src/com/mbien/opencl/CLInfoAccessor.java +++ b/src/com/mbien/opencl/CLInfoAccessor.java @@ -32,7 +32,7 @@ abstract class CLInfoAccessor { }; - final long getLong(int key) { + public final long getLong(int key) { ByteBuffer buffer = localBB.get(); int ret = getInfo(key, 8, buffer, null); @@ -41,7 +41,7 @@ abstract class CLInfoAccessor { return buffer.getLong(0); } - final String getString(int key) { + public final String getString(int key) { ByteBuffer buffer = localBB.get(); PointerBuffer pbuffer = localPB.get(); diff --git a/src/com/mbien/opencl/CLMemory.java b/src/com/mbien/opencl/CLMemory.java index 275f39ff..8bebea4c 100644 --- a/src/com/mbien/opencl/CLMemory.java +++ b/src/com/mbien/opencl/CLMemory.java @@ -52,6 +52,10 @@ public abstract class CLMemory extends CLObject implements CL throw new RuntimeException("Unexpected buffer type " + buffer.getClass().getName()); } + protected static CL getCL(CLContext context) { + return context.cl; + } + /** * Returns a new instance of CLMemory pointing to the same CLResource but using a different Buffer. */ @@ -250,7 +254,7 @@ public abstract class CLMemory extends CLObject implements CL return null; } - static int flagsToInt(Mem[] flags) { + public static int flagsToInt(Mem[] flags) { int clFlags = 0; if (flags != null) { for (int i = 0; i < flags.length; i++) { diff --git a/src/com/mbien/opencl/gl/CLGLBuffer.java b/src/com/mbien/opencl/gl/CLGLBuffer.java new file mode 100644 index 00000000..54e849ff --- /dev/null +++ b/src/com/mbien/opencl/gl/CLGLBuffer.java @@ -0,0 +1,68 @@ +package com.mbien.opencl.gl; + +import com.mbien.opencl.CL; +import com.mbien.opencl.CLBuffer; +import com.mbien.opencl.CLContext; +import com.mbien.opencl.CLGLI; +import com.mbien.opencl.CLMemory.GLObjectType; +import java.nio.Buffer; + + +/** + * Shared buffer between OpenGL and OpenCL contexts. + * @author Michael Bien + */ +public final class CLGLBuffer extends CLBuffer implements CLGLObject { + + + /** + * The OpenGL object handle. + */ + public final int GLID; + + private CLGLBuffer(CLContext context, B directBuffer, long id, int glObject) { + super(context, directBuffer, id); + this.GLID = glObject; + } + + + static CLGLBuffer create(CLContext context, B directBuffer, int flags, int glObject) { + checkBuffer(directBuffer, flags); + + CL cl = getCL(context); + int[] result = new int[1]; + CLGLI clgli = (CLGLI)cl; + + long id = clgli.clCreateFromGLBuffer(context.ID, flags, glObject, result, 0); + + return new CLGLBuffer(context, directBuffer, id, glObject); + } + + static void checkBuffer(B directBuffer, int flags) throws IllegalArgumentException { + if (directBuffer != null && !directBuffer.isDirect()) { + throw new IllegalArgumentException("buffer is not a direct buffer"); + } + if (isHostPointerFlag(flags)) { + throw new IllegalArgumentException("CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR can not be used with OpenGL Buffers."); + } + } + + public int getGLObjectID() { + return GLID; + } + + public GLObjectType getGLObjectType() { + return GLObjectType.GL_OBJECT_BUFFER; + } + + @Override + public CLGLBuffer cloneWith(T directBuffer) { + return new CLGLBuffer(context, directBuffer, ID, GLID); + } + + @Override + public String toString() { + return "CLGLBuffer [id: " + ID+" glID: "+GLID+"]"; + } + +} diff --git a/src/com/mbien/opencl/gl/CLGLContext.java b/src/com/mbien/opencl/gl/CLGLContext.java new file mode 100644 index 00000000..23539049 --- /dev/null +++ b/src/com/mbien/opencl/gl/CLGLContext.java @@ -0,0 +1,229 @@ +package com.mbien.opencl.gl; + +import com.mbien.opencl.CLContext; +import com.mbien.opencl.CLDevice; +import java.nio.Buffer; +import com.mbien.opencl.CLMemory.Mem; +import com.mbien.opencl.CLPlatform; +import com.sun.gluegen.runtime.PointerBuffer; +import com.sun.opengl.impl.GLContextImpl; +import com.sun.opengl.impl.macosx.cgl.MacOSXCGLContext; +import com.sun.opengl.impl.windows.wgl.WindowsWGLContext; +import com.sun.opengl.impl.x11.glx.X11GLXContext; +import javax.media.nativewindow.DefaultGraphicsConfiguration; +import javax.media.opengl.GLContext; + +import static com.mbien.opencl.CLGLI.*; + +/** + * OpenCL Context supporting JOGL-JOCL interoperablity. + * @author Michael Bien + */ +public final class CLGLContext extends CLContext { + + final long glID; + + private CLGLContext(CLPlatform platform, long clContextID, long glContextID) { + super(platform, clContextID); + this.glID = glContextID; + } + + /** + * Creates a shared context on all available devices (CL_DEVICE_TYPE_ALL). + */ + public static 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); + } + + /** + * Creates a shared context on the specified platform and with the specified + * device types. + */ + 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 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. + */ + public static CLGLContext create(GLContext glContext, CLPlatform platform, CLDevice.Type... deviceTypes) { + + if(platform == null) { + platform = CLPlatform.getDefault(); + } + + long[] glID = new long[1]; + PointerBuffer properties = setupContextProperties(platform, glContext, glID); + long clID = createContextFromType(properties, toDeviceBitmap(deviceTypes)); + + return new CLGLContext(platform, clID, glID[0]); + + } + + /** + * Creates a shared context on the specified platform and with the specified + * devices. + */ + public static CLGLContext create(GLContext glContext, CLPlatform platform, CLDevice... devices) { + + if(platform == null) { + platform = CLPlatform.getDefault(); + } + + long[] glID = new long[1]; + PointerBuffer properties = setupContextProperties(platform, glContext, glID); + long clID = createContext(properties, devices); + + CLGLContext context = new CLGLContext(platform, clID, glID[0]); + if(devices != null) { + for (int i = 0; i < devices.length; i++) { + context.overrideContext(devices[i]); + } + } + return context; + } + + + private static PointerBuffer setupContextProperties(CLPlatform platform, GLContext glContext, long[] glID) { + + if(platform == null) { + throw new RuntimeException("no OpenCL installation found"); + } + + GLContextImpl ctxImpl = (GLContextImpl)glContext; + + DefaultGraphicsConfiguration config = (DefaultGraphicsConfiguration)ctxImpl.getDrawableImpl() + .getNativeWindow().getGraphicsConfiguration().getNativeGraphicsConfiguration(); + + PointerBuffer properties; + if(glContext instanceof X11GLXContext) { + properties = PointerBuffer.allocateDirect(7); + long handle = config.getScreen().getDevice().getHandle(); + 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 + //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 + //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); + } + + return properties.put(0).rewind(); // 0 terminated array + } + + // Buffers + public final CLGLBuffer createFromGLBuffer(int glBuffer, Mem... flags) { + return createFromGLBuffer(null, glBuffer, Mem.flagsToInt(flags)); + } + + public final CLGLBuffer createFromGLBuffer(int glBuffer, int flags) { + return createFromGLBuffer(null, glBuffer, flags); + } + + public final CLGLBuffer createFromGLBuffer(B directBuffer, int glBuffer, Mem... flags) { + return createFromGLBuffer(directBuffer, glBuffer, Mem.flagsToInt(flags)); + } + + public final CLGLBuffer createFromGLBuffer(B directBuffer, int glBuffer, int flags) { + CLGLBuffer buffer = CLGLBuffer.create(this, directBuffer, flags, glBuffer); + memoryObjects.add(buffer); + return buffer; + } + + // Renderbuffers + public final CLGLImage2d createFromGLRenderbuffer(int glBuffer, Mem... flags) { + return createFromGLRenderbuffer(null, glBuffer, Mem.flagsToInt(flags)); + } + + public final CLGLImage2d createFromGLRenderbuffer(int glBuffer, int flags) { + return createFromGLRenderbuffer(null, glBuffer, flags); + } + + public final CLGLImage2d createFromGLRenderbuffer(B directBuffer, int glBuffer, Mem... flags) { + return createFromGLRenderbuffer(directBuffer, glBuffer, Mem.flagsToInt(flags)); + } + + public final CLGLImage2d createFromGLRenderbuffer(B directBuffer, int glBuffer, int flags) { + CLGLImage2d buffer = CLGLImage2d.createFromGLRenderbuffer(this, directBuffer, flags, glBuffer); + memoryObjects.add(buffer); + return buffer; + } + + //2d Textures + public final CLGLTexture2d createFromGLTexture2d(int target, int texture, int mipmap, Mem... flags) { + return createFromGLTexture2d(null, target, texture, mipmap, Mem.flagsToInt(flags)); + } + + public final CLGLTexture2d createFromGLTexture2d(int target, int texture, int mipmap, int flags) { + return createFromGLTexture2d(null, target, texture, mipmap, flags); + } + + public final CLGLTexture2d createFromGLTexture2d(B directBuffer, int target, int texture, int mipmap, Mem... flags) { + return createFromGLTexture2d(directBuffer, target, texture, mipmap, Mem.flagsToInt(flags)); + } + + public final CLGLTexture2d createFromGLTexture2d(B directBuffer, int target, int texture, int mipmap, int flags) { + CLGLTexture2d buffer = CLGLTexture2d.createFromGLTexture2d(this, directBuffer, target, texture, mipmap, flags); + memoryObjects.add(buffer); + return buffer; + } + + //3d Textures + public final CLGLTexture3d createFromGLTexture3d(int target, int texture, int mipmap, Mem... flags) { + return createFromGLTexture3d(null, target, texture, mipmap, Mem.flagsToInt(flags)); + } + + public final CLGLTexture3d createFromGLTexture3d(int target, int texture, int mipmap, int flags) { + return createFromGLTexture3d(null, target, texture, mipmap, flags); + } + + public final CLGLTexture3d createFromGLTexture3d(B directBuffer, int target, int texture, int mipmap, Mem... flags) { + return createFromGLTexture3d(directBuffer, target, texture, mipmap, Mem.flagsToInt(flags)); + } + + public final CLGLTexture3d createFromGLTexture3d(B directBuffer, int target, int texture, int mipmap, int flags) { + CLGLTexture3d buffer = CLGLTexture3d.createFromGLTexture3d(this, directBuffer, target, texture, mipmap, flags); + memoryObjects.add(buffer); + return buffer; + } + + +} diff --git a/src/com/mbien/opencl/gl/CLGLImage2d.java b/src/com/mbien/opencl/gl/CLGLImage2d.java new file mode 100644 index 00000000..ab5f1c95 --- /dev/null +++ b/src/com/mbien/opencl/gl/CLGLImage2d.java @@ -0,0 +1,65 @@ +package com.mbien.opencl.gl; + +import com.mbien.opencl.CL; +import com.mbien.opencl.CLContext; +import com.mbien.opencl.CLGLI; +import com.mbien.opencl.CLImage2d; +import com.mbien.opencl.CLImageFormat; +import com.mbien.opencl.CLMemory.GLObjectType; +import com.mbien.opencl.impl.CLImageFormatImpl; +import java.nio.Buffer; + +import static com.mbien.opencl.CL.*; + +/** + * 2D OpenCL image representing an OpenGL renderbuffer. + * @author Michael Bien + */ +public class CLGLImage2d extends CLImage2d implements CLGLObject { + + /** + * The OpenGL object handle. + */ + public final int GLID; + + protected CLGLImage2d(CLContext context, B directBuffer, CLImageFormat format, CLImageInfoAccessor accessor, int width, int height, long id, int glid) { + super(context, directBuffer, format, accessor, width, height, id); + this.GLID = glid; + } + + static CLGLImage2d createFromGLRenderbuffer(CLContext context, B directBuffer, int flags, int glObject) { + + CLGLBuffer.checkBuffer(directBuffer, flags); + + CL cl = getCL(context); + int[] result = new int[1]; + CLGLI clgli = (CLGLI)cl; + + long id = clgli.clCreateFromGLRenderbuffer(context.ID, flags, glObject, result, 0); + + return createImage(context, id, directBuffer, glObject); + } + + static CLGLImage2d createImage(CLContext context, long id, B directBuffer, int glObject) { + CLImageInfoAccessor accessor = new CLImageInfoAccessor(getCL(context), id); + + CLImageFormat format = createUninitializedImageFormat(); + accessor.getInfo(CL_IMAGE_FORMAT, CLImageFormatImpl.size(), format.getFormatImpl().getBuffer(), null); + + int width = (int)accessor.getLong(CL_IMAGE_WIDTH); + int height = (int)accessor.getLong(CL_IMAGE_HEIGHT); + + return new CLGLImage2d(context, directBuffer, format, accessor, width, height, id, glObject); + } + + @Override + public GLObjectType getGLObjectType() { + return GLObjectType.GL_OBJECT_RENDERBUFFER; + } + + @Override + public int getGLObjectID() { + return GLID; + } + +} diff --git a/src/com/mbien/opencl/gl/CLGLObject.java b/src/com/mbien/opencl/gl/CLGLObject.java new file mode 100644 index 00000000..51a730ed --- /dev/null +++ b/src/com/mbien/opencl/gl/CLGLObject.java @@ -0,0 +1,24 @@ +/* + * Created on Friday, February 26 2010 + */ +package com.mbien.opencl.gl; + +import com.mbien.opencl.CLMemory.GLObjectType; + +/** + * + * @author Michael Bien + */ +interface CLGLObject { + + /** + * Returns the OpenGL object id of this shared object. + */ + public int getGLObjectID(); + + /** + * Returns the OpenGL buffer type of this shared object. + */ + public GLObjectType getGLObjectType(); + +} diff --git a/src/com/mbien/opencl/gl/CLGLTexture.java b/src/com/mbien/opencl/gl/CLGLTexture.java new file mode 100644 index 00000000..49fe4cb2 --- /dev/null +++ b/src/com/mbien/opencl/gl/CLGLTexture.java @@ -0,0 +1,23 @@ +/* + * Created on Friday, February 26 2010 + */ + +package com.mbien.opencl.gl; + +/** + * + * @author Michael Bien + */ +interface CLGLTexture { + + /** + * Returns the OpenGL texture target of this texture. + */ + public int getTextureTarget(); + + /** + * Returns the OpenGL mipmap level of this texture. + */ + public int getMipMapLevel(); + +} diff --git a/src/com/mbien/opencl/gl/CLGLTexture2d.java b/src/com/mbien/opencl/gl/CLGLTexture2d.java new file mode 100644 index 00000000..2dbb09d9 --- /dev/null +++ b/src/com/mbien/opencl/gl/CLGLTexture2d.java @@ -0,0 +1,65 @@ +package com.mbien.opencl.gl; + +import com.mbien.opencl.CL; +import com.mbien.opencl.CLContext; +import com.mbien.opencl.CLGLI; +import com.mbien.opencl.CLImageFormat; +import com.mbien.opencl.CLMemory.GLObjectType; +import com.mbien.opencl.impl.CLImageFormatImpl; +import java.nio.Buffer; + +import static com.mbien.opencl.CL.*; + +/** + * 2D OpenCL image representing an 2D OpenGL texture. + * @author Michael Bien + */ +public class CLGLTexture2d extends CLGLImage2d implements CLGLTexture { + + public final int target; + + public final int mipMapLevel; + + public CLGLTexture2d(CLContext context, B directBuffer, CLImageFormat format, CLImageInfoAccessor accessor, int target, int mipLevel, int width, int height, long id, int glid) { + super(context, directBuffer, format, accessor, width, height, id, glid); + this.target = target; + this.mipMapLevel = mipLevel; + } + + static CLGLTexture2d createFromGLTexture2d(CLContext context, B directBuffer, int target, int texture, int mipLevel, int flags) { + + CLGLBuffer.checkBuffer(directBuffer, flags); + + CL cl = getCL(context); + int[] result = new int[1]; + CLGLI clgli = (CLGLI)cl; + + long id = clgli.clCreateFromGLTexture2D(context.ID, flags, target, mipLevel, texture, result, 0); + + CLImageInfoAccessor accessor = new CLImageInfoAccessor(cl, id); + + CLImageFormat format = createUninitializedImageFormat(); + accessor.getInfo(CL_IMAGE_FORMAT, CLImageFormatImpl.size(), format.getFormatImpl().getBuffer(), null); + + int width = (int)accessor.getLong(CL_IMAGE_WIDTH); + int height = (int)accessor.getLong(CL_IMAGE_HEIGHT); + + return new CLGLTexture2d(context, directBuffer, format, accessor, target, mipLevel, width, height, id, width); + + } + + public int getTextureTarget() { + return target; + } + + public int getMipMapLevel() { + return mipMapLevel; + } + + @Override + public GLObjectType getGLObjectType() { + return GLObjectType.GL_OBJECT_TEXTURE2D; + } + + +} diff --git a/src/com/mbien/opencl/gl/CLGLTexture3d.java b/src/com/mbien/opencl/gl/CLGLTexture3d.java new file mode 100644 index 00000000..6c68b92a --- /dev/null +++ b/src/com/mbien/opencl/gl/CLGLTexture3d.java @@ -0,0 +1,74 @@ +package com.mbien.opencl.gl; + +import com.mbien.opencl.CL; +import com.mbien.opencl.CLContext; +import com.mbien.opencl.CLGLI; +import com.mbien.opencl.CLImage3d; +import com.mbien.opencl.CLImageFormat; +import com.mbien.opencl.CLMemory.GLObjectType; +import com.mbien.opencl.impl.CLImageFormatImpl; +import java.nio.Buffer; + +import static com.mbien.opencl.CL.*; + +/** + * 3D OpenCL image representing an 3D OpenGL texture. + * @author Michael Bien + */ +public class CLGLTexture3d extends CLImage3d implements CLGLObject, CLGLTexture { + + /** + * The OpenGL object handle. + */ + public final int GLID; + + public final int target; + + public final int mipMapLevel; + + private CLGLTexture3d(CLContext context, B directBuffer, CLImageFormat format, CLImageInfoAccessor accessor, int target, int mipLevel, int width, int height, int depth, long id, int glid) { + super(context, directBuffer, format, accessor, width, height, depth, id); + this.GLID = glid; + this.target = target; + this.mipMapLevel = mipLevel; + } + + static CLGLTexture3d createFromGLTexture3d(CLContext context, B directBuffer, int flags, int target, int mipLevel, int texture) { + + CLGLBuffer.checkBuffer(directBuffer, flags); + + CL cl = getCL(context); + int[] result = new int[1]; + CLGLI clgli = (CLGLI)cl; + + long id = clgli.clCreateFromGLTexture3D(context.ID, flags, target, mipLevel, texture, result, 0); + + CLImageInfoAccessor accessor = new CLImageInfoAccessor(cl, id); + + CLImageFormat format = createUninitializedImageFormat(); + accessor.getInfo(CL_IMAGE_FORMAT, CLImageFormatImpl.size(), format.getFormatImpl().getBuffer(), null); + + int width = (int)accessor.getLong(CL_IMAGE_WIDTH); + int height = (int)accessor.getLong(CL_IMAGE_HEIGHT); + int depth = (int)accessor.getLong(CL_IMAGE_DEPTH); + + return new CLGLTexture3d(context, directBuffer, format, accessor, target, mipLevel, width, height, depth, id, texture); + } + + public int getGLObjectID() { + return GLID; + } + + public int getTextureTarget() { + return target; + } + + public int getMipMapLevel() { + return mipMapLevel; + } + + public GLObjectType getGLObjectType() { + return GLObjectType.GL_OBJECT_TEXTURE3D; + } + +} diff --git a/src/com/mbien/opencl/util/CLBuildConfiguration.java b/src/com/mbien/opencl/util/CLBuildConfiguration.java index e65a9f98..a4d85b60 100644 --- a/src/com/mbien/opencl/util/CLBuildConfiguration.java +++ b/src/com/mbien/opencl/util/CLBuildConfiguration.java @@ -69,25 +69,25 @@ public interface CLBuildConfiguration extends Cloneable { /** * Adds the definitions to the build configuration. - * @see CLProgram#define(java.lang.String) + * @see com.mbien.opencl.CLProgram#define(java.lang.String) */ public CLBuildConfiguration withDefines(String... names); /** * Adds the definitions to the build configuration. - * @see CLProgram#define(java.lang.String, java.lang.Object) + * @see com.mbien.opencl.CLProgram#define(java.lang.String, java.lang.Object) */ public CLBuildConfiguration withDefines(Map defines); /** * Adds the compiler option to the build configuration. - * @see CLProgram.CompilerOptions + * @see com.mbien.opencl.CLProgram.CompilerOptions */ public CLBuildConfiguration withOption(String option); /** * Adds the compiler options to the build configuration. - * @see CLProgram.CompilerOptions + * @see com.mbien.opencl.CLProgram.CompilerOptions */ public CLBuildConfiguration withOptions(String... options); -- cgit v1.2.3