blob: 16186faaf3b0c78f02341252701f86667f026054 [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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070028/** @deprecated renderscript is deprecated in J
Jason Sams0011bcf2009-12-15 12:58:36 -080029 *
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080030 * Program is a base class for all the objects that modify
31 * various stages of the graphics pipeline
32 *
Jason Sams0011bcf2009-12-15 12:58:36 -080033 **/
34public class Program extends BaseObj {
Alex Sakhartchouk0473ff12011-01-14 11:27:27 -080035 static final int MAX_INPUT = 8;
36 static final int MAX_OUTPUT = 8;
37 static final int MAX_CONSTANT = 8;
38 static final int MAX_TEXTURE = 8;
Jason Sams0011bcf2009-12-15 12:58:36 -080039
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070040 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080041 *
42 * TextureType specifies what textures are attached to Program
43 * objects
44 *
45 **/
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080046 public enum TextureType {
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070047 /** @deprecated renderscript is deprecated in J
48 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080049 TEXTURE_2D (0),
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070050 /** @deprecated renderscript is deprecated in J
51 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080052 TEXTURE_CUBE (1);
53
54 int mID;
55 TextureType(int id) {
56 mID = id;
57 }
58 }
59
60 enum ProgramParam {
61 INPUT (0),
62 OUTPUT (1),
63 CONSTANT (2),
64 TEXTURE_TYPE (3);
65
66 int mID;
67 ProgramParam(int id) {
68 mID = id;
69 }
70 };
71
Jason Sams0011bcf2009-12-15 12:58:36 -080072 Element mInputs[];
73 Element mOutputs[];
74 Type mConstants[];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080075 TextureType mTextures[];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080076 String mTextureNames[];
Jason Sams7e5ab3b2009-12-15 13:27:04 -080077 int mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -080078 String mShader;
79
80 Program(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070081 super(id, rs);
Jason Sams0011bcf2009-12-15 12:58:36 -080082 }
83
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070084 /** @hide renderscript is deprecated in J
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070085 * Program object can have zero or more constant allocations
86 * associated with it. This method returns the total count.
87 * @return number of constant input types
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -080088 */
89 public int getConstantCount() {
90 return mConstants != null ? mConstants.length : 0;
91 }
92
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070093 /** @hide renderscript is deprecated in J
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070094 * Returns the type of the constant buffer used in the program
95 * object. It could be used to query internal elements or create
96 * an allocation to store constant data.
97 * @param slot index of the constant input type to return
98 * @return constant input type
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -080099 */
100 public Type getConstant(int slot) {
101 if (slot < 0 || slot >= mConstants.length) {
102 throw new IllegalArgumentException("Slot ID out of range.");
103 }
104 return mConstants[slot];
105 }
106
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700107 /** @hide renderscript is deprecated in J
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700108 * Returns the number of textures used in this program object
109 * @return number of texture inputs
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -0800110 */
111 public int getTextureCount() {
112 return mTextureCount;
113 }
114
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700115 /** @hide renderscript is deprecated in J
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700116 * Returns the type of texture at a given slot. e.g. 2D or Cube
117 * @param slot index of the texture input
118 * @return texture input type
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -0800119 */
120 public TextureType getTextureType(int slot) {
121 if ((slot < 0) || (slot >= mTextureCount)) {
122 throw new IllegalArgumentException("Slot ID out of range.");
123 }
124 return mTextures[slot];
125 }
126
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700127 /** @hide renderscript is deprecated in J
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700128 * Returns the name of the texture input at a given slot. e.g.
129 * tex0, diffuse, spec
130 * @param slot index of the texture input
131 * @return texture input name
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800132 */
133 public String getTextureName(int slot) {
134 if ((slot < 0) || (slot >= mTextureCount)) {
135 throw new IllegalArgumentException("Slot ID out of range.");
136 }
137 return mTextureNames[slot];
138 }
139
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700140 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800141 * Binds a constant buffer to be used as uniform inputs to the
142 * program
143 *
144 * @param a allocation containing uniform data
145 * @param slot index within the program's list of constant
146 * buffer allocations
147 */
Jason Sams0011bcf2009-12-15 12:58:36 -0800148 public void bindConstants(Allocation a, int slot) {
Jason Samsc1d62102010-11-04 14:32:19 -0700149 if (slot < 0 || slot >= mConstants.length) {
150 throw new IllegalArgumentException("Slot ID out of range.");
151 }
152 if (a != null &&
Jason Samse07694b2012-04-03 15:36:36 -0700153 a.getType().getID(mRS) != mConstants[slot].getID(mRS)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700154 throw new IllegalArgumentException("Allocation type does not match slot type.");
155 }
Jason Samse07694b2012-04-03 15:36:36 -0700156 int id = a != null ? a.getID(mRS) : 0;
157 mRS.nProgramBindConstants(getID(mRS), slot, id);
Jason Sams0011bcf2009-12-15 12:58:36 -0800158 }
159
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700160 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800161 * Binds a texture to be used in the program
162 *
163 * @param va allocation containing texture data
164 * @param slot index within the program's list of textures
165 *
166 */
Jason Sams68afd012009-12-17 16:55:08 -0800167 public void bindTexture(Allocation va, int slot)
168 throws IllegalArgumentException {
169 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800170 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -0800171 throw new IllegalArgumentException("Slot ID out of range.");
172 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800173 if (va != null && va.getType().hasFaces() &&
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800174 mTextures[slot] != TextureType.TEXTURE_CUBE) {
175 throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot");
176 }
Jason Sams68afd012009-12-17 16:55:08 -0800177
Jason Samse07694b2012-04-03 15:36:36 -0700178 int id = va != null ? va.getID(mRS) : 0;
179 mRS.nProgramBindTexture(getID(mRS), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -0800180 }
181
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700182 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800183 * Binds an object that describes how a texture at the
184 * corresponding location is sampled
185 *
186 * @param vs sampler for a corresponding texture
187 * @param slot index within the program's list of textures to
188 * use the sampler on
189 *
190 */
Jason Sams68afd012009-12-17 16:55:08 -0800191 public void bindSampler(Sampler vs, int slot)
192 throws IllegalArgumentException {
193 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800194 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -0800195 throw new IllegalArgumentException("Slot ID out of range.");
196 }
197
Jason Samse07694b2012-04-03 15:36:36 -0700198 int id = vs != null ? vs.getID(mRS) : 0;
199 mRS.nProgramBindSampler(getID(mRS), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -0800200 }
201
202
Jason Sams0011bcf2009-12-15 12:58:36 -0800203 public static class BaseProgramBuilder {
204 RenderScript mRS;
205 Element mInputs[];
206 Element mOutputs[];
207 Type mConstants[];
208 Type mTextures[];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800209 TextureType mTextureTypes[];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800210 String mTextureNames[];
Jason Sams0011bcf2009-12-15 12:58:36 -0800211 int mInputCount;
212 int mOutputCount;
213 int mConstantCount;
214 int mTextureCount;
215 String mShader;
216
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700217 /** @deprecated renderscript is deprecated in J
218 */
Jason Sams0011bcf2009-12-15 12:58:36 -0800219 protected BaseProgramBuilder(RenderScript rs) {
220 mRS = rs;
221 mInputs = new Element[MAX_INPUT];
222 mOutputs = new Element[MAX_OUTPUT];
223 mConstants = new Type[MAX_CONSTANT];
224 mInputCount = 0;
225 mOutputCount = 0;
226 mConstantCount = 0;
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800227 mTextureCount = 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800228 mTextureTypes = new TextureType[MAX_TEXTURE];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800229 mTextureNames = new String[MAX_TEXTURE];
Jason Sams0011bcf2009-12-15 12:58:36 -0800230 }
231
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700232 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800233 * Sets the GLSL shader code to be used in the program
234 *
235 * @param s GLSL shader string
236 * @return self
237 */
Jim Shuma288c8712010-07-07 14:24:21 -0700238 public BaseProgramBuilder setShader(String s) {
Jason Sams0011bcf2009-12-15 12:58:36 -0800239 mShader = s;
Jim Shuma288c8712010-07-07 14:24:21 -0700240 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800241 }
242
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700243 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800244 * Sets the GLSL shader code to be used in the program
245 *
246 * @param resources application resources
247 * @param resourceID id of the file containing GLSL shader code
248 *
249 * @return self
250 */
Alex Sakhartchouka41174e2010-08-27 16:10:55 -0700251 public BaseProgramBuilder setShader(Resources resources, int resourceID) {
252 byte[] str;
253 int strLength;
254 InputStream is = resources.openRawResource(resourceID);
255 try {
256 try {
257 str = new byte[1024];
258 strLength = 0;
259 while(true) {
260 int bytesLeft = str.length - strLength;
261 if (bytesLeft == 0) {
262 byte[] buf2 = new byte[str.length * 2];
263 System.arraycopy(str, 0, buf2, 0, str.length);
264 str = buf2;
265 bytesLeft = str.length - strLength;
266 }
267 int bytesRead = is.read(str, strLength, bytesLeft);
268 if (bytesRead <= 0) {
269 break;
270 }
271 strLength += bytesRead;
272 }
273 } finally {
274 is.close();
275 }
276 } catch(IOException e) {
277 throw new Resources.NotFoundException();
278 }
279
280 try {
281 mShader = new String(str, 0, strLength, "UTF-8");
282 } catch (UnsupportedEncodingException e) {
283 Log.e("Renderscript shader creation", "Could not decode shader string");
284 }
285
286 return this;
287 }
288
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700289 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800290 * Queries the index of the last added constant buffer type
291 *
292 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800293 public int getCurrentConstantIndex() {
294 return mConstantCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800295 }
296
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700297 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800298 * Queries the index of the last added texture type
299 *
300 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800301 public int getCurrentTextureIndex() {
302 return mTextureCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800303 }
304
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700305 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800306 * Adds constant (uniform) inputs to the program
307 *
308 * @param t Type that describes the layout of the Allocation
309 * object to be used as constant inputs to the Program
310 * @return self
311 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800312 public BaseProgramBuilder addConstant(Type t) throws IllegalStateException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800313 // Should check for consistant and non-conflicting names...
314 if(mConstantCount >= MAX_CONSTANT) {
Jason Samsc1d62102010-11-04 14:32:19 -0700315 throw new RSIllegalArgumentException("Max input count exceeded.");
316 }
317 if (t.getElement().isComplex()) {
318 throw new RSIllegalArgumentException("Complex elements not allowed.");
Jason Sams0011bcf2009-12-15 12:58:36 -0800319 }
Jason Samsea87e962010-01-12 12:12:28 -0800320 mConstants[mConstantCount] = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800321 mConstantCount++;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800322 return this;
323 }
324
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700325 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800326 * Adds a texture input to the Program
327 *
328 * @param texType describes that the texture to append it (2D,
329 * Cubemap, etc.)
330 * @return self
331 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800332 public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException {
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800333 addTexture(texType, "Tex" + mTextureCount);
334 return this;
335 }
336
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700337 /** @hide renderscript is deprecated in J
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800338 * Adds a texture input to the Program
339 *
340 * @param texType describes that the texture to append it (2D,
341 * Cubemap, etc.)
342 * @param texName what the texture should be called in the
343 * shader
344 * @return self
345 */
346 public BaseProgramBuilder addTexture(TextureType texType, String texName)
347 throws IllegalArgumentException {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800348 if(mTextureCount >= MAX_TEXTURE) {
349 throw new IllegalArgumentException("Max texture count exceeded.");
350 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800351 mTextureTypes[mTextureCount] = texType;
352 mTextureNames[mTextureCount] = texName;
353 mTextureCount ++;
Jim Shuma288c8712010-07-07 14:24:21 -0700354 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800355 }
356
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700357 /** @deprecated renderscript is deprecated in J
358 */
Jason Sams0011bcf2009-12-15 12:58:36 -0800359 protected void initProgram(Program p) {
360 p.mInputs = new Element[mInputCount];
361 System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
362 p.mOutputs = new Element[mOutputCount];
363 System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
364 p.mConstants = new Type[mConstantCount];
365 System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800366 p.mTextureCount = mTextureCount;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800367 p.mTextures = new TextureType[mTextureCount];
368 System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800369 p.mTextureNames = new String[mTextureCount];
370 System.arraycopy(mTextureNames, 0, p.mTextureNames, 0, mTextureCount);
Jason Sams0011bcf2009-12-15 12:58:36 -0800371 }
372 }
373
374}
375
376