10011bcf57ff711a221a3a4c73f2a79125111647dJason Sams/*
20011bcf57ff711a221a3a4c73f2a79125111647dJason Sams * Copyright (C) 2008 The Android Open Source Project
30011bcf57ff711a221a3a4c73f2a79125111647dJason Sams *
40011bcf57ff711a221a3a4c73f2a79125111647dJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
50011bcf57ff711a221a3a4c73f2a79125111647dJason Sams * you may not use this file except in compliance with the License.
60011bcf57ff711a221a3a4c73f2a79125111647dJason Sams * You may obtain a copy of the License at
70011bcf57ff711a221a3a4c73f2a79125111647dJason Sams *
80011bcf57ff711a221a3a4c73f2a79125111647dJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
90011bcf57ff711a221a3a4c73f2a79125111647dJason Sams *
100011bcf57ff711a221a3a4c73f2a79125111647dJason Sams * Unless required by applicable law or agreed to in writing, software
110011bcf57ff711a221a3a4c73f2a79125111647dJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
120011bcf57ff711a221a3a4c73f2a79125111647dJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130011bcf57ff711a221a3a4c73f2a79125111647dJason Sams * See the License for the specific language governing permissions and
140011bcf57ff711a221a3a4c73f2a79125111647dJason Sams * limitations under the License.
150011bcf57ff711a221a3a4c73f2a79125111647dJason Sams */
160011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
170011bcf57ff711a221a3a4c73f2a79125111647dJason Samspackage android.renderscript;
180011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
190011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
20a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchoukimport java.io.IOException;
21a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchoukimport java.io.InputStream;
22a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchoukimport java.io.UnsupportedEncodingException;
23a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk
24a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchoukimport android.content.res.Resources;
250011bcf57ff711a221a3a4c73f2a79125111647dJason Samsimport android.util.Log;
260011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
270011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
280011bcf57ff711a221a3a4c73f2a79125111647dJason Sams/**
290011bcf57ff711a221a3a4c73f2a79125111647dJason Sams *
30df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk * Program is a base class for all the objects that modify
31df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk * various stages of the graphics pipeline
32df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk *
330011bcf57ff711a221a3a4c73f2a79125111647dJason Sams **/
340011bcf57ff711a221a3a4c73f2a79125111647dJason Samspublic class Program extends BaseObj {
350473ff1ef653434a1a0f3c07be00f7ebcbb472adAlex Sakhartchouk    static final int MAX_INPUT = 8;
360473ff1ef653434a1a0f3c07be00f7ebcbb472adAlex Sakhartchouk    static final int MAX_OUTPUT = 8;
370473ff1ef653434a1a0f3c07be00f7ebcbb472adAlex Sakhartchouk    static final int MAX_CONSTANT = 8;
380473ff1ef653434a1a0f3c07be00f7ebcbb472adAlex Sakhartchouk    static final int MAX_TEXTURE = 8;
390011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
40df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk    /**
41df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
42df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * TextureType specifies what textures are attached to Program
43df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * objects
44df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
45df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     **/
4667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    public enum TextureType {
4767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        TEXTURE_2D (0),
4867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        TEXTURE_CUBE (1);
4967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
5067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int mID;
5167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        TextureType(int id) {
5267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            mID = id;
5367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
5467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    }
5567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
5667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    enum ProgramParam {
5767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        INPUT (0),
5867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        OUTPUT (1),
5967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        CONSTANT (2),
6067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        TEXTURE_TYPE (3);
6167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
6267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int mID;
6367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        ProgramParam(int id) {
6467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            mID = id;
6567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
6667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    };
6767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
680011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    Element mInputs[];
690011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    Element mOutputs[];
700011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    Type mConstants[];
7167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    TextureType mTextures[];
727e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams    int mTextureCount;
730011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    String mShader;
740011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
750011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    Program(int id, RenderScript rs) {
760de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk        super(id, rs);
770011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    }
780011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
79df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk    /**
80df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * Binds a constant buffer to be used as uniform inputs to the
81df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * program
82df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
83df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param a allocation containing uniform data
84df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param slot index within the program's list of constant
85df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *             buffer allocations
86df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     */
870011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    public void bindConstants(Allocation a, int slot) {
88c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams        if (slot < 0 || slot >= mConstants.length) {
89c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams            throw new IllegalArgumentException("Slot ID out of range.");
90c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams        }
91c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams        if (a != null &&
92c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams            a.getType().getID() != mConstants[slot].getID()) {
93c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams            throw new IllegalArgumentException("Allocation type does not match slot type.");
94c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams        }
9567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int id = a != null ? a.getID() : 0;
9667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        mRS.nProgramBindConstants(getID(), slot, id);
970011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    }
980011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
99df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk    /**
100df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * Binds a texture to be used in the program
101df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
102df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param va allocation containing texture data
103df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param slot index within the program's list of textures
104df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
105df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     */
10668afd01ec9fd37774d8291192952a25e5605b6fbJason Sams    public void bindTexture(Allocation va, int slot)
10768afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        throws IllegalArgumentException {
10868afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        mRS.validate();
10967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if ((slot < 0) || (slot >= mTextureCount)) {
11068afd01ec9fd37774d8291192952a25e5605b6fbJason Sams            throw new IllegalArgumentException("Slot ID out of range.");
11168afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        }
112bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if (va != null && va.getType().hasFaces() &&
11367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            mTextures[slot] != TextureType.TEXTURE_CUBE) {
11467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot");
11567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
11668afd01ec9fd37774d8291192952a25e5605b6fbJason Sams
11767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int id = va != null ? va.getID() : 0;
11867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        mRS.nProgramBindTexture(getID(), slot, id);
11968afd01ec9fd37774d8291192952a25e5605b6fbJason Sams    }
12068afd01ec9fd37774d8291192952a25e5605b6fbJason Sams
121df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk    /**
122df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * Binds an object that describes how a texture at the
123df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * corresponding location is sampled
124df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
125df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param vs sampler for a corresponding texture
126df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param slot index within the program's list of textures to
127df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *             use the sampler on
128df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
129df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     */
13068afd01ec9fd37774d8291192952a25e5605b6fbJason Sams    public void bindSampler(Sampler vs, int slot)
13168afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        throws IllegalArgumentException {
13268afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        mRS.validate();
13367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if ((slot < 0) || (slot >= mTextureCount)) {
13468afd01ec9fd37774d8291192952a25e5605b6fbJason Sams            throw new IllegalArgumentException("Slot ID out of range.");
13568afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        }
13668afd01ec9fd37774d8291192952a25e5605b6fbJason Sams
13767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int id = vs != null ? vs.getID() : 0;
13867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        mRS.nProgramBindSampler(getID(), slot, id);
13968afd01ec9fd37774d8291192952a25e5605b6fbJason Sams    }
14068afd01ec9fd37774d8291192952a25e5605b6fbJason Sams
14168afd01ec9fd37774d8291192952a25e5605b6fbJason Sams
1420011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    public static class BaseProgramBuilder {
1430011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        RenderScript mRS;
1440011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        Element mInputs[];
1450011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        Element mOutputs[];
1460011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        Type mConstants[];
1470011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        Type mTextures[];
14867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        TextureType mTextureTypes[];
1490011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        int mInputCount;
1500011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        int mOutputCount;
1510011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        int mConstantCount;
1520011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        int mTextureCount;
1530011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        String mShader;
1540011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
1550011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
1560011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        protected BaseProgramBuilder(RenderScript rs) {
1570011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mRS = rs;
1580011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mInputs = new Element[MAX_INPUT];
1590011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mOutputs = new Element[MAX_OUTPUT];
1600011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mConstants = new Type[MAX_CONSTANT];
1610011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mInputCount = 0;
1620011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mOutputCount = 0;
1630011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mConstantCount = 0;
1647e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            mTextureCount = 0;
16567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            mTextureTypes = new TextureType[MAX_TEXTURE];
1660011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
1670011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
168df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk        /**
169df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Sets the GLSL shader code to be used in the program
170df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
171df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param s GLSL shader string
172df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  self
173df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
174288c8711a64893acb3f4a31caf69153be9809d17Jim Shuma        public BaseProgramBuilder setShader(String s) {
1750011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mShader = s;
176288c8711a64893acb3f4a31caf69153be9809d17Jim Shuma            return this;
1770011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
1780011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
179df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk        /**
180df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Sets the GLSL shader code to be used in the program
181df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
182df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param resources application resources
183df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param resourceID id of the file containing GLSL shader code
184df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
185df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  self
186df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
187a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk        public BaseProgramBuilder setShader(Resources resources, int resourceID) {
188a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            byte[] str;
189a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            int strLength;
190a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            InputStream is = resources.openRawResource(resourceID);
191a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            try {
192a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                try {
193a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                    str = new byte[1024];
194a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                    strLength = 0;
195a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                    while(true) {
196a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        int bytesLeft = str.length - strLength;
197a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        if (bytesLeft == 0) {
198a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                            byte[] buf2 = new byte[str.length * 2];
199a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                            System.arraycopy(str, 0, buf2, 0, str.length);
200a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                            str = buf2;
201a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                            bytesLeft = str.length - strLength;
202a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        }
203a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        int bytesRead = is.read(str, strLength, bytesLeft);
204a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        if (bytesRead <= 0) {
205a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                            break;
206a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        }
207a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        strLength += bytesRead;
208a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                    }
209a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                } finally {
210a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                    is.close();
211a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                }
212a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            } catch(IOException e) {
213a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                throw new Resources.NotFoundException();
214a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            }
215a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk
216a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            try {
217a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                mShader = new String(str, 0, strLength, "UTF-8");
218a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            } catch (UnsupportedEncodingException e) {
219a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                Log.e("Renderscript shader creation", "Could not decode shader string");
220a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            }
221a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk
222a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            return this;
223a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk        }
224a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk
225df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk        /**
226df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Queries the index of the last added constant buffer type
227df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
228df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
229b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public int getCurrentConstantIndex() {
230b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            return mConstantCount - 1;
2310011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
2320011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
233df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk        /**
234df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Queries the index of the last added texture type
235df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
236df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
237b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public int getCurrentTextureIndex() {
238b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            return mTextureCount - 1;
2390011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
2400011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
241df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk        /**
242df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Adds constant (uniform) inputs to the program
243df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
244df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param t Type that describes the layout of the Allocation
245df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *          object to be used as constant inputs to the Program
246df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  self
247df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
248b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public BaseProgramBuilder addConstant(Type t) throws IllegalStateException {
2490011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            // Should check for consistant and non-conflicting names...
2500011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            if(mConstantCount >= MAX_CONSTANT) {
251c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams                throw new RSIllegalArgumentException("Max input count exceeded.");
252c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams            }
253c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams            if (t.getElement().isComplex()) {
254c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams                throw new RSIllegalArgumentException("Complex elements not allowed.");
2550011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            }
256ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams            mConstants[mConstantCount] = t;
257b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            mConstantCount++;
25867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            return this;
25967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
26067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
261df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk        /**
262df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Adds a texture input to the Program
263df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
264df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param texType describes that the texture to append it (2D,
265df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *                Cubemap, etc.)
266df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  self
267df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
26867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException {
26967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            if(mTextureCount >= MAX_TEXTURE) {
27067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                throw new IllegalArgumentException("Max texture count exceeded.");
27167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            }
27267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            mTextureTypes[mTextureCount ++] = texType;
273288c8711a64893acb3f4a31caf69153be9809d17Jim Shuma            return this;
2740011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
2750011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
2760011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        protected void initProgram(Program p) {
2770011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            p.mInputs = new Element[mInputCount];
2780011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
2790011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            p.mOutputs = new Element[mOutputCount];
2800011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
2810011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            p.mConstants = new Type[mConstantCount];
2820011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
2837e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            p.mTextureCount = mTextureCount;
28467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            p.mTextures = new TextureType[mTextureCount];
28567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount);
2860011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
2870011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    }
2880011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
2890011bcf57ff711a221a3a4c73f2a79125111647dJason Sams}
2900011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
2910011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
292