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