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