Merge pull request #527 from shamanDevel/OpenCL2
OpenCL for jME3 - some missing features
This commit is contained in:
commit
4f41a28a8c
@ -43,8 +43,9 @@ public abstract class AbstractOpenCLObject implements OpenCLObject {
|
|||||||
this.releaser = releaser;
|
this.releaser = releaser;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void register() {
|
public AbstractOpenCLObject register() {
|
||||||
OpenCLObjectManager.getInstance().registerObject(this);
|
OpenCLObjectManager.getInstance().registerObject(this);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void release() {
|
public void release() {
|
||||||
|
@ -53,6 +53,12 @@ public abstract class Buffer extends AbstractOpenCLObject {
|
|||||||
super(releaser);
|
super(releaser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Buffer register() {
|
||||||
|
super.register();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the size of the buffer in bytes.
|
* @return the size of the buffer in bytes.
|
||||||
* @see Context#createBuffer(long)
|
* @see Context#createBuffer(long)
|
||||||
@ -424,4 +430,9 @@ public abstract class Buffer extends AbstractOpenCLObject {
|
|||||||
releaseBufferForSharingAsync(queue).release();
|
releaseBufferForSharingAsync(queue).release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Buffer (" + getSize() + "B)";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -44,8 +44,27 @@ package com.jme3.opencl;
|
|||||||
*/
|
*/
|
||||||
public abstract class CommandQueue extends AbstractOpenCLObject {
|
public abstract class CommandQueue extends AbstractOpenCLObject {
|
||||||
|
|
||||||
protected CommandQueue(ObjectReleaser releaser) {
|
protected Device device;
|
||||||
|
|
||||||
|
protected CommandQueue(ObjectReleaser releaser, Device device) {
|
||||||
super(releaser);
|
super(releaser);
|
||||||
|
this.device = device;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandQueue register() {
|
||||||
|
super.register();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the device associated with this command queue.
|
||||||
|
* It can be used to query properties of the device that is used to execute
|
||||||
|
* the commands issued to this command queue.
|
||||||
|
* @return the associated device
|
||||||
|
*/
|
||||||
|
public Device getDevice() {
|
||||||
|
return device;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -72,6 +72,12 @@ public abstract class Context extends AbstractOpenCLObject {
|
|||||||
super(releaser);
|
super(releaser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Context register() {
|
||||||
|
super.register();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all available devices for this context.
|
* Returns all available devices for this context.
|
||||||
* These devices all belong to the same {@link Platform}.
|
* These devices all belong to the same {@link Platform}.
|
||||||
@ -436,4 +442,10 @@ public abstract class Context extends AbstractOpenCLObject {
|
|||||||
* @return the new program
|
* @return the new program
|
||||||
*/
|
*/
|
||||||
public abstract Program createProgramFromBinary(ByteBuffer binaries, Device device);
|
public abstract Program createProgramFromBinary(ByteBuffer binaries, Device device);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Context (" + getDevices() + ')';
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,12 @@ public abstract class Event extends AbstractOpenCLObject {
|
|||||||
super(releaser);
|
super(releaser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Event register() {
|
||||||
|
super.register();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Waits until the action has finished (blocking).
|
* Waits until the action has finished (blocking).
|
||||||
* This automatically releases the event.
|
* This automatically releases the event.
|
||||||
|
@ -250,6 +250,12 @@ memory layout in which channels are stored in the image.
|
|||||||
super(releaser);
|
super(releaser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Image register() {
|
||||||
|
super.register();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the width of the image
|
* @return the width of the image
|
||||||
*/
|
*/
|
||||||
@ -533,5 +539,25 @@ memory layout in which channels are stored in the image.
|
|||||||
releaseImageForSharingAsync(queue).release();
|
releaseImageForSharingAsync(queue).release();
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: add variants of the above two methods that don't create the event object, but release the event immediately
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder str = new StringBuilder();
|
||||||
|
str.append("Image (");
|
||||||
|
ImageType t = getImageType();
|
||||||
|
str.append(t);
|
||||||
|
str.append(", w=").append(getWidth());
|
||||||
|
if (t == ImageType.IMAGE_2D || t == ImageType.IMAGE_3D) {
|
||||||
|
str.append(", h=").append(getHeight());
|
||||||
|
}
|
||||||
|
if (t == ImageType.IMAGE_3D) {
|
||||||
|
str.append(", d=").append(getDepth());
|
||||||
|
}
|
||||||
|
if (t == ImageType.IMAGE_1D_ARRAY || t == ImageType.IMAGE_2D_ARRAY) {
|
||||||
|
str.append(", arrays=").append(getArraySize());
|
||||||
|
}
|
||||||
|
str.append(", ").append(getImageFormat());
|
||||||
|
str.append(')');
|
||||||
|
return str.toString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -97,6 +97,12 @@ public abstract class Kernel extends AbstractOpenCLObject {
|
|||||||
this.workGroupSize = new WorkSize(0);
|
this.workGroupSize = new WorkSize(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Kernel register() {
|
||||||
|
super.register();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the name of the kernel as defined in the program source code
|
* @return the name of the kernel as defined in the program source code
|
||||||
*/
|
*/
|
||||||
@ -425,6 +431,11 @@ public abstract class Kernel extends AbstractOpenCLObject {
|
|||||||
RunNoEvent(queue);
|
RunNoEvent(queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Kernel (" + getName() + ")";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A placeholder for kernel arguments representing local kernel memory.
|
* A placeholder for kernel arguments representing local kernel memory.
|
||||||
* This defines the size of available shared memory of a {@code __shared} kernel
|
* This defines the size of available shared memory of a {@code __shared} kernel
|
||||||
@ -468,6 +479,12 @@ public abstract class Kernel extends AbstractOpenCLObject {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "LocalMem (" + size + "B)";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -519,6 +536,12 @@ public abstract class Kernel extends AbstractOpenCLObject {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "LocalMemPerElement (" + size + "B)";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -623,6 +646,21 @@ public abstract class Kernel extends AbstractOpenCLObject {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder str = new StringBuilder();
|
||||||
|
str.append("WorkSize[");
|
||||||
|
for (int i=0; i<dimension; ++i) {
|
||||||
|
if (i>0) {
|
||||||
|
str.append(", ");
|
||||||
|
}
|
||||||
|
str.append(sizes[i]);
|
||||||
|
}
|
||||||
|
str.append(']');
|
||||||
|
return str.toString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -61,6 +61,7 @@ public interface OpenCLObject {
|
|||||||
ObjectReleaser getReleaser();
|
ObjectReleaser getReleaser();
|
||||||
/**
|
/**
|
||||||
* Releases this native object.
|
* Releases this native object.
|
||||||
|
*
|
||||||
* Should delegate to {@code getReleaser().release()}.
|
* Should delegate to {@code getReleaser().release()}.
|
||||||
*/
|
*/
|
||||||
void release();
|
void release();
|
||||||
@ -70,6 +71,10 @@ public interface OpenCLObject {
|
|||||||
* {@link OpenCLObjectManager}, you have to release it manually
|
* {@link OpenCLObjectManager}, you have to release it manually
|
||||||
* by calling {@link #release() }.
|
* by calling {@link #release() }.
|
||||||
* Without registering or releasing, a memory leak might occur.
|
* Without registering or releasing, a memory leak might occur.
|
||||||
|
* <br>
|
||||||
|
* Returns {@code this} to allow calls like
|
||||||
|
* {@code Buffer buffer = clContext.createBuffer(1024).register();}.
|
||||||
|
* @return {@code this}
|
||||||
*/
|
*/
|
||||||
void register();
|
OpenCLObject register();
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,12 @@ public abstract class Program extends AbstractOpenCLObject {
|
|||||||
super(releaser);
|
super(releaser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Program register() {
|
||||||
|
super.register();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds this program with the specified argument string on the specified
|
* Builds this program with the specified argument string on the specified
|
||||||
* devices.
|
* devices.
|
||||||
|
@ -131,8 +131,7 @@ public class TestContextSwitching extends SimpleApplication implements ScreenCon
|
|||||||
|
|
||||||
if (testBuffer == null && clContext != null && !bufferCreated) {
|
if (testBuffer == null && clContext != null && !bufferCreated) {
|
||||||
try {
|
try {
|
||||||
testBuffer = clContext.createBuffer(1024);
|
testBuffer = clContext.createBuffer(1024).register();
|
||||||
testBuffer.register();
|
|
||||||
LOG.info("Test buffer created");
|
LOG.info("Test buffer created");
|
||||||
} catch (OpenCLException ex) {
|
} catch (OpenCLException ex) {
|
||||||
LOG.log(Level.SEVERE, "Unable to create buffer", ex);
|
LOG.log(Level.SEVERE, "Unable to create buffer", ex);
|
||||||
|
@ -115,8 +115,7 @@ public class TestVertexBufferSharing extends SimpleApplication {
|
|||||||
private void initOpenCL1() {
|
private void initOpenCL1() {
|
||||||
clContext = context.getOpenCLContext();
|
clContext = context.getOpenCLContext();
|
||||||
Device device = clContext.getDevices().get(0);
|
Device device = clContext.getDevices().get(0);
|
||||||
clQueue = clContext.createQueue(device);
|
clQueue = clContext.createQueue(device).register();
|
||||||
clQueue.register();
|
|
||||||
//create kernel
|
//create kernel
|
||||||
Program program = null;
|
Program program = null;
|
||||||
File tmpFolder = JmeSystem.getStorageFolder();
|
File tmpFolder = JmeSystem.getStorageFolder();
|
||||||
@ -157,14 +156,12 @@ public class TestVertexBufferSharing extends SimpleApplication {
|
|||||||
LOG.info("create new program from sources");
|
LOG.info("create new program from sources");
|
||||||
}
|
}
|
||||||
program.register();
|
program.register();
|
||||||
kernel = program.createKernel("ScaleKernel");
|
kernel = program.createKernel("ScaleKernel").register();
|
||||||
kernel.register();
|
|
||||||
}
|
}
|
||||||
private void initOpenCL2() {
|
private void initOpenCL2() {
|
||||||
//bind vertex buffer to OpenCL
|
//bind vertex buffer to OpenCL
|
||||||
VertexBuffer vb = geom.getMesh().getBuffer(VertexBuffer.Type.Position);
|
VertexBuffer vb = geom.getMesh().getBuffer(VertexBuffer.Type.Position);
|
||||||
buffer = clContext.bindVertexBuffer(vb, MemoryAccess.READ_WRITE);
|
buffer = clContext.bindVertexBuffer(vb, MemoryAccess.READ_WRITE).register();
|
||||||
buffer.register();
|
|
||||||
ws = new com.jme3.opencl.Kernel.WorkSize(geom.getMesh().getVertexCount());
|
ws = new com.jme3.opencl.Kernel.WorkSize(geom.getMesh().getVertexCount());
|
||||||
}
|
}
|
||||||
private void updateOpenCL(float tpf) {
|
private void updateOpenCL(float tpf) {
|
||||||
|
@ -122,8 +122,7 @@ public class TestWriteToTexture extends SimpleApplication implements AnalogListe
|
|||||||
|
|
||||||
private void initOpenCL1() {
|
private void initOpenCL1() {
|
||||||
clContext = context.getOpenCLContext();
|
clContext = context.getOpenCLContext();
|
||||||
clQueue = clContext.createQueue();
|
clQueue = clContext.createQueue().register();
|
||||||
clQueue.register();
|
|
||||||
programCache = new ProgramCache(clContext);
|
programCache = new ProgramCache(clContext);
|
||||||
//create kernel
|
//create kernel
|
||||||
String cacheID = getClass().getName()+".Julia";
|
String cacheID = getClass().getName()+".Julia";
|
||||||
@ -135,14 +134,12 @@ public class TestWriteToTexture extends SimpleApplication implements AnalogListe
|
|||||||
programCache.saveToCache(cacheID, program);
|
programCache.saveToCache(cacheID, program);
|
||||||
}
|
}
|
||||||
program.register();
|
program.register();
|
||||||
kernel = program.createKernel("JuliaSet");
|
kernel = program.createKernel("JuliaSet").register();
|
||||||
kernel.register();
|
|
||||||
C = new Vector2f(0.12f, -0.2f);
|
C = new Vector2f(0.12f, -0.2f);
|
||||||
}
|
}
|
||||||
private void initOpenCL2() {
|
private void initOpenCL2() {
|
||||||
//bind image to OpenCL
|
//bind image to OpenCL
|
||||||
texCL = clContext.bindImage(tex, MemoryAccess.WRITE_ONLY);
|
texCL = clContext.bindImage(tex, MemoryAccess.WRITE_ONLY).register();
|
||||||
texCL.register();
|
|
||||||
}
|
}
|
||||||
private void updateOpenCL(float tpf) {
|
private void updateOpenCL(float tpf) {
|
||||||
//aquire resource
|
//aquire resource
|
||||||
|
@ -32,11 +32,9 @@
|
|||||||
package com.jme3.opencl.jocl;
|
package com.jme3.opencl.jocl;
|
||||||
|
|
||||||
import com.jme3.opencl.CommandQueue;
|
import com.jme3.opencl.CommandQueue;
|
||||||
import com.jme3.opencl.OpenCLObjectManager;
|
import com.jme3.opencl.Device;
|
||||||
import com.jogamp.opencl.CLCommandQueue;
|
|
||||||
import com.jogamp.opencl.CLPlatform;
|
import com.jogamp.opencl.CLPlatform;
|
||||||
import com.jogamp.opencl.llb.CL;
|
import com.jogamp.opencl.llb.CL;
|
||||||
import com.jogamp.opencl.llb.CLCommandQueueBinding;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -47,8 +45,8 @@ public class JoclCommandQueue extends CommandQueue {
|
|||||||
final CL cl;
|
final CL cl;
|
||||||
final long id;
|
final long id;
|
||||||
|
|
||||||
public JoclCommandQueue(long id) {
|
public JoclCommandQueue(long id, Device device) {
|
||||||
super(new ReleaserImpl(id));
|
super(new ReleaserImpl(id), device);
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.cl = CLPlatform.getLowLevelCLInterface();
|
this.cl = CLPlatform.getLowLevelCLInterface();
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ public class JoclContext extends Context {
|
|||||||
long properties = 0;
|
long properties = 0;
|
||||||
long q = cl.clCreateCommandQueue(id, d, properties, Utils.errorBuffer);
|
long q = cl.clCreateCommandQueue(id, d, properties, Utils.errorBuffer);
|
||||||
Utils.checkError(Utils.errorBuffer, "clCreateCommandQueue");
|
Utils.checkError(Utils.errorBuffer, "clCreateCommandQueue");
|
||||||
return new JoclCommandQueue(q);
|
return new JoclCommandQueue(q, device);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -124,4 +124,9 @@ public final class JoclPlatform implements Platform {
|
|||||||
return platform.getExtensions();
|
return platform.getExtensions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getName();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
package com.jme3.opencl.lwjgl;
|
package com.jme3.opencl.lwjgl;
|
||||||
|
|
||||||
import com.jme3.opencl.CommandQueue;
|
import com.jme3.opencl.CommandQueue;
|
||||||
|
import com.jme3.opencl.Device;
|
||||||
import com.jme3.opencl.OpenCLObjectManager;
|
import com.jme3.opencl.OpenCLObjectManager;
|
||||||
import org.lwjgl.opencl.CL10;
|
import org.lwjgl.opencl.CL10;
|
||||||
import org.lwjgl.opencl.CLCommandQueue;
|
import org.lwjgl.opencl.CLCommandQueue;
|
||||||
@ -44,8 +45,8 @@ public class LwjglCommandQueue extends CommandQueue {
|
|||||||
|
|
||||||
private final CLCommandQueue queue;
|
private final CLCommandQueue queue;
|
||||||
|
|
||||||
public LwjglCommandQueue(CLCommandQueue queue) {
|
public LwjglCommandQueue(CLCommandQueue queue, Device device) {
|
||||||
super(new ReleaserImpl(queue));
|
super(new ReleaserImpl(queue), device);
|
||||||
this.queue = queue;
|
this.queue = queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ public class LwjglContext extends Context {
|
|||||||
long properties = 0;
|
long properties = 0;
|
||||||
CLCommandQueue q = CL10.clCreateCommandQueue(context, d, properties, Utils.errorBuffer);
|
CLCommandQueue q = CL10.clCreateCommandQueue(context, d, properties, Utils.errorBuffer);
|
||||||
Utils.checkError(Utils.errorBuffer, "clCreateCommandQueue");
|
Utils.checkError(Utils.errorBuffer, "clCreateCommandQueue");
|
||||||
return new LwjglCommandQueue(q);
|
return new LwjglCommandQueue(q, device);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -124,4 +124,9 @@ public final class LwjglPlatform implements Platform {
|
|||||||
return Arrays.asList(platform.getInfoString(CL10.CL_PLATFORM_EXTENSIONS).split(" "));
|
return Arrays.asList(platform.getInfoString(CL10.CL_PLATFORM_EXTENSIONS).split(" "));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getName();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
package com.jme3.opencl.lwjgl;
|
package com.jme3.opencl.lwjgl;
|
||||||
|
|
||||||
import com.jme3.opencl.CommandQueue;
|
import com.jme3.opencl.CommandQueue;
|
||||||
|
import com.jme3.opencl.Device;
|
||||||
import org.lwjgl.opencl.CL10;
|
import org.lwjgl.opencl.CL10;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,8 +43,8 @@ public class LwjglCommandQueue extends CommandQueue {
|
|||||||
|
|
||||||
private final long queue;
|
private final long queue;
|
||||||
|
|
||||||
public LwjglCommandQueue(long queue) {
|
public LwjglCommandQueue(long queue, Device device) {
|
||||||
super(new ReleaserImpl(queue));
|
super(new ReleaserImpl(queue), device);
|
||||||
this.queue = queue;
|
this.queue = queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ public class LwjglContext extends Context {
|
|||||||
long properties = 0;
|
long properties = 0;
|
||||||
long q = CL10.clCreateCommandQueue(context, d, properties, Utils.errorBuffer);
|
long q = CL10.clCreateCommandQueue(context, d, properties, Utils.errorBuffer);
|
||||||
Utils.checkError(Utils.errorBuffer, "clCreateCommandQueue");
|
Utils.checkError(Utils.errorBuffer, "clCreateCommandQueue");
|
||||||
return new LwjglCommandQueue(q);
|
return new LwjglCommandQueue(q, device);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -125,4 +125,9 @@ public final class LwjglPlatform implements Platform {
|
|||||||
return Arrays.asList(Info.clGetPlatformInfoStringASCII(platform.address(), CL10.CL_PLATFORM_EXTENSIONS).split(" "));
|
return Arrays.asList(Info.clGetPlatformInfoStringASCII(platform.address(), CL10.CL_PLATFORM_EXTENSIONS).split(" "));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getName();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user