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 | |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame^] | 19 | #include <renderengine/ProgramCache.h> |
| 20 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 21 | #include <GLES2/gl2.h> |
| 22 | #include <GLES2/gl2ext.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame^] | 23 | #include <renderengine/Description.h> |
| 24 | #include <renderengine/Program.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 25 | #include <utils/String8.h> |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 26 | #include <utils/Trace.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 27 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | // ----------------------------------------------------------------------------------------------- |
| 30 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 31 | /* |
| 32 | * A simple formatter class to automatically add the endl and |
| 33 | * manage the indentation. |
| 34 | */ |
| 35 | |
| 36 | class Formatter; |
| 37 | static Formatter& indent(Formatter& f); |
| 38 | static Formatter& dedent(Formatter& f); |
| 39 | |
| 40 | class Formatter { |
| 41 | String8 mString; |
| 42 | int mIndent; |
| 43 | typedef Formatter& (*FormaterManipFunc)(Formatter&); |
| 44 | friend Formatter& indent(Formatter& f); |
| 45 | friend Formatter& dedent(Formatter& f); |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 46 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 47 | public: |
Andy McFadden | 892f22d | 2013-08-15 10:05:01 -0700 | [diff] [blame] | 48 | Formatter() : mIndent(0) {} |
| 49 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 50 | String8 getString() const { return mString; } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 51 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 52 | friend Formatter& operator<<(Formatter& out, const char* in) { |
| 53 | for (int i = 0; i < out.mIndent; i++) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 54 | out.mString.append(" "); |
| 55 | } |
| 56 | out.mString.append(in); |
| 57 | out.mString.append("\n"); |
| 58 | return out; |
| 59 | } |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 60 | friend inline Formatter& operator<<(Formatter& out, const String8& in) { |
| 61 | return operator<<(out, in.string()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 62 | } |
| 63 | friend inline Formatter& operator<<(Formatter& to, FormaterManipFunc func) { |
| 64 | return (*func)(to); |
| 65 | } |
| 66 | }; |
| 67 | Formatter& indent(Formatter& f) { |
| 68 | f.mIndent++; |
| 69 | return f; |
| 70 | } |
| 71 | Formatter& dedent(Formatter& f) { |
| 72 | f.mIndent--; |
| 73 | return f; |
| 74 | } |
| 75 | |
| 76 | // ----------------------------------------------------------------------------------------------- |
| 77 | |
| 78 | ANDROID_SINGLETON_STATIC_INSTANCE(ProgramCache) |
| 79 | |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 80 | ProgramCache::ProgramCache() {} |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 81 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 82 | ProgramCache::~ProgramCache() {} |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 83 | |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 84 | void ProgramCache::primeCache(bool useColorManagement) { |
Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 85 | uint32_t shaderCount = 0; |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 86 | 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] | 87 | // Prime the cache for all combinations of the above masks, |
| 88 | // leaving off the experimental color matrix mask options. |
| 89 | |
| 90 | nsecs_t timeBefore = systemTime(); |
| 91 | for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) { |
| 92 | Key shaderKey; |
| 93 | shaderKey.set(keyMask, keyVal); |
| 94 | uint32_t tex = shaderKey.getTextureTarget(); |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 95 | 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] | 96 | continue; |
| 97 | } |
| 98 | Program* program = mCache.valueFor(shaderKey); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 99 | if (program == nullptr) { |
Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 100 | program = generateProgram(shaderKey); |
| 101 | mCache.add(shaderKey, program); |
| 102 | shaderCount++; |
| 103 | } |
| 104 | } |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 105 | |
| 106 | // Prime for sRGB->P3 conversion |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 107 | if (useColorManagement) { |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 108 | Key shaderKey; |
| 109 | shaderKey.set(Key::BLEND_MASK | Key::TEXTURE_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK | |
| 110 | Key::INPUT_TF_MASK | Key::OUTPUT_TF_MASK, |
| 111 | Key::BLEND_PREMULT | Key::TEXTURE_EXT | Key::OUTPUT_TRANSFORM_MATRIX_ON | |
| 112 | Key::INPUT_TF_SRGB | Key::OUTPUT_TF_SRGB); |
| 113 | for (int i = 0; i < 4; i++) { |
| 114 | shaderKey.set(Key::OPACITY_MASK, |
| 115 | (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT); |
| 116 | shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE); |
| 117 | Program* program = mCache.valueFor(shaderKey); |
| 118 | if (program == nullptr) { |
| 119 | program = generateProgram(shaderKey); |
| 120 | mCache.add(shaderKey, program); |
| 121 | shaderCount++; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 126 | nsecs_t timeAfter = systemTime(); |
| 127 | float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6; |
| 128 | ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs); |
| 129 | } |
| 130 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 131 | ProgramCache::Key ProgramCache::computeKey(const Description& description) { |
| 132 | Key needs; |
| 133 | needs.set(Key::TEXTURE_MASK, |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 134 | !description.mTextureEnabled |
| 135 | ? Key::TEXTURE_OFF |
| 136 | : description.mTexture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES |
| 137 | ? Key::TEXTURE_EXT |
| 138 | : description.mTexture.getTextureTarget() == GL_TEXTURE_2D |
| 139 | ? Key::TEXTURE_2D |
| 140 | : Key::TEXTURE_OFF) |
| 141 | .set(Key::ALPHA_MASK, |
| 142 | (description.mColor.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE) |
| 143 | .set(Key::BLEND_MASK, |
| 144 | description.mPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL) |
| 145 | .set(Key::OPACITY_MASK, |
| 146 | description.mOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT) |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 147 | .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK, |
| 148 | description.hasInputTransformMatrix() ? |
| 149 | Key::INPUT_TRANSFORM_MATRIX_ON : Key::INPUT_TRANSFORM_MATRIX_OFF) |
| 150 | .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK, |
Chia-I Wu | d49d669 | 2018-06-27 07:17:41 +0800 | [diff] [blame] | 151 | description.hasOutputTransformMatrix() || description.hasColorMatrix() ? |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 152 | Key::OUTPUT_TRANSFORM_MATRIX_ON : Key::OUTPUT_TRANSFORM_MATRIX_OFF); |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 153 | |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 154 | needs.set(Key::Y410_BT2020_MASK, |
| 155 | description.mY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF); |
| 156 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 157 | if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) { |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 158 | switch (description.mInputTransferFunction) { |
| 159 | case Description::TransferFunction::LINEAR: |
| 160 | default: |
| 161 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR); |
| 162 | break; |
| 163 | case Description::TransferFunction::SRGB: |
| 164 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB); |
| 165 | break; |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 166 | case Description::TransferFunction::ST2084: |
| 167 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084); |
| 168 | break; |
Peiyong Lin | a3fb7d6 | 2018-04-11 17:41:47 -0700 | [diff] [blame] | 169 | case Description::TransferFunction::HLG: |
| 170 | needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG); |
| 171 | break; |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | switch (description.mOutputTransferFunction) { |
| 175 | case Description::TransferFunction::LINEAR: |
| 176 | default: |
| 177 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR); |
| 178 | break; |
| 179 | case Description::TransferFunction::SRGB: |
| 180 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB); |
| 181 | break; |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 182 | case Description::TransferFunction::ST2084: |
| 183 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084); |
| 184 | break; |
Peiyong Lin | a3fb7d6 | 2018-04-11 17:41:47 -0700 | [diff] [blame] | 185 | case Description::TransferFunction::HLG: |
| 186 | needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG); |
| 187 | break; |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 191 | return needs; |
| 192 | } |
| 193 | |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 194 | // Generate EOTF that converts signal values to relative display light, |
| 195 | // both normalized to [0, 1]. |
| 196 | void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) { |
| 197 | switch (needs.getInputTF()) { |
| 198 | case Key::INPUT_TF_SRGB: |
| 199 | fs << R"__SHADER__( |
| 200 | float EOTF_sRGB(float srgb) { |
| 201 | return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4); |
| 202 | } |
| 203 | |
| 204 | vec3 EOTF_sRGB(const vec3 srgb) { |
| 205 | return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b)); |
| 206 | } |
| 207 | |
| 208 | vec3 EOTF(const vec3 srgb) { |
| 209 | return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb)); |
| 210 | } |
| 211 | )__SHADER__"; |
| 212 | break; |
| 213 | case Key::INPUT_TF_ST2084: |
| 214 | fs << R"__SHADER__( |
| 215 | vec3 EOTF(const highp vec3 color) { |
| 216 | const highp float m1 = (2610.0 / 4096.0) / 4.0; |
| 217 | const highp float m2 = (2523.0 / 4096.0) * 128.0; |
| 218 | const highp float c1 = (3424.0 / 4096.0); |
| 219 | const highp float c2 = (2413.0 / 4096.0) * 32.0; |
| 220 | const highp float c3 = (2392.0 / 4096.0) * 32.0; |
| 221 | |
| 222 | highp vec3 tmp = pow(color, 1.0 / vec3(m2)); |
| 223 | tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp); |
| 224 | return pow(tmp, 1.0 / vec3(m1)); |
| 225 | } |
| 226 | )__SHADER__"; |
| 227 | break; |
| 228 | case Key::INPUT_TF_HLG: |
| 229 | fs << R"__SHADER__( |
| 230 | highp float EOTF_channel(const highp float channel) { |
| 231 | const highp float a = 0.17883277; |
| 232 | const highp float b = 0.28466892; |
| 233 | const highp float c = 0.55991073; |
| 234 | return channel <= 0.5 ? channel * channel / 3.0 : |
| 235 | (exp((channel - c) / a) + b) / 12.0; |
| 236 | } |
| 237 | |
| 238 | vec3 EOTF(const highp vec3 color) { |
| 239 | return vec3(EOTF_channel(color.r), EOTF_channel(color.g), |
| 240 | EOTF_channel(color.b)); |
| 241 | } |
| 242 | )__SHADER__"; |
| 243 | break; |
| 244 | default: |
| 245 | fs << R"__SHADER__( |
| 246 | vec3 EOTF(const vec3 linear) { |
| 247 | return linear; |
| 248 | } |
| 249 | )__SHADER__"; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 254 | void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) { |
| 255 | // Convert relative light to absolute light. |
| 256 | switch (needs.getInputTF()) { |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 257 | case Key::INPUT_TF_ST2084: |
| 258 | fs << R"__SHADER__( |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 259 | highp vec3 ScaleLuminance(highp vec3 color) { |
| 260 | return color * 10000.0; |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 261 | } |
| 262 | )__SHADER__"; |
| 263 | break; |
| 264 | case Key::INPUT_TF_HLG: |
| 265 | fs << R"__SHADER__( |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 266 | highp vec3 ScaleLuminance(highp vec3 color) { |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 267 | // The formula is: |
| 268 | // alpha * pow(Y, gamma - 1.0) * color + beta; |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 269 | // where alpha is 1000.0, gamma is 1.2, beta is 0.0. |
| 270 | return color * 1000.0 * pow(color.y, 0.2); |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 271 | } |
| 272 | )__SHADER__"; |
| 273 | break; |
| 274 | default: |
| 275 | fs << R"__SHADER__( |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 276 | highp vec3 ScaleLuminance(highp vec3 color) { |
| 277 | return color * displayMaxLuminance; |
| 278 | } |
| 279 | )__SHADER__"; |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | // Tone map absolute light to display luminance range. |
| 284 | switch (needs.getInputTF()) { |
| 285 | case Key::INPUT_TF_ST2084: |
| 286 | case Key::INPUT_TF_HLG: |
| 287 | switch (needs.getOutputTF()) { |
| 288 | case Key::OUTPUT_TF_HLG: |
| 289 | // Right now when mixed PQ and HLG contents are presented, |
| 290 | // HLG content will always be converted to PQ. However, for |
| 291 | // completeness, we simply clamp the value to [0.0, 1000.0]. |
| 292 | fs << R"__SHADER__( |
| 293 | highp vec3 ToneMap(highp vec3 color) { |
| 294 | return clamp(color, 0.0, 1000.0); |
| 295 | } |
| 296 | )__SHADER__"; |
| 297 | break; |
| 298 | case Key::OUTPUT_TF_ST2084: |
| 299 | fs << R"__SHADER__( |
| 300 | highp vec3 ToneMap(highp vec3 color) { |
| 301 | return color; |
| 302 | } |
| 303 | )__SHADER__"; |
| 304 | break; |
| 305 | default: |
| 306 | fs << R"__SHADER__( |
| 307 | highp vec3 ToneMap(highp vec3 color) { |
| 308 | const float maxMasteringLumi = 1000.0; |
| 309 | const float maxContentLumi = 1000.0; |
| 310 | const float maxInLumi = min(maxMasteringLumi, maxContentLumi); |
| 311 | float maxOutLumi = displayMaxLuminance; |
| 312 | |
| 313 | float nits = color.y; |
| 314 | |
| 315 | // clamp to max input luminance |
| 316 | nits = clamp(nits, 0.0, maxInLumi); |
| 317 | |
| 318 | // scale [0.0, maxInLumi] to [0.0, maxOutLumi] |
| 319 | if (maxInLumi <= maxOutLumi) { |
| 320 | nits *= maxOutLumi / maxInLumi; |
| 321 | } else { |
| 322 | // three control points |
| 323 | const float x0 = 10.0; |
| 324 | const float y0 = 17.0; |
| 325 | float x1 = maxOutLumi * 0.75; |
| 326 | float y1 = x1; |
| 327 | float x2 = x1 + (maxInLumi - x1) / 2.0; |
| 328 | float y2 = y1 + (maxOutLumi - y1) * 0.75; |
| 329 | |
| 330 | // horizontal distances between the last three control points |
Peiyong Lin | f2410b6 | 2018-05-14 16:31:17 -0700 | [diff] [blame] | 331 | float h12 = x2 - x1; |
| 332 | float h23 = maxInLumi - x2; |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 333 | // tangents at the last three control points |
Peiyong Lin | f2410b6 | 2018-05-14 16:31:17 -0700 | [diff] [blame] | 334 | float m1 = (y2 - y1) / h12; |
| 335 | float m3 = (maxOutLumi - y2) / h23; |
| 336 | float m2 = (m1 + m3) / 2.0; |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 337 | |
| 338 | if (nits < x0) { |
| 339 | // scale [0.0, x0] to [0.0, y0] linearly |
Peiyong Lin | f2410b6 | 2018-05-14 16:31:17 -0700 | [diff] [blame] | 340 | float slope = y0 / x0; |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 341 | nits *= slope; |
| 342 | } else if (nits < x1) { |
| 343 | // scale [x0, x1] to [y0, y1] linearly |
Peiyong Lin | f2410b6 | 2018-05-14 16:31:17 -0700 | [diff] [blame] | 344 | float slope = (y1 - y0) / (x1 - x0); |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 345 | nits = y0 + (nits - x0) * slope; |
| 346 | } else if (nits < x2) { |
| 347 | // scale [x1, x2] to [y1, y2] using Hermite interp |
| 348 | float t = (nits - x1) / h12; |
| 349 | nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) + |
| 350 | (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t; |
| 351 | } else { |
| 352 | // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp |
| 353 | float t = (nits - x2) / h23; |
| 354 | nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) + |
| 355 | (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | return color * (nits / max(1e-6, color.y)); |
| 360 | } |
| 361 | )__SHADER__"; |
| 362 | break; |
| 363 | } |
| 364 | break; |
| 365 | default: |
Peiyong Lin | 9a1b655 | 2018-05-23 16:52:29 -0700 | [diff] [blame] | 366 | // inverse tone map; the output luminance can be up to maxOutLumi. |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 367 | fs << R"__SHADER__( |
| 368 | highp vec3 ToneMap(highp vec3 color) { |
Peiyong Lin | 9a1b655 | 2018-05-23 16:52:29 -0700 | [diff] [blame] | 369 | const float maxOutLumi = 3000.0; |
| 370 | |
| 371 | const float x0 = 5.0; |
| 372 | const float y0 = 2.5; |
| 373 | float x1 = displayMaxLuminance * 0.7; |
| 374 | float y1 = maxOutLumi * 0.15; |
| 375 | float x2 = displayMaxLuminance * 0.9; |
| 376 | float y2 = maxOutLumi * 0.45; |
| 377 | float x3 = displayMaxLuminance; |
| 378 | float y3 = maxOutLumi; |
| 379 | |
| 380 | float c1 = y1 / 3.0; |
| 381 | float c2 = y2 / 2.0; |
| 382 | float c3 = y3 / 1.5; |
| 383 | |
| 384 | float nits = color.y; |
| 385 | |
| 386 | float scale; |
| 387 | if (nits <= x0) { |
| 388 | // scale [0.0, x0] to [0.0, y0] linearly |
| 389 | const float slope = y0 / x0; |
| 390 | nits *= slope; |
| 391 | } else if (nits <= x1) { |
| 392 | // scale [x0, x1] to [y0, y1] using a curve |
| 393 | float t = (nits - x0) / (x1 - x0); |
| 394 | nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1; |
| 395 | } else if (nits <= x2) { |
| 396 | // scale [x1, x2] to [y1, y2] using a curve |
| 397 | float t = (nits - x1) / (x2 - x1); |
| 398 | nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2; |
| 399 | } else { |
| 400 | // scale [x2, x3] to [y2, y3] using a curve |
| 401 | float t = (nits - x2) / (x3 - x2); |
| 402 | nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3; |
| 403 | } |
| 404 | |
| 405 | return color * (nits / max(1e-6, color.y)); |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 406 | } |
| 407 | )__SHADER__"; |
| 408 | break; |
| 409 | } |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 410 | |
| 411 | // convert absolute light to relative light. |
| 412 | switch (needs.getOutputTF()) { |
| 413 | case Key::OUTPUT_TF_ST2084: |
| 414 | fs << R"__SHADER__( |
| 415 | highp vec3 NormalizeLuminance(highp vec3 color) { |
| 416 | return color / 10000.0; |
| 417 | } |
| 418 | )__SHADER__"; |
| 419 | break; |
| 420 | case Key::OUTPUT_TF_HLG: |
| 421 | fs << R"__SHADER__( |
| 422 | highp vec3 NormalizeLuminance(highp vec3 color) { |
| 423 | return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2); |
| 424 | } |
| 425 | )__SHADER__"; |
| 426 | break; |
| 427 | default: |
| 428 | fs << R"__SHADER__( |
| 429 | highp vec3 NormalizeLuminance(highp vec3 color) { |
| 430 | return color / displayMaxLuminance; |
| 431 | } |
| 432 | )__SHADER__"; |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | // Generate OOTF that modifies the relative scence light to relative display light. |
| 438 | void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) { |
| 439 | if (!needs.needsToneMapping()) { |
| 440 | fs << R"__SHADER__( |
| 441 | highp vec3 OOTF(const highp vec3 color) { |
| 442 | return color; |
| 443 | } |
| 444 | )__SHADER__"; |
| 445 | } else { |
| 446 | generateToneMappingProcess(fs, needs); |
| 447 | fs << R"__SHADER__( |
| 448 | highp vec3 OOTF(const highp vec3 color) { |
| 449 | return NormalizeLuminance(ToneMap(ScaleLuminance(color))); |
| 450 | } |
| 451 | )__SHADER__"; |
| 452 | } |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // Generate OETF that converts relative display light to signal values, |
| 456 | // both normalized to [0, 1] |
| 457 | void ProgramCache::generateOETF(Formatter& fs, const Key& needs) { |
| 458 | switch (needs.getOutputTF()) { |
| 459 | case Key::OUTPUT_TF_SRGB: |
| 460 | fs << R"__SHADER__( |
| 461 | float OETF_sRGB(const float linear) { |
| 462 | return linear <= 0.0031308 ? |
| 463 | linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055; |
| 464 | } |
| 465 | |
| 466 | vec3 OETF_sRGB(const vec3 linear) { |
| 467 | return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); |
| 468 | } |
| 469 | |
| 470 | vec3 OETF(const vec3 linear) { |
| 471 | return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)); |
| 472 | } |
| 473 | )__SHADER__"; |
| 474 | break; |
| 475 | case Key::OUTPUT_TF_ST2084: |
| 476 | fs << R"__SHADER__( |
| 477 | vec3 OETF(const vec3 linear) { |
Peiyong Lin | 834be49 | 2018-05-18 15:12:55 -0700 | [diff] [blame] | 478 | const highp float m1 = (2610.0 / 4096.0) / 4.0; |
| 479 | const highp float m2 = (2523.0 / 4096.0) * 128.0; |
| 480 | const highp float c1 = (3424.0 / 4096.0); |
| 481 | const highp float c2 = (2413.0 / 4096.0) * 32.0; |
| 482 | const highp float c3 = (2392.0 / 4096.0) * 32.0; |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 483 | |
Peiyong Lin | 834be49 | 2018-05-18 15:12:55 -0700 | [diff] [blame] | 484 | highp vec3 tmp = pow(linear, vec3(m1)); |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 485 | tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp); |
| 486 | return pow(tmp, vec3(m2)); |
| 487 | } |
| 488 | )__SHADER__"; |
| 489 | break; |
| 490 | case Key::OUTPUT_TF_HLG: |
| 491 | fs << R"__SHADER__( |
| 492 | highp float OETF_channel(const highp float channel) { |
| 493 | const highp float a = 0.17883277; |
| 494 | const highp float b = 0.28466892; |
| 495 | const highp float c = 0.55991073; |
| 496 | return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) : |
| 497 | a * log(12.0 * channel - b) + c; |
| 498 | } |
| 499 | |
| 500 | vec3 OETF(const highp vec3 color) { |
| 501 | return vec3(OETF_channel(color.r), OETF_channel(color.g), |
| 502 | OETF_channel(color.b)); |
| 503 | } |
| 504 | )__SHADER__"; |
| 505 | break; |
| 506 | default: |
| 507 | fs << R"__SHADER__( |
| 508 | vec3 OETF(const vec3 linear) { |
| 509 | return linear; |
| 510 | } |
| 511 | )__SHADER__"; |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 516 | String8 ProgramCache::generateVertexShader(const Key& needs) { |
| 517 | Formatter vs; |
| 518 | if (needs.isTexturing()) { |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 519 | vs << "attribute vec4 texCoords;" |
| 520 | << "varying vec2 outTexCoords;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 521 | } |
| 522 | vs << "attribute vec4 position;" |
| 523 | << "uniform mat4 projection;" |
| 524 | << "uniform mat4 texture;" |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 525 | << "void main(void) {" << indent << "gl_Position = projection * position;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 526 | if (needs.isTexturing()) { |
| 527 | vs << "outTexCoords = (texture * texCoords).st;"; |
| 528 | } |
| 529 | vs << dedent << "}"; |
| 530 | return vs.getString(); |
| 531 | } |
| 532 | |
| 533 | String8 ProgramCache::generateFragmentShader(const Key& needs) { |
| 534 | Formatter fs; |
| 535 | if (needs.getTextureTarget() == Key::TEXTURE_EXT) { |
| 536 | fs << "#extension GL_OES_EGL_image_external : require"; |
| 537 | } |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 538 | |
| 539 | // default precision is required-ish in fragment shaders |
| 540 | fs << "precision mediump float;"; |
| 541 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 542 | if (needs.getTextureTarget() == Key::TEXTURE_EXT) { |
| 543 | fs << "uniform samplerExternalOES sampler;" |
| 544 | << "varying vec2 outTexCoords;"; |
| 545 | } else if (needs.getTextureTarget() == Key::TEXTURE_2D) { |
| 546 | fs << "uniform sampler2D sampler;" |
| 547 | << "varying vec2 outTexCoords;"; |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 551 | fs << "uniform vec4 color;"; |
| 552 | } |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 553 | |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 554 | if (needs.isY410BT2020()) { |
| 555 | fs << R"__SHADER__( |
| 556 | vec3 convertY410BT2020(const vec3 color) { |
| 557 | const vec3 offset = vec3(0.0625, 0.5, 0.5); |
| 558 | const mat3 transform = mat3( |
| 559 | vec3(1.1678, 1.1678, 1.1678), |
| 560 | vec3( 0.0, -0.1878, 2.1481), |
| 561 | vec3(1.6836, -0.6523, 0.0)); |
| 562 | // Y is in G, U is in R, and V is in B |
| 563 | return clamp(transform * (color.grb - offset), 0.0, 1.0); |
| 564 | } |
| 565 | )__SHADER__"; |
| 566 | } |
| 567 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 568 | if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) { |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 569 | // Currently, display maximum luminance is needed when doing tone mapping. |
| 570 | if (needs.needsToneMapping()) { |
Chia-I Wu | 8bbacdf | 2018-05-04 15:19:37 -0700 | [diff] [blame] | 571 | fs << "uniform float displayMaxLuminance;"; |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 572 | } |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 573 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 574 | if (needs.hasInputTransformMatrix()) { |
Peiyong Lin | 76dd77a | 2018-05-09 15:35:33 -0700 | [diff] [blame] | 575 | fs << "uniform mat4 inputTransformMatrix;"; |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 576 | fs << R"__SHADER__( |
| 577 | highp vec3 InputTransform(const highp vec3 color) { |
Peiyong Lin | 76dd77a | 2018-05-09 15:35:33 -0700 | [diff] [blame] | 578 | return vec3(inputTransformMatrix * vec4(color, 1.0)); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 579 | } |
| 580 | )__SHADER__"; |
| 581 | } else { |
| 582 | fs << R"__SHADER__( |
| 583 | highp vec3 InputTransform(const highp vec3 color) { |
| 584 | return color; |
| 585 | } |
| 586 | )__SHADER__"; |
| 587 | } |
| 588 | |
| 589 | // the transformation from a wider colorspace to a narrower one can |
| 590 | // result in >1.0 or <0.0 pixel values |
| 591 | if (needs.hasOutputTransformMatrix()) { |
| 592 | fs << "uniform mat4 outputTransformMatrix;"; |
| 593 | fs << R"__SHADER__( |
| 594 | highp vec3 OutputTransform(const highp vec3 color) { |
| 595 | return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0); |
| 596 | } |
| 597 | )__SHADER__"; |
| 598 | } else { |
| 599 | fs << R"__SHADER__( |
| 600 | highp vec3 OutputTransform(const highp vec3 color) { |
| 601 | return clamp(color, 0.0, 1.0); |
| 602 | } |
| 603 | )__SHADER__"; |
| 604 | } |
| 605 | |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 606 | generateEOTF(fs, needs); |
| 607 | generateOOTF(fs, needs); |
| 608 | generateOETF(fs, needs); |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 609 | } |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 610 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 611 | fs << "void main(void) {" << indent; |
| 612 | if (needs.isTexturing()) { |
| 613 | fs << "gl_FragColor = texture2D(sampler, outTexCoords);"; |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 614 | if (needs.isY410BT2020()) { |
| 615 | fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);"; |
| 616 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 617 | } else { |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 618 | fs << "gl_FragColor.rgb = color.rgb;"; |
| 619 | fs << "gl_FragColor.a = 1.0;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 620 | } |
Mathias Agopian | 2eaefe1 | 2013-08-14 16:33:27 -0700 | [diff] [blame] | 621 | if (needs.isOpaque()) { |
| 622 | fs << "gl_FragColor.a = 1.0;"; |
| 623 | } |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 624 | if (needs.hasAlpha()) { |
| 625 | // modulate the current alpha value with alpha set |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 626 | if (needs.isPremultiplied()) { |
| 627 | // ... and the color too if we're premultiplied |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 628 | fs << "gl_FragColor *= color.a;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 629 | } else { |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 630 | fs << "gl_FragColor.a *= color.a;"; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 631 | } |
| 632 | } |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 633 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 634 | if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) { |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 635 | if (!needs.isOpaque() && needs.isPremultiplied()) { |
| 636 | // un-premultiply if needed before linearization |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 637 | // avoid divide by 0 by adding 0.5/256 to the alpha channel |
| 638 | fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);"; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 639 | } |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 640 | fs << "gl_FragColor.rgb = OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));"; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 641 | if (!needs.isOpaque() && needs.isPremultiplied()) { |
| 642 | // and re-premultiply if needed after gamma correction |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 643 | fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);"; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 647 | fs << dedent << "}"; |
| 648 | return fs.getString(); |
| 649 | } |
| 650 | |
| 651 | Program* ProgramCache::generateProgram(const Key& needs) { |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 652 | ATRACE_CALL(); |
| 653 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 654 | // vertex shader |
| 655 | String8 vs = generateVertexShader(needs); |
| 656 | |
| 657 | // fragment shader |
| 658 | String8 fs = generateFragmentShader(needs); |
| 659 | |
| 660 | Program* program = new Program(needs, vs.string(), fs.string()); |
| 661 | return program; |
| 662 | } |
| 663 | |
| 664 | void ProgramCache::useProgram(const Description& description) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 665 | // generate the key for the shader based on the description |
| 666 | Key needs(computeKey(description)); |
| 667 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 668 | // look-up the program in the cache |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 669 | Program* program = mCache.valueFor(needs); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 670 | if (program == nullptr) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 671 | // we didn't find our program, so generate one... |
| 672 | nsecs_t time = -systemTime(); |
| 673 | program = generateProgram(needs); |
| 674 | mCache.add(needs, program); |
| 675 | time += systemTime(); |
| 676 | |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 677 | ALOGV(">>> generated new program: needs=%08X, time=%u ms (%zu programs)", needs.mKey, |
| 678 | uint32_t(ns2ms(time)), mCache.size()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | // here we have a suitable program for this description |
| 682 | if (program->isValid()) { |
| 683 | program->use(); |
| 684 | program->setUniforms(description); |
| 685 | } |
| 686 | } |
| 687 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 688 | } /* namespace android */ |