blob: e1c35c57d1c7e9b05008b264291e1346c0e0de8c [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
Robert Ly11518ac2011-02-09 13:57:06 -080023 * <p>ProgramFragmentFixedFunction is a helper class that provides
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080024 * a way to make a simple fragment shader without writing any
Robert Ly11518ac2011-02-09 13:57:06 -080025 * GLSL code. This class allows for display of constant color, interpolated
26 * color from the vertex shader, or combinations of the both
27 * blended with results of up to two texture lookups.</p
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080028 *
29 **/
30public class ProgramFragmentFixedFunction extends ProgramFragment {
Tim Murray460a0492013-11-19 12:45:54 -080031 ProgramFragmentFixedFunction(long id, RenderScript rs) {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080032 super(id, rs);
33 }
34
35 static class InternalBuilder extends BaseProgramBuilder {
Jason Sams65c80f82012-05-08 17:30:26 -070036 /**
37 * @deprecated in API 16
38 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080039 public InternalBuilder(RenderScript rs) {
40 super(rs);
41 }
42
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070043 /**
Jason Sams65c80f82012-05-08 17:30:26 -070044 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080045 * Creates ProgramFragmentFixedFunction from the current state
46 * of the builder
47 *
48 * @return ProgramFragmentFixedFunction
49 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080050 public ProgramFragmentFixedFunction create() {
51 mRS.validate();
52 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080053 String[] texNames = new String[mTextureCount];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080054 int idx = 0;
55
56 for (int i=0; i < mInputCount; i++) {
57 tmp[idx++] = ProgramParam.INPUT.mID;
Tim Murray460a0492013-11-19 12:45:54 -080058 tmp[idx++] = (int)mInputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080059 }
60 for (int i=0; i < mOutputCount; i++) {
61 tmp[idx++] = ProgramParam.OUTPUT.mID;
Tim Murray460a0492013-11-19 12:45:54 -080062 tmp[idx++] = (int)mOutputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080063 }
64 for (int i=0; i < mConstantCount; i++) {
65 tmp[idx++] = ProgramParam.CONSTANT.mID;
Tim Murray460a0492013-11-19 12:45:54 -080066 tmp[idx++] = (int)mConstants[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080067 }
68 for (int i=0; i < mTextureCount; i++) {
69 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
Tim Murray460a0492013-11-19 12:45:54 -080070 tmp[idx++] = (int)mTextureTypes[i].mID;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080071 texNames[i] = mTextureNames[i];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080072 }
73
Tim Murray460a0492013-11-19 12:45:54 -080074 long id = mRS.nProgramFragmentCreate(mShader, texNames, tmp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080075 ProgramFragmentFixedFunction pf = new ProgramFragmentFixedFunction(id, mRS);
76 initProgram(pf);
77 return pf;
78 }
79 }
80
Jason Sams65c80f82012-05-08 17:30:26 -070081 /**
82 * @deprecated in API 16
83 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080084 public static class Builder {
Jason Sams65c80f82012-05-08 17:30:26 -070085 /**
86 * @deprecated in API 16
87 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080088 public static final int MAX_TEXTURE = 2;
89 int mNumTextures;
90 boolean mPointSpriteEnable;
91 boolean mVaryingColorEnable;
92 String mShader;
93 RenderScript mRS;
94
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070095 /**
Jason Sams65c80f82012-05-08 17:30:26 -070096 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080097 * EnvMode describes how textures are combined with the existing
98 * color in the fixed function fragment shader
99 *
100 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800101 public enum EnvMode {
Jason Sams65c80f82012-05-08 17:30:26 -0700102 /**
103 * @deprecated in API 16
104 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800105 REPLACE (1),
Jason Sams65c80f82012-05-08 17:30:26 -0700106 /**
107 * @deprecated in API 16
108 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800109 MODULATE (2),
Jason Sams65c80f82012-05-08 17:30:26 -0700110 /**
111 * @deprecated in API 16
112 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800113 DECAL (3);
114
115 int mID;
116 EnvMode(int id) {
117 mID = id;
118 }
119 }
120
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700121 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700122 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800123 * Format describes the pixel format of textures in the fixed
124 * function fragment shader and how they are sampled
125 *
126 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800127 public enum Format {
Jason Sams65c80f82012-05-08 17:30:26 -0700128 /**
129 * @deprecated in API 16
130 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800131 ALPHA (1),
Jason Sams65c80f82012-05-08 17:30:26 -0700132 /**
133 * @deprecated in API 16
134 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800135 LUMINANCE_ALPHA (2),
Jason Sams65c80f82012-05-08 17:30:26 -0700136 /**
137 * @deprecated in API 16
138 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800139 RGB (3),
Jason Sams65c80f82012-05-08 17:30:26 -0700140 /**
141 * @deprecated in API 16
142 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800143 RGBA (4);
144
145 int mID;
146 Format(int id) {
147 mID = id;
148 }
149 }
150
151 private class Slot {
152 EnvMode env;
153 Format format;
154 Slot(EnvMode _env, Format _fmt) {
155 env = _env;
156 format = _fmt;
157 }
158 }
159 Slot[] mSlots;
160
161 private void buildShaderString() {
162 mShader = "//rs_shader_internal\n";
163 mShader += "varying lowp vec4 varColor;\n";
164 mShader += "varying vec2 varTex0;\n";
165
166 mShader += "void main() {\n";
167 if (mVaryingColorEnable) {
168 mShader += " lowp vec4 col = varColor;\n";
169 } else {
170 mShader += " lowp vec4 col = UNI_Color;\n";
171 }
172
173 if (mNumTextures != 0) {
174 if (mPointSpriteEnable) {
175 mShader += " vec2 t0 = gl_PointCoord;\n";
176 } else {
177 mShader += " vec2 t0 = varTex0.xy;\n";
178 }
179 }
180
181 for(int i = 0; i < mNumTextures; i ++) {
182 switch(mSlots[i].env) {
183 case REPLACE:
184 switch (mSlots[i].format) {
185 case ALPHA:
186 mShader += " col.a = texture2D(UNI_Tex0, t0).a;\n";
187 break;
188 case LUMINANCE_ALPHA:
189 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
190 break;
191 case RGB:
192 mShader += " col.rgb = texture2D(UNI_Tex0, t0).rgb;\n";
193 break;
194 case RGBA:
195 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
196 break;
197 }
198 break;
199 case MODULATE:
200 switch (mSlots[i].format) {
201 case ALPHA:
202 mShader += " col.a *= texture2D(UNI_Tex0, t0).a;\n";
203 break;
204 case LUMINANCE_ALPHA:
205 mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
206 break;
207 case RGB:
208 mShader += " col.rgb *= texture2D(UNI_Tex0, t0).rgb;\n";
209 break;
210 case RGBA:
211 mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
212 break;
213 }
214 break;
215 case DECAL:
216 mShader += " col = texture2D(UNI_Tex0, t0);\n";
217 break;
218 }
219 }
220
221 mShader += " gl_FragColor = col;\n";
222 mShader += "}\n";
223 }
224
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700225 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700226 * @deprecated
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800227 * Creates a builder for fixed function fragment program
228 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800229 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800230 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800231 public Builder(RenderScript rs) {
232 mRS = rs;
233 mSlots = new Slot[MAX_TEXTURE];
234 mPointSpriteEnable = false;
235 }
236
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700237 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700238 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800239 * Adds a texture to be fetched as part of the fixed function
240 * fragment program
241 *
242 * @param env specifies how the texture is combined with the
243 * current color
244 * @param fmt specifies the format of the texture and how its
245 * components will be used to combine with the
246 * current color
247 * @param slot index of the texture to apply the operations on
248 *
249 * @return this
250 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800251 public Builder setTexture(EnvMode env, Format fmt, int slot)
252 throws IllegalArgumentException {
253 if((slot < 0) || (slot >= MAX_TEXTURE)) {
254 throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
255 }
256 mSlots[slot] = new Slot(env, fmt);
257 return this;
258 }
259
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700260 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700261 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800262 * Specifies whether the texture coordinate passed from the
263 * vertex program is replaced with an openGL internal point
264 * sprite texture coordinate
265 *
266 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800267 public Builder setPointSpriteTexCoordinateReplacement(boolean enable) {
268 mPointSpriteEnable = enable;
269 return this;
270 }
271
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700272 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700273 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800274 * Specifies whether the varying color passed from the vertex
275 * program or the constant color set on the fragment program is
276 * used in the final color calculation in the fixed function
277 * fragment shader
278 *
279 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800280 public Builder setVaryingColor(boolean enable) {
281 mVaryingColorEnable = enable;
282 return this;
283 }
284
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700285 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700286 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800287 * Creates the fixed function fragment program from the current
288 * state of the builder.
289 *
290 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800291 public ProgramFragmentFixedFunction create() {
292 InternalBuilder sb = new InternalBuilder(mRS);
293 mNumTextures = 0;
294 for(int i = 0; i < MAX_TEXTURE; i ++) {
295 if(mSlots[i] != null) {
296 mNumTextures ++;
297 }
298 }
299 buildShaderString();
300 sb.setShader(mShader);
301
302 Type constType = null;
303 if (!mVaryingColorEnable) {
304 Element.Builder b = new Element.Builder(mRS);
305 b.add(Element.F32_4(mRS), "Color");
306 Type.Builder typeBuilder = new Type.Builder(mRS, b.create());
307 typeBuilder.setX(1);
308 constType = typeBuilder.create();
309 sb.addConstant(constType);
310 }
311 for (int i = 0; i < mNumTextures; i ++) {
312 sb.addTexture(TextureType.TEXTURE_2D);
313 }
314
315 ProgramFragmentFixedFunction pf = sb.create();
316 pf.mTextureCount = MAX_TEXTURE;
317 if (!mVaryingColorEnable) {
318 Allocation constantData = Allocation.createTyped(mRS,constType);
Jason Samsb97b2512011-01-16 15:04:08 -0800319 FieldPacker fp = new FieldPacker(16);
320 Float4 f4 = new Float4(1.f, 1.f, 1.f, 1.f);
321 fp.addF32(f4);
322 constantData.setFromFieldPacker(0, fp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800323 pf.bindConstants(constantData, 0);
324 }
325 return pf;
326 }
327 }
328}
329
330
331
332