Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | #include <GLES2/gl2.h> |
| 18 | #include <GLES2/gl2ext.h> |
| 19 | |
| 20 | #include <utils/String8.h> |
| 21 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 22 | #include "Description.h" |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 23 | #include "Program.h" |
| 24 | #include "ProgramCache.h" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | // ----------------------------------------------------------------------------------------------- |
| 28 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 29 | /* |
| 30 | * A simple formatter class to automatically add the endl and |
| 31 | * manage the indentation. |
| 32 | */ |
| 33 | |
| 34 | class Formatter; |
| 35 | static Formatter& indent(Formatter& f); |
| 36 | static Formatter& dedent(Formatter& f); |
| 37 | |
| 38 | class Formatter { |
| 39 | String8 mString; |
| 40 | int mIndent; |
| 41 | typedef Formatter& (*FormaterManipFunc)(Formatter&); |
| 42 | friend Formatter& indent(Formatter& f); |
| 43 | friend Formatter& dedent(Formatter& f); |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 44 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 45 | public: |
Andy McFadden | 892f22d | 2013-08-15 10:05:01 -0700 | [diff] [blame] | 46 | Formatter() : mIndent(0) {} |
| 47 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 48 | String8 getString() const { return mString; } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 49 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 50 | friend Formatter& operator<<(Formatter& out, const char* in) { |
| 51 | for (int i = 0; i < out.mIndent; i++) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 52 | out.mString.append(" "); |
| 53 | } |
| 54 | out.mString.append(in); |
| 55 | out.mString.append("\n"); |
| 56 | return out; |
| 57 | } |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 58 | friend inline Formatter& operator<<(Formatter& out, const String8& in) { |
| 59 | return operator<<(out, in.string()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 60 | } |
| 61 | friend inline Formatter& operator<<(Formatter& to, FormaterManipFunc func) { |
| 62 | return (*func)(to); |
| 63 | } |
| 64 | }; |
| 65 | Formatter& indent(Formatter& f) { |
| 66 | f.mIndent++; |
| 67 | return f; |
| 68 | } |
| 69 | Formatter& dedent(Formatter& f) { |
| 70 | f.mIndent--; |
| 71 | return f; |
| 72 | } |
| 73 | |
| 74 | // ----------------------------------------------------------------------------------------------- |
| 75 | |
| 76 | ANDROID_SINGLETON_STATIC_INSTANCE(ProgramCache) |
| 77 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 78 | ProgramCache::ProgramCache() { |
Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 79 | // Until surfaceflinger has a dependable blob cache on the filesystem, |
| 80 | // generate shaders on initialization so as to avoid jank. |
| 81 | primeCache(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 84 | ProgramCache::~ProgramCache() {} |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 85 | |
Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 86 | void ProgramCache::primeCache() { |
| 87 | uint32_t shaderCount = 0; |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 88 | uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK; |
Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 89 | // Prime the cache for all combinations of the above masks, |
| 90 | // leaving off the experimental color matrix mask options. |
| 91 | |
| 92 | nsecs_t timeBefore = systemTime(); |
| 93 | for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) { |
| 94 | Key shaderKey; |
| 95 | shaderKey.set(keyMask, keyVal); |
| 96 | uint32_t tex = shaderKey.getTextureTarget(); |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 97 | if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) { |
Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 98 | continue; |
| 99 | } |
| 100 | Program* program = mCache.valueFor(shaderKey); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 101 | if (program == nullptr) { |
Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 102 | program = generateProgram(shaderKey); |
| 103 | mCache.add(shaderKey, program); |
| 104 | shaderCount++; |
| 105 | } |
| 106 | } |
| 107 | nsecs_t timeAfter = systemTime(); |
| 108 | float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6; |
| 109 | ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs); |
| 110 | } |
| 111 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 112 | ProgramCache::Key ProgramCache::computeKey(const Description& description) { |
| 113 | Key needs; |
| 114 | needs.set(Key::TEXTURE_MASK, |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 115 | !description.mTextureEnabled |
| 116 | ? Key::TEXTURE_OFF |
| 117 | : description.mTexture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES |
| 118 | ? Key::TEXTURE_EXT |
| 119 | : description.mTexture.getTextureTarget() == GL_TEXTURE_2D |
| 120 | ? Key::TEXTURE_2D |
| 121 | : Key::TEXTURE_OFF) |
| 122 | .set(Key::ALPHA_MASK, |
| 123 | (description.mColor.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE) |
| 124 | .set(Key::BLEND_MASK, |
| 125 | description.mPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL) |
| 126 | .set(Key::OPACITY_MASK, |
| 127 | description.mOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT) |
| 128 | .set(Key::COLOR_MATRIX_MASK, |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame^] | 129 | description.mColorMatrixEnabled ? Key::COLOR_MATRIX_ON : Key::COLOR_MATRIX_OFF); |
| 130 | |
| 131 | if (needs.hasColorMatrix()) { |
| 132 | switch (description.mInputTransferFunction) { |
| 133 | case Description::TransferFunction::LINEAR: |
| 134 | default: |
| 135 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR); |
| 136 | break; |
| 137 | case Description::TransferFunction::SRGB: |
| 138 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB); |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | switch (description.mOutputTransferFunction) { |
| 143 | case Description::TransferFunction::LINEAR: |
| 144 | default: |
| 145 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR); |
| 146 | break; |
| 147 | case Description::TransferFunction::SRGB: |
| 148 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 153 | return needs; |
| 154 | } |
| 155 | |
| 156 | String8 ProgramCache::generateVertexShader(const Key& needs) { |
| 157 | Formatter vs; |
| 158 | if (needs.isTexturing()) { |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 159 | vs << "attribute vec4 texCoords;" |
| 160 | << "varying vec2 outTexCoords;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 161 | } |
| 162 | vs << "attribute vec4 position;" |
| 163 | << "uniform mat4 projection;" |
| 164 | << "uniform mat4 texture;" |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 165 | << "void main(void) {" << indent << "gl_Position = projection * position;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 166 | if (needs.isTexturing()) { |
| 167 | vs << "outTexCoords = (texture * texCoords).st;"; |
| 168 | } |
| 169 | vs << dedent << "}"; |
| 170 | return vs.getString(); |
| 171 | } |
| 172 | |
| 173 | String8 ProgramCache::generateFragmentShader(const Key& needs) { |
| 174 | Formatter fs; |
| 175 | if (needs.getTextureTarget() == Key::TEXTURE_EXT) { |
| 176 | fs << "#extension GL_OES_EGL_image_external : require"; |
| 177 | } |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 178 | |
| 179 | // default precision is required-ish in fragment shaders |
| 180 | fs << "precision mediump float;"; |
| 181 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 182 | if (needs.getTextureTarget() == Key::TEXTURE_EXT) { |
| 183 | fs << "uniform samplerExternalOES sampler;" |
| 184 | << "varying vec2 outTexCoords;"; |
| 185 | } else if (needs.getTextureTarget() == Key::TEXTURE_2D) { |
| 186 | fs << "uniform sampler2D sampler;" |
| 187 | << "varying vec2 outTexCoords;"; |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 191 | fs << "uniform vec4 color;"; |
| 192 | } |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 193 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 194 | if (needs.hasColorMatrix()) { |
| 195 | fs << "uniform mat4 colorMatrix;"; |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 196 | |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame^] | 197 | switch (needs.getInputTF()) { |
| 198 | case Key::INPUT_TF_LINEAR: |
| 199 | default: |
| 200 | fs << R"__SHADER__( |
| 201 | vec3 EOTF(const vec3 linear) { |
| 202 | return linear; |
| 203 | } |
| 204 | )__SHADER__"; |
| 205 | break; |
| 206 | case Key::INPUT_TF_SRGB: |
| 207 | fs << R"__SHADER__( |
| 208 | float EOTF_sRGB(float srgb) { |
| 209 | return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4); |
| 210 | } |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 211 | |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame^] | 212 | vec3 EOTF_sRGB(const vec3 srgb) { |
| 213 | return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b)); |
| 214 | } |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 215 | |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame^] | 216 | vec3 EOTF(const vec3 srgb) { |
| 217 | return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb)); |
| 218 | } |
| 219 | )__SHADER__"; |
| 220 | break; |
| 221 | } |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 222 | |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame^] | 223 | switch (needs.getOutputTF()) { |
| 224 | case Key::OUTPUT_TF_LINEAR: |
| 225 | default: |
| 226 | fs << R"__SHADER__( |
| 227 | vec3 OETF(const vec3 linear) { |
| 228 | return linear; |
| 229 | } |
| 230 | )__SHADER__"; |
| 231 | break; |
| 232 | case Key::OUTPUT_TF_SRGB: |
| 233 | fs << R"__SHADER__( |
| 234 | float OETF_sRGB(const float linear) { |
| 235 | return linear <= 0.0031308 ? |
| 236 | linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055; |
| 237 | } |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 238 | |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame^] | 239 | vec3 OETF_sRGB(const vec3 linear) { |
| 240 | return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); |
| 241 | } |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 242 | |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame^] | 243 | vec3 OETF(const vec3 linear) { |
| 244 | return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)); |
| 245 | } |
| 246 | )__SHADER__"; |
| 247 | break; |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame^] | 250 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 251 | fs << "void main(void) {" << indent; |
| 252 | if (needs.isTexturing()) { |
| 253 | fs << "gl_FragColor = texture2D(sampler, outTexCoords);"; |
| 254 | } else { |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 255 | fs << "gl_FragColor.rgb = color.rgb;"; |
| 256 | fs << "gl_FragColor.a = 1.0;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 257 | } |
Mathias Agopian | 2eaefe1 | 2013-08-14 16:33:27 -0700 | [diff] [blame] | 258 | if (needs.isOpaque()) { |
| 259 | fs << "gl_FragColor.a = 1.0;"; |
| 260 | } |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 261 | if (needs.hasAlpha()) { |
| 262 | // modulate the current alpha value with alpha set |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 263 | if (needs.isPremultiplied()) { |
| 264 | // ... and the color too if we're premultiplied |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 265 | fs << "gl_FragColor *= color.a;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 266 | } else { |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 267 | fs << "gl_FragColor.a *= color.a;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 270 | |
| 271 | if (needs.hasColorMatrix()) { |
| 272 | if (!needs.isOpaque() && needs.isPremultiplied()) { |
| 273 | // un-premultiply if needed before linearization |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 274 | // avoid divide by 0 by adding 0.5/256 to the alpha channel |
| 275 | fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);"; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 276 | } |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame^] | 277 | fs << "vec4 transformed = colorMatrix * vec4(EOTF(gl_FragColor.rgb), 1);"; |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 278 | // We assume the last row is always {0,0,0,1} and we skip the division by w |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame^] | 279 | fs << "gl_FragColor.rgb = OETF(transformed.rgb);"; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 280 | if (!needs.isOpaque() && needs.isPremultiplied()) { |
| 281 | // and re-premultiply if needed after gamma correction |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 282 | fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);"; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 286 | fs << dedent << "}"; |
| 287 | return fs.getString(); |
| 288 | } |
| 289 | |
| 290 | Program* ProgramCache::generateProgram(const Key& needs) { |
| 291 | // vertex shader |
| 292 | String8 vs = generateVertexShader(needs); |
| 293 | |
| 294 | // fragment shader |
| 295 | String8 fs = generateFragmentShader(needs); |
| 296 | |
| 297 | Program* program = new Program(needs, vs.string(), fs.string()); |
| 298 | return program; |
| 299 | } |
| 300 | |
| 301 | void ProgramCache::useProgram(const Description& description) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 302 | // generate the key for the shader based on the description |
| 303 | Key needs(computeKey(description)); |
| 304 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 305 | // look-up the program in the cache |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 306 | Program* program = mCache.valueFor(needs); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 307 | if (program == nullptr) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 308 | // we didn't find our program, so generate one... |
| 309 | nsecs_t time = -systemTime(); |
| 310 | program = generateProgram(needs); |
| 311 | mCache.add(needs, program); |
| 312 | time += systemTime(); |
| 313 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 314 | // ALOGD(">>> generated new program: needs=%08X, time=%u ms (%d programs)", |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 315 | // needs.mNeeds, uint32_t(ns2ms(time)), mCache.size()); |
| 316 | } |
| 317 | |
| 318 | // here we have a suitable program for this description |
| 319 | if (program->isValid()) { |
| 320 | program->use(); |
| 321 | program->setUniforms(description); |
| 322 | } |
| 323 | } |
| 324 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 325 | } /* namespace android */ |