Home | History | Annotate | Download | only in renderscript
      1 /*
      2  * Copyright (C) 2008 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  /**
     18  * <p>The Renderscript vertex program, also known as a vertex shader, describes a stage in
     19  * the graphics pipeline responsible for manipulating geometric data in a user-defined way.
     20  * The object is constructed by providing the Renderscript system with the following data:</p>
     21  * <ul>
     22  *   <li>Element describing its varying inputs or attributes</li>
     23  *   <li>GLSL shader string that defines the body of the program</li>
     24  *   <li>a Type that describes the layout of an Allocation containing constant or uniform inputs</li>
     25  * </ul>
     26  *
     27  * <p>Once the program is created, you bind it to the graphics context, RenderScriptGL, and it will be used for
     28  * all subsequent draw calls until you bind a new program. If the program has constant inputs,
     29  * the user needs to bind an allocation containing those inputs. The allocation's type must match
     30  * the one provided during creation. The Renderscript library then does all the necessary plumbing
     31  * to send those constants to the graphics hardware. Varying inputs to the shader, such as position, normal,
     32  * and texture coordinates are matched by name between the input Element and the Mesh object being drawn.
     33  * The signatures don't have to be exact or in any strict order. As long as the input name in the shader
     34  * matches a channel name and size available on the mesh, the runtime takes care of connecting the
     35  * two. Unlike OpenGL, there is no need to link the vertex and fragment programs.</p>
     36  *
     37  **/
     38 package android.renderscript;
     39 
     40 
     41 import android.graphics.Matrix;
     42 import android.util.Log;
     43 
     44 
     45 /**
     46  * @deprecated in API 16
     47  * ProgramVertex, also know as a vertex shader, describes a
     48  * stage in the graphics pipeline responsible for manipulating
     49  * geometric data in a user-defined way.
     50  *
     51  **/
     52 public class ProgramVertex extends Program {
     53 
     54     ProgramVertex(int id, RenderScript rs) {
     55         super(id, rs);
     56     }
     57 
     58     /**
     59      * @deprecated in API 16
     60      * @return number of input attribute elements
     61      */
     62     public int getInputCount() {
     63         return mInputs != null ? mInputs.length : 0;
     64     }
     65 
     66     /**
     67      * @deprecated in API 16
     68      * @param slot location of the input to return
     69      * @return input attribute element
     70      */
     71     public Element getInput(int slot) {
     72         if (slot < 0 || slot >= mInputs.length) {
     73             throw new IllegalArgumentException("Slot ID out of range.");
     74         }
     75         return mInputs[slot];
     76     }
     77 
     78     /**
     79     * @deprecated in API 16
     80     * Builder class for creating ProgramVertex objects.
     81     * The builder starts empty and the user must minimally provide
     82     * the GLSL shader code, and the varying inputs. Constant, or
     83     * uniform parameters to the shader may optionally be provided as
     84     * well.
     85     *
     86     **/
     87     public static class Builder extends BaseProgramBuilder {
     88         /**
     89          * @deprecated in API 16
     90          * Create a builder object.
     91          *
     92          * @param rs Context to which the program will belong.
     93          */
     94         public Builder(RenderScript rs) {
     95             super(rs);
     96         }
     97 
     98         /**
     99          * @deprecated in API 16
    100          * Add varying inputs to the program
    101          *
    102          * @param e element describing the layout of the varying input
    103          *          structure
    104          * @return  self
    105          */
    106         public Builder addInput(Element e) throws IllegalStateException {
    107             // Should check for consistant and non-conflicting names...
    108             if(mInputCount >= MAX_INPUT) {
    109                 throw new RSIllegalArgumentException("Max input count exceeded.");
    110             }
    111             if (e.isComplex()) {
    112                 throw new RSIllegalArgumentException("Complex elements not allowed.");
    113             }
    114             mInputs[mInputCount++] = e;
    115             return this;
    116         }
    117 
    118         /**
    119          * @deprecated in API 16
    120          * Creates ProgramVertex from the current state of the builder
    121          *
    122          * @return  ProgramVertex
    123          */
    124         public ProgramVertex create() {
    125             mRS.validate();
    126             int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
    127             String[] texNames = new String[mTextureCount];
    128             int idx = 0;
    129 
    130             for (int i=0; i < mInputCount; i++) {
    131                 tmp[idx++] = ProgramParam.INPUT.mID;
    132                 tmp[idx++] = mInputs[i].getID(mRS);
    133             }
    134             for (int i=0; i < mOutputCount; i++) {
    135                 tmp[idx++] = ProgramParam.OUTPUT.mID;
    136                 tmp[idx++] = mOutputs[i].getID(mRS);
    137             }
    138             for (int i=0; i < mConstantCount; i++) {
    139                 tmp[idx++] = ProgramParam.CONSTANT.mID;
    140                 tmp[idx++] = mConstants[i].getID(mRS);
    141             }
    142             for (int i=0; i < mTextureCount; i++) {
    143                 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
    144                 tmp[idx++] = mTextureTypes[i].mID;
    145                 texNames[i] = mTextureNames[i];
    146             }
    147 
    148             int id = mRS.nProgramVertexCreate(mShader, texNames, tmp);
    149             ProgramVertex pv = new ProgramVertex(id, mRS);
    150             initProgram(pv);
    151             return pv;
    152         }
    153     }
    154 
    155 }
    156