blob: 2fe68be703a588c6258c5021c2f0b31f042dad4b [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
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080020import android.util.Log;
21
22
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070023/**
Tim Murraya9084222013-04-05 22:06:43 +000024 * @hide
Jason Sams65c80f82012-05-08 17:30:26 -070025 * @deprecated in API 16
Robert Ly11518ac2011-02-09 13:57:06 -080026 * <p>ProgramFragmentFixedFunction is a helper class that provides
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080027 * a way to make a simple fragment shader without writing any
Robert Ly11518ac2011-02-09 13:57:06 -080028 * GLSL code. This class allows for display of constant color, interpolated
29 * color from the vertex shader, or combinations of the both
30 * blended with results of up to two texture lookups.</p
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080031 *
32 **/
33public class ProgramFragmentFixedFunction extends ProgramFragment {
Tim Murray7a629fa2013-11-19 12:45:54 -080034 ProgramFragmentFixedFunction(long id, RenderScript rs) {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080035 super(id, rs);
36 }
37
38 static class InternalBuilder extends BaseProgramBuilder {
Jason Sams65c80f82012-05-08 17:30:26 -070039 /**
40 * @deprecated in API 16
41 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080042 public InternalBuilder(RenderScript rs) {
43 super(rs);
44 }
45
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070046 /**
Jason Sams65c80f82012-05-08 17:30:26 -070047 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080048 * Creates ProgramFragmentFixedFunction from the current state
49 * of the builder
50 *
51 * @return ProgramFragmentFixedFunction
52 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080053 public ProgramFragmentFixedFunction create() {
54 mRS.validate();
Ashok Bhat98071552014-02-12 09:54:43 +000055 long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080056 String[] texNames = new String[mTextureCount];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080057 int idx = 0;
58
59 for (int i=0; i < mInputCount; i++) {
60 tmp[idx++] = ProgramParam.INPUT.mID;
Ashok Bhat98071552014-02-12 09:54:43 +000061 tmp[idx++] = mInputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080062 }
63 for (int i=0; i < mOutputCount; i++) {
64 tmp[idx++] = ProgramParam.OUTPUT.mID;
Ashok Bhat98071552014-02-12 09:54:43 +000065 tmp[idx++] = mOutputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080066 }
67 for (int i=0; i < mConstantCount; i++) {
68 tmp[idx++] = ProgramParam.CONSTANT.mID;
Ashok Bhat98071552014-02-12 09:54:43 +000069 tmp[idx++] = mConstants[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080070 }
71 for (int i=0; i < mTextureCount; i++) {
72 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
Ashok Bhat98071552014-02-12 09:54:43 +000073 tmp[idx++] = mTextureTypes[i].mID;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080074 texNames[i] = mTextureNames[i];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080075 }
76
Tim Murray7a629fa2013-11-19 12:45:54 -080077 long id = mRS.nProgramFragmentCreate(mShader, texNames, tmp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080078 ProgramFragmentFixedFunction pf = new ProgramFragmentFixedFunction(id, mRS);
79 initProgram(pf);
80 return pf;
81 }
82 }
83
Jason Sams65c80f82012-05-08 17:30:26 -070084 /**
85 * @deprecated in API 16
86 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080087 public static class Builder {
Jason Sams65c80f82012-05-08 17:30:26 -070088 /**
89 * @deprecated in API 16
90 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080091 public static final int MAX_TEXTURE = 2;
92 int mNumTextures;
93 boolean mPointSpriteEnable;
94 boolean mVaryingColorEnable;
95 String mShader;
96 RenderScript mRS;
97
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070098 /**
Jason Sams65c80f82012-05-08 17:30:26 -070099 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800100 * EnvMode describes how textures are combined with the existing
101 * color in the fixed function fragment shader
102 *
103 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800104 public enum EnvMode {
Jason Sams65c80f82012-05-08 17:30:26 -0700105 /**
106 * @deprecated in API 16
107 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800108 REPLACE (1),
Jason Sams65c80f82012-05-08 17:30:26 -0700109 /**
110 * @deprecated in API 16
111 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800112 MODULATE (2),
Jason Sams65c80f82012-05-08 17:30:26 -0700113 /**
114 * @deprecated in API 16
115 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800116 DECAL (3);
117
118 int mID;
119 EnvMode(int id) {
120 mID = id;
121 }
122 }
123
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700124 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700125 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800126 * Format describes the pixel format of textures in the fixed
127 * function fragment shader and how they are sampled
128 *
129 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800130 public enum Format {
Jason Sams65c80f82012-05-08 17:30:26 -0700131 /**
132 * @deprecated in API 16
133 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800134 ALPHA (1),
Jason Sams65c80f82012-05-08 17:30:26 -0700135 /**
136 * @deprecated in API 16
137 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800138 LUMINANCE_ALPHA (2),
Jason Sams65c80f82012-05-08 17:30:26 -0700139 /**
140 * @deprecated in API 16
141 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800142 RGB (3),
Jason Sams65c80f82012-05-08 17:30:26 -0700143 /**
144 * @deprecated in API 16
145 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800146 RGBA (4);
147
148 int mID;
149 Format(int id) {
150 mID = id;
151 }
152 }
153
154 private class Slot {
155 EnvMode env;
156 Format format;
157 Slot(EnvMode _env, Format _fmt) {
158 env = _env;
159 format = _fmt;
160 }
161 }
162 Slot[] mSlots;
163
164 private void buildShaderString() {
165 mShader = "//rs_shader_internal\n";
166 mShader += "varying lowp vec4 varColor;\n";
167 mShader += "varying vec2 varTex0;\n";
168
169 mShader += "void main() {\n";
170 if (mVaryingColorEnable) {
171 mShader += " lowp vec4 col = varColor;\n";
172 } else {
173 mShader += " lowp vec4 col = UNI_Color;\n";
174 }
175
176 if (mNumTextures != 0) {
177 if (mPointSpriteEnable) {
178 mShader += " vec2 t0 = gl_PointCoord;\n";
179 } else {
180 mShader += " vec2 t0 = varTex0.xy;\n";
181 }
182 }
183
184 for(int i = 0; i < mNumTextures; i ++) {
185 switch(mSlots[i].env) {
186 case REPLACE:
187 switch (mSlots[i].format) {
188 case ALPHA:
189 mShader += " col.a = texture2D(UNI_Tex0, t0).a;\n";
190 break;
191 case LUMINANCE_ALPHA:
192 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
193 break;
194 case RGB:
195 mShader += " col.rgb = texture2D(UNI_Tex0, t0).rgb;\n";
196 break;
197 case RGBA:
198 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
199 break;
200 }
201 break;
202 case MODULATE:
203 switch (mSlots[i].format) {
204 case ALPHA:
205 mShader += " col.a *= texture2D(UNI_Tex0, t0).a;\n";
206 break;
207 case LUMINANCE_ALPHA:
208 mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
209 break;
210 case RGB:
211 mShader += " col.rgb *= texture2D(UNI_Tex0, t0).rgb;\n";
212 break;
213 case RGBA:
214 mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
215 break;
216 }
217 break;
218 case DECAL:
219 mShader += " col = texture2D(UNI_Tex0, t0);\n";
220 break;
221 }
222 }
223
224 mShader += " gl_FragColor = col;\n";
225 mShader += "}\n";
226 }
227
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700228 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700229 * @deprecated
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800230 * Creates a builder for fixed function fragment program
231 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800232 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800233 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800234 public Builder(RenderScript rs) {
235 mRS = rs;
236 mSlots = new Slot[MAX_TEXTURE];
237 mPointSpriteEnable = false;
238 }
239
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700240 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700241 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800242 * Adds a texture to be fetched as part of the fixed function
243 * fragment program
244 *
245 * @param env specifies how the texture is combined with the
246 * current color
247 * @param fmt specifies the format of the texture and how its
248 * components will be used to combine with the
249 * current color
250 * @param slot index of the texture to apply the operations on
251 *
252 * @return this
253 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800254 public Builder setTexture(EnvMode env, Format fmt, int slot)
255 throws IllegalArgumentException {
256 if((slot < 0) || (slot >= MAX_TEXTURE)) {
257 throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
258 }
259 mSlots[slot] = new Slot(env, fmt);
260 return this;
261 }
262
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700263 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700264 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800265 * Specifies whether the texture coordinate passed from the
266 * vertex program is replaced with an openGL internal point
267 * sprite texture coordinate
268 *
269 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800270 public Builder setPointSpriteTexCoordinateReplacement(boolean enable) {
271 mPointSpriteEnable = enable;
272 return this;
273 }
274
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700275 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700276 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800277 * Specifies whether the varying color passed from the vertex
278 * program or the constant color set on the fragment program is
279 * used in the final color calculation in the fixed function
280 * fragment shader
281 *
282 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800283 public Builder setVaryingColor(boolean enable) {
284 mVaryingColorEnable = enable;
285 return this;
286 }
287
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700288 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700289 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800290 * Creates the fixed function fragment program from the current
291 * state of the builder.
292 *
293 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800294 public ProgramFragmentFixedFunction create() {
295 InternalBuilder sb = new InternalBuilder(mRS);
296 mNumTextures = 0;
297 for(int i = 0; i < MAX_TEXTURE; i ++) {
298 if(mSlots[i] != null) {
299 mNumTextures ++;
300 }
301 }
302 buildShaderString();
303 sb.setShader(mShader);
304
305 Type constType = null;
306 if (!mVaryingColorEnable) {
307 Element.Builder b = new Element.Builder(mRS);
308 b.add(Element.F32_4(mRS), "Color");
309 Type.Builder typeBuilder = new Type.Builder(mRS, b.create());
310 typeBuilder.setX(1);
311 constType = typeBuilder.create();
312 sb.addConstant(constType);
313 }
314 for (int i = 0; i < mNumTextures; i ++) {
315 sb.addTexture(TextureType.TEXTURE_2D);
316 }
317
318 ProgramFragmentFixedFunction pf = sb.create();
319 pf.mTextureCount = MAX_TEXTURE;
320 if (!mVaryingColorEnable) {
321 Allocation constantData = Allocation.createTyped(mRS,constType);
Jason Samsb97b2512011-01-16 15:04:08 -0800322 FieldPacker fp = new FieldPacker(16);
323 Float4 f4 = new Float4(1.f, 1.f, 1.f, 1.f);
324 fp.addF32(f4);
325 constantData.setFromFieldPacker(0, fp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800326 pf.bindConstants(constantData, 0);
327 }
328 return pf;
329 }
330 }
331}
332
333
334
335