blob: 88cade4db64e6560ed232010213c033213ad3c30 [file] [log] [blame]
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -08001/*
Jason Sams65c80f82012-05-08 17:30:26 -07002 * Copyright (C) 2008-2012 The Android Open Source Project
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -08003 *
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
20import android.graphics.Matrix;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080021import android.util.Log;
22
23
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070024/**
Jason Sams65c80f82012-05-08 17:30:26 -070025 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080026 * ProgramVertexFixedFunction is a helper class that provides a
27 * simple way to create a fixed function emulation vertex shader
28 * without writing any GLSL code.
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080029 *
30 **/
31public class ProgramVertexFixedFunction extends ProgramVertex {
32
33 ProgramVertexFixedFunction(int id, RenderScript rs) {
34 super(id, rs);
35 }
36
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070037 /**
Jason Sams65c80f82012-05-08 17:30:26 -070038 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080039 * Binds the constant buffer containing fixed function emulation
40 * matrices
41 *
42 * @param va allocation containing fixed function matrices
43 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080044 public void bindConstants(Constants va) {
45 mRS.validate();
46 bindConstants(va.getAllocation(), 0);
47 }
48
49 static class InternalBuilder extends BaseProgramBuilder {
Jason Sams65c80f82012-05-08 17:30:26 -070050 /**
51 * @deprecated in API 16
52 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080053 public InternalBuilder(RenderScript rs) {
54 super(rs);
55 }
56
Jason Sams65c80f82012-05-08 17:30:26 -070057 /**
58 * @deprecated in API 16
59 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080060 public InternalBuilder addInput(Element e) throws IllegalStateException {
61 // Should check for consistant and non-conflicting names...
62 if(mInputCount >= MAX_INPUT) {
63 throw new RSIllegalArgumentException("Max input count exceeded.");
64 }
65 if (e.isComplex()) {
66 throw new RSIllegalArgumentException("Complex elements not allowed.");
67 }
68 mInputs[mInputCount++] = e;
69 return this;
70 }
71
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070072 /**
Jason Sams65c80f82012-05-08 17:30:26 -070073 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080074 * Creates ProgramVertexFixedFunction from the current state of
75 * the builder
76 *
77 * @return ProgramVertexFixedFunction
78 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080079 public ProgramVertexFixedFunction create() {
80 mRS.validate();
81 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080082 String[] texNames = new String[mTextureCount];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080083 int idx = 0;
84
85 for (int i=0; i < mInputCount; i++) {
86 tmp[idx++] = ProgramParam.INPUT.mID;
Jason Samse07694b2012-04-03 15:36:36 -070087 tmp[idx++] = mInputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080088 }
89 for (int i=0; i < mOutputCount; i++) {
90 tmp[idx++] = ProgramParam.OUTPUT.mID;
Jason Samse07694b2012-04-03 15:36:36 -070091 tmp[idx++] = mOutputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080092 }
93 for (int i=0; i < mConstantCount; i++) {
94 tmp[idx++] = ProgramParam.CONSTANT.mID;
Jason Samse07694b2012-04-03 15:36:36 -070095 tmp[idx++] = mConstants[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080096 }
97 for (int i=0; i < mTextureCount; i++) {
98 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
99 tmp[idx++] = mTextureTypes[i].mID;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800100 texNames[i] = mTextureNames[i];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800101 }
102
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800103 int id = mRS.nProgramVertexCreate(mShader, texNames, tmp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800104 ProgramVertexFixedFunction pv = new ProgramVertexFixedFunction(id, mRS);
105 initProgram(pv);
106 return pv;
107 }
108 }
109
Jason Sams65c80f82012-05-08 17:30:26 -0700110 /**
111 * @deprecated in API 16
112 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800113 public static class Builder {
114 boolean mTextureMatrixEnable;
115 String mShader;
116 RenderScript mRS;
117
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700118 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700119 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800120 * Creates a builder for fixed function vertex program
121 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800122 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800123 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800124 public Builder(RenderScript rs) {
125 mRS = rs;
126 }
127
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700128 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700129 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800130 * Specifies whether texture matrix calculations are to be added
131 * to the shader
132 *
133 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800134 public Builder setTextureMatrixEnable(boolean enable) {
135 mTextureMatrixEnable = enable;
136 return this;
137 }
138 static Type getConstantInputType(RenderScript rs) {
139 Element.Builder b = new Element.Builder(rs);
140 b.add(Element.MATRIX4X4(rs), "MV");
141 b.add(Element.MATRIX4X4(rs), "P");
142 b.add(Element.MATRIX4X4(rs), "TexMatrix");
143 b.add(Element.MATRIX4X4(rs), "MVP");
144
145 Type.Builder typeBuilder = new Type.Builder(rs, b.create());
146 typeBuilder.setX(1);
147 return typeBuilder.create();
148 }
149
150 private void buildShaderString() {
151
152 mShader = "//rs_shader_internal\n";
153 mShader += "varying vec4 varColor;\n";
154 mShader += "varying vec2 varTex0;\n";
155
156 mShader += "void main() {\n";
157 mShader += " gl_Position = UNI_MVP * ATTRIB_position;\n";
158 mShader += " gl_PointSize = 1.0;\n";
159
160 mShader += " varColor = ATTRIB_color;\n";
161 if (mTextureMatrixEnable) {
162 mShader += " varTex0 = (UNI_TexMatrix * vec4(ATTRIB_texture0, 0.0, 1.0)).xy;\n";
163 } else {
164 mShader += " varTex0 = ATTRIB_texture0;\n";
165 }
166 mShader += "}\n";
167 }
168
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700169 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700170 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800171 * Creates ProgramVertexFixedFunction from the current state of
172 * the builder
173 *
174 * @return Fixed function emulation ProgramVertex
175 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800176 public ProgramVertexFixedFunction create() {
177 buildShaderString();
178
179 InternalBuilder sb = new InternalBuilder(mRS);
180 sb.setShader(mShader);
181 sb.addConstant(getConstantInputType(mRS));
182
183 Element.Builder b = new Element.Builder(mRS);
184 b.add(Element.F32_4(mRS), "position");
185 b.add(Element.F32_4(mRS), "color");
186 b.add(Element.F32_3(mRS), "normal");
187 b.add(Element.F32_2(mRS), "texture0");
188 sb.addInput(b.create());
189
190 return sb.create();
191 }
192 }
193
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700194 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700195 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800196 * Helper class to store modelview, projection and texture
197 * matrices for ProgramVertexFixedFunction
198 *
199 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800200 public static class Constants {
201 static final int MODELVIEW_OFFSET = 0;
202 static final int PROJECTION_OFFSET = 16;
203 static final int TEXTURE_OFFSET = 32;
204
205 Matrix4f mModel;
206 Matrix4f mProjection;
207 Matrix4f mTexture;
208
209 Allocation mAlloc;
210 Allocation getAllocation() {
211 return mAlloc;
212 }
213 private FieldPacker mIOBuffer;
214
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700215 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700216 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800217 * Creates a buffer to store fixed function emulation matrices
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800218 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800219 * @param rs Context to which the allocation will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800220 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800221 public Constants(RenderScript rs) {
222 Type constInputType = ProgramVertexFixedFunction.Builder.getConstantInputType(rs);
223 mAlloc = Allocation.createTyped(rs, constInputType);
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700224 int bufferSize = constInputType.getElement().getBytesSize()*
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800225 constInputType.getCount();
226 mIOBuffer = new FieldPacker(bufferSize);
227 mModel = new Matrix4f();
228 mProjection = new Matrix4f();
229 mTexture = new Matrix4f();
230 setModelview(new Matrix4f());
231 setProjection(new Matrix4f());
232 setTexture(new Matrix4f());
233 }
234
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700235 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700236 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800237 * Forces deallocation of memory backing the contant matrices.
238 * Normally, this is unnecessary and will be garbage collected
239 *
240 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800241 public void destroy() {
242 mAlloc.destroy();
243 mAlloc = null;
244 }
245
246 private void addToBuffer(int offset, Matrix4f m) {
247 mIOBuffer.reset(offset);
248 for(int i = 0; i < 16; i ++) {
249 mIOBuffer.addF32(m.mMat[i]);
250 }
Jason Samsb97b2512011-01-16 15:04:08 -0800251 mAlloc.setFromFieldPacker(0, mIOBuffer);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800252 }
253
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700254 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700255 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800256 * Sets the modelview matrix in the fixed function matrix buffer
257 *
258 * @param m modelview matrix
259 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800260 public void setModelview(Matrix4f m) {
261 mModel.load(m);
262 addToBuffer(MODELVIEW_OFFSET*4, m);
263 }
264
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700265 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700266 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800267 * Sets the projection matrix in the fixed function matrix buffer
268 *
269 * @param m projection matrix
270 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800271 public void setProjection(Matrix4f m) {
272 mProjection.load(m);
273 addToBuffer(PROJECTION_OFFSET*4, m);
274 }
275
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700276 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700277 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800278 * Sets the texture matrix in the fixed function matrix buffer.
279 * Texture matrix must be enabled in the
280 * ProgramVertexFixedFunction builder for the shader to utilize
281 * it.
282 *
283 * @param m modelview matrix
284 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800285 public void setTexture(Matrix4f m) {
286 mTexture.load(m);
287 addToBuffer(TEXTURE_OFFSET*4, m);
288 }
289 }
290}