bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 7 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 8 | #include "GrGLSLBlend.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 9 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Brian Osman | dff5d43 | 2017-08-01 14:46:18 -0400 | [diff] [blame] | 10 | #include "glsl/GrGLSLProgramBuilder.h" |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 11 | #include "SkBlendModePriv.h" |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 12 | |
| 13 | ////////////////////////////////////////////////////////////////////////////// |
| 14 | // Advanced (non-coeff) blend helpers |
| 15 | ////////////////////////////////////////////////////////////////////////////// |
| 16 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 17 | static void hard_light(GrGLSLFragmentBuilder* fsBuilder, |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 18 | const char* final, |
| 19 | const char* src, |
| 20 | const char* dst) { |
| 21 | static const char kComponents[] = { 'r', 'g', 'b' }; |
| 22 | for (size_t i = 0; i < SK_ARRAY_COUNT(kComponents); ++i) { |
| 23 | char component = kComponents[i]; |
| 24 | fsBuilder->codeAppendf("if (2.0 * %s.%c <= %s.a) {", src, component, src); |
| 25 | fsBuilder->codeAppendf("%s.%c = 2.0 * %s.%c * %s.%c;", |
| 26 | final, component, src, component, dst, component); |
| 27 | fsBuilder->codeAppend("} else {"); |
| 28 | fsBuilder->codeAppendf("%s.%c = %s.a * %s.a - 2.0 * (%s.a - %s.%c) * (%s.a - %s.%c);", |
| 29 | final, component, src, dst, dst, dst, component, src, src, |
| 30 | component); |
| 31 | fsBuilder->codeAppend("}"); |
| 32 | } |
| 33 | fsBuilder->codeAppendf("%s.rgb += %s.rgb * (1.0 - %s.a) + %s.rgb * (1.0 - %s.a);", |
| 34 | final, src, dst, dst, src); |
| 35 | } |
| 36 | |
| 37 | // Does one component of color-dodge |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 38 | static void color_dodge_component(GrGLSLFragmentBuilder* fsBuilder, |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 39 | const char* final, |
| 40 | const char* src, |
| 41 | const char* dst, |
| 42 | const char component) { |
Brian Osman | dff5d43 | 2017-08-01 14:46:18 -0400 | [diff] [blame] | 43 | const char* divisorGuard = ""; |
| 44 | const GrShaderCaps* shaderCaps = fsBuilder->getProgramBuilder()->shaderCaps(); |
| 45 | if (shaderCaps->mustGuardDivisionEvenAfterExplicitZeroCheck()) { |
| 46 | divisorGuard = "+ 0.00000001"; |
| 47 | } |
| 48 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 49 | fsBuilder->codeAppendf("if (0.0 == %s.%c) {", dst, component); |
| 50 | fsBuilder->codeAppendf("%s.%c = %s.%c * (1.0 - %s.a);", |
| 51 | final, component, src, component, dst); |
| 52 | fsBuilder->codeAppend("} else {"); |
| 53 | fsBuilder->codeAppendf("float d = %s.a - %s.%c;", src, src, component); |
| 54 | fsBuilder->codeAppend("if (0.0 == d) {"); |
| 55 | fsBuilder->codeAppendf("%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);", |
| 56 | final, component, src, dst, src, component, dst, dst, component, |
| 57 | src); |
| 58 | fsBuilder->codeAppend("} else {"); |
Brian Osman | dff5d43 | 2017-08-01 14:46:18 -0400 | [diff] [blame] | 59 | fsBuilder->codeAppendf("d = min(%s.a, %s.%c * %s.a / (d %s));", |
| 60 | dst, dst, component, src, divisorGuard); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 61 | fsBuilder->codeAppendf("%s.%c = d * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);", |
| 62 | final, component, src, src, component, dst, dst, component, src); |
| 63 | fsBuilder->codeAppend("}"); |
| 64 | fsBuilder->codeAppend("}"); |
| 65 | } |
| 66 | |
| 67 | // Does one component of color-burn |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 68 | static void color_burn_component(GrGLSLFragmentBuilder* fsBuilder, |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 69 | const char* final, |
| 70 | const char* src, |
| 71 | const char* dst, |
| 72 | const char component) { |
Brian Osman | 1a39d3f | 2017-08-02 13:00:36 -0400 | [diff] [blame] | 73 | const char* divisorGuard = ""; |
| 74 | const GrShaderCaps* shaderCaps = fsBuilder->getProgramBuilder()->shaderCaps(); |
| 75 | if (shaderCaps->mustGuardDivisionEvenAfterExplicitZeroCheck()) { |
| 76 | divisorGuard = "+ 0.00000001"; |
| 77 | } |
| 78 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 79 | fsBuilder->codeAppendf("if (%s.a == %s.%c) {", dst, dst, component); |
| 80 | fsBuilder->codeAppendf("%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);", |
| 81 | final, component, src, dst, src, component, dst, dst, component, |
| 82 | src); |
| 83 | fsBuilder->codeAppendf("} else if (0.0 == %s.%c) {", src, component); |
| 84 | fsBuilder->codeAppendf("%s.%c = %s.%c * (1.0 - %s.a);", |
| 85 | final, component, dst, component, src); |
| 86 | fsBuilder->codeAppend("} else {"); |
Brian Osman | 1a39d3f | 2017-08-02 13:00:36 -0400 | [diff] [blame] | 87 | fsBuilder->codeAppendf("float d = max(0.0, %s.a - (%s.a - %s.%c) * %s.a / (%s.%c %s));", |
| 88 | dst, dst, dst, component, src, src, component, divisorGuard); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 89 | fsBuilder->codeAppendf("%s.%c = %s.a * d + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);", |
| 90 | final, component, src, src, component, dst, dst, component, src); |
| 91 | fsBuilder->codeAppend("}"); |
| 92 | } |
| 93 | |
| 94 | // Does one component of soft-light. Caller should have already checked that dst alpha > 0. |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 95 | static void soft_light_component_pos_dst_alpha(GrGLSLFragmentBuilder* fsBuilder, |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 96 | const char* final, |
| 97 | const char* src, |
| 98 | const char* dst, |
| 99 | const char component) { |
Brian Osman | dff5d43 | 2017-08-01 14:46:18 -0400 | [diff] [blame] | 100 | const char* divisorGuard = ""; |
| 101 | const GrShaderCaps* shaderCaps = fsBuilder->getProgramBuilder()->shaderCaps(); |
| 102 | if (shaderCaps->mustGuardDivisionEvenAfterExplicitZeroCheck()) { |
| 103 | divisorGuard = "+ 0.00000001"; |
| 104 | } |
| 105 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 106 | // if (2S < Sa) |
| 107 | fsBuilder->codeAppendf("if (2.0 * %s.%c <= %s.a) {", src, component, src); |
| 108 | // (D^2 (Sa-2 S))/Da+(1-Da) S+D (-Sa+2 S+1) |
Brian Osman | dff5d43 | 2017-08-01 14:46:18 -0400 | [diff] [blame] | 109 | fsBuilder->codeAppendf("%s.%c = (%s.%c*%s.%c*(%s.a - 2.0*%s.%c)) / (%s.a %s) +" |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 110 | "(1.0 - %s.a) * %s.%c + %s.%c*(-%s.a + 2.0*%s.%c + 1.0);", |
| 111 | final, component, dst, component, dst, component, src, src, |
Brian Osman | dff5d43 | 2017-08-01 14:46:18 -0400 | [diff] [blame] | 112 | component, dst, divisorGuard, dst, src, component, dst, component, src, |
| 113 | src, component); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 114 | // else if (4D < Da) |
| 115 | fsBuilder->codeAppendf("} else if (4.0 * %s.%c <= %s.a) {", |
| 116 | dst, component, dst); |
| 117 | fsBuilder->codeAppendf("float DSqd = %s.%c * %s.%c;", |
| 118 | dst, component, dst, component); |
| 119 | fsBuilder->codeAppendf("float DCub = DSqd * %s.%c;", dst, component); |
| 120 | fsBuilder->codeAppendf("float DaSqd = %s.a * %s.a;", dst, dst); |
| 121 | fsBuilder->codeAppendf("float DaCub = DaSqd * %s.a;", dst); |
| 122 | // (Da^3 (-S)+Da^2 (S-D (3 Sa-6 S-1))+12 Da D^2 (Sa-2 S)-16 D^3 (Sa-2 S))/Da^2 |
| 123 | fsBuilder->codeAppendf("%s.%c =" |
| 124 | "(DaSqd*(%s.%c - %s.%c * (3.0*%s.a - 6.0*%s.%c - 1.0)) +" |
| 125 | " 12.0*%s.a*DSqd*(%s.a - 2.0*%s.%c) - 16.0*DCub * (%s.a - 2.0*%s.%c) -" |
Brian Osman | dff5d43 | 2017-08-01 14:46:18 -0400 | [diff] [blame] | 126 | " DaCub*%s.%c) / (DaSqd %s);", |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 127 | final, component, src, component, dst, component, |
| 128 | src, src, component, dst, src, src, component, src, src, |
Brian Osman | dff5d43 | 2017-08-01 14:46:18 -0400 | [diff] [blame] | 129 | component, src, component, divisorGuard); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 130 | fsBuilder->codeAppendf("} else {"); |
| 131 | // -sqrt(Da * D) (Sa-2 S)-Da S+D (Sa-2 S+1)+S |
| 132 | fsBuilder->codeAppendf("%s.%c = %s.%c*(%s.a - 2.0*%s.%c + 1.0) + %s.%c -" |
| 133 | " sqrt(%s.a*%s.%c)*(%s.a - 2.0*%s.%c) - %s.a*%s.%c;", |
| 134 | final, component, dst, component, src, src, component, src, component, |
| 135 | dst, dst, component, src, src, component, dst, src, component); |
| 136 | fsBuilder->codeAppendf("}"); |
| 137 | } |
| 138 | |
| 139 | // Adds a function that takes two colors and an alpha as input. It produces a color with the |
| 140 | // hue and saturation of the first color, the luminosity of the second color, and the input |
| 141 | // alpha. It has this signature: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 142 | // float3 set_luminance(float3 hueSatColor, float alpha, float3 lumColor). |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 143 | static void add_lum_function(GrGLSLFragmentBuilder* fsBuilder, SkString* setLumFunction) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 144 | // Emit a helper that gets the luminance of a color. |
| 145 | SkString getFunction; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 146 | GrShaderVar getLumArgs[] = { |
| 147 | GrShaderVar("color", kVec3f_GrSLType), |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 148 | }; |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 149 | SkString getLumBody("return dot(float3(0.3, 0.59, 0.11), color);"); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 150 | fsBuilder->emitFunction(kFloat_GrSLType, |
| 151 | "luminance", |
| 152 | SK_ARRAY_COUNT(getLumArgs), getLumArgs, |
| 153 | getLumBody.c_str(), |
| 154 | &getFunction); |
| 155 | |
| 156 | // Emit the set luminance function. |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 157 | GrShaderVar setLumArgs[] = { |
| 158 | GrShaderVar("hueSat", kVec3f_GrSLType), |
| 159 | GrShaderVar("alpha", kFloat_GrSLType), |
| 160 | GrShaderVar("lumColor", kVec3f_GrSLType), |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 161 | }; |
| 162 | SkString setLumBody; |
| 163 | setLumBody.printf("float diff = %s(lumColor - hueSat);", getFunction.c_str()); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 164 | setLumBody.append("float3 outColor = hueSat + diff;"); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 165 | setLumBody.appendf("float outLum = %s(outColor);", getFunction.c_str()); |
| 166 | setLumBody.append("float minComp = min(min(outColor.r, outColor.g), outColor.b);" |
| 167 | "float maxComp = max(max(outColor.r, outColor.g), outColor.b);" |
| 168 | "if (minComp < 0.0 && outLum != minComp) {" |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 169 | "outColor = outLum + ((outColor - float3(outLum, outLum, outLum)) * outLum) /" |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 170 | "(outLum - minComp);" |
| 171 | "}" |
| 172 | "if (maxComp > alpha && maxComp != outLum) {" |
| 173 | "outColor = outLum +" |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 174 | "((outColor - float3(outLum, outLum, outLum)) * (alpha - outLum)) /" |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 175 | "(maxComp - outLum);" |
| 176 | "}" |
| 177 | "return outColor;"); |
| 178 | fsBuilder->emitFunction(kVec3f_GrSLType, |
| 179 | "set_luminance", |
| 180 | SK_ARRAY_COUNT(setLumArgs), setLumArgs, |
| 181 | setLumBody.c_str(), |
| 182 | setLumFunction); |
| 183 | } |
| 184 | |
| 185 | // Adds a function that creates a color with the hue and luminosity of one input color and |
| 186 | // the saturation of another color. It will have this signature: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 187 | // float set_saturation(float3 hueLumColor, float3 satColor) |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 188 | static void add_sat_function(GrGLSLFragmentBuilder* fsBuilder, SkString* setSatFunction) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 189 | // Emit a helper that gets the saturation of a color |
| 190 | SkString getFunction; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 191 | GrShaderVar getSatArgs[] = { GrShaderVar("color", kVec3f_GrSLType) }; |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 192 | SkString getSatBody; |
| 193 | getSatBody.printf("return max(max(color.r, color.g), color.b) - " |
| 194 | "min(min(color.r, color.g), color.b);"); |
| 195 | fsBuilder->emitFunction(kFloat_GrSLType, |
| 196 | "saturation", |
| 197 | SK_ARRAY_COUNT(getSatArgs), getSatArgs, |
| 198 | getSatBody.c_str(), |
| 199 | &getFunction); |
| 200 | |
| 201 | // Emit a helper that sets the saturation given sorted input channels. This used |
| 202 | // to use inout params for min, mid, and max components but that seems to cause |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 203 | // problems on PowerVR drivers. So instead it returns a float3 where r, g ,b are the |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 204 | // adjusted min, mid, and max inputs, respectively. |
| 205 | SkString helperFunction; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 206 | GrShaderVar helperArgs[] = { |
| 207 | GrShaderVar("minComp", kFloat_GrSLType), |
| 208 | GrShaderVar("midComp", kFloat_GrSLType), |
| 209 | GrShaderVar("maxComp", kFloat_GrSLType), |
| 210 | GrShaderVar("sat", kFloat_GrSLType), |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 211 | }; |
| 212 | static const char kHelperBody[] = "if (minComp < maxComp) {" |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 213 | "float3 result;" |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 214 | "result.r = 0.0;" |
| 215 | "result.g = sat * (midComp - minComp) / (maxComp - minComp);" |
| 216 | "result.b = sat;" |
| 217 | "return result;" |
| 218 | "} else {" |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 219 | "return float3(0, 0, 0);" |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 220 | "}"; |
| 221 | fsBuilder->emitFunction(kVec3f_GrSLType, |
| 222 | "set_saturation_helper", |
| 223 | SK_ARRAY_COUNT(helperArgs), helperArgs, |
| 224 | kHelperBody, |
| 225 | &helperFunction); |
| 226 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 227 | GrShaderVar setSatArgs[] = { |
| 228 | GrShaderVar("hueLumColor", kVec3f_GrSLType), |
| 229 | GrShaderVar("satColor", kVec3f_GrSLType), |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 230 | }; |
| 231 | const char* helpFunc = helperFunction.c_str(); |
| 232 | SkString setSatBody; |
| 233 | setSatBody.appendf("float sat = %s(satColor);" |
| 234 | "if (hueLumColor.r <= hueLumColor.g) {" |
| 235 | "if (hueLumColor.g <= hueLumColor.b) {" |
| 236 | "hueLumColor.rgb = %s(hueLumColor.r, hueLumColor.g, hueLumColor.b, sat);" |
| 237 | "} else if (hueLumColor.r <= hueLumColor.b) {" |
| 238 | "hueLumColor.rbg = %s(hueLumColor.r, hueLumColor.b, hueLumColor.g, sat);" |
| 239 | "} else {" |
| 240 | "hueLumColor.brg = %s(hueLumColor.b, hueLumColor.r, hueLumColor.g, sat);" |
| 241 | "}" |
| 242 | "} else if (hueLumColor.r <= hueLumColor.b) {" |
| 243 | "hueLumColor.grb = %s(hueLumColor.g, hueLumColor.r, hueLumColor.b, sat);" |
| 244 | "} else if (hueLumColor.g <= hueLumColor.b) {" |
| 245 | "hueLumColor.gbr = %s(hueLumColor.g, hueLumColor.b, hueLumColor.r, sat);" |
| 246 | "} else {" |
| 247 | "hueLumColor.bgr = %s(hueLumColor.b, hueLumColor.g, hueLumColor.r, sat);" |
| 248 | "}" |
| 249 | "return hueLumColor;", |
| 250 | getFunction.c_str(), helpFunc, helpFunc, helpFunc, helpFunc, |
| 251 | helpFunc, helpFunc); |
| 252 | fsBuilder->emitFunction(kVec3f_GrSLType, |
| 253 | "set_saturation", |
| 254 | SK_ARRAY_COUNT(setSatArgs), setSatArgs, |
| 255 | setSatBody.c_str(), |
| 256 | setSatFunction); |
| 257 | } |
| 258 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 259 | static void emit_advanced_xfermode_code(GrGLSLFragmentBuilder* fsBuilder, const char* srcColor, |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 260 | const char* dstColor, const char* outputColor, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 261 | SkBlendMode mode) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 262 | SkASSERT(srcColor); |
| 263 | SkASSERT(dstColor); |
| 264 | SkASSERT(outputColor); |
| 265 | // These all perform src-over on the alpha channel. |
| 266 | fsBuilder->codeAppendf("%s.a = %s.a + (1.0 - %s.a) * %s.a;", |
| 267 | outputColor, srcColor, srcColor, dstColor); |
| 268 | |
| 269 | switch (mode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 270 | case SkBlendMode::kOverlay: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 271 | // Overlay is Hard-Light with the src and dst reversed |
| 272 | hard_light(fsBuilder, outputColor, dstColor, srcColor); |
| 273 | break; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 274 | case SkBlendMode::kDarken: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 275 | fsBuilder->codeAppendf("%s.rgb = min((1.0 - %s.a) * %s.rgb + %s.rgb, " |
| 276 | "(1.0 - %s.a) * %s.rgb + %s.rgb);", |
| 277 | outputColor, |
| 278 | srcColor, dstColor, srcColor, |
| 279 | dstColor, srcColor, dstColor); |
| 280 | break; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 281 | case SkBlendMode::kLighten: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 282 | fsBuilder->codeAppendf("%s.rgb = max((1.0 - %s.a) * %s.rgb + %s.rgb, " |
| 283 | "(1.0 - %s.a) * %s.rgb + %s.rgb);", |
| 284 | outputColor, |
| 285 | srcColor, dstColor, srcColor, |
| 286 | dstColor, srcColor, dstColor); |
| 287 | break; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 288 | case SkBlendMode::kColorDodge: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 289 | color_dodge_component(fsBuilder, outputColor, srcColor, dstColor, 'r'); |
| 290 | color_dodge_component(fsBuilder, outputColor, srcColor, dstColor, 'g'); |
| 291 | color_dodge_component(fsBuilder, outputColor, srcColor, dstColor, 'b'); |
| 292 | break; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 293 | case SkBlendMode::kColorBurn: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 294 | color_burn_component(fsBuilder, outputColor, srcColor, dstColor, 'r'); |
| 295 | color_burn_component(fsBuilder, outputColor, srcColor, dstColor, 'g'); |
| 296 | color_burn_component(fsBuilder, outputColor, srcColor, dstColor, 'b'); |
| 297 | break; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 298 | case SkBlendMode::kHardLight: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 299 | hard_light(fsBuilder, outputColor, srcColor, dstColor); |
| 300 | break; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 301 | case SkBlendMode::kSoftLight: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 302 | fsBuilder->codeAppendf("if (0.0 == %s.a) {", dstColor); |
| 303 | fsBuilder->codeAppendf("%s.rgba = %s;", outputColor, srcColor); |
| 304 | fsBuilder->codeAppendf("} else {"); |
| 305 | soft_light_component_pos_dst_alpha(fsBuilder, outputColor, srcColor, dstColor, 'r'); |
| 306 | soft_light_component_pos_dst_alpha(fsBuilder, outputColor, srcColor, dstColor, 'g'); |
| 307 | soft_light_component_pos_dst_alpha(fsBuilder, outputColor, srcColor, dstColor, 'b'); |
| 308 | fsBuilder->codeAppendf("}"); |
| 309 | break; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 310 | case SkBlendMode::kDifference: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 311 | fsBuilder->codeAppendf("%s.rgb = %s.rgb + %s.rgb -" |
| 312 | "2.0 * min(%s.rgb * %s.a, %s.rgb * %s.a);", |
| 313 | outputColor, srcColor, dstColor, srcColor, dstColor, |
| 314 | dstColor, srcColor); |
| 315 | break; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 316 | case SkBlendMode::kExclusion: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 317 | fsBuilder->codeAppendf("%s.rgb = %s.rgb + %s.rgb - " |
| 318 | "2.0 * %s.rgb * %s.rgb;", |
| 319 | outputColor, dstColor, srcColor, dstColor, srcColor); |
| 320 | break; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 321 | case SkBlendMode::kMultiply: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 322 | fsBuilder->codeAppendf("%s.rgb = (1.0 - %s.a) * %s.rgb + " |
| 323 | "(1.0 - %s.a) * %s.rgb + " |
| 324 | "%s.rgb * %s.rgb;", |
| 325 | outputColor, srcColor, dstColor, dstColor, srcColor, |
| 326 | srcColor, dstColor); |
| 327 | break; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 328 | case SkBlendMode::kHue: { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 329 | // SetLum(SetSat(S * Da, Sat(D * Sa)), Sa*Da, D*Sa) + (1 - Sa) * D + (1 - Da) * S |
| 330 | SkString setSat, setLum; |
| 331 | add_sat_function(fsBuilder, &setSat); |
| 332 | add_lum_function(fsBuilder, &setLum); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 333 | fsBuilder->codeAppendf("float4 dstSrcAlpha = %s * %s.a;", |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 334 | dstColor, srcColor); |
| 335 | fsBuilder->codeAppendf("%s.rgb = %s(%s(%s.rgb * %s.a, dstSrcAlpha.rgb)," |
| 336 | "dstSrcAlpha.a, dstSrcAlpha.rgb);", |
| 337 | outputColor, setLum.c_str(), setSat.c_str(), srcColor, |
| 338 | dstColor); |
| 339 | fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;", |
| 340 | outputColor, srcColor, dstColor, dstColor, srcColor); |
| 341 | break; |
| 342 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 343 | case SkBlendMode::kSaturation: { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 344 | // SetLum(SetSat(D * Sa, Sat(S * Da)), Sa*Da, D*Sa)) + (1 - Sa) * D + (1 - Da) * S |
| 345 | SkString setSat, setLum; |
| 346 | add_sat_function(fsBuilder, &setSat); |
| 347 | add_lum_function(fsBuilder, &setLum); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 348 | fsBuilder->codeAppendf("float4 dstSrcAlpha = %s * %s.a;", |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 349 | dstColor, srcColor); |
| 350 | fsBuilder->codeAppendf("%s.rgb = %s(%s(dstSrcAlpha.rgb, %s.rgb * %s.a)," |
| 351 | "dstSrcAlpha.a, dstSrcAlpha.rgb);", |
| 352 | outputColor, setLum.c_str(), setSat.c_str(), srcColor, |
| 353 | dstColor); |
| 354 | fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;", |
| 355 | outputColor, srcColor, dstColor, dstColor, srcColor); |
| 356 | break; |
| 357 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 358 | case SkBlendMode::kColor: { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 359 | // SetLum(S * Da, Sa* Da, D * Sa) + (1 - Sa) * D + (1 - Da) * S |
| 360 | SkString setLum; |
| 361 | add_lum_function(fsBuilder, &setLum); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 362 | fsBuilder->codeAppendf("float4 srcDstAlpha = %s * %s.a;", |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 363 | srcColor, dstColor); |
| 364 | fsBuilder->codeAppendf("%s.rgb = %s(srcDstAlpha.rgb, srcDstAlpha.a, %s.rgb * %s.a);", |
| 365 | outputColor, setLum.c_str(), dstColor, srcColor); |
| 366 | fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;", |
| 367 | outputColor, srcColor, dstColor, dstColor, srcColor); |
| 368 | break; |
| 369 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 370 | case SkBlendMode::kLuminosity: { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 371 | // SetLum(D * Sa, Sa* Da, S * Da) + (1 - Sa) * D + (1 - Da) * S |
| 372 | SkString setLum; |
| 373 | add_lum_function(fsBuilder, &setLum); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 374 | fsBuilder->codeAppendf("float4 srcDstAlpha = %s * %s.a;", |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 375 | srcColor, dstColor); |
| 376 | fsBuilder->codeAppendf("%s.rgb = %s(%s.rgb * %s.a, srcDstAlpha.a, srcDstAlpha.rgb);", |
| 377 | outputColor, setLum.c_str(), dstColor, srcColor); |
| 378 | fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;", |
| 379 | outputColor, srcColor, dstColor, dstColor, srcColor); |
| 380 | break; |
| 381 | } |
| 382 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame^] | 383 | SK_ABORT("Unknown Custom Xfer mode."); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 384 | break; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | ////////////////////////////////////////////////////////////////////////////// |
| 389 | // Porter-Duff blend helper |
| 390 | ////////////////////////////////////////////////////////////////////////////// |
| 391 | |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 392 | static bool append_porterduff_term(GrGLSLFragmentBuilder* fsBuilder, SkBlendModeCoeff coeff, |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 393 | const char* colorName, const char* srcColorName, |
| 394 | const char* dstColorName, bool hasPrevious) { |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 395 | if (SkBlendModeCoeff::kZero == coeff) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 396 | return hasPrevious; |
| 397 | } else { |
| 398 | if (hasPrevious) { |
| 399 | fsBuilder->codeAppend(" + "); |
| 400 | } |
| 401 | fsBuilder->codeAppendf("%s", colorName); |
| 402 | switch (coeff) { |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 403 | case SkBlendModeCoeff::kOne: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 404 | break; |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 405 | case SkBlendModeCoeff::kSC: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 406 | fsBuilder->codeAppendf(" * %s", srcColorName); |
| 407 | break; |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 408 | case SkBlendModeCoeff::kISC: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 409 | fsBuilder->codeAppendf(" * (float4(1.0) - %s)", srcColorName); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 410 | break; |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 411 | case SkBlendModeCoeff::kDC: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 412 | fsBuilder->codeAppendf(" * %s", dstColorName); |
| 413 | break; |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 414 | case SkBlendModeCoeff::kIDC: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 415 | fsBuilder->codeAppendf(" * (float4(1.0) - %s)", dstColorName); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 416 | break; |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 417 | case SkBlendModeCoeff::kSA: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 418 | fsBuilder->codeAppendf(" * %s.a", srcColorName); |
| 419 | break; |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 420 | case SkBlendModeCoeff::kISA: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 421 | fsBuilder->codeAppendf(" * (1.0 - %s.a)", srcColorName); |
| 422 | break; |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 423 | case SkBlendModeCoeff::kDA: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 424 | fsBuilder->codeAppendf(" * %s.a", dstColorName); |
| 425 | break; |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 426 | case SkBlendModeCoeff::kIDA: |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 427 | fsBuilder->codeAppendf(" * (1.0 - %s.a)", dstColorName); |
| 428 | break; |
| 429 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame^] | 430 | SK_ABORT("Unsupported Blend Coeff"); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 431 | } |
| 432 | return true; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | ////////////////////////////////////////////////////////////////////////////// |
| 437 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 438 | void GrGLSLBlend::AppendMode(GrGLSLFragmentBuilder* fsBuilder, const char* srcColor, |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 439 | const char* dstColor, const char* outColor, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 440 | SkBlendMode mode) { |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 441 | |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 442 | SkBlendModeCoeff srcCoeff, dstCoeff; |
| 443 | if (SkBlendMode_AsCoeff(mode, &srcCoeff, &dstCoeff)) { |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 444 | // The only coeff mode that can go out of range is plus. |
| 445 | bool clamp = mode == SkBlendMode::kPlus; |
| 446 | |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 447 | fsBuilder->codeAppendf("%s = ", outColor); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 448 | if (clamp) { |
| 449 | fsBuilder->codeAppend("clamp("); |
| 450 | } |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 451 | // append src blend |
| 452 | bool didAppend = append_porterduff_term(fsBuilder, srcCoeff, srcColor, srcColor, dstColor, |
| 453 | false); |
| 454 | // append dst blend |
| 455 | if(!append_porterduff_term(fsBuilder, dstCoeff, dstColor, srcColor, dstColor, didAppend)) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 456 | fsBuilder->codeAppend("float4(0, 0, 0, 0)"); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 457 | } |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 458 | if (clamp) { |
| 459 | fsBuilder->codeAppend(", 0, 1);"); |
| 460 | } |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 461 | fsBuilder->codeAppend(";"); |
| 462 | } else { |
| 463 | emit_advanced_xfermode_code(fsBuilder, srcColor, dstColor, outColor, mode); |
| 464 | } |
| 465 | } |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 466 | |
| 467 | void GrGLSLBlend::AppendRegionOp(GrGLSLFragmentBuilder* fsBuilder, const char* srcColor, |
| 468 | const char* dstColor, const char* outColor, |
| 469 | SkRegion::Op regionOp) { |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 470 | SkBlendModeCoeff srcCoeff, dstCoeff; |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 471 | switch (regionOp) { |
| 472 | case SkRegion::kReplace_Op: |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 473 | srcCoeff = SkBlendModeCoeff::kOne; |
| 474 | dstCoeff = SkBlendModeCoeff::kZero; |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 475 | break; |
| 476 | case SkRegion::kIntersect_Op: |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 477 | srcCoeff = SkBlendModeCoeff::kDC; |
| 478 | dstCoeff = SkBlendModeCoeff::kZero; |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 479 | break; |
| 480 | case SkRegion::kUnion_Op: |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 481 | srcCoeff = SkBlendModeCoeff::kOne; |
| 482 | dstCoeff = SkBlendModeCoeff::kISC; |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 483 | break; |
| 484 | case SkRegion::kXOR_Op: |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 485 | srcCoeff = SkBlendModeCoeff::kIDC; |
| 486 | dstCoeff = SkBlendModeCoeff::kISC; |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 487 | break; |
| 488 | case SkRegion::kDifference_Op: |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 489 | srcCoeff = SkBlendModeCoeff::kZero; |
| 490 | dstCoeff = SkBlendModeCoeff::kISC; |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 491 | break; |
| 492 | case SkRegion::kReverseDifference_Op: |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 493 | srcCoeff = SkBlendModeCoeff::kIDC; |
| 494 | dstCoeff = SkBlendModeCoeff::kZero; |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 495 | break; |
| 496 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame^] | 497 | SK_ABORT("Unsupported Op"); |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 498 | // We should never get here but to make compiler happy |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 499 | srcCoeff = SkBlendModeCoeff::kZero; |
| 500 | dstCoeff = SkBlendModeCoeff::kZero; |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 501 | } |
| 502 | fsBuilder->codeAppendf("%s = ", outColor); |
| 503 | // append src blend |
| 504 | bool didAppend = append_porterduff_term(fsBuilder, srcCoeff, srcColor, srcColor, dstColor, |
| 505 | false); |
| 506 | // append dst blend |
| 507 | if(!append_porterduff_term(fsBuilder, dstCoeff, dstColor, srcColor, dstColor, didAppend)) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 508 | fsBuilder->codeAppend("float4(0, 0, 0, 0)"); |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 509 | } |
| 510 | fsBuilder->codeAppend(";"); |
| 511 | } |