blob: a965b819d8b1c7aa15949d081b69cc7caf94eb87 [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
17package android.renderscript;
18
19
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070020import android.graphics.Matrix;
Jason Sams110195f2009-08-04 18:47:46 -070021import android.util.Config;
22import android.util.Log;
23
24
25/**
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080026 * ProgramVertex, also know as a vertex shader, describes a
27 * stage in the graphics pipeline responsible for manipulating
28 * geometric data in a user-defined way.
Jason Sams110195f2009-08-04 18:47:46 -070029 *
30 **/
Jason Sams0011bcf2009-12-15 12:58:36 -080031public class ProgramVertex extends Program {
Jason Sams0011bcf2009-12-15 12:58:36 -080032
Jason Sams110195f2009-08-04 18:47:46 -070033 ProgramVertex(int id, RenderScript rs) {
Jason Sams0011bcf2009-12-15 12:58:36 -080034 super(id, rs);
Jason Sams110195f2009-08-04 18:47:46 -070035 }
36
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080037 /**
38 * Builder class for creating ProgramVertex objects.
39 * The builder starts empty and the user must minimally provide
40 * the GLSL shader code, and the varying inputs. Constant, or
41 * uniform parameters to the shader may optionally be provided as
42 * well.
43 *
44 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080045 public static class Builder extends BaseProgramBuilder {
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080046 /**
47 * Create a builder object.
48 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080049 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080050 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080051 public Builder(RenderScript rs) {
Jason Sams0011bcf2009-12-15 12:58:36 -080052 super(rs);
Jason Sams110195f2009-08-04 18:47:46 -070053 }
54
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080055 /**
56 * Add varying inputs to the program
57 *
58 * @param e element describing the layout of the varying input
59 * structure
60 * @return self
61 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080062 public Builder addInput(Element e) throws IllegalStateException {
63 // Should check for consistant and non-conflicting names...
64 if(mInputCount >= MAX_INPUT) {
65 throw new RSIllegalArgumentException("Max input count exceeded.");
66 }
67 if (e.isComplex()) {
68 throw new RSIllegalArgumentException("Complex elements not allowed.");
69 }
70 mInputs[mInputCount++] = e;
71 return this;
72 }
73
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080074 /**
75 * Creates ProgramVertex from the current state of the builder
76 *
77 * @return ProgramVertex
78 */
Jason Sams110195f2009-08-04 18:47:46 -070079 public ProgramVertex create() {
Jason Sams771bebb2009-12-07 12:40:12 -080080 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080081 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Jason Sams0011bcf2009-12-15 12:58:36 -080082 int idx = 0;
83
84 for (int i=0; i < mInputCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080085 tmp[idx++] = ProgramParam.INPUT.mID;
Jason Sams06d69de2010-11-09 17:11:40 -080086 tmp[idx++] = mInputs[i].getID();
Jason Sams0011bcf2009-12-15 12:58:36 -080087 }
88 for (int i=0; i < mOutputCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080089 tmp[idx++] = ProgramParam.OUTPUT.mID;
Jason Sams06d69de2010-11-09 17:11:40 -080090 tmp[idx++] = mOutputs[i].getID();
Jason Sams0011bcf2009-12-15 12:58:36 -080091 }
92 for (int i=0; i < mConstantCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080093 tmp[idx++] = ProgramParam.CONSTANT.mID;
Jason Sams06d69de2010-11-09 17:11:40 -080094 tmp[idx++] = mConstants[i].getID();
Jason Sams0011bcf2009-12-15 12:58:36 -080095 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080096 for (int i=0; i < mTextureCount; i++) {
97 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
98 tmp[idx++] = mTextureTypes[i].mID;
99 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800100
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700101 int id = mRS.nProgramVertexCreate(mShader, tmp);
Jason Sams0011bcf2009-12-15 12:58:36 -0800102 ProgramVertex pv = new ProgramVertex(id, mRS);
103 initProgram(pv);
104 return pv;
Jason Sams110195f2009-08-04 18:47:46 -0700105 }
106 }
107
Jason Sams110195f2009-08-04 18:47:46 -0700108}