1110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams/*
2110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams * Copyright (C) 2008 The Android Open Source Project
3110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams *
4110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams * you may not use this file except in compliance with the License.
6110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams * You may obtain a copy of the License at
7110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams *
8110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams *
10110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams * Unless required by applicable law or agreed to in writing, software
11110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams * See the License for the specific language governing permissions and
14110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams * limitations under the License.
15110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams */
16110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
179c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines /**
1811518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>The Renderscript vertex program, also known as a vertex shader, describes a stage in
1911518acc8c416023d8c2192b441a1767205676d9Robert Ly * the graphics pipeline responsible for manipulating geometric data in a user-defined way.
2011518acc8c416023d8c2192b441a1767205676d9Robert Ly * The object is constructed by providing the Renderscript system with the following data:</p>
2111518acc8c416023d8c2192b441a1767205676d9Robert Ly * <ul>
2211518acc8c416023d8c2192b441a1767205676d9Robert Ly *   <li>Element describing its varying inputs or attributes</li>
2311518acc8c416023d8c2192b441a1767205676d9Robert Ly *   <li>GLSL shader string that defines the body of the program</li>
2411518acc8c416023d8c2192b441a1767205676d9Robert Ly *   <li>a Type that describes the layout of an Allocation containing constant or uniform inputs</li>
2511518acc8c416023d8c2192b441a1767205676d9Robert Ly * </ul>
2611518acc8c416023d8c2192b441a1767205676d9Robert Ly *
2711518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>Once the program is created, you bind it to the graphics context, RenderScriptGL, and it will be used for
2811518acc8c416023d8c2192b441a1767205676d9Robert Ly * all subsequent draw calls until you bind a new program. If the program has constant inputs,
2911518acc8c416023d8c2192b441a1767205676d9Robert Ly * the user needs to bind an allocation containing those inputs. The allocation's type must match
3011518acc8c416023d8c2192b441a1767205676d9Robert Ly * the one provided during creation. The Renderscript library then does all the necessary plumbing
3111518acc8c416023d8c2192b441a1767205676d9Robert Ly * to send those constants to the graphics hardware. Varying inputs to the shader, such as position, normal,
3211518acc8c416023d8c2192b441a1767205676d9Robert Ly * and texture coordinates are matched by name between the input Element and the Mesh object being drawn.
3311518acc8c416023d8c2192b441a1767205676d9Robert Ly * The signatures don't have to be exact or in any strict order. As long as the input name in the shader
3411518acc8c416023d8c2192b441a1767205676d9Robert Ly * matches a channel name and size available on the mesh, the runtime takes care of connecting the
3511518acc8c416023d8c2192b441a1767205676d9Robert Ly * two. Unlike OpenGL, there is no need to link the vertex and fragment programs.</p>
3611518acc8c416023d8c2192b441a1767205676d9Robert Ly *
3711518acc8c416023d8c2192b441a1767205676d9Robert Ly **/
38110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Samspackage android.renderscript;
39110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
40110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
41c984dd73c6f96d16e11813ae433ef70f7648ae77Alex Sakhartchoukimport android.graphics.Matrix;
42110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Samsimport android.util.Log;
43110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
44110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
459c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines/**
46d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams * @deprecated in API 16
47df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk * ProgramVertex, also know as a vertex shader, describes a
48df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk * stage in the graphics pipeline responsible for manipulating
49df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk * geometric data in a user-defined way.
50110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams *
51110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams **/
520011bcf57ff711a221a3a4c73f2a79125111647dJason Samspublic class ProgramVertex extends Program {
530011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
54110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams    ProgramVertex(int id, RenderScript rs) {
550011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        super(id, rs);
56110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams    }
57110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
589c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
59d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams     * @deprecated in API 16
60918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return number of input attribute elements
61d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk     */
62d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    public int getInputCount() {
63d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        return mInputs != null ? mInputs.length : 0;
64d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    }
65d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk
669c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
67d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams     * @deprecated in API 16
68918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @param slot location of the input to return
69918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return input attribute element
70d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk     */
71d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    public Element getInput(int slot) {
72d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        if (slot < 0 || slot >= mInputs.length) {
73d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk            throw new IllegalArgumentException("Slot ID out of range.");
74d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        }
75d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        return mInputs[slot];
76d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    }
77d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk
789c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
79d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams    * @deprecated in API 16
80df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk    * Builder class for creating ProgramVertex objects.
81df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk    * The builder starts empty and the user must minimally provide
82df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk    * the GLSL shader code, and the varying inputs. Constant, or
83df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk    * uniform parameters to the shader may optionally be provided as
84df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk    * well.
85df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk    *
86df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk    **/
87b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk    public static class Builder extends BaseProgramBuilder {
889c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
89d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams         * @deprecated in API 16
90df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Create a builder object.
91df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
92f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk         * @param rs Context to which the program will belong.
93df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
94b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public Builder(RenderScript rs) {
950011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            super(rs);
96110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams        }
97110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
989c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
99d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams         * @deprecated in API 16
100df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Add varying inputs to the program
101df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
102df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param e element describing the layout of the varying input
103df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *          structure
104df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  self
105df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
106b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public Builder addInput(Element e) throws IllegalStateException {
107b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            // Should check for consistant and non-conflicting names...
108b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            if(mInputCount >= MAX_INPUT) {
109b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk                throw new RSIllegalArgumentException("Max input count exceeded.");
110b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            }
111b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            if (e.isComplex()) {
112b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk                throw new RSIllegalArgumentException("Complex elements not allowed.");
113b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            }
114b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            mInputs[mInputCount++] = e;
115b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            return this;
116b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        }
117b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk
1189c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
119d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams         * @deprecated in API 16
120df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Creates ProgramVertex from the current state of the builder
121df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
122df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  ProgramVertex
123df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
1240011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        public ProgramVertex create() {
1250011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mRS.validate();
12667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
1272123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            String[] texNames = new String[mTextureCount];
1280011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            int idx = 0;
129110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
1300011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            for (int i=0; i < mInputCount; i++) {
13167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.INPUT.mID;
132e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams                tmp[idx++] = mInputs[i].getID(mRS);
133110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams            }
1340011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            for (int i=0; i < mOutputCount; i++) {
13567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.OUTPUT.mID;
136e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams                tmp[idx++] = mOutputs[i].getID(mRS);
137110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams            }
1380011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            for (int i=0; i < mConstantCount; i++) {
13967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.CONSTANT.mID;
140e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams                tmp[idx++] = mConstants[i].getID(mRS);
1410011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            }
14267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            for (int i=0; i < mTextureCount; i++) {
14367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
14467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = mTextureTypes[i].mID;
1452123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk                texNames[i] = mTextureNames[i];
14667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            }
147110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
1482123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            int id = mRS.nProgramVertexCreate(mShader, texNames, tmp);
1490011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            ProgramVertex pv = new ProgramVertex(id, mRS);
1500011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            initProgram(pv);
1510011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            return pv;
152110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams        }
153110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams    }
154110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
155110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams}
156