diff options
author | Michael Bien <[email protected]> | 2009-10-27 18:51:36 +0100 |
---|---|---|
committer | Michael Bien <[email protected]> | 2009-10-27 18:51:36 +0100 |
commit | 4df2a1b266a25c1d37126acdb82cf578ac61f9a8 (patch) | |
tree | c9543bcae35dd674d09aa47eb712b9b261638d67 | |
parent | 30cd68083930688693ebdfefdeda609d857a2c8a (diff) |
generified CLBuffer, added createFromGLBuffer(...).
-rw-r--r-- | resources/clImplCustomCode.c | 4 | ||||
-rw-r--r-- | resources/clImplCustomCode.java | 4 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLBuffer.java | 26 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLCommandQueue.java | 6 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLContext.java | 38 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLKernel.java | 2 | ||||
-rw-r--r-- | test/com/mbien/opencl/HighLevelBindingTest.java | 10 |
7 files changed, 58 insertions, 32 deletions
diff --git a/resources/clImplCustomCode.c b/resources/clImplCustomCode.c index 57d1c3e3..864edc6b 100644 --- a/resources/clImplCustomCode.c +++ b/resources/clImplCustomCode.c @@ -110,7 +110,7 @@ Java_com_mbien_opencl_impl_CLImpl_clCreateContext1(JNIEnv *env, jobject _unused, */ JNIEXPORT jint JNICALL Java_com_mbien_opencl_impl_CLImpl_clBuildProgram1(JNIEnv *env, jobject _unused, - jlong program, jint deviceCount, jobject deviceList, jint offset, jstring options, jobject cb, jobject data) { + jlong program, jint deviceCount, jobject deviceList, jstring options, jobject cb, jobject data) { const char* _strchars_options = NULL; cl_int _res; @@ -126,7 +126,7 @@ Java_com_mbien_opencl_impl_CLImpl_clBuildProgram1(JNIEnv *env, jobject _unused, } if (deviceList != NULL) { - _deviceListPtr = (void *) (((char*) (*env)->GetPrimitiveArrayCritical(env, deviceList, NULL)) + offset); + _deviceListPtr = (void *) (((char*) (*env)->GetPrimitiveArrayCritical(env, deviceList, NULL))); } // TODO payload, callback... diff --git a/resources/clImplCustomCode.java b/resources/clImplCustomCode.java index d74d5b82..c368fe53 100644 --- a/resources/clImplCustomCode.java +++ b/resources/clImplCustomCode.java @@ -46,8 +46,8 @@ if(deviceList != null) listLength = deviceList.length; - return clBuildProgram1(program, listLength, deviceList, 0, options, cb, userData); + return clBuildProgram1(program, listLength, deviceList, options, cb, userData); } /** Entry point to C language function: <code> int32_t clBuildProgram(cl_program, uint32_t, cl_device_id * , const char * , void * ); </code> */ - private native int clBuildProgram1(long program, int devices, Object deviceList, int arg2_byte_offset, String options, BuildProgramCallback cb, Object userData); + private native int clBuildProgram1(long program, int devices, Object deviceList, String options, BuildProgramCallback cb, Object userData); 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<B extends Buffer> { - 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<CLProgram> programs; - private final List<CLBuffer> buffers; + private final List<CLBuffer<? extends Buffer>> buffers; private final Map<CLDevice, List<CLCommandQueue>> queuesMap; @@ -40,7 +41,7 @@ public final class CLContext { this.cl = CLPlatform.getLowLevelBinding(); this.ID = contextID; this.programs = new ArrayList<CLProgram>(); - this.buffers = new ArrayList<CLBuffer>(); + this.buffers = new ArrayList<CLBuffer<? extends Buffer>>(); this.queuesMap = new HashMap<CLDevice, List<CLCommandQueue>>(); } @@ -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 <B extends Buffer> CLBuffer<B> 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<ByteBuffer> 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<ByteBuffer> 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 <B extends Buffer> CLBuffer<B> createBuffer(B directBuffer, int flags) { + CLBuffer<B> buffer = new CLBuffer<B>(this, directBuffer, flags); + buffers.add(buffer); + return buffer; + } + + public <B extends Buffer> CLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, Mem... flags) { + return createFromGLBuffer(directBuffer, glBuffer, Mem.flagsToInt(flags)); + } + + public <B extends Buffer> CLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, int flags) { + CLBuffer<B> buffer = new CLBuffer<B>(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<CLBuffer> getCLBuffers() { + public List<CLBuffer<? extends Buffer>> 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; diff --git a/test/com/mbien/opencl/HighLevelBindingTest.java b/test/com/mbien/opencl/HighLevelBindingTest.java index a0a2c13d..85dbda1d 100644 --- a/test/com/mbien/opencl/HighLevelBindingTest.java +++ b/test/com/mbien/opencl/HighLevelBindingTest.java @@ -106,9 +106,9 @@ public class HighLevelBindingTest { fillBuffer(srcA, 23456); fillBuffer(srcB, 46987); - CLBuffer clBufferA = context.createBuffer(srcA, Mem.READ_ONLY); - CLBuffer clBufferB = context.createBuffer(srcB, Mem.READ_ONLY); - CLBuffer clBufferC = context.createBuffer(dest, Mem.WRITE_ONLY); + CLBuffer<ByteBuffer> clBufferA = context.createBuffer(srcA, Mem.READ_ONLY); + CLBuffer<ByteBuffer> clBufferB = context.createBuffer(srcB, Mem.READ_ONLY); + CLBuffer<ByteBuffer> clBufferC = context.createBuffer(dest, Mem.WRITE_ONLY); Map<String, CLKernel> kernels = program.getCLKernels(); for (CLKernel kernel : kernels.values()) { @@ -171,8 +171,8 @@ public class HighLevelBindingTest { CLContext context = CLContext.create(); // the CL.MEM_* flag is probably completly irrelevant in our case since we do not use a kernel in this test - CLBuffer clBufferA = context.createBuffer(elements*SIZEOF_INT, Mem.READ_ONLY); - CLBuffer clBufferB = context.createBuffer(elements*SIZEOF_INT, Mem.READ_ONLY); + CLBuffer<ByteBuffer> clBufferA = context.createBuffer(elements*SIZEOF_INT, Mem.READ_ONLY); + CLBuffer<ByteBuffer> clBufferB = context.createBuffer(elements*SIZEOF_INT, Mem.READ_ONLY); // fill only first read buffer -> we will copy the payload to the second later. fillBuffer(clBufferA.buffer, 12345); |