blob: a350154c456c37445060bb9bf76992eaf4a1d487 [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/**
Tim Murraya9084222013-04-05 22:06:43 +000025 * @hide
Jason Sams65c80f82012-05-08 17:30:26 -070026 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080027 * ProgramVertexFixedFunction is a helper class that provides a
28 * simple way to create a fixed function emulation vertex shader
29 * without writing any GLSL code.
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080030 *
31 **/
32public class ProgramVertexFixedFunction extends ProgramVertex {
33
Tim Murray7a629fa2013-11-19 12:45:54 -080034 ProgramVertexFixedFunction(long id, RenderScript rs) {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080035 super(id, rs);
36 }
37
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070038 /**
Jason Sams65c80f82012-05-08 17:30:26 -070039 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080040 * Binds the constant buffer containing fixed function emulation
41 * matrices
42 *
43 * @param va allocation containing fixed function matrices
44 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080045 public void bindConstants(Constants va) {
46 mRS.validate();
47 bindConstants(va.getAllocation(), 0);
48 }
49
50 static class InternalBuilder extends BaseProgramBuilder {
Jason Sams65c80f82012-05-08 17:30:26 -070051 /**
52 * @deprecated in API 16
53 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080054 public InternalBuilder(RenderScript rs) {
55 super(rs);
56 }
57
Jason Sams65c80f82012-05-08 17:30:26 -070058 /**
59 * @deprecated in API 16
60 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080061 public InternalBuilder addInput(Element e) throws IllegalStateException {
62 // Should check for consistant and non-conflicting names...
63 if(mInputCount >= MAX_INPUT) {
64 throw new RSIllegalArgumentException("Max input count exceeded.");
65 }
66 if (e.isComplex()) {
67 throw new RSIllegalArgumentException("Complex elements not allowed.");
68 }
69 mInputs[mInputCount++] = e;
70 return this;
71 }
72
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070073 /**
Jason Sams65c80f82012-05-08 17:30:26 -070074 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080075 * Creates ProgramVertexFixedFunction from the current state of
76 * the builder
77 *
78 * @return ProgramVertexFixedFunction
79 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080080 public ProgramVertexFixedFunction create() {
81 mRS.validate();
Ashok Bhat98071552014-02-12 09:54:43 +000082 long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080083 String[] texNames = new String[mTextureCount];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080084 int idx = 0;
85
86 for (int i=0; i < mInputCount; i++) {
87 tmp[idx++] = ProgramParam.INPUT.mID;
Ashok Bhat98071552014-02-12 09:54:43 +000088 tmp[idx++] = mInputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080089 }
90 for (int i=0; i < mOutputCount; i++) {
91 tmp[idx++] = ProgramParam.OUTPUT.mID;
Ashok Bhat98071552014-02-12 09:54:43 +000092 tmp[idx++] = mOutputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080093 }
94 for (int i=0; i < mConstantCount; i++) {
95 tmp[idx++] = ProgramParam.CONSTANT.mID;
Ashok Bhat98071552014-02-12 09:54:43 +000096 tmp[idx++] = mConstants[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080097 }
98 for (int i=0; i < mTextureCount; i++) {
99 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
Ashok Bhat98071552014-02-12 09:54:43 +0000100 tmp[idx++] = mTextureTypes[i].mID;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800101 texNames[i] = mTextureNames[i];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800102 }
103
Tim Murray7a629fa2013-11-19 12:45:54 -0800104 long id = mRS.nProgramVertexCreate(mShader, texNames, tmp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800105 ProgramVertexFixedFunction pv = new ProgramVertexFixedFunction(id, mRS);
106 initProgram(pv);
107 return pv;
108 }
109 }
110
Jason Sams65c80f82012-05-08 17:30:26 -0700111 /**
112 * @deprecated in API 16
113 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800114 public static class Builder {
115 boolean mTextureMatrixEnable;
116 String mShader;
117 RenderScript mRS;
118
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700119 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700120 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800121 * Creates a builder for fixed function vertex program
122 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800123 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800124 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800125 public Builder(RenderScript rs) {
126 mRS = rs;
127 }
128
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700129 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700130 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800131 * Specifies whether texture matrix calculations are to be added
132 * to the shader
133 *
134 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800135 public Builder setTextureMatrixEnable(boolean enable) {
136 mTextureMatrixEnable = enable;
137 return this;
138 }
139 static Type getConstantInputType(RenderScript rs) {
140 Element.Builder b = new Element.Builder(rs);
141 b.add(Element.MATRIX4X4(rs), "MV");
142 b.add(Element.MATRIX4X4(rs), "P");
143 b.add(Element.MATRIX4X4(rs), "TexMatrix");
144 b.add(Element.MATRIX4X4(rs), "MVP");
145
146 Type.Builder typeBuilder = new Type.Builder(rs, b.create());
147 typeBuilder.setX(1);
148 return typeBuilder.create();
149 }
150
151 private void buildShaderString() {
152
153 mShader = "//rs_shader_internal\n";
154 mShader += "varying vec4 varColor;\n";
155 mShader += "varying vec2 varTex0;\n";
156
157 mShader += "void main() {\n";
158 mShader += " gl_Position = UNI_MVP * ATTRIB_position;\n";
159 mShader += " gl_PointSize = 1.0;\n";
160
161 mShader += " varColor = ATTRIB_color;\n";
162 if (mTextureMatrixEnable) {
163 mShader += " varTex0 = (UNI_TexMatrix * vec4(ATTRIB_texture0, 0.0, 1.0)).xy;\n";
164 } else {
165 mShader += " varTex0 = ATTRIB_texture0;\n";
166 }
167 mShader += "}\n";
168 }
169
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700170 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700171 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800172 * Creates ProgramVertexFixedFunction from the current state of
173 * the builder
174 *
175 * @return Fixed function emulation ProgramVertex
176 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800177 public ProgramVertexFixedFunction create() {
178 buildShaderString();
179
180 InternalBuilder sb = new InternalBuilder(mRS);
181 sb.setShader(mShader);
182 sb.addConstant(getConstantInputType(mRS));
183
184 Element.Builder b = new Element.Builder(mRS);
185 b.add(Element.F32_4(mRS), "position");
186 b.add(Element.F32_4(mRS), "color");
187 b.add(Element.F32_3(mRS), "normal");
188 b.add(Element.F32_2(mRS), "texture0");
189 sb.addInput(b.create());
190
191 return sb.create();
192 }
193 }
194
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700195 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700196 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800197 * Helper class to store modelview, projection and texture
198 * matrices for ProgramVertexFixedFunction
199 *
200 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800201 public static class Constants {
202 static final int MODELVIEW_OFFSET = 0;
203 static final int PROJECTION_OFFSET = 16;
204 static final int TEXTURE_OFFSET = 32;
205
206 Matrix4f mModel;
207 Matrix4f mProjection;
208 Matrix4f mTexture;
209
210 Allocation mAlloc;
211 Allocation getAllocation() {
212 return mAlloc;
213 }
214 private FieldPacker mIOBuffer;
215
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700216 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700217 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800218 * Creates a buffer to store fixed function emulation matrices
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800219 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800220 * @param rs Context to which the allocation will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800221 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800222 public Constants(RenderScript rs) {
223 Type constInputType = ProgramVertexFixedFunction.Builder.getConstantInputType(rs);
224 mAlloc = Allocation.createTyped(rs, constInputType);
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700225 int bufferSize = constInputType.getElement().getBytesSize()*
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800226 constInputType.getCount();
227 mIOBuffer = new FieldPacker(bufferSize);
228 mModel = new Matrix4f();
229 mProjection = new Matrix4f();
230 mTexture = new Matrix4f();
231 setModelview(new Matrix4f());
232 setProjection(new Matrix4f());
233 setTexture(new Matrix4f());
234 }
235
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700236 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700237 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800238 * Forces deallocation of memory backing the contant matrices.
239 * Normally, this is unnecessary and will be garbage collected
240 *
241 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800242 public void destroy() {
243 mAlloc.destroy();
244 mAlloc = null;
245 }
246
247 private void addToBuffer(int offset, Matrix4f m) {
248 mIOBuffer.reset(offset);
249 for(int i = 0; i < 16; i ++) {
250 mIOBuffer.addF32(m.mMat[i]);
251 }
Jason Samsb97b2512011-01-16 15:04:08 -0800252 mAlloc.setFromFieldPacker(0, mIOBuffer);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800253 }
254
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700255 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700256 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800257 * Sets the modelview matrix in the fixed function matrix buffer
258 *
259 * @param m modelview matrix
260 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800261 public void setModelview(Matrix4f m) {
262 mModel.load(m);
263 addToBuffer(MODELVIEW_OFFSET*4, m);
264 }
265
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700266 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700267 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800268 * Sets the projection matrix in the fixed function matrix buffer
269 *
270 * @param m projection matrix
271 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800272 public void setProjection(Matrix4f m) {
273 mProjection.load(m);
274 addToBuffer(PROJECTION_OFFSET*4, m);
275 }
276
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700277 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700278 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800279 * Sets the texture matrix in the fixed function matrix buffer.
280 * Texture matrix must be enabled in the
281 * ProgramVertexFixedFunction builder for the shader to utilize
282 * it.
283 *
284 * @param m modelview matrix
285 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800286 public void setTexture(Matrix4f m) {
287 mTexture.load(m);
288 addToBuffer(TEXTURE_OFFSET*4, m);
289 }
290 }
291}