From 195a5a69be1d2662c94291c19867c517d5b75591 Mon Sep 17 00:00:00 2001 From: shamanDevel Date: Mon, 18 Apr 2016 17:38:38 +0200 Subject: [PATCH] added first version of the cl wrapper in CL.java, contains a subset of the OpenCL specification --- .../src/main/java/com/jme3/opencl/Buffer.java | 4 + .../src/main/java/com/jme3/opencl/CL.java | 143 ++++++++++++++ .../java/com/jme3/opencl/CommandQueue.java | 4 + .../main/java/com/jme3/opencl/Context.java | 4 + .../src/main/java/com/jme3/opencl/Device.java | 174 ++++++++++++++++++ .../opencl/KernelCompilationException.java | 48 +++++ .../java/com/jme3/opencl/OpenCLException.java | 71 +++++++ 7 files changed, 448 insertions(+) create mode 100644 jme3-core/src/main/java/com/jme3/opencl/CL.java create mode 100644 jme3-core/src/main/java/com/jme3/opencl/Device.java create mode 100644 jme3-core/src/main/java/com/jme3/opencl/KernelCompilationException.java create mode 100644 jme3-core/src/main/java/com/jme3/opencl/OpenCLException.java diff --git a/jme3-core/src/main/java/com/jme3/opencl/Buffer.java b/jme3-core/src/main/java/com/jme3/opencl/Buffer.java index 71d80f843..ca1f86409 100644 --- a/jme3-core/src/main/java/com/jme3/opencl/Buffer.java +++ b/jme3-core/src/main/java/com/jme3/opencl/Buffer.java @@ -127,4 +127,8 @@ public class Buffer { } //TODO: async mapping + + //TODO: clEnqueueFillBuffer + + //TODO: image read/write } diff --git a/jme3-core/src/main/java/com/jme3/opencl/CL.java b/jme3-core/src/main/java/com/jme3/opencl/CL.java new file mode 100644 index 000000000..71ae9d5b5 --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/opencl/CL.java @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2009-2016 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.opencl; + +import java.nio.*; + +/** + * Interface for OpenCL implementations + * @author Sebastian Weiss + */ +public interface CL { + + //temp buffers for argument passing + public static class TempBuffer { + /** + * 16-Bytes (4 floats) of a byte buffer + */ + public final ByteBuffer b16; + /** + * Short-buffer view on b16 + */ + public final ShortBuffer b16s; + /** + * int buffer view on b16 + */ + public final IntBuffer b16i; + /** + * long buffer view on b16 + */ + public final LongBuffer b16l; + /** + * float buffer view on b16 + */ + public final FloatBuffer b16f; + /** + * double buffer view on b16 + */ + public final DoubleBuffer b16d; + + public TempBuffer(ByteBuffer b16) { + this.b16 = b16; + b16.rewind(); + this.b16s = b16.asShortBuffer(); + this.b16i = b16.asIntBuffer(); + this.b16l = b16.asLongBuffer(); + this.b16f = b16.asFloatBuffer(); + this.b16d = b16.asDoubleBuffer(); + } + } + /** + * Returns up to 4 temp buffer instances + * @return + */ + TempBuffer[] getTempBuffers(); + + //entry point + long getContext(); + + //OpenCL functions + long clCreateCommandQueue(long context, long device, boolean profiling); + void clReleaseCommandQueue(long queue); + + long clCreateBuffer(long context, MemoryAccess access, int size, ByteBuffer hostPtr); + long clEnqueueReadBuffer(long queue, long buffer, boolean blocking, int offset, int size, ByteBuffer ptr); + long clEnqueueWriteBuffer(long queue, long buffer, boolean blocking, int offset, int size, ByteBuffer ptr); + long clEnqueueCopyBuffer(long queue, long srcBuffer, long dstBuffer, int srcOffset, int dstOffset, int size); + long clEnqueueFillBuffer(long queue, long buffer, ByteBuffer pattern, int patternSize, int offset, int size); + long clEnqueueMapBuffer(long queue, long buffer, boolean blocking, MappingAccess flags, int offset, int size); + + long clCreateImage(long context, MemoryAccess access, Context.ImageFormat format, Context.ImageDescriptor desc, ByteBuffer ptr); + Context.ImageFormat[] clGetSupportedImageFormats(long context, MemoryAccess ma, Context.ImageType type); + + long clEnqueueReadImage(long queue, long image, boolean blocking, ByteBuffer origin, ByteBuffer region, int rowPitch, int slicePitch, ByteBuffer ptr); + long clEnqueueWriteImage(long queue, long image, boolean blocking, ByteBuffer origin, ByteBuffer region, int inputRowPitch, int intputSlicePitch, ByteBuffer ptr); + long clEnqueueCopyImage(long queue, long srcImage, long dstImage, ByteBuffer srcOrigin, ByteBuffer dstOrigin, ByteBuffer region); + long clEnqueueFillImage(long queue, long image, ByteBuffer fillColor, ByteBuffer origin, ByteBuffer region); + + long clEnqueueCopyImageToBuffer(long queue, long srcImage, long dstBuffer, ByteBuffer srcOrigin, ByteBuffer region, int dstOffset); + long clEnqueueCopyBufferToImage(long queue, long srcBuffer, long dstImage, int srcOffset, ByteBuffer dstOrigin, ByteBuffer region); + + long clEnqueueMapImage(long queue, long image, boolean blocking, MappingAccess ma, ByteBuffer origin, ByteBuffer region, int rowPitch, int slicePitch); + //TODO: clGetImageInfo + + void clReleaseMemObject(long mem); + long clEnqueueUnmapMemObject(long queue, long mem, ByteBuffer ptr); + int getMemSize(long mem); //uses clGetMemObjectInfo + + long clCreateProgramWithSource(long context, CharSequence[] sources); + //TODO: create from binary + long clReleaseProgram(long program); + + void clBuildProgram(long program, long[] devices, CharSequence optpions) throws KernelCompilationException; + String getKernelNames(long program); //uses clGetProgramInfo + + long clCreateKernel(long program, String kernelName); + void clReleaseKernel(long kernel); + void clSetKernelArg(long kernel, int argIndex, int argSize, ByteBuffer argValue); + String getKernelName(long kernel); //uses clGetKernelInfo + int getKernelNumArgs(long kernel); //uses clGetKernelInfo + //TODO: clGetKernelWorkGroupInfo + + long clEnqueueNDRangeKernel(long queue, long kernel, int workDim, + ByteBuffer globalWorkOffset, ByteBuffer globalWorkSize, ByteBuffer localWorkSize); + + void clWaitForEvents(long[] events); + void clWaitForEvent(long event); + boolean isEventCompleted(long event); //uses clGetEventInfo + void clReleaseEvent(long event); + + void clFlush(long queue); + void clFinish(long queue); + + +} diff --git a/jme3-core/src/main/java/com/jme3/opencl/CommandQueue.java b/jme3-core/src/main/java/com/jme3/opencl/CommandQueue.java index a25675e7a..6354677fa 100644 --- a/jme3-core/src/main/java/com/jme3/opencl/CommandQueue.java +++ b/jme3-core/src/main/java/com/jme3/opencl/CommandQueue.java @@ -119,6 +119,10 @@ public final class CommandQueue { //TODO: async mapping + //TODO: clEnqueueFillBuffer + + //TODO: image read/write + public void flush() { throw new UnsupportedOperationException("not supported yet"); } diff --git a/jme3-core/src/main/java/com/jme3/opencl/Context.java b/jme3-core/src/main/java/com/jme3/opencl/Context.java index e8d1c3c2c..4e3063fc9 100644 --- a/jme3-core/src/main/java/com/jme3/opencl/Context.java +++ b/jme3-core/src/main/java/com/jme3/opencl/Context.java @@ -52,6 +52,10 @@ public final class Context { this.context = context; } + public List getDevices() { + throw new UnsupportedOperationException("not supported yet"); + } + public CommandQueue createQueue() { throw new UnsupportedOperationException("not supported yet"); } diff --git a/jme3-core/src/main/java/com/jme3/opencl/Device.java b/jme3-core/src/main/java/com/jme3/opencl/Device.java new file mode 100644 index 000000000..045b44ecb --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/opencl/Device.java @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2009-2016 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.opencl; + +import java.util.Collection; + +/** + * + * @author Sebastian Weiss + */ +public final class Device { + private final long device; + + public Device(long device) { + this.device = device; + } + + public static enum DeviceType { + DEFAULT, + CPU, + GPU, + ACCELEARTOR, + ALL + } + public DeviceType getDeviceType() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getVendorId() { + throw new UnsupportedOperationException("not supported yet"); + } + public boolean isAvailable() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getPCIeBus() { + throw new UnsupportedOperationException("not supported yet"); + } + + public boolean hasCompiler() { + throw new UnsupportedOperationException("not supported yet"); + } + public boolean hasDouble() { + throw new UnsupportedOperationException("not supported yet"); + } + public boolean hasHalfFloat() { + throw new UnsupportedOperationException("not supported yet"); + } + public boolean hasErrorCorrectingMemory() { + throw new UnsupportedOperationException("not supported yet"); + } + public boolean hasUnifiedMemory() { + throw new UnsupportedOperationException("not supported yet"); + } + public boolean hasImageSupport() { + throw new UnsupportedOperationException("not supported yet"); + } + public boolean hasExtension(String extension) { + throw new UnsupportedOperationException("not supported yet"); + } + public Collection getExtensions() { + throw new UnsupportedOperationException("not supported yet"); + } + + public int getComputeUnits() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getClockFrequency() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getAddressBits() { + throw new UnsupportedOperationException("not supported yet"); + } + public boolean isLittleEndian() { + throw new UnsupportedOperationException("not supported yet"); + } + + public int getMaximumWorkItemDimensions() { + throw new UnsupportedOperationException("not supported yet"); + } + public int[] getMaximumWorkItemSizes() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getMaxiumWorkItemsPerGroup() { + throw new UnsupportedOperationException("not supported yet"); + } + + public int getMaximumSamplers() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getMaximumReadImages() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getMaximumWriteImages() { + throw new UnsupportedOperationException("not supported yet"); + } + public int[] getMaximumImage2DSize() { + throw new UnsupportedOperationException("not supported yet"); + } + public int[] getMaximumImage3DSize() { + throw new UnsupportedOperationException("not supported yet"); + } + + //TODO: cache, prefered sizes properties + + public String getProfile() { + throw new UnsupportedOperationException("not supported yet"); + } + public String getVersion() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getVersionMajor() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getVersionMinor() { + throw new UnsupportedOperationException("not supported yet"); + } + public String getCompilerVersion() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getCompilerVersionMajor() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getCompilerVersionMinor() { + throw new UnsupportedOperationException("not supported yet"); + } + public String getDriverVersion() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getDriverVersionMajor() { + throw new UnsupportedOperationException("not supported yet"); + } + public int getDriverVersionMinor() { + throw new UnsupportedOperationException("not supported yet"); + } + public String getName() { + throw new UnsupportedOperationException("not supported yet"); + } + public String getVendor() { + throw new UnsupportedOperationException("not supported yet"); + } + + @Override + public String toString() { + throw new UnsupportedOperationException("not supported yet"); + } +} diff --git a/jme3-core/src/main/java/com/jme3/opencl/KernelCompilationException.java b/jme3-core/src/main/java/com/jme3/opencl/KernelCompilationException.java new file mode 100644 index 000000000..37d7a07b7 --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/opencl/KernelCompilationException.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2009-2016 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.opencl; + + +public class KernelCompilationException extends OpenCLException { + + private final String log; + + public KernelCompilationException(String msg, int errorCode, String log) { + super(msg, errorCode); + this.log = log; + } + + public String getLog() { + return log; + } + +} \ No newline at end of file diff --git a/jme3-core/src/main/java/com/jme3/opencl/OpenCLException.java b/jme3-core/src/main/java/com/jme3/opencl/OpenCLException.java new file mode 100644 index 000000000..b0603b36a --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/opencl/OpenCLException.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2009-2016 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.opencl; + +/** + * + * @author Sebastian Weiss + */ +public class OpenCLException extends Exception { + + private final int errorCode; + + /** + * Creates a new instance of OpenCLExceptionn without detail + * message. + */ + public OpenCLException() { + errorCode = 0; + } + + /** + * Constructs an instance of OpenCLExceptionn with the + * specified detail message. + * + * @param msg the detail message. + */ + public OpenCLException(String msg) { + super(msg); + errorCode = 0; + } + + public OpenCLException(String msg, int errorCode) { + super(msg); + this.errorCode = errorCode; + } + + public int getErrorCode() { + return errorCode; + } + + +}