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.util.Log;
21
22
23/**
24 * @deprecated in API 16
25 * <p>ProgramFragmentFixedFunction is a helper class that provides
26 * a way to make a simple fragment shader without writing any
27 * GLSL code. This class allows for display of constant color, interpolated
28 * color from the vertex shader, or combinations of the both
29 * blended with results of up to two texture lookups.</p
30 *
31 **/
32public class ProgramFragmentFixedFunction extends ProgramFragment {
33    ProgramFragmentFixedFunction(int id, RenderScript rs) {
34        super(id, rs);
35    }
36
37    static class InternalBuilder extends BaseProgramBuilder {
38        /**
39         * @deprecated in API 16
40         */
41        public InternalBuilder(RenderScript rs) {
42            super(rs);
43        }
44
45        /**
46         * @deprecated in API 16
47         * Creates ProgramFragmentFixedFunction from the current state
48         * of the builder
49         *
50         * @return  ProgramFragmentFixedFunction
51         */
52        public ProgramFragmentFixedFunction create() {
53            mRS.validate();
54            int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
55            String[] texNames = new String[mTextureCount];
56            int idx = 0;
57
58            for (int i=0; i < mInputCount; i++) {
59                tmp[idx++] = ProgramParam.INPUT.mID;
60                tmp[idx++] = mInputs[i].getID(mRS);
61            }
62            for (int i=0; i < mOutputCount; i++) {
63                tmp[idx++] = ProgramParam.OUTPUT.mID;
64                tmp[idx++] = mOutputs[i].getID(mRS);
65            }
66            for (int i=0; i < mConstantCount; i++) {
67                tmp[idx++] = ProgramParam.CONSTANT.mID;
68                tmp[idx++] = mConstants[i].getID(mRS);
69            }
70            for (int i=0; i < mTextureCount; i++) {
71                tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
72                tmp[idx++] = mTextureTypes[i].mID;
73                texNames[i] = mTextureNames[i];
74            }
75
76            int id = mRS.nProgramFragmentCreate(mShader, texNames, tmp);
77            ProgramFragmentFixedFunction pf = new ProgramFragmentFixedFunction(id, mRS);
78            initProgram(pf);
79            return pf;
80        }
81    }
82
83    /**
84     * @deprecated in API 16
85     */
86    public static class Builder {
87        /**
88         * @deprecated in API 16
89         */
90        public static final int MAX_TEXTURE = 2;
91        int mNumTextures;
92        boolean mPointSpriteEnable;
93        boolean mVaryingColorEnable;
94        String mShader;
95        RenderScript mRS;
96
97        /**
98         * @deprecated in API 16
99         * EnvMode describes how textures are combined with the existing
100         * color in the fixed function fragment shader
101         *
102         **/
103        public enum EnvMode {
104            /**
105             * @deprecated in API 16
106             **/
107            REPLACE (1),
108            /**
109             * @deprecated in API 16
110             **/
111            MODULATE (2),
112            /**
113             * @deprecated in API 16
114             **/
115            DECAL (3);
116
117            int mID;
118            EnvMode(int id) {
119                mID = id;
120            }
121        }
122
123        /**
124         * @deprecated in API 16
125         * Format describes the pixel format of textures in the fixed
126         * function fragment shader and how they are sampled
127         *
128         **/
129        public enum Format {
130            /**
131             * @deprecated in API 16
132             **/
133            ALPHA (1),
134            /**
135             * @deprecated in API 16
136             **/
137            LUMINANCE_ALPHA (2),
138            /**
139             * @deprecated in API 16
140             **/
141            RGB (3),
142            /**
143             * @deprecated in API 16
144             **/
145            RGBA (4);
146
147            int mID;
148            Format(int id) {
149                mID = id;
150            }
151        }
152
153        private class Slot {
154            EnvMode env;
155            Format format;
156            Slot(EnvMode _env, Format _fmt) {
157                env = _env;
158                format = _fmt;
159            }
160        }
161        Slot[] mSlots;
162
163        private void buildShaderString() {
164            mShader  = "//rs_shader_internal\n";
165            mShader += "varying lowp vec4 varColor;\n";
166            mShader += "varying vec2 varTex0;\n";
167
168            mShader += "void main() {\n";
169            if (mVaryingColorEnable) {
170                mShader += "  lowp vec4 col = varColor;\n";
171            } else {
172                mShader += "  lowp vec4 col = UNI_Color;\n";
173            }
174
175            if (mNumTextures != 0) {
176                if (mPointSpriteEnable) {
177                    mShader += "  vec2 t0 = gl_PointCoord;\n";
178                } else {
179                    mShader += "  vec2 t0 = varTex0.xy;\n";
180                }
181            }
182
183            for(int i = 0; i < mNumTextures; i ++) {
184                switch(mSlots[i].env) {
185                case REPLACE:
186                    switch (mSlots[i].format) {
187                    case ALPHA:
188                        mShader += "  col.a = texture2D(UNI_Tex0, t0).a;\n";
189                        break;
190                    case LUMINANCE_ALPHA:
191                        mShader += "  col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
192                        break;
193                    case RGB:
194                        mShader += "  col.rgb = texture2D(UNI_Tex0, t0).rgb;\n";
195                        break;
196                    case RGBA:
197                        mShader += "  col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
198                        break;
199                    }
200                    break;
201                case MODULATE:
202                    switch (mSlots[i].format) {
203                    case ALPHA:
204                        mShader += "  col.a *= texture2D(UNI_Tex0, t0).a;\n";
205                        break;
206                    case LUMINANCE_ALPHA:
207                        mShader += "  col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
208                        break;
209                    case RGB:
210                        mShader += "  col.rgb *= texture2D(UNI_Tex0, t0).rgb;\n";
211                        break;
212                    case RGBA:
213                        mShader += "  col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
214                        break;
215                    }
216                    break;
217                case DECAL:
218                    mShader += "  col = texture2D(UNI_Tex0, t0);\n";
219                    break;
220                }
221            }
222
223            mShader += "  gl_FragColor = col;\n";
224            mShader += "}\n";
225        }
226
227        /**
228         * @deprecated
229         * Creates a builder for fixed function fragment program
230         *
231         * @param rs Context to which the program will belong.
232         */
233        public Builder(RenderScript rs) {
234            mRS = rs;
235            mSlots = new Slot[MAX_TEXTURE];
236            mPointSpriteEnable = false;
237        }
238
239        /**
240         * @deprecated in API 16
241         * Adds a texture to be fetched as part of the fixed function
242         * fragment program
243         *
244         * @param env specifies how the texture is combined with the
245         *            current color
246         * @param fmt specifies the format of the texture and how its
247         *            components will be used to combine with the
248         *            current color
249         * @param slot index of the texture to apply the operations on
250         *
251         * @return this
252         */
253        public Builder setTexture(EnvMode env, Format fmt, int slot)
254            throws IllegalArgumentException {
255            if((slot < 0) || (slot >= MAX_TEXTURE)) {
256                throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
257            }
258            mSlots[slot] = new Slot(env, fmt);
259            return this;
260        }
261
262        /**
263         * @deprecated in API 16
264         * Specifies whether the texture coordinate passed from the
265         * vertex program is replaced with an openGL internal point
266         * sprite texture coordinate
267         *
268         **/
269        public Builder setPointSpriteTexCoordinateReplacement(boolean enable) {
270            mPointSpriteEnable = enable;
271            return this;
272        }
273
274        /**
275         * @deprecated in API 16
276         * Specifies whether the varying color passed from the vertex
277         * program or the constant color set on the fragment program is
278         * used in the final color calculation in the fixed function
279         * fragment shader
280         *
281         **/
282        public Builder setVaryingColor(boolean enable) {
283            mVaryingColorEnable = enable;
284            return this;
285        }
286
287        /**
288         * @deprecated in API 16
289        * Creates the fixed function fragment program from the current
290        * state of the builder.
291        *
292        */
293        public ProgramFragmentFixedFunction create() {
294            InternalBuilder sb = new InternalBuilder(mRS);
295            mNumTextures = 0;
296            for(int i = 0; i < MAX_TEXTURE; i ++) {
297                if(mSlots[i] != null) {
298                    mNumTextures ++;
299                }
300            }
301            buildShaderString();
302            sb.setShader(mShader);
303
304            Type constType = null;
305            if (!mVaryingColorEnable) {
306                Element.Builder b = new Element.Builder(mRS);
307                b.add(Element.F32_4(mRS), "Color");
308                Type.Builder typeBuilder = new Type.Builder(mRS, b.create());
309                typeBuilder.setX(1);
310                constType = typeBuilder.create();
311                sb.addConstant(constType);
312            }
313            for (int i = 0; i < mNumTextures; i ++) {
314                sb.addTexture(TextureType.TEXTURE_2D);
315            }
316
317            ProgramFragmentFixedFunction pf = sb.create();
318            pf.mTextureCount = MAX_TEXTURE;
319            if (!mVaryingColorEnable) {
320                Allocation constantData = Allocation.createTyped(mRS,constType);
321                FieldPacker fp = new FieldPacker(16);
322                Float4 f4 = new Float4(1.f, 1.f, 1.f, 1.f);
323                fp.addF32(f4);
324                constantData.setFromFieldPacker(0, fp);
325                pf.bindConstants(constantData, 0);
326            }
327            return pf;
328        }
329    }
330}
331
332
333
334
335