Implement holders for Matrix and Vector data.

This commit is contained in:
Jason Sams
2010-02-02 15:26:40 -08:00
parent 5dbfe93b3f
commit 25430d0734
8 changed files with 385 additions and 29 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -24,9 +24,9 @@ import android.util.Log;
* @hide
*
**/
public class Matrix {
public class Matrix4f {
public Matrix() {
public Matrix4f() {
mMat = new float[16];
loadIdentity();
}
@@ -49,7 +49,7 @@ public class Matrix {
mMat[5] = 1;
mMat[6] = 0;
mMat[7] = 0;
mMat[8] = 0;
mMat[9] = 0;
mMat[10] = 1;
@@ -61,8 +61,8 @@ public class Matrix {
mMat[15] = 1;
}
public void load(Matrix src) {
mMat = src.mMat;
public void load(Matrix4f src) {
System.arraycopy(mMat, 0, src, 0, 16);
}
public void loadRotate(float rot, float x, float y, float z) {
@@ -77,7 +77,7 @@ public class Matrix {
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;
@@ -91,7 +91,7 @@ public class Matrix {
float zx = z * x;
float xs = x * s;
float ys = y * s;
float zs = z * s;
float zs = z * s;
mMat[ 0] = x*x*nc + c;
mMat[ 4] = xy*nc - zs;
mMat[ 8] = zx*nc + ys;
@@ -109,7 +109,7 @@ public class Matrix {
mMat[5] = y;
mMat[10] = z;
}
public void loadTranslate(float x, float y, float z) {
loadIdentity();
mMat[12] = x;
@@ -117,7 +117,7 @@ public class Matrix {
mMat[14] = z;
}
public void loadMultiply(Matrix lhs, Matrix rhs) {
public void loadMultiply(Matrix4f lhs, Matrix4f rhs) {
for (int i=0 ; i<4 ; i++) {
float ri0 = 0;
float ri1 = 0;
@@ -159,31 +159,28 @@ public class Matrix {
mMat[15]= 0;
}
public void multiply(Matrix rhs) {
Matrix tmp = new Matrix();
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) {
Matrix tmp = new Matrix();
Matrix4f tmp = new Matrix4f();
tmp.loadRotate(rot, x, y, z);
multiply(tmp);
}
public void scale(float x, float y, float z) {
Matrix tmp = new Matrix();
Matrix4f tmp = new Matrix4f();
tmp.loadScale(x, y, z);
multiply(tmp);
}
public void translate(float x, float y, float z) {
Matrix tmp = new Matrix();
Matrix4f tmp = new Matrix4f();
tmp.loadTranslate(x, y, z);
multiply(tmp);
}
float[] mMat;
final float[] mMat;
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}