From 25430d0734d12d12ca2d2d7a9d18c0cf3c5bdc4e Mon Sep 17 00:00:00 2001 From: Jason Sams Date: Tue, 2 Feb 2010 15:26:40 -0800 Subject: Implement holders for Matrix and Vector data. --- .../java/android/renderscript/FieldPacker.java | 125 ++++++++++++++ graphics/java/android/renderscript/Matrix.java | 192 --------------------- graphics/java/android/renderscript/Matrix2f.java | 58 +++++++ graphics/java/android/renderscript/Matrix3f.java | 63 +++++++ graphics/java/android/renderscript/Matrix4f.java | 189 ++++++++++++++++++++ .../java/android/renderscript/ProgramVertex.java | 22 +-- graphics/java/android/renderscript/Vector2f.java | 37 ++++ graphics/java/android/renderscript/Vector3f.java | 38 ++++ graphics/java/android/renderscript/Vector4f.java | 38 ++++ 9 files changed, 559 insertions(+), 203 deletions(-) create mode 100644 graphics/java/android/renderscript/FieldPacker.java delete mode 100644 graphics/java/android/renderscript/Matrix.java create mode 100644 graphics/java/android/renderscript/Matrix2f.java create mode 100644 graphics/java/android/renderscript/Matrix3f.java create mode 100644 graphics/java/android/renderscript/Matrix4f.java create mode 100644 graphics/java/android/renderscript/Vector2f.java create mode 100644 graphics/java/android/renderscript/Vector3f.java create mode 100644 graphics/java/android/renderscript/Vector4f.java (limited to 'graphics/java') diff --git a/graphics/java/android/renderscript/FieldPacker.java b/graphics/java/android/renderscript/FieldPacker.java new file mode 100644 index 0000000..fc79caf --- /dev/null +++ b/graphics/java/android/renderscript/FieldPacker.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + + +public class FieldPacker { + public FieldPacker(int len) { + mPos = 0; + mData = new byte[len]; + } + + public void align(int v) { + while ((mPos & (v - 1)) != 0) { + mData[mPos++] = 0; + } + } + + void reset() { + mPos = 0; + } + + void addI8(byte v) { + mData[mPos++] = v; + } + + void addI16(short v) { + align(2); + mData[mPos++] = (byte)(v & 0xff); + mData[mPos++] = (byte)(v >> 8); + } + + void addI32(int v) { + align(4); + mData[mPos++] = (byte)(v & 0xff); + mData[mPos++] = (byte)((v >> 8) & 0xff); + mData[mPos++] = (byte)((v >> 16) & 0xff); + mData[mPos++] = (byte)((v >> 24) & 0xff); + } + + void addI64(long v) { + align(8); + mData[mPos++] = (byte)(v & 0xff); + mData[mPos++] = (byte)((v >> 8) & 0xff); + mData[mPos++] = (byte)((v >> 16) & 0xff); + mData[mPos++] = (byte)((v >> 24) & 0xff); + mData[mPos++] = (byte)((v >> 32) & 0xff); + mData[mPos++] = (byte)((v >> 40) & 0xff); + mData[mPos++] = (byte)((v >> 48) & 0xff); + mData[mPos++] = (byte)((v >> 56) & 0xff); + } + + void addU8(short v) { + if ((v < 0) || (v > 0xff)) { + throw new IllegalArgumentException("Saving value out of range for type"); + } + mData[mPos++] = (byte)v; + } + + void addU16(int v) { + if ((v < 0) || (v > 0xffff)) { + throw new IllegalArgumentException("Saving value out of range for type"); + } + align(2); + mData[mPos++] = (byte)(v & 0xff); + mData[mPos++] = (byte)(v >> 8); + } + + void addU32(long v) { + if ((v < 0) || (v > 0xffffffff)) { + throw new IllegalArgumentException("Saving value out of range for type"); + } + align(4); + mData[mPos++] = (byte)(v & 0xff); + mData[mPos++] = (byte)((v >> 8) & 0xff); + mData[mPos++] = (byte)((v >> 16) & 0xff); + mData[mPos++] = (byte)((v >> 24) & 0xff); + } + + void addU64(long v) { + if (v < 0) { + throw new IllegalArgumentException("Saving value out of range for type"); + } + align(8); + mData[mPos++] = (byte)(v & 0xff); + mData[mPos++] = (byte)((v >> 8) & 0xff); + mData[mPos++] = (byte)((v >> 16) & 0xff); + mData[mPos++] = (byte)((v >> 24) & 0xff); + mData[mPos++] = (byte)((v >> 32) & 0xff); + mData[mPos++] = (byte)((v >> 40) & 0xff); + mData[mPos++] = (byte)((v >> 48) & 0xff); + mData[mPos++] = (byte)((v >> 56) & 0xff); + } + + void addF32(float v) { + addI32(Float.floatToRawIntBits(v)); + } + + void addF64(float v) { + addI64(Double.doubleToRawLongBits(v)); + } + + final byte[] getData() { + return mData; + } + + private final byte mData[]; + private int mPos; + +} + + diff --git a/graphics/java/android/renderscript/Matrix.java b/graphics/java/android/renderscript/Matrix.java deleted file mode 100644 index a266d6b..0000000 --- a/graphics/java/android/renderscript/Matrix.java +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.renderscript; - -import java.lang.Math; -import android.util.Log; - - -/** - * @hide - * - **/ -public class Matrix { - - public Matrix() { - mMat = new float[16]; - loadIdentity(); - } - - public float get(int i, int j) { - return mMat[i*4 + j]; - } - - public void set(int i, int j, float v) { - mMat[i*4 + j] = v; - } - - public void loadIdentity() { - mMat[0] = 1; - mMat[1] = 0; - mMat[2] = 0; - mMat[3] = 0; - - mMat[4] = 0; - mMat[5] = 1; - mMat[6] = 0; - mMat[7] = 0; - - mMat[8] = 0; - mMat[9] = 0; - mMat[10] = 1; - mMat[11] = 0; - - mMat[12] = 0; - mMat[13] = 0; - mMat[14] = 0; - mMat[15] = 1; - } - - public void load(Matrix src) { - mMat = src.mMat; - } - - public void loadRotate(float rot, float x, float y, float z) { - float c, s; - mMat[3] = 0; - mMat[7] = 0; - mMat[11]= 0; - mMat[12]= 0; - mMat[13]= 0; - mMat[14]= 0; - mMat[15]= 1; - rot *= (float)(java.lang.Math.PI / 180.0f); - c = (float)java.lang.Math.cos(rot); - s = (float)java.lang.Math.sin(rot); - - float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z); - if (!(len != 1)) { - float recipLen = 1.f / len; - x *= recipLen; - y *= recipLen; - z *= recipLen; - } - float nc = 1.0f - c; - float xy = x * y; - float yz = y * z; - float zx = z * x; - float xs = x * s; - float ys = y * s; - float zs = z * s; - mMat[ 0] = x*x*nc + c; - mMat[ 4] = xy*nc - zs; - mMat[ 8] = zx*nc + ys; - mMat[ 1] = xy*nc + zs; - mMat[ 5] = y*y*nc + c; - mMat[ 9] = yz*nc - xs; - mMat[ 2] = zx*nc - ys; - mMat[ 6] = yz*nc + xs; - mMat[10] = z*z*nc + c; - } - - public void loadScale(float x, float y, float z) { - loadIdentity(); - mMat[0] = x; - mMat[5] = y; - mMat[10] = z; - } - - public void loadTranslate(float x, float y, float z) { - loadIdentity(); - mMat[12] = x; - mMat[13] = y; - mMat[14] = z; - } - - public void loadMultiply(Matrix lhs, Matrix rhs) { - for (int i=0 ; i<4 ; i++) { - float ri0 = 0; - float ri1 = 0; - float ri2 = 0; - float ri3 = 0; - for (int j=0 ; j<4 ; j++) { - float rhs_ij = rhs.get(i,j); - ri0 += lhs.get(j,0) * rhs_ij; - ri1 += lhs.get(j,1) * rhs_ij; - ri2 += lhs.get(j,2) * rhs_ij; - ri3 += lhs.get(j,3) * rhs_ij; - } - set(i,0, ri0); - set(i,1, ri1); - set(i,2, ri2); - set(i,3, ri3); - } - } - - public void loadOrtho(float l, float r, float b, float t, float n, float f) { - loadIdentity(); - mMat[0] = 2 / (r - l); - mMat[5] = 2 / (t - b); - mMat[10]= -2 / (f - n); - mMat[12]= -(r + l) / (r - l); - mMat[13]= -(t + b) / (t - b); - mMat[14]= -(f + n) / (f - n); - } - - public void loadFrustum(float l, float r, float b, float t, float n, float f) { - loadIdentity(); - mMat[0] = 2 * n / (r - l); - mMat[5] = 2 * n / (t - b); - mMat[8] = (r + l) / (r - l); - mMat[9] = (t + b) / (t - b); - mMat[10]= -(f + n) / (f - n); - mMat[11]= -1; - mMat[14]= -2*f*n / (f - n); - mMat[15]= 0; - } - - public void multiply(Matrix rhs) { - Matrix tmp = new Matrix(); - tmp.loadMultiply(this, rhs); - load(tmp); - } - public void rotate(float rot, float x, float y, float z) { - Matrix tmp = new Matrix(); - tmp.loadRotate(rot, x, y, z); - multiply(tmp); - } - public void scale(float x, float y, float z) { - Matrix tmp = new Matrix(); - tmp.loadScale(x, y, z); - multiply(tmp); - } - public void translate(float x, float y, float z) { - Matrix tmp = new Matrix(); - tmp.loadTranslate(x, y, z); - multiply(tmp); - } - - - - float[] mMat; - -} - - - - - diff --git a/graphics/java/android/renderscript/Matrix2f.java b/graphics/java/android/renderscript/Matrix2f.java new file mode 100644 index 0000000..4b5e61b --- /dev/null +++ b/graphics/java/android/renderscript/Matrix2f.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Matrix2f { + + public Matrix2f() { + mMat = new float[4]; + loadIdentity(); + } + + public float get(int i, int j) { + return mMat[i*2 + j]; + } + + public void set(int i, int j, float v) { + mMat[i*2 + j] = v; + } + + public void loadIdentity() { + mMat[0] = 1; + mMat[1] = 0; + + mMat[2] = 0; + mMat[3] = 1; + } + + public void load(Matrix2f src) { + System.arraycopy(mMat, 0, src, 0, 4); + } + + final float[] mMat; +} + + + diff --git a/graphics/java/android/renderscript/Matrix3f.java b/graphics/java/android/renderscript/Matrix3f.java new file mode 100644 index 0000000..19d7b43 --- /dev/null +++ b/graphics/java/android/renderscript/Matrix3f.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Matrix3f { + + public Matrix3f() { + mMat = new float[9]; + loadIdentity(); + } + + public float get(int i, int j) { + return mMat[i*3 + j]; + } + + public void set(int i, int j, float v) { + mMat[i*3 + j] = v; + } + + public void loadIdentity() { + mMat[0] = 1; + mMat[1] = 0; + mMat[2] = 0; + + mMat[3] = 0; + mMat[4] = 1; + mMat[5] = 0; + + mMat[6] = 0; + mMat[7] = 0; + mMat[8] = 1; + } + + public void load(Matrix3f src) { + System.arraycopy(mMat, 0, src, 0, 9); + } + + final float[] mMat; +} + + diff --git a/graphics/java/android/renderscript/Matrix4f.java b/graphics/java/android/renderscript/Matrix4f.java new file mode 100644 index 0000000..ebd5bde --- /dev/null +++ b/graphics/java/android/renderscript/Matrix4f.java @@ -0,0 +1,189 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Matrix4f { + + public Matrix4f() { + mMat = new float[16]; + loadIdentity(); + } + + public float get(int i, int j) { + return mMat[i*4 + j]; + } + + public void set(int i, int j, float v) { + mMat[i*4 + j] = v; + } + + public void loadIdentity() { + mMat[0] = 1; + mMat[1] = 0; + mMat[2] = 0; + mMat[3] = 0; + + mMat[4] = 0; + mMat[5] = 1; + mMat[6] = 0; + mMat[7] = 0; + + mMat[8] = 0; + mMat[9] = 0; + mMat[10] = 1; + mMat[11] = 0; + + mMat[12] = 0; + mMat[13] = 0; + mMat[14] = 0; + mMat[15] = 1; + } + + public void load(Matrix4f src) { + System.arraycopy(mMat, 0, src, 0, 16); + } + + public void loadRotate(float rot, float x, float y, float z) { + float c, s; + mMat[3] = 0; + mMat[7] = 0; + mMat[11]= 0; + mMat[12]= 0; + mMat[13]= 0; + mMat[14]= 0; + mMat[15]= 1; + rot *= (float)(java.lang.Math.PI / 180.0f); + c = (float)java.lang.Math.cos(rot); + s = (float)java.lang.Math.sin(rot); + + float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z); + if (!(len != 1)) { + float recipLen = 1.f / len; + x *= recipLen; + y *= recipLen; + z *= recipLen; + } + float nc = 1.0f - c; + float xy = x * y; + float yz = y * z; + float zx = z * x; + float xs = x * s; + float ys = y * s; + float zs = z * s; + mMat[ 0] = x*x*nc + c; + mMat[ 4] = xy*nc - zs; + mMat[ 8] = zx*nc + ys; + mMat[ 1] = xy*nc + zs; + mMat[ 5] = y*y*nc + c; + mMat[ 9] = yz*nc - xs; + mMat[ 2] = zx*nc - ys; + mMat[ 6] = yz*nc + xs; + mMat[10] = z*z*nc + c; + } + + public void loadScale(float x, float y, float z) { + loadIdentity(); + mMat[0] = x; + mMat[5] = y; + mMat[10] = z; + } + + public void loadTranslate(float x, float y, float z) { + loadIdentity(); + mMat[12] = x; + mMat[13] = y; + mMat[14] = z; + } + + public void loadMultiply(Matrix4f lhs, Matrix4f rhs) { + for (int i=0 ; i<4 ; i++) { + float ri0 = 0; + float ri1 = 0; + float ri2 = 0; + float ri3 = 0; + for (int j=0 ; j<4 ; j++) { + float rhs_ij = rhs.get(i,j); + ri0 += lhs.get(j,0) * rhs_ij; + ri1 += lhs.get(j,1) * rhs_ij; + ri2 += lhs.get(j,2) * rhs_ij; + ri3 += lhs.get(j,3) * rhs_ij; + } + set(i,0, ri0); + set(i,1, ri1); + set(i,2, ri2); + set(i,3, ri3); + } + } + + public void loadOrtho(float l, float r, float b, float t, float n, float f) { + loadIdentity(); + mMat[0] = 2 / (r - l); + mMat[5] = 2 / (t - b); + mMat[10]= -2 / (f - n); + mMat[12]= -(r + l) / (r - l); + mMat[13]= -(t + b) / (t - b); + mMat[14]= -(f + n) / (f - n); + } + + public void loadFrustum(float l, float r, float b, float t, float n, float f) { + loadIdentity(); + mMat[0] = 2 * n / (r - l); + mMat[5] = 2 * n / (t - b); + mMat[8] = (r + l) / (r - l); + mMat[9] = (t + b) / (t - b); + mMat[10]= -(f + n) / (f - n); + mMat[11]= -1; + mMat[14]= -2*f*n / (f - n); + mMat[15]= 0; + } + + public void multiply(Matrix4f rhs) { + Matrix4f tmp = new Matrix4f(); + tmp.loadMultiply(this, rhs); + load(tmp); + } + public void rotate(float rot, float x, float y, float z) { + Matrix4f tmp = new Matrix4f(); + tmp.loadRotate(rot, x, y, z); + multiply(tmp); + } + public void scale(float x, float y, float z) { + Matrix4f tmp = new Matrix4f(); + tmp.loadScale(x, y, z); + multiply(tmp); + } + public void translate(float x, float y, float z) { + Matrix4f tmp = new Matrix4f(); + tmp.loadTranslate(x, y, z); + multiply(tmp); + } + + final float[] mMat; +} + + + + + diff --git a/graphics/java/android/renderscript/ProgramVertex.java b/graphics/java/android/renderscript/ProgramVertex.java index 84f6f2d..1b155d7 100644 --- a/graphics/java/android/renderscript/ProgramVertex.java +++ b/graphics/java/android/renderscript/ProgramVertex.java @@ -96,16 +96,16 @@ public class ProgramVertex extends Program { static final int PROJECTION_OFFSET = 16; static final int TEXTURE_OFFSET = 32; - Matrix mModel; - Matrix mProjection; - Matrix mTexture; + Matrix4f mModel; + Matrix4f mProjection; + Matrix4f mTexture; public Allocation mAlloc; public MatrixAllocation(RenderScript rs) { - mModel = new Matrix(); - mProjection = new Matrix(); - mTexture = new Matrix(); + mModel = new Matrix4f(); + mProjection = new Matrix4f(); + mTexture = new Matrix4f(); mAlloc = Allocation.createSized(rs, Element.createUser(rs, Element.DataType.FLOAT_32), 48); mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat); @@ -118,17 +118,17 @@ public class ProgramVertex extends Program { mAlloc = null; } - public void loadModelview(Matrix m) { + public void loadModelview(Matrix4f m) { mModel = m; mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat); } - public void loadProjection(Matrix m) { + public void loadProjection(Matrix4f m) { mProjection = m; mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat); } - public void loadTexture(Matrix m) { + public void loadTexture(Matrix4f m) { mTexture = m; mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat); } @@ -152,8 +152,8 @@ public class ProgramVertex extends Program { public void setupProjectionNormalized(int w, int h) { // range -1,1 in the narrow axis at z = 0. - Matrix m1 = new Matrix(); - Matrix m2 = new Matrix(); + Matrix4f m1 = new Matrix4f(); + Matrix4f m2 = new Matrix4f(); if(w > h) { float aspect = ((float)w) / h; diff --git a/graphics/java/android/renderscript/Vector2f.java b/graphics/java/android/renderscript/Vector2f.java new file mode 100644 index 0000000..567d57f --- /dev/null +++ b/graphics/java/android/renderscript/Vector2f.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Vector2f { + public Vector2f() { + } + + public float x; + public float y; +} + + + + diff --git a/graphics/java/android/renderscript/Vector3f.java b/graphics/java/android/renderscript/Vector3f.java new file mode 100644 index 0000000..f2842f3 --- /dev/null +++ b/graphics/java/android/renderscript/Vector3f.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Vector3f { + public Vector3f() { + } + + public float x; + public float y; + public float z; +} + + + + diff --git a/graphics/java/android/renderscript/Vector4f.java b/graphics/java/android/renderscript/Vector4f.java new file mode 100644 index 0000000..fabd959 --- /dev/null +++ b/graphics/java/android/renderscript/Vector4f.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Vector4f { + public Vector4f() { + } + + public float x; + public float y; + public float z; + public float w; +} + + + -- cgit v1.1