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 | |
Artur Satayev | 2ebb31c | 2020-01-08 12:24:36 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 20 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 21 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 22 | /** |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [diff] [blame] | 23 | * @hide |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 24 | * @deprecated in API 16 |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 25 | * <p>ProgramFragmentFixedFunction is a helper class that provides |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 26 | * a way to make a simple fragment shader without writing any |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 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 |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 30 | * |
| 31 | **/ |
| 32 | public class ProgramFragmentFixedFunction extends ProgramFragment { |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 33 | ProgramFragmentFixedFunction(long id, RenderScript rs) { |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 34 | super(id, rs); |
| 35 | } |
| 36 | |
| 37 | static class InternalBuilder extends BaseProgramBuilder { |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 38 | /** |
| 39 | * @deprecated in API 16 |
| 40 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 41 | public InternalBuilder(RenderScript rs) { |
| 42 | super(rs); |
| 43 | } |
| 44 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 45 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 46 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 47 | * Creates ProgramFragmentFixedFunction from the current state |
| 48 | * of the builder |
| 49 | * |
| 50 | * @return ProgramFragmentFixedFunction |
| 51 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 52 | public ProgramFragmentFixedFunction create() { |
| 53 | mRS.validate(); |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 54 | long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2]; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 55 | String[] texNames = new String[mTextureCount]; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 56 | int idx = 0; |
| 57 | |
| 58 | for (int i=0; i < mInputCount; i++) { |
| 59 | tmp[idx++] = ProgramParam.INPUT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 60 | tmp[idx++] = mInputs[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 61 | } |
| 62 | for (int i=0; i < mOutputCount; i++) { |
| 63 | tmp[idx++] = ProgramParam.OUTPUT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 64 | tmp[idx++] = mOutputs[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 65 | } |
| 66 | for (int i=0; i < mConstantCount; i++) { |
| 67 | tmp[idx++] = ProgramParam.CONSTANT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 68 | tmp[idx++] = mConstants[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 69 | } |
| 70 | for (int i=0; i < mTextureCount; i++) { |
| 71 | tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 72 | tmp[idx++] = mTextureTypes[i].mID; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 73 | texNames[i] = mTextureNames[i]; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 76 | long id = mRS.nProgramFragmentCreate(mShader, texNames, tmp); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 77 | ProgramFragmentFixedFunction pf = new ProgramFragmentFixedFunction(id, mRS); |
| 78 | initProgram(pf); |
| 79 | return pf; |
| 80 | } |
| 81 | } |
| 82 | |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 83 | /** |
| 84 | * @deprecated in API 16 |
| 85 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 86 | public static class Builder { |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 87 | /** |
| 88 | * @deprecated in API 16 |
| 89 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 90 | public static final int MAX_TEXTURE = 2; |
| 91 | int mNumTextures; |
| 92 | boolean mPointSpriteEnable; |
| 93 | boolean mVaryingColorEnable; |
| 94 | String mShader; |
| 95 | RenderScript mRS; |
| 96 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 97 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 98 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 99 | * EnvMode describes how textures are combined with the existing |
| 100 | * color in the fixed function fragment shader |
| 101 | * |
| 102 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 103 | public enum EnvMode { |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 104 | /** |
| 105 | * @deprecated in API 16 |
| 106 | **/ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 107 | @UnsupportedAppUsage |
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 | **/ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 112 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 113 | MODULATE (2), |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 114 | /** |
| 115 | * @deprecated in API 16 |
| 116 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 117 | DECAL (3); |
| 118 | |
| 119 | int mID; |
| 120 | EnvMode(int id) { |
| 121 | mID = id; |
| 122 | } |
| 123 | } |
| 124 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 125 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 126 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 127 | * Format describes the pixel format of textures in the fixed |
| 128 | * function fragment shader and how they are sampled |
| 129 | * |
| 130 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 131 | public enum Format { |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 132 | /** |
| 133 | * @deprecated in API 16 |
| 134 | **/ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 135 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 136 | ALPHA (1), |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 137 | /** |
| 138 | * @deprecated in API 16 |
| 139 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 140 | LUMINANCE_ALPHA (2), |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 141 | /** |
| 142 | * @deprecated in API 16 |
| 143 | **/ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 144 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 145 | RGB (3), |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 146 | /** |
| 147 | * @deprecated in API 16 |
| 148 | **/ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 149 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 150 | 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 Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 232 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 233 | * @deprecated |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 234 | * Creates a builder for fixed function fragment program |
| 235 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 236 | * @param rs Context to which the program will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 237 | */ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 238 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 239 | public Builder(RenderScript rs) { |
| 240 | mRS = rs; |
| 241 | mSlots = new Slot[MAX_TEXTURE]; |
| 242 | mPointSpriteEnable = false; |
| 243 | } |
| 244 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 245 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 246 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 247 | * 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 Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 259 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 260 | 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 Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 269 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 270 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 271 | * 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 276 | public Builder setPointSpriteTexCoordinateReplacement(boolean enable) { |
| 277 | mPointSpriteEnable = enable; |
| 278 | return this; |
| 279 | } |
| 280 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 281 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 282 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 283 | * 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 Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 289 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 290 | public Builder setVaryingColor(boolean enable) { |
| 291 | mVaryingColorEnable = enable; |
| 292 | return this; |
| 293 | } |
| 294 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 295 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 296 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 297 | * Creates the fixed function fragment program from the current |
| 298 | * state of the builder. |
| 299 | * |
| 300 | */ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 301 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 302 | 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 Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 330 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 334 | pf.bindConstants(constantData, 0); |
| 335 | } |
| 336 | return pf; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | |
| 343 | |