summaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl
diff options
context:
space:
mode:
authorMichael Bien <[email protected]>2009-10-27 18:51:36 +0100
committerMichael Bien <[email protected]>2009-10-27 18:51:36 +0100
commit4df2a1b266a25c1d37126acdb82cf578ac61f9a8 (patch)
treec9543bcae35dd674d09aa47eb712b9b261638d67 /src/com/mbien/opencl
parent30cd68083930688693ebdfefdeda609d857a2c8a (diff)
generified CLBuffer, added createFromGLBuffer(...).
Diffstat (limited to 'src/com/mbien/opencl')
-rw-r--r--src/com/mbien/opencl/CLBuffer.java26
-rw-r--r--src/com/mbien/opencl/CLCommandQueue.java6
-rw-r--r--src/com/mbien/opencl/CLContext.java38
-rw-r--r--src/com/mbien/opencl/CLKernel.java2
4 files changed, 49 insertions, 23 deletions
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;