blob: 3cadc935e67c0722e6ea9a8aa8197d6078a7e865 [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
Artur Satayev2ebb31c2020-01-08 12:24:36 +000020import android.compat.annotation.UnsupportedAppUsage;
21import android.content.res.Resources;
22import android.util.Log;
23
Alex Sakhartchouka41174e2010-08-27 16:10:55 -070024import java.io.IOException;
25import java.io.InputStream;
26import java.io.UnsupportedEncodingException;
27
Jason Sams0011bcf2009-12-15 12:58:36 -080028
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070029/**
Tim Murraya9084222013-04-05 22:06:43 +000030 * @hide
Jason Sams0011bcf2009-12-15 12:58:36 -080031 *
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080032 * Program is a base class for all the objects that modify
33 * various stages of the graphics pipeline
34 *
Xusong Wang8b4548c2021-01-05 10:09:52 -080035 * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
36 * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
37 * guide</a> for the proposed alternatives.
Jason Sams0011bcf2009-12-15 12:58:36 -080038 **/
Xusong Wang8b4548c2021-01-05 10:09:52 -080039@Deprecated
Jason Sams0011bcf2009-12-15 12:58:36 -080040public class Program extends BaseObj {
Alex Sakhartchouk0473ff12011-01-14 11:27:27 -080041 static final int MAX_INPUT = 8;
42 static final int MAX_OUTPUT = 8;
43 static final int MAX_CONSTANT = 8;
44 static final int MAX_TEXTURE = 8;
Jason Sams0011bcf2009-12-15 12:58:36 -080045
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070046 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080047 *
48 * TextureType specifies what textures are attached to Program
49 * objects
50 *
51 **/
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080052 public enum TextureType {
Mathew Inwood15324472018-08-06 11:18:49 +010053 @UnsupportedAppUsage
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080054 TEXTURE_2D (0),
55 TEXTURE_CUBE (1);
56
57 int mID;
58 TextureType(int id) {
59 mID = id;
60 }
61 }
62
63 enum ProgramParam {
64 INPUT (0),
65 OUTPUT (1),
66 CONSTANT (2),
67 TEXTURE_TYPE (3);
68
69 int mID;
70 ProgramParam(int id) {
71 mID = id;
72 }
73 };
74
Jason Sams0011bcf2009-12-15 12:58:36 -080075 Element mInputs[];
76 Element mOutputs[];
77 Type mConstants[];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080078 TextureType mTextures[];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080079 String mTextureNames[];
Jason Sams7e5ab3b2009-12-15 13:27:04 -080080 int mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -080081 String mShader;
82
Tim Murray460a0492013-11-19 12:45:54 -080083 Program(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070084 super(id, rs);
Yang Nieb4dd082016-03-24 09:40:32 -070085 guard.open("destroy");
Jason Sams0011bcf2009-12-15 12:58:36 -080086 }
87
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070088 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070089 * Program object can have zero or more constant allocations
90 * associated with it. This method returns the total count.
91 * @return number of constant input types
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -080092 */
93 public int getConstantCount() {
94 return mConstants != null ? mConstants.length : 0;
95 }
96
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070097 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070098 * Returns the type of the constant buffer used in the program
99 * object. It could be used to query internal elements or create
100 * an allocation to store constant data.
101 * @param slot index of the constant input type to return
102 * @return constant input type
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -0800103 */
104 public Type getConstant(int slot) {
105 if (slot < 0 || slot >= mConstants.length) {
106 throw new IllegalArgumentException("Slot ID out of range.");
107 }
108 return mConstants[slot];
109 }
110
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700111 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700112 * Returns the number of textures used in this program object
113 * @return number of texture inputs
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -0800114 */
115 public int getTextureCount() {
116 return mTextureCount;
117 }
118
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700119 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700120 * Returns the type of texture at a given slot. e.g. 2D or Cube
121 * @param slot index of the texture input
122 * @return texture input type
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -0800123 */
124 public TextureType getTextureType(int slot) {
125 if ((slot < 0) || (slot >= mTextureCount)) {
126 throw new IllegalArgumentException("Slot ID out of range.");
127 }
128 return mTextures[slot];
129 }
130
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700131 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700132 * Returns the name of the texture input at a given slot. e.g.
133 * tex0, diffuse, spec
134 * @param slot index of the texture input
135 * @return texture input name
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800136 */
137 public String getTextureName(int slot) {
138 if ((slot < 0) || (slot >= mTextureCount)) {
139 throw new IllegalArgumentException("Slot ID out of range.");
140 }
141 return mTextureNames[slot];
142 }
143
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700144 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800145 * Binds a constant buffer to be used as uniform inputs to the
146 * program
147 *
148 * @param a allocation containing uniform data
149 * @param slot index within the program's list of constant
150 * buffer allocations
151 */
Jason Sams0011bcf2009-12-15 12:58:36 -0800152 public void bindConstants(Allocation a, int slot) {
Jason Samsc1d62102010-11-04 14:32:19 -0700153 if (slot < 0 || slot >= mConstants.length) {
154 throw new IllegalArgumentException("Slot ID out of range.");
155 }
156 if (a != null &&
Jason Samse07694b2012-04-03 15:36:36 -0700157 a.getType().getID(mRS) != mConstants[slot].getID(mRS)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700158 throw new IllegalArgumentException("Allocation type does not match slot type.");
159 }
Tim Murray460a0492013-11-19 12:45:54 -0800160 long id = a != null ? a.getID(mRS) : 0;
Jason Samse07694b2012-04-03 15:36:36 -0700161 mRS.nProgramBindConstants(getID(mRS), slot, id);
Jason Sams0011bcf2009-12-15 12:58:36 -0800162 }
163
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700164 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800165 * Binds a texture to be used in the program
166 *
167 * @param va allocation containing texture data
168 * @param slot index within the program's list of textures
169 *
170 */
Jason Sams68afd012009-12-17 16:55:08 -0800171 public void bindTexture(Allocation va, int slot)
172 throws IllegalArgumentException {
173 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800174 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -0800175 throw new IllegalArgumentException("Slot ID out of range.");
176 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800177 if (va != null && va.getType().hasFaces() &&
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800178 mTextures[slot] != TextureType.TEXTURE_CUBE) {
179 throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot");
180 }
Jason Sams68afd012009-12-17 16:55:08 -0800181
Tim Murray460a0492013-11-19 12:45:54 -0800182 long id = va != null ? va.getID(mRS) : 0;
Jason Samse07694b2012-04-03 15:36:36 -0700183 mRS.nProgramBindTexture(getID(mRS), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -0800184 }
185
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700186 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800187 * Binds an object that describes how a texture at the
188 * corresponding location is sampled
189 *
190 * @param vs sampler for a corresponding texture
191 * @param slot index within the program's list of textures to
192 * use the sampler on
193 *
194 */
Jason Sams68afd012009-12-17 16:55:08 -0800195 public void bindSampler(Sampler vs, int slot)
196 throws IllegalArgumentException {
197 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800198 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -0800199 throw new IllegalArgumentException("Slot ID out of range.");
200 }
201
Tim Murray460a0492013-11-19 12:45:54 -0800202 long id = vs != null ? vs.getID(mRS) : 0;
Jason Samse07694b2012-04-03 15:36:36 -0700203 mRS.nProgramBindSampler(getID(mRS), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -0800204 }
205
206
Jason Sams0011bcf2009-12-15 12:58:36 -0800207 public static class BaseProgramBuilder {
Mathew Inwood15324472018-08-06 11:18:49 +0100208 @UnsupportedAppUsage
Jason Sams0011bcf2009-12-15 12:58:36 -0800209 RenderScript mRS;
Mathew Inwood15324472018-08-06 11:18:49 +0100210 @UnsupportedAppUsage
Jason Sams0011bcf2009-12-15 12:58:36 -0800211 Element mInputs[];
Mathew Inwood15324472018-08-06 11:18:49 +0100212 @UnsupportedAppUsage
Jason Sams0011bcf2009-12-15 12:58:36 -0800213 Element mOutputs[];
Mathew Inwood15324472018-08-06 11:18:49 +0100214 @UnsupportedAppUsage
Jason Sams0011bcf2009-12-15 12:58:36 -0800215 Type mConstants[];
216 Type mTextures[];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800217 TextureType mTextureTypes[];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800218 String mTextureNames[];
Mathew Inwood15324472018-08-06 11:18:49 +0100219 @UnsupportedAppUsage
Jason Sams0011bcf2009-12-15 12:58:36 -0800220 int mInputCount;
Mathew Inwood15324472018-08-06 11:18:49 +0100221 @UnsupportedAppUsage
Jason Sams0011bcf2009-12-15 12:58:36 -0800222 int mOutputCount;
Mathew Inwood15324472018-08-06 11:18:49 +0100223 @UnsupportedAppUsage
Jason Sams0011bcf2009-12-15 12:58:36 -0800224 int mConstantCount;
Mathew Inwood15324472018-08-06 11:18:49 +0100225 @UnsupportedAppUsage
Jason Sams0011bcf2009-12-15 12:58:36 -0800226 int mTextureCount;
Mathew Inwood15324472018-08-06 11:18:49 +0100227 @UnsupportedAppUsage
Jason Sams0011bcf2009-12-15 12:58:36 -0800228 String mShader;
229
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700230
Mathew Inwood15324472018-08-06 11:18:49 +0100231 @UnsupportedAppUsage
Jason Sams0011bcf2009-12-15 12:58:36 -0800232 protected BaseProgramBuilder(RenderScript rs) {
233 mRS = rs;
234 mInputs = new Element[MAX_INPUT];
235 mOutputs = new Element[MAX_OUTPUT];
236 mConstants = new Type[MAX_CONSTANT];
237 mInputCount = 0;
238 mOutputCount = 0;
239 mConstantCount = 0;
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800240 mTextureCount = 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800241 mTextureTypes = new TextureType[MAX_TEXTURE];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800242 mTextureNames = new String[MAX_TEXTURE];
Jason Sams0011bcf2009-12-15 12:58:36 -0800243 }
244
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700245 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800246 * Sets the GLSL shader code to be used in the program
247 *
248 * @param s GLSL shader string
249 * @return self
250 */
Jim Shuma288c8712010-07-07 14:24:21 -0700251 public BaseProgramBuilder setShader(String s) {
Jason Sams0011bcf2009-12-15 12:58:36 -0800252 mShader = s;
Jim Shuma288c8712010-07-07 14:24:21 -0700253 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800254 }
255
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700256 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800257 * Sets the GLSL shader code to be used in the program
258 *
259 * @param resources application resources
260 * @param resourceID id of the file containing GLSL shader code
261 *
262 * @return self
263 */
Alex Sakhartchouka41174e2010-08-27 16:10:55 -0700264 public BaseProgramBuilder setShader(Resources resources, int resourceID) {
265 byte[] str;
266 int strLength;
267 InputStream is = resources.openRawResource(resourceID);
268 try {
269 try {
270 str = new byte[1024];
271 strLength = 0;
272 while(true) {
273 int bytesLeft = str.length - strLength;
274 if (bytesLeft == 0) {
275 byte[] buf2 = new byte[str.length * 2];
276 System.arraycopy(str, 0, buf2, 0, str.length);
277 str = buf2;
278 bytesLeft = str.length - strLength;
279 }
280 int bytesRead = is.read(str, strLength, bytesLeft);
281 if (bytesRead <= 0) {
282 break;
283 }
284 strLength += bytesRead;
285 }
286 } finally {
287 is.close();
288 }
289 } catch(IOException e) {
290 throw new Resources.NotFoundException();
291 }
292
293 try {
294 mShader = new String(str, 0, strLength, "UTF-8");
295 } catch (UnsupportedEncodingException e) {
Tim Murrayc11e25c2013-04-09 11:01:01 -0700296 Log.e("RenderScript shader creation", "Could not decode shader string");
Alex Sakhartchouka41174e2010-08-27 16:10:55 -0700297 }
298
299 return this;
300 }
301
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700302 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800303 * Queries the index of the last added constant buffer type
304 *
305 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800306 public int getCurrentConstantIndex() {
307 return mConstantCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800308 }
309
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700310 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800311 * Queries the index of the last added texture type
312 *
313 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800314 public int getCurrentTextureIndex() {
315 return mTextureCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800316 }
317
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700318 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800319 * Adds constant (uniform) inputs to the program
320 *
321 * @param t Type that describes the layout of the Allocation
322 * object to be used as constant inputs to the Program
323 * @return self
324 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800325 public BaseProgramBuilder addConstant(Type t) throws IllegalStateException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800326 // Should check for consistant and non-conflicting names...
327 if(mConstantCount >= MAX_CONSTANT) {
Jason Samsc1d62102010-11-04 14:32:19 -0700328 throw new RSIllegalArgumentException("Max input count exceeded.");
329 }
330 if (t.getElement().isComplex()) {
331 throw new RSIllegalArgumentException("Complex elements not allowed.");
Jason Sams0011bcf2009-12-15 12:58:36 -0800332 }
Jason Samsea87e962010-01-12 12:12:28 -0800333 mConstants[mConstantCount] = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800334 mConstantCount++;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800335 return this;
336 }
337
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700338 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800339 * Adds a texture input to the Program
340 *
341 * @param texType describes that the texture to append it (2D,
342 * Cubemap, etc.)
343 * @return self
344 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800345 public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException {
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800346 addTexture(texType, "Tex" + mTextureCount);
347 return this;
348 }
349
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700350 /**
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800351 * Adds a texture input to the Program
352 *
353 * @param texType describes that the texture to append it (2D,
354 * Cubemap, etc.)
355 * @param texName what the texture should be called in the
356 * shader
357 * @return self
358 */
359 public BaseProgramBuilder addTexture(TextureType texType, String texName)
360 throws IllegalArgumentException {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800361 if(mTextureCount >= MAX_TEXTURE) {
362 throw new IllegalArgumentException("Max texture count exceeded.");
363 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800364 mTextureTypes[mTextureCount] = texType;
365 mTextureNames[mTextureCount] = texName;
366 mTextureCount ++;
Jim Shuma288c8712010-07-07 14:24:21 -0700367 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800368 }
369
370 protected void initProgram(Program p) {
371 p.mInputs = new Element[mInputCount];
372 System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
373 p.mOutputs = new Element[mOutputCount];
374 System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
375 p.mConstants = new Type[mConstantCount];
376 System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800377 p.mTextureCount = mTextureCount;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800378 p.mTextures = new TextureType[mTextureCount];
379 System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800380 p.mTextureNames = new String[mTextureCount];
381 System.arraycopy(mTextureNames, 0, p.mTextureNames, 0, mTextureCount);
Jason Sams0011bcf2009-12-15 12:58:36 -0800382 }
383 }
384
385}
386
387