blob: 19fca5873b51847f1592c8774153487cfe4dd747 [file] [log] [blame]
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070023/** @deprecated renderscript is deprecated in J
Robert Ly11518ac2011-02-09 13:57:06 -080024 * <p>ProgramFragmentFixedFunction is a helper class that provides
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080025 * a way to make a simple fragment shader without writing any
Robert Ly11518ac2011-02-09 13:57:06 -080026 * GLSL code. This class allows for display of constant color, interpolated
27 * color from the vertex shader, or combinations of the both
28 * blended with results of up to two texture lookups.</p
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080029 *
30 **/
31public class ProgramFragmentFixedFunction extends ProgramFragment {
32 ProgramFragmentFixedFunction(int id, RenderScript rs) {
33 super(id, rs);
34 }
35
36 static class InternalBuilder extends BaseProgramBuilder {
37 public InternalBuilder(RenderScript rs) {
38 super(rs);
39 }
40
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070041 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080042 * Creates ProgramFragmentFixedFunction from the current state
43 * of the builder
44 *
45 * @return ProgramFragmentFixedFunction
46 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080047 public ProgramFragmentFixedFunction create() {
48 mRS.validate();
49 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080050 String[] texNames = new String[mTextureCount];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080051 int idx = 0;
52
53 for (int i=0; i < mInputCount; i++) {
54 tmp[idx++] = ProgramParam.INPUT.mID;
Jason Samse07694b2012-04-03 15:36:36 -070055 tmp[idx++] = mInputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080056 }
57 for (int i=0; i < mOutputCount; i++) {
58 tmp[idx++] = ProgramParam.OUTPUT.mID;
Jason Samse07694b2012-04-03 15:36:36 -070059 tmp[idx++] = mOutputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080060 }
61 for (int i=0; i < mConstantCount; i++) {
62 tmp[idx++] = ProgramParam.CONSTANT.mID;
Jason Samse07694b2012-04-03 15:36:36 -070063 tmp[idx++] = mConstants[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080064 }
65 for (int i=0; i < mTextureCount; i++) {
66 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
67 tmp[idx++] = mTextureTypes[i].mID;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080068 texNames[i] = mTextureNames[i];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080069 }
70
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080071 int id = mRS.nProgramFragmentCreate(mShader, texNames, tmp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080072 ProgramFragmentFixedFunction pf = new ProgramFragmentFixedFunction(id, mRS);
73 initProgram(pf);
74 return pf;
75 }
76 }
77
78 public static class Builder {
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070079 /** @deprecated renderscript is deprecated in J
80 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080081 public static final int MAX_TEXTURE = 2;
82 int mNumTextures;
83 boolean mPointSpriteEnable;
84 boolean mVaryingColorEnable;
85 String mShader;
86 RenderScript mRS;
87
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070088 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080089 * EnvMode describes how textures are combined with the existing
90 * color in the fixed function fragment shader
91 *
92 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080093 public enum EnvMode {
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070094 /** @deprecated renderscript is deprecated in J
95 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080096 REPLACE (1),
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070097 /** @deprecated renderscript is deprecated in J
98 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080099 MODULATE (2),
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700100 /** @deprecated renderscript is deprecated in J
101 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800102 DECAL (3);
103
104 int mID;
105 EnvMode(int id) {
106 mID = id;
107 }
108 }
109
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700110 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800111 * Format describes the pixel format of textures in the fixed
112 * function fragment shader and how they are sampled
113 *
114 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800115 public enum Format {
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700116 /** @deprecated renderscript is deprecated in J
117 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800118 ALPHA (1),
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700119 /** @deprecated renderscript is deprecated in J
120 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800121 LUMINANCE_ALPHA (2),
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700122 /** @deprecated renderscript is deprecated in J
123 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800124 RGB (3),
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700125 /** @deprecated renderscript is deprecated in J
126 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800127 RGBA (4);
128
129 int mID;
130 Format(int id) {
131 mID = id;
132 }
133 }
134
135 private class Slot {
136 EnvMode env;
137 Format format;
138 Slot(EnvMode _env, Format _fmt) {
139 env = _env;
140 format = _fmt;
141 }
142 }
143 Slot[] mSlots;
144
145 private void buildShaderString() {
146 mShader = "//rs_shader_internal\n";
147 mShader += "varying lowp vec4 varColor;\n";
148 mShader += "varying vec2 varTex0;\n";
149
150 mShader += "void main() {\n";
151 if (mVaryingColorEnable) {
152 mShader += " lowp vec4 col = varColor;\n";
153 } else {
154 mShader += " lowp vec4 col = UNI_Color;\n";
155 }
156
157 if (mNumTextures != 0) {
158 if (mPointSpriteEnable) {
159 mShader += " vec2 t0 = gl_PointCoord;\n";
160 } else {
161 mShader += " vec2 t0 = varTex0.xy;\n";
162 }
163 }
164
165 for(int i = 0; i < mNumTextures; i ++) {
166 switch(mSlots[i].env) {
167 case REPLACE:
168 switch (mSlots[i].format) {
169 case ALPHA:
170 mShader += " col.a = texture2D(UNI_Tex0, t0).a;\n";
171 break;
172 case LUMINANCE_ALPHA:
173 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
174 break;
175 case RGB:
176 mShader += " col.rgb = texture2D(UNI_Tex0, t0).rgb;\n";
177 break;
178 case RGBA:
179 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
180 break;
181 }
182 break;
183 case MODULATE:
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 DECAL:
200 mShader += " col = texture2D(UNI_Tex0, t0);\n";
201 break;
202 }
203 }
204
205 mShader += " gl_FragColor = col;\n";
206 mShader += "}\n";
207 }
208
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700209 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800210 * Creates a builder for fixed function fragment program
211 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800212 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800213 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800214 public Builder(RenderScript rs) {
215 mRS = rs;
216 mSlots = new Slot[MAX_TEXTURE];
217 mPointSpriteEnable = false;
218 }
219
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700220 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800221 * Adds a texture to be fetched as part of the fixed function
222 * fragment program
223 *
224 * @param env specifies how the texture is combined with the
225 * current color
226 * @param fmt specifies the format of the texture and how its
227 * components will be used to combine with the
228 * current color
229 * @param slot index of the texture to apply the operations on
230 *
231 * @return this
232 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800233 public Builder setTexture(EnvMode env, Format fmt, int slot)
234 throws IllegalArgumentException {
235 if((slot < 0) || (slot >= MAX_TEXTURE)) {
236 throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
237 }
238 mSlots[slot] = new Slot(env, fmt);
239 return this;
240 }
241
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700242 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800243 * Specifies whether the texture coordinate passed from the
244 * vertex program is replaced with an openGL internal point
245 * sprite texture coordinate
246 *
247 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800248 public Builder setPointSpriteTexCoordinateReplacement(boolean enable) {
249 mPointSpriteEnable = enable;
250 return this;
251 }
252
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700253 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800254 * Specifies whether the varying color passed from the vertex
255 * program or the constant color set on the fragment program is
256 * used in the final color calculation in the fixed function
257 * fragment shader
258 *
259 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800260 public Builder setVaryingColor(boolean enable) {
261 mVaryingColorEnable = enable;
262 return this;
263 }
264
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700265 /** @deprecated renderscript is deprecated in J
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800266 * Creates the fixed function fragment program from the current
267 * state of the builder.
268 *
269 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800270 public ProgramFragmentFixedFunction create() {
271 InternalBuilder sb = new InternalBuilder(mRS);
272 mNumTextures = 0;
273 for(int i = 0; i < MAX_TEXTURE; i ++) {
274 if(mSlots[i] != null) {
275 mNumTextures ++;
276 }
277 }
278 buildShaderString();
279 sb.setShader(mShader);
280
281 Type constType = null;
282 if (!mVaryingColorEnable) {
283 Element.Builder b = new Element.Builder(mRS);
284 b.add(Element.F32_4(mRS), "Color");
285 Type.Builder typeBuilder = new Type.Builder(mRS, b.create());
286 typeBuilder.setX(1);
287 constType = typeBuilder.create();
288 sb.addConstant(constType);
289 }
290 for (int i = 0; i < mNumTextures; i ++) {
291 sb.addTexture(TextureType.TEXTURE_2D);
292 }
293
294 ProgramFragmentFixedFunction pf = sb.create();
295 pf.mTextureCount = MAX_TEXTURE;
296 if (!mVaryingColorEnable) {
297 Allocation constantData = Allocation.createTyped(mRS,constType);
Jason Samsb97b2512011-01-16 15:04:08 -0800298 FieldPacker fp = new FieldPacker(16);
299 Float4 f4 = new Float4(1.f, 1.f, 1.f, 1.f);
300 fp.addF32(f4);
301 constantData.setFromFieldPacker(0, fp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800302 pf.bindConstants(constantData, 0);
303 }
304 return pf;
305 }
306 }
307}
308
309
310
311