blob: 13ddf7502ded38263d0624d2c107d9e0e1a9e140 [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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070020/**
Tim Murraya9084222013-04-05 22:06:43 +000021 * @hide
Jason Sams65c80f82012-05-08 17:30:26 -070022 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080023 * ProgramVertexFixedFunction is a helper class that provides a
24 * simple way to create a fixed function emulation vertex shader
25 * without writing any GLSL code.
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080026 *
27 **/
28public class ProgramVertexFixedFunction extends ProgramVertex {
29
30 ProgramVertexFixedFunction(int id, RenderScript rs) {
31 super(id, rs);
32 }
33
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070034 /**
Jason Sams65c80f82012-05-08 17:30:26 -070035 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080036 * Binds the constant buffer containing fixed function emulation
37 * matrices
38 *
39 * @param va allocation containing fixed function matrices
40 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080041 public void bindConstants(Constants va) {
42 mRS.validate();
43 bindConstants(va.getAllocation(), 0);
44 }
45
46 static class InternalBuilder extends BaseProgramBuilder {
Jason Sams65c80f82012-05-08 17:30:26 -070047 /**
48 * @deprecated in API 16
49 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080050 public InternalBuilder(RenderScript rs) {
51 super(rs);
52 }
53
Jason Sams65c80f82012-05-08 17:30:26 -070054 /**
55 * @deprecated in API 16
56 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080057 public InternalBuilder addInput(Element e) throws IllegalStateException {
58 // Should check for consistant and non-conflicting names...
59 if(mInputCount >= MAX_INPUT) {
60 throw new RSIllegalArgumentException("Max input count exceeded.");
61 }
62 if (e.isComplex()) {
63 throw new RSIllegalArgumentException("Complex elements not allowed.");
64 }
65 mInputs[mInputCount++] = e;
66 return this;
67 }
68
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070069 /**
Jason Sams65c80f82012-05-08 17:30:26 -070070 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080071 * Creates ProgramVertexFixedFunction from the current state of
72 * the builder
73 *
74 * @return ProgramVertexFixedFunction
75 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080076 public ProgramVertexFixedFunction create() {
77 mRS.validate();
78 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080079 String[] texNames = new String[mTextureCount];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080080 int idx = 0;
81
82 for (int i=0; i < mInputCount; i++) {
83 tmp[idx++] = ProgramParam.INPUT.mID;
Jason Samse07694b2012-04-03 15:36:36 -070084 tmp[idx++] = mInputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080085 }
86 for (int i=0; i < mOutputCount; i++) {
87 tmp[idx++] = ProgramParam.OUTPUT.mID;
Jason Samse07694b2012-04-03 15:36:36 -070088 tmp[idx++] = mOutputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080089 }
90 for (int i=0; i < mConstantCount; i++) {
91 tmp[idx++] = ProgramParam.CONSTANT.mID;
Jason Samse07694b2012-04-03 15:36:36 -070092 tmp[idx++] = mConstants[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080093 }
94 for (int i=0; i < mTextureCount; i++) {
95 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
96 tmp[idx++] = mTextureTypes[i].mID;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080097 texNames[i] = mTextureNames[i];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080098 }
99
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800100 int id = mRS.nProgramVertexCreate(mShader, texNames, tmp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800101 ProgramVertexFixedFunction pv = new ProgramVertexFixedFunction(id, mRS);
102 initProgram(pv);
103 return pv;
104 }
105 }
106
Jason Sams65c80f82012-05-08 17:30:26 -0700107 /**
108 * @deprecated in API 16
109 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800110 public static class Builder {
111 boolean mTextureMatrixEnable;
112 String mShader;
113 RenderScript mRS;
114
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700115 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700116 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800117 * Creates a builder for fixed function vertex program
118 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800119 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800120 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800121 public Builder(RenderScript rs) {
122 mRS = rs;
123 }
124
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700125 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700126 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800127 * Specifies whether texture matrix calculations are to be added
128 * to the shader
129 *
130 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800131 public Builder setTextureMatrixEnable(boolean enable) {
132 mTextureMatrixEnable = enable;
133 return this;
134 }
135 static Type getConstantInputType(RenderScript rs) {
136 Element.Builder b = new Element.Builder(rs);
137 b.add(Element.MATRIX4X4(rs), "MV");
138 b.add(Element.MATRIX4X4(rs), "P");
139 b.add(Element.MATRIX4X4(rs), "TexMatrix");
140 b.add(Element.MATRIX4X4(rs), "MVP");
141
142 Type.Builder typeBuilder = new Type.Builder(rs, b.create());
143 typeBuilder.setX(1);
144 return typeBuilder.create();
145 }
146
147 private void buildShaderString() {
148
149 mShader = "//rs_shader_internal\n";
150 mShader += "varying vec4 varColor;\n";
151 mShader += "varying vec2 varTex0;\n";
152
153 mShader += "void main() {\n";
154 mShader += " gl_Position = UNI_MVP * ATTRIB_position;\n";
155 mShader += " gl_PointSize = 1.0;\n";
156
157 mShader += " varColor = ATTRIB_color;\n";
158 if (mTextureMatrixEnable) {
159 mShader += " varTex0 = (UNI_TexMatrix * vec4(ATTRIB_texture0, 0.0, 1.0)).xy;\n";
160 } else {
161 mShader += " varTex0 = ATTRIB_texture0;\n";
162 }
163 mShader += "}\n";
164 }
165
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700166 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700167 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800168 * Creates ProgramVertexFixedFunction from the current state of
169 * the builder
170 *
171 * @return Fixed function emulation ProgramVertex
172 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800173 public ProgramVertexFixedFunction create() {
174 buildShaderString();
175
176 InternalBuilder sb = new InternalBuilder(mRS);
177 sb.setShader(mShader);
178 sb.addConstant(getConstantInputType(mRS));
179
180 Element.Builder b = new Element.Builder(mRS);
181 b.add(Element.F32_4(mRS), "position");
182 b.add(Element.F32_4(mRS), "color");
183 b.add(Element.F32_3(mRS), "normal");
184 b.add(Element.F32_2(mRS), "texture0");
185 sb.addInput(b.create());
186
187 return sb.create();
188 }
189 }
190
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700191 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700192 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800193 * Helper class to store modelview, projection and texture
194 * matrices for ProgramVertexFixedFunction
195 *
196 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800197 public static class Constants {
198 static final int MODELVIEW_OFFSET = 0;
199 static final int PROJECTION_OFFSET = 16;
200 static final int TEXTURE_OFFSET = 32;
201
202 Matrix4f mModel;
203 Matrix4f mProjection;
204 Matrix4f mTexture;
205
206 Allocation mAlloc;
207 Allocation getAllocation() {
208 return mAlloc;
209 }
210 private FieldPacker mIOBuffer;
211
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700212 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700213 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800214 * Creates a buffer to store fixed function emulation matrices
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800215 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800216 * @param rs Context to which the allocation will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800217 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800218 public Constants(RenderScript rs) {
219 Type constInputType = ProgramVertexFixedFunction.Builder.getConstantInputType(rs);
220 mAlloc = Allocation.createTyped(rs, constInputType);
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700221 int bufferSize = constInputType.getElement().getBytesSize()*
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800222 constInputType.getCount();
223 mIOBuffer = new FieldPacker(bufferSize);
224 mModel = new Matrix4f();
225 mProjection = new Matrix4f();
226 mTexture = new Matrix4f();
227 setModelview(new Matrix4f());
228 setProjection(new Matrix4f());
229 setTexture(new Matrix4f());
230 }
231
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700232 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700233 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800234 * Forces deallocation of memory backing the contant matrices.
235 * Normally, this is unnecessary and will be garbage collected
236 *
237 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800238 public void destroy() {
239 mAlloc.destroy();
240 mAlloc = null;
241 }
242
243 private void addToBuffer(int offset, Matrix4f m) {
244 mIOBuffer.reset(offset);
245 for(int i = 0; i < 16; i ++) {
246 mIOBuffer.addF32(m.mMat[i]);
247 }
Jason Samsb97b2512011-01-16 15:04:08 -0800248 mAlloc.setFromFieldPacker(0, mIOBuffer);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800249 }
250
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700251 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700252 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800253 * Sets the modelview matrix in the fixed function matrix buffer
254 *
255 * @param m modelview matrix
256 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800257 public void setModelview(Matrix4f m) {
258 mModel.load(m);
259 addToBuffer(MODELVIEW_OFFSET*4, m);
260 }
261
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700262 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700263 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800264 * Sets the projection matrix in the fixed function matrix buffer
265 *
266 * @param m projection matrix
267 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800268 public void setProjection(Matrix4f m) {
269 mProjection.load(m);
270 addToBuffer(PROJECTION_OFFSET*4, m);
271 }
272
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700273 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700274 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800275 * Sets the texture matrix in the fixed function matrix buffer.
276 * Texture matrix must be enabled in the
277 * ProgramVertexFixedFunction builder for the shader to utilize
278 * it.
279 *
280 * @param m modelview matrix
281 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800282 public void setTexture(Matrix4f m) {
283 mTexture.load(m);
284 addToBuffer(TEXTURE_OFFSET*4, m);
285 }
286 }
287}