summaryrefslogtreecommitdiffstats
path: root/graphics/java/android/renderscript/ProgramVertex.java
blob: 84f6f2d03e17ebc4440b7a380a41dc7ff587a799 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * 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;


import android.util.Config;
import android.util.Log;


/**
 * @hide
 *
 **/
public class ProgramVertex extends Program {
    public static final int MAX_LIGHT = 8;


    ProgramVertex(int id, RenderScript rs) {
        super(id, rs);
    }

    public void bindAllocation(MatrixAllocation va) {
        mRS.validate();
        bindConstants(va.mAlloc, 0);
    }


    public static class Builder {
        RenderScript mRS;
        boolean mTextureMatrixEnable;

        public Builder(RenderScript rs, Element in, Element out) {
            mRS = rs;
        }

        public void setTextureMatrixEnable(boolean enable) {
            mTextureMatrixEnable = enable;
        }

        public ProgramVertex create() {
            int id = mRS.nProgramVertexCreate(mTextureMatrixEnable);
            return new ProgramVertex(id, mRS);
        }
    }

    public static class ShaderBuilder extends BaseProgramBuilder {
        public ShaderBuilder(RenderScript rs) {
            super(rs);
        }

        public ProgramVertex create() {
            mRS.validate();
            int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount +1) * 2];
            int idx = 0;

            for (int i=0; i < mInputCount; i++) {
                tmp[idx++] = 0;
                tmp[idx++] = mInputs[i].mID;
            }
            for (int i=0; i < mOutputCount; i++) {
                tmp[idx++] = 1;
                tmp[idx++] = mOutputs[i].mID;
            }
            for (int i=0; i < mConstantCount; i++) {
                tmp[idx++] = 2;
                tmp[idx++] = mConstants[i].mID;
            }
            tmp[idx++] = 3;
            tmp[idx++] = mTextureCount;

            int id = mRS.nProgramVertexCreate2(mShader, tmp);
            ProgramVertex pv = new ProgramVertex(id, mRS);
            initProgram(pv);
            return pv;
        }
    }



    public static class MatrixAllocation {
        static final int MODELVIEW_OFFSET = 0;
        static final int PROJECTION_OFFSET = 16;
        static final int TEXTURE_OFFSET = 32;

        Matrix mModel;
        Matrix mProjection;
        Matrix mTexture;

        public Allocation mAlloc;

        public MatrixAllocation(RenderScript rs) {
            mModel = new Matrix();
            mProjection = new Matrix();
            mTexture = new Matrix();

            mAlloc = Allocation.createSized(rs, Element.createUser(rs, Element.DataType.FLOAT_32), 48);
            mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
            mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
            mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
        }

        public void destroy() {
            mAlloc.destroy();
            mAlloc = null;
        }

        public void loadModelview(Matrix m) {
            mModel = m;
            mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat);
        }

        public void loadProjection(Matrix m) {
            mProjection = m;
            mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat);
        }

        public void loadTexture(Matrix m) {
            mTexture = m;
            mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat);
        }

        public void setupOrthoWindow(int w, int h) {
            mProjection.loadOrtho(0,w, h,0, -1,1);
            mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
        }

        public void setupOrthoNormalized(int w, int h) {
            // range -1,1 in the narrow axis.
            if(w > h) {
                float aspect = ((float)w) / h;
                mProjection.loadOrtho(-aspect,aspect,  -1,1,  -1,1);
            } else {
                float aspect = ((float)h) / w;
                mProjection.loadOrtho(-1,1, -aspect,aspect,  -1,1);
            }
            mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
        }

        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();

            if(w > h) {
                float aspect = ((float)w) / h;
                m1.loadFrustum(-aspect,aspect,  -1,1,  1,100);
            } else {
                float aspect = ((float)h) / w;
                m1.loadFrustum(-1,1, -aspect,aspect, 1,100);
            }

            m2.loadRotate(180, 0, 1, 0);
            m1.loadMultiply(m1, m2);

            m2.loadScale(-2, 2, 1);
            m1.loadMultiply(m1, m2);

            m2.loadTranslate(0, 0, 2);
            m1.loadMultiply(m1, m2);

            mProjection = m1;
            mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
        }

    }

}