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