Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 1 | /* |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 2 | * Copyright (C) 2008-2012 The Android Open Source Project |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 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 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 20 | import android.util.Log; |
| 21 | |
| 22 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 23 | /** |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [diff] [blame^] | 24 | * @hide |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 25 | * @deprecated in API 16 |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 26 | * <p>ProgramFragmentFixedFunction is a helper class that provides |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 27 | * a way to make a simple fragment shader without writing any |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 28 | * GLSL code. This class allows for display of constant color, interpolated |
| 29 | * color from the vertex shader, or combinations of the both |
| 30 | * blended with results of up to two texture lookups.</p |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 31 | * |
| 32 | **/ |
| 33 | public class ProgramFragmentFixedFunction extends ProgramFragment { |
| 34 | ProgramFragmentFixedFunction(int id, RenderScript rs) { |
| 35 | super(id, rs); |
| 36 | } |
| 37 | |
| 38 | static class InternalBuilder extends BaseProgramBuilder { |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 39 | /** |
| 40 | * @deprecated in API 16 |
| 41 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 42 | public InternalBuilder(RenderScript rs) { |
| 43 | super(rs); |
| 44 | } |
| 45 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 46 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 47 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 48 | * Creates ProgramFragmentFixedFunction from the current state |
| 49 | * of the builder |
| 50 | * |
| 51 | * @return ProgramFragmentFixedFunction |
| 52 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 53 | public ProgramFragmentFixedFunction create() { |
| 54 | mRS.validate(); |
| 55 | int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2]; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 56 | String[] texNames = new String[mTextureCount]; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 57 | int idx = 0; |
| 58 | |
| 59 | for (int i=0; i < mInputCount; i++) { |
| 60 | tmp[idx++] = ProgramParam.INPUT.mID; |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 61 | tmp[idx++] = mInputs[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 62 | } |
| 63 | for (int i=0; i < mOutputCount; i++) { |
| 64 | tmp[idx++] = ProgramParam.OUTPUT.mID; |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 65 | tmp[idx++] = mOutputs[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 66 | } |
| 67 | for (int i=0; i < mConstantCount; i++) { |
| 68 | tmp[idx++] = ProgramParam.CONSTANT.mID; |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 69 | tmp[idx++] = mConstants[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 70 | } |
| 71 | for (int i=0; i < mTextureCount; i++) { |
| 72 | tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID; |
| 73 | tmp[idx++] = mTextureTypes[i].mID; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 74 | texNames[i] = mTextureNames[i]; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 77 | int id = mRS.nProgramFragmentCreate(mShader, texNames, tmp); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 78 | ProgramFragmentFixedFunction pf = new ProgramFragmentFixedFunction(id, mRS); |
| 79 | initProgram(pf); |
| 80 | return pf; |
| 81 | } |
| 82 | } |
| 83 | |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 84 | /** |
| 85 | * @deprecated in API 16 |
| 86 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 87 | public static class Builder { |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 88 | /** |
| 89 | * @deprecated in API 16 |
| 90 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 91 | public static final int MAX_TEXTURE = 2; |
| 92 | int mNumTextures; |
| 93 | boolean mPointSpriteEnable; |
| 94 | boolean mVaryingColorEnable; |
| 95 | String mShader; |
| 96 | RenderScript mRS; |
| 97 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 98 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 99 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 100 | * EnvMode describes how textures are combined with the existing |
| 101 | * color in the fixed function fragment shader |
| 102 | * |
| 103 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 104 | public enum EnvMode { |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 105 | /** |
| 106 | * @deprecated in API 16 |
| 107 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 108 | REPLACE (1), |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 109 | /** |
| 110 | * @deprecated in API 16 |
| 111 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 112 | MODULATE (2), |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 113 | /** |
| 114 | * @deprecated in API 16 |
| 115 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 116 | DECAL (3); |
| 117 | |
| 118 | int mID; |
| 119 | EnvMode(int id) { |
| 120 | mID = id; |
| 121 | } |
| 122 | } |
| 123 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 124 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 125 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 126 | * Format describes the pixel format of textures in the fixed |
| 127 | * function fragment shader and how they are sampled |
| 128 | * |
| 129 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 130 | public enum Format { |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 131 | /** |
| 132 | * @deprecated in API 16 |
| 133 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 134 | ALPHA (1), |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 135 | /** |
| 136 | * @deprecated in API 16 |
| 137 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 138 | LUMINANCE_ALPHA (2), |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 139 | /** |
| 140 | * @deprecated in API 16 |
| 141 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 142 | RGB (3), |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 143 | /** |
| 144 | * @deprecated in API 16 |
| 145 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 146 | RGBA (4); |
| 147 | |
| 148 | int mID; |
| 149 | Format(int id) { |
| 150 | mID = id; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private class Slot { |
| 155 | EnvMode env; |
| 156 | Format format; |
| 157 | Slot(EnvMode _env, Format _fmt) { |
| 158 | env = _env; |
| 159 | format = _fmt; |
| 160 | } |
| 161 | } |
| 162 | Slot[] mSlots; |
| 163 | |
| 164 | private void buildShaderString() { |
| 165 | mShader = "//rs_shader_internal\n"; |
| 166 | mShader += "varying lowp vec4 varColor;\n"; |
| 167 | mShader += "varying vec2 varTex0;\n"; |
| 168 | |
| 169 | mShader += "void main() {\n"; |
| 170 | if (mVaryingColorEnable) { |
| 171 | mShader += " lowp vec4 col = varColor;\n"; |
| 172 | } else { |
| 173 | mShader += " lowp vec4 col = UNI_Color;\n"; |
| 174 | } |
| 175 | |
| 176 | if (mNumTextures != 0) { |
| 177 | if (mPointSpriteEnable) { |
| 178 | mShader += " vec2 t0 = gl_PointCoord;\n"; |
| 179 | } else { |
| 180 | mShader += " vec2 t0 = varTex0.xy;\n"; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | for(int i = 0; i < mNumTextures; i ++) { |
| 185 | switch(mSlots[i].env) { |
| 186 | case REPLACE: |
| 187 | switch (mSlots[i].format) { |
| 188 | case ALPHA: |
| 189 | mShader += " col.a = texture2D(UNI_Tex0, t0).a;\n"; |
| 190 | break; |
| 191 | case LUMINANCE_ALPHA: |
| 192 | mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n"; |
| 193 | break; |
| 194 | case RGB: |
| 195 | mShader += " col.rgb = texture2D(UNI_Tex0, t0).rgb;\n"; |
| 196 | break; |
| 197 | case RGBA: |
| 198 | mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n"; |
| 199 | break; |
| 200 | } |
| 201 | break; |
| 202 | case MODULATE: |
| 203 | switch (mSlots[i].format) { |
| 204 | case ALPHA: |
| 205 | mShader += " col.a *= texture2D(UNI_Tex0, t0).a;\n"; |
| 206 | break; |
| 207 | case LUMINANCE_ALPHA: |
| 208 | mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n"; |
| 209 | break; |
| 210 | case RGB: |
| 211 | mShader += " col.rgb *= texture2D(UNI_Tex0, t0).rgb;\n"; |
| 212 | break; |
| 213 | case RGBA: |
| 214 | mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n"; |
| 215 | break; |
| 216 | } |
| 217 | break; |
| 218 | case DECAL: |
| 219 | mShader += " col = texture2D(UNI_Tex0, t0);\n"; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | mShader += " gl_FragColor = col;\n"; |
| 225 | mShader += "}\n"; |
| 226 | } |
| 227 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 228 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 229 | * @deprecated |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 230 | * Creates a builder for fixed function fragment program |
| 231 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 232 | * @param rs Context to which the program will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 233 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 234 | public Builder(RenderScript rs) { |
| 235 | mRS = rs; |
| 236 | mSlots = new Slot[MAX_TEXTURE]; |
| 237 | mPointSpriteEnable = false; |
| 238 | } |
| 239 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 240 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 241 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 242 | * Adds a texture to be fetched as part of the fixed function |
| 243 | * fragment program |
| 244 | * |
| 245 | * @param env specifies how the texture is combined with the |
| 246 | * current color |
| 247 | * @param fmt specifies the format of the texture and how its |
| 248 | * components will be used to combine with the |
| 249 | * current color |
| 250 | * @param slot index of the texture to apply the operations on |
| 251 | * |
| 252 | * @return this |
| 253 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 254 | public Builder setTexture(EnvMode env, Format fmt, int slot) |
| 255 | throws IllegalArgumentException { |
| 256 | if((slot < 0) || (slot >= MAX_TEXTURE)) { |
| 257 | throw new IllegalArgumentException("MAX_TEXTURE exceeded."); |
| 258 | } |
| 259 | mSlots[slot] = new Slot(env, fmt); |
| 260 | return this; |
| 261 | } |
| 262 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 263 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 264 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 265 | * Specifies whether the texture coordinate passed from the |
| 266 | * vertex program is replaced with an openGL internal point |
| 267 | * sprite texture coordinate |
| 268 | * |
| 269 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 270 | public Builder setPointSpriteTexCoordinateReplacement(boolean enable) { |
| 271 | mPointSpriteEnable = enable; |
| 272 | return this; |
| 273 | } |
| 274 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 275 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 276 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 277 | * Specifies whether the varying color passed from the vertex |
| 278 | * program or the constant color set on the fragment program is |
| 279 | * used in the final color calculation in the fixed function |
| 280 | * fragment shader |
| 281 | * |
| 282 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 283 | public Builder setVaryingColor(boolean enable) { |
| 284 | mVaryingColorEnable = enable; |
| 285 | return this; |
| 286 | } |
| 287 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 288 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 289 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 290 | * Creates the fixed function fragment program from the current |
| 291 | * state of the builder. |
| 292 | * |
| 293 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 294 | public ProgramFragmentFixedFunction create() { |
| 295 | InternalBuilder sb = new InternalBuilder(mRS); |
| 296 | mNumTextures = 0; |
| 297 | for(int i = 0; i < MAX_TEXTURE; i ++) { |
| 298 | if(mSlots[i] != null) { |
| 299 | mNumTextures ++; |
| 300 | } |
| 301 | } |
| 302 | buildShaderString(); |
| 303 | sb.setShader(mShader); |
| 304 | |
| 305 | Type constType = null; |
| 306 | if (!mVaryingColorEnable) { |
| 307 | Element.Builder b = new Element.Builder(mRS); |
| 308 | b.add(Element.F32_4(mRS), "Color"); |
| 309 | Type.Builder typeBuilder = new Type.Builder(mRS, b.create()); |
| 310 | typeBuilder.setX(1); |
| 311 | constType = typeBuilder.create(); |
| 312 | sb.addConstant(constType); |
| 313 | } |
| 314 | for (int i = 0; i < mNumTextures; i ++) { |
| 315 | sb.addTexture(TextureType.TEXTURE_2D); |
| 316 | } |
| 317 | |
| 318 | ProgramFragmentFixedFunction pf = sb.create(); |
| 319 | pf.mTextureCount = MAX_TEXTURE; |
| 320 | if (!mVaryingColorEnable) { |
| 321 | Allocation constantData = Allocation.createTyped(mRS,constType); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 322 | FieldPacker fp = new FieldPacker(16); |
| 323 | Float4 f4 = new Float4(1.f, 1.f, 1.f, 1.f); |
| 324 | fp.addF32(f4); |
| 325 | constantData.setFromFieldPacker(0, fp); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 326 | pf.bindConstants(constantData, 0); |
| 327 | } |
| 328 | return pf; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | |
| 334 | |
| 335 | |