Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 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 | package android.renderscript; |
| 18 | |
| 19 | |
Alex Sakhartchouk | a41174e | 2010-08-27 16:10:55 -0700 | [diff] [blame] | 20 | import java.io.IOException; |
| 21 | import java.io.InputStream; |
| 22 | import java.io.UnsupportedEncodingException; |
| 23 | |
| 24 | import android.content.res.Resources; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
| 27 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 28 | /** |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [diff] [blame] | 29 | * @hide |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 30 | * |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 31 | * Program is a base class for all the objects that modify |
| 32 | * various stages of the graphics pipeline |
| 33 | * |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 34 | **/ |
| 35 | public class Program extends BaseObj { |
Alex Sakhartchouk | 0473ff1 | 2011-01-14 11:27:27 -0800 | [diff] [blame] | 36 | static final int MAX_INPUT = 8; |
| 37 | static final int MAX_OUTPUT = 8; |
| 38 | static final int MAX_CONSTANT = 8; |
| 39 | static final int MAX_TEXTURE = 8; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 40 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 41 | /** |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 42 | * |
| 43 | * TextureType specifies what textures are attached to Program |
| 44 | * objects |
| 45 | * |
| 46 | **/ |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 47 | public enum TextureType { |
| 48 | TEXTURE_2D (0), |
| 49 | TEXTURE_CUBE (1); |
| 50 | |
| 51 | int mID; |
| 52 | TextureType(int id) { |
| 53 | mID = id; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | enum ProgramParam { |
| 58 | INPUT (0), |
| 59 | OUTPUT (1), |
| 60 | CONSTANT (2), |
| 61 | TEXTURE_TYPE (3); |
| 62 | |
| 63 | int mID; |
| 64 | ProgramParam(int id) { |
| 65 | mID = id; |
| 66 | } |
| 67 | }; |
| 68 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 69 | Element mInputs[]; |
| 70 | Element mOutputs[]; |
| 71 | Type mConstants[]; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 72 | TextureType mTextures[]; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 73 | String mTextureNames[]; |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 74 | int mTextureCount; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 75 | String mShader; |
| 76 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 77 | Program(long id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 78 | super(id, rs); |
Yang Ni | eb4dd08 | 2016-03-24 09:40:32 -0700 | [diff] [blame] | 79 | guard.open("destroy"); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 82 | /** |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 83 | * Program object can have zero or more constant allocations |
| 84 | * associated with it. This method returns the total count. |
| 85 | * @return number of constant input types |
Alex Sakhartchouk | d5a62bb | 2012-01-06 10:36:06 -0800 | [diff] [blame] | 86 | */ |
| 87 | public int getConstantCount() { |
| 88 | return mConstants != null ? mConstants.length : 0; |
| 89 | } |
| 90 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 91 | /** |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 92 | * Returns the type of the constant buffer used in the program |
| 93 | * object. It could be used to query internal elements or create |
| 94 | * an allocation to store constant data. |
| 95 | * @param slot index of the constant input type to return |
| 96 | * @return constant input type |
Alex Sakhartchouk | d5a62bb | 2012-01-06 10:36:06 -0800 | [diff] [blame] | 97 | */ |
| 98 | public Type getConstant(int slot) { |
| 99 | if (slot < 0 || slot >= mConstants.length) { |
| 100 | throw new IllegalArgumentException("Slot ID out of range."); |
| 101 | } |
| 102 | return mConstants[slot]; |
| 103 | } |
| 104 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 105 | /** |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 106 | * Returns the number of textures used in this program object |
| 107 | * @return number of texture inputs |
Alex Sakhartchouk | d5a62bb | 2012-01-06 10:36:06 -0800 | [diff] [blame] | 108 | */ |
| 109 | public int getTextureCount() { |
| 110 | return mTextureCount; |
| 111 | } |
| 112 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 113 | /** |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 114 | * Returns the type of texture at a given slot. e.g. 2D or Cube |
| 115 | * @param slot index of the texture input |
| 116 | * @return texture input type |
Alex Sakhartchouk | d5a62bb | 2012-01-06 10:36:06 -0800 | [diff] [blame] | 117 | */ |
| 118 | public TextureType getTextureType(int slot) { |
| 119 | if ((slot < 0) || (slot >= mTextureCount)) { |
| 120 | throw new IllegalArgumentException("Slot ID out of range."); |
| 121 | } |
| 122 | return mTextures[slot]; |
| 123 | } |
| 124 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 125 | /** |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 126 | * Returns the name of the texture input at a given slot. e.g. |
| 127 | * tex0, diffuse, spec |
| 128 | * @param slot index of the texture input |
| 129 | * @return texture input name |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 130 | */ |
| 131 | public String getTextureName(int slot) { |
| 132 | if ((slot < 0) || (slot >= mTextureCount)) { |
| 133 | throw new IllegalArgumentException("Slot ID out of range."); |
| 134 | } |
| 135 | return mTextureNames[slot]; |
| 136 | } |
| 137 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 138 | /** |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 139 | * Binds a constant buffer to be used as uniform inputs to the |
| 140 | * program |
| 141 | * |
| 142 | * @param a allocation containing uniform data |
| 143 | * @param slot index within the program's list of constant |
| 144 | * buffer allocations |
| 145 | */ |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 146 | public void bindConstants(Allocation a, int slot) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 147 | if (slot < 0 || slot >= mConstants.length) { |
| 148 | throw new IllegalArgumentException("Slot ID out of range."); |
| 149 | } |
| 150 | if (a != null && |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 151 | a.getType().getID(mRS) != mConstants[slot].getID(mRS)) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 152 | throw new IllegalArgumentException("Allocation type does not match slot type."); |
| 153 | } |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 154 | long id = a != null ? a.getID(mRS) : 0; |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 155 | mRS.nProgramBindConstants(getID(mRS), slot, id); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 158 | /** |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 159 | * Binds a texture to be used in the program |
| 160 | * |
| 161 | * @param va allocation containing texture data |
| 162 | * @param slot index within the program's list of textures |
| 163 | * |
| 164 | */ |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 165 | public void bindTexture(Allocation va, int slot) |
| 166 | throws IllegalArgumentException { |
| 167 | mRS.validate(); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 168 | if ((slot < 0) || (slot >= mTextureCount)) { |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 169 | throw new IllegalArgumentException("Slot ID out of range."); |
| 170 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 171 | if (va != null && va.getType().hasFaces() && |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 172 | mTextures[slot] != TextureType.TEXTURE_CUBE) { |
| 173 | throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot"); |
| 174 | } |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 175 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 176 | long id = va != null ? va.getID(mRS) : 0; |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 177 | mRS.nProgramBindTexture(getID(mRS), slot, id); |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 180 | /** |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 181 | * Binds an object that describes how a texture at the |
| 182 | * corresponding location is sampled |
| 183 | * |
| 184 | * @param vs sampler for a corresponding texture |
| 185 | * @param slot index within the program's list of textures to |
| 186 | * use the sampler on |
| 187 | * |
| 188 | */ |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 189 | public void bindSampler(Sampler vs, int slot) |
| 190 | throws IllegalArgumentException { |
| 191 | mRS.validate(); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 192 | if ((slot < 0) || (slot >= mTextureCount)) { |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 193 | throw new IllegalArgumentException("Slot ID out of range."); |
| 194 | } |
| 195 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 196 | long id = vs != null ? vs.getID(mRS) : 0; |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 197 | mRS.nProgramBindSampler(getID(mRS), slot, id); |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 201 | public static class BaseProgramBuilder { |
| 202 | RenderScript mRS; |
| 203 | Element mInputs[]; |
| 204 | Element mOutputs[]; |
| 205 | Type mConstants[]; |
| 206 | Type mTextures[]; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 207 | TextureType mTextureTypes[]; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 208 | String mTextureNames[]; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 209 | int mInputCount; |
| 210 | int mOutputCount; |
| 211 | int mConstantCount; |
| 212 | int mTextureCount; |
| 213 | String mShader; |
| 214 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 215 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 216 | protected BaseProgramBuilder(RenderScript rs) { |
| 217 | mRS = rs; |
| 218 | mInputs = new Element[MAX_INPUT]; |
| 219 | mOutputs = new Element[MAX_OUTPUT]; |
| 220 | mConstants = new Type[MAX_CONSTANT]; |
| 221 | mInputCount = 0; |
| 222 | mOutputCount = 0; |
| 223 | mConstantCount = 0; |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 224 | mTextureCount = 0; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 225 | mTextureTypes = new TextureType[MAX_TEXTURE]; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 226 | mTextureNames = new String[MAX_TEXTURE]; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 229 | /** |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 230 | * Sets the GLSL shader code to be used in the program |
| 231 | * |
| 232 | * @param s GLSL shader string |
| 233 | * @return self |
| 234 | */ |
Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 235 | public BaseProgramBuilder setShader(String s) { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 236 | mShader = s; |
Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 237 | return this; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 240 | /** |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 241 | * Sets the GLSL shader code to be used in the program |
| 242 | * |
| 243 | * @param resources application resources |
| 244 | * @param resourceID id of the file containing GLSL shader code |
| 245 | * |
| 246 | * @return self |
| 247 | */ |
Alex Sakhartchouk | a41174e | 2010-08-27 16:10:55 -0700 | [diff] [blame] | 248 | public BaseProgramBuilder setShader(Resources resources, int resourceID) { |
| 249 | byte[] str; |
| 250 | int strLength; |
| 251 | InputStream is = resources.openRawResource(resourceID); |
| 252 | try { |
| 253 | try { |
| 254 | str = new byte[1024]; |
| 255 | strLength = 0; |
| 256 | while(true) { |
| 257 | int bytesLeft = str.length - strLength; |
| 258 | if (bytesLeft == 0) { |
| 259 | byte[] buf2 = new byte[str.length * 2]; |
| 260 | System.arraycopy(str, 0, buf2, 0, str.length); |
| 261 | str = buf2; |
| 262 | bytesLeft = str.length - strLength; |
| 263 | } |
| 264 | int bytesRead = is.read(str, strLength, bytesLeft); |
| 265 | if (bytesRead <= 0) { |
| 266 | break; |
| 267 | } |
| 268 | strLength += bytesRead; |
| 269 | } |
| 270 | } finally { |
| 271 | is.close(); |
| 272 | } |
| 273 | } catch(IOException e) { |
| 274 | throw new Resources.NotFoundException(); |
| 275 | } |
| 276 | |
| 277 | try { |
| 278 | mShader = new String(str, 0, strLength, "UTF-8"); |
| 279 | } catch (UnsupportedEncodingException e) { |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 280 | Log.e("RenderScript shader creation", "Could not decode shader string"); |
Alex Sakhartchouk | a41174e | 2010-08-27 16:10:55 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | return this; |
| 284 | } |
| 285 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 286 | /** |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 287 | * Queries the index of the last added constant buffer type |
| 288 | * |
| 289 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 290 | public int getCurrentConstantIndex() { |
| 291 | return mConstantCount - 1; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 294 | /** |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 295 | * Queries the index of the last added texture type |
| 296 | * |
| 297 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 298 | public int getCurrentTextureIndex() { |
| 299 | return mTextureCount - 1; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 302 | /** |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 303 | * Adds constant (uniform) inputs to the program |
| 304 | * |
| 305 | * @param t Type that describes the layout of the Allocation |
| 306 | * object to be used as constant inputs to the Program |
| 307 | * @return self |
| 308 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 309 | public BaseProgramBuilder addConstant(Type t) throws IllegalStateException { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 310 | // Should check for consistant and non-conflicting names... |
| 311 | if(mConstantCount >= MAX_CONSTANT) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 312 | throw new RSIllegalArgumentException("Max input count exceeded."); |
| 313 | } |
| 314 | if (t.getElement().isComplex()) { |
| 315 | throw new RSIllegalArgumentException("Complex elements not allowed."); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 316 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 317 | mConstants[mConstantCount] = t; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 318 | mConstantCount++; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 319 | return this; |
| 320 | } |
| 321 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 322 | /** |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 323 | * Adds a texture input to the Program |
| 324 | * |
| 325 | * @param texType describes that the texture to append it (2D, |
| 326 | * Cubemap, etc.) |
| 327 | * @return self |
| 328 | */ |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 329 | public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException { |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 330 | addTexture(texType, "Tex" + mTextureCount); |
| 331 | return this; |
| 332 | } |
| 333 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 334 | /** |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 335 | * Adds a texture input to the Program |
| 336 | * |
| 337 | * @param texType describes that the texture to append it (2D, |
| 338 | * Cubemap, etc.) |
| 339 | * @param texName what the texture should be called in the |
| 340 | * shader |
| 341 | * @return self |
| 342 | */ |
| 343 | public BaseProgramBuilder addTexture(TextureType texType, String texName) |
| 344 | throws IllegalArgumentException { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 345 | if(mTextureCount >= MAX_TEXTURE) { |
| 346 | throw new IllegalArgumentException("Max texture count exceeded."); |
| 347 | } |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 348 | mTextureTypes[mTextureCount] = texType; |
| 349 | mTextureNames[mTextureCount] = texName; |
| 350 | mTextureCount ++; |
Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 351 | return this; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | protected void initProgram(Program p) { |
| 355 | p.mInputs = new Element[mInputCount]; |
| 356 | System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount); |
| 357 | p.mOutputs = new Element[mOutputCount]; |
| 358 | System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount); |
| 359 | p.mConstants = new Type[mConstantCount]; |
| 360 | System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount); |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 361 | p.mTextureCount = mTextureCount; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 362 | p.mTextures = new TextureType[mTextureCount]; |
| 363 | System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount); |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 364 | p.mTextureNames = new String[mTextureCount]; |
| 365 | System.arraycopy(mTextureNames, 0, p.mTextureNames, 0, mTextureCount); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
| 369 | } |
| 370 | |
| 371 | |