1/*
2 * Copyright (C) 2008-2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
19
20import android.graphics.Matrix;
21import android.util.Log;
22
23
24/**
25 * @deprecated in API 16
26 * ProgramVertexFixedFunction is a helper class that provides a
27 * simple way to create a fixed function emulation vertex shader
28 * without writing any GLSL code.
29 *
30 **/
31public class ProgramVertexFixedFunction extends ProgramVertex {
32
33    ProgramVertexFixedFunction(int id, RenderScript rs) {
34        super(id, rs);
35    }
36
37    /**
38     * @deprecated in API 16
39     * Binds the constant buffer containing fixed function emulation
40     * matrices
41     *
42     * @param va allocation containing fixed function matrices
43     */
44    public void bindConstants(Constants va) {
45        mRS.validate();
46        bindConstants(va.getAllocation(), 0);
47    }
48
49    static class InternalBuilder extends BaseProgramBuilder {
50        /**
51         * @deprecated in API 16
52         */
53        public InternalBuilder(RenderScript rs) {
54            super(rs);
55        }
56
57        /**
58         * @deprecated in API 16
59         */
60        public InternalBuilder addInput(Element e) throws IllegalStateException {
61            // Should check for consistant and non-conflicting names...
62            if(mInputCount >= MAX_INPUT) {
63                throw new RSIllegalArgumentException("Max input count exceeded.");
64            }
65            if (e.isComplex()) {
66                throw new RSIllegalArgumentException("Complex elements not allowed.");
67            }
68            mInputs[mInputCount++] = e;
69            return this;
70        }
71
72        /**
73         * @deprecated in API 16
74         * Creates ProgramVertexFixedFunction from the current state of
75         * the builder
76         *
77         * @return  ProgramVertexFixedFunction
78         */
79        public ProgramVertexFixedFunction create() {
80            mRS.validate();
81            int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
82            String[] texNames = new String[mTextureCount];
83            int idx = 0;
84
85            for (int i=0; i < mInputCount; i++) {
86                tmp[idx++] = ProgramParam.INPUT.mID;
87                tmp[idx++] = mInputs[i].getID(mRS);
88            }
89            for (int i=0; i < mOutputCount; i++) {
90                tmp[idx++] = ProgramParam.OUTPUT.mID;
91                tmp[idx++] = mOutputs[i].getID(mRS);
92            }
93            for (int i=0; i < mConstantCount; i++) {
94                tmp[idx++] = ProgramParam.CONSTANT.mID;
95                tmp[idx++] = mConstants[i].getID(mRS);
96            }
97            for (int i=0; i < mTextureCount; i++) {
98                tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
99                tmp[idx++] = mTextureTypes[i].mID;
100                texNames[i] = mTextureNames[i];
101            }
102
103            int id = mRS.nProgramVertexCreate(mShader, texNames, tmp);
104            ProgramVertexFixedFunction pv = new ProgramVertexFixedFunction(id, mRS);
105            initProgram(pv);
106            return pv;
107        }
108    }
109
110    /**
111     * @deprecated in API 16
112     */
113    public static class Builder {
114        boolean mTextureMatrixEnable;
115        String mShader;
116        RenderScript mRS;
117
118        /**
119         * @deprecated in API 16
120         * Creates a builder for fixed function vertex program
121         *
122         * @param rs Context to which the program will belong.
123         */
124        public Builder(RenderScript rs) {
125            mRS = rs;
126        }
127
128        /**
129         * @deprecated in API 16
130         * Specifies whether texture matrix calculations are to be added
131         * to the shader
132         *
133         */
134        public Builder setTextureMatrixEnable(boolean enable) {
135            mTextureMatrixEnable = enable;
136            return this;
137        }
138        static Type getConstantInputType(RenderScript rs) {
139            Element.Builder b = new Element.Builder(rs);
140            b.add(Element.MATRIX4X4(rs), "MV");
141            b.add(Element.MATRIX4X4(rs), "P");
142            b.add(Element.MATRIX4X4(rs), "TexMatrix");
143            b.add(Element.MATRIX4X4(rs), "MVP");
144
145            Type.Builder typeBuilder = new Type.Builder(rs, b.create());
146            typeBuilder.setX(1);
147            return typeBuilder.create();
148        }
149
150        private void buildShaderString() {
151
152            mShader  = "//rs_shader_internal\n";
153            mShader += "varying vec4 varColor;\n";
154            mShader += "varying vec2 varTex0;\n";
155
156            mShader += "void main() {\n";
157            mShader += "  gl_Position = UNI_MVP * ATTRIB_position;\n";
158            mShader += "  gl_PointSize = 1.0;\n";
159
160            mShader += "  varColor = ATTRIB_color;\n";
161            if (mTextureMatrixEnable) {
162                mShader += "  varTex0 = (UNI_TexMatrix * vec4(ATTRIB_texture0, 0.0, 1.0)).xy;\n";
163            } else {
164                mShader += "  varTex0 = ATTRIB_texture0;\n";
165            }
166            mShader += "}\n";
167        }
168
169        /**
170         * @deprecated in API 16
171         * Creates ProgramVertexFixedFunction from the current state of
172         * the builder
173         *
174         * @return Fixed function emulation ProgramVertex
175         */
176        public ProgramVertexFixedFunction create() {
177            buildShaderString();
178
179            InternalBuilder sb = new InternalBuilder(mRS);
180            sb.setShader(mShader);
181            sb.addConstant(getConstantInputType(mRS));
182
183            Element.Builder b = new Element.Builder(mRS);
184            b.add(Element.F32_4(mRS), "position");
185            b.add(Element.F32_4(mRS), "color");
186            b.add(Element.F32_3(mRS), "normal");
187            b.add(Element.F32_2(mRS), "texture0");
188            sb.addInput(b.create());
189
190            return sb.create();
191        }
192    }
193
194    /**
195     * @deprecated in API 16
196     * Helper class to store modelview, projection and texture
197     * matrices for ProgramVertexFixedFunction
198     *
199     */
200    public static class Constants {
201        static final int MODELVIEW_OFFSET = 0;
202        static final int PROJECTION_OFFSET = 16;
203        static final int TEXTURE_OFFSET = 32;
204
205        Matrix4f mModel;
206        Matrix4f mProjection;
207        Matrix4f mTexture;
208
209        Allocation mAlloc;
210        Allocation getAllocation() {
211            return mAlloc;
212        }
213        private FieldPacker mIOBuffer;
214
215        /**
216        * @deprecated in API 16
217        * Creates a buffer to store fixed function emulation matrices
218        *
219        * @param rs Context to which the allocation will belong.
220        **/
221        public Constants(RenderScript rs) {
222            Type constInputType = ProgramVertexFixedFunction.Builder.getConstantInputType(rs);
223            mAlloc = Allocation.createTyped(rs, constInputType);
224            int bufferSize = constInputType.getElement().getBytesSize()*
225                             constInputType.getCount();
226            mIOBuffer = new FieldPacker(bufferSize);
227            mModel = new Matrix4f();
228            mProjection = new Matrix4f();
229            mTexture = new Matrix4f();
230            setModelview(new Matrix4f());
231            setProjection(new Matrix4f());
232            setTexture(new Matrix4f());
233        }
234
235        /**
236        * @deprecated in API 16
237        * Forces deallocation of memory backing the contant matrices.
238        * Normally, this is unnecessary and will be garbage collected
239        *
240        */
241        public void destroy() {
242            mAlloc.destroy();
243            mAlloc = null;
244        }
245
246        private void addToBuffer(int offset, Matrix4f m) {
247            mIOBuffer.reset(offset);
248            for(int i = 0; i < 16; i ++) {
249                mIOBuffer.addF32(m.mMat[i]);
250            }
251            mAlloc.setFromFieldPacker(0, mIOBuffer);
252        }
253
254        /**
255        * @deprecated in API 16
256        * Sets the modelview matrix in the fixed function matrix buffer
257        *
258        * @param m modelview matrix
259        */
260        public void setModelview(Matrix4f m) {
261            mModel.load(m);
262            addToBuffer(MODELVIEW_OFFSET*4, m);
263        }
264
265        /**
266        * @deprecated in API 16
267        * Sets the projection matrix in the fixed function matrix buffer
268        *
269        * @param m projection matrix
270        */
271        public void setProjection(Matrix4f m) {
272            mProjection.load(m);
273            addToBuffer(PROJECTION_OFFSET*4, m);
274        }
275
276        /**
277        * @deprecated in API 16
278        * Sets the texture matrix in the fixed function matrix buffer.
279        * Texture matrix must be enabled in the
280        * ProgramVertexFixedFunction builder for the shader to utilize
281        * it.
282        *
283        * @param m modelview matrix
284        */
285        public void setTexture(Matrix4f m) {
286            mTexture.load(m);
287            addToBuffer(TEXTURE_OFFSET*4, m);
288        }
289    }
290}
291