Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <shaders/shaders.h> |
| 18 | |
| 19 | #include <tonemap/tonemap.h> |
| 20 | |
| 21 | #include <optional> |
| 22 | |
| 23 | #include <math/mat4.h> |
| 24 | #include <system/graphics-base-v1.0.h> |
| 25 | #include <ui/ColorSpace.h> |
| 26 | |
| 27 | namespace android::shaders { |
| 28 | |
| 29 | static aidl::android::hardware::graphics::common::Dataspace toAidlDataspace( |
| 30 | ui::Dataspace dataspace) { |
| 31 | return static_cast<aidl::android::hardware::graphics::common::Dataspace>(dataspace); |
| 32 | } |
| 33 | |
| 34 | static void generateEOTF(ui::Dataspace dataspace, std::string& shader) { |
| 35 | switch (dataspace & HAL_DATASPACE_TRANSFER_MASK) { |
| 36 | case HAL_DATASPACE_TRANSFER_ST2084: |
| 37 | shader.append(R"( |
| 38 | |
| 39 | float3 EOTF(float3 color) { |
| 40 | float m1 = (2610.0 / 4096.0) / 4.0; |
| 41 | float m2 = (2523.0 / 4096.0) * 128.0; |
| 42 | float c1 = (3424.0 / 4096.0); |
| 43 | float c2 = (2413.0 / 4096.0) * 32.0; |
| 44 | float c3 = (2392.0 / 4096.0) * 32.0; |
| 45 | |
| 46 | float3 tmp = pow(clamp(color, 0.0, 1.0), 1.0 / float3(m2)); |
| 47 | tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp); |
| 48 | return pow(tmp, 1.0 / float3(m1)); |
| 49 | } |
| 50 | )"); |
| 51 | break; |
| 52 | case HAL_DATASPACE_TRANSFER_HLG: |
| 53 | shader.append(R"( |
| 54 | float EOTF_channel(float channel) { |
| 55 | const float a = 0.17883277; |
| 56 | const float b = 0.28466892; |
| 57 | const float c = 0.55991073; |
| 58 | return channel <= 0.5 ? channel * channel / 3.0 : |
| 59 | (exp((channel - c) / a) + b) / 12.0; |
| 60 | } |
| 61 | |
| 62 | float3 EOTF(float3 color) { |
| 63 | return float3(EOTF_channel(color.r), EOTF_channel(color.g), |
| 64 | EOTF_channel(color.b)); |
| 65 | } |
| 66 | )"); |
| 67 | break; |
| 68 | case HAL_DATASPACE_TRANSFER_LINEAR: |
| 69 | shader.append(R"( |
| 70 | float3 EOTF(float3 color) { |
| 71 | return color; |
| 72 | } |
| 73 | )"); |
| 74 | break; |
| 75 | case HAL_DATASPACE_TRANSFER_SRGB: |
| 76 | default: |
| 77 | shader.append(R"( |
| 78 | |
| 79 | float EOTF_sRGB(float srgb) { |
| 80 | return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4); |
| 81 | } |
| 82 | |
| 83 | float3 EOTF_sRGB(float3 srgb) { |
| 84 | return float3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b)); |
| 85 | } |
| 86 | |
| 87 | float3 EOTF(float3 srgb) { |
| 88 | return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb)); |
| 89 | } |
| 90 | )"); |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static void generateXYZTransforms(std::string& shader) { |
| 96 | shader.append(R"( |
| 97 | uniform float4x4 in_rgbToXyz; |
| 98 | uniform float4x4 in_xyzToRgb; |
| 99 | float3 ToXYZ(float3 rgb) { |
| 100 | return (in_rgbToXyz * float4(rgb, 1.0)).rgb; |
| 101 | } |
| 102 | |
| 103 | float3 ToRGB(float3 xyz) { |
| 104 | return clamp((in_xyzToRgb * float4(xyz, 1.0)).rgb, 0.0, 1.0); |
| 105 | } |
| 106 | )"); |
| 107 | } |
| 108 | |
| 109 | // Conversion from relative light to absolute light (maps from [0, 1] to [0, maxNits]) |
| 110 | static void generateLuminanceScalesForOOTF(ui::Dataspace inputDataspace, |
| 111 | ui::Dataspace outputDataspace, std::string& shader) { |
| 112 | switch (inputDataspace & HAL_DATASPACE_TRANSFER_MASK) { |
| 113 | case HAL_DATASPACE_TRANSFER_ST2084: |
| 114 | shader.append(R"( |
| 115 | float3 ScaleLuminance(float3 xyz) { |
| 116 | return xyz * 10000.0; |
| 117 | } |
| 118 | )"); |
| 119 | break; |
| 120 | case HAL_DATASPACE_TRANSFER_HLG: |
| 121 | shader.append(R"( |
| 122 | float3 ScaleLuminance(float3 xyz) { |
| 123 | return xyz * 1000.0 * pow(xyz.y, 0.2); |
| 124 | } |
| 125 | )"); |
| 126 | break; |
| 127 | default: |
| 128 | switch (outputDataspace & HAL_DATASPACE_TRANSFER_MASK) { |
| 129 | case HAL_DATASPACE_TRANSFER_ST2084: |
| 130 | case HAL_DATASPACE_TRANSFER_HLG: |
| 131 | // SDR -> HDR tonemap |
| 132 | shader.append(R"( |
| 133 | float3 ScaleLuminance(float3 xyz) { |
| 134 | return xyz * in_libtonemap_inputMaxLuminance; |
| 135 | } |
| 136 | )"); |
| 137 | break; |
| 138 | default: |
| 139 | // Input and output are both SDR, so no tone-mapping is expected so |
| 140 | // no-op the luminance normalization. |
| 141 | shader.append(R"( |
| 142 | float3 ScaleLuminance(float3 xyz) { |
| 143 | return xyz * in_libtonemap_displayMaxLuminance; |
| 144 | } |
| 145 | )"); |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Normalizes from absolute light back to relative light (maps from [0, maxNits] back to [0, 1]) |
| 152 | static void generateLuminanceNormalizationForOOTF(ui::Dataspace outputDataspace, |
| 153 | std::string& shader) { |
| 154 | switch (outputDataspace & HAL_DATASPACE_TRANSFER_MASK) { |
| 155 | case HAL_DATASPACE_TRANSFER_ST2084: |
| 156 | shader.append(R"( |
| 157 | float3 NormalizeLuminance(float3 xyz) { |
| 158 | return xyz / 10000.0; |
| 159 | } |
| 160 | )"); |
| 161 | break; |
| 162 | case HAL_DATASPACE_TRANSFER_HLG: |
| 163 | shader.append(R"( |
| 164 | float3 NormalizeLuminance(float3 xyz) { |
| 165 | return xyz / 1000.0 * pow(xyz.y / 1000.0, -0.2 / 1.2); |
| 166 | } |
| 167 | )"); |
| 168 | break; |
| 169 | default: |
| 170 | shader.append(R"( |
| 171 | float3 NormalizeLuminance(float3 xyz) { |
| 172 | return xyz / in_libtonemap_displayMaxLuminance; |
| 173 | } |
| 174 | )"); |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static void generateOOTF(ui::Dataspace inputDataspace, ui::Dataspace outputDataspace, |
| 180 | std::string& shader) { |
| 181 | shader.append(tonemap::getToneMapper() |
| 182 | ->generateTonemapGainShaderSkSL(toAidlDataspace(inputDataspace), |
| 183 | toAidlDataspace(outputDataspace)) |
| 184 | .c_str()); |
| 185 | |
| 186 | generateLuminanceScalesForOOTF(inputDataspace, outputDataspace, shader); |
| 187 | generateLuminanceNormalizationForOOTF(outputDataspace, shader); |
| 188 | |
| 189 | shader.append(R"( |
| 190 | float3 OOTF(float3 linearRGB, float3 xyz) { |
| 191 | float3 scaledLinearRGB = ScaleLuminance(linearRGB); |
| 192 | float3 scaledXYZ = ScaleLuminance(xyz); |
| 193 | |
| 194 | float gain = libtonemap_LookupTonemapGain(scaledLinearRGB, scaledXYZ); |
| 195 | |
| 196 | return NormalizeLuminance(scaledXYZ * gain); |
| 197 | } |
| 198 | )"); |
| 199 | } |
| 200 | |
| 201 | static void generateOETF(ui::Dataspace dataspace, std::string& shader) { |
| 202 | switch (dataspace & HAL_DATASPACE_TRANSFER_MASK) { |
| 203 | case HAL_DATASPACE_TRANSFER_ST2084: |
| 204 | shader.append(R"( |
| 205 | |
| 206 | float3 OETF(float3 xyz) { |
| 207 | float m1 = (2610.0 / 4096.0) / 4.0; |
| 208 | float m2 = (2523.0 / 4096.0) * 128.0; |
| 209 | float c1 = (3424.0 / 4096.0); |
| 210 | float c2 = (2413.0 / 4096.0) * 32.0; |
| 211 | float c3 = (2392.0 / 4096.0) * 32.0; |
| 212 | |
| 213 | float3 tmp = pow(xyz, float3(m1)); |
| 214 | tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp); |
| 215 | return pow(tmp, float3(m2)); |
| 216 | } |
| 217 | )"); |
| 218 | break; |
| 219 | case HAL_DATASPACE_TRANSFER_HLG: |
| 220 | shader.append(R"( |
| 221 | float OETF_channel(float channel) { |
| 222 | const float a = 0.17883277; |
| 223 | const float b = 0.28466892; |
| 224 | const float c = 0.55991073; |
| 225 | return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) : |
| 226 | a * log(12.0 * channel - b) + c; |
| 227 | } |
| 228 | |
| 229 | float3 OETF(float3 linear) { |
| 230 | return float3(OETF_channel(linear.r), OETF_channel(linear.g), |
| 231 | OETF_channel(linear.b)); |
| 232 | } |
| 233 | )"); |
| 234 | break; |
| 235 | case HAL_DATASPACE_TRANSFER_LINEAR: |
| 236 | shader.append(R"( |
| 237 | float3 OETF(float3 linear) { |
| 238 | return linear; |
| 239 | } |
| 240 | )"); |
| 241 | break; |
| 242 | case HAL_DATASPACE_TRANSFER_SRGB: |
| 243 | default: |
| 244 | shader.append(R"( |
| 245 | float OETF_sRGB(float linear) { |
| 246 | return linear <= 0.0031308 ? |
| 247 | linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055; |
| 248 | } |
| 249 | |
| 250 | float3 OETF_sRGB(float3 linear) { |
| 251 | return float3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); |
| 252 | } |
| 253 | |
| 254 | float3 OETF(float3 linear) { |
| 255 | return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)); |
| 256 | } |
| 257 | )"); |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static void generateEffectiveOOTF(bool undoPremultipliedAlpha, std::string& shader) { |
| 263 | shader.append(R"( |
| 264 | uniform shader child; |
| 265 | half4 main(float2 xy) { |
| 266 | float4 c = float4(child.eval(xy)); |
| 267 | )"); |
| 268 | if (undoPremultipliedAlpha) { |
| 269 | shader.append(R"( |
| 270 | c.rgb = c.rgb / (c.a + 0.0019); |
| 271 | )"); |
| 272 | } |
| 273 | shader.append(R"( |
| 274 | float3 linearRGB = EOTF(c.rgb); |
| 275 | float3 xyz = ToXYZ(linearRGB); |
| 276 | c.rgb = OETF(ToRGB(OOTF(linearRGB, xyz))); |
| 277 | )"); |
| 278 | if (undoPremultipliedAlpha) { |
| 279 | shader.append(R"( |
| 280 | c.rgb = c.rgb * (c.a + 0.0019); |
| 281 | )"); |
| 282 | } |
| 283 | shader.append(R"( |
| 284 | return c; |
| 285 | } |
| 286 | )"); |
| 287 | } |
| 288 | static ColorSpace toColorSpace(ui::Dataspace dataspace) { |
| 289 | switch (dataspace & HAL_DATASPACE_STANDARD_MASK) { |
| 290 | case HAL_DATASPACE_STANDARD_BT709: |
| 291 | return ColorSpace::sRGB(); |
| 292 | break; |
| 293 | case HAL_DATASPACE_STANDARD_DCI_P3: |
| 294 | return ColorSpace::DisplayP3(); |
| 295 | break; |
| 296 | case HAL_DATASPACE_STANDARD_BT2020: |
| 297 | return ColorSpace::BT2020(); |
| 298 | break; |
| 299 | default: |
| 300 | return ColorSpace::sRGB(); |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | std::string buildLinearEffectSkSL(const LinearEffect& linearEffect) { |
| 306 | std::string shaderString; |
| 307 | generateEOTF(linearEffect.fakeInputDataspace == ui::Dataspace::UNKNOWN |
| 308 | ? linearEffect.inputDataspace |
| 309 | : linearEffect.fakeInputDataspace, |
| 310 | shaderString); |
| 311 | generateXYZTransforms(shaderString); |
| 312 | generateOOTF(linearEffect.inputDataspace, linearEffect.outputDataspace, shaderString); |
| 313 | generateOETF(linearEffect.outputDataspace, shaderString); |
| 314 | generateEffectiveOOTF(linearEffect.undoPremultipliedAlpha, shaderString); |
| 315 | return shaderString; |
| 316 | } |
| 317 | |
| 318 | template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, bool> = true> |
| 319 | std::vector<uint8_t> buildUniformValue(T value) { |
| 320 | std::vector<uint8_t> result; |
| 321 | result.resize(sizeof(value)); |
| 322 | std::memcpy(result.data(), &value, sizeof(value)); |
| 323 | return result; |
| 324 | } |
| 325 | |
| 326 | // Generates a list of uniforms to set on the LinearEffect shader above. |
| 327 | std::vector<tonemap::ShaderUniform> buildLinearEffectUniforms(const LinearEffect& linearEffect, |
| 328 | const mat4& colorTransform, |
| 329 | float maxDisplayLuminance, |
| 330 | float maxLuminance) { |
| 331 | std::vector<tonemap::ShaderUniform> uniforms; |
| 332 | if (linearEffect.inputDataspace == linearEffect.outputDataspace) { |
| 333 | uniforms.push_back({.name = "in_rgbToXyz", .value = buildUniformValue<mat4>(mat4())}); |
| 334 | uniforms.push_back( |
| 335 | {.name = "in_xyzToRgb", .value = buildUniformValue<mat4>(colorTransform)}); |
| 336 | } else { |
| 337 | ColorSpace inputColorSpace = toColorSpace(linearEffect.inputDataspace); |
| 338 | ColorSpace outputColorSpace = toColorSpace(linearEffect.outputDataspace); |
| 339 | uniforms.push_back({.name = "in_rgbToXyz", |
| 340 | .value = buildUniformValue<mat4>(mat4(inputColorSpace.getRGBtoXYZ()))}); |
| 341 | uniforms.push_back({.name = "in_xyzToRgb", |
| 342 | .value = buildUniformValue<mat4>( |
| 343 | colorTransform * mat4(outputColorSpace.getXYZtoRGB()))}); |
| 344 | } |
| 345 | |
| 346 | tonemap::Metadata metadata{.displayMaxLuminance = maxDisplayLuminance, |
| 347 | // If the input luminance is unknown, use display luminance (aka, |
| 348 | // no-op any luminance changes) |
| 349 | // This will be the case for eg screenshots in addition to |
| 350 | // uncalibrated displays |
| 351 | .contentMaxLuminance = |
| 352 | maxLuminance > 0 ? maxLuminance : maxDisplayLuminance}; |
| 353 | |
| 354 | for (const auto uniform : tonemap::getToneMapper()->generateShaderSkSLUniforms(metadata)) { |
| 355 | uniforms.push_back(uniform); |
| 356 | } |
| 357 | |
| 358 | return uniforms; |
| 359 | } |
| 360 | |
| 361 | } // namespace android::shaders |