|
@@ -40,11 +40,19 @@ import com.jme3.scene.Geometry;
|
|
import com.jme3.scene.VertexBuffer;
|
|
import com.jme3.scene.VertexBuffer;
|
|
import com.jme3.scene.shape.Box;
|
|
import com.jme3.scene.shape.Box;
|
|
import com.jme3.system.AppSettings;
|
|
import com.jme3.system.AppSettings;
|
|
|
|
+import com.jme3.system.JmeSystem;
|
|
|
|
+import com.jme3.util.BufferUtils;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.nio.ByteBuffer;
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
/**
|
|
/**
|
|
* This test class tests the capability to read and modify an OpenGL vertex buffer.
|
|
* This test class tests the capability to read and modify an OpenGL vertex buffer.
|
|
- *
|
|
|
|
|
|
+ * It is also shown how to use the program binaries to implement a simple program
|
|
|
|
+ * cache.
|
|
* @author shaman
|
|
* @author shaman
|
|
*/
|
|
*/
|
|
public class TestVertexBufferSharing extends SimpleApplication {
|
|
public class TestVertexBufferSharing extends SimpleApplication {
|
|
@@ -106,19 +114,48 @@ public class TestVertexBufferSharing extends SimpleApplication {
|
|
|
|
|
|
private void initOpenCL1() {
|
|
private void initOpenCL1() {
|
|
clContext = context.getOpenCLContext();
|
|
clContext = context.getOpenCLContext();
|
|
- clQueue = clContext.createQueue();
|
|
|
|
|
|
+ Device device = clContext.getDevices().get(0);
|
|
|
|
+ clQueue = clContext.createQueue(device);
|
|
clQueue.register();
|
|
clQueue.register();
|
|
//create kernel
|
|
//create kernel
|
|
- String source = ""
|
|
|
|
- + "__kernel void ScaleKernel(__global float* vb, float scale)\n"
|
|
|
|
- + "{\n"
|
|
|
|
- + " int idx = get_global_id(0);\n"
|
|
|
|
- + " float3 pos = vload3(idx, vb);\n"
|
|
|
|
- + " pos *= scale;\n"
|
|
|
|
- + " vstore3(pos, idx, vb);\n"
|
|
|
|
- + "}\n";
|
|
|
|
- Program program = clContext.createProgramFromSourceCode(source);
|
|
|
|
- program.build();
|
|
|
|
|
|
+ Program program = null;
|
|
|
|
+ File tmpFolder = JmeSystem.getStorageFolder();
|
|
|
|
+ File binaryFile = new File(tmpFolder, getClass().getSimpleName()+".clc");
|
|
|
|
+ try {
|
|
|
|
+ //attempt to load cached binary
|
|
|
|
+ byte[] bytes = Files.readAllBytes(binaryFile.toPath());
|
|
|
|
+ ByteBuffer bb = BufferUtils.createByteBuffer(bytes);
|
|
|
|
+ program = clContext.createProgramFromBinary(bb, device);
|
|
|
|
+ program.build();
|
|
|
|
+ LOG.info("reuse program from cached binaries");
|
|
|
|
+ } catch (java.nio.file.NoSuchFileException ex) {
|
|
|
|
+ //do nothing, cache was not created yet
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
+ LOG.log(Level.INFO, "Unable to use cached program binaries", ex);
|
|
|
|
+ }
|
|
|
|
+ if (program == null) {
|
|
|
|
+ //build from sources
|
|
|
|
+ String source = ""
|
|
|
|
+ + "__kernel void ScaleKernel(__global float* vb, float scale)\n"
|
|
|
|
+ + "{\n"
|
|
|
|
+ + " int idx = get_global_id(0);\n"
|
|
|
|
+ + " float3 pos = vload3(idx, vb);\n"
|
|
|
|
+ + " pos *= scale;\n"
|
|
|
|
+ + " vstore3(pos, idx, vb);\n"
|
|
|
|
+ + "}\n";
|
|
|
|
+ program = clContext.createProgramFromSourceCode(source);
|
|
|
|
+ program.build();
|
|
|
|
+ //Save binary
|
|
|
|
+ ByteBuffer bb = program.getBinary(device);
|
|
|
|
+ byte[] bytes = new byte[bb.remaining()];
|
|
|
|
+ bb.get(bytes);
|
|
|
|
+ try {
|
|
|
|
+ Files.write(binaryFile.toPath(), bytes);
|
|
|
|
+ } catch (IOException ex) {
|
|
|
|
+ LOG.log(Level.SEVERE, "Unable to save program binaries", ex);
|
|
|
|
+ }
|
|
|
|
+ LOG.info("create new program from sources");
|
|
|
|
+ }
|
|
program.register();
|
|
program.register();
|
|
kernel = program.createKernel("ScaleKernel");
|
|
kernel = program.createKernel("ScaleKernel");
|
|
kernel.register();
|
|
kernel.register();
|