diff --git a/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java b/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java index 991ad25c0..35c3c3911 100644 --- a/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java +++ b/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java @@ -457,4 +457,10 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex } }); } + + @Override + public com.jme3.opencl.Context getOpenCLContext() { + logger.warning("OpenCL is not yet supported on android"); + return null; + } } diff --git a/jme3-examples/src/main/java/jme3test/opencl/TestVertexBufferSharing.java b/jme3-examples/src/main/java/jme3test/opencl/TestVertexBufferSharing.java index 295b7c354..514a41d63 100644 --- a/jme3-examples/src/main/java/jme3test/opencl/TestVertexBufferSharing.java +++ b/jme3-examples/src/main/java/jme3test/opencl/TestVertexBufferSharing.java @@ -146,12 +146,12 @@ public class TestVertexBufferSharing extends SimpleApplication { program = clContext.createProgramFromSourceCode(source); program.build(); //Save binary - ByteBuffer bb = program.getBinary(device); - byte[] bytes = new byte[bb.remaining()]; - bb.get(bytes); try { + ByteBuffer bb = program.getBinary(device); + byte[] bytes = new byte[bb.remaining()]; + bb.get(bytes); Files.write(binaryFile.toPath(), bytes); - } catch (IOException ex) { + } catch (UnsupportedOperationException | OpenCLException | IOException ex) { LOG.log(Level.SEVERE, "Unable to save program binaries", ex); } LOG.info("create new program from sources"); diff --git a/jme3-ios/src/main/java/com/jme3/system/ios/IGLESContext.java b/jme3-ios/src/main/java/com/jme3/system/ios/IGLESContext.java index 2d046550e..14ab252a5 100644 --- a/jme3-ios/src/main/java/com/jme3/system/ios/IGLESContext.java +++ b/jme3-ios/src/main/java/com/jme3/system/ios/IGLESContext.java @@ -36,6 +36,7 @@ import com.jme3.input.dummy.DummyKeyInput; import com.jme3.input.dummy.DummyMouseInput; import com.jme3.system.*; import com.jme3.input.ios.IosInputHandler; +import com.jme3.opencl.Context; import com.jme3.renderer.ios.IosGL; import com.jme3.renderer.opengl.GL; import com.jme3.renderer.opengl.GLDebugES; @@ -212,4 +213,10 @@ public class IGLESContext implements JmeContext { } } } + + @Override + public Context getOpenCLContext() { + logger.warning("OpenCL not yet supported on this platform"); + return null; + } } \ No newline at end of file diff --git a/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglEvent.java b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglEvent.java index 1078c8c17..b7f992c95 100644 --- a/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglEvent.java +++ b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglEvent.java @@ -32,7 +32,6 @@ package com.jme3.opencl.lwjgl; import com.jme3.opencl.Event; -import com.jme3.opencl.OpenCLObjectManager; import java.util.logging.Logger; import org.lwjgl.opencl.CL10; import org.lwjgl.opencl.CLEvent; @@ -44,16 +43,10 @@ import org.lwjgl.opencl.CLEvent; public class LwjglEvent extends Event { private static final Logger LOG = Logger.getLogger(LwjglEvent.class.getName()); private CLEvent event; - private ReleaserImpl releaser; public LwjglEvent(CLEvent event) { super(new ReleaserImpl(event)); this.event = event; - if (event == null) { - LOG.warning("event is null!"); - } else { - this.releaser = new ReleaserImpl(event); - } } public CLEvent getEvent() { diff --git a/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglBuffer.java b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglBuffer.java new file mode 100644 index 000000000..f91cf0692 --- /dev/null +++ b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglBuffer.java @@ -0,0 +1,235 @@ +/* + * 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.lwjgl; + +import com.jme3.opencl.*; +import java.nio.ByteBuffer; +import org.lwjgl.opencl.*; + +/** + * + * @author shaman + */ +public class LwjglBuffer extends Buffer { + + private final long buffer; + + public LwjglBuffer(long buffer) { + super(new ReleaserImpl(buffer)); + this.buffer = buffer; + } + public long getBuffer() { + return buffer; + } + + @Override + public long getSize() { + return Info.clGetMemObjectInfoLong(buffer, CL10.CL_MEM_SIZE); + } + + @Override + public MemoryAccess getMemoryAccessFlags() { + return Utils.getMemoryAccessFromFlag(Info.clGetMemObjectInfoLong(buffer, CL10.CL_MEM_FLAGS)); + } + + @Override + public void read(CommandQueue queue, ByteBuffer dest, long size, long offset) { + //Note: LWJGL does not support the size parameter, I have to set the buffer limit + dest.limit((int) (dest.position() + size)); + int ret = CL10.clEnqueueReadBuffer(((LwjglCommandQueue)queue).getQueue(), + buffer, CL10.CL_TRUE, offset, dest, null, null); + Utils.checkError(ret, "clEnqueueReadBuffer"); + } + + @Override + public Event readAsync(CommandQueue queue, ByteBuffer dest, long size, long offset) { + //Note: LWJGL does not support the size parameter, I have to set the buffer limit + dest.limit((int) (dest.position() + size)); + Utils.pointerBuffers[0].rewind(); + long q = ((LwjglCommandQueue)queue).getQueue(); + int ret = CL10.clEnqueueReadBuffer(q, buffer, CL10.CL_FALSE, offset, dest, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueReadBuffer"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + + @Override + public void write(CommandQueue queue, ByteBuffer src, long size, long offset) { + //Note: LWJGL does not support the size parameter, I have to set the buffer limit + src.limit((int) (src.position() + size)); + long q = ((LwjglCommandQueue)queue).getQueue(); + int ret = CL10.clEnqueueWriteBuffer(q, buffer, CL10.CL_TRUE, offset, src, null, null); + Utils.checkError(ret, "clEnqueueWriteBuffer"); + } + + @Override + public Event writeAsync(CommandQueue queue, ByteBuffer src, long size, long offset) { + //Note: LWJGL does not support the size parameter, I have to set the buffer limit + src.limit((int) (src.position() + size)); + Utils.pointerBuffers[0].rewind(); + long q = ((LwjglCommandQueue)queue).getQueue(); + int ret = CL10.clEnqueueWriteBuffer(q, buffer, CL10.CL_FALSE, offset, src, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueWriteBuffer"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + + @Override + public void copyTo(CommandQueue queue, Buffer dest, long size, long srcOffset, long destOffset) { + long q = ((LwjglCommandQueue)queue).getQueue(); + Utils.pointerBuffers[0].rewind(); + int ret = CL10.clEnqueueCopyBuffer(q, buffer, ((LwjglBuffer) dest).buffer, srcOffset, destOffset, size, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueCopyBuffer"); + long event = Utils.pointerBuffers[0].get(0); + ret = CL10.clWaitForEvents(event); + Utils.checkError(ret, "clWaitForEvents"); + } + + @Override + public Event copyToAsync(CommandQueue queue, Buffer dest, long size, long srcOffset, long destOffset) { + long q = ((LwjglCommandQueue)queue).getQueue(); + Utils.pointerBuffers[0].rewind(); + int ret = CL10.clEnqueueCopyBuffer(q, buffer, ((LwjglBuffer) dest).buffer, srcOffset, destOffset, size, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueCopyBuffer"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + + @Override + public ByteBuffer map(CommandQueue queue, long size, long offset, MappingAccess access) { + long q = ((LwjglCommandQueue) queue).getQueue(); + long flags = Utils.getMappingAccessFlags(access); + Utils.errorBuffer.rewind(); + ByteBuffer b = CL10.clEnqueueMapBuffer(q, buffer, CL10.CL_TRUE, flags, offset, size, null, null, Utils.errorBuffer, null); + Utils.checkError(Utils.errorBuffer, "clEnqueueMapBuffer"); + return b; + } + + @Override + public void unmap(CommandQueue queue, ByteBuffer ptr) { + long q = ((LwjglCommandQueue) queue).getQueue(); + Utils.pointerBuffers[0].rewind(); + int ret = CL10.clEnqueueUnmapMemObject(q, buffer, ptr, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueUnmapMemObject"); + long event = Utils.pointerBuffers[0].get(0); + ret = CL10.clWaitForEvents(event); + Utils.checkError(ret, "clWaitForEvents"); + } + + @Override + public com.jme3.opencl.Buffer.AsyncMapping mapAsync(CommandQueue queue, long size, long offset, MappingAccess access) { + Utils.pointerBuffers[0].rewind(); + Utils.errorBuffer.rewind(); + long q = ((LwjglCommandQueue) queue).getQueue(); + long flags = Utils.getMappingAccessFlags(access); + ByteBuffer buf = CL10.clEnqueueMapBuffer(q, buffer, CL10.CL_FALSE, flags, offset, size, null, Utils.pointerBuffers[0], Utils.errorBuffer, null); + Utils.checkError(Utils.errorBuffer, "clEnqueueMapBuffer"); + long event = Utils.pointerBuffers[0].get(0); + return new com.jme3.opencl.Buffer.AsyncMapping(new LwjglEvent(event), buf); + } + + @Override + public Event fillAsync(CommandQueue queue, ByteBuffer pattern, long size, long offset) { + Utils.pointerBuffers[0].rewind(); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL12.clEnqueueFillBuffer(q, buffer, pattern, offset, size, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueFillBuffer"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + + @Override + public Event copyToImageAsync(CommandQueue queue, Image dest, long srcOffset, long[] destOrigin, long[] destRegion) { + if (destOrigin.length!=3 || destRegion.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[0].rewind(); + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[1].put(destOrigin).position(0); + Utils.pointerBuffers[2].put(destRegion).position(0); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10.clEnqueueCopyBufferToImage(q, buffer, ((LwjglImage) dest).getImage(), + srcOffset, Utils.pointerBuffers[1], Utils.pointerBuffers[2], null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueCopyBufferToImage"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + + @Override + public Event acquireBufferForSharingAsync(CommandQueue queue) { + Utils.pointerBuffers[0].rewind(); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10GL.clEnqueueAcquireGLObjects(q, buffer, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueAcquireGLObjects"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + @Override + public void acquireBufferForSharingNoEvent(CommandQueue queue) { + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10GL.clEnqueueAcquireGLObjects(q, buffer, null, null); + Utils.checkError(ret, "clEnqueueAcquireGLObjects"); + } + + @Override + public Event releaseBufferForSharingAsync(CommandQueue queue) { + Utils.pointerBuffers[0].rewind(); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10GL.clEnqueueReleaseGLObjects(q, buffer, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueReleaseGLObjects"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + @Override + public void releaseBufferForSharingNoEvent(CommandQueue queue) { + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10GL.clEnqueueReleaseGLObjects(q, buffer, null, null); + Utils.checkError(ret, "clEnqueueReleaseGLObjects"); + } + + private static class ReleaserImpl implements ObjectReleaser { + private long mem; + private ReleaserImpl(long mem) { + this.mem = mem; + } + @Override + public void release() { + if (mem != 0) { + int ret = CL10.clReleaseMemObject(mem); + mem = 0; + Utils.reportError(ret, "clReleaseMemObject"); + } + } + + } +} diff --git a/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglCommandQueue.java b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglCommandQueue.java new file mode 100644 index 000000000..5be22813a --- /dev/null +++ b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglCommandQueue.java @@ -0,0 +1,80 @@ +/* + * 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.lwjgl; + +import com.jme3.opencl.CommandQueue; +import org.lwjgl.opencl.CL10; + +/** + * + * @author shaman + */ +public class LwjglCommandQueue extends CommandQueue { + + private final long queue; + + public LwjglCommandQueue(long queue) { + super(new ReleaserImpl(queue)); + this.queue = queue; + } + + public long getQueue() { + return queue; + } + + @Override + public void flush() { + int ret = CL10.clFlush(queue); + Utils.checkError(ret, "clFlush"); + } + + @Override + public void finish() { + int ret = CL10.clFinish(queue); + Utils.checkError(ret, "clFinish"); + } + + private static class ReleaserImpl implements ObjectReleaser { + private long queue; + private ReleaserImpl(long queue) { + this.queue = queue; + } + @Override + public void release() { + if (queue != 0) { + int ret = CL10.clReleaseCommandQueue(queue); + queue = 0; + Utils.reportError(ret, "clReleaseCommandQueue"); + } + } + } +} diff --git a/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglContext.java b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglContext.java new file mode 100644 index 000000000..e8c2aca26 --- /dev/null +++ b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglContext.java @@ -0,0 +1,252 @@ +/* + * 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.lwjgl; + +import com.jme3.opencl.*; +import com.jme3.opencl.Context; +import com.jme3.opencl.Image.ImageDescriptor; +import com.jme3.opencl.Image.ImageFormat; +import com.jme3.scene.VertexBuffer; +import com.jme3.texture.FrameBuffer; +import com.jme3.texture.Texture; +import java.nio.ByteBuffer; +import java.nio.IntBuffer; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.lwjgl.BufferUtils; +import org.lwjgl.opencl.*; +import org.lwjgl.opengl.*; + +/** + * + * @author shaman + */ +public class LwjglContext extends Context { + private static final Logger LOG = Logger.getLogger(LwjglContext.class.getName()); + private final long context; + private final List devices; + + public LwjglContext(long context, List devices) { + super(new ReleaserImpl(context, devices)); + this.context = context; + this.devices = devices; + } + + public long getContext() { + return context; + } + + @Override + public List getDevices() { + return devices; + } + + @Override + @SuppressWarnings("element-type-mismatch") + public CommandQueue createQueue(Device device) { + assert (devices.contains(device)); //this also ensures that device is a LwjglDevice + long d = ((LwjglDevice) device).getDevice(); + long properties = 0; + long q = CL10.clCreateCommandQueue(context, d, properties, Utils.errorBuffer); + Utils.checkError(Utils.errorBuffer, "clCreateCommandQueue"); + return new LwjglCommandQueue(q); + } + + @Override + public Buffer createBuffer(long size, MemoryAccess access) { + long flags = Utils.getMemoryAccessFlags(access); + long mem = CL10.clCreateBuffer(context, flags, size, Utils.errorBuffer); + Utils.checkError(Utils.errorBuffer, "clCreateBuffer"); + return new LwjglBuffer(mem); + } + + @Override + public Buffer createBufferFromHost(ByteBuffer data, MemoryAccess access) { + long flags = Utils.getMemoryAccessFlags(access); + flags |= CL10.CL_MEM_USE_HOST_PTR; + long mem = CL10.clCreateBuffer(context, flags, data, Utils.errorBuffer); + Utils.checkError(Utils.errorBuffer, "clCreateBuffer"); + return new LwjglBuffer(mem); + } + + @Override + public Image createImage(MemoryAccess access, ImageFormat format, ImageDescriptor descr) { + long memFlags = Utils.getMemoryAccessFlags(access); + Utils.errorBuffer.rewind(); + + CLImageFormat f = null; + CLImageDesc d = null; + try { + f = CLImageFormat.malloc(); + d = CLImageDesc.calloc(); + f.image_channel_data_type(LwjglImage.decodeImageChannelType(format.channelType)); + f.image_channel_order(LwjglImage.decodeImageChannelOrder(format.channelOrder)); + d.image_type(LwjglImage.decodeImageType(descr.type)); + d.image_width(descr.width); + d.image_height(descr.height); + d.image_depth(descr.depth); + d.image_array_size(descr.arraySize); + d.image_row_pitch(descr.rowPitch); + d.image_slice_pitch(descr.slicePitch); + //create image + long mem = CL12.clCreateImage(context, memFlags, f, d, descr.hostPtr, Utils.errorBuffer); + Utils.checkError(Utils.errorBuffer, "clCreateImage"); + return new LwjglImage(mem); + } finally { + if (f != null) { + f.free(); + } + if (d != null) { + d.free(); + } + } + } + + @Override + public ImageFormat[] querySupportedFormats(MemoryAccess access, Image.ImageType type) { + long memFlags = Utils.getMemoryAccessFlags(access); + int typeFlag = LwjglImage.decodeImageType(type); + Utils.tempBuffers[0].b16i.rewind(); + //query count + int ret = CL10.clGetSupportedImageFormats(context, memFlags, typeFlag, null, Utils.tempBuffers[0].b16i); + Utils.checkError(ret, "clGetSupportedImageFormats"); + int count = Utils.tempBuffers[0].b16i.get(0); + if (count == 0) { + return new ImageFormat[0]; + } + //get formats + CLImageFormat.Buffer formatsB = new CLImageFormat.Buffer(BufferUtils.createByteBuffer(count * CLImageFormat.SIZEOF)); + ret = CL10.clGetSupportedImageFormats(context, memFlags, typeFlag, formatsB, null); + Utils.checkError(ret, "clGetSupportedImageFormats"); + //convert formats + ImageFormat[] formats = new ImageFormat[count]; + for (int i=0; i devices; + private ReleaserImpl(long mem, List devices) { + this.context = mem; + this.devices = devices; + } + @Override + public void release() { + if (context != 0) { + int ret = CL10.clReleaseContext(context); + context = 0; + devices.clear(); + Utils.reportError(ret, "clReleaseMemObject"); + } + } + + } +} diff --git a/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglDevice.java b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglDevice.java new file mode 100644 index 000000000..31325a469 --- /dev/null +++ b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglDevice.java @@ -0,0 +1,303 @@ +/* + * 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.lwjgl; + +import com.jme3.opencl.Device; +import com.jme3.opencl.Platform; +import java.util.Arrays; +import java.util.Collection; +import org.lwjgl.PointerBuffer; +import org.lwjgl.opencl.CL10; +import org.lwjgl.opencl.CL11; +import org.lwjgl.opencl.CLDevice; +import org.lwjgl.opencl.Info; + +/** + * + * @author shaman + */ +public final class LwjglDevice implements Device { + + final CLDevice device; + final LwjglPlatform platform; + + public LwjglDevice(CLDevice device, LwjglPlatform platform) { + this.device = device; + this.platform = platform; + } + + public long getDevice() { + return device.address(); + } + public CLDevice getCLDevice() { + return device; + } + + @Override + public LwjglPlatform getPlatform() { + return platform; + } + + @Override + public DeviceType getDeviceType() { + int type = Info.clGetDeviceInfoInt(device.address(), CL10.CL_DEVICE_TYPE); + switch (type) { + case CL10.CL_DEVICE_TYPE_ACCELERATOR: return DeviceType.ACCELEARTOR; + case CL10.CL_DEVICE_TYPE_CPU: return DeviceType.CPU; + case CL10.CL_DEVICE_TYPE_GPU: return DeviceType.GPU; + default: return DeviceType.DEFAULT; + } + } + + @Override + public int getVendorId() { + return Info.clGetDeviceInfoInt(device.address(), CL10.CL_DEVICE_VENDOR_ID); + } + + @Override + public boolean isAvailable() { + return Info.clGetDeviceInfoBoolean(device.address(), CL10.CL_DEVICE_AVAILABLE); + } + + @Override + public boolean hasCompiler() { + return Info.clGetDeviceInfoBoolean(device.address(), CL10.CL_DEVICE_COMPILER_AVAILABLE); + } + + @Override + public boolean hasDouble() { + return hasExtension("cl_khr_fp64"); + } + + @Override + public boolean hasHalfFloat() { + return hasExtension("cl_khr_fp16"); + } + + @Override + public boolean hasErrorCorrectingMemory() { + return Info.clGetDeviceInfoBoolean(device.address(), CL10.CL_DEVICE_ERROR_CORRECTION_SUPPORT); + } + + @Override + public boolean hasUnifiedMemory() { + return Info.clGetDeviceInfoBoolean(device.address(), CL11.CL_DEVICE_HOST_UNIFIED_MEMORY); + } + + @Override + public boolean hasImageSupport() { + return Info.clGetDeviceInfoBoolean(device.address(), CL10.CL_DEVICE_IMAGE_SUPPORT); + } + + @Override + public boolean hasWritableImage3D() { + return hasExtension("cl_khr_3d_image_writes"); + } + + @Override + public boolean hasOpenGLInterop() { + return hasExtension("cl_khr_gl_sharing"); + } + + @Override + public boolean hasExtension(String extension) { + return getExtensions().contains(extension); + } + + @Override + public Collection getExtensions() { + return Arrays.asList(Info.clGetDeviceInfoStringASCII(device.address(), CL10.CL_DEVICE_EXTENSIONS).split(" ")); + } + + @Override + public int getComputeUnits() { + return Info.clGetDeviceInfoInt(device.address(), CL10.CL_DEVICE_MAX_COMPUTE_UNITS); + } + + @Override + public int getClockFrequency() { + return Info.clGetDeviceInfoInt(device.address(), CL10.CL_DEVICE_MAX_CLOCK_FREQUENCY); + } + + @Override + public int getAddressBits() { + return Info.clGetDeviceInfoInt(device.address(), CL10.CL_DEVICE_ADDRESS_BITS); + } + + @Override + public boolean isLittleEndian() { + return Info.clGetDeviceInfoBoolean(device.address(), CL10.CL_DEVICE_ENDIAN_LITTLE); + } + + @Override + public long getMaximumWorkItemDimensions() { + return Info.clGetDeviceInfoInt(device.address(), CL10.CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS); + } + + @Override + public long[] getMaximumWorkItemSizes() { + int dim = (int) getMaximumWorkItemDimensions(); + PointerBuffer sizes = PointerBuffer.allocateDirect(dim); + Info.clGetDeviceInfoPointers(device.address(), CL10.CL_DEVICE_MAX_WORK_ITEM_SIZES, sizes); + long[] sx = new long[dim]; + sizes.get(sx); + return sx; + } + + @Override + public long getMaxiumWorkItemsPerGroup() { + return Info.clGetDeviceInfoPointer(device.address(), CL10.CL_DEVICE_MAX_WORK_GROUP_SIZE); + } + + @Override + public int getMaximumSamplers() { + return Info.clGetDeviceInfoInt(device.address(), CL10.CL_DEVICE_MAX_SAMPLERS); + } + + @Override + public int getMaximumReadImages() { + return Info.clGetDeviceInfoInt(device.address(), CL10.CL_DEVICE_MAX_READ_IMAGE_ARGS); + } + + @Override + public int getMaximumWriteImages() { + return Info.clGetDeviceInfoInt(device.address(), CL10.CL_DEVICE_MAX_WRITE_IMAGE_ARGS); + } + + @Override + public long[] getMaximumImage2DSize() { + return new long[] { + Info.clGetDeviceInfoPointer(device.address(), CL10.CL_DEVICE_IMAGE2D_MAX_WIDTH), + Info.clGetDeviceInfoPointer(device.address(), CL10.CL_DEVICE_IMAGE2D_MAX_HEIGHT) + }; + } + + @Override + public long[] getMaximumImage3DSize() { + return new long[] { + Info.clGetDeviceInfoPointer(device.address(), CL10.CL_DEVICE_IMAGE3D_MAX_WIDTH), + Info.clGetDeviceInfoPointer(device.address(), CL10.CL_DEVICE_IMAGE3D_MAX_HEIGHT), + Info.clGetDeviceInfoPointer(device.address(), CL10.CL_DEVICE_IMAGE3D_MAX_DEPTH) + }; + } + + @Override + public long getMaximumAllocationSize() { + return Info.clGetDeviceInfoLong(device.address(), CL10.CL_DEVICE_MAX_MEM_ALLOC_SIZE); + } + + @Override + public long getGlobalMemorySize() { + return Info.clGetDeviceInfoLong(device.address(), CL10.CL_DEVICE_GLOBAL_MEM_SIZE); + } + + @Override + public long getLocalMemorySize() { + return Info.clGetDeviceInfoLong(device.address(), CL10.CL_DEVICE_LOCAL_MEM_SIZE); + } + + @Override + public long getMaximumConstantBufferSize() { + return Info.clGetDeviceInfoLong(device.address(), CL10.CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE); + } + + @Override + public int getMaximumConstantArguments() { + return Info.clGetDeviceInfoInt(device.address(), CL10.CL_DEVICE_MAX_CONSTANT_ARGS); + } + + @Override + public String getProfile() { + return Info.clGetDeviceInfoStringASCII(device.address(), CL10.CL_DEVICE_PROFILE); + } + + @Override + public String getVersion() { + return Info.clGetDeviceInfoStringASCII(device.address(), CL10.CL_DEVICE_VERSION); + } + + @Override + public int getVersionMajor() { + return Utils.getMajorVersion(getVersion(), "OpenCL "); + } + + @Override + public int getVersionMinor() { + return Utils.getMinorVersion(getVersion(), "OpenCL "); + } + + @Override + public String getCompilerVersion() { + return Info.clGetDeviceInfoStringASCII(device.address(), CL11.CL_DEVICE_OPENCL_C_VERSION); + } + + @Override + public int getCompilerVersionMajor() { + return Utils.getMajorVersion(getCompilerVersion(), "OpenCL C "); + } + + @Override + public int getCompilerVersionMinor() { + return Utils.getMinorVersion(getCompilerVersion(), "OpenCL C "); + } + + @Override + public String getDriverVersion() { + return Info.clGetDeviceInfoStringASCII(device.address(), CL10.CL_DRIVER_VERSION); + } + + @Override + public int getDriverVersionMajor() { + return Utils.getMajorVersion(getDriverVersion(), ""); + } + + @Override + public int getDriverVersionMinor() { + return Utils.getMinorVersion(getDriverVersion(), ""); + } + + @Override + public String getName() { + return Info.clGetDeviceInfoStringASCII(device.address(), CL10.CL_DEVICE_NAME); + } + + @Override + public String getVendor() { + return Info.clGetDeviceInfoStringASCII(device.address(), CL10.CL_DEVICE_VENDOR); + } + + @Override + public String toString() { + return getName(); + } + +} diff --git a/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglEvent.java b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglEvent.java new file mode 100644 index 000000000..5ded851d3 --- /dev/null +++ b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglEvent.java @@ -0,0 +1,94 @@ +/* + * 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.lwjgl; + +import com.jme3.opencl.Event; +import java.util.logging.Logger; +import org.lwjgl.opencl.CL10; +import org.lwjgl.opencl.Info; + +/** + * + * @author shaman + */ +public class LwjglEvent extends Event { + private static final Logger LOG = Logger.getLogger(LwjglEvent.class.getName()); + private long event; + + public LwjglEvent(long event) { + super(new ReleaserImpl(event)); + this.event = event; + } + + public long getEvent() { + return event; + } + + @Override + public void waitForFinished() { + CL10.clWaitForEvents(event); + release(); //short cut to save resources + } + + @Override + public boolean isCompleted() { + int status = Info.clGetEventInfoInt(event, CL10.CL_EVENT_COMMAND_EXECUTION_STATUS); + if (status == CL10.CL_SUCCESS) { + release(); //short cut to save resources + return true; + } else if (status < 0) { + Utils.checkError(status, "EventStatus"); + return false; + } else { + return false; + } + } + + private static class ReleaserImpl implements ObjectReleaser { + private long event; + + private ReleaserImpl(long event) { + this.event = event; + } + + @Override + public void release() { + if (event != 0) { + int ret = CL10.clReleaseEvent(event); + event = 0; + Utils.reportError(ret, "clReleaseEvent"); + LOG.finer("Event deleted"); + } + } + + } +} diff --git a/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglImage.java b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglImage.java new file mode 100644 index 000000000..ff9606c13 --- /dev/null +++ b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglImage.java @@ -0,0 +1,576 @@ +/* + * 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.lwjgl; + +import com.jme3.math.ColorRGBA; +import com.jme3.opencl.*; +import java.nio.ByteBuffer; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.lwjgl.opencl.*; + +/** + * + * @author shaman + */ +public class LwjglImage extends Image { + private static final Logger LOG = Logger.getLogger(LwjglImage.class.getName()); + + private final long image; + + public LwjglImage(long image) { + super(new ReleaserImpl(image)); + this.image = image; + } + + public long getImage() { + return image; + } + + public static int decodeImageChannelOrder(ImageChannelOrder order) { + switch (order) { + case A: + return CL10.CL_A; + case ARGB: + return CL10.CL_ARGB; + case BGRA: + return CL10.CL_BGRA; + case INTENSITY: + return CL10.CL_INTENSITY; + case LUMINANCE: + return CL10.CL_LUMINANCE; + case R: + return CL10.CL_R; + case RA: + return CL10.CL_RA; + case RG: + return CL10.CL_RG; + case RGB: + return CL10.CL_RGB; + case RGBA: + return CL10.CL_RGBA; + case RGBx: + return CL11.CL_RGBx; + case RGx: + return CL11.CL_RGx; + case Rx: + return CL11.CL_Rx; + default: + throw new IllegalArgumentException("unknown image channel order: " + order); + } + } + + public static ImageChannelOrder encodeImageChannelOrder(int order) { + switch (order) { + case CL10.CL_A: + return ImageChannelOrder.A; + case CL10.CL_ARGB: + return ImageChannelOrder.ARGB; + case CL10.CL_BGRA: + return ImageChannelOrder.BGRA; + case CL10.CL_INTENSITY: + return ImageChannelOrder.INTENSITY; + case CL10.CL_LUMINANCE: + return ImageChannelOrder.LUMINANCE; + case CL10.CL_R: + return ImageChannelOrder.R; + case CL10.CL_RA: + return ImageChannelOrder.RA; + case CL10.CL_RG: + return ImageChannelOrder.RG; + case CL10.CL_RGB: + return ImageChannelOrder.RGB; + case CL10.CL_RGBA: + return ImageChannelOrder.RGBA; + case CL11.CL_RGBx: + return ImageChannelOrder.RGBx; + case CL11.CL_RGx: + return ImageChannelOrder.RGx; + case CL11.CL_Rx: + return ImageChannelOrder.Rx; + default: + //throw new com.jme3.opencl.OpenCLException("unknown image channel order id: " + order); + LOG.log(Level.WARNING, "Unknown image channel order id: {0}", order); + return null; + } + } + + public static int decodeImageChannelType(ImageChannelType type) { + switch (type) { + case FLOAT: + return CL10.CL_FLOAT; + case HALF_FLOAT: + return CL10.CL_HALF_FLOAT; + case SIGNED_INT16: + return CL10.CL_SIGNED_INT16; + case SIGNED_INT32: + return CL10.CL_SIGNED_INT32; + case SIGNED_INT8: + return CL10.CL_SIGNED_INT8; + case SNORM_INT16: + return CL10.CL_SNORM_INT16; + case SNORM_INT8: + return CL10.CL_SNORM_INT8; + case UNORM_INT8: + return CL10.CL_UNORM_INT8; + case UNORM_INT_101010: + return CL10.CL_UNORM_INT_101010; + case UNORM_INT16: + return CL10.CL_UNORM_INT16; + case UNORM_SHORT_565: + return CL10.CL_UNORM_SHORT_565; + case UNORM_SHORT_555: + return CL10.CL_UNORM_SHORT_555; + case UNSIGNED_INT16: + return CL10.CL_UNSIGNED_INT16; + case UNSIGNED_INT32: + return CL10.CL_UNSIGNED_INT32; + case UNSIGNED_INT8: + return CL10.CL_UNSIGNED_INT8; + default: + throw new IllegalArgumentException("Unknown image channel type: " + type); + } + } + + public static ImageChannelType encodeImageChannelType(int type) { + switch (type) { + case CL10.CL_FLOAT: + return ImageChannelType.FLOAT; + case CL10.CL_HALF_FLOAT: + return ImageChannelType.HALF_FLOAT; + case CL10.CL_SIGNED_INT16: + return ImageChannelType.SIGNED_INT16; + case CL10.CL_SIGNED_INT32: + return ImageChannelType.SIGNED_INT32; + case CL10.CL_SIGNED_INT8: + return ImageChannelType.SIGNED_INT8; + case CL10.CL_SNORM_INT16: + return ImageChannelType.SNORM_INT16; + case CL10.CL_SNORM_INT8: + return ImageChannelType.SNORM_INT8; + case CL10.CL_UNORM_INT8: + return ImageChannelType.UNORM_INT8; + case CL10.CL_UNORM_INT16: + return ImageChannelType.UNORM_INT16; + case CL10.CL_UNORM_INT_101010: + return ImageChannelType.UNORM_INT_101010; + case CL10.CL_UNORM_SHORT_555: + return ImageChannelType.UNORM_SHORT_555; + case CL10.CL_UNORM_SHORT_565: + return ImageChannelType.UNORM_SHORT_565; + case CL10.CL_UNSIGNED_INT16: + return ImageChannelType.UNSIGNED_INT16; + case CL10.CL_UNSIGNED_INT32: + return ImageChannelType.UNSIGNED_INT32; + case CL10.CL_UNSIGNED_INT8: + return ImageChannelType.UNSIGNED_INT8; + default: + //throw new com.jme3.opencl.OpenCLException("unknown image channel type id: " + type); + LOG.log(Level.WARNING, "Unknown image channel type id: {0}", type); + return null; + } + } + + public static int decodeImageType(ImageType type) { + switch (type) { + case IMAGE_1D: + return CL12.CL_MEM_OBJECT_IMAGE1D; + case IMAGE_1D_ARRAY: + return CL12.CL_MEM_OBJECT_IMAGE1D_ARRAY; + case IMAGE_1D_BUFFER: + return CL12.CL_MEM_OBJECT_IMAGE1D_BUFFER; + case IMAGE_2D: + return CL10.CL_MEM_OBJECT_IMAGE2D; + case IMAGE_2D_ARRAY: + return CL12.CL_MEM_OBJECT_IMAGE2D_ARRAY; + case IMAGE_3D: + return CL10.CL_MEM_OBJECT_IMAGE3D; + default: + throw new IllegalArgumentException("Unknown image type: " + type); + } + } + + public static ImageType encodeImageType(int type) { + switch (type) { + case CL12.CL_MEM_OBJECT_IMAGE1D: + return ImageType.IMAGE_1D; + case CL12.CL_MEM_OBJECT_IMAGE1D_ARRAY: + return ImageType.IMAGE_1D_ARRAY; + case CL12.CL_MEM_OBJECT_IMAGE1D_BUFFER: + return ImageType.IMAGE_1D_BUFFER; + case CL10.CL_MEM_OBJECT_IMAGE2D: + return ImageType.IMAGE_2D; + case CL12.CL_MEM_OBJECT_IMAGE2D_ARRAY: + return ImageType.IMAGE_2D_ARRAY; + case CL10.CL_MEM_OBJECT_IMAGE3D: + return ImageType.IMAGE_3D; + default: + throw new com.jme3.opencl.OpenCLException("Unknown image type id: " + type); + } + } + + @Override + public long getWidth() { + return Info.clGetImageInfoPointer(image, CL10.CL_IMAGE_WIDTH); + } + + @Override + public long getHeight() { + return Info.clGetImageInfoPointer(image, CL10.CL_IMAGE_HEIGHT); + } + + @Override + public long getDepth() { + return Info.clGetImageInfoPointer(image, CL10.CL_IMAGE_DEPTH); + } + + @Override + public long getRowPitch() { + return Info.clGetImageInfoPointer(image, CL10.CL_IMAGE_ROW_PITCH); + } + + @Override + public long getSlicePitch() { + return Info.clGetImageInfoPointer(image, CL10.CL_IMAGE_SLICE_PITCH); + } + + @Override + public long getArraySize() { + return Info.clGetImageInfoPointer(image, CL12.CL_IMAGE_ARRAY_SIZE); + } + + @Override + public ImageFormat getImageFormat() { + Utils.b80.rewind(); + CLImageFormat format = new CLImageFormat(Utils.b80); + int ret = CL10.clGetImageInfo(image, CL10.CL_IMAGE_FORMAT, format.sizeof(), Utils.b80, null); + Utils.checkError(ret, "clGetImageInfo"); + return new ImageFormat(encodeImageChannelOrder(format.image_channel_order()), encodeImageChannelType(format.image_channel_data_type())); + } + + @Override + public ImageType getImageType() { + int type = Info.clGetMemObjectInfoInt(image, CL10.CL_MEM_TYPE); + return encodeImageType(type); + } + + @Override + public int getElementSize() { + return Info.clGetImageInfoInt(image, CL10.CL_IMAGE_ELEMENT_SIZE); + } + + @Override + public void readImage(CommandQueue queue, ByteBuffer dest, long[] origin, long[] region, long rowPitch, long slicePitch) { + if (origin.length!=3 || region.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[1].put(origin).position(0); + Utils.pointerBuffers[2].put(region).position(0); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10.clEnqueueReadImage(q, image, CL10.CL_TRUE, + Utils.pointerBuffers[1], Utils.pointerBuffers[2], + rowPitch, slicePitch, dest, null, null); + Utils.checkError(ret, "clEnqueueReadImage"); + } + + @Override + public Event readImageAsync(CommandQueue queue, ByteBuffer dest, long[] origin, long[] region, long rowPitch, long slicePitch) { + if (origin.length!=3 || region.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[0].rewind(); + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[1].put(origin).position(0); + Utils.pointerBuffers[2].put(region).position(0); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10.clEnqueueReadImage(q, image, CL10.CL_FALSE, + Utils.pointerBuffers[1], Utils.pointerBuffers[2], + rowPitch, slicePitch, dest, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueReadImage"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + + @Override + public void writeImage(CommandQueue queue, ByteBuffer dest, long[] origin, long[] region, long rowPitch, long slicePitch) { + if (origin.length!=3 || region.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[1].put(origin).position(0); + Utils.pointerBuffers[2].put(region).position(0); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10.clEnqueueWriteImage(q, image, CL10.CL_TRUE, + Utils.pointerBuffers[1], Utils.pointerBuffers[2], + rowPitch, slicePitch, dest, null, null); + Utils.checkError(ret, "clEnqueueWriteImage"); + } + + @Override + public Event writeImageAsync(CommandQueue queue, ByteBuffer dest, long[] origin, long[] region, long rowPitch, long slicePitch) { + if (origin.length!=3 || region.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[0].rewind(); + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[1].put(origin).position(0); + Utils.pointerBuffers[2].put(region).position(0); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10.clEnqueueWriteImage(q, image, CL10.CL_FALSE, + Utils.pointerBuffers[1], Utils.pointerBuffers[2], + rowPitch, slicePitch, dest, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueWriteImage"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + + @Override + public void copyTo(CommandQueue queue, Image dest, long[] srcOrigin, long[] destOrigin, long[] region) { + if (srcOrigin.length!=3 || destOrigin.length!=3 || region.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[0].rewind(); + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[3].rewind(); + Utils.pointerBuffers[1].put(srcOrigin).position(0); + Utils.pointerBuffers[2].put(destOrigin).position(0); + Utils.pointerBuffers[3].put(region).position(0); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10.clEnqueueCopyImage(q, image, ((LwjglImage) dest).getImage(), + Utils.pointerBuffers[1], Utils.pointerBuffers[2], Utils.pointerBuffers[3], + null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueCopyImage"); + long event = Utils.pointerBuffers[0].get(0); + ret = CL10.clWaitForEvents(event); + Utils.checkError(ret, "clWaitForEvents"); + } + + @Override + public Event copyToAsync(CommandQueue queue, Image dest, long[] srcOrigin, long[] destOrigin, long[] region) { + if (srcOrigin.length!=3 || destOrigin.length!=3 || region.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[0].rewind(); + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[3].rewind(); + Utils.pointerBuffers[1].put(srcOrigin).position(0); + Utils.pointerBuffers[2].put(destOrigin).position(0); + Utils.pointerBuffers[3].put(region).position(0); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10.clEnqueueCopyImage(q, image, ((LwjglImage) dest).getImage(), + Utils.pointerBuffers[1], Utils.pointerBuffers[2], Utils.pointerBuffers[3], + null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueCopyImage"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + + @Override + public ImageMapping map(CommandQueue queue, long[] origin, long[] region, MappingAccess access) { + if (origin.length!=3 || region.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[3].rewind(); + Utils.pointerBuffers[4].rewind(); + Utils.pointerBuffers[1].put(origin).position(0); + Utils.pointerBuffers[2].put(region).position(0); + long q = ((LwjglCommandQueue) queue).getQueue(); + long flags = Utils.getMappingAccessFlags(access); + Utils.errorBuffer.rewind(); + ByteBuffer buf = CL10.clEnqueueMapImage(q, image, CL10.CL_TRUE, flags, + Utils.pointerBuffers[1], Utils.pointerBuffers[2], + Utils.pointerBuffers[3], Utils.pointerBuffers[4], null, null, + Utils.errorBuffer, null); + Utils.checkError(Utils.errorBuffer, "clEnqueueMapBuffer"); + return new ImageMapping(buf, Utils.pointerBuffers[3].get(0), Utils.pointerBuffers[4].get(0)); + } + + @Override + public ImageMapping mapAsync(CommandQueue queue, long[] origin, long[] region, MappingAccess access) { + if (origin.length!=3 || region.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[0].rewind(); + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[3].rewind(); + Utils.pointerBuffers[4].rewind(); + Utils.pointerBuffers[1].put(origin).position(0); + Utils.pointerBuffers[2].put(region).position(0); + long q = ((LwjglCommandQueue) queue).getQueue(); + long flags = Utils.getMappingAccessFlags(access); + Utils.errorBuffer.rewind(); + ByteBuffer buf = CL10.clEnqueueMapImage(q, image, CL10.CL_FALSE, flags, + Utils.pointerBuffers[1], Utils.pointerBuffers[2], + Utils.pointerBuffers[3], Utils.pointerBuffers[4], null, Utils.pointerBuffers[0], + Utils.errorBuffer, null); + Utils.checkError(Utils.errorBuffer, "clEnqueueMapBuffer"); + long event = Utils.pointerBuffers[0].get(0); + return new ImageMapping(buf, Utils.pointerBuffers[3].get(0), Utils.pointerBuffers[4].get(0), new LwjglEvent(event)); + } + + @Override + public void unmap(CommandQueue queue, ImageMapping mapping) { + long q = ((LwjglCommandQueue) queue).getQueue(); + Utils.pointerBuffers[0].rewind(); + int ret = CL10.clEnqueueUnmapMemObject(q, image, mapping.buffer, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueUnmapMemObject"); + long event = Utils.pointerBuffers[0].get(0); + ret = CL10.clWaitForEvents(event); + Utils.checkError(ret, "clWaitForEvents"); + } + + @Override + public Event fillAsync(CommandQueue queue, long[] origin, long[] region, ColorRGBA color) { + if (origin.length!=3 || region.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[0].rewind(); + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[1].put(origin).position(0); + Utils.pointerBuffers[2].put(region).position(0); + Utils.tempBuffers[0].b16f.rewind(); + Utils.tempBuffers[0].b16f.limit(4); + Utils.tempBuffers[0].b16f.put(color.r).put(color.g).put(color.b).put(color.a); + Utils.tempBuffers[0].b16.rewind(); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL12.clEnqueueFillImage(q, image, Utils.tempBuffers[0].b16, + Utils.pointerBuffers[1], Utils.pointerBuffers[2], null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueFillImage"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + //TODO: why does q.getCLEvent(event) return null? + //This is a bug in LWJGL: they forgot to include the line + // if ( __result == CL_SUCCESS ) command_queue.registerCLEvent(event); + // after the native call + } + + @Override + public Event fillAsync(CommandQueue queue, long[] origin, long[] region, int[] color) { + if (color.length != 4) { + throw new IllegalArgumentException("the passed color array must have length 4"); + } + if (origin.length!=3 || region.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[0].rewind(); + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[1].put(origin).position(0); + Utils.pointerBuffers[2].put(region).position(0); + Utils.tempBuffers[0].b16i.rewind(); + Utils.tempBuffers[0].b16i.limit(4); + Utils.tempBuffers[0].b16i.put(color); + Utils.tempBuffers[0].b16.rewind(); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL12.clEnqueueFillImage(q, image, Utils.tempBuffers[0].b16, + Utils.pointerBuffers[1], Utils.pointerBuffers[2], null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueFillImage"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + + @Override + public Event copyToBufferAsync(CommandQueue queue, Buffer dest, long[] srcOrigin, long[] srcRegion, long destOffset) { + if (srcOrigin.length!=3 || srcRegion.length!=3) { + throw new IllegalArgumentException("origin and region must both be arrays of length 3"); + } + Utils.pointerBuffers[0].rewind(); + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[2].rewind(); + Utils.pointerBuffers[1].put(srcOrigin).position(0); + Utils.pointerBuffers[2].put(srcRegion).position(0); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10.clEnqueueCopyImageToBuffer(q, image, ((LwjglBuffer) dest).getBuffer(), + Utils.pointerBuffers[1], Utils.pointerBuffers[2], destOffset, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueCopyImageToBuffer"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + + @Override + public Event acquireImageForSharingAsync(CommandQueue queue) { + Utils.pointerBuffers[0].rewind(); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10GL.clEnqueueAcquireGLObjects(q, image, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueAcquireGLObjects"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + @Override + public void acquireImageForSharingNoEvent(CommandQueue queue) { + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10GL.clEnqueueAcquireGLObjects(q, image, null, null); + Utils.checkError(ret, "clEnqueueAcquireGLObjects"); + } + @Override + public Event releaseImageForSharingAsync(CommandQueue queue) { + Utils.pointerBuffers[0].rewind(); + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10GL.clEnqueueReleaseGLObjects(q, image, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueReleaseGLObjects"); + long event = Utils.pointerBuffers[0].get(0); + return new LwjglEvent(event); + } + @Override + public void releaseImageForSharingNoEvent(CommandQueue queue) { + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10GL.clEnqueueReleaseGLObjects(q, image, null, null); + Utils.checkError(ret, "clEnqueueReleaseGLObjects"); + } + + private static class ReleaserImpl implements ObjectReleaser { + private long mem; + private ReleaserImpl(long mem) { + this.mem = mem; + } + @Override + public void release() { + if (mem != 0) { + int ret = CL10.clReleaseMemObject(mem); + mem = 0; + Utils.reportError(ret, "clReleaseMemObject"); + } + } + + } +} diff --git a/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglKernel.java b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglKernel.java new file mode 100644 index 000000000..9e4a31a73 --- /dev/null +++ b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglKernel.java @@ -0,0 +1,218 @@ +/* + * 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.lwjgl; + +import com.jme3.math.Quaternion; +import com.jme3.math.Vector2f; +import com.jme3.math.Vector4f; +import com.jme3.opencl.*; +import com.jme3.opencl.Buffer; +import java.nio.*; +import org.lwjgl.PointerBuffer; +import org.lwjgl.opencl.CL10; +import org.lwjgl.opencl.CLDevice; +import org.lwjgl.opencl.Info; + +/** + * + * @author shaman + */ +public class LwjglKernel extends Kernel { + + private final long kernel; + + public LwjglKernel(long kernel) { + super(new ReleaserImpl(kernel)); + this.kernel = kernel; + } + + public long getKernel() { + return kernel; + } + + @Override + public String getName() { + return Info.clGetKernelInfoStringASCII(kernel, CL10.CL_KERNEL_FUNCTION_NAME); + } + + @Override + public int getArgCount() { + return Info.clGetKernelInfoInt(kernel, CL10.CL_KERNEL_NUM_ARGS); + } + + @Override + public long getMaxWorkGroupSize(Device device) { + long d = ((LwjglDevice) device).getDevice(); + return Info.clGetKernelWorkGroupInfoPointer(kernel, d, CL10.CL_KERNEL_WORK_GROUP_SIZE); + } + + @Override + public void setArg(int index, LocalMemPerElement t) { + int ret = CL10.clSetKernelArg (kernel, index, t.getSize() * workGroupSize.getSizes()[0] * workGroupSize.getSizes()[1] * workGroupSize.getSizes()[2]); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, LocalMem t) { + int ret = CL10.clSetKernelArg (kernel, index, t.getSize()); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, Buffer t) { + int ret = CL10.clSetKernelArg1p(kernel, index, ((LwjglBuffer) t).getBuffer()); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, Image i) { + int ret = CL10.clSetKernelArg1p(kernel, index, ((LwjglImage) i).getImage()); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, byte b) { + int ret = CL10.clSetKernelArg1b(kernel, index, b); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, short s) { + int ret = CL10.clSetKernelArg1s(kernel, index, s); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, int i) { + int ret = CL10.clSetKernelArg1i(kernel, index, i); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, long l) { + int ret = CL10.clSetKernelArg1l(kernel, index, l); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, float f) { + int ret = CL10.clSetKernelArg1f(kernel, index, f); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, double d) { + int ret = CL10.clSetKernelArg1d(kernel, index, d); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, Vector2f v) { + int ret = CL10.clSetKernelArg2f(kernel, index, v.x, v.y); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, Vector4f v) { + int ret = CL10.clSetKernelArg4f(kernel, index, v.x, v.y, v.z, v.w); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, Quaternion q) { + int ret = CL10.clSetKernelArg4f(kernel, index, q.getX(), q.getY(), q.getZ(), q.getW()); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public void setArg(int index, ByteBuffer buffer, long size) { + buffer.limit((int) (buffer.position() + size)); + int ret = CL10.clSetKernelArg(kernel, index, buffer); + Utils.checkError(ret, "clSetKernelArg"); + } + + @Override + public Event Run(CommandQueue queue) { + Utils.pointerBuffers[0].rewind(); + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[1].put(globalWorkSize.getSizes()); + Utils.pointerBuffers[1].position(0); + PointerBuffer p2 = null; + if (workGroupSize.getSizes()[0] > 0) { + p2 = Utils.pointerBuffers[2].rewind(); + p2.put(workGroupSize.getSizes()); + p2.position(0); + } + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10.clEnqueueNDRangeKernel(q, kernel, + globalWorkSize.getDimension(), null, Utils.pointerBuffers[1], + p2, null, Utils.pointerBuffers[0]); + Utils.checkError(ret, "clEnqueueNDRangeKernel"); + return new LwjglEvent(Utils.pointerBuffers[0].get(0)); + } + @Override + public void RunNoEvent(CommandQueue queue) { + Utils.pointerBuffers[1].rewind(); + Utils.pointerBuffers[1].put(globalWorkSize.getSizes()); + Utils.pointerBuffers[1].position(0); + PointerBuffer p2 = null; + if (workGroupSize.getSizes()[0] > 0) { + p2 = Utils.pointerBuffers[2].rewind(); + p2.put(workGroupSize.getSizes()); + p2.position(0); + } + long q = ((LwjglCommandQueue) queue).getQueue(); + int ret = CL10.clEnqueueNDRangeKernel(q, kernel, + globalWorkSize.getDimension(), null, Utils.pointerBuffers[1], + p2, null, null); + Utils.checkError(ret, "clEnqueueNDRangeKernel"); + } + + @Override + public ObjectReleaser getReleaser() { + return new ReleaserImpl(kernel); + } + private static class ReleaserImpl implements ObjectReleaser { + private long kernel; + private ReleaserImpl(long kernel) { + this.kernel = kernel; + } + @Override + public void release() { + if (kernel != 0) { + int ret = CL10.clReleaseKernel(kernel); + kernel = 0; + Utils.reportError(ret, "clReleaseKernel"); + } + } + } +} diff --git a/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglPlatform.java b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglPlatform.java new file mode 100644 index 000000000..74fc685c1 --- /dev/null +++ b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglPlatform.java @@ -0,0 +1,128 @@ +/* + * 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.lwjgl; + +import com.jme3.opencl.Device; +import com.jme3.opencl.Platform; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import org.lwjgl.opencl.CL10; +import org.lwjgl.opencl.CLDevice; +import org.lwjgl.opencl.CLPlatform; +import org.lwjgl.opencl.Info; + +/** + * + * @author shaman + */ +public final class LwjglPlatform implements Platform { + + final CLPlatform platform; + List devices; + + public LwjglPlatform(CLPlatform platform) { + this.platform = platform; + } + + public CLPlatform getPlatform() { + return platform; + } + + @Override + public List getDevices() { + if (devices == null) { + devices = new ArrayList<>(); + for (CLDevice d : platform.getDevices(CL10.CL_DEVICE_TYPE_ALL)) { + devices.add(new LwjglDevice(d, this)); + } + } + return devices; + } + + @Override + public String getProfile() { + return Info.clGetPlatformInfoStringASCII(platform.address(), CL10.CL_PLATFORM_PROFILE); + } + + @Override + public boolean isFullProfile() { + return getProfile().contains("FULL_PROFILE"); + } + + @Override + public boolean isEmbeddedProfile() { + return getProfile().contains("EMBEDDED_PROFILE"); + } + + @Override + public String getVersion() { + return Info.clGetPlatformInfoStringASCII(platform.address(), CL10.CL_PLATFORM_VERSION); + } + + @Override + public int getVersionMajor() { + return Utils.getMajorVersion(getVersion(), "OpenCL "); + } + + @Override + public int getVersionMinor() { + return Utils.getMinorVersion(getVersion(), "OpenCL "); + } + + @Override + public String getName() { + return Info.clGetPlatformInfoStringASCII(platform.address(), CL10.CL_PLATFORM_NAME); + } + + @Override + public String getVendor() { + return Info.clGetPlatformInfoStringASCII(platform.address(), CL10.CL_PLATFORM_VENDOR); + } + + @Override + public boolean hasExtension(String extension) { + return getExtensions().contains(extension); + } + + @Override + public boolean hasOpenGLInterop() { + return hasExtension("cl_khr_gl_sharing"); + } + + @Override + public Collection getExtensions() { + return Arrays.asList(Info.clGetPlatformInfoStringASCII(platform.address(), CL10.CL_PLATFORM_EXTENSIONS).split(" ")); + } + +} diff --git a/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglProgram.java b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglProgram.java new file mode 100644 index 000000000..5e1e06466 --- /dev/null +++ b/jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglProgram.java @@ -0,0 +1,178 @@ +/* + * 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.lwjgl; + +import com.jme3.opencl.*; +import java.nio.ByteBuffer; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.lwjgl.BufferUtils; +import org.lwjgl.PointerBuffer; +import org.lwjgl.opencl.*; +import org.lwjgl.system.MemoryUtil; +import org.lwjgl.system.Pointer; + +/** + * + * @author shaman + */ +public class LwjglProgram extends Program { + private static final Logger LOG = Logger.getLogger(LwjglProgram.class.getName()); + + private final long program; + private final LwjglContext context; + + public LwjglProgram(long program, LwjglContext context) { + super(new ReleaserImpl(program)); + this.program = program; + this.context = context; + } + + public long getProgram() { + return program; + } + + @Override + public void build(String args, Device... devices) throws KernelCompilationException { + PointerBuffer deviceList = null; + if (devices != null) { + deviceList = PointerBuffer.allocateDirect(devices.length); + deviceList.rewind(); + for (Device d : devices) { + deviceList.put(((LwjglDevice) d).getDevice()); + } + deviceList.flip(); + } + int ret = CL10.clBuildProgram(program, deviceList, args, null, 0); + if (ret != CL10.CL_SUCCESS) { + String log = Log(); + LOG.log(Level.WARNING, "Unable to compile program:\n{0}", log); + if (ret == CL10.CL_BUILD_PROGRAM_FAILURE) { + throw new KernelCompilationException("Failed to build program", ret, log); + } else { + Utils.checkError(ret, "clBuildProgram"); + } + } else { + LOG.log(Level.INFO, "Program compiled:\n{0}", Log()); + } + } + + private String Log() { + StringBuilder str = new StringBuilder(); + for (LwjglDevice device : context.getDevices()) { + long d = device.getDevice(); + str.append(device.getName()).append(":\n"); + str.append(Info.clGetProgramBuildInfoStringASCII(program, d, CL10.CL_PROGRAM_BUILD_LOG)); + str.append('\n'); + } + return str.toString(); + } + + @Override + public Kernel createKernel(String name) { + long kernel = CL10.clCreateKernel(program, name, Utils.errorBuffer); + Utils.checkError(Utils.errorBuffer, "clCreateKernel"); + return new LwjglKernel(kernel); + } + + @Override + public Kernel[] createAllKernels() { + Utils.tempBuffers[0].b16i.rewind(); + int ret = CL10.clCreateKernelsInProgram(program, null, Utils.tempBuffers[0].b16i); + Utils.checkError(ret, "clCreateKernelsInProgram"); + int count = Utils.tempBuffers[0].b16i.get(0); + PointerBuffer buf = PointerBuffer.allocateDirect(count); + ret = CL10.clCreateKernelsInProgram(program, buf, null); + Utils.checkError(ret, "clCreateKernelsInProgram"); + Kernel[] kx = new Kernel[count]; + for (int i=0; i 0) { + return MemoryAccess.READ_WRITE; + } + if ((flag & CL10.CL_MEM_READ_ONLY) > 0) { + return MemoryAccess.READ_ONLY; + } + if ((flag & CL10.CL_MEM_WRITE_ONLY) > 0) { + return MemoryAccess.WRITE_ONLY; + } + throw new OpenCLException("Unknown memory access flag: "+flag); + } + + public static long getMappingAccessFlags(MappingAccess ma) { + switch (ma) { + case MAP_READ_ONLY: return CL10.CL_MAP_READ; + case MAP_READ_WRITE: return CL10.CL_MAP_READ | CL10.CL_MAP_WRITE; + case MAP_WRITE_ONLY: return CL10.CL_MAP_WRITE; + case MAP_WRITE_INVALIDATE: return CL12.CL_MAP_WRITE_INVALIDATE_REGION; + default: throw new IllegalArgumentException("Unknown mapping access: "+ma); + } + } + +} diff --git a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java index 986544510..4b60bd0d2 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java @@ -36,6 +36,12 @@ import com.jme3.input.lwjgl.GlfwJoystickInput; import com.jme3.input.lwjgl.GlfwKeyInput; import com.jme3.input.lwjgl.GlfwMouseInput; import com.jme3.opencl.Context; +import com.jme3.opencl.DefaultPlatformChooser; +import com.jme3.opencl.Device; +import com.jme3.opencl.PlatformChooser; +import com.jme3.opencl.lwjgl.LwjglDevice; +import com.jme3.opencl.lwjgl.LwjglPlatform; +import com.jme3.opencl.lwjgl.Utils; import com.jme3.renderer.Renderer; import com.jme3.renderer.RendererException; import com.jme3.renderer.lwjgl.LwjglGL; @@ -44,19 +50,21 @@ import com.jme3.renderer.lwjgl.LwjglGLFboEXT; import com.jme3.renderer.lwjgl.LwjglGLFboGL3; import com.jme3.renderer.opengl.*; import com.jme3.system.*; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.lwjgl.PointerBuffer; import org.lwjgl.glfw.GLFW; +import org.lwjgl.opencl.*; +import org.lwjgl.opengl.ARBDebugOutput; import org.lwjgl.opengl.ARBFramebufferObject; import org.lwjgl.opengl.EXTFramebufferMultisample; import org.lwjgl.opengl.GLCapabilities; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.logging.Level; -import java.util.logging.Logger; - import static org.lwjgl.glfw.GLFW.GLFW_TRUE; - -import org.lwjgl.opengl.ARBDebugOutput; - +import static org.lwjgl.opencl.CL10.CL_CONTEXT_PLATFORM; import static org.lwjgl.opengl.GL.createCapabilities; import static org.lwjgl.opengl.GL11.glGetInteger; @@ -81,8 +89,7 @@ public abstract class LwjglContext implements JmeContext { protected Timer timer; protected SystemListener listener; - protected long clPlatform; - protected Context clContext; + protected com.jme3.opencl.lwjgl.LwjglContext clContext; public void setSystemListener(SystemListener listener) { this.listener = listener; @@ -194,6 +201,139 @@ public abstract class LwjglContext implements JmeContext { protected void initOpenCL() { logger.info("Initialize OpenCL with LWJGL3"); + +// try { +// CL.create(); +// } catch (Exception ex) { +// logger.log(Level.SEVERE, "Unable to initialize OpenCL", ex); +// return; +// } + + //load platforms and devices + StringBuilder platformInfos = new StringBuilder(); + ArrayList platforms = new ArrayList<>(); + for (CLPlatform p : CLPlatform.getPlatforms()) { + platforms.add(new LwjglPlatform(p)); + } + platformInfos.append("Available OpenCL platforms:"); + for (int i=0; i devices = platform.getDevices(); + platformInfos.append("\n * Available devices:"); + for (int j=0; j choosenDevices = chooser.chooseDevices(platforms); + List devices = new ArrayList<>(choosenDevices.size()); + LwjglPlatform platform = null; + for (Device d : choosenDevices) { + if (!(d instanceof LwjglDevice)) { + logger.log(Level.SEVERE, "attempt to return a custom Device implementation from PlatformChooser: {0}", d); + return; + } + LwjglDevice ld = (LwjglDevice) d; + if (platform == null) { + platform = ld.getPlatform(); + } else if (platform != ld.getPlatform()) { + logger.severe("attempt to use devices from different platforms"); + return; + } + devices.add(ld.getCLDevice()); + } + if (devices.isEmpty()) { + logger.warning("no devices specified, no OpenCL context created"); + return; + } + logger.log(Level.INFO, "chosen platform: {0}", platform.getName()); + logger.log(Level.INFO, "chosen devices: {0}", choosenDevices); + + //create context + try { + long c = createContext(platform.getPlatform(), devices); + clContext = new com.jme3.opencl.lwjgl.LwjglContext(c, (List) choosenDevices); + } catch (Exception ex) { + logger.log(Level.SEVERE, "Unable to create OpenCL context", ex); + return; + } + + logger.info("OpenCL context created"); + } + private long createContext(final CLPlatform platform, final List devices) throws Exception { + final int propertyCount = 2 + 4 + 1; + + final PointerBuffer properties = PointerBuffer.allocateDirect(propertyCount + devices.size()); + + //set sharing properties + //https://github.com/glfw/glfw/issues/104 + //TODO: test on Linus and MacOSX + switch ( org.lwjgl.system.Platform.get() ) { + case WINDOWS: + long wglContext = org.lwjgl.opengl.WGL.wglGetCurrentContext(); + long wglDC = org.lwjgl.opengl.WGL.wglGetCurrentDC(); + properties.put(KHRGLSharing.CL_GL_CONTEXT_KHR).put(wglContext); + properties.put(KHRGLSharing.CL_WGL_HDC_KHR).put(wglDC); + break; + case LINUX: + properties.put(KHRGLSharing.CL_GL_CONTEXT_KHR).put(org.lwjgl.opengl.GLX.glXGetCurrentContext()); + properties.put(KHRGLSharing.CL_GLX_DISPLAY_KHR).put(org.lwjgl.opengl.GLX.glXGetCurrentDrawable()); + break; + case MACOSX: + properties.put(APPLEGLSharing.CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE) + .put(org.lwjgl.opengl.CGL.CGLGetShareGroup(org.lwjgl.opengl.CGL.CGLGetCurrentContext())); + break; + default: + throw new UnsupportedOperationException("CL/GL context sharing is not supported on this platform."); + } + properties.put(CL_CONTEXT_PLATFORM).put(platform); + properties.put(0); + + Utils.errorBuffer.rewind(); + PointerBuffer deviceBuffer = PointerBuffer.allocateDirect(devices.size()); + for (CLDevice d : devices) { + deviceBuffer.put(d); + } + deviceBuffer.flip(); + long context = CL10.clCreateContext(properties, deviceBuffer, null, 0, Utils.errorBuffer); + Utils.checkError(Utils.errorBuffer, "clCreateContext"); + + return context; } public void internalDestroy() {