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