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