From e02eec0abd52b600d0b6f00a806576a063e7fa66 Mon Sep 17 00:00:00 2001 From: shamanDevel Date: Tue, 19 Apr 2016 15:51:20 +0200 Subject: [PATCH] added customizability of the platforms and devices by PlatformChooser --- .../src/main/java/com/jme3/opencl/Buffer.java | 106 ++----- .../src/main/java/com/jme3/opencl/CL.java | 1 + .../java/com/jme3/opencl/CommandQueue.java | 112 +------ .../main/java/com/jme3/opencl/Context.java | 96 ++---- .../src/main/java/com/jme3/opencl/Device.java | 164 +++------- .../{NativeCLObject.java => Image.java} | 14 +- .../src/main/java/com/jme3/opencl/Kernel.java | 5 +- .../main/java/com/jme3/opencl/Platform.java | 60 ++++ .../java/com/jme3/opencl/PlatformChooser.java | 54 ++++ .../java/com/jme3/system/AppSettings.java | 17 + .../com/jme3/opencl/lwjgl/LwjglContext.java | 136 ++++++++ .../com/jme3/opencl/lwjgl/LwjglDevice.java | 293 ++++++++++++++++++ .../com/jme3/opencl/lwjgl/LwjglPlatform.java | 127 ++++++++ .../opencl/lwjgl/PlatformChooserImpl.java | 92 ++++++ .../java/com/jme3/opencl/lwjgl/Utils.java | 51 +++ .../com/jme3/system/lwjgl/LwjglContext.java | 129 ++++---- 16 files changed, 1005 insertions(+), 452 deletions(-) rename jme3-core/src/main/java/com/jme3/opencl/{NativeCLObject.java => Image.java} (89%) create mode 100644 jme3-core/src/main/java/com/jme3/opencl/Platform.java create mode 100644 jme3-core/src/main/java/com/jme3/opencl/PlatformChooser.java create mode 100644 jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglContext.java create mode 100644 jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglDevice.java create mode 100644 jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglPlatform.java create mode 100644 jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/PlatformChooserImpl.java create mode 100644 jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/Utils.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 ca1f86409..6cdf178c6 100644 --- a/jme3-core/src/main/java/com/jme3/opencl/Buffer.java +++ b/jme3-core/src/main/java/com/jme3/opencl/Buffer.java @@ -37,94 +37,40 @@ import java.nio.ByteBuffer; * * @author Sebastian Weiss */ -public class Buffer { +public interface Buffer { - private final long buffer; - - public Buffer(long buffer) { - this.buffer = buffer; - } - - public int getSize() { - throw new UnsupportedOperationException("not supported yet"); - } + int getSize(); - public MemoryAccess getMemoryAccessFlags() { - throw new UnsupportedOperationException("not supported yet"); - } + MemoryAccess getMemoryAccessFlags(); - public void read(CommandQueue queue, Buffer src, ByteBuffer dest, int size, int offset) { - queue.read(src, dest, size, offset); - } - public void read(CommandQueue queue, Buffer src, ByteBuffer dest, int size) { - queue.read(src, dest, size); - } - public void read(CommandQueue queue, Buffer src, ByteBuffer dest) { - queue.read(src, dest); - } + void read(CommandQueue queue, ByteBuffer dest, int size, int offset); + void read(CommandQueue queue, ByteBuffer dest, int size); + void read(CommandQueue queue, ByteBuffer dest); - public Event readAsync(CommandQueue queue, Buffer src, ByteBuffer dest, int size, int offset) { - return queue.readAsync(src, dest, size, offset); - } - public Event readAsync(CommandQueue queue, Buffer src, ByteBuffer dest, int size) { - return queue.readAsync(src, dest, size); - } - public Event readAsync(CommandQueue queue, Buffer src, ByteBuffer dest) { - return queue.readAsync(src, dest); - } + Event readAsync(CommandQueue queue, ByteBuffer dest, int size, int offset); + Event readAsync(CommandQueue queue, ByteBuffer dest, int size); + Event readAsync(CommandQueue queue, ByteBuffer dest); - public void write(CommandQueue queue, ByteBuffer src, Buffer dest, int size, int offset) { - queue.write(src, dest, size, offset); - } - public void write(CommandQueue queue, ByteBuffer src, Buffer dest, int size) { - queue.write(src, dest, size); - } - public void write(CommandQueue queue, ByteBuffer src, Buffer dest) { - queue.write(src, dest); - } + void write(CommandQueue queue, ByteBuffer src, int size, int offset); + void write(CommandQueue queue, ByteBuffer src, int size); + void write(CommandQueue queue, ByteBuffer src); - public Event writeAsync(CommandQueue queue, ByteBuffer src, Buffer dest, int size, int offset) { - return queue.writeAsync(src, dest, size, offset); - } - public Event writeAsync(CommandQueue queue, ByteBuffer src, Buffer dest, int size) { - return queue.writeAsync(src, dest, size); - } - public Event writeAsync(CommandQueue queue, ByteBuffer src, Buffer dest) { - return queue.writeAsync(src, dest); - } + Event writeAsync(CommandQueue queue, ByteBuffer src, int size, int offset); + Event writeAsync(CommandQueue queue, ByteBuffer src, int size); + Event writeAsync(CommandQueue queue, ByteBuffer src); - public void copyTo(CommandQueue queue, Buffer src, Buffer dest, int size, int srcOffset, int destOffset) { - queue.copyTo(src, dest, size, srcOffset, destOffset); - } - public void copyTo(CommandQueue queue, Buffer src, Buffer dest, int size) { - queue.copyTo(src, dest, size); - } - public void copyTo(CommandQueue queue, Buffer src, Buffer dest) { - queue.copyTo(src, dest); - } - - public Event copyToAsync(CommandQueue queue, Buffer src, Buffer dest, int size, int srcOffset, int destOffset) { - return queue.copyToAsync(src, dest, size, srcOffset, destOffset); - } - public Event copyToAsync(CommandQueue queue, Buffer src, Buffer dest, int size) { - return queue.copyToAsync(src, dest, size); - } - public Event copyToAsync(CommandQueue queue, Buffer src, Buffer dest) { - return queue.copyToAsync(src, dest); - } + void copyTo(CommandQueue queue, Buffer dest, int size, int srcOffset, int destOffset); + void copyTo(CommandQueue queue, Buffer dest, int size); + void copyTo(CommandQueue queue, Buffer dest); + + Event copyToAsync(CommandQueue queue, Buffer dest, int size, int srcOffset, int destOffset); + Event copyToAsync(CommandQueue queue, Buffer dest, int size); + Event copyToAsync(CommandQueue queue, Buffer dest); - public ByteBuffer map(CommandQueue queue, Buffer src, int size, int offset, MappingAccess access) { - return queue.map(src, size, offset, access); - } - public ByteBuffer map(CommandQueue queue, Buffer src, int size, MappingAccess access) { - return queue.map(src, size, access); - } - public ByteBuffer map(CommandQueue queue, Buffer src, MappingAccess access) { - return queue.map(src, access); - } - public void unmap(CommandQueue queue, Buffer src, ByteBuffer ptr) { - queue.unmap(src, ptr); - } + ByteBuffer map(CommandQueue queue, int size, int offset, MappingAccess access); + ByteBuffer map(CommandQueue queue, int size, MappingAccess access); + ByteBuffer map(CommandQueue queue, MappingAccess access); + void unmap(CommandQueue queue, ByteBuffer ptr); //TODO: async mapping diff --git a/jme3-core/src/main/java/com/jme3/opencl/CL.java b/jme3-core/src/main/java/com/jme3/opencl/CL.java index b7792ac49..8ae5dab11 100644 --- a/jme3-core/src/main/java/com/jme3/opencl/CL.java +++ b/jme3-core/src/main/java/com/jme3/opencl/CL.java @@ -38,6 +38,7 @@ import java.nio.*; * * @author Sebastian Weiss */ +@Deprecated public interface CL { //temp buffers for argument passing 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 6d070143b..a12e947e8 100644 --- a/jme3-core/src/main/java/com/jme3/opencl/CommandQueue.java +++ b/jme3-core/src/main/java/com/jme3/opencl/CommandQueue.java @@ -37,116 +37,10 @@ import java.nio.ByteBuffer; * * @author Sebastian Weiss */ -public final class CommandQueue extends NativeCLObject { +public interface CommandQueue { - private final long queue; + void flush(); - public CommandQueue(long queue) { - this.queue = queue; - } - - public void read(Buffer src, ByteBuffer dest, int size, int offset) { - throw new UnsupportedOperationException("not supported yet"); - } - - public void read(Buffer src, ByteBuffer dest, int size) { - read(src, dest, size, 0); - } - - public void read(Buffer src, ByteBuffer dest) { - read(src, dest, src.getSize(), 0); - } - - public Event readAsync(Buffer src, ByteBuffer dest, int size, int offset) { - throw new UnsupportedOperationException("not supported yet"); - } - - public Event readAsync(Buffer src, ByteBuffer dest, int size) { - return readAsync(src, dest, size, 0); - } - - public Event readAsync(Buffer src, ByteBuffer dest) { - return readAsync(src, dest, src.getSize()); - } - - public void write(ByteBuffer src, Buffer dest, int size, int offset) { - throw new UnsupportedOperationException("not supported yet"); - } - - public void write(ByteBuffer src, Buffer dest, int size) { - write(src, dest, size, 0); - } - - public void write(ByteBuffer src, Buffer dest) { - write(src, dest, dest.getSize()); - } - - public Event writeAsync(ByteBuffer src, Buffer dest, int size, int offset) { - throw new UnsupportedOperationException("not supported yet"); - } - - public Event writeAsync(ByteBuffer src, Buffer dest, int size) { - return writeAsync(src, dest, size, 0); - } - - public Event writeAsync(ByteBuffer src, Buffer dest) { - return writeAsync(src, dest, dest.getSize()); - } - - public void copyTo(Buffer src, Buffer dest, int size, int srcOffset, int destOffset) { - throw new UnsupportedOperationException("not supported yet"); - } - - public void copyTo(Buffer src, Buffer dest, int size) { - copyTo(src, dest, size, 0, 0); - } - - public void copyTo(Buffer src, Buffer dest) { - copyTo(src, dest, src.getSize()); - } - - public Event copyToAsync(Buffer src, Buffer dest, int size, int srcOffset, int destOffset) { - throw new UnsupportedOperationException("not supported yet"); - } - - public Event copyToAsync(Buffer src, Buffer dest, int size) { - return copyToAsync(src, dest, size, 0, 0); - } - - public Event copyToAsync(Buffer src, Buffer dest) { - return copyToAsync(src, dest, src.getSize()); - } - - public ByteBuffer map(Buffer src, int size, int offset, MappingAccess access) { - throw new UnsupportedOperationException("not supported yet"); - } - - public ByteBuffer map(Buffer src, int size, MappingAccess access) { - return map(src, size, 0, access); - } - - public ByteBuffer map(Buffer src, MappingAccess access) { - return map(src, src.getSize(), access); - } - - public void unmap(Buffer src, ByteBuffer ptr) { - throw new UnsupportedOperationException("not supported yet"); - } - - //TODO: async mapping - //TODO: clEnqueueFillBuffer - //TODO: image read/write - public void flush() { - throw new UnsupportedOperationException("not supported yet"); - } - - public void finish() { - throw new UnsupportedOperationException("not supported yet"); - } - - @Override - public void deleteObject() { - throw new UnsupportedOperationException("Not supported yet."); - } + void finish(); } 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 4cd181d10..0201f696a 100644 --- a/jme3-core/src/main/java/com/jme3/opencl/Context.java +++ b/jme3-core/src/main/java/com/jme3/opencl/Context.java @@ -33,10 +33,6 @@ package com.jme3.opencl; import com.jme3.scene.VertexBuffer; import com.jme3.scene.mesh.IndexBuffer; -import com.jme3.scene.mesh.IndexByteBuffer; -import com.jme3.scene.mesh.IndexIntBuffer; -import com.jme3.scene.mesh.IndexShortBuffer; -import com.jme3.texture.Image; import java.nio.ByteBuffer; import java.util.List; @@ -45,43 +41,18 @@ import java.util.List; * * @author Sebastian Weiss */ -public final class Context extends NativeCLObject { +public interface Context { - private final long context; + List getDevices(); - public Context(long context) { - this.context = context; - } - - public List getDevices() { - throw new UnsupportedOperationException("not supported yet"); - } - - public CommandQueue createQueue() { - throw new UnsupportedOperationException("not supported yet"); - } - //TODO: constructor with specific device and properties + CommandQueue createQueue(); + CommandQueue createQueue(Device device); - public Buffer createBuffer(int size, MemoryAccess access) { - throw new UnsupportedOperationException("not supported yet"); - } + Buffer createBuffer(int size, MemoryAccess access); + Buffer createBuffer(int size); - public Buffer createBuffer(int size) { - return createBuffer(size, MemoryAccess.READ_WRITE); - } - - public Buffer useHostBuffer(ByteBuffer data, int size, MemoryAccess access) { - throw new UnsupportedOperationException("not supported yet"); - } - - public Buffer useHostBuffer(ByteBuffer data, int size) { - return useHostBuffer(data, size, MemoryAccess.READ_WRITE); - } - - @Override - public void deleteObject() { - throw new UnsupportedOperationException("Not supported yet."); - } + Buffer useHostBuffer(ByteBuffer data, int size, MemoryAccess access); + Buffer useHostBuffer(ByteBuffer data, int size); public static enum ImageChannelOrder { @@ -143,49 +114,18 @@ public final class Context extends NativeCLObject { public Buffer buffer; } - public Buffer createImage(MemoryAccess access, ImageFormat format, ImageDescriptor descr, ByteBuffer hostPtr) { - throw new UnsupportedOperationException("not supported yet"); - } + Image createImage(MemoryAccess access, ImageFormat format, ImageDescriptor descr, ByteBuffer hostPtr); //TODO: add simplified methods for 1D, 2D, 3D textures //Interop - public Buffer bindVertexBuffer(VertexBuffer vb) { - throw new UnsupportedOperationException("not supported yet"); - } - - public Buffer bindIndexBuffer(IndexBuffer ib) { - if (!(ib instanceof IndexByteBuffer) - && !(ib instanceof IndexShortBuffer) - && !(ib instanceof IndexIntBuffer)) { - throw new IllegalArgumentException("Index buffer must be an IndexByteBuffer, IndexShortBuffer or IndexIntBuffer"); - } - throw new UnsupportedOperationException("not supported yet"); - } - - public Buffer bindImage(Image image) { - throw new UnsupportedOperationException("not supported yet"); - } - - public Program createProgramFromSourceCode(String sourceCode) { - throw new UnsupportedOperationException("not supported yet"); - } - - public Program createProgramFromSourceFilesWithInclude( - String include, String... resources) { - //TODO: load resources - throw new UnsupportedOperationException("not implemented yet"); - } - - public Program createProgramFormSourcesWithInclude(String include, List resources) { - return createProgramFromSourceFilesWithInclude(include, resources.toArray(new String[resources.size()])); - } - - public Program createProgramFromSources(String... resources) { - return createProgramFromSourceFilesWithInclude(null, resources); - } - - public Program createProgramFromSources(List resources) { - return createProgramFormSourcesWithInclude(null, resources); - } + Buffer bindVertexBuffer(VertexBuffer vb); + Buffer bindIndexBuffer(IndexBuffer ib); + Image bindImage(com.jme3.texture.Image image); + + Program createProgramFromSourceCode(String sourceCode); + Program createProgramFromSourceFilesWithInclude(String include, String... resources); + Program createProgramFormSourcesWithInclude(String include, List resources); + Program createProgramFromSources(String... resources); + Program createProgramFromSources(List resources); } diff --git a/jme3-core/src/main/java/com/jme3/opencl/Device.java b/jme3-core/src/main/java/com/jme3/opencl/Device.java index 045b44ecb..5d9095b6f 100644 --- a/jme3-core/src/main/java/com/jme3/opencl/Device.java +++ b/jme3-core/src/main/java/com/jme3/opencl/Device.java @@ -37,13 +37,10 @@ import java.util.Collection; * * @author Sebastian Weiss */ -public final class Device { - private final long device; - - public Device(long device) { - this.device = device; - } +public interface Device { + Platform getPlatform(); + public static enum DeviceType { DEFAULT, CPU, @@ -51,124 +48,55 @@ public final class Device { 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"); - } + DeviceType getDeviceType(); + int getVendorId(); + boolean isAvailable(); - 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"); - } + boolean hasCompiler(); + boolean hasDouble(); + boolean hasHalfFloat(); + boolean hasErrorCorrectingMemory(); + boolean hasUnifiedMemory(); + boolean hasImageSupport(); + boolean hasWritableImage3D(); + boolean hasOpenGLInterop(); + boolean hasExtension(String extension); + Collection getExtensions(); - 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"); - } + int getComputeUnits(); + int getClockFrequency(); + int getAddressBits(); + boolean isLittleEndian(); - 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"); - } + long getMaximumWorkItemDimensions(); + long[] getMaximumWorkItemSizes(); + long getMaxiumWorkItemsPerGroup(); - 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"); - } + int getMaximumSamplers(); + int getMaximumReadImages(); + int getMaximumWriteImages(); + long[] getMaximumImage2DSize(); + long[] getMaximumImage3DSize(); + long getMaximumAllocationSize(); + long getGlobalMemorySize(); + long getLocalMemorySize(); + long getMaximumConstantBufferSize(); + int getMaximumConstantArguments(); + //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"); - } + String getProfile(); + String getVersion(); + int getVersionMajor(); + int getVersionMinor(); + String getCompilerVersion(); + int getCompilerVersionMajor(); + int getCompilerVersionMinor(); + String getDriverVersion(); + int getDriverVersionMajor(); + int getDriverVersionMinor(); + String getName(); + String getVendor(); - @Override - public String toString() { - throw new UnsupportedOperationException("not supported yet"); - } } diff --git a/jme3-core/src/main/java/com/jme3/opencl/NativeCLObject.java b/jme3-core/src/main/java/com/jme3/opencl/Image.java similarity index 89% rename from jme3-core/src/main/java/com/jme3/opencl/NativeCLObject.java rename to jme3-core/src/main/java/com/jme3/opencl/Image.java index a4ac5a857..850dedd10 100644 --- a/jme3-core/src/main/java/com/jme3/opencl/NativeCLObject.java +++ b/jme3-core/src/main/java/com/jme3/opencl/Image.java @@ -35,16 +35,6 @@ package com.jme3.opencl; * * @author Sebastian Weiss */ -public abstract class NativeCLObject { - protected CL cl; - - public CL getCl() { - return cl; - } - - public void setCl(CL cl) { - this.cl = cl; - } - - public abstract void deleteObject(); +public interface Image { + } diff --git a/jme3-core/src/main/java/com/jme3/opencl/Kernel.java b/jme3-core/src/main/java/com/jme3/opencl/Kernel.java index 7716284ca..933b153ce 100644 --- a/jme3-core/src/main/java/com/jme3/opencl/Kernel.java +++ b/jme3-core/src/main/java/com/jme3/opencl/Kernel.java @@ -39,8 +39,8 @@ import com.jme3.math.Vector4f; * * @author Sebastian Weiss */ -public final class Kernel extends NativeCLObject { - +public final class Kernel { +/* private final WorkSize globalWorkSize; private final WorkSize workGroupSize; private final long kernel; @@ -212,4 +212,5 @@ public final class Kernel extends NativeCLObject { public void deleteObject() { throw new UnsupportedOperationException("Not supported yet."); } +*/ } diff --git a/jme3-core/src/main/java/com/jme3/opencl/Platform.java b/jme3-core/src/main/java/com/jme3/opencl/Platform.java new file mode 100644 index 000000000..0d5b636ac --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/opencl/Platform.java @@ -0,0 +1,60 @@ +/* + * 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; +import java.util.List; + +/** + * + * @author Sebastian Weiss + */ +public interface Platform { + + List getDevices(); + + String getProfile(); + boolean isFullProfile(); + boolean isEmbeddedProfile(); + + String getVersion(); + int getVersionMajor(); + int getVersionMinor(); + + String getName(); + String getVendor(); + boolean hasOpenGLInterop(); + boolean hasExtension(String extension); + Collection getExtensions(); + + +} diff --git a/jme3-core/src/main/java/com/jme3/opencl/PlatformChooser.java b/jme3-core/src/main/java/com/jme3/opencl/PlatformChooser.java new file mode 100644 index 000000000..51453cbe5 --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/opencl/PlatformChooser.java @@ -0,0 +1,54 @@ +/* + * 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 com.jme3.system.AppSettings; +import java.util.List; + +/** + * This SPI is called on startup to specify which platform and which devices + * are used for context creation. + * @author Sebastian Weiss + * @see AppSettings#setOpenCLPlatformChooser(java.lang.Class) + */ +public interface PlatformChooser { + + /** + * Chooses one or more devices for the opencl context. + * All returned devices must belong to the same platform. + * If the returned list is empty, no context will be created. + * @param platforms the available platforms + * @return the list of devices + */ + List chooseDevices(List platforms); + +} diff --git a/jme3-core/src/main/java/com/jme3/system/AppSettings.java b/jme3-core/src/main/java/com/jme3/system/AppSettings.java index e77725bfc..991854ebf 100644 --- a/jme3-core/src/main/java/com/jme3/system/AppSettings.java +++ b/jme3-core/src/main/java/com/jme3/system/AppSettings.java @@ -31,6 +31,7 @@ */ package com.jme3.system; +import com.jme3.opencl.PlatformChooser; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -1033,4 +1034,20 @@ public final class AppSettings extends HashMap { public boolean isOpenCLSupport() { return getBoolean("OpenCL"); } + + /** + * Sets a custom platform chooser. This chooser specifies which platform and + * which devices are used for the OpenCL context. + * + * Default: not set, an implementation defined one is used. + * + * @param chooser the class of the chooser, must have a default constructor + */ + public void setOpenCLPlatformChooser(Class chooser) { + putString("OpenCLPlatformChooser", chooser.getName()); + } + + public String getOpenCLPlatformChooser() { + return getString("OpenCLPlatformChooser"); + } } diff --git a/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglContext.java b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglContext.java new file mode 100644 index 000000000..e2d08e4d7 --- /dev/null +++ b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglContext.java @@ -0,0 +1,136 @@ +/* + * 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.scene.VertexBuffer; +import com.jme3.scene.mesh.IndexBuffer; +import java.nio.ByteBuffer; +import java.util.List; +import org.lwjgl.opencl.CLContext; + +/** + * + * @author Sebastian Weiss + */ +public class LwjglContext implements Context { + private final CLContext context; + + public LwjglContext(CLContext context) { + this.context = context; + } + + public CLContext getContext() { + return context; + } + + @Override + public List getDevices() { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public CommandQueue createQueue() { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public CommandQueue createQueue(Device device) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Buffer createBuffer(int size, MemoryAccess access) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Buffer createBuffer(int size) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Buffer useHostBuffer(ByteBuffer data, int size, MemoryAccess access) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Buffer useHostBuffer(ByteBuffer data, int size) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Image createImage(MemoryAccess access, ImageFormat format, ImageDescriptor descr, ByteBuffer hostPtr) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Buffer bindVertexBuffer(VertexBuffer vb) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Buffer bindIndexBuffer(IndexBuffer ib) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Image bindImage(com.jme3.texture.Image image) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Program createProgramFromSourceCode(String sourceCode) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Program createProgramFromSourceFilesWithInclude(String include, String... resources) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Program createProgramFormSourcesWithInclude(String include, List resources) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Program createProgramFromSources(String... resources) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Program createProgramFromSources(List resources) { + throw new UnsupportedOperationException("Not supported yet."); + } + +} diff --git a/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglDevice.java b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglDevice.java new file mode 100644 index 000000000..b8985d6f5 --- /dev/null +++ b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglDevice.java @@ -0,0 +1,293 @@ +/* + * 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.opencl.CL10; +import org.lwjgl.opencl.CL11; +import org.lwjgl.opencl.CLDevice; + +/** + * + * @author Sebastian Weiss + */ +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 CLDevice getDevice() { + return device; + } + + @Override + public LwjglPlatform getPlatform() { + return platform; + } + + @Override + public DeviceType getDeviceType() { + int type = device.getInfoInt(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 device.getInfoInt(CL10.CL_DEVICE_VENDOR_ID); + } + + @Override + public boolean isAvailable() { + return device.getInfoBoolean(CL10.CL_DEVICE_AVAILABLE); + } + + @Override + public boolean hasCompiler() { + return device.getInfoBoolean(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 device.getInfoBoolean(CL10.CL_DEVICE_ERROR_CORRECTION_SUPPORT); + } + + @Override + public boolean hasUnifiedMemory() { + return device.getInfoBoolean(CL11.CL_DEVICE_HOST_UNIFIED_MEMORY); + } + + @Override + public boolean hasImageSupport() { + return device.getInfoBoolean(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(device.getInfoString(CL10.CL_DEVICE_EXTENSIONS).split(" ")); + } + + @Override + public int getComputeUnits() { + return device.getInfoInt(CL10.CL_DEVICE_MAX_COMPUTE_UNITS); + } + + @Override + public int getClockFrequency() { + return device.getInfoInt(CL10.CL_DEVICE_MAX_CLOCK_FREQUENCY); + } + + @Override + public int getAddressBits() { + return device.getInfoInt(CL10.CL_DEVICE_ADDRESS_BITS); + } + + @Override + public boolean isLittleEndian() { + return device.getInfoBoolean(CL10.CL_DEVICE_ENDIAN_LITTLE); + } + + @Override + public long getMaximumWorkItemDimensions() { + return device.getInfoSize(CL10.CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS); + } + + @Override + public long[] getMaximumWorkItemSizes() { + return device.getInfoSizeArray(CL10.CL_DEVICE_MAX_WORK_ITEM_SIZES); + } + + @Override + public long getMaxiumWorkItemsPerGroup() { + return device.getInfoSize(CL10.CL_DEVICE_MAX_WORK_GROUP_SIZE); + } + + @Override + public int getMaximumSamplers() { + return device.getInfoInt(CL10.CL_DEVICE_MAX_SAMPLERS); + } + + @Override + public int getMaximumReadImages() { + return device.getInfoInt(CL10.CL_DEVICE_MAX_READ_IMAGE_ARGS); + } + + @Override + public int getMaximumWriteImages() { + return device.getInfoInt(CL10.CL_DEVICE_MAX_WRITE_IMAGE_ARGS); + } + + @Override + public long[] getMaximumImage2DSize() { + return new long[] { + device.getInfoSize(CL10.CL_DEVICE_IMAGE2D_MAX_WIDTH), + device.getInfoSize(CL10.CL_DEVICE_IMAGE2D_MAX_HEIGHT) + }; + } + + @Override + public long[] getMaximumImage3DSize() { + return new long[] { + device.getInfoSize(CL10.CL_DEVICE_IMAGE3D_MAX_WIDTH), + device.getInfoSize(CL10.CL_DEVICE_IMAGE3D_MAX_HEIGHT), + device.getInfoSize(CL10.CL_DEVICE_IMAGE3D_MAX_DEPTH) + }; + } + + @Override + public long getMaximumAllocationSize() { + return device.getInfoLong(CL10.CL_DEVICE_MAX_MEM_ALLOC_SIZE); + } + + @Override + public long getGlobalMemorySize() { + return device.getInfoLong(CL10.CL_DEVICE_GLOBAL_MEM_SIZE); + } + + @Override + public long getLocalMemorySize() { + return device.getInfoLong(CL10.CL_DEVICE_LOCAL_MEM_SIZE); + } + + @Override + public long getMaximumConstantBufferSize() { + return device.getInfoLong(CL10.CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE); + } + + @Override + public int getMaximumConstantArguments() { + return device.getInfoInt(CL10.CL_DEVICE_MAX_CONSTANT_ARGS); + } + + @Override + public String getProfile() { + return device.getInfoString(CL10.CL_DEVICE_PROFILE); + } + + @Override + public String getVersion() { + return device.getInfoString(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 device.getInfoString(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 device.getInfoString(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 device.getInfoString(CL10.CL_DEVICE_NAME); + } + + @Override + public String getVendor() { + return device.getInfoString(CL10.CL_DEVICE_VENDOR); + } + + @Override + public String toString() { + return getName(); + } + +} diff --git a/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglPlatform.java b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglPlatform.java new file mode 100644 index 000000000..67f7910be --- /dev/null +++ b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglPlatform.java @@ -0,0 +1,127 @@ +/* + * 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; + +/** + * + * @author Sebastian Weiss + */ +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 platform.getInfoString(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 platform.getInfoString(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 platform.getInfoString(CL10.CL_PLATFORM_NAME); + } + + @Override + public String getVendor() { + return platform.getInfoString(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(platform.getInfoString(CL10.CL_PLATFORM_EXTENSIONS).split(" ")); + } + +} diff --git a/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/PlatformChooserImpl.java b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/PlatformChooserImpl.java new file mode 100644 index 000000000..fd3a7147b --- /dev/null +++ b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/PlatformChooserImpl.java @@ -0,0 +1,92 @@ +/* + * 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 com.jme3.opencl.PlatformChooser; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +/** + * + * @author Sebastian Weiss + */ +public class PlatformChooserImpl implements PlatformChooser { + private static final Logger LOG = Logger.getLogger(PlatformChooserImpl.class.getName()); + + @Override + public List chooseDevices(List platforms) { + ArrayList result = new ArrayList(); + for (Platform p : platforms) { + if (!p.hasOpenGLInterop()) { + continue; //must support interop + } + for (Device d : p.getDevices()) { + if (d.hasOpenGLInterop() && d.getDeviceType()==Device.DeviceType.GPU) { + result.add(d); //GPU prefered + } + } + if (!result.isEmpty()) { + return result; + } + } + //no GPU devices found, try all + for (Platform p : platforms) { + if (!p.hasOpenGLInterop()) { + continue; //must support interop + } + for (Device d : p.getDevices()) { + if (d.hasOpenGLInterop()) { + result.add(d); //just interop needed + } + } + if (!result.isEmpty()) { + return result; + } + } + //still no one found, try without interop + LOG.warning("No device with OpenCL-OpenGL-interop found, try without"); + for (Platform p : platforms) { + for (Device d : p.getDevices()) { + result.add(d); + } + if (!result.isEmpty()) { + return result; + } + } + //no devices available at all! + return result; //result is empty + } + +} diff --git a/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/Utils.java b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/Utils.java new file mode 100644 index 000000000..985d60ad3 --- /dev/null +++ b/jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/Utils.java @@ -0,0 +1,51 @@ +/* + * 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; + +/** + * + * @author Sebastian Weiss + */ +public class Utils { + + public static int getMajorVersion(String version, String prefix) { + String s = version.substring(prefix.length()); + return Integer.parseInt(s); + } + + public static int getMinorVersion(String version, String prefix) { + String s = version.substring(prefix.length()); + int major = Integer.parseInt(s); + s = s.substring((int) (Math.log10(major) + 2)); + return Integer.parseInt(s); + } +} diff --git a/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglContext.java b/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglContext.java index 1a28eada3..02bdd281a 100644 --- a/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglContext.java +++ b/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglContext.java @@ -36,7 +36,12 @@ import com.jme3.input.lwjgl.JInputJoyInput; import com.jme3.input.lwjgl.LwjglKeyInput; import com.jme3.input.lwjgl.LwjglMouseInput; import com.jme3.opencl.Context; +import com.jme3.opencl.Device; +import com.jme3.opencl.PlatformChooser; import com.jme3.opencl.lwjgl.LwjglCL; +import com.jme3.opencl.lwjgl.LwjglDevice; +import com.jme3.opencl.lwjgl.LwjglPlatform; +import com.jme3.opencl.lwjgl.PlatformChooserImpl; import com.jme3.renderer.Renderer; import com.jme3.renderer.RendererException; import com.jme3.renderer.lwjgl.LwjglGL; @@ -87,9 +92,8 @@ public abstract class LwjglContext implements JmeContext { protected Timer timer; protected SystemListener listener; - protected LwjglCL clImpl; - protected CLPlatform clPlatform; - protected com.jme3.opencl.Context clContext; + protected LwjglPlatform clPlatform; + protected com.jme3.opencl.lwjgl.LwjglContext clContext; public void setSystemListener(SystemListener listener) { this.listener = listener; @@ -267,70 +271,89 @@ public abstract class LwjglContext implements JmeContext { return; } - //load platforms - List platforms = CLPlatform.getPlatforms(); + //load platforms and devices StringBuilder platformInfos = new StringBuilder(); - platformInfos.append("Available OpenCL platforms:\n"); - ArrayList possiblePlatforms = new ArrayList(); + 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 devices = clPlatform.getDevices(CL10.CL_DEVICE_TYPE_GPU); - StringBuilder deviceInfos = new StringBuilder(); - deviceInfos.append("Available OpenCL devices:\n"); - ArrayList possibleDevices = new ArrayList(); - for (int i=0; i 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.getDevice()); + } + if (devices.isEmpty()) { + logger.warning("no devices specified, no OpenCL context created"); + return; + } + clPlatform = platform; + logger.log(Level.INFO, "chosen platform: {0}", platform.getName()); + logger.log(Level.INFO, "chosen devices: {0}", choosenDevices); //create context - CLContext context; try { - context = CLContext.create(clPlatform, possibleDevices, null, Display.getDrawable(), null); + clContext = new com.jme3.opencl.lwjgl.LwjglContext(CLContext.create(platform.getPlatform(), devices, null, Display.getDrawable(), null)); } catch (LWJGLException ex) { logger.log(Level.SEVERE, "Unable to create OpenCL context", ex); return; } - clContext = new Context(context.getPointer()); - - //create cl implementation - clImpl = new LwjglCL(); - clContext.setCl(clImpl); logger.info("OpenCL context created"); }