diff options
author | Michael Bien <[email protected]> | 2010-07-04 22:10:17 +0200 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-07-04 22:10:17 +0200 |
commit | 4760089650228703bab990565fa9a3bdba2edb43 (patch) | |
tree | 66d9a3cad38f9a2c8891dce22e579fc4e68c70c6 /src | |
parent | b9c7501cc93743b168f8bc938adadf0be7d4ff80 (diff) |
initial import of CLSubBuffer to HLB.
Diffstat (limited to 'src')
-rw-r--r-- | src/com/jogamp/opencl/CLBuffer.java | 55 | ||||
-rw-r--r-- | src/com/jogamp/opencl/CLSubBuffer.java | 57 |
2 files changed, 111 insertions, 1 deletions
diff --git a/src/com/jogamp/opencl/CLBuffer.java b/src/com/jogamp/opencl/CLBuffer.java index 5a2a37e..e2c74c7 100644 --- a/src/com/jogamp/opencl/CLBuffer.java +++ b/src/com/jogamp/opencl/CLBuffer.java @@ -1,15 +1,22 @@ package com.jogamp.opencl; +import com.jogamp.common.nio.Buffers; +import java.util.List; +import com.jogamp.common.nio.PointerBuffer; +import com.jogamp.opencl.CLMemory.Mem; import java.nio.Buffer; +import java.util.ArrayList; import static com.jogamp.opencl.CLException.*; /** - * + * OpenCL buffer object. * @author Michael Bien */ public class CLBuffer<B extends Buffer> extends CLMemory<B> { + private List<CLBuffer<B>> childs; + protected CLBuffer(CLContext context, long id, int flags) { super(context, id, flags); } @@ -51,6 +58,52 @@ public class CLBuffer<B extends Buffer> extends CLMemory<B> { return new CLBuffer<B>(context, directBuffer, id, flags); } + + /** + * Creates a sub buffer with the specified region from this buffer. + */ + public CLBuffer<B> createSubBuffer(int origin, int size, Mem... flags) { + PointerBuffer info = PointerBuffer.allocateDirect(2); + info.put(origin).put(size).rewind(); + int bitset = Mem.flagsToInt(flags); + + int[] err = new int[1]; + long subID = cl.clCreateSubBuffer(ID, bitset, CL.CL_BUFFER_CREATE_TYPE_REGION, info.getBuffer(), err, 0); + checkForError(err[0], "can not create sub buffer"); + + B slice = null; + if(buffer != null) { + slice = (B)Buffers.slice(buffer, origin, size); + } + + CLSubBuffer<B> clSubBuffer = new CLSubBuffer<B>(this, origin, slice, subID, bitset); + if(childs == null) { + childs = new ArrayList<CLBuffer<B>>(); + } + childs.add(clSubBuffer); + return clSubBuffer; + } + + @Override + public void release() { + if(childs != null) { + for (CLBuffer<B> child : childs) { + child.release(); + } + } + super.release(); + } + + void onReleaseSubBuffer(CLSubBuffer sub) { + childs.remove(sub); + } + + /** + * Returns true if this is a sub buffer. + */ + public boolean isSubBuffer() { + return false; + } @Override public <T extends Buffer> CLBuffer<T> cloneWith(T directBuffer) { diff --git a/src/com/jogamp/opencl/CLSubBuffer.java b/src/com/jogamp/opencl/CLSubBuffer.java new file mode 100644 index 0000000..614dfa8 --- /dev/null +++ b/src/com/jogamp/opencl/CLSubBuffer.java @@ -0,0 +1,57 @@ +package com.jogamp.opencl; + +import com.jogamp.opencl.CLMemory.Mem; +import java.nio.Buffer; + +/** + * A sub buffer of a CLBuffer. + * @author Michael Bien + */ +public class CLSubBuffer<B extends Buffer> extends CLBuffer<B> { + + private CLBuffer<B> parent; + private final int offset; + + CLSubBuffer(CLBuffer<B> parent, int origin, B directBuffer, long id, int flags) { + super(parent.getContext(), directBuffer, id, flags); + this.parent = parent; + this.offset = origin; + } + + /** + * Throws an UnsupportedOperationException since creating sub buffers + * from sub buffers is not allowed. + */ + @Override + public CLBuffer<B> createSubBuffer(int origin, int size, Mem... flags) { + throw new UnsupportedOperationException("creating sub buffers from sub buffers is not allowed."); + } + + @Override + public void release() { + parent.onReleaseSubBuffer(this); + super.release(); + } + + /** + * Returns the parent buffer this buffer was created from. + */ + public CLBuffer<B> getParent() { + return parent; + } + + /** + * Returns the offset of this sub buffer to its parent. + */ + public int getOffset() { + return offset; + } + + /** + * Returns true. + */ + @Override + public boolean isSubBuffer() { + return true; + } +} |