1 /* 2 * Copyright (C) 2008-2012 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 17 package android.renderscript; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 21 22 /** 23 * @hide 24 * @deprecated in API 16 25 * <p>ProgramFragmentFixedFunction is a helper class that provides 26 * a way to make a simple fragment shader without writing any 27 * 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 30 * 31 **/ 32 @Deprecated 33 public class ProgramFragmentFixedFunction extends ProgramFragment { ProgramFragmentFixedFunction(long id, RenderScript rs)34 ProgramFragmentFixedFunction(long id, RenderScript rs) { 35 super(id, rs); 36 } 37 38 static class InternalBuilder extends BaseProgramBuilder { 39 /** 40 * @deprecated in API 16 41 */ InternalBuilder(RenderScript rs)42 public InternalBuilder(RenderScript rs) { 43 super(rs); 44 } 45 46 /** 47 * @deprecated in API 16 48 * Creates ProgramFragmentFixedFunction from the current state 49 * of the builder 50 * 51 * @return ProgramFragmentFixedFunction 52 */ create()53 public ProgramFragmentFixedFunction create() { 54 mRS.validate(); 55 long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2]; 56 String[] texNames = new String[mTextureCount]; 57 int idx = 0; 58 59 for (int i=0; i < mInputCount; i++) { 60 tmp[idx++] = ProgramParam.INPUT.mID; 61 tmp[idx++] = mInputs[i].getID(mRS); 62 } 63 for (int i=0; i < mOutputCount; i++) { 64 tmp[idx++] = ProgramParam.OUTPUT.mID; 65 tmp[idx++] = mOutputs[i].getID(mRS); 66 } 67 for (int i=0; i < mConstantCount; i++) { 68 tmp[idx++] = ProgramParam.CONSTANT.mID; 69 tmp[idx++] = mConstants[i].getID(mRS); 70 } 71 for (int i=0; i < mTextureCount; i++) { 72 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID; 73 tmp[idx++] = mTextureTypes[i].mID; 74 texNames[i] = mTextureNames[i]; 75 } 76 77 long id = mRS.nProgramFragmentCreate(mShader, texNames, tmp); 78 ProgramFragmentFixedFunction pf = new ProgramFragmentFixedFunction(id, mRS); 79 initProgram(pf); 80 return pf; 81 } 82 } 83 84 /** 85 * @deprecated in API 16 86 */ 87 public static class Builder { 88 /** 89 * @deprecated in API 16 90 */ 91 public static final int MAX_TEXTURE = 2; 92 int mNumTextures; 93 boolean mPointSpriteEnable; 94 boolean mVaryingColorEnable; 95 String mShader; 96 RenderScript mRS; 97 98 /** 99 * @deprecated in API 16 100 * EnvMode describes how textures are combined with the existing 101 * color in the fixed function fragment shader 102 * 103 **/ 104 public enum EnvMode { 105 /** 106 * @deprecated in API 16 107 **/ 108 @UnsupportedAppUsage 109 REPLACE (1), 110 /** 111 * @deprecated in API 16 112 **/ 113 @UnsupportedAppUsage 114 MODULATE (2), 115 /** 116 * @deprecated in API 16 117 **/ 118 DECAL (3); 119 120 int mID; EnvMode(int id)121 EnvMode(int id) { 122 mID = id; 123 } 124 } 125 126 /** 127 * @deprecated in API 16 128 * Format describes the pixel format of textures in the fixed 129 * function fragment shader and how they are sampled 130 * 131 **/ 132 public enum Format { 133 /** 134 * @deprecated in API 16 135 **/ 136 @UnsupportedAppUsage 137 ALPHA (1), 138 /** 139 * @deprecated in API 16 140 **/ 141 LUMINANCE_ALPHA (2), 142 /** 143 * @deprecated in API 16 144 **/ 145 @UnsupportedAppUsage 146 RGB (3), 147 /** 148 * @deprecated in API 16 149 **/ 150 @UnsupportedAppUsage 151 RGBA (4); 152 153 int mID; Format(int id)154 Format(int id) { 155 mID = id; 156 } 157 } 158 159 private class Slot { 160 EnvMode env; 161 Format format; Slot(EnvMode _env, Format _fmt)162 Slot(EnvMode _env, Format _fmt) { 163 env = _env; 164 format = _fmt; 165 } 166 } 167 Slot[] mSlots; 168 buildShaderString()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 233 /** 234 * @deprecated 235 * Creates a builder for fixed function fragment program 236 * 237 * @param rs Context to which the program will belong. 238 */ 239 @UnsupportedAppUsage Builder(RenderScript rs)240 public Builder(RenderScript rs) { 241 mRS = rs; 242 mSlots = new Slot[MAX_TEXTURE]; 243 mPointSpriteEnable = false; 244 } 245 246 /** 247 * @deprecated in API 16 248 * 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 */ 260 @UnsupportedAppUsage setTexture(EnvMode env, Format fmt, int slot)261 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 270 /** 271 * @deprecated in API 16 272 * Specifies whether the texture coordinate passed from the 273 * vertex program is replaced with an openGL internal point 274 * sprite texture coordinate 275 * 276 **/ setPointSpriteTexCoordinateReplacement(boolean enable)277 public Builder setPointSpriteTexCoordinateReplacement(boolean enable) { 278 mPointSpriteEnable = enable; 279 return this; 280 } 281 282 /** 283 * @deprecated in API 16 284 * 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 **/ 290 @UnsupportedAppUsage setVaryingColor(boolean enable)291 public Builder setVaryingColor(boolean enable) { 292 mVaryingColorEnable = enable; 293 return this; 294 } 295 296 /** 297 * @deprecated in API 16 298 * Creates the fixed function fragment program from the current 299 * state of the builder. 300 * 301 */ 302 @UnsupportedAppUsage create()303 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); 331 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); 335 pf.bindConstants(constantData, 0); 336 } 337 return pf; 338 } 339 } 340 } 341 342 343 344 345