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