blob: c741ce6e77ed428f4cd75b2d5eacc1fabee030d2 [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 Satayev53ada2a2019-12-10 17:47:56 +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 **/
32public class ProgramFragmentFixedFunction extends ProgramFragment {
Tim Murray460a0492013-11-19 12:45:54 -080033 ProgramFragmentFixedFunction(long id, RenderScript rs) {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080034 super(id, rs);
35 }
36
37 static class InternalBuilder extends BaseProgramBuilder {
Jason Sams65c80f82012-05-08 17:30:26 -070038 /**
39 * @deprecated in API 16
40 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080041 public InternalBuilder(RenderScript rs) {
42 super(rs);
43 }
44
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070045 /**
Jason Sams65c80f82012-05-08 17:30:26 -070046 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080047 * Creates ProgramFragmentFixedFunction from the current state
48 * of the builder
49 *
50 * @return ProgramFragmentFixedFunction
51 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080052 public ProgramFragmentFixedFunction create() {
53 mRS.validate();
Ashok Bhat98071552014-02-12 09:54:43 +000054 long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080055 String[] texNames = new String[mTextureCount];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080056 int idx = 0;
57
58 for (int i=0; i < mInputCount; i++) {
59 tmp[idx++] = ProgramParam.INPUT.mID;
Ashok Bhat98071552014-02-12 09:54:43 +000060 tmp[idx++] = mInputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080061 }
62 for (int i=0; i < mOutputCount; i++) {
63 tmp[idx++] = ProgramParam.OUTPUT.mID;
Ashok Bhat98071552014-02-12 09:54:43 +000064 tmp[idx++] = mOutputs[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080065 }
66 for (int i=0; i < mConstantCount; i++) {
67 tmp[idx++] = ProgramParam.CONSTANT.mID;
Ashok Bhat98071552014-02-12 09:54:43 +000068 tmp[idx++] = mConstants[i].getID(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080069 }
70 for (int i=0; i < mTextureCount; i++) {
71 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
Ashok Bhat98071552014-02-12 09:54:43 +000072 tmp[idx++] = mTextureTypes[i].mID;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080073 texNames[i] = mTextureNames[i];
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080074 }
75
Tim Murray460a0492013-11-19 12:45:54 -080076 long id = mRS.nProgramFragmentCreate(mShader, texNames, tmp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080077 ProgramFragmentFixedFunction pf = new ProgramFragmentFixedFunction(id, mRS);
78 initProgram(pf);
79 return pf;
80 }
81 }
82
Jason Sams65c80f82012-05-08 17:30:26 -070083 /**
84 * @deprecated in API 16
85 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080086 public static class Builder {
Jason Sams65c80f82012-05-08 17:30:26 -070087 /**
88 * @deprecated in API 16
89 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080090 public static final int MAX_TEXTURE = 2;
91 int mNumTextures;
92 boolean mPointSpriteEnable;
93 boolean mVaryingColorEnable;
94 String mShader;
95 RenderScript mRS;
96
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070097 /**
Jason Sams65c80f82012-05-08 17:30:26 -070098 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080099 * EnvMode describes how textures are combined with the existing
100 * color in the fixed function fragment shader
101 *
102 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800103 public enum EnvMode {
Jason Sams65c80f82012-05-08 17:30:26 -0700104 /**
105 * @deprecated in API 16
106 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100107 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800108 REPLACE (1),
Jason Sams65c80f82012-05-08 17:30:26 -0700109 /**
110 * @deprecated in API 16
111 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100112 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800113 MODULATE (2),
Jason Sams65c80f82012-05-08 17:30:26 -0700114 /**
115 * @deprecated in API 16
116 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800117 DECAL (3);
118
119 int mID;
120 EnvMode(int id) {
121 mID = id;
122 }
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 * Format describes the pixel format of textures in the fixed
128 * function fragment shader and how they are sampled
129 *
130 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800131 public enum Format {
Jason Sams65c80f82012-05-08 17:30:26 -0700132 /**
133 * @deprecated in API 16
134 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100135 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800136 ALPHA (1),
Jason Sams65c80f82012-05-08 17:30:26 -0700137 /**
138 * @deprecated in API 16
139 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800140 LUMINANCE_ALPHA (2),
Jason Sams65c80f82012-05-08 17:30:26 -0700141 /**
142 * @deprecated in API 16
143 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100144 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800145 RGB (3),
Jason Sams65c80f82012-05-08 17:30:26 -0700146 /**
147 * @deprecated in API 16
148 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100149 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800150 RGBA (4);
151
152 int mID;
153 Format(int id) {
154 mID = id;
155 }
156 }
157
158 private class Slot {
159 EnvMode env;
160 Format format;
161 Slot(EnvMode _env, Format _fmt) {
162 env = _env;
163 format = _fmt;
164 }
165 }
166 Slot[] mSlots;
167
168 private void buildShaderString() {
169 mShader = "//rs_shader_internal\n";
170 mShader += "varying lowp vec4 varColor;\n";
171 mShader += "varying vec2 varTex0;\n";
172
173 mShader += "void main() {\n";
174 if (mVaryingColorEnable) {
175 mShader += " lowp vec4 col = varColor;\n";
176 } else {
177 mShader += " lowp vec4 col = UNI_Color;\n";
178 }
179
180 if (mNumTextures != 0) {
181 if (mPointSpriteEnable) {
182 mShader += " vec2 t0 = gl_PointCoord;\n";
183 } else {
184 mShader += " vec2 t0 = varTex0.xy;\n";
185 }
186 }
187
188 for(int i = 0; i < mNumTextures; i ++) {
189 switch(mSlots[i].env) {
190 case REPLACE:
191 switch (mSlots[i].format) {
192 case ALPHA:
193 mShader += " col.a = texture2D(UNI_Tex0, t0).a;\n";
194 break;
195 case LUMINANCE_ALPHA:
196 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
197 break;
198 case RGB:
199 mShader += " col.rgb = texture2D(UNI_Tex0, t0).rgb;\n";
200 break;
201 case RGBA:
202 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
203 break;
204 }
205 break;
206 case MODULATE:
207 switch (mSlots[i].format) {
208 case ALPHA:
209 mShader += " col.a *= texture2D(UNI_Tex0, t0).a;\n";
210 break;
211 case LUMINANCE_ALPHA:
212 mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
213 break;
214 case RGB:
215 mShader += " col.rgb *= texture2D(UNI_Tex0, t0).rgb;\n";
216 break;
217 case RGBA:
218 mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
219 break;
220 }
221 break;
222 case DECAL:
223 mShader += " col = texture2D(UNI_Tex0, t0);\n";
224 break;
225 }
226 }
227
228 mShader += " gl_FragColor = col;\n";
229 mShader += "}\n";
230 }
231
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700232 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700233 * @deprecated
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800234 * Creates a builder for fixed function fragment program
235 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800236 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800237 */
Mathew Inwood15324472018-08-06 11:18:49 +0100238 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800239 public Builder(RenderScript rs) {
240 mRS = rs;
241 mSlots = new Slot[MAX_TEXTURE];
242 mPointSpriteEnable = false;
243 }
244
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700245 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700246 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800247 * Adds a texture to be fetched as part of the fixed function
248 * fragment program
249 *
250 * @param env specifies how the texture is combined with the
251 * current color
252 * @param fmt specifies the format of the texture and how its
253 * components will be used to combine with the
254 * current color
255 * @param slot index of the texture to apply the operations on
256 *
257 * @return this
258 */
Mathew Inwood15324472018-08-06 11:18:49 +0100259 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800260 public Builder setTexture(EnvMode env, Format fmt, int slot)
261 throws IllegalArgumentException {
262 if((slot < 0) || (slot >= MAX_TEXTURE)) {
263 throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
264 }
265 mSlots[slot] = new Slot(env, fmt);
266 return this;
267 }
268
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700269 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700270 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800271 * Specifies whether the texture coordinate passed from the
272 * vertex program is replaced with an openGL internal point
273 * sprite texture coordinate
274 *
275 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800276 public Builder setPointSpriteTexCoordinateReplacement(boolean enable) {
277 mPointSpriteEnable = enable;
278 return this;
279 }
280
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700281 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700282 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800283 * Specifies whether the varying color passed from the vertex
284 * program or the constant color set on the fragment program is
285 * used in the final color calculation in the fixed function
286 * fragment shader
287 *
288 **/
Mathew Inwood15324472018-08-06 11:18:49 +0100289 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800290 public Builder setVaryingColor(boolean enable) {
291 mVaryingColorEnable = enable;
292 return this;
293 }
294
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700295 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700296 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800297 * Creates the fixed function fragment program from the current
298 * state of the builder.
299 *
300 */
Mathew Inwood15324472018-08-06 11:18:49 +0100301 @UnsupportedAppUsage
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800302 public ProgramFragmentFixedFunction create() {
303 InternalBuilder sb = new InternalBuilder(mRS);
304 mNumTextures = 0;
305 for(int i = 0; i < MAX_TEXTURE; i ++) {
306 if(mSlots[i] != null) {
307 mNumTextures ++;
308 }
309 }
310 buildShaderString();
311 sb.setShader(mShader);
312
313 Type constType = null;
314 if (!mVaryingColorEnable) {
315 Element.Builder b = new Element.Builder(mRS);
316 b.add(Element.F32_4(mRS), "Color");
317 Type.Builder typeBuilder = new Type.Builder(mRS, b.create());
318 typeBuilder.setX(1);
319 constType = typeBuilder.create();
320 sb.addConstant(constType);
321 }
322 for (int i = 0; i < mNumTextures; i ++) {
323 sb.addTexture(TextureType.TEXTURE_2D);
324 }
325
326 ProgramFragmentFixedFunction pf = sb.create();
327 pf.mTextureCount = MAX_TEXTURE;
328 if (!mVaryingColorEnable) {
329 Allocation constantData = Allocation.createTyped(mRS,constType);
Jason Samsb97b2512011-01-16 15:04:08 -0800330 FieldPacker fp = new FieldPacker(16);
331 Float4 f4 = new Float4(1.f, 1.f, 1.f, 1.f);
332 fp.addF32(f4);
333 constantData.setFromFieldPacker(0, fp);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800334 pf.bindConstants(constantData, 0);
335 }
336 return pf;
337 }
338 }
339}
340
341
342
343