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