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) |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame^] | 128 | .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK, |
| 129 | description.hasInputTransformMatrix() ? |
| 130 | Key::INPUT_TRANSFORM_MATRIX_ON : Key::INPUT_TRANSFORM_MATRIX_OFF) |
| 131 | .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK, |
| 132 | description.hasOutputTransformMatrix() || description.hasColorMatrix() ? |
| 133 | Key::OUTPUT_TRANSFORM_MATRIX_ON : Key::OUTPUT_TRANSFORM_MATRIX_OFF); |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 134 | |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 135 | needs.set(Key::Y410_BT2020_MASK, |
| 136 | description.mY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF); |
| 137 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame^] | 138 | if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) { |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 139 | switch (description.mInputTransferFunction) { |
| 140 | case Description::TransferFunction::LINEAR: |
| 141 | default: |
| 142 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR); |
| 143 | break; |
| 144 | case Description::TransferFunction::SRGB: |
| 145 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB); |
| 146 | break; |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 147 | case Description::TransferFunction::ST2084: |
| 148 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084); |
| 149 | break; |
Peiyong Lin | a3fb7d6 | 2018-04-11 17:41:47 -0700 | [diff] [blame] | 150 | case Description::TransferFunction::HLG: |
| 151 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG); |
| 152 | break; |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | switch (description.mOutputTransferFunction) { |
| 156 | case Description::TransferFunction::LINEAR: |
| 157 | default: |
| 158 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR); |
| 159 | break; |
| 160 | case Description::TransferFunction::SRGB: |
| 161 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB); |
| 162 | break; |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 163 | case Description::TransferFunction::ST2084: |
| 164 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084); |
| 165 | break; |
Peiyong Lin | a3fb7d6 | 2018-04-11 17:41:47 -0700 | [diff] [blame] | 166 | case Description::TransferFunction::HLG: |
| 167 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG); |
| 168 | break; |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 172 | return needs; |
| 173 | } |
| 174 | |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 175 | // Generate EOTF that converts signal values to relative display light, |
| 176 | // both normalized to [0, 1]. |
| 177 | void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) { |
| 178 | switch (needs.getInputTF()) { |
| 179 | case Key::INPUT_TF_SRGB: |
| 180 | fs << R"__SHADER__( |
| 181 | float EOTF_sRGB(float srgb) { |
| 182 | return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4); |
| 183 | } |
| 184 | |
| 185 | vec3 EOTF_sRGB(const vec3 srgb) { |
| 186 | return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b)); |
| 187 | } |
| 188 | |
| 189 | vec3 EOTF(const vec3 srgb) { |
| 190 | return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb)); |
| 191 | } |
| 192 | )__SHADER__"; |
| 193 | break; |
| 194 | case Key::INPUT_TF_ST2084: |
| 195 | fs << R"__SHADER__( |
| 196 | vec3 EOTF(const highp vec3 color) { |
| 197 | const highp float m1 = (2610.0 / 4096.0) / 4.0; |
| 198 | const highp float m2 = (2523.0 / 4096.0) * 128.0; |
| 199 | const highp float c1 = (3424.0 / 4096.0); |
| 200 | const highp float c2 = (2413.0 / 4096.0) * 32.0; |
| 201 | const highp float c3 = (2392.0 / 4096.0) * 32.0; |
| 202 | |
| 203 | highp vec3 tmp = pow(color, 1.0 / vec3(m2)); |
| 204 | tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp); |
| 205 | return pow(tmp, 1.0 / vec3(m1)); |
| 206 | } |
| 207 | )__SHADER__"; |
| 208 | break; |
| 209 | case Key::INPUT_TF_HLG: |
| 210 | fs << R"__SHADER__( |
| 211 | highp float EOTF_channel(const highp float channel) { |
| 212 | const highp float a = 0.17883277; |
| 213 | const highp float b = 0.28466892; |
| 214 | const highp float c = 0.55991073; |
| 215 | return channel <= 0.5 ? channel * channel / 3.0 : |
| 216 | (exp((channel - c) / a) + b) / 12.0; |
| 217 | } |
| 218 | |
| 219 | vec3 EOTF(const highp vec3 color) { |
| 220 | return vec3(EOTF_channel(color.r), EOTF_channel(color.g), |
| 221 | EOTF_channel(color.b)); |
| 222 | } |
| 223 | )__SHADER__"; |
| 224 | break; |
| 225 | default: |
| 226 | fs << R"__SHADER__( |
| 227 | vec3 EOTF(const vec3 linear) { |
| 228 | return linear; |
| 229 | } |
| 230 | )__SHADER__"; |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Generate OOTF that modifies the relative scence light to relative display light. |
| 236 | void ProgramCache::generateOOTF(Formatter& fs, const Key& needs) { |
| 237 | fs << R"__SHADER__( |
| 238 | highp float CalculateY(const highp vec3 color) { |
| 239 | // BT2020 standard uses the unadjusted KR = 0.2627, |
| 240 | // KB = 0.0593 luminance interpretation for RGB conversion. |
| 241 | return color.r * 0.262700 + color.g * 0.677998 + color.b * 0.059302; |
| 242 | } |
| 243 | )__SHADER__"; |
| 244 | |
| 245 | // Generate OOTF that modifies the relative display light. |
| 246 | switch(needs.getInputTF()) { |
| 247 | case Key::INPUT_TF_ST2084: |
| 248 | fs << R"__SHADER__( |
| 249 | highp vec3 OOTF(const highp vec3 color) { |
| 250 | const float maxLumi = 10000.0; |
| 251 | const float maxMasteringLumi = 1000.0; |
| 252 | const float maxContentLumi = 1000.0; |
| 253 | const float maxInLumi = min(maxMasteringLumi, maxContentLumi); |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 254 | const float maxOutLumi = displayMaxLuminance; |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 255 | |
| 256 | // Calculate Y value in XYZ color space. |
| 257 | float colorY = CalculateY(color); |
| 258 | |
| 259 | // convert to nits first |
| 260 | float nits = colorY * maxLumi; |
| 261 | |
| 262 | // clamp to max input luminance |
| 263 | nits = clamp(nits, 0.0, maxInLumi); |
| 264 | |
| 265 | // scale [0.0, maxInLumi] to [0.0, maxOutLumi] |
| 266 | if (maxInLumi <= maxOutLumi) { |
| 267 | nits *= maxOutLumi / maxInLumi; |
| 268 | } else { |
| 269 | // three control points |
| 270 | const float x0 = 10.0; |
| 271 | const float y0 = 17.0; |
| 272 | const float x1 = maxOutLumi * 0.75; |
| 273 | const float y1 = x1; |
| 274 | const float x2 = x1 + (maxInLumi - x1) / 2.0; |
| 275 | const float y2 = y1 + (maxOutLumi - y1) * 0.75; |
| 276 | |
| 277 | // horizontal distances between the last three control points |
| 278 | const float h12 = x2 - x1; |
| 279 | const float h23 = maxInLumi - x2; |
| 280 | // tangents at the last three control points |
| 281 | const float m1 = (y2 - y1) / h12; |
| 282 | const float m3 = (maxOutLumi - y2) / h23; |
| 283 | const float m2 = (m1 + m3) / 2.0; |
| 284 | |
| 285 | if (nits < x0) { |
| 286 | // scale [0.0, x0] to [0.0, y0] linearly |
| 287 | const float slope = y0 / x0; |
| 288 | nits *= slope; |
| 289 | } else if (nits < x1) { |
| 290 | // scale [x0, x1] to [y0, y1] linearly |
| 291 | const float slope = (y1 - y0) / (x1 - x0); |
| 292 | nits = y0 + (nits - x0) * slope; |
| 293 | } else if (nits < x2) { |
| 294 | // scale [x1, x2] to [y1, y2] using Hermite interp |
| 295 | float t = (nits - x1) / h12; |
| 296 | nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) + |
| 297 | (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t; |
| 298 | } else { |
| 299 | // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp |
| 300 | float t = (nits - x2) / h23; |
| 301 | nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) + |
| 302 | (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // convert back to [0.0, 1.0] |
| 307 | float targetY = nits / maxOutLumi; |
| 308 | return color * (targetY / max(1e-6, colorY)); |
| 309 | } |
| 310 | )__SHADER__"; |
| 311 | break; |
| 312 | case Key::INPUT_TF_HLG: |
| 313 | fs << R"__SHADER__( |
| 314 | highp vec3 OOTF(const highp vec3 color) { |
| 315 | const float maxOutLumi = 500.0; |
| 316 | const float gamma = 1.2 + 0.42 * log(maxOutLumi / 1000.0) / log(10.0); |
| 317 | // The formula is: |
| 318 | // alpha * pow(Y, gamma - 1.0) * color + beta; |
| 319 | // where alpha is 1.0, beta is 0.0 as recommended in |
| 320 | // Rec. ITU-R BT.2100-1 TABLE 5. |
| 321 | return pow(CalculateY(color), gamma - 1.0) * color; |
| 322 | } |
| 323 | )__SHADER__"; |
| 324 | break; |
| 325 | default: |
| 326 | fs << R"__SHADER__( |
| 327 | highp vec3 OOTF(const highp vec3 color) { |
| 328 | return color; |
| 329 | } |
| 330 | )__SHADER__"; |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // Generate OETF that converts relative display light to signal values, |
| 336 | // both normalized to [0, 1] |
| 337 | void ProgramCache::generateOETF(Formatter& fs, const Key& needs) { |
| 338 | switch (needs.getOutputTF()) { |
| 339 | case Key::OUTPUT_TF_SRGB: |
| 340 | fs << R"__SHADER__( |
| 341 | float OETF_sRGB(const float linear) { |
| 342 | return linear <= 0.0031308 ? |
| 343 | linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055; |
| 344 | } |
| 345 | |
| 346 | vec3 OETF_sRGB(const vec3 linear) { |
| 347 | return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); |
| 348 | } |
| 349 | |
| 350 | vec3 OETF(const vec3 linear) { |
| 351 | return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)); |
| 352 | } |
| 353 | )__SHADER__"; |
| 354 | break; |
| 355 | case Key::OUTPUT_TF_ST2084: |
| 356 | fs << R"__SHADER__( |
| 357 | vec3 OETF(const vec3 linear) { |
| 358 | const float m1 = (2610.0 / 4096.0) / 4.0; |
| 359 | const float m2 = (2523.0 / 4096.0) * 128.0; |
| 360 | const float c1 = (3424.0 / 4096.0); |
| 361 | const float c2 = (2413.0 / 4096.0) * 32.0; |
| 362 | const float c3 = (2392.0 / 4096.0) * 32.0; |
| 363 | |
| 364 | vec3 tmp = pow(linear, vec3(m1)); |
| 365 | tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp); |
| 366 | return pow(tmp, vec3(m2)); |
| 367 | } |
| 368 | )__SHADER__"; |
| 369 | break; |
| 370 | case Key::OUTPUT_TF_HLG: |
| 371 | fs << R"__SHADER__( |
| 372 | highp float OETF_channel(const highp float channel) { |
| 373 | const highp float a = 0.17883277; |
| 374 | const highp float b = 0.28466892; |
| 375 | const highp float c = 0.55991073; |
| 376 | return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) : |
| 377 | a * log(12.0 * channel - b) + c; |
| 378 | } |
| 379 | |
| 380 | vec3 OETF(const highp vec3 color) { |
| 381 | return vec3(OETF_channel(color.r), OETF_channel(color.g), |
| 382 | OETF_channel(color.b)); |
| 383 | } |
| 384 | )__SHADER__"; |
| 385 | break; |
| 386 | default: |
| 387 | fs << R"__SHADER__( |
| 388 | vec3 OETF(const vec3 linear) { |
| 389 | return linear; |
| 390 | } |
| 391 | )__SHADER__"; |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 396 | String8 ProgramCache::generateVertexShader(const Key& needs) { |
| 397 | Formatter vs; |
| 398 | if (needs.isTexturing()) { |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 399 | vs << "attribute vec4 texCoords;" |
| 400 | << "varying vec2 outTexCoords;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 401 | } |
| 402 | vs << "attribute vec4 position;" |
| 403 | << "uniform mat4 projection;" |
| 404 | << "uniform mat4 texture;" |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 405 | << "void main(void) {" << indent << "gl_Position = projection * position;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 406 | if (needs.isTexturing()) { |
| 407 | vs << "outTexCoords = (texture * texCoords).st;"; |
| 408 | } |
| 409 | vs << dedent << "}"; |
| 410 | return vs.getString(); |
| 411 | } |
| 412 | |
| 413 | String8 ProgramCache::generateFragmentShader(const Key& needs) { |
| 414 | Formatter fs; |
| 415 | if (needs.getTextureTarget() == Key::TEXTURE_EXT) { |
| 416 | fs << "#extension GL_OES_EGL_image_external : require"; |
| 417 | } |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 418 | |
| 419 | // default precision is required-ish in fragment shaders |
| 420 | fs << "precision mediump float;"; |
| 421 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 422 | if (needs.getTextureTarget() == Key::TEXTURE_EXT) { |
| 423 | fs << "uniform samplerExternalOES sampler;" |
| 424 | << "varying vec2 outTexCoords;"; |
| 425 | } else if (needs.getTextureTarget() == Key::TEXTURE_2D) { |
| 426 | fs << "uniform sampler2D sampler;" |
| 427 | << "varying vec2 outTexCoords;"; |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 431 | fs << "uniform vec4 color;"; |
| 432 | } |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 433 | |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 434 | if (needs.isY410BT2020()) { |
| 435 | fs << R"__SHADER__( |
| 436 | vec3 convertY410BT2020(const vec3 color) { |
| 437 | const vec3 offset = vec3(0.0625, 0.5, 0.5); |
| 438 | const mat3 transform = mat3( |
| 439 | vec3(1.1678, 1.1678, 1.1678), |
| 440 | vec3( 0.0, -0.1878, 2.1481), |
| 441 | vec3(1.6836, -0.6523, 0.0)); |
| 442 | // Y is in G, U is in R, and V is in B |
| 443 | return clamp(transform * (color.grb - offset), 0.0, 1.0); |
| 444 | } |
| 445 | )__SHADER__"; |
| 446 | } |
| 447 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame^] | 448 | if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) { |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 449 | // Currently, only the OOTF of BT2020 PQ needs display maximum luminance. |
| 450 | if (needs.getInputTF() == Key::INPUT_TF_ST2084) { |
| 451 | fs << "uniform float displayMaxLuminance"; |
| 452 | } |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 453 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame^] | 454 | if (needs.hasInputTransformMatrix()) { |
| 455 | fs << "uniform mat3 inputTransformMatrix;"; |
| 456 | fs << R"__SHADER__( |
| 457 | highp vec3 InputTransform(const highp vec3 color) { |
| 458 | return inputTransformMatrix * color; |
| 459 | } |
| 460 | )__SHADER__"; |
| 461 | } else { |
| 462 | fs << R"__SHADER__( |
| 463 | highp vec3 InputTransform(const highp vec3 color) { |
| 464 | return color; |
| 465 | } |
| 466 | )__SHADER__"; |
| 467 | } |
| 468 | |
| 469 | // the transformation from a wider colorspace to a narrower one can |
| 470 | // result in >1.0 or <0.0 pixel values |
| 471 | if (needs.hasOutputTransformMatrix()) { |
| 472 | fs << "uniform mat4 outputTransformMatrix;"; |
| 473 | fs << R"__SHADER__( |
| 474 | highp vec3 OutputTransform(const highp vec3 color) { |
| 475 | return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0); |
| 476 | } |
| 477 | )__SHADER__"; |
| 478 | } else { |
| 479 | fs << R"__SHADER__( |
| 480 | highp vec3 OutputTransform(const highp vec3 color) { |
| 481 | return clamp(color, 0.0, 1.0); |
| 482 | } |
| 483 | )__SHADER__"; |
| 484 | } |
| 485 | |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 486 | generateEOTF(fs, needs); |
| 487 | generateOOTF(fs, needs); |
| 488 | generateOETF(fs, needs); |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 489 | } |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 490 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 491 | fs << "void main(void) {" << indent; |
| 492 | if (needs.isTexturing()) { |
| 493 | fs << "gl_FragColor = texture2D(sampler, outTexCoords);"; |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 494 | if (needs.isY410BT2020()) { |
| 495 | fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);"; |
| 496 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 497 | } else { |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 498 | fs << "gl_FragColor.rgb = color.rgb;"; |
| 499 | fs << "gl_FragColor.a = 1.0;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 500 | } |
Mathias Agopian | 2eaefe1 | 2013-08-14 16:33:27 -0700 | [diff] [blame] | 501 | if (needs.isOpaque()) { |
| 502 | fs << "gl_FragColor.a = 1.0;"; |
| 503 | } |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 504 | if (needs.hasAlpha()) { |
| 505 | // modulate the current alpha value with alpha set |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 506 | if (needs.isPremultiplied()) { |
| 507 | // ... and the color too if we're premultiplied |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 508 | fs << "gl_FragColor *= color.a;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 509 | } else { |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 510 | fs << "gl_FragColor.a *= color.a;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 513 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame^] | 514 | if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) { |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 515 | if (!needs.isOpaque() && needs.isPremultiplied()) { |
| 516 | // un-premultiply if needed before linearization |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 517 | // avoid divide by 0 by adding 0.5/256 to the alpha channel |
| 518 | fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);"; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 519 | } |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame^] | 520 | fs << "gl_FragColor.rgb = OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));"; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 521 | if (!needs.isOpaque() && needs.isPremultiplied()) { |
| 522 | // and re-premultiply if needed after gamma correction |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 523 | fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);"; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 527 | fs << dedent << "}"; |
| 528 | return fs.getString(); |
| 529 | } |
| 530 | |
| 531 | Program* ProgramCache::generateProgram(const Key& needs) { |
| 532 | // vertex shader |
| 533 | String8 vs = generateVertexShader(needs); |
| 534 | |
| 535 | // fragment shader |
| 536 | String8 fs = generateFragmentShader(needs); |
| 537 | |
| 538 | Program* program = new Program(needs, vs.string(), fs.string()); |
| 539 | return program; |
| 540 | } |
| 541 | |
| 542 | void ProgramCache::useProgram(const Description& description) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 543 | // generate the key for the shader based on the description |
| 544 | Key needs(computeKey(description)); |
| 545 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 546 | // look-up the program in the cache |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 547 | Program* program = mCache.valueFor(needs); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 548 | if (program == nullptr) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 549 | // we didn't find our program, so generate one... |
| 550 | nsecs_t time = -systemTime(); |
| 551 | program = generateProgram(needs); |
| 552 | mCache.add(needs, program); |
| 553 | time += systemTime(); |
| 554 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 555 | // ALOGD(">>> generated new program: needs=%08X, time=%u ms (%d programs)", |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 556 | // needs.mNeeds, uint32_t(ns2ms(time)), mCache.size()); |
| 557 | } |
| 558 | |
| 559 | // here we have a suitable program for this description |
| 560 | if (program->isValid()) { |
| 561 | program->use(); |
| 562 | program->setUniforms(description); |
| 563 | } |
| 564 | } |
| 565 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 566 | } /* namespace android */ |