From 4df2a1b266a25c1d37126acdb82cf578ac61f9a8 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Tue, 27 Oct 2009 18:51:36 +0100 Subject: generified CLBuffer, added createFromGLBuffer(...). --- src/com/mbien/opencl/CLBuffer.java | 26 ++++++++++++++-------- src/com/mbien/opencl/CLCommandQueue.java | 6 ++--- src/com/mbien/opencl/CLContext.java | 38 +++++++++++++++++++++++--------- src/com/mbien/opencl/CLKernel.java | 2 +- 4 files changed, 49 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/com/mbien/opencl/CLBuffer.java b/src/com/mbien/opencl/CLBuffer.java index 7c195ba7..223b074b 100644 --- a/src/com/mbien/opencl/CLBuffer.java +++ b/src/com/mbien/opencl/CLBuffer.java @@ -1,22 +1,26 @@ package com.mbien.opencl; -import java.nio.ByteBuffer; +import java.nio.Buffer; import static com.mbien.opencl.CLException.*; /** * * @author Michael Bien */ -public class CLBuffer { +public class CLBuffer { - public final ByteBuffer buffer; + public final B buffer; public final long ID; private final CLContext context; private final CL cl; - CLBuffer(CLContext context, ByteBuffer directBuffer, int flags) { - + CLBuffer(CLContext context, B directBuffer, int flags) { + this(context, directBuffer, 0, flags); + } + + CLBuffer(CLContext context, B directBuffer, int glBuffer, int flags) { + if(!directBuffer.isDirect()) throw new IllegalArgumentException("buffer is not a direct buffer"); @@ -26,10 +30,14 @@ public class CLBuffer { int[] intArray = new int[1]; - this.ID = cl.clCreateBuffer(context.ID, flags, directBuffer.capacity(), null, intArray, 0); - + if(glBuffer == 0) { + this.ID = cl.clCreateBuffer(context.ID, flags, directBuffer.capacity(), null, intArray, 0); + }else{ + CLGLI clgli = (CLGLI)cl; + this.ID = clgli.clCreateFromGLBuffer(context.ID, flags, glBuffer, intArray, 0); + } checkForError(intArray[0], "can not create cl buffer"); - + } public void release() { @@ -46,7 +54,7 @@ public class CLBuffer { if (getClass() != obj.getClass()) { return false; } - final CLBuffer other = (CLBuffer) obj; + final CLBuffer other = (CLBuffer) obj; if (this.buffer != other.buffer && (this.buffer == null || !this.buffer.equals(other.buffer))) { return false; } diff --git a/src/com/mbien/opencl/CLCommandQueue.java b/src/com/mbien/opencl/CLCommandQueue.java index 00e1d6b0..28c4bad0 100644 --- a/src/com/mbien/opencl/CLCommandQueue.java +++ b/src/com/mbien/opencl/CLCommandQueue.java @@ -30,7 +30,7 @@ public class CLCommandQueue { throw new CLException(status[0], "can not create command queue on "+device); } - public CLCommandQueue putWriteBuffer(CLBuffer writeBuffer, boolean blockingWrite) { + public CLCommandQueue putWriteBuffer(CLBuffer writeBuffer, boolean blockingWrite) { int ret = cl.clEnqueueWriteBuffer( ID, writeBuffer.ID, blockingWrite ? CL.CL_TRUE : CL.CL_FALSE, @@ -44,7 +44,7 @@ public class CLCommandQueue { return this; } - public CLCommandQueue putReadBuffer(CLBuffer readBuffer, boolean blockingRead) { + public CLCommandQueue putReadBuffer(CLBuffer readBuffer, boolean blockingRead) { int ret = cl.clEnqueueReadBuffer( ID, readBuffer.ID, blockingRead ? CL.CL_TRUE : CL.CL_FALSE, @@ -64,7 +64,7 @@ public class CLCommandQueue { return this; } - public CLCommandQueue putCopyBuffer(CLBuffer src, CLBuffer dest, long bytesToCopy) { + public CLCommandQueue putCopyBuffer(CLBuffer src, CLBuffer dest, long bytesToCopy) { int ret = cl.clEnqueueCopyBuffer( ID, src.ID, dest.ID, src.buffer.position(), dest.buffer.position(), bytesToCopy, // 0, null, null); //TODO solve NPE in gluegen when PointerBuffer == null diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java index 68ff131c..66716e41 100644 --- a/src/com/mbien/opencl/CLContext.java +++ b/src/com/mbien/opencl/CLContext.java @@ -7,6 +7,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; @@ -32,7 +33,7 @@ public final class CLContext { private CLDevice[] devices; private final List programs; - private final List buffers; + private final List> buffers; private final Map> queuesMap; @@ -40,7 +41,7 @@ public final class CLContext { this.cl = CLPlatform.getLowLevelBinding(); this.ID = contextID; this.programs = new ArrayList(); - this.buffers = new ArrayList(); + this.buffers = new ArrayList>(); this.queuesMap = new HashMap>(); } @@ -194,22 +195,39 @@ public final class CLContext { /** * Creates a CLBuffer with the specified flags. No flags creates a MEM.READ_WRITE buffer. */ - public CLBuffer createBuffer(ByteBuffer directBuffer, Mem... flags) { + public CLBuffer createBuffer(B directBuffer, Mem... flags) { return createBuffer(directBuffer, Mem.flagsToInt(flags)); } + /** - * Creates a CLBuffer with the specified flags. No flags creates a MEM.READ_WRITE buffer. + * Creates a CLBuffer with the specified flags and buffer size in bytes. No flags creates a MEM.READ_WRITE buffer. */ - public CLBuffer createBuffer(int size, Mem... flags) { + public CLBuffer createBuffer(int size, Mem... flags) { return createBuffer(size, Mem.flagsToInt(flags)); } - public CLBuffer createBuffer(int size, int flags) { + /** + * Creates a CLBuffer with the specified flags and buffer size in bytes. + */ + public CLBuffer createBuffer(int size, int flags) { return createBuffer(BufferFactory.newDirectByteBuffer(size), flags); } - public CLBuffer createBuffer(ByteBuffer directBuffer, int flags) { - CLBuffer buffer = new CLBuffer(this, directBuffer, flags); + /** + * Creates a CLBuffer with the specified flags. + */ + public CLBuffer createBuffer(B directBuffer, int flags) { + CLBuffer buffer = new CLBuffer(this, directBuffer, flags); + buffers.add(buffer); + return buffer; + } + + public CLBuffer createFromGLBuffer(B directBuffer, int glBuffer, Mem... flags) { + return createFromGLBuffer(directBuffer, glBuffer, Mem.flagsToInt(flags)); + } + + public CLBuffer createFromGLBuffer(B directBuffer, int glBuffer, int flags) { + CLBuffer buffer = new CLBuffer(this, directBuffer, glBuffer, flags); buffers.add(buffer); return buffer; } @@ -232,7 +250,7 @@ public final class CLContext { programs.remove(program); } - void onBufferReleased(CLBuffer buffer) { + void onBufferReleased(CLBuffer buffer) { buffers.remove(buffer); } @@ -270,7 +288,7 @@ public final class CLContext { /** * Returns a read only view of all buffers associated with this context. */ - public List getCLBuffers() { + public List> getCLBuffers() { return Collections.unmodifiableList(buffers); } diff --git a/src/com/mbien/opencl/CLKernel.java b/src/com/mbien/opencl/CLKernel.java index 3388251b..21b176c7 100644 --- a/src/com/mbien/opencl/CLKernel.java +++ b/src/com/mbien/opencl/CLKernel.java @@ -38,7 +38,7 @@ public class CLKernel { } - public CLKernel setArg(int argumentIndex, CLBuffer value) { + public CLKernel setArg(int argumentIndex, CLBuffer value) { int ret = cl.clSetKernelArg(ID, argumentIndex, CPU.is32Bit()?4:8, wrap(value.ID)); checkForError(ret, "error on clSetKernelArg"); return this; -- cgit v1.2.3