blob: bc2ca3536ab9d51747bd45a28baa5b2592485a5f [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.Log;
26
27
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070028/**
Tim Murraya9084222013-04-05 22:06:43 +000029 * @hide
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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070041 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080042 *
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[];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080073 String mTextureNames[];
Jason Sams7e5ab3b2009-12-15 13:27:04 -080074 int mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -080075 String mShader;
76
77 Program(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070078 super(id, rs);
Jason Sams0011bcf2009-12-15 12:58:36 -080079 }
80
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070081 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070082 * Program object can have zero or more constant allocations
83 * associated with it. This method returns the total count.
84 * @return number of constant input types
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -080085 */
86 public int getConstantCount() {
87 return mConstants != null ? mConstants.length : 0;
88 }
89
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070090 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070091 * Returns the type of the constant buffer used in the program
92 * object. It could be used to query internal elements or create
93 * an allocation to store constant data.
94 * @param slot index of the constant input type to return
95 * @return constant input type
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -080096 */
97 public Type getConstant(int slot) {
98 if (slot < 0 || slot >= mConstants.length) {
99 throw new IllegalArgumentException("Slot ID out of range.");
100 }
101 return mConstants[slot];
102 }
103
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700104 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700105 * Returns the number of textures used in this program object
106 * @return number of texture inputs
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -0800107 */
108 public int getTextureCount() {
109 return mTextureCount;
110 }
111
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700112 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700113 * Returns the type of texture at a given slot. e.g. 2D or Cube
114 * @param slot index of the texture input
115 * @return texture input type
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -0800116 */
117 public TextureType getTextureType(int slot) {
118 if ((slot < 0) || (slot >= mTextureCount)) {
119 throw new IllegalArgumentException("Slot ID out of range.");
120 }
121 return mTextures[slot];
122 }
123
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700124 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700125 * Returns the name of the texture input at a given slot. e.g.
126 * tex0, diffuse, spec
127 * @param slot index of the texture input
128 * @return texture input name
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800129 */
130 public String getTextureName(int slot) {
131 if ((slot < 0) || (slot >= mTextureCount)) {
132 throw new IllegalArgumentException("Slot ID out of range.");
133 }
134 return mTextureNames[slot];
135 }
136
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700137 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800138 * Binds a constant buffer to be used as uniform inputs to the
139 * program
140 *
141 * @param a allocation containing uniform data
142 * @param slot index within the program's list of constant
143 * buffer allocations
144 */
Jason Sams0011bcf2009-12-15 12:58:36 -0800145 public void bindConstants(Allocation a, int slot) {
Jason Samsc1d62102010-11-04 14:32:19 -0700146 if (slot < 0 || slot >= mConstants.length) {
147 throw new IllegalArgumentException("Slot ID out of range.");
148 }
149 if (a != null &&
Jason Samse07694b2012-04-03 15:36:36 -0700150 a.getType().getID(mRS) != mConstants[slot].getID(mRS)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700151 throw new IllegalArgumentException("Allocation type does not match slot type.");
152 }
Jason Samse07694b2012-04-03 15:36:36 -0700153 int id = a != null ? a.getID(mRS) : 0;
154 mRS.nProgramBindConstants(getID(mRS), slot, id);
Jason Sams0011bcf2009-12-15 12:58:36 -0800155 }
156
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700157 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800158 * Binds a texture to be used in the program
159 *
160 * @param va allocation containing texture data
161 * @param slot index within the program's list of textures
162 *
163 */
Jason Sams68afd012009-12-17 16:55:08 -0800164 public void bindTexture(Allocation va, int slot)
165 throws IllegalArgumentException {
166 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800167 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -0800168 throw new IllegalArgumentException("Slot ID out of range.");
169 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800170 if (va != null && va.getType().hasFaces() &&
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800171 mTextures[slot] != TextureType.TEXTURE_CUBE) {
172 throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot");
173 }
Jason Sams68afd012009-12-17 16:55:08 -0800174
Jason Samse07694b2012-04-03 15:36:36 -0700175 int id = va != null ? va.getID(mRS) : 0;
176 mRS.nProgramBindTexture(getID(mRS), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -0800177 }
178
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700179 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800180 * Binds an object that describes how a texture at the
181 * corresponding location is sampled
182 *
183 * @param vs sampler for a corresponding texture
184 * @param slot index within the program's list of textures to
185 * use the sampler on
186 *
187 */
Jason Sams68afd012009-12-17 16:55:08 -0800188 public void bindSampler(Sampler vs, int slot)
189 throws IllegalArgumentException {
190 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800191 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -0800192 throw new IllegalArgumentException("Slot ID out of range.");
193 }
194
Jason Samse07694b2012-04-03 15:36:36 -0700195 int id = vs != null ? vs.getID(mRS) : 0;
196 mRS.nProgramBindSampler(getID(mRS), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -0800197 }
198
199
Jason Sams0011bcf2009-12-15 12:58:36 -0800200 public static class BaseProgramBuilder {
201 RenderScript mRS;
202 Element mInputs[];
203 Element mOutputs[];
204 Type mConstants[];
205 Type mTextures[];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800206 TextureType mTextureTypes[];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800207 String mTextureNames[];
Jason Sams0011bcf2009-12-15 12:58:36 -0800208 int mInputCount;
209 int mOutputCount;
210 int mConstantCount;
211 int mTextureCount;
212 String mShader;
213
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700214
Jason Sams0011bcf2009-12-15 12:58:36 -0800215 protected BaseProgramBuilder(RenderScript rs) {
216 mRS = rs;
217 mInputs = new Element[MAX_INPUT];
218 mOutputs = new Element[MAX_OUTPUT];
219 mConstants = new Type[MAX_CONSTANT];
220 mInputCount = 0;
221 mOutputCount = 0;
222 mConstantCount = 0;
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800223 mTextureCount = 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800224 mTextureTypes = new TextureType[MAX_TEXTURE];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800225 mTextureNames = new String[MAX_TEXTURE];
Jason Sams0011bcf2009-12-15 12:58:36 -0800226 }
227
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700228 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800229 * Sets the GLSL shader code to be used in the program
230 *
231 * @param s GLSL shader string
232 * @return self
233 */
Jim Shuma288c8712010-07-07 14:24:21 -0700234 public BaseProgramBuilder setShader(String s) {
Jason Sams0011bcf2009-12-15 12:58:36 -0800235 mShader = s;
Jim Shuma288c8712010-07-07 14:24:21 -0700236 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800237 }
238
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700239 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800240 * Sets the GLSL shader code to be used in the program
241 *
242 * @param resources application resources
243 * @param resourceID id of the file containing GLSL shader code
244 *
245 * @return self
246 */
Alex Sakhartchouka41174e2010-08-27 16:10:55 -0700247 public BaseProgramBuilder setShader(Resources resources, int resourceID) {
248 byte[] str;
249 int strLength;
250 InputStream is = resources.openRawResource(resourceID);
251 try {
252 try {
253 str = new byte[1024];
254 strLength = 0;
255 while(true) {
256 int bytesLeft = str.length - strLength;
257 if (bytesLeft == 0) {
258 byte[] buf2 = new byte[str.length * 2];
259 System.arraycopy(str, 0, buf2, 0, str.length);
260 str = buf2;
261 bytesLeft = str.length - strLength;
262 }
263 int bytesRead = is.read(str, strLength, bytesLeft);
264 if (bytesRead <= 0) {
265 break;
266 }
267 strLength += bytesRead;
268 }
269 } finally {
270 is.close();
271 }
272 } catch(IOException e) {
273 throw new Resources.NotFoundException();
274 }
275
276 try {
277 mShader = new String(str, 0, strLength, "UTF-8");
278 } catch (UnsupportedEncodingException e) {
Tim Murrayc11e25c2013-04-09 11:01:01 -0700279 Log.e("RenderScript shader creation", "Could not decode shader string");
Alex Sakhartchouka41174e2010-08-27 16:10:55 -0700280 }
281
282 return this;
283 }
284
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700285 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800286 * Queries the index of the last added constant buffer type
287 *
288 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800289 public int getCurrentConstantIndex() {
290 return mConstantCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800291 }
292
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700293 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800294 * Queries the index of the last added texture type
295 *
296 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800297 public int getCurrentTextureIndex() {
298 return mTextureCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800299 }
300
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700301 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800302 * Adds constant (uniform) inputs to the program
303 *
304 * @param t Type that describes the layout of the Allocation
305 * object to be used as constant inputs to the Program
306 * @return self
307 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800308 public BaseProgramBuilder addConstant(Type t) throws IllegalStateException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800309 // Should check for consistant and non-conflicting names...
310 if(mConstantCount >= MAX_CONSTANT) {
Jason Samsc1d62102010-11-04 14:32:19 -0700311 throw new RSIllegalArgumentException("Max input count exceeded.");
312 }
313 if (t.getElement().isComplex()) {
314 throw new RSIllegalArgumentException("Complex elements not allowed.");
Jason Sams0011bcf2009-12-15 12:58:36 -0800315 }
Jason Samsea87e962010-01-12 12:12:28 -0800316 mConstants[mConstantCount] = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800317 mConstantCount++;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800318 return this;
319 }
320
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700321 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800322 * Adds a texture input to the Program
323 *
324 * @param texType describes that the texture to append it (2D,
325 * Cubemap, etc.)
326 * @return self
327 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800328 public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException {
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800329 addTexture(texType, "Tex" + mTextureCount);
330 return this;
331 }
332
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700333 /**
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800334 * Adds a texture input to the Program
335 *
336 * @param texType describes that the texture to append it (2D,
337 * Cubemap, etc.)
338 * @param texName what the texture should be called in the
339 * shader
340 * @return self
341 */
342 public BaseProgramBuilder addTexture(TextureType texType, String texName)
343 throws IllegalArgumentException {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800344 if(mTextureCount >= MAX_TEXTURE) {
345 throw new IllegalArgumentException("Max texture count exceeded.");
346 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800347 mTextureTypes[mTextureCount] = texType;
348 mTextureNames[mTextureCount] = texName;
349 mTextureCount ++;
Jim Shuma288c8712010-07-07 14:24:21 -0700350 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800351 }
352
353 protected void initProgram(Program p) {
354 p.mInputs = new Element[mInputCount];
355 System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
356 p.mOutputs = new Element[mOutputCount];
357 System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
358 p.mConstants = new Type[mConstantCount];
359 System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800360 p.mTextureCount = mTextureCount;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800361 p.mTextures = new TextureType[mTextureCount];
362 System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800363 p.mTextureNames = new String[mTextureCount];
364 System.arraycopy(mTextureNames, 0, p.mTextureNames, 0, mTextureCount);
Jason Sams0011bcf2009-12-15 12:58:36 -0800365 }
366 }
367
368}
369
370