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
17a90842283e322b1210e2bea5850b05177e3d87afTim Murray/**
18a90842283e322b1210e2bea5850b05177e3d87afTim Murray * @hide
19c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * <p>The RenderScript vertex program, also known as a vertex shader, describes a stage in
2011518acc8c416023d8c2192b441a1767205676d9Robert Ly * the graphics pipeline responsible for manipulating geometric data in a user-defined way.
21c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * The object is constructed by providing the RenderScript system with the following data:</p>
2211518acc8c416023d8c2192b441a1767205676d9Robert Ly * <ul>
2311518acc8c416023d8c2192b441a1767205676d9Robert Ly *   <li>Element describing its varying inputs or attributes</li>
2411518acc8c416023d8c2192b441a1767205676d9Robert Ly *   <li>GLSL shader string that defines the body of the program</li>
2511518acc8c416023d8c2192b441a1767205676d9Robert Ly *   <li>a Type that describes the layout of an Allocation containing constant or uniform inputs</li>
2611518acc8c416023d8c2192b441a1767205676d9Robert Ly * </ul>
2711518acc8c416023d8c2192b441a1767205676d9Robert Ly *
2811518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>Once the program is created, you bind it to the graphics context, RenderScriptGL, and it will be used for
2911518acc8c416023d8c2192b441a1767205676d9Robert Ly * all subsequent draw calls until you bind a new program. If the program has constant inputs,
3011518acc8c416023d8c2192b441a1767205676d9Robert Ly * the user needs to bind an allocation containing those inputs. The allocation's type must match
31c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * the one provided during creation. The RenderScript library then does all the necessary plumbing
3211518acc8c416023d8c2192b441a1767205676d9Robert Ly * to send those constants to the graphics hardware. Varying inputs to the shader, such as position, normal,
3311518acc8c416023d8c2192b441a1767205676d9Robert Ly * and texture coordinates are matched by name between the input Element and the Mesh object being drawn.
3411518acc8c416023d8c2192b441a1767205676d9Robert Ly * The signatures don't have to be exact or in any strict order. As long as the input name in the shader
3511518acc8c416023d8c2192b441a1767205676d9Robert Ly * matches a channel name and size available on the mesh, the runtime takes care of connecting the
3611518acc8c416023d8c2192b441a1767205676d9Robert Ly * two. Unlike OpenGL, there is no need to link the vertex and fragment programs.</p>
3711518acc8c416023d8c2192b441a1767205676d9Robert Ly *
3811518acc8c416023d8c2192b441a1767205676d9Robert Ly **/
39110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Samspackage android.renderscript;
40110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
41110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
429c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines/**
43a90842283e322b1210e2bea5850b05177e3d87afTim Murray * @hide
44d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams * @deprecated in API 16
45df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk * ProgramVertex, also know as a vertex shader, describes a
46df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk * stage in the graphics pipeline responsible for manipulating
47df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk * geometric data in a user-defined way.
48110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams *
49110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams **/
500011bcf57ff711a221a3a4c73f2a79125111647dJason Samspublic class ProgramVertex extends Program {
510011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
52460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray    ProgramVertex(long id, RenderScript rs) {
530011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        super(id, rs);
54110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams    }
55110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
569c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
57d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams     * @deprecated in API 16
58918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return number of input attribute elements
59d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk     */
60d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    public int getInputCount() {
61d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        return mInputs != null ? mInputs.length : 0;
62d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    }
63d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk
649c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
65d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams     * @deprecated in API 16
66918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @param slot location of the input to return
67918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return input attribute element
68d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk     */
69d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    public Element getInput(int slot) {
70d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        if (slot < 0 || slot >= mInputs.length) {
71d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk            throw new IllegalArgumentException("Slot ID out of range.");
72d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        }
73d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        return mInputs[slot];
74d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    }
75d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk
769c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
77a90842283e322b1210e2bea5850b05177e3d87afTim Murray     * @hide
78a90842283e322b1210e2bea5850b05177e3d87afTim Murray     * @deprecated in API 16
79a90842283e322b1210e2bea5850b05177e3d87afTim Murray     * Builder class for creating ProgramVertex objects.
80a90842283e322b1210e2bea5850b05177e3d87afTim Murray     * The builder starts empty and the user must minimally provide
81a90842283e322b1210e2bea5850b05177e3d87afTim Murray     * the GLSL shader code, and the varying inputs. Constant, or
82a90842283e322b1210e2bea5850b05177e3d87afTim Murray     * uniform parameters to the shader may optionally be provided as
83a90842283e322b1210e2bea5850b05177e3d87afTim Murray     * well.
84a90842283e322b1210e2bea5850b05177e3d87afTim Murray     *
85a90842283e322b1210e2bea5850b05177e3d87afTim Murray     **/
86b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk    public static class Builder extends BaseProgramBuilder {
879c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
88d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams         * @deprecated in API 16
89df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Create a builder object.
90df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
91f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk         * @param rs Context to which the program will belong.
92df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
93b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public Builder(RenderScript rs) {
940011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            super(rs);
95110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams        }
96110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
979c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
98d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams         * @deprecated in API 16
99df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Add varying inputs to the program
100df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
101df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param e element describing the layout of the varying input
102df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *          structure
103df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  self
104df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
105b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public Builder addInput(Element e) throws IllegalStateException {
106b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            // Should check for consistant and non-conflicting names...
107b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            if(mInputCount >= MAX_INPUT) {
108b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk                throw new RSIllegalArgumentException("Max input count exceeded.");
109b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            }
110b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            if (e.isComplex()) {
111b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk                throw new RSIllegalArgumentException("Complex elements not allowed.");
112b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            }
113b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            mInputs[mInputCount++] = e;
114b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            return this;
115b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        }
116b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk
1179c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
118d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams         * @deprecated in API 16
119df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Creates ProgramVertex from the current state of the builder
120df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
121df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  ProgramVertex
122df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
1230011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        public ProgramVertex create() {
1240011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mRS.validate();
1259807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat            long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
1262123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            String[] texNames = new String[mTextureCount];
1270011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            int idx = 0;
128110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
1290011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            for (int i=0; i < mInputCount; i++) {
13067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.INPUT.mID;
1319807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat                tmp[idx++] = mInputs[i].getID(mRS);
132110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams            }
1330011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            for (int i=0; i < mOutputCount; i++) {
13467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.OUTPUT.mID;
1359807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat                tmp[idx++] = mOutputs[i].getID(mRS);
136110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams            }
1370011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            for (int i=0; i < mConstantCount; i++) {
13867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.CONSTANT.mID;
1399807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat                tmp[idx++] = mConstants[i].getID(mRS);
1400011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            }
14167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            for (int i=0; i < mTextureCount; i++) {
14267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
1439807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat                tmp[idx++] = mTextureTypes[i].mID;
1442123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk                texNames[i] = mTextureNames[i];
14567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            }
146110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
147460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray            long id = mRS.nProgramVertexCreate(mShader, texNames, tmp);
1480011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            ProgramVertex pv = new ProgramVertex(id, mRS);
1490011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            initProgram(pv);
1500011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            return pv;
151110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams        }
152110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams    }
153110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams
154110195fe9ff96255242bfa4df1d15c6a56b140d6Jason Sams}
155