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
289c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines/**
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
409c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
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[];
722123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    String mTextureNames[];
737e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams    int mTextureCount;
740011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    String mShader;
750011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
760011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    Program(int id, RenderScript rs) {
770de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk        super(id, rs);
780011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    }
790011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
809c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
81918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * Program object can have zero or more constant allocations
82918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * associated with it. This method returns the total count.
83918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return number of constant input types
84d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk     */
85d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    public int getConstantCount() {
86d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        return mConstants != null ? mConstants.length : 0;
87d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    }
88d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk
899c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
90918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * Returns the type of the constant buffer used in the program
91918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * object. It could be used to query internal elements or create
92918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * an allocation to store constant data.
93918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @param slot index of the constant input type to return
94918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return constant input type
95d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk     */
96d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    public Type getConstant(int slot) {
97d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        if (slot < 0 || slot >= mConstants.length) {
98d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk            throw new IllegalArgumentException("Slot ID out of range.");
99d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        }
100d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        return mConstants[slot];
101d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    }
102d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk
1039c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
104918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * Returns the number of textures used in this program object
105918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return number of texture inputs
106d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk     */
107d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    public int getTextureCount() {
108d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        return mTextureCount;
109d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    }
110d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk
1119c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
112918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * Returns the type of texture at a given slot. e.g. 2D or Cube
113918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @param slot index of the texture input
114918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return texture input type
115d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk     */
116d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    public TextureType getTextureType(int slot) {
117d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        if ((slot < 0) || (slot >= mTextureCount)) {
118d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk            throw new IllegalArgumentException("Slot ID out of range.");
119d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        }
120d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk        return mTextures[slot];
121d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk    }
122d5a62bb802887134ed652b01fa6a3159a5c6ad0eAlex Sakhartchouk
1239c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
124918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * Returns the name of the texture input at a given slot. e.g.
125918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * tex0, diffuse, spec
126918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @param slot index of the texture input
127918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return texture input name
1282123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk     */
1292123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    public String getTextureName(int slot) {
1302123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        if ((slot < 0) || (slot >= mTextureCount)) {
1312123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            throw new IllegalArgumentException("Slot ID out of range.");
1322123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        }
1332123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        return mTextureNames[slot];
1342123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    }
1352123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk
1369c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
137df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * Binds a constant buffer to be used as uniform inputs to the
138df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * program
139df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
140df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param a allocation containing uniform data
141df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param slot index within the program's list of constant
142df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *             buffer allocations
143df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     */
1440011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    public void bindConstants(Allocation a, int slot) {
145c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams        if (slot < 0 || slot >= mConstants.length) {
146c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams            throw new IllegalArgumentException("Slot ID out of range.");
147c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams        }
148c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams        if (a != null &&
149e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams            a.getType().getID(mRS) != mConstants[slot].getID(mRS)) {
150c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams            throw new IllegalArgumentException("Allocation type does not match slot type.");
151c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams        }
152e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        int id = a != null ? a.getID(mRS) : 0;
153e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nProgramBindConstants(getID(mRS), slot, id);
1540011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    }
1550011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
1569c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
157df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * Binds a texture to be used in the program
158df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
159df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param va allocation containing texture data
160df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param slot index within the program's list of textures
161df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
162df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     */
16368afd01ec9fd37774d8291192952a25e5605b6fbJason Sams    public void bindTexture(Allocation va, int slot)
16468afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        throws IllegalArgumentException {
16568afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        mRS.validate();
16667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if ((slot < 0) || (slot >= mTextureCount)) {
16768afd01ec9fd37774d8291192952a25e5605b6fbJason Sams            throw new IllegalArgumentException("Slot ID out of range.");
16868afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        }
169bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if (va != null && va.getType().hasFaces() &&
17067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            mTextures[slot] != TextureType.TEXTURE_CUBE) {
17167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot");
17267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
17368afd01ec9fd37774d8291192952a25e5605b6fbJason Sams
174e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        int id = va != null ? va.getID(mRS) : 0;
175e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nProgramBindTexture(getID(mRS), slot, id);
17668afd01ec9fd37774d8291192952a25e5605b6fbJason Sams    }
17768afd01ec9fd37774d8291192952a25e5605b6fbJason Sams
1789c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
179df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * Binds an object that describes how a texture at the
180df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * corresponding location is sampled
181df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
182df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param vs sampler for a corresponding texture
183df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     * @param slot index within the program's list of textures to
184df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *             use the sampler on
185df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     *
186df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk     */
18768afd01ec9fd37774d8291192952a25e5605b6fbJason Sams    public void bindSampler(Sampler vs, int slot)
18868afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        throws IllegalArgumentException {
18968afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        mRS.validate();
19067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if ((slot < 0) || (slot >= mTextureCount)) {
19168afd01ec9fd37774d8291192952a25e5605b6fbJason Sams            throw new IllegalArgumentException("Slot ID out of range.");
19268afd01ec9fd37774d8291192952a25e5605b6fbJason Sams        }
19368afd01ec9fd37774d8291192952a25e5605b6fbJason Sams
194e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        int id = vs != null ? vs.getID(mRS) : 0;
195e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nProgramBindSampler(getID(mRS), slot, id);
19668afd01ec9fd37774d8291192952a25e5605b6fbJason Sams    }
19768afd01ec9fd37774d8291192952a25e5605b6fbJason Sams
19868afd01ec9fd37774d8291192952a25e5605b6fbJason Sams
1990011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    public static class BaseProgramBuilder {
2000011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        RenderScript mRS;
2010011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        Element mInputs[];
2020011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        Element mOutputs[];
2030011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        Type mConstants[];
2040011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        Type mTextures[];
20567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        TextureType mTextureTypes[];
2062123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        String mTextureNames[];
2070011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        int mInputCount;
2080011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        int mOutputCount;
2090011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        int mConstantCount;
2100011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        int mTextureCount;
2110011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        String mShader;
2120011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
2139c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines
2140011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        protected BaseProgramBuilder(RenderScript rs) {
2150011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mRS = rs;
2160011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mInputs = new Element[MAX_INPUT];
2170011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mOutputs = new Element[MAX_OUTPUT];
2180011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mConstants = new Type[MAX_CONSTANT];
2190011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mInputCount = 0;
2200011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mOutputCount = 0;
2210011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mConstantCount = 0;
2227e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            mTextureCount = 0;
22367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            mTextureTypes = new TextureType[MAX_TEXTURE];
2242123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            mTextureNames = new String[MAX_TEXTURE];
2250011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
2260011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
2279c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
228df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Sets the GLSL shader code to be used in the program
229df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
230df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param s GLSL shader string
231df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  self
232df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
233288c8711a64893acb3f4a31caf69153be9809d17Jim Shuma        public BaseProgramBuilder setShader(String s) {
2340011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            mShader = s;
235288c8711a64893acb3f4a31caf69153be9809d17Jim Shuma            return this;
2360011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
2370011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
2389c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
239df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Sets the GLSL shader code to be used in the program
240df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
241df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param resources application resources
242df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param resourceID id of the file containing GLSL shader code
243df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
244df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  self
245df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
246a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk        public BaseProgramBuilder setShader(Resources resources, int resourceID) {
247a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            byte[] str;
248a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            int strLength;
249a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            InputStream is = resources.openRawResource(resourceID);
250a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            try {
251a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                try {
252a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                    str = new byte[1024];
253a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                    strLength = 0;
254a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                    while(true) {
255a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        int bytesLeft = str.length - strLength;
256a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        if (bytesLeft == 0) {
257a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                            byte[] buf2 = new byte[str.length * 2];
258a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                            System.arraycopy(str, 0, buf2, 0, str.length);
259a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                            str = buf2;
260a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                            bytesLeft = str.length - strLength;
261a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        }
262a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        int bytesRead = is.read(str, strLength, bytesLeft);
263a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        if (bytesRead <= 0) {
264a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                            break;
265a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        }
266a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                        strLength += bytesRead;
267a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                    }
268a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                } finally {
269a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                    is.close();
270a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                }
271a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            } catch(IOException e) {
272a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                throw new Resources.NotFoundException();
273a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            }
274a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk
275a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            try {
276a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                mShader = new String(str, 0, strLength, "UTF-8");
277a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            } catch (UnsupportedEncodingException e) {
278a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk                Log.e("Renderscript shader creation", "Could not decode shader string");
279a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            }
280a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk
281a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk            return this;
282a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk        }
283a41174ecb03331d770614ecc6351cbc890874c28Alex Sakhartchouk
2849c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
285df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Queries the index of the last added constant buffer type
286df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
287df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
288b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public int getCurrentConstantIndex() {
289b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            return mConstantCount - 1;
2900011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
2910011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
2929c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
293df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Queries the index of the last added texture type
294df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
295df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
296b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public int getCurrentTextureIndex() {
297b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            return mTextureCount - 1;
2980011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
2990011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
3009c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
301df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Adds constant (uniform) inputs to the program
302df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
303df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param t Type that describes the layout of the Allocation
304df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *          object to be used as constant inputs to the Program
305df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  self
306df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
307b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public BaseProgramBuilder addConstant(Type t) throws IllegalStateException {
3080011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            // Should check for consistant and non-conflicting names...
3090011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            if(mConstantCount >= MAX_CONSTANT) {
310c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams                throw new RSIllegalArgumentException("Max input count exceeded.");
311c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams            }
312c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams            if (t.getElement().isComplex()) {
313c1d6210fb5cc558ccea95a59a2b33bb9015fc7deJason Sams                throw new RSIllegalArgumentException("Complex elements not allowed.");
3140011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            }
315ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams            mConstants[mConstantCount] = t;
316b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk            mConstantCount++;
31767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            return this;
31867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
31967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
3209c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
321df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Adds a texture input to the Program
322df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
323df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @param texType describes that the texture to append it (2D,
324df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *                Cubemap, etc.)
325df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  self
326df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
32767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException {
3282123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            addTexture(texType, "Tex" + mTextureCount);
3292123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            return this;
3302123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        }
3312123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk
3329c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
3332123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk         * Adds a texture input to the Program
3342123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk         *
3352123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk         * @param texType describes that the texture to append it (2D,
3362123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk         *                Cubemap, etc.)
3372123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk         * @param texName what the texture should be called in the
3382123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk         *                shader
3392123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk         * @return  self
3402123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk         */
3412123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        public BaseProgramBuilder addTexture(TextureType texType, String texName)
3422123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            throws IllegalArgumentException {
34367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            if(mTextureCount >= MAX_TEXTURE) {
34467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                throw new IllegalArgumentException("Max texture count exceeded.");
34567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            }
3462123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            mTextureTypes[mTextureCount] = texType;
3472123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            mTextureNames[mTextureCount] = texName;
3482123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            mTextureCount ++;
349288c8711a64893acb3f4a31caf69153be9809d17Jim Shuma            return this;
3500011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
3510011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
3520011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        protected void initProgram(Program p) {
3530011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            p.mInputs = new Element[mInputCount];
3540011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
3550011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            p.mOutputs = new Element[mOutputCount];
3560011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
3570011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            p.mConstants = new Type[mConstantCount];
3580011bcf57ff711a221a3a4c73f2a79125111647dJason Sams            System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
3597e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            p.mTextureCount = mTextureCount;
36067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            p.mTextures = new TextureType[mTextureCount];
36167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount);
3622123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            p.mTextureNames = new String[mTextureCount];
3632123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            System.arraycopy(mTextureNames, 0, p.mTextureNames, 0, mTextureCount);
3640011bcf57ff711a221a3a4c73f2a79125111647dJason Sams        }
3650011bcf57ff711a221a3a4c73f2a79125111647dJason Sams    }
3660011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
3670011bcf57ff711a221a3a4c73f2a79125111647dJason Sams}
3680011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
3690011bcf57ff711a221a3a4c73f2a79125111647dJason Sams
370