blob: 1c5a191ae20500c05cac8e13a9bda63dd97b9a53 [file] [log] [blame]
Jason Sams110195f2009-08-04 18:47:46 -07001/*
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
Tim Murraya9084222013-04-05 22:06:43 +000017/**
18 * @hide
Tim Murrayc11e25c2013-04-09 11:01:01 -070019 * <p>The RenderScript vertex program, also known as a vertex shader, describes a stage in
Robert Ly11518ac2011-02-09 13:57:06 -080020 * the graphics pipeline responsible for manipulating geometric data in a user-defined way.
Tim Murrayc11e25c2013-04-09 11:01:01 -070021 * The object is constructed by providing the RenderScript system with the following data:</p>
Robert Ly11518ac2011-02-09 13:57:06 -080022 * <ul>
23 * <li>Element describing its varying inputs or attributes</li>
24 * <li>GLSL shader string that defines the body of the program</li>
25 * <li>a Type that describes the layout of an Allocation containing constant or uniform inputs</li>
26 * </ul>
27 *
28 * <p>Once the program is created, you bind it to the graphics context, RenderScriptGL, and it will be used for
29 * all subsequent draw calls until you bind a new program. If the program has constant inputs,
30 * the user needs to bind an allocation containing those inputs. The allocation's type must match
Tim Murrayc11e25c2013-04-09 11:01:01 -070031 * the one provided during creation. The RenderScript library then does all the necessary plumbing
Robert Ly11518ac2011-02-09 13:57:06 -080032 * to send those constants to the graphics hardware. Varying inputs to the shader, such as position, normal,
33 * and texture coordinates are matched by name between the input Element and the Mesh object being drawn.
34 * The signatures don't have to be exact or in any strict order. As long as the input name in the shader
35 * matches a channel name and size available on the mesh, the runtime takes care of connecting the
36 * two. Unlike OpenGL, there is no need to link the vertex and fragment programs.</p>
37 *
38 **/
Jason Sams110195f2009-08-04 18:47:46 -070039package android.renderscript;
40
41
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070042import android.graphics.Matrix;
Jason Sams110195f2009-08-04 18:47:46 -070043import android.util.Log;
44
45
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070046/**
Tim Murraya9084222013-04-05 22:06:43 +000047 * @hide
Jason Samsd4ca9912012-05-08 19:02:07 -070048 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080049 * ProgramVertex, also know as a vertex shader, describes a
50 * stage in the graphics pipeline responsible for manipulating
51 * geometric data in a user-defined way.
Jason Sams110195f2009-08-04 18:47:46 -070052 *
53 **/
Jason Sams0011bcf2009-12-15 12:58:36 -080054public class ProgramVertex extends Program {
Jason Sams0011bcf2009-12-15 12:58:36 -080055
Jason Sams110195f2009-08-04 18:47:46 -070056 ProgramVertex(int id, RenderScript rs) {
Jason Sams0011bcf2009-12-15 12:58:36 -080057 super(id, rs);
Jason Sams110195f2009-08-04 18:47:46 -070058 }
59
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070060 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070061 * @deprecated in API 16
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070062 * @return number of input attribute elements
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -080063 */
64 public int getInputCount() {
65 return mInputs != null ? mInputs.length : 0;
66 }
67
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070068 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070069 * @deprecated in API 16
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070070 * @param slot location of the input to return
71 * @return input attribute element
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -080072 */
73 public Element getInput(int slot) {
74 if (slot < 0 || slot >= mInputs.length) {
75 throw new IllegalArgumentException("Slot ID out of range.");
76 }
77 return mInputs[slot];
78 }
79
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070080 /**
Tim Murraya9084222013-04-05 22:06:43 +000081 * @hide
82 * @deprecated in API 16
83 * Builder class for creating ProgramVertex objects.
84 * The builder starts empty and the user must minimally provide
85 * the GLSL shader code, and the varying inputs. Constant, or
86 * uniform parameters to the shader may optionally be provided as
87 * well.
88 *
89 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080090 public static class Builder extends BaseProgramBuilder {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070091 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070092 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080093 * Create a builder object.
94 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080095 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080096 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080097 public Builder(RenderScript rs) {
Jason Sams0011bcf2009-12-15 12:58:36 -080098 super(rs);
Jason Sams110195f2009-08-04 18:47:46 -070099 }
100
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700101 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700102 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800103 * Add varying inputs to the program
104 *
105 * @param e element describing the layout of the varying input
106 * structure
107 * @return self
108 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800109 public Builder addInput(Element e) throws IllegalStateException {
110 // Should check for consistant and non-conflicting names...
111 if(mInputCount >= MAX_INPUT) {
112 throw new RSIllegalArgumentException("Max input count exceeded.");
113 }
114 if (e.isComplex()) {
115 throw new RSIllegalArgumentException("Complex elements not allowed.");
116 }
117 mInputs[mInputCount++] = e;
118 return this;
119 }
120
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700121 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700122 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800123 * Creates ProgramVertex from the current state of the builder
124 *
125 * @return ProgramVertex
126 */
Jason Sams110195f2009-08-04 18:47:46 -0700127 public ProgramVertex create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800128 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800129 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800130 String[] texNames = new String[mTextureCount];
Jason Sams0011bcf2009-12-15 12:58:36 -0800131 int idx = 0;
132
133 for (int i=0; i < mInputCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800134 tmp[idx++] = ProgramParam.INPUT.mID;
Jason Samse07694b2012-04-03 15:36:36 -0700135 tmp[idx++] = mInputs[i].getID(mRS);
Jason Sams0011bcf2009-12-15 12:58:36 -0800136 }
137 for (int i=0; i < mOutputCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800138 tmp[idx++] = ProgramParam.OUTPUT.mID;
Jason Samse07694b2012-04-03 15:36:36 -0700139 tmp[idx++] = mOutputs[i].getID(mRS);
Jason Sams0011bcf2009-12-15 12:58:36 -0800140 }
141 for (int i=0; i < mConstantCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800142 tmp[idx++] = ProgramParam.CONSTANT.mID;
Jason Samse07694b2012-04-03 15:36:36 -0700143 tmp[idx++] = mConstants[i].getID(mRS);
Jason Sams0011bcf2009-12-15 12:58:36 -0800144 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800145 for (int i=0; i < mTextureCount; i++) {
146 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
147 tmp[idx++] = mTextureTypes[i].mID;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800148 texNames[i] = mTextureNames[i];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800149 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800150
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800151 int id = mRS.nProgramVertexCreate(mShader, texNames, tmp);
Jason Sams0011bcf2009-12-15 12:58:36 -0800152 ProgramVertex pv = new ProgramVertex(id, mRS);
153 initProgram(pv);
154 return pv;
Jason Sams110195f2009-08-04 18:47:46 -0700155 }
156 }
157
Jason Sams110195f2009-08-04 18:47:46 -0700158}