blob: 74d666bd9a5884a5d056752b2aa895c448319a96 [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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070017 /**
Robert Ly11518ac2011-02-09 13:57:06 -080018 * <p>The Renderscript vertex program, also known as a vertex shader, describes a stage in
19 * the graphics pipeline responsible for manipulating geometric data in a user-defined way.
20 * The object is constructed by providing the Renderscript system with the following data:</p>
21 * <ul>
22 * <li>Element describing its varying inputs or attributes</li>
23 * <li>GLSL shader string that defines the body of the program</li>
24 * <li>a Type that describes the layout of an Allocation containing constant or uniform inputs</li>
25 * </ul>
26 *
27 * <p>Once the program is created, you bind it to the graphics context, RenderScriptGL, and it will be used for
28 * all subsequent draw calls until you bind a new program. If the program has constant inputs,
29 * the user needs to bind an allocation containing those inputs. The allocation's type must match
30 * the one provided during creation. The Renderscript library then does all the necessary plumbing
31 * to send those constants to the graphics hardware. Varying inputs to the shader, such as position, normal,
32 * and texture coordinates are matched by name between the input Element and the Mesh object being drawn.
33 * The signatures don't have to be exact or in any strict order. As long as the input name in the shader
34 * matches a channel name and size available on the mesh, the runtime takes care of connecting the
35 * two. Unlike OpenGL, there is no need to link the vertex and fragment programs.</p>
36 *
37 **/
Jason Sams110195f2009-08-04 18:47:46 -070038package android.renderscript;
39
40
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070041import android.graphics.Matrix;
Jason Sams110195f2009-08-04 18:47:46 -070042import android.util.Log;
43
44
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070045/**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080046 * ProgramVertex, also know as a vertex shader, describes a
47 * stage in the graphics pipeline responsible for manipulating
48 * geometric data in a user-defined way.
Jason Sams110195f2009-08-04 18:47:46 -070049 *
50 **/
Jason Sams0011bcf2009-12-15 12:58:36 -080051public class ProgramVertex extends Program {
Jason Sams0011bcf2009-12-15 12:58:36 -080052
Jason Sams110195f2009-08-04 18:47:46 -070053 ProgramVertex(int id, RenderScript rs) {
Jason Sams0011bcf2009-12-15 12:58:36 -080054 super(id, rs);
Jason Sams110195f2009-08-04 18:47:46 -070055 }
56
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070057 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070058 * @return number of input attribute elements
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -080059 */
60 public int getInputCount() {
61 return mInputs != null ? mInputs.length : 0;
62 }
63
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070064 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070065 * @param slot location of the input to return
66 * @return input attribute element
Alex Sakhartchoukd5a62bb2012-01-06 10:36:06 -080067 */
68 public Element getInput(int slot) {
69 if (slot < 0 || slot >= mInputs.length) {
70 throw new IllegalArgumentException("Slot ID out of range.");
71 }
72 return mInputs[slot];
73 }
74
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070075 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080076 * Builder class for creating ProgramVertex objects.
77 * The builder starts empty and the user must minimally provide
78 * the GLSL shader code, and the varying inputs. Constant, or
79 * uniform parameters to the shader may optionally be provided as
80 * well.
81 *
82 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080083 public static class Builder extends BaseProgramBuilder {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070084 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080085 * Create a builder object.
86 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080087 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080088 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080089 public Builder(RenderScript rs) {
Jason Sams0011bcf2009-12-15 12:58:36 -080090 super(rs);
Jason Sams110195f2009-08-04 18:47:46 -070091 }
92
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070093 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080094 * Add varying inputs to the program
95 *
96 * @param e element describing the layout of the varying input
97 * structure
98 * @return self
99 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800100 public Builder addInput(Element e) throws IllegalStateException {
101 // Should check for consistant and non-conflicting names...
102 if(mInputCount >= MAX_INPUT) {
103 throw new RSIllegalArgumentException("Max input count exceeded.");
104 }
105 if (e.isComplex()) {
106 throw new RSIllegalArgumentException("Complex elements not allowed.");
107 }
108 mInputs[mInputCount++] = e;
109 return this;
110 }
111
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700112 /**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800113 * Creates ProgramVertex from the current state of the builder
114 *
115 * @return ProgramVertex
116 */
Jason Sams110195f2009-08-04 18:47:46 -0700117 public ProgramVertex create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800118 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800119 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800120 String[] texNames = new String[mTextureCount];
Jason Sams0011bcf2009-12-15 12:58:36 -0800121 int idx = 0;
122
123 for (int i=0; i < mInputCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800124 tmp[idx++] = ProgramParam.INPUT.mID;
Jason Samse07694b2012-04-03 15:36:36 -0700125 tmp[idx++] = mInputs[i].getID(mRS);
Jason Sams0011bcf2009-12-15 12:58:36 -0800126 }
127 for (int i=0; i < mOutputCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800128 tmp[idx++] = ProgramParam.OUTPUT.mID;
Jason Samse07694b2012-04-03 15:36:36 -0700129 tmp[idx++] = mOutputs[i].getID(mRS);
Jason Sams0011bcf2009-12-15 12:58:36 -0800130 }
131 for (int i=0; i < mConstantCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800132 tmp[idx++] = ProgramParam.CONSTANT.mID;
Jason Samse07694b2012-04-03 15:36:36 -0700133 tmp[idx++] = mConstants[i].getID(mRS);
Jason Sams0011bcf2009-12-15 12:58:36 -0800134 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800135 for (int i=0; i < mTextureCount; i++) {
136 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
137 tmp[idx++] = mTextureTypes[i].mID;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800138 texNames[i] = mTextureNames[i];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800139 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800140
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800141 int id = mRS.nProgramVertexCreate(mShader, texNames, tmp);
Jason Sams0011bcf2009-12-15 12:58:36 -0800142 ProgramVertex pv = new ProgramVertex(id, mRS);
143 initProgram(pv);
144 return pv;
Jason Sams110195f2009-08-04 18:47:46 -0700145 }
146 }
147
Jason Sams110195f2009-08-04 18:47:46 -0700148}