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