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 
17 package android.renderscript;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 
21 
22 /**
23  * @hide
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  **/
32 public class ProgramFragmentFixedFunction extends ProgramFragment {
ProgramFragmentFixedFunction(long id, RenderScript rs)33     ProgramFragmentFixedFunction(long id, RenderScript rs) {
34         super(id, rs);
35     }
36 
37     static class InternalBuilder extends BaseProgramBuilder {
38         /**
39          * @deprecated in API 16
40          */
InternalBuilder(RenderScript rs)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          */
create()52         public ProgramFragmentFixedFunction create() {
53             mRS.validate();
54             long[] tmp = new long[(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             long 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             @UnsupportedAppUsage
108             REPLACE (1),
109             /**
110              * @deprecated in API 16
111              **/
112             @UnsupportedAppUsage
113             MODULATE (2),
114             /**
115              * @deprecated in API 16
116              **/
117             DECAL (3);
118 
119             int mID;
EnvMode(int id)120             EnvMode(int id) {
121                 mID = id;
122             }
123         }
124 
125         /**
126          * @deprecated in API 16
127          * Format describes the pixel format of textures in the fixed
128          * function fragment shader and how they are sampled
129          *
130          **/
131         public enum Format {
132             /**
133              * @deprecated in API 16
134              **/
135             @UnsupportedAppUsage
136             ALPHA (1),
137             /**
138              * @deprecated in API 16
139              **/
140             LUMINANCE_ALPHA (2),
141             /**
142              * @deprecated in API 16
143              **/
144             @UnsupportedAppUsage
145             RGB (3),
146             /**
147              * @deprecated in API 16
148              **/
149             @UnsupportedAppUsage
150             RGBA (4);
151 
152             int mID;
Format(int id)153             Format(int id) {
154                 mID = id;
155             }
156         }
157 
158         private class Slot {
159             EnvMode env;
160             Format format;
Slot(EnvMode _env, Format _fmt)161             Slot(EnvMode _env, Format _fmt) {
162                 env = _env;
163                 format = _fmt;
164             }
165         }
166         Slot[] mSlots;
167 
buildShaderString()168         private void buildShaderString() {
169             mShader  = "//rs_shader_internal\n";
170             mShader += "varying lowp vec4 varColor;\n";
171             mShader += "varying vec2 varTex0;\n";
172 
173             mShader += "void main() {\n";
174             if (mVaryingColorEnable) {
175                 mShader += "  lowp vec4 col = varColor;\n";
176             } else {
177                 mShader += "  lowp vec4 col = UNI_Color;\n";
178             }
179 
180             if (mNumTextures != 0) {
181                 if (mPointSpriteEnable) {
182                     mShader += "  vec2 t0 = gl_PointCoord;\n";
183                 } else {
184                     mShader += "  vec2 t0 = varTex0.xy;\n";
185                 }
186             }
187 
188             for(int i = 0; i < mNumTextures; i ++) {
189                 switch(mSlots[i].env) {
190                 case REPLACE:
191                     switch (mSlots[i].format) {
192                     case ALPHA:
193                         mShader += "  col.a = texture2D(UNI_Tex0, t0).a;\n";
194                         break;
195                     case LUMINANCE_ALPHA:
196                         mShader += "  col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
197                         break;
198                     case RGB:
199                         mShader += "  col.rgb = texture2D(UNI_Tex0, t0).rgb;\n";
200                         break;
201                     case RGBA:
202                         mShader += "  col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
203                         break;
204                     }
205                     break;
206                 case MODULATE:
207                     switch (mSlots[i].format) {
208                     case ALPHA:
209                         mShader += "  col.a *= texture2D(UNI_Tex0, t0).a;\n";
210                         break;
211                     case LUMINANCE_ALPHA:
212                         mShader += "  col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
213                         break;
214                     case RGB:
215                         mShader += "  col.rgb *= texture2D(UNI_Tex0, t0).rgb;\n";
216                         break;
217                     case RGBA:
218                         mShader += "  col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
219                         break;
220                     }
221                     break;
222                 case DECAL:
223                     mShader += "  col = texture2D(UNI_Tex0, t0);\n";
224                     break;
225                 }
226             }
227 
228             mShader += "  gl_FragColor = col;\n";
229             mShader += "}\n";
230         }
231 
232         /**
233          * @deprecated
234          * Creates a builder for fixed function fragment program
235          *
236          * @param rs Context to which the program will belong.
237          */
238         @UnsupportedAppUsage
Builder(RenderScript rs)239         public Builder(RenderScript rs) {
240             mRS = rs;
241             mSlots = new Slot[MAX_TEXTURE];
242             mPointSpriteEnable = false;
243         }
244 
245         /**
246          * @deprecated in API 16
247          * Adds a texture to be fetched as part of the fixed function
248          * fragment program
249          *
250          * @param env specifies how the texture is combined with the
251          *            current color
252          * @param fmt specifies the format of the texture and how its
253          *            components will be used to combine with the
254          *            current color
255          * @param slot index of the texture to apply the operations on
256          *
257          * @return this
258          */
259         @UnsupportedAppUsage
setTexture(EnvMode env, Format fmt, int slot)260         public Builder setTexture(EnvMode env, Format fmt, int slot)
261             throws IllegalArgumentException {
262             if((slot < 0) || (slot >= MAX_TEXTURE)) {
263                 throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
264             }
265             mSlots[slot] = new Slot(env, fmt);
266             return this;
267         }
268 
269         /**
270          * @deprecated in API 16
271          * Specifies whether the texture coordinate passed from the
272          * vertex program is replaced with an openGL internal point
273          * sprite texture coordinate
274          *
275          **/
setPointSpriteTexCoordinateReplacement(boolean enable)276         public Builder setPointSpriteTexCoordinateReplacement(boolean enable) {
277             mPointSpriteEnable = enable;
278             return this;
279         }
280 
281         /**
282          * @deprecated in API 16
283          * Specifies whether the varying color passed from the vertex
284          * program or the constant color set on the fragment program is
285          * used in the final color calculation in the fixed function
286          * fragment shader
287          *
288          **/
289         @UnsupportedAppUsage
setVaryingColor(boolean enable)290         public Builder setVaryingColor(boolean enable) {
291             mVaryingColorEnable = enable;
292             return this;
293         }
294 
295         /**
296          * @deprecated in API 16
297         * Creates the fixed function fragment program from the current
298         * state of the builder.
299         *
300         */
301         @UnsupportedAppUsage
create()302         public ProgramFragmentFixedFunction create() {
303             InternalBuilder sb = new InternalBuilder(mRS);
304             mNumTextures = 0;
305             for(int i = 0; i < MAX_TEXTURE; i ++) {
306                 if(mSlots[i] != null) {
307                     mNumTextures ++;
308                 }
309             }
310             buildShaderString();
311             sb.setShader(mShader);
312 
313             Type constType = null;
314             if (!mVaryingColorEnable) {
315                 Element.Builder b = new Element.Builder(mRS);
316                 b.add(Element.F32_4(mRS), "Color");
317                 Type.Builder typeBuilder = new Type.Builder(mRS, b.create());
318                 typeBuilder.setX(1);
319                 constType = typeBuilder.create();
320                 sb.addConstant(constType);
321             }
322             for (int i = 0; i < mNumTextures; i ++) {
323                 sb.addTexture(TextureType.TEXTURE_2D);
324             }
325 
326             ProgramFragmentFixedFunction pf = sb.create();
327             pf.mTextureCount = MAX_TEXTURE;
328             if (!mVaryingColorEnable) {
329                 Allocation constantData = Allocation.createTyped(mRS,constType);
330                 FieldPacker fp = new FieldPacker(16);
331                 Float4 f4 = new Float4(1.f, 1.f, 1.f, 1.f);
332                 fp.addF32(f4);
333                 constantData.setFromFieldPacker(0, fp);
334                 pf.bindConstants(constantData, 0);
335             }
336             return pf;
337         }
338     }
339 }
340 
341 
342 
343 
344