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  * ProgramVertexFixedFunction is a helper class that provides a
     24  * simple way to create a fixed function emulation vertex shader
     25  * without writing any GLSL code.
     26  *
     27  **/
     28 public class ProgramVertexFixedFunction extends ProgramVertex {
     29 
     30     ProgramVertexFixedFunction(long id, RenderScript rs) {
     31         super(id, rs);
     32     }
     33 
     34     /**
     35      * @deprecated in API 16
     36      * Binds the constant buffer containing fixed function emulation
     37      * matrices
     38      *
     39      * @param va allocation containing fixed function matrices
     40      */
     41     public void bindConstants(Constants va) {
     42         mRS.validate();
     43         bindConstants(va.getAllocation(), 0);
     44     }
     45 
     46     static class InternalBuilder extends BaseProgramBuilder {
     47         /**
     48          * @deprecated in API 16
     49          */
     50         public InternalBuilder(RenderScript rs) {
     51             super(rs);
     52         }
     53 
     54         /**
     55          * @deprecated in API 16
     56          */
     57         public InternalBuilder addInput(Element e) throws IllegalStateException {
     58             // Should check for consistant and non-conflicting names...
     59             if(mInputCount >= MAX_INPUT) {
     60                 throw new RSIllegalArgumentException("Max input count exceeded.");
     61             }
     62             if (e.isComplex()) {
     63                 throw new RSIllegalArgumentException("Complex elements not allowed.");
     64             }
     65             mInputs[mInputCount++] = e;
     66             return this;
     67         }
     68 
     69         /**
     70          * @deprecated in API 16
     71          * Creates ProgramVertexFixedFunction from the current state of
     72          * the builder
     73          *
     74          * @return  ProgramVertexFixedFunction
     75          */
     76         public ProgramVertexFixedFunction create() {
     77             mRS.validate();
     78             long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
     79             String[] texNames = new String[mTextureCount];
     80             int idx = 0;
     81 
     82             for (int i=0; i < mInputCount; i++) {
     83                 tmp[idx++] = ProgramParam.INPUT.mID;
     84                 tmp[idx++] = mInputs[i].getID(mRS);
     85             }
     86             for (int i=0; i < mOutputCount; i++) {
     87                 tmp[idx++] = ProgramParam.OUTPUT.mID;
     88                 tmp[idx++] = mOutputs[i].getID(mRS);
     89             }
     90             for (int i=0; i < mConstantCount; i++) {
     91                 tmp[idx++] = ProgramParam.CONSTANT.mID;
     92                 tmp[idx++] = mConstants[i].getID(mRS);
     93             }
     94             for (int i=0; i < mTextureCount; i++) {
     95                 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
     96                 tmp[idx++] = mTextureTypes[i].mID;
     97                 texNames[i] = mTextureNames[i];
     98             }
     99 
    100             long id = mRS.nProgramVertexCreate(mShader, texNames, tmp);
    101             ProgramVertexFixedFunction pv = new ProgramVertexFixedFunction(id, mRS);
    102             initProgram(pv);
    103             return pv;
    104         }
    105     }
    106 
    107     /**
    108      * @deprecated in API 16
    109      */
    110     public static class Builder {
    111         boolean mTextureMatrixEnable;
    112         String mShader;
    113         RenderScript mRS;
    114 
    115         /**
    116          * @deprecated in API 16
    117          * Creates a builder for fixed function vertex program
    118          *
    119          * @param rs Context to which the program will belong.
    120          */
    121         public Builder(RenderScript rs) {
    122             mRS = rs;
    123         }
    124 
    125         /**
    126          * @deprecated in API 16
    127          * Specifies whether texture matrix calculations are to be added
    128          * to the shader
    129          *
    130          */
    131         public Builder setTextureMatrixEnable(boolean enable) {
    132             mTextureMatrixEnable = enable;
    133             return this;
    134         }
    135         static Type getConstantInputType(RenderScript rs) {
    136             Element.Builder b = new Element.Builder(rs);
    137             b.add(Element.MATRIX4X4(rs), "MV");
    138             b.add(Element.MATRIX4X4(rs), "P");
    139             b.add(Element.MATRIX4X4(rs), "TexMatrix");
    140             b.add(Element.MATRIX4X4(rs), "MVP");
    141 
    142             Type.Builder typeBuilder = new Type.Builder(rs, b.create());
    143             typeBuilder.setX(1);
    144             return typeBuilder.create();
    145         }
    146 
    147         private void buildShaderString() {
    148 
    149             mShader  = "//rs_shader_internal\n";
    150             mShader += "varying vec4 varColor;\n";
    151             mShader += "varying vec2 varTex0;\n";
    152 
    153             mShader += "void main() {\n";
    154             mShader += "  gl_Position = UNI_MVP * ATTRIB_position;\n";
    155             mShader += "  gl_PointSize = 1.0;\n";
    156 
    157             mShader += "  varColor = ATTRIB_color;\n";
    158             if (mTextureMatrixEnable) {
    159                 mShader += "  varTex0 = (UNI_TexMatrix * vec4(ATTRIB_texture0, 0.0, 1.0)).xy;\n";
    160             } else {
    161                 mShader += "  varTex0 = ATTRIB_texture0;\n";
    162             }
    163             mShader += "}\n";
    164         }
    165 
    166         /**
    167          * @deprecated in API 16
    168          * Creates ProgramVertexFixedFunction from the current state of
    169          * the builder
    170          *
    171          * @return Fixed function emulation ProgramVertex
    172          */
    173         public ProgramVertexFixedFunction create() {
    174             buildShaderString();
    175 
    176             InternalBuilder sb = new InternalBuilder(mRS);
    177             sb.setShader(mShader);
    178             sb.addConstant(getConstantInputType(mRS));
    179 
    180             Element.Builder b = new Element.Builder(mRS);
    181             b.add(Element.F32_4(mRS), "position");
    182             b.add(Element.F32_4(mRS), "color");
    183             b.add(Element.F32_3(mRS), "normal");
    184             b.add(Element.F32_2(mRS), "texture0");
    185             sb.addInput(b.create());
    186 
    187             return sb.create();
    188         }
    189     }
    190 
    191     /**
    192      * @deprecated in API 16
    193      * Helper class to store modelview, projection and texture
    194      * matrices for ProgramVertexFixedFunction
    195      *
    196      */
    197     public static class Constants {
    198         static final int MODELVIEW_OFFSET = 0;
    199         static final int PROJECTION_OFFSET = 16;
    200         static final int TEXTURE_OFFSET = 32;
    201 
    202         Matrix4f mModel;
    203         Matrix4f mProjection;
    204         Matrix4f mTexture;
    205 
    206         Allocation mAlloc;
    207         Allocation getAllocation() {
    208             return mAlloc;
    209         }
    210         private FieldPacker mIOBuffer;
    211 
    212         /**
    213         * @deprecated in API 16
    214         * Creates a buffer to store fixed function emulation matrices
    215         *
    216         * @param rs Context to which the allocation will belong.
    217         **/
    218         public Constants(RenderScript rs) {
    219             Type constInputType = ProgramVertexFixedFunction.Builder.getConstantInputType(rs);
    220             mAlloc = Allocation.createTyped(rs, constInputType);
    221             int bufferSize = constInputType.getElement().getBytesSize()*
    222                              constInputType.getCount();
    223             mIOBuffer = new FieldPacker(bufferSize);
    224             mModel = new Matrix4f();
    225             mProjection = new Matrix4f();
    226             mTexture = new Matrix4f();
    227             setModelview(new Matrix4f());
    228             setProjection(new Matrix4f());
    229             setTexture(new Matrix4f());
    230         }
    231 
    232         /**
    233         * @deprecated in API 16
    234         * Forces deallocation of memory backing the contant matrices.
    235         * Normally, this is unnecessary and will be garbage collected
    236         *
    237         */
    238         public void destroy() {
    239             mAlloc.destroy();
    240             mAlloc = null;
    241         }
    242 
    243         private void addToBuffer(int offset, Matrix4f m) {
    244             mIOBuffer.reset(offset);
    245             for(int i = 0; i < 16; i ++) {
    246                 mIOBuffer.addF32(m.mMat[i]);
    247             }
    248             // Reset the buffer back to the end, since we want to flush all of
    249             // the contents back (and not just what we wrote now).
    250             mIOBuffer.reset(mIOBuffer.getData().length);
    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