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.Config; |
| 26 | import android.util.Log; |
| 27 | |
| 28 | |
| 29 | /** |
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 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 41 | /** |
| 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[]; |
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 | /** |
| 81 | * Binds a constant buffer to be used as uniform inputs to the |
| 82 | * program |
| 83 | * |
| 84 | * @param a allocation containing uniform data |
| 85 | * @param slot index within the program's list of constant |
| 86 | * buffer allocations |
| 87 | */ |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 88 | public void bindConstants(Allocation a, int slot) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 89 | if (slot < 0 || slot >= mConstants.length) { |
| 90 | throw new IllegalArgumentException("Slot ID out of range."); |
| 91 | } |
| 92 | if (a != null && |
| 93 | a.getType().getID() != mConstants[slot].getID()) { |
| 94 | throw new IllegalArgumentException("Allocation type does not match slot type."); |
| 95 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 96 | int id = a != null ? a.getID() : 0; |
| 97 | mRS.nProgramBindConstants(getID(), slot, id); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Binds a texture to be used in the program |
| 102 | * |
| 103 | * @param va allocation containing texture data |
| 104 | * @param slot index within the program's list of textures |
| 105 | * |
| 106 | */ |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 107 | public void bindTexture(Allocation va, int slot) |
| 108 | throws IllegalArgumentException { |
| 109 | mRS.validate(); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 110 | if ((slot < 0) || (slot >= mTextureCount)) { |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 111 | throw new IllegalArgumentException("Slot ID out of range."); |
| 112 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 113 | if (va != null && va.getType().hasFaces() && |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 114 | mTextures[slot] != TextureType.TEXTURE_CUBE) { |
| 115 | throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot"); |
| 116 | } |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 117 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 118 | int id = va != null ? va.getID() : 0; |
| 119 | mRS.nProgramBindTexture(getID(), slot, id); |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Binds an object that describes how a texture at the |
| 124 | * corresponding location is sampled |
| 125 | * |
| 126 | * @param vs sampler for a corresponding texture |
| 127 | * @param slot index within the program's list of textures to |
| 128 | * use the sampler on |
| 129 | * |
| 130 | */ |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 131 | public void bindSampler(Sampler vs, int slot) |
| 132 | throws IllegalArgumentException { |
| 133 | mRS.validate(); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 134 | if ((slot < 0) || (slot >= mTextureCount)) { |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 135 | throw new IllegalArgumentException("Slot ID out of range."); |
| 136 | } |
| 137 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 138 | int id = vs != null ? vs.getID() : 0; |
| 139 | mRS.nProgramBindSampler(getID(), slot, id); |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 143 | public static class BaseProgramBuilder { |
| 144 | RenderScript mRS; |
| 145 | Element mInputs[]; |
| 146 | Element mOutputs[]; |
| 147 | Type mConstants[]; |
| 148 | Type mTextures[]; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 149 | TextureType mTextureTypes[]; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 150 | int mInputCount; |
| 151 | int mOutputCount; |
| 152 | int mConstantCount; |
| 153 | int mTextureCount; |
| 154 | String mShader; |
| 155 | |
| 156 | |
| 157 | protected BaseProgramBuilder(RenderScript rs) { |
| 158 | mRS = rs; |
| 159 | mInputs = new Element[MAX_INPUT]; |
| 160 | mOutputs = new Element[MAX_OUTPUT]; |
| 161 | mConstants = new Type[MAX_CONSTANT]; |
| 162 | mInputCount = 0; |
| 163 | mOutputCount = 0; |
| 164 | mConstantCount = 0; |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 165 | mTextureCount = 0; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 166 | mTextureTypes = new TextureType[MAX_TEXTURE]; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 169 | /** |
| 170 | * Sets the GLSL shader code to be used in the program |
| 171 | * |
| 172 | * @param s GLSL shader string |
| 173 | * @return self |
| 174 | */ |
Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 175 | public BaseProgramBuilder setShader(String s) { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 176 | mShader = s; |
Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 177 | return this; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 180 | /** |
| 181 | * Sets the GLSL shader code to be used in the program |
| 182 | * |
| 183 | * @param resources application resources |
| 184 | * @param resourceID id of the file containing GLSL shader code |
| 185 | * |
| 186 | * @return self |
| 187 | */ |
Alex Sakhartchouk | a41174e | 2010-08-27 16:10:55 -0700 | [diff] [blame] | 188 | public BaseProgramBuilder setShader(Resources resources, int resourceID) { |
| 189 | byte[] str; |
| 190 | int strLength; |
| 191 | InputStream is = resources.openRawResource(resourceID); |
| 192 | try { |
| 193 | try { |
| 194 | str = new byte[1024]; |
| 195 | strLength = 0; |
| 196 | while(true) { |
| 197 | int bytesLeft = str.length - strLength; |
| 198 | if (bytesLeft == 0) { |
| 199 | byte[] buf2 = new byte[str.length * 2]; |
| 200 | System.arraycopy(str, 0, buf2, 0, str.length); |
| 201 | str = buf2; |
| 202 | bytesLeft = str.length - strLength; |
| 203 | } |
| 204 | int bytesRead = is.read(str, strLength, bytesLeft); |
| 205 | if (bytesRead <= 0) { |
| 206 | break; |
| 207 | } |
| 208 | strLength += bytesRead; |
| 209 | } |
| 210 | } finally { |
| 211 | is.close(); |
| 212 | } |
| 213 | } catch(IOException e) { |
| 214 | throw new Resources.NotFoundException(); |
| 215 | } |
| 216 | |
| 217 | try { |
| 218 | mShader = new String(str, 0, strLength, "UTF-8"); |
| 219 | } catch (UnsupportedEncodingException e) { |
| 220 | Log.e("Renderscript shader creation", "Could not decode shader string"); |
| 221 | } |
| 222 | |
| 223 | return this; |
| 224 | } |
| 225 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 226 | /** |
| 227 | * Queries the index of the last added constant buffer type |
| 228 | * |
| 229 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 230 | public int getCurrentConstantIndex() { |
| 231 | return mConstantCount - 1; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 234 | /** |
| 235 | * Queries the index of the last added texture type |
| 236 | * |
| 237 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 238 | public int getCurrentTextureIndex() { |
| 239 | return mTextureCount - 1; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 242 | /** |
| 243 | * Adds constant (uniform) inputs to the program |
| 244 | * |
| 245 | * @param t Type that describes the layout of the Allocation |
| 246 | * object to be used as constant inputs to the Program |
| 247 | * @return self |
| 248 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 249 | public BaseProgramBuilder addConstant(Type t) throws IllegalStateException { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 250 | // Should check for consistant and non-conflicting names... |
| 251 | if(mConstantCount >= MAX_CONSTANT) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 252 | throw new RSIllegalArgumentException("Max input count exceeded."); |
| 253 | } |
| 254 | if (t.getElement().isComplex()) { |
| 255 | throw new RSIllegalArgumentException("Complex elements not allowed."); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 256 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 257 | mConstants[mConstantCount] = t; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 258 | mConstantCount++; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 259 | return this; |
| 260 | } |
| 261 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 262 | /** |
| 263 | * Adds a texture input to the Program |
| 264 | * |
| 265 | * @param texType describes that the texture to append it (2D, |
| 266 | * Cubemap, etc.) |
| 267 | * @return self |
| 268 | */ |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 269 | public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException { |
| 270 | if(mTextureCount >= MAX_TEXTURE) { |
| 271 | throw new IllegalArgumentException("Max texture count exceeded."); |
| 272 | } |
| 273 | mTextureTypes[mTextureCount ++] = texType; |
Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 274 | return this; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | protected void initProgram(Program p) { |
| 278 | p.mInputs = new Element[mInputCount]; |
| 279 | System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount); |
| 280 | p.mOutputs = new Element[mOutputCount]; |
| 281 | System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount); |
| 282 | p.mConstants = new Type[mConstantCount]; |
| 283 | System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount); |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 284 | p.mTextureCount = mTextureCount; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 285 | p.mTextures = new TextureType[mTextureCount]; |
| 286 | System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | } |
| 291 | |
| 292 | |