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 | /** |
| 30 | * @hide |
| 31 | * |
| 32 | **/ |
| 33 | public class Program extends BaseObj { |
| 34 | public static final int MAX_INPUT = 8; |
| 35 | public static final int MAX_OUTPUT = 8; |
| 36 | public static final int MAX_CONSTANT = 8; |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 37 | public static final int MAX_TEXTURE = 8; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 38 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 39 | public enum TextureType { |
| 40 | TEXTURE_2D (0), |
| 41 | TEXTURE_CUBE (1); |
| 42 | |
| 43 | int mID; |
| 44 | TextureType(int id) { |
| 45 | mID = id; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | enum ProgramParam { |
| 50 | INPUT (0), |
| 51 | OUTPUT (1), |
| 52 | CONSTANT (2), |
| 53 | TEXTURE_TYPE (3); |
| 54 | |
| 55 | int mID; |
| 56 | ProgramParam(int id) { |
| 57 | mID = id; |
| 58 | } |
| 59 | }; |
| 60 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 61 | Element mInputs[]; |
| 62 | Element mOutputs[]; |
| 63 | Type mConstants[]; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 64 | TextureType mTextures[]; |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 65 | int mTextureCount; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 66 | String mShader; |
| 67 | |
| 68 | Program(int id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 69 | super(id, rs); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | public void bindConstants(Allocation a, int slot) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 73 | if (slot < 0 || slot >= mConstants.length) { |
| 74 | throw new IllegalArgumentException("Slot ID out of range."); |
| 75 | } |
| 76 | if (a != null && |
| 77 | a.getType().getID() != mConstants[slot].getID()) { |
| 78 | throw new IllegalArgumentException("Allocation type does not match slot type."); |
| 79 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 80 | int id = a != null ? a.getID() : 0; |
| 81 | mRS.nProgramBindConstants(getID(), slot, id); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 84 | public void bindTexture(Allocation va, int slot) |
| 85 | throws IllegalArgumentException { |
| 86 | mRS.validate(); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 87 | if ((slot < 0) || (slot >= mTextureCount)) { |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 88 | throw new IllegalArgumentException("Slot ID out of range."); |
| 89 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame^] | 90 | if (va != null && va.getType().hasFaces() && |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 91 | mTextures[slot] != TextureType.TEXTURE_CUBE) { |
| 92 | throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot"); |
| 93 | } |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 94 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 95 | int id = va != null ? va.getID() : 0; |
| 96 | mRS.nProgramBindTexture(getID(), slot, id); |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | public void bindSampler(Sampler vs, int slot) |
| 100 | throws IllegalArgumentException { |
| 101 | mRS.validate(); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 102 | if ((slot < 0) || (slot >= mTextureCount)) { |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 103 | throw new IllegalArgumentException("Slot ID out of range."); |
| 104 | } |
| 105 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 106 | int id = vs != null ? vs.getID() : 0; |
| 107 | mRS.nProgramBindSampler(getID(), slot, id); |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 111 | public static class BaseProgramBuilder { |
| 112 | RenderScript mRS; |
| 113 | Element mInputs[]; |
| 114 | Element mOutputs[]; |
| 115 | Type mConstants[]; |
| 116 | Type mTextures[]; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 117 | TextureType mTextureTypes[]; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 118 | int mInputCount; |
| 119 | int mOutputCount; |
| 120 | int mConstantCount; |
| 121 | int mTextureCount; |
| 122 | String mShader; |
| 123 | |
| 124 | |
| 125 | protected BaseProgramBuilder(RenderScript rs) { |
| 126 | mRS = rs; |
| 127 | mInputs = new Element[MAX_INPUT]; |
| 128 | mOutputs = new Element[MAX_OUTPUT]; |
| 129 | mConstants = new Type[MAX_CONSTANT]; |
| 130 | mInputCount = 0; |
| 131 | mOutputCount = 0; |
| 132 | mConstantCount = 0; |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 133 | mTextureCount = 0; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 134 | mTextureTypes = new TextureType[MAX_TEXTURE]; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 137 | public BaseProgramBuilder setShader(String s) { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 138 | mShader = s; |
Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 139 | return this; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Alex Sakhartchouk | a41174e | 2010-08-27 16:10:55 -0700 | [diff] [blame] | 142 | public BaseProgramBuilder setShader(Resources resources, int resourceID) { |
| 143 | byte[] str; |
| 144 | int strLength; |
| 145 | InputStream is = resources.openRawResource(resourceID); |
| 146 | try { |
| 147 | try { |
| 148 | str = new byte[1024]; |
| 149 | strLength = 0; |
| 150 | while(true) { |
| 151 | int bytesLeft = str.length - strLength; |
| 152 | if (bytesLeft == 0) { |
| 153 | byte[] buf2 = new byte[str.length * 2]; |
| 154 | System.arraycopy(str, 0, buf2, 0, str.length); |
| 155 | str = buf2; |
| 156 | bytesLeft = str.length - strLength; |
| 157 | } |
| 158 | int bytesRead = is.read(str, strLength, bytesLeft); |
| 159 | if (bytesRead <= 0) { |
| 160 | break; |
| 161 | } |
| 162 | strLength += bytesRead; |
| 163 | } |
| 164 | } finally { |
| 165 | is.close(); |
| 166 | } |
| 167 | } catch(IOException e) { |
| 168 | throw new Resources.NotFoundException(); |
| 169 | } |
| 170 | |
| 171 | try { |
| 172 | mShader = new String(str, 0, strLength, "UTF-8"); |
| 173 | } catch (UnsupportedEncodingException e) { |
| 174 | Log.e("Renderscript shader creation", "Could not decode shader string"); |
| 175 | } |
| 176 | |
| 177 | return this; |
| 178 | } |
| 179 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 180 | public void addInput(Element e) throws IllegalStateException { |
| 181 | // Should check for consistant and non-conflicting names... |
| 182 | if(mInputCount >= MAX_INPUT) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 183 | throw new RSIllegalArgumentException("Max input count exceeded."); |
| 184 | } |
| 185 | if (e.isComplex()) { |
| 186 | throw new RSIllegalArgumentException("Complex elements not allowed."); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 187 | } |
| 188 | mInputs[mInputCount++] = e; |
| 189 | } |
| 190 | |
| 191 | public void addOutput(Element e) throws IllegalStateException { |
| 192 | // Should check for consistant and non-conflicting names... |
| 193 | if(mOutputCount >= MAX_OUTPUT) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 194 | throw new RSIllegalArgumentException("Max output count exceeded."); |
| 195 | } |
| 196 | if (e.isComplex()) { |
| 197 | throw new RSIllegalArgumentException("Complex elements not allowed."); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 198 | } |
| 199 | mOutputs[mOutputCount++] = e; |
| 200 | } |
| 201 | |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 202 | void resetConstant() { |
| 203 | mConstantCount = 0; |
| 204 | for(int i = 0; i < MAX_CONSTANT; i ++) { |
| 205 | mConstants[i] = null; |
| 206 | } |
| 207 | } |
| 208 | |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 209 | public int addConstant(Type t) throws IllegalStateException { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 210 | // Should check for consistant and non-conflicting names... |
| 211 | if(mConstantCount >= MAX_CONSTANT) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 212 | throw new RSIllegalArgumentException("Max input count exceeded."); |
| 213 | } |
| 214 | if (t.getElement().isComplex()) { |
| 215 | throw new RSIllegalArgumentException("Complex elements not allowed."); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 216 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 217 | mConstants[mConstantCount] = t; |
| 218 | return mConstantCount++; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 221 | public BaseProgramBuilder setTextureCount(int count) throws IllegalArgumentException { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 222 | // Should check for consistant and non-conflicting names... |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 223 | if(count >= MAX_TEXTURE) { |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 224 | throw new IllegalArgumentException("Max texture count exceeded."); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 225 | } |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 226 | mTextureCount = count; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 227 | for (int i = 0; i < mTextureCount; i ++) { |
| 228 | mTextureTypes[i] = TextureType.TEXTURE_2D; |
| 229 | } |
| 230 | return this; |
| 231 | } |
| 232 | |
| 233 | public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException { |
| 234 | if(mTextureCount >= MAX_TEXTURE) { |
| 235 | throw new IllegalArgumentException("Max texture count exceeded."); |
| 236 | } |
| 237 | mTextureTypes[mTextureCount ++] = texType; |
Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 238 | return this; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | protected void initProgram(Program p) { |
| 242 | p.mInputs = new Element[mInputCount]; |
| 243 | System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount); |
| 244 | p.mOutputs = new Element[mOutputCount]; |
| 245 | System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount); |
| 246 | p.mConstants = new Type[mConstantCount]; |
| 247 | System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount); |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 248 | p.mTextureCount = mTextureCount; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 249 | p.mTextures = new TextureType[mTextureCount]; |
| 250 | System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | } |
| 255 | |
| 256 | |