blob: 8dbf6f44f1374a74b3ae4b4bbd233e7c3fb350ed [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
Artur Satayev2ebb31c2020-01-08 12:24:36 +000019import android.compat.annotation.UnsupportedAppUsage;
Mathew Inwood15324472018-08-06 11:18:49 +010020
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080021
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070022/**
Tim Murraya9084222013-04-05 22:06:43 +000023 * @hide
Jason Sams65c80f82012-05-08 17:30:26 -070024 * @deprecated in API 16
Robert Ly11518ac2011-02-09 13:57:06 -080025 * <p>ProgramFragmentFixedFunction is a helper class that provides
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080026 * a way to make a simple fragment shader without writing any
Robert Ly11518ac2011-02-09 13:57:06 -080027 * GLSL code. This class allows for display of constant color, interpolated
28 * color from the vertex shader, or combinations of the both
29 * blended with results of up to two texture lookups.</p
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080030 *
31 **/
Xusong Wang8b4548c2021-01-05 10:09:52 -080032@Deprecated
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080033public class ProgramFragmentFixedFunction extends ProgramFragment {
Tim Murray460a0492013-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 Murray460a0492013-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 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100108 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800109 REPLACE (1),
Jason Sams65c80f82012-05-08 17:30:26 -0700110 /**
111 * @deprecated in API 16
112 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100113 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800114 MODULATE (2),
Jason Sams65c80f82012-05-08 17:30:26 -0700115 /**
116 * @deprecated in API 16
117 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800118 DECAL (3);
119
120 int mID;
121 EnvMode(int id) {
122 mID = id;
123 }
124 }
125
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700126 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700127 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800128 * Format describes the pixel format of textures in the fixed
129 * function fragment shader and how they are sampled
130 *
131 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800132 public enum Format {
Jason Sams65c80f82012-05-08 17:30:26 -0700133 /**
134 * @deprecated in API 16
135 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100136 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800137 ALPHA (1),
Jason Sams65c80f82012-05-08 17:30:26 -0700138 /**
139 * @deprecated in API 16
140 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800141 LUMINANCE_ALPHA (2),
Jason Sams65c80f82012-05-08 17:30:26 -0700142 /**
143 * @deprecated in API 16
144 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100145 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800146 RGB (3),
Jason Sams65c80f82012-05-08 17:30:26 -0700147 /**
148 * @deprecated in API 16
149 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100150 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800151 RGBA (4);
152
153 int mID;
154 Format(int id) {
155 mID = id;
156 }
157 }
158
159 private class Slot {
160 EnvMode env;
161 Format format;
162 Slot(EnvMode _env, Format _fmt) {
163 env = _env;
164 format = _fmt;
165 }
166 }
167 Slot[] mSlots;
168
169 private void buildShaderString() {
170 mShader = "//rs_shader_internal\n";
171 mShader += "varying lowp vec4 varColor;\n";
172 mShader += "varying vec2 varTex0;\n";
173
174 mShader += "void main() {\n";
175 if (mVaryingColorEnable) {
176 mShader += " lowp vec4 col = varColor;\n";
177 } else {
178 mShader += " lowp vec4 col = UNI_Color;\n";
179 }
180
181 if (mNumTextures != 0) {
182 if (mPointSpriteEnable) {
183 mShader += " vec2 t0 = gl_PointCoord;\n";
184 } else {
185 mShader += " vec2 t0 = varTex0.xy;\n";
186 }
187 }
188
189 for(int i = 0; i < mNumTextures; i ++) {
190 switch(mSlots[i].env) {
191 case REPLACE:
192 switch (mSlots[i].format) {
193 case ALPHA:
194 mShader += " col.a = texture2D(UNI_Tex0, t0).a;\n";
195 break;
196 case LUMINANCE_ALPHA:
197 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
198 break;
199 case RGB:
200 mShader += " col.rgb = texture2D(UNI_Tex0, t0).rgb;\n";
201 break;
202 case RGBA:
203 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
204 break;
205 }
206 break;
207 case MODULATE:
208 switch (mSlots[i].format) {
209 case ALPHA:
210 mShader += " col.a *= texture2D(UNI_Tex0, t0).a;\n";
211 break;
212 case LUMINANCE_ALPHA:
213 mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
214 break;
215 case RGB:
216 mShader += " col.rgb *= texture2D(UNI_Tex0, t0).rgb;\n";
217 break;
218 case RGBA:
219 mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
220 break;
221 }
222 break;
223 case DECAL:
224 mShader += " col = texture2D(UNI_Tex0, t0);\n";
225 break;
226 }
227 }
228
229 mShader += " gl_FragColor = col;\n";
230 mShader += "}\n";
231 }
232
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700233 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700234 * @deprecated
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800235 * Creates a builder for fixed function fragment program
236 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800237 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800238 */
Mathew Inwood15324472018-08-06 11:18:49 +0100239 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800240 public Builder(RenderScript rs) {
241 mRS = rs;
242 mSlots = new Slot[MAX_TEXTURE];
243 mPointSpriteEnable = false;
244 }
245
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700246 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700247 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800248 * Adds a texture to be fetched as part of the fixed function
249 * fragment program
250 *
251 * @param env specifies how the texture is combined with the
252 * current color
253 * @param fmt specifies the format of the texture and how its
254 * components will be used to combine with the
255 * current color
256 * @param slot index of the texture to apply the operations on
257 *
258 * @return this
259 */
Mathew Inwood15324472018-08-06 11:18:49 +0100260 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800261 public Builder setTexture(EnvMode env, Format fmt, int slot)
262 throws IllegalArgumentException {
263 if((slot < 0) || (slot >= MAX_TEXTURE)) {
264 throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
265 }
266 mSlots[slot] = new Slot(env, fmt);
267 return this;
268 }
269
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700270 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700271 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800272 * Specifies whether the texture coordinate passed from the
273 * vertex program is replaced with an openGL internal point
274 * sprite texture coordinate
275 *
276 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800277 public Builder setPointSpriteTexCoordinateReplacement(boolean enable) {
278 mPointSpriteEnable = enable;
279 return this;
280 }
281
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700282 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700283 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800284 * Specifies whether the varying color passed from the vertex
285 * program or the constant color set on the fragment program is
286 * used in the final color calculation in the fixed function
287 * fragment shader
288 *
289 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100290 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800291 public Builder setVaryingColor(boolean enable) {
292 mVaryingColorEnable = enable;
293 return this;
294 }
295
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700296 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700297 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800298 * Creates the fixed function fragment program from the current
299 * state of the builder.
300 *
301 */
Mathew Inwood15324472018-08-06 11:18:49 +0100302 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800303 public ProgramFragmentFixedFunction create() {
304 InternalBuilder sb = new InternalBuilder(mRS);
305 mNumTextures = 0;
306 for(int i = 0; i < MAX_TEXTURE; i ++) {
307 if(mSlots[i] != null) {
308 mNumTextures ++;
309 }
310 }
311 buildShaderString();
312 sb.setShader(mShader);
313
314 Type constType = null;
315 if (!mVaryingColorEnable) {
316 Element.Builder b = new Element.Builder(mRS);
317 b.add(Element.F32_4(mRS), "Color");
318 Type.Builder typeBuilder = new Type.Builder(mRS, b.create());
319 typeBuilder.setX(1);
320 constType = typeBuilder.create();
321 sb.addConstant(constType);
322 }
323 for (int i = 0; i < mNumTextures; i ++) {
324 sb.addTexture(TextureType.TEXTURE_2D);
325 }
326
327 ProgramFragmentFixedFunction pf = sb.create();
328 pf.mTextureCount = MAX_TEXTURE;
329 if (!mVaryingColorEnable) {
330 Allocation constantData = Allocation.createTyped(mRS,constType);
Jason Samsb97b2512011-01-16 15:04:08 -0800331 FieldPacker fp = new FieldPacker(16);
332 Float4 f4 = new Float4(1.f, 1.f, 1.f, 1.f);
333 fp.addF32(f4);
334 constantData.setFromFieldPacker(0, fp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800335 pf.bindConstants(constantData, 0);
336 }
337 return pf;
338 }
339 }
340}
341
342
343
344