Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2a594b1a authored by Jason Sams's avatar Jason Sams Committed by Android (Google) Code Review
Browse files

Merge "Implement holders for Matrix and Vector data."

parents 37b22176 25430d07
Loading
Loading
Loading
Loading
+125 −0
Original line number Diff line number Diff line
/*
 * 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;

}

+58 −0
Original line number Diff line number Diff line
/*
 * 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;
}


+63 −0
Original line number Diff line number Diff line
/*
 * 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;
}

+15 −18
Original line number Diff line number Diff line
@@ -24,9 +24,9 @@ import android.util.Log;
 * @hide
 *
 **/
public class Matrix {
public class Matrix4f {

    public Matrix() {
    public Matrix4f() {
        mMat = new float[16];
        loadIdentity();
    }
@@ -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) {
@@ -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;
}


+11 −11
Original line number Diff line number Diff line
@@ -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;
Loading