blob: 998e05e92471aa65de17b917257798e992c0f316 [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/**
26 * @hide
27 *
28 **/
Jason Sams0011bcf2009-12-15 12:58:36 -080029public class ProgramVertex extends Program {
Jason Sams110195f2009-08-04 18:47:46 -070030 public static final int MAX_LIGHT = 8;
31
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
Jason Sams9bee51c2009-08-05 13:57:03 -070037 public void bindAllocation(MatrixAllocation va) {
Jason Sams771bebb2009-12-07 12:40:12 -080038 mRS.validate();
Jason Sams0011bcf2009-12-15 12:58:36 -080039 bindConstants(va.mAlloc, 0);
Jason Sams110195f2009-08-04 18:47:46 -070040 }
41
Jason Sams0011bcf2009-12-15 12:58:36 -080042 public static class ShaderBuilder extends BaseProgramBuilder {
43 public ShaderBuilder(RenderScript rs) {
44 super(rs);
Jason Sams110195f2009-08-04 18:47:46 -070045 }
46
47 public ProgramVertex create() {
Jason Sams771bebb2009-12-07 12:40:12 -080048 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080049 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Jason Sams0011bcf2009-12-15 12:58:36 -080050 int idx = 0;
51
52 for (int i=0; i < mInputCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080053 tmp[idx++] = ProgramParam.INPUT.mID;
Jason Sams06d69de2010-11-09 17:11:40 -080054 tmp[idx++] = mInputs[i].getID();
Jason Sams0011bcf2009-12-15 12:58:36 -080055 }
56 for (int i=0; i < mOutputCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080057 tmp[idx++] = ProgramParam.OUTPUT.mID;
Jason Sams06d69de2010-11-09 17:11:40 -080058 tmp[idx++] = mOutputs[i].getID();
Jason Sams0011bcf2009-12-15 12:58:36 -080059 }
60 for (int i=0; i < mConstantCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080061 tmp[idx++] = ProgramParam.CONSTANT.mID;
Jason Sams06d69de2010-11-09 17:11:40 -080062 tmp[idx++] = mConstants[i].getID();
Jason Sams0011bcf2009-12-15 12:58:36 -080063 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080064 for (int i=0; i < mTextureCount; i++) {
65 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
66 tmp[idx++] = mTextureTypes[i].mID;
67 }
Jason Sams0011bcf2009-12-15 12:58:36 -080068
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -070069 int id = mRS.nProgramVertexCreate(mShader, tmp);
Jason Sams0011bcf2009-12-15 12:58:36 -080070 ProgramVertex pv = new ProgramVertex(id, mRS);
71 initProgram(pv);
72 return pv;
Jason Sams110195f2009-08-04 18:47:46 -070073 }
74 }
75
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070076 public static class Builder extends ShaderBuilder {
77 boolean mTextureMatrixEnable;
78
79 public Builder(RenderScript rs, Element in, Element out) {
80 super(rs);
81 }
82 public Builder(RenderScript rs) {
83 super(rs);
84 }
85
86 public Builder setTextureMatrixEnable(boolean enable) {
87 mTextureMatrixEnable = enable;
88 return this;
89 }
90 static Type getConstantInputType(RenderScript rs) {
91 Element.Builder b = new Element.Builder(rs);
92 b.add(Element.MATRIX4X4(rs), "MV");
93 b.add(Element.MATRIX4X4(rs), "P");
94 b.add(Element.MATRIX4X4(rs), "TexMatrix");
95 b.add(Element.MATRIX4X4(rs), "MVP");
96
97 Type.Builder typeBuilder = new Type.Builder(rs, b.create());
98 typeBuilder.add(Dimension.X, 1);
99 return typeBuilder.create();
100 }
101
102 private void buildShaderString() {
103
104 mShader = "//rs_shader_internal\n";
105 mShader += "varying vec4 varColor;\n";
Alex Sakhartchoukd2091632010-10-06 11:15:01 -0700106 mShader += "varying vec2 varTex0;\n";
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700107
108 mShader += "void main() {\n";
109 mShader += " gl_Position = UNI_MVP * ATTRIB_position;\n";
110 mShader += " gl_PointSize = 1.0;\n";
111
112 mShader += " varColor = ATTRIB_color;\n";
113 if (mTextureMatrixEnable) {
Alex Sakhartchoukd2091632010-10-06 11:15:01 -0700114 mShader += " varTex0 = (UNI_TexMatrix * vec4(ATTRIB_texture0, 0.0, 1.0)).xy;\n";
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700115 } else {
116 mShader += " varTex0 = ATTRIB_texture0;\n";
117 }
118 mShader += "}\n";
119 }
120
121 @Override
122 public ProgramVertex create() {
123 buildShaderString();
124
125 addConstant(getConstantInputType(mRS));
126
127 Element.Builder b = new Element.Builder(mRS);
128 b.add(Element.F32_4(mRS), "position");
129 b.add(Element.F32_4(mRS), "color");
130 b.add(Element.F32_3(mRS), "normal");
Alex Sakhartchoukd2091632010-10-06 11:15:01 -0700131 b.add(Element.F32_2(mRS), "texture0");
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700132 addInput(b.create());
133
134 return super.create();
135 }
136 }
137
Jason Sams110195f2009-08-04 18:47:46 -0700138
139
140 public static class MatrixAllocation {
141 static final int MODELVIEW_OFFSET = 0;
142 static final int PROJECTION_OFFSET = 16;
143 static final int TEXTURE_OFFSET = 32;
144
Jason Sams25430d02010-02-02 15:26:40 -0800145 Matrix4f mModel;
146 Matrix4f mProjection;
147 Matrix4f mTexture;
Jason Sams110195f2009-08-04 18:47:46 -0700148
149 public Allocation mAlloc;
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700150 private FieldPacker mIOBuffer;
Jason Sams110195f2009-08-04 18:47:46 -0700151
152 public MatrixAllocation(RenderScript rs) {
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700153 Type constInputType = ProgramVertex.Builder.getConstantInputType(rs);
154 mAlloc = Allocation.createTyped(rs, constInputType);
155 int bufferSize = constInputType.getElement().getSizeBytes()*
156 constInputType.getElementCount();
157 mIOBuffer = new FieldPacker(bufferSize);
Alex Sakhartchouka41174e2010-08-27 16:10:55 -0700158 loadModelview(new Matrix4f());
159 loadProjection(new Matrix4f());
160 loadTexture(new Matrix4f());
Jason Sams110195f2009-08-04 18:47:46 -0700161 }
162
163 public void destroy() {
164 mAlloc.destroy();
165 mAlloc = null;
166 }
167
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700168 private void addToBuffer(int offset, Matrix4f m) {
169 mIOBuffer.reset(offset);
170 for(int i = 0; i < 16; i ++) {
171 mIOBuffer.addF32(m.mMat[i]);
172 }
173 mAlloc.data(mIOBuffer.getData());
174 }
175
Jason Sams25430d02010-02-02 15:26:40 -0800176 public void loadModelview(Matrix4f m) {
Jason Sams110195f2009-08-04 18:47:46 -0700177 mModel = m;
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700178 addToBuffer(MODELVIEW_OFFSET*4, m);
Jason Sams110195f2009-08-04 18:47:46 -0700179 }
180
Jason Sams25430d02010-02-02 15:26:40 -0800181 public void loadProjection(Matrix4f m) {
Jason Sams110195f2009-08-04 18:47:46 -0700182 mProjection = m;
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700183 addToBuffer(PROJECTION_OFFSET*4, m);
Jason Sams110195f2009-08-04 18:47:46 -0700184 }
185
Jason Sams25430d02010-02-02 15:26:40 -0800186 public void loadTexture(Matrix4f m) {
Jason Sams110195f2009-08-04 18:47:46 -0700187 mTexture = m;
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700188 addToBuffer(TEXTURE_OFFSET*4, m);
Jason Sams110195f2009-08-04 18:47:46 -0700189 }
190
191 public void setupOrthoWindow(int w, int h) {
192 mProjection.loadOrtho(0,w, h,0, -1,1);
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700193 addToBuffer(PROJECTION_OFFSET*4, mProjection);
Jason Sams110195f2009-08-04 18:47:46 -0700194 }
195
196 public void setupOrthoNormalized(int w, int h) {
197 // range -1,1 in the narrow axis.
198 if(w > h) {
199 float aspect = ((float)w) / h;
200 mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1);
201 } else {
202 float aspect = ((float)h) / w;
203 mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1);
204 }
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700205 addToBuffer(PROJECTION_OFFSET*4, mProjection);
Jason Sams110195f2009-08-04 18:47:46 -0700206 }
207
208 public void setupProjectionNormalized(int w, int h) {
209 // range -1,1 in the narrow axis at z = 0.
Jason Sams25430d02010-02-02 15:26:40 -0800210 Matrix4f m1 = new Matrix4f();
211 Matrix4f m2 = new Matrix4f();
Jason Sams110195f2009-08-04 18:47:46 -0700212
213 if(w > h) {
214 float aspect = ((float)w) / h;
215 m1.loadFrustum(-aspect,aspect, -1,1, 1,100);
216 } else {
217 float aspect = ((float)h) / w;
218 m1.loadFrustum(-1,1, -aspect,aspect, 1,100);
219 }
220
221 m2.loadRotate(180, 0, 1, 0);
222 m1.loadMultiply(m1, m2);
223
224 m2.loadScale(-2, 2, 1);
225 m1.loadMultiply(m1, m2);
226
227 m2.loadTranslate(0, 0, 2);
228 m1.loadMultiply(m1, m2);
229
230 mProjection = m1;
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700231 addToBuffer(PROJECTION_OFFSET*4, mProjection);
Jason Sams110195f2009-08-04 18:47:46 -0700232 }
233
234 }
235
236}
237