diff options
author | Michael Bien <[email protected]> | 2010-01-21 15:06:00 +0100 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-01-21 15:06:00 +0100 |
commit | 564b4d7e2fcd4c408ae6046710b29f3a12280574 (patch) | |
tree | 7d0a92eaf66034fd2f34e0a71c095fdd4190faba /src/com/mbien/opencl/CLGLBuffer.java | |
parent | 96251f7aa2770d2d8278afbd6e4b603c24932049 (diff) |
moved all CLGL specific methods into CLGLBuffer.
Diffstat (limited to 'src/com/mbien/opencl/CLGLBuffer.java')
-rw-r--r-- | src/com/mbien/opencl/CLGLBuffer.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/com/mbien/opencl/CLGLBuffer.java b/src/com/mbien/opencl/CLGLBuffer.java new file mode 100644 index 00000000..566f45ec --- /dev/null +++ b/src/com/mbien/opencl/CLGLBuffer.java @@ -0,0 +1,88 @@ +package com.mbien.opencl; + +import java.nio.Buffer; + + +import static com.mbien.opencl.CLGLI.*; + +/** + * Shared buffer between OpenGL and OpenCL contexts. + * @author Michael Bien + */ +public final class CLGLBuffer<B extends Buffer> extends CLBuffer<B> { + + private CLGLBuffer(CLContext context, B directBuffer, long id) { + super(context, directBuffer, id); + } + + + static <B extends Buffer> CLGLBuffer<B> create(CLContext context, B directBuffer, int flags, int glBuffer) { + 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."); + } + + CL cl = context.cl; + int[] result = new int[1]; + CLGLI clgli = (CLGLI)cl; + + long id = clgli.clCreateFromGLBuffer(context.ID, flags, glBuffer, result, 0); + + return new CLGLBuffer<B>(context, directBuffer, id); + } + + @Override + public <T extends Buffer> CLGLBuffer<T> cloneWith(T directBuffer) { + return new CLGLBuffer<T>(context, directBuffer, ID); + } + + /** + * Returns the OpenGL buffer type of this shared buffer. + */ + public GLObjectType getGLObjectType() { + int[] array = new int[1]; + int ret = ((CLGLI)cl).clGetGLObjectInfo(ID, array, 0, null, 0); + CLException.checkForError(ret, "error while asking for gl object info"); + return GLObjectType.valueOf(array[0]); + } + + /** + * Returns the OpenGL object id of this shared buffer. + */ + public int getGLObjectID() { + int[] array = new int[1]; + int ret = ((CLGLI)cl).clGetGLObjectInfo(ID, null, 0, array, 0); + CLException.checkForError(ret, "error while asking for gl object info"); + return array[0]; + } + + public enum GLObjectType { + + GL_OBJECT_BUFFER(CL_GL_OBJECT_BUFFER), + GL_OBJECT_TEXTURE2D(CL_GL_OBJECT_TEXTURE2D), + GL_OBJECT_TEXTURE3D(CL_GL_OBJECT_TEXTURE3D), + GL_OBJECT_RENDERBUFFER(CL_GL_OBJECT_RENDERBUFFER); + + public final int TYPE; + + private GLObjectType(int type) { + this.TYPE = type; + } + + public static GLObjectType valueOf(int type) { + if(type == CL_GL_OBJECT_BUFFER) + return GL_OBJECT_BUFFER; + else if(type == CL_GL_OBJECT_TEXTURE2D) + return GL_OBJECT_TEXTURE2D; + else if(type == CL_GL_OBJECT_TEXTURE3D) + return GL_OBJECT_TEXTURE3D; + else if(type == CL_GL_OBJECT_RENDERBUFFER) + return GL_OBJECT_RENDERBUFFER; + return null; + } + } + +} |