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