diff options
author | Michael Bien <[email protected]> | 2010-04-12 22:18:39 +0200 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-04-12 22:18:39 +0200 |
commit | bf07b44ed6a8958dd321cc4c08fd2bdd08299611 (patch) | |
tree | e24b7c4e4197a80e0ecaad75b9b3667299fd8323 /src/com/jogamp/opencl/gl | |
parent | 7680472b21ec1e2deacb49addae65c820a2e2a4d (diff) |
renamed package com.mbien.* in com.jogamp.* JOCL is now officially a JogAmp team player ;).
Diffstat (limited to 'src/com/jogamp/opencl/gl')
-rw-r--r-- | src/com/jogamp/opencl/gl/CLGLBuffer.java | 77 | ||||
-rw-r--r-- | src/com/jogamp/opencl/gl/CLGLContext.java | 250 | ||||
-rw-r--r-- | src/com/jogamp/opencl/gl/CLGLImage2d.java | 73 | ||||
-rw-r--r-- | src/com/jogamp/opencl/gl/CLGLObject.java | 35 | ||||
-rw-r--r-- | src/com/jogamp/opencl/gl/CLGLTexture.java | 23 | ||||
-rw-r--r-- | src/com/jogamp/opencl/gl/CLGLTexture2d.java | 63 | ||||
-rw-r--r-- | src/com/jogamp/opencl/gl/CLGLTexture3d.java | 86 |
7 files changed, 607 insertions, 0 deletions
diff --git a/src/com/jogamp/opencl/gl/CLGLBuffer.java b/src/com/jogamp/opencl/gl/CLGLBuffer.java new file mode 100644 index 00000000..c7f14c4c --- /dev/null +++ b/src/com/jogamp/opencl/gl/CLGLBuffer.java @@ -0,0 +1,77 @@ +package com.jogamp.opencl.gl; + +import com.jogamp.opencl.CL; +import com.jogamp.opencl.CLBuffer; +import com.jogamp.opencl.CLContext; + +import java.nio.Buffer; +import javax.media.opengl.GLContext; + + +/** + * Shared buffer between OpenGL and OpenCL contexts. + * @author Michael Bien + */ +public final class CLGLBuffer<B extends Buffer> extends CLBuffer<B> implements CLGLObject { + + + /** + * The OpenGL object handle. + */ + public final int GLID; + + private CLGLBuffer(CLContext context, B directBuffer, long id, int glObject, int flags) { + super(context, directBuffer, id, flags); + this.GLID = glObject; + } + + + static <B extends Buffer> CLGLBuffer<B> 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<B>(context, directBuffer, id, glObject, flags); + } + + static <B extends Buffer> 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 CLGLContext getContext() { + return (CLGLContext) super.getContext(); + } + + public GLContext getGLContext() { + return getContext().getGLContext(); + } + + @Override + public <T extends Buffer> CLGLBuffer<T> cloneWith(T directBuffer) { + return new CLGLBuffer<T>(context, directBuffer, ID, GLID, FLAGS); + } + + @Override + public String toString() { + return "CLGLBuffer [id: " + ID+" glID: "+GLID+"]"; + } + +} diff --git a/src/com/jogamp/opencl/gl/CLGLContext.java b/src/com/jogamp/opencl/gl/CLGLContext.java new file mode 100644 index 00000000..92618729 --- /dev/null +++ b/src/com/jogamp/opencl/gl/CLGLContext.java @@ -0,0 +1,250 @@ +package com.jogamp.opencl.gl; + +import com.jogamp.opencl.CLContext; +import com.jogamp.opencl.CLDevice; +import java.nio.Buffer; +import com.jogamp.opencl.CLMemory.Mem; +import com.jogamp.opencl.CLPlatform; +import com.jogamp.common.nio.PointerBuffer; +import com.jogamp.opengl.impl.GLContextImpl; +import com.jogamp.opengl.impl.macosx.cgl.MacOSXCGLContext; +import com.jogamp.opengl.impl.windows.wgl.WindowsWGLContext; +import com.jogamp.opengl.impl.x11.glx.X11GLXContext; +import javax.media.nativewindow.DefaultGraphicsConfiguration; +import javax.media.opengl.GLContext; + +import static com.jogamp.opencl.gl.CLGLI.*; + +/** + * OpenCL Context supporting JOGL-JOCL interoperablity. + * @author Michael Bien + */ +public final class CLGLContext extends CLContext { + + final long glID; + private final GLContext glContext; + + private CLGLContext(CLPlatform platform, GLContext glContext, long clContextID, long glContextID) { + super(platform, clContextID); + this.glID = glContextID; + this.glContext = glContext; + } + + /** + * 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, glContext, 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, glContext, 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 (PointerBuffer)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 <B extends Buffer> CLGLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, Mem... flags) { + return createFromGLBuffer(directBuffer, glBuffer, Mem.flagsToInt(flags)); + } + + public final <B extends Buffer> CLGLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, int flags) { + CLGLBuffer<B> 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 <B extends Buffer> CLGLImage2d<B> createFromGLRenderbuffer(B directBuffer, int glBuffer, Mem... flags) { + return createFromGLRenderbuffer(directBuffer, glBuffer, Mem.flagsToInt(flags)); + } + + public final <B extends Buffer> CLGLImage2d<B> createFromGLRenderbuffer(B directBuffer, int glBuffer, int flags) { + CLGLImage2d<B> 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 <B extends Buffer> CLGLTexture2d<B> createFromGLTexture2d(B directBuffer, int target, int texture, int mipmap, Mem... flags) { + return createFromGLTexture2d(directBuffer, target, texture, mipmap, Mem.flagsToInt(flags)); + } + + public final <B extends Buffer> CLGLTexture2d<B> createFromGLTexture2d(B directBuffer, int target, int texture, int mipmap, int flags) { + CLGLTexture2d<B> 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 <B extends Buffer> CLGLTexture3d<B> createFromGLTexture3d(B directBuffer, int target, int texture, int mipmap, Mem... flags) { + return createFromGLTexture3d(directBuffer, target, texture, mipmap, Mem.flagsToInt(flags)); + } + + public final <B extends Buffer> CLGLTexture3d<B> createFromGLTexture3d(B directBuffer, int target, int texture, int mipmap, int flags) { + CLGLTexture3d<B> buffer = CLGLTexture3d.createFromGLTexture3d(this, directBuffer, target, texture, mipmap, flags); + memoryObjects.add(buffer); + return buffer; + } + + /** + * Return the low level OpenCL interface with OpenGL interoperability. + */ + @Override + public CLGLI getCL() { + return (CLGLI)super.getCL(); + } + + /** + * Returns the OpenGL context this context was shared with. + */ + public GLContext getGLContext() { + return glContext; + } + + @Override + public CLGLContext getContext() { + return this; + } + +} diff --git a/src/com/jogamp/opencl/gl/CLGLImage2d.java b/src/com/jogamp/opencl/gl/CLGLImage2d.java new file mode 100644 index 00000000..82021816 --- /dev/null +++ b/src/com/jogamp/opencl/gl/CLGLImage2d.java @@ -0,0 +1,73 @@ +package com.jogamp.opencl.gl; + +import com.jogamp.opencl.CL; +import com.jogamp.opencl.CLContext; +import com.jogamp.opencl.CLImage2d; +import com.jogamp.opencl.CLImageFormat; +import com.jogamp.opencl.impl.CLImageFormatImpl; +import java.nio.Buffer; +import javax.media.opengl.GLContext; + +import static com.jogamp.opencl.CL.*; + +/** + * 2D OpenCL image representing an OpenGL renderbuffer. + * @author Michael Bien + */ +public class CLGLImage2d<B extends Buffer> extends CLImage2d<B> 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, int flags) { + super(context, directBuffer, format, accessor, width, height, id, flags); + this.GLID = glid; + } + + static <B extends Buffer> CLGLImage2d<B> 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, flags); + } + + static <B extends Buffer> CLGLImage2d<B> createImage(CLContext context, long id, B directBuffer, int glObject, int flags) { + 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<B>(context, directBuffer, format, accessor, width, height, id, glObject, flags); + } + + @Override + public GLObjectType getGLObjectType() { + return GLObjectType.GL_OBJECT_RENDERBUFFER; + } + + @Override + public int getGLObjectID() { + return GLID; + } + + @Override + public CLGLContext getContext() { + return (CLGLContext) super.getContext(); + } + + public GLContext getGLContext() { + return getContext().getGLContext(); + } + +} diff --git a/src/com/jogamp/opencl/gl/CLGLObject.java b/src/com/jogamp/opencl/gl/CLGLObject.java new file mode 100644 index 00000000..6e2aa9d2 --- /dev/null +++ b/src/com/jogamp/opencl/gl/CLGLObject.java @@ -0,0 +1,35 @@ +/* + * Created on Friday, February 26 2010 + */ +package com.jogamp.opencl.gl; + +import com.jogamp.opencl.CLMemory.GLObjectType; +import javax.media.opengl.GLContext; + +/** + * + * @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(); + + /** + * Returns the OpenCL context of this shared object. + */ + public CLGLContext getContext(); + + /** + * Returns the OpenGL context of this shared object. + */ + public GLContext getGLContext(); + +} diff --git a/src/com/jogamp/opencl/gl/CLGLTexture.java b/src/com/jogamp/opencl/gl/CLGLTexture.java new file mode 100644 index 00000000..312df80b --- /dev/null +++ b/src/com/jogamp/opencl/gl/CLGLTexture.java @@ -0,0 +1,23 @@ +/* + * Created on Friday, February 26 2010 + */ + +package com.jogamp.opencl.gl; + +/** + * + * @author Michael Bien + */ +interface CLGLTexture extends CLGLObject { + + /** + * 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/jogamp/opencl/gl/CLGLTexture2d.java b/src/com/jogamp/opencl/gl/CLGLTexture2d.java new file mode 100644 index 00000000..88b32286 --- /dev/null +++ b/src/com/jogamp/opencl/gl/CLGLTexture2d.java @@ -0,0 +1,63 @@ +package com.jogamp.opencl.gl; + +import com.jogamp.opencl.CL; +import com.jogamp.opencl.CLContext; +import com.jogamp.opencl.CLImageFormat; +import com.jogamp.opencl.impl.CLImageFormatImpl; +import java.nio.Buffer; + +import static com.jogamp.opencl.CL.*; + +/** + * 2D OpenCL image representing an 2D OpenGL texture. + * @author Michael Bien + */ +public class CLGLTexture2d<B extends Buffer> extends CLGLImage2d<B> 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, int flags) { + super(context, directBuffer, format, accessor, width, height, id, glid, flags); + this.target = target; + this.mipMapLevel = mipLevel; + } + + static <B extends Buffer> CLGLTexture2d<B> 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<B>(context, directBuffer, format, accessor, target, mipLevel, width, height, id, width, flags); + + } + + public int getTextureTarget() { + return target; + } + + public int getMipMapLevel() { + return mipMapLevel; + } + + @Override + public GLObjectType getGLObjectType() { + return GLObjectType.GL_OBJECT_TEXTURE2D; + } + + +} diff --git a/src/com/jogamp/opencl/gl/CLGLTexture3d.java b/src/com/jogamp/opencl/gl/CLGLTexture3d.java new file mode 100644 index 00000000..8731b5e7 --- /dev/null +++ b/src/com/jogamp/opencl/gl/CLGLTexture3d.java @@ -0,0 +1,86 @@ +package com.jogamp.opencl.gl; + +import com.jogamp.opencl.CL; +import com.jogamp.opencl.CLContext; +import com.jogamp.opencl.CLImage3d; +import com.jogamp.opencl.CLImageFormat; +import com.jogamp.opencl.impl.CLImageFormatImpl; +import java.nio.Buffer; +import javax.media.opengl.GLContext; + +import static com.jogamp.opencl.CL.*; + +/** + * 3D OpenCL image representing an 3D OpenGL texture. + * @author Michael Bien + */ +public class CLGLTexture3d<B extends Buffer> extends CLImage3d<B> 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, int flags) { + super(context, directBuffer, format, accessor, width, height, depth, id, flags); + this.GLID = glid; + this.target = target; + this.mipMapLevel = mipLevel; + } + + static <B extends Buffer> CLGLTexture3d<B> 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<B>(context, directBuffer, format, accessor, target, mipLevel, width, height, depth, id, texture, flags); + } + + public int getGLObjectID() { + return GLID; + } + + public int getTextureTarget() { + return target; + } + + public int getMipMapLevel() { + return mipMapLevel; + } + + public GLObjectType getGLObjectType() { + return GLObjectType.GL_OBJECT_TEXTURE3D; + } + + /** + * Returns the shared CLGLContext. + */ + @Override + public CLGLContext getContext() { + return (CLGLContext) super.getContext(); + } + + @Override + public GLContext getGLContext() { + return getContext().getGLContext(); + } + +} |