diff options
Diffstat (limited to 'src/com/mbien')
-rw-r--r-- | src/com/mbien/opencl/CLBuffer.java | 31 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLGLBuffer.java | 88 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLGLContext.java | 48 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLMemory.java | 8 |
4 files changed, 107 insertions, 68 deletions
diff --git a/src/com/mbien/opencl/CLBuffer.java b/src/com/mbien/opencl/CLBuffer.java index e42b9064..fd8611e5 100644 --- a/src/com/mbien/opencl/CLBuffer.java +++ b/src/com/mbien/opencl/CLBuffer.java @@ -8,46 +8,31 @@ import static com.mbien.opencl.CLException.*; * * @author Michael Bien */ -public final class CLBuffer<B extends Buffer> extends CLMemory<B> { +public class CLBuffer<B extends Buffer> extends CLMemory<B> { - private CLBuffer(CLContext context, B directBuffer, long id) { + protected CLBuffer(CLContext context, B directBuffer, long id) { super(context, directBuffer, id); } static <B extends Buffer> CLBuffer<B> create(CLContext context, B directBuffer, int flags) { - return create(context, directBuffer, flags, 0); - } - - static <B extends Buffer> CLBuffer<B> create(CLContext context, B directBuffer, int flags, int glBuffer) { if(directBuffer != null && !directBuffer.isDirect()) throw new IllegalArgumentException("buffer is not a direct buffer"); + B host_ptr = null; CL cl = context.cl; - long id; - int[] result = new int[1]; - if(glBuffer == 0) { - B host_ptr = null; - if(isHostPointerFlag(flags)) { - host_ptr = directBuffer; - } - id = cl.clCreateBuffer(context.ID, flags, sizeOfBufferElem(directBuffer)*directBuffer.capacity(), host_ptr, result, 0); - }else{ - if(isHostPointerFlag(flags)) { - throw new IllegalArgumentException( - "CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR can not be used with OpenGL Buffers."); - } - CLGLI clgli = (CLGLI)cl; - id = clgli.clCreateFromGLBuffer(context.ID, flags, glBuffer, result, 0); + if(isHostPointerFlag(flags)) { + host_ptr = directBuffer; } + long id = cl.clCreateBuffer(context.ID, flags, sizeOfBufferElem(directBuffer)*directBuffer.capacity(), host_ptr, result, 0); checkForError(result[0], "can not create cl buffer"); - + return new CLBuffer<B>(context, directBuffer, id); } - + @Override public <T extends Buffer> CLBuffer<T> cloneWith(T directBuffer) { return new CLBuffer<T>(context, directBuffer, ID); 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; + } + } + +} diff --git a/src/com/mbien/opencl/CLGLContext.java b/src/com/mbien/opencl/CLGLContext.java index 48efb6c0..03367a35 100644 --- a/src/com/mbien/opencl/CLGLContext.java +++ b/src/com/mbien/opencl/CLGLContext.java @@ -76,56 +76,14 @@ public final class CLGLContext extends CLContext { } - public final <B extends Buffer> CLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, Mem... 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> CLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, int flags) { - CLBuffer<B> buffer = CLBuffer.create(this, directBuffer, flags, glBuffer); + 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; } - // TODO move somewhere else - public GLObjectType getGLObjectType(CLBuffer<?> buffer) { - int[] array = new int[1]; - int ret = ((CLGLI)cl).clGetGLObjectInfo(buffer.ID, array, 0, null, 0); - CLException.checkForError(ret, "error while asking for gl object info"); - return GLObjectType.valueOf(array[0]); - } - - public int getGLObjectID(CLBuffer<?> buffer) { - int[] array = new int[1]; - int ret = ((CLGLI)cl).clGetGLObjectInfo(buffer.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; - } - } - - } diff --git a/src/com/mbien/opencl/CLMemory.java b/src/com/mbien/opencl/CLMemory.java index 86ad66d4..836987e6 100644 --- a/src/com/mbien/opencl/CLMemory.java +++ b/src/com/mbien/opencl/CLMemory.java @@ -1,6 +1,7 @@ package com.mbien.opencl; import com.sun.gluegen.runtime.BufferFactory; +import com.sun.gluegen.runtime.PointerBuffer; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.DoubleBuffer; @@ -85,6 +86,13 @@ public abstract class CLMemory <B extends Buffer> implements CLResource { return sizeOfBufferElem(buffer) * buffer.capacity(); } + public long getCLSize() { + PointerBuffer pb = PointerBuffer.allocateDirect(1); + int ret = cl.clGetMemObjectInfo(ID, CL.CL_MEM_SIZE, PointerBuffer.elementSize(), pb.getBuffer(), null); + checkForError(ret, "can not optain buffer info"); + return pb.get(); + } + public void release() { int ret = cl.clReleaseMemObject(ID); context.onMemoryReleased(this); |