blob: 323225f6f5a48db47bbd898ffa4932ec015d88ad [file] [log] [blame]
Jason Sams0011bcf2009-12-15 12:58:36 -08001/*
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
17package android.renderscript;
18
19
Alex Sakhartchouka41174e2010-08-27 16:10:55 -070020import java.io.IOException;
21import java.io.InputStream;
22import java.io.UnsupportedEncodingException;
23
24import android.content.res.Resources;
Jason Sams0011bcf2009-12-15 12:58:36 -080025import android.util.Config;
26import android.util.Log;
27
28
29/**
Jason Sams0011bcf2009-12-15 12:58:36 -080030 *
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080031 * Program is a base class for all the objects that modify
32 * various stages of the graphics pipeline
33 *
Jason Sams0011bcf2009-12-15 12:58:36 -080034 **/
35public class Program extends BaseObj {
Alex Sakhartchouk0473ff12011-01-14 11:27:27 -080036 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 Sams0011bcf2009-12-15 12:58:36 -080040
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080041 /**
42 *
43 * TextureType specifies what textures are attached to Program
44 * objects
45 *
46 **/
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080047 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 Sams0011bcf2009-12-15 12:58:36 -080069 Element mInputs[];
70 Element mOutputs[];
71 Type mConstants[];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080072 TextureType mTextures[];
Jason Sams7e5ab3b2009-12-15 13:27:04 -080073 int mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -080074 String mShader;
75
76 Program(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070077 super(id, rs);
Jason Sams0011bcf2009-12-15 12:58:36 -080078 }
79
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080080 /**
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 Sams0011bcf2009-12-15 12:58:36 -080088 public void bindConstants(Allocation a, int slot) {
Jason Samsc1d62102010-11-04 14:32:19 -070089 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 Sakhartchouk67f2e442010-11-18 15:22:43 -080096 int id = a != null ? a.getID() : 0;
97 mRS.nProgramBindConstants(getID(), slot, id);
Jason Sams0011bcf2009-12-15 12:58:36 -080098 }
99
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800100 /**
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 Sams68afd012009-12-17 16:55:08 -0800107 public void bindTexture(Allocation va, int slot)
108 throws IllegalArgumentException {
109 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800110 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -0800111 throw new IllegalArgumentException("Slot ID out of range.");
112 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800113 if (va != null && va.getType().hasFaces() &&
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800114 mTextures[slot] != TextureType.TEXTURE_CUBE) {
115 throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot");
116 }
Jason Sams68afd012009-12-17 16:55:08 -0800117
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800118 int id = va != null ? va.getID() : 0;
119 mRS.nProgramBindTexture(getID(), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -0800120 }
121
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800122 /**
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 Sams68afd012009-12-17 16:55:08 -0800131 public void bindSampler(Sampler vs, int slot)
132 throws IllegalArgumentException {
133 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800134 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -0800135 throw new IllegalArgumentException("Slot ID out of range.");
136 }
137
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800138 int id = vs != null ? vs.getID() : 0;
139 mRS.nProgramBindSampler(getID(), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -0800140 }
141
142
Jason Sams0011bcf2009-12-15 12:58:36 -0800143 public static class BaseProgramBuilder {
144 RenderScript mRS;
145 Element mInputs[];
146 Element mOutputs[];
147 Type mConstants[];
148 Type mTextures[];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800149 TextureType mTextureTypes[];
Jason Sams0011bcf2009-12-15 12:58:36 -0800150 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 Sams7e5ab3b2009-12-15 13:27:04 -0800165 mTextureCount = 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800166 mTextureTypes = new TextureType[MAX_TEXTURE];
Jason Sams0011bcf2009-12-15 12:58:36 -0800167 }
168
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800169 /**
170 * Sets the GLSL shader code to be used in the program
171 *
172 * @param s GLSL shader string
173 * @return self
174 */
Jim Shuma288c8712010-07-07 14:24:21 -0700175 public BaseProgramBuilder setShader(String s) {
Jason Sams0011bcf2009-12-15 12:58:36 -0800176 mShader = s;
Jim Shuma288c8712010-07-07 14:24:21 -0700177 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800178 }
179
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800180 /**
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 Sakhartchouka41174e2010-08-27 16:10:55 -0700188 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 Sakhartchoukdf272022011-01-09 11:34:03 -0800226 /**
227 * Queries the index of the last added constant buffer type
228 *
229 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800230 public int getCurrentConstantIndex() {
231 return mConstantCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800232 }
233
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800234 /**
235 * Queries the index of the last added texture type
236 *
237 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800238 public int getCurrentTextureIndex() {
239 return mTextureCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800240 }
241
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800242 /**
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 Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800249 public BaseProgramBuilder addConstant(Type t) throws IllegalStateException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800250 // Should check for consistant and non-conflicting names...
251 if(mConstantCount >= MAX_CONSTANT) {
Jason Samsc1d62102010-11-04 14:32:19 -0700252 throw new RSIllegalArgumentException("Max input count exceeded.");
253 }
254 if (t.getElement().isComplex()) {
255 throw new RSIllegalArgumentException("Complex elements not allowed.");
Jason Sams0011bcf2009-12-15 12:58:36 -0800256 }
Jason Samsea87e962010-01-12 12:12:28 -0800257 mConstants[mConstantCount] = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800258 mConstantCount++;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800259 return this;
260 }
261
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800262 /**
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 Sakhartchouk67f2e442010-11-18 15:22:43 -0800269 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 Shuma288c8712010-07-07 14:24:21 -0700274 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800275 }
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 Sams7e5ab3b2009-12-15 13:27:04 -0800284 p.mTextureCount = mTextureCount;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800285 p.mTextures = new TextureType[mTextureCount];
286 System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount);
Jason Sams0011bcf2009-12-15 12:58:36 -0800287 }
288 }
289
290}
291
292