Browse Source

added matrix support for kernel arguments (mapped to float16)

shamanDevel 9 years ago
parent
commit
c162d474c7

+ 27 - 4
jme3-core/src/main/java/com/jme3/opencl/Kernel.java

@@ -31,9 +31,8 @@
  */
 package com.jme3.opencl;
 
-import com.jme3.math.Quaternion;
-import com.jme3.math.Vector2f;
-import com.jme3.math.Vector4f;
+import com.jme3.math.*;
+import com.jme3.util.TempVars;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
 
@@ -237,6 +236,24 @@ public abstract class Kernel extends AbstractOpenCLObject {
 
     public abstract void setArg(int index, Quaternion q);
     
+    public abstract void setArg(int index, Matrix4f mat);
+    
+    public void setArg(int index, Matrix3f mat) {
+        TempVars vars = TempVars.get();
+        try {
+            Matrix4f m = vars.tempMat4;
+            m.zero();
+            for (int i=0; i<3; ++i) {
+                for (int j=0; j<3; ++j) {
+                    m.set(i, j, mat.get(i, j));
+                }
+            }
+            setArg(index, m);
+        } finally {
+            vars.release();
+        }
+    }
+    
     /**
      * Raw version to set an argument.
      * {@code size} bytes of the provided byte buffer are copied to the kernel
@@ -253,7 +270,9 @@ public abstract class Kernel extends AbstractOpenCLObject {
      * Sets the kernel argument at the specified index.<br>
      * The argument must be a known type:
      * {@code LocalMemPerElement, LocalMem, Image, Buffer, byte, short, int,
-     * long, float, double, Vector2f, Vector4f, Quaternion}
+     * long, float, double, Vector2f, Vector4f, Quaternion, Matrix3f, Matrix4f}.
+     * <br>
+     * Note: Matrix3f and Matrix4f will be mapped to a {@code float16} (row major).
      * @param index the index of the argument, from 0 to {@link #getArgCount()}-1
      * @param arg the argument
      * @throws IllegalArgumentException if the argument type is not one of the listed ones
@@ -277,6 +296,10 @@ public abstract class Kernel extends AbstractOpenCLObject {
             setArg(index, (Vector4f) arg);
         } else if (arg instanceof Quaternion) {
             setArg(index, (Quaternion) arg);
+        } else if (arg instanceof Matrix3f) {
+            setArg(index, (Matrix3f) arg);
+        } else if (arg instanceof Matrix4f) {
+            setArg(index, (Matrix4f) arg);
         } else if (arg instanceof LocalMemPerElement) {
             setArg(index, (LocalMemPerElement) arg);
         } else if (arg instanceof LocalMem) {

+ 14 - 0
jme3-jogl/src/main/java/com/jme3/opencl/jocl/JoclKernel.java

@@ -31,6 +31,7 @@
  */
 package com.jme3.opencl.jocl;
 
+import com.jme3.math.Matrix4f;
 import com.jme3.math.Quaternion;
 import com.jme3.math.Vector2f;
 import com.jme3.math.Vector4f;
@@ -213,6 +214,19 @@ public class JoclKernel extends Kernel {
         int ret = cl.clSetKernelArg(kernel, index, 16, buf);
         Utils.checkError(ret, "clSetKernelArg");
     }
+    
+    @Override
+    public void setArg(int index, Matrix4f m) {
+        FloatBuffer buf = Utils.b80f;
+        buf.position(0);
+        buf.limit(16);
+        buf.put(m.m00).put(m.m01).put(m.m02).put(m.m03);
+        buf.put(m.m10).put(m.m11).put(m.m12).put(m.m13);
+        buf.put(m.m20).put(m.m21).put(m.m22).put(m.m23);
+        buf.put(m.m30).put(m.m31).put(m.m32).put(m.m33);
+        int ret = cl.clSetKernelArg(kernel, index, 16*4, buf);
+        Utils.checkError(ret, "clSetKernelArg");
+    }
 
     @Override
     public void setArg(int index, ByteBuffer buffer, long size) {

+ 2 - 0
jme3-jogl/src/main/java/com/jme3/opencl/jocl/Utils.java

@@ -86,6 +86,7 @@ public class Utils {
     }
     public static final ByteBuffer b80; //needed for ImageDescriptor
     public static final LongBuffer b80l;
+    public static final FloatBuffer b80f;
     public static final TempBuffer[] tempBuffers = new TempBuffer[8];
     public static final PointerBuffer[] pointers = new PointerBuffer[8];
     static {
@@ -96,6 +97,7 @@ public class Utils {
         errorBuffer = BufferUtils.createIntBuffer(1);
         b80 = BufferUtils.createByteBuffer(80);
         b80l = b80.asLongBuffer();
+        b80f = b80.asFloatBuffer();
     }
     
     public static IntBuffer errorBuffer;

+ 14 - 0
jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/LwjglKernel.java

@@ -31,6 +31,7 @@
  */
 package com.jme3.opencl.lwjgl;
 
+import com.jme3.math.Matrix4f;
 import com.jme3.math.Quaternion;
 import com.jme3.math.Vector2f;
 import com.jme3.math.Vector4f;
@@ -196,6 +197,19 @@ public class LwjglKernel extends Kernel {
         int ret = CL10.clSetKernelArg(kernel, index, buf);
         Utils.checkError(ret, "clSetKernelArg");
     }
+    
+    @Override
+    public void setArg(int index, Matrix4f m) {
+        FloatBuffer buf = Utils.b80f;
+        buf.position(0);
+        buf.limit(16);
+        buf.put(m.m00).put(m.m01).put(m.m02).put(m.m03);
+        buf.put(m.m10).put(m.m11).put(m.m12).put(m.m13);
+        buf.put(m.m20).put(m.m21).put(m.m22).put(m.m23);
+        buf.put(m.m30).put(m.m31).put(m.m32).put(m.m33);
+        int ret = CL10.clSetKernelArg(kernel, index, buf);
+        Utils.checkError(ret, "clSetKernelArg");
+    }
 
     @Override
     public void setArg(int index, ByteBuffer buffer, long size) {

+ 3 - 1
jme3-lwjgl/src/main/java/com/jme3/opencl/lwjgl/Utils.java

@@ -89,8 +89,9 @@ public class Utils {
             b16d = b16.asDoubleBuffer();
         }
     }
-    public static final ByteBuffer b80; //needed for ImageDescriptor
+    public static final ByteBuffer b80; //needed for ImageDescriptor and Matrix4f
     public static final LongBuffer b80l;
+    public static final FloatBuffer b80f;
     public static final TempBuffer[] tempBuffers = new TempBuffer[8];
     public static final PointerBuffer[] pointerBuffers = new PointerBuffer[8];
     static {
@@ -101,6 +102,7 @@ public class Utils {
         errorBuffer = BufferUtils.createIntBuffer(1);
         b80 = BufferUtils.createByteBuffer(80);
         b80l = b80.asLongBuffer();
+        b80f = b80.asFloatBuffer();
     }
     
     public static IntBuffer errorBuffer;

+ 14 - 0
jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/LwjglKernel.java

@@ -31,6 +31,7 @@
  */
 package com.jme3.opencl.lwjgl;
 
+import com.jme3.math.Matrix4f;
 import com.jme3.math.Quaternion;
 import com.jme3.math.Vector2f;
 import com.jme3.math.Vector4f;
@@ -152,6 +153,19 @@ public class LwjglKernel extends Kernel {
         int ret = CL10.clSetKernelArg4f(kernel, index, q.getX(), q.getY(), q.getZ(), q.getW());
         Utils.checkError(ret, "clSetKernelArg");
     }
+    
+    @Override
+    public void setArg(int index, Matrix4f m) {
+        FloatBuffer buf = Utils.b80f;
+        buf.position(0);
+        buf.limit(16);
+        buf.put(m.m00).put(m.m01).put(m.m02).put(m.m03);
+        buf.put(m.m10).put(m.m11).put(m.m12).put(m.m13);
+        buf.put(m.m20).put(m.m21).put(m.m22).put(m.m23);
+        buf.put(m.m30).put(m.m31).put(m.m32).put(m.m33);
+        int ret = CL10.clSetKernelArg(kernel, index, buf);
+        Utils.checkError(ret, "clSetKernelArg");
+    }
 
     @Override
     public void setArg(int index, ByteBuffer buffer, long size) {

+ 2 - 0
jme3-lwjgl3/src/main/java/com/jme3/opencl/lwjgl/Utils.java

@@ -87,6 +87,7 @@ public class Utils {
     }
     public static final ByteBuffer b80; //needed for ImageDescriptor
     public static final LongBuffer b80l;
+    public static final FloatBuffer b80f;
     public static final TempBuffer[] tempBuffers = new TempBuffer[8];
     public static final PointerBuffer[] pointerBuffers = new PointerBuffer[8];
     static {
@@ -97,6 +98,7 @@ public class Utils {
         errorBuffer = BufferUtils.createIntBuffer(1);
         b80 = BufferUtils.createByteBuffer(80);
         b80l = b80.asLongBuffer();
+        b80f = b80.asFloatBuffer();
     }
     
     public static IntBuffer errorBuffer;