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