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