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