diff options
Diffstat (limited to 'src/com/jogamp/opencl/CLKernel.java')
-rw-r--r-- | src/com/jogamp/opencl/CLKernel.java | 66 |
1 files changed, 56 insertions, 10 deletions
diff --git a/src/com/jogamp/opencl/CLKernel.java b/src/com/jogamp/opencl/CLKernel.java index b2d783ee..135174c4 100644 --- a/src/com/jogamp/opencl/CLKernel.java +++ b/src/com/jogamp/opencl/CLKernel.java @@ -31,11 +31,12 @@ package com.jogamp.opencl; import com.jogamp.opencl.util.CLUtil; import com.jogamp.common.nio.Buffers; import com.jogamp.common.nio.PointerBuffer; +import com.jogamp.opencl.llb.CLKernelBinding; import java.nio.Buffer; import java.nio.ByteBuffer; import static com.jogamp.opencl.CLException.*; -import static com.jogamp.opencl.CL.*; +import static com.jogamp.opencl.llb.CL.*; import static com.jogamp.common.os.Platform.*; /** @@ -49,12 +50,13 @@ import static com.jogamp.common.os.Platform.*; * @see CLProgram#createCLKernels() * @author Michael Bien */ -public class CLKernel extends CLObject implements CLResource, Cloneable { +public class CLKernel extends CLObjectResource implements Cloneable { public final String name; public final int numArgs; private final CLProgram program; + private final CLKernelBinding binding; private final ByteBuffer buffer; @@ -71,15 +73,17 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { this.program = program; this.buffer = Buffers.newDirectByteBuffer((is32Bit()?4:8)*3); + binding = program.getPlatform().getKernelBinding(); + if(name == null) { // get function name PointerBuffer size = PointerBuffer.wrap(buffer); - int ret = cl.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, 0, null, size); + int ret = binding.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, 0, null, size); checkForError(ret, "error while asking for kernel function name"); ByteBuffer bb = Buffers.newDirectByteBuffer((int)size.get(0)); - ret = cl.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb, null); + ret = binding.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb, null); checkForError(ret, "error while asking for kernel function name"); this.name = CLUtil.clString2JavaString(bb, bb.capacity()); @@ -88,7 +92,7 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { } // get number of arguments - int ret = cl.clGetKernelInfo(ID, CL_KERNEL_NUM_ARGS, buffer.capacity(), buffer, null); + int ret = binding.clGetKernelInfo(ID, CL_KERNEL_NUM_ARGS, buffer.capacity(), buffer, null); checkForError(ret, "error while asking for number of function arguments."); numArgs = buffer.getInt(0); @@ -106,6 +110,12 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { return this; } + public CLKernel putArg(short value) { + setArg(argIndex, value); + argIndex++; + return this; + } + public CLKernel putArg(int value) { setArg(argIndex, value); argIndex++; @@ -167,6 +177,11 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { return this; } + public CLKernel setArg(int argumentIndex, short value) { + setArgument(argumentIndex, 2, wrap(value)); + return this; + } + public CLKernel setArg(int argumentIndex, int value) { setArgument(argumentIndex, 4, wrap(value)); return this; @@ -205,6 +220,31 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { return this; } + public CLKernel setArgs(Object... values) { + if(values == null || values.length == 0) { + throw new IllegalArgumentException("values array was empty or null."); + } + for (int i = 0; i < values.length; i++) { + Object value = values[i]; + if(value instanceof CLMemory<?>) { + setArg(i, (CLMemory<?>)value); + }else if(value instanceof Short) { + setArg(i, (Short)value); + }else if(value instanceof Integer) { + setArg(i, (Integer)value); + }else if(value instanceof Long) { + setArg(i, (Long)value); + }else if(value instanceof Float) { + setArg(i, (Float)value); + }else if(value instanceof Double) { + setArg(i, (Double)value); + }else{ + throw new IllegalArgumentException(value + " is not a valid argument."); + } + } + return this; + } + private void setArgs(int startIndex, CLMemory<?>... values) { for (int i = 0; i < values.length; i++) { setArg(i+startIndex, values[i]); @@ -221,7 +261,7 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { " arguments for a not executable program. "+program); } - int ret = cl.clSetKernelArg(ID, argumentIndex, size, value); + int ret = binding.clSetKernelArg(ID, argumentIndex, size, value); if(ret != CL_SUCCESS) { throw newException(ret, "error setting arg "+argumentIndex+" to value "+value+" of size "+size+" of "+this); } @@ -255,6 +295,10 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { return buffer.putDouble(0, value); } + private Buffer wrap(short value) { + return buffer.putShort(0, value); + } + private Buffer wrap(int value) { return buffer.putInt(0, value); } @@ -293,7 +337,7 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { * The returned array has always three elements. */ public long[] getCompileWorkGroupSize(CLDevice device) { - int ret = cl.clGetKernelWorkGroupInfo(ID, device.ID, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, (is32Bit()?4:8)*3, buffer, null); + int ret = binding.clGetKernelWorkGroupInfo(ID, device.ID, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, (is32Bit()?4:8)*3, buffer, null); if(ret != CL_SUCCESS) { throw newException(ret, "error while asking for CL_KERNEL_COMPILE_WORK_GROUP_SIZE of "+this+" on "+device); } @@ -306,7 +350,7 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { } private long getWorkGroupInfo(CLDevice device, int flag) { - int ret = cl.clGetKernelWorkGroupInfo(ID, device.ID, flag, 8, buffer, null); + int ret = binding.clGetKernelWorkGroupInfo(ID, device.ID, flag, 8, buffer, null); if(ret != CL_SUCCESS) { throw newException(ret, "error while asking for clGetKernelWorkGroupInfo of "+this+" on "+device); } @@ -316,10 +360,12 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { /** * Releases all resources of this kernel from its context. */ + @Override public void release() { - int ret = cl.clReleaseKernel(ID); + super.release(); + int ret = binding.clReleaseKernel(ID); program.onKernelReleased(this); - if(ret != CL.CL_SUCCESS) { + if(ret != CL_SUCCESS) { throw newException(ret, "can not release "+this); } } |