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