refactoring of Context: moved simplifying functions from LwjglContext to Context

define_list_fix
shamanDevel 9 years ago
parent ecda9135ad
commit a6be243db1
  1. 80
      jme3-core/src/main/java/com/jme3/opencl/Context.java
  2. 1
      jme3-examples/src/main/java/jme3test/opencl/HelloOpenCL.java
  3. 57
      jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglContext.java

@ -31,29 +31,44 @@
*/
package com.jme3.opencl;
import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetKey;
import com.jme3.asset.AssetManager;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.mesh.IndexBuffer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* The central OpenCL context. Every actions start from here.
*
* @author Sebastian Weiss
*/
public interface Context {
public abstract class Context {
private static final Logger LOG = Logger.getLogger(Context.class.getName());
List<? extends Device> getDevices();
public abstract List<? extends Device> getDevices();
CommandQueue createQueue();
CommandQueue createQueue(Device device);
public CommandQueue createQueue() {
return createQueue(getDevices().get(0));
}
public abstract CommandQueue createQueue(Device device);
Buffer createBuffer(int size, MemoryAccess access);
Buffer createBuffer(int size);
public abstract Buffer createBuffer(int size, MemoryAccess access);
public Buffer createBuffer(int size) {
return createBuffer(size, MemoryAccess.READ_WRITE);
}
Buffer createBufferFromHost(ByteBuffer data, MemoryAccess access);
Buffer createBufferFromHost(ByteBuffer data);
public abstract Buffer createBufferFromHost(ByteBuffer data, MemoryAccess access);
public Buffer createBufferFromHost(ByteBuffer data) {
return createBufferFromHost(data, MemoryAccess.READ_WRITE);
}
public static enum ImageChannelOrder {
@ -115,18 +130,49 @@ public interface Context {
public Buffer buffer;
}
Image createImage(MemoryAccess access, ImageFormat format, ImageDescriptor descr, ByteBuffer hostPtr);
public abstract Image createImage(MemoryAccess access, ImageFormat format, ImageDescriptor descr, ByteBuffer hostPtr);
//TODO: add simplified methods for 1D, 2D, 3D textures
//Interop
Buffer bindVertexBuffer(VertexBuffer vb);
Buffer bindIndexBuffer(IndexBuffer ib);
Image bindImage(com.jme3.texture.Image image);
public abstract Buffer bindVertexBuffer(VertexBuffer vb);
public abstract Buffer bindIndexBuffer(IndexBuffer ib);
public abstract Image bindImage(com.jme3.texture.Image image);
public abstract Program createProgramFromSourceCode(String sourceCode);
public Program createProgramFromSourceFilesWithInclude(AssetManager assetManager, String include, String... resources) {
return createProgramFromSourceFilesWithInclude(assetManager, include, Arrays.asList(resources));
}
Program createProgramFromSourceCode(String sourceCode);
Program createProgramFromSourceFilesWithInclude(AssetManager assetManager, String include, String... resources);
Program createProgramFromSourceFilesWithInclude(AssetManager assetManager, String include, List<String> resources);
Program createProgramFromSourceFiles(AssetManager assetManager, String... resources);
Program createProgramFromSourceFiles(AssetManager assetManager, List<String> resources);
public Program createProgramFromSourceFilesWithInclude(AssetManager assetManager, String include, List<String> resources) {
StringBuilder str = new StringBuilder();
str.append(include);
for (String res : resources) {
AssetInfo info = assetManager.locateAsset(new AssetKey<String>(res));
if (info == null) {
LOG.log(Level.WARNING, "unable to load source file ''{0}''", res);
continue;
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(info.openStream()))) {
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
str.append(line).append('\n');
}
} catch (IOException ex) {
LOG.log(Level.WARNING, "unable to load source file '"+res+"'", ex);
}
}
return createProgramFromSourceCode(str.toString());
}
public Program createProgramFromSourceFiles(AssetManager assetManager, String... resources) {
return createProgramFromSourceFilesWithInclude(assetManager, "", resources);
}
public Program createProgramFromSourceFiles(AssetManager assetManager, List<String> resources) {
return createProgramFromSourceFilesWithInclude(assetManager, "", resources);
}
}

@ -53,6 +53,7 @@ public class HelloOpenCL extends SimpleApplication {
HelloOpenCL app = new HelloOpenCL();
AppSettings settings = new AppSettings(true);
settings.setOpenCLSupport(true);
settings.setVSync(true);
app.setSettings(settings);
app.start(); // start the game
}

@ -52,7 +52,7 @@ import sun.misc.IOUtils;
*
* @author Sebastian Weiss
*/
public class LwjglContext implements Context {
public class LwjglContext extends Context {
private static final Logger LOG = Logger.getLogger(LwjglContext.class.getName());
private final CLContext context;
private final List<LwjglDevice> devices;
@ -71,11 +71,6 @@ public class LwjglContext implements Context {
return devices;
}
@Override
public CommandQueue createQueue() {
return createQueue(devices.get(0));
}
@Override
@SuppressWarnings("element-type-mismatch")
public CommandQueue createQueue(Device device) {
@ -95,11 +90,6 @@ public class LwjglContext implements Context {
return new LwjglBuffer(mem);
}
@Override
public Buffer createBuffer(int size) {
return createBuffer(size, MemoryAccess.READ_WRITE);
}
@Override
public Buffer createBufferFromHost(ByteBuffer data, MemoryAccess access) {
long flags = Utils.getMemoryAccessFlags(access);
@ -109,11 +99,6 @@ public class LwjglContext implements Context {
return new LwjglBuffer(mem);
}
@Override
public Buffer createBufferFromHost(ByteBuffer data) {
return createBufferFromHost(data, MemoryAccess.READ_WRITE);
}
@Override
public Image createImage(MemoryAccess access, ImageFormat format, ImageDescriptor descr, ByteBuffer hostPtr) {
throw new UnsupportedOperationException("Not supported yet.");
@ -142,45 +127,5 @@ public class LwjglContext implements Context {
Utils.checkError(Utils.errorBuffer, "clCreateProgramWithSource");
return new LwjglProgram(p, this);
}
@Override
public Program createProgramFromSourceFilesWithInclude(AssetManager assetManager, String include, String... resources) {
return createProgramFromSourceFilesWithInclude(assetManager, include, Arrays.asList(resources));
}
@Override
public Program createProgramFromSourceFilesWithInclude(AssetManager assetManager, String include, List<String> resources) {
StringBuilder str = new StringBuilder();
str.append(include);
for (String res : resources) {
AssetInfo info = assetManager.locateAsset(new AssetKey<String>(res));
if (info == null) {
LOG.log(Level.WARNING, "unable to load source file ''{0}''", res);
continue;
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(info.openStream()))) {
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
str.append(line).append('\n');
}
} catch (IOException ex) {
LOG.log(Level.WARNING, "unable to load source file '"+res+"'", ex);
}
}
return createProgramFromSourceCode(str.toString());
}
@Override
public Program createProgramFromSourceFiles(AssetManager assetManager, String... resources) {
return createProgramFromSourceFilesWithInclude(assetManager, "", resources);
}
@Override
public Program createProgramFromSourceFiles(AssetManager assetManager, List<String> resources) {
return createProgramFromSourceFilesWithInclude(assetManager, "", resources);
}
}

Loading…
Cancel
Save