From 3794db84e141861052fd5a58e518c70e9a7350a4 Mon Sep 17 00:00:00 2001 From: Brian Salomon Date: Tue, 17 Dec 2019 14:24:36 -0500 Subject: [PATCH] Blend functions as SkSL built ins. GPU-only for right now. Use in GrGLSLBlend::AppendMode. Remove unused GrGLSLBlend::AppendRegionOp. Change-Id: I23affe255a1adafa55f3a07ca6291dc84e18e213 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/254678 Reviewed-by: Ethan Nicholas Reviewed-by: Brian Osman Commit-Queue: Brian Salomon --- src/gpu/glsl/GrGLSLBlend.cpp | 527 +++-------------------------------- src/gpu/glsl/GrGLSLBlend.h | 3 - src/sksl/SkSLCompiler.cpp | 7 + src/sksl/SkSLIRGenerator.cpp | 1 + src/sksl/SkSLUtil.h | 4 + src/sksl/sksl_blend.inc | 294 +++++++++++++++++++ src/sksl/sksl_gpu.inc | 5 +- tests/ProcessorTest.cpp | 4 +- 8 files changed, 344 insertions(+), 501 deletions(-) create mode 100644 src/sksl/sksl_blend.inc diff --git a/src/gpu/glsl/GrGLSLBlend.cpp b/src/gpu/glsl/GrGLSLBlend.cpp index 49810c31fe..a91bbb5fa9 100644 --- a/src/gpu/glsl/GrGLSLBlend.cpp +++ b/src/gpu/glsl/GrGLSLBlend.cpp @@ -9,501 +9,42 @@ #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" #include "src/gpu/glsl/GrGLSLProgramBuilder.h" -////////////////////////////////////////////////////////////////////////////// -// Advanced (non-coeff) blend helpers -////////////////////////////////////////////////////////////////////////////// - -static void hard_light(GrGLSLFragmentBuilder* fsBuilder, - const char* final, - const char* src, - const char* dst) { - static const char kComponents[] = { 'r', 'g', 'b' }; - for (size_t i = 0; i < SK_ARRAY_COUNT(kComponents); ++i) { - char component = kComponents[i]; - fsBuilder->codeAppendf("if (2.0 * %s.%c <= %s.a) {", src, component, src); - fsBuilder->codeAppendf("%s.%c = 2.0 * %s.%c * %s.%c;", - final, component, src, component, dst, component); - fsBuilder->codeAppend("} else {"); - fsBuilder->codeAppendf("%s.%c = %s.a * %s.a - 2.0 * (%s.a - %s.%c) * (%s.a - %s.%c);", - final, component, src, dst, dst, dst, component, src, src, - component); - fsBuilder->codeAppend("}"); - } - fsBuilder->codeAppendf("%s.rgb += %s.rgb * (1.0 - %s.a) + %s.rgb * (1.0 - %s.a);", - final, src, dst, dst, src); -} - -// Does one component of color-dodge -static void color_dodge_component(GrGLSLFragmentBuilder* fsBuilder, - const char* final, - const char* src, - const char* dst, - const char component) { - const char* divisorGuard = ""; - const GrShaderCaps* shaderCaps = fsBuilder->getProgramBuilder()->shaderCaps(); - if (shaderCaps->mustGuardDivisionEvenAfterExplicitZeroCheck()) { - divisorGuard = "+ 0.00000001"; - } - - fsBuilder->codeAppendf("if (0.0 == %s.%c) {", dst, component); - fsBuilder->codeAppendf("%s.%c = %s.%c * (1.0 - %s.a);", - final, component, src, component, dst); - fsBuilder->codeAppend("} else {"); - fsBuilder->codeAppendf("half d = %s.a - %s.%c;", src, src, component); - fsBuilder->codeAppend("if (0.0 == d) {"); - fsBuilder->codeAppendf("%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);", - final, component, src, dst, src, component, dst, dst, component, - src); - fsBuilder->codeAppend("} else {"); - fsBuilder->codeAppendf("d = min(%s.a, %s.%c * %s.a / (d %s));", - dst, dst, component, src, divisorGuard); - fsBuilder->codeAppendf("%s.%c = d * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);", - final, component, src, src, component, dst, dst, component, src); - fsBuilder->codeAppend("}"); - fsBuilder->codeAppend("}"); -} - -// Does one component of color-burn -static void color_burn_component(GrGLSLFragmentBuilder* fsBuilder, - const char* final, - const char* src, - const char* dst, - const char component) { - const char* divisorGuard = ""; - const GrShaderCaps* shaderCaps = fsBuilder->getProgramBuilder()->shaderCaps(); - if (shaderCaps->mustGuardDivisionEvenAfterExplicitZeroCheck()) { - divisorGuard = "+ 0.00000001"; - } - - fsBuilder->codeAppendf("if (%s.a == %s.%c) {", dst, dst, component); - fsBuilder->codeAppendf("%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);", - final, component, src, dst, src, component, dst, dst, component, - src); - fsBuilder->codeAppendf("} else if (0.0 == %s.%c) {", src, component); - fsBuilder->codeAppendf("%s.%c = %s.%c * (1.0 - %s.a);", - final, component, dst, component, src); - fsBuilder->codeAppend("} else {"); - fsBuilder->codeAppendf("half d = max(0.0, %s.a - (%s.a - %s.%c) * %s.a / (%s.%c %s));", - dst, dst, dst, component, src, src, component, divisorGuard); - fsBuilder->codeAppendf("%s.%c = %s.a * d + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);", - final, component, src, src, component, dst, dst, component, src); - fsBuilder->codeAppend("}"); -} - -// Does one component of soft-light. Caller should have already checked that dst alpha > 0. -static void soft_light_component_pos_dst_alpha(GrGLSLFragmentBuilder* fsBuilder, - const char* final, - const char* src, - const char* dst, - const char component) { - const char* divisorGuard = ""; - const GrShaderCaps* shaderCaps = fsBuilder->getProgramBuilder()->shaderCaps(); - if (shaderCaps->mustGuardDivisionEvenAfterExplicitZeroCheck()) { - divisorGuard = "+ 0.00000001"; - } - - // if (2S < Sa) - fsBuilder->codeAppendf("if (2.0 * %s.%c <= %s.a) {", src, component, src); - // (D^2 (Sa-2 S))/Da+(1-Da) S+D (-Sa+2 S+1) - fsBuilder->codeAppendf("%s.%c = (%s.%c*%s.%c*(%s.a - 2.0*%s.%c)) / (%s.a %s) +" - "(1.0 - %s.a) * %s.%c + %s.%c*(-%s.a + 2.0*%s.%c + 1.0);", - final, component, dst, component, dst, component, src, src, - component, dst, divisorGuard, dst, src, component, dst, component, src, - src, component); - // else if (4D < Da) - fsBuilder->codeAppendf("} else if (4.0 * %s.%c <= %s.a) {", - dst, component, dst); - fsBuilder->codeAppendf("half DSqd = %s.%c * %s.%c;", - dst, component, dst, component); - fsBuilder->codeAppendf("half DCub = DSqd * %s.%c;", dst, component); - fsBuilder->codeAppendf("half DaSqd = %s.a * %s.a;", dst, dst); - fsBuilder->codeAppendf("half DaCub = DaSqd * %s.a;", dst); - // (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 - fsBuilder->codeAppendf("%s.%c =" - "(DaSqd*(%s.%c - %s.%c * (3.0*%s.a - 6.0*%s.%c - 1.0)) +" - " 12.0*%s.a*DSqd*(%s.a - 2.0*%s.%c) - 16.0*DCub * (%s.a - 2.0*%s.%c) -" - " DaCub*%s.%c) / (DaSqd %s);", - final, component, src, component, dst, component, - src, src, component, dst, src, src, component, src, src, - component, src, component, divisorGuard); - fsBuilder->codeAppendf("} else {"); - // -sqrt(Da * D) (Sa-2 S)-Da S+D (Sa-2 S+1)+S - fsBuilder->codeAppendf("%s.%c = %s.%c*(%s.a - 2.0*%s.%c + 1.0) + %s.%c -" - " sqrt(%s.a*%s.%c)*(%s.a - 2.0*%s.%c) - %s.a*%s.%c;", - final, component, dst, component, src, src, component, src, component, - dst, dst, component, src, src, component, dst, src, component); - fsBuilder->codeAppendf("}"); -} - -// Adds a function that takes two colors and an alpha as input. It produces a color with the -// hue and saturation of the first color, the luminosity of the second color, and the input -// alpha. It has this signature: -// float3 set_luminance(float3 hueSatColor, float alpha, float3 lumColor). -static void add_lum_function(GrGLSLFragmentBuilder* fsBuilder, SkString* setLumFunction) { - // Emit a helper that gets the luminance of a color. - SkString getFunction; - GrShaderVar getLumArgs[] = { - GrShaderVar("color", kHalf3_GrSLType), - }; - SkString getLumBody("return dot(half3(0.3, 0.59, 0.11), color);"); - fsBuilder->emitFunction(kHalf_GrSLType, - "luminance", - SK_ARRAY_COUNT(getLumArgs), getLumArgs, - getLumBody.c_str(), - &getFunction); - - // Emit the set luminance function. - GrShaderVar setLumArgs[] = { - GrShaderVar("hueSat", kHalf3_GrSLType), - GrShaderVar("alpha", kHalf_GrSLType), - GrShaderVar("lumColor", kHalf3_GrSLType), - }; - SkString setLumBody; - setLumBody.printf("half outLum = %s(lumColor);", getFunction.c_str()); - setLumBody.appendf("half3 outColor = outLum - %s(hueSat) + hueSat;", getFunction.c_str()); - setLumBody.append("half minComp = min(min(outColor.r, outColor.g), outColor.b);" - "half maxComp = max(max(outColor.r, outColor.g), outColor.b);" - "if (minComp < 0.0 && outLum != minComp) {" - "outColor = outLum + ((outColor - half3(outLum, outLum, outLum)) * outLum) /" - "(outLum - minComp);" - "}" - "if (maxComp > alpha && maxComp != outLum) {" - "outColor = outLum +" - "((outColor - half3(outLum, outLum, outLum)) * (alpha - outLum)) /" - "(maxComp - outLum);" - "}" - "return outColor;"); - fsBuilder->emitFunction(kHalf3_GrSLType, - "set_luminance", - SK_ARRAY_COUNT(setLumArgs), setLumArgs, - setLumBody.c_str(), - setLumFunction); -} - -// Adds a function that creates a color with the hue and luminosity of one input color and -// the saturation of another color. It will have this signature: -// float set_saturation(float3 hueLumColor, float3 satColor) -static void add_sat_function(GrGLSLFragmentBuilder* fsBuilder, SkString* setSatFunction) { - // Emit a helper that gets the saturation of a color - SkString getFunction; - GrShaderVar getSatArgs[] = { GrShaderVar("color", kHalf3_GrSLType) }; - SkString getSatBody; - getSatBody.printf("return max(max(color.r, color.g), color.b) - " - "min(min(color.r, color.g), color.b);"); - fsBuilder->emitFunction(kHalf_GrSLType, - "saturation", - SK_ARRAY_COUNT(getSatArgs), getSatArgs, - getSatBody.c_str(), - &getFunction); - - // Emit a helper that sets the saturation given sorted input channels. This used - // to use inout params for min, mid, and max components but that seems to cause - // problems on PowerVR drivers. So instead it returns a float3 where r, g ,b are the - // adjusted min, mid, and max inputs, respectively. - SkString helperFunction; - GrShaderVar helperArgs[] = { - GrShaderVar("minComp", kHalf_GrSLType), - GrShaderVar("midComp", kHalf_GrSLType), - GrShaderVar("maxComp", kHalf_GrSLType), - GrShaderVar("sat", kHalf_GrSLType), - }; - static const char kHelperBody[] = "if (minComp < maxComp) {" - "half3 result;" - "result.r = 0.0;" - "result.g = sat * (midComp - minComp) / (maxComp - minComp);" - "result.b = sat;" - "return result;" - "} else {" - "return half3(0, 0, 0);" - "}"; - fsBuilder->emitFunction(kHalf3_GrSLType, - "set_saturation_helper", - SK_ARRAY_COUNT(helperArgs), helperArgs, - kHelperBody, - &helperFunction); - - GrShaderVar setSatArgs[] = { - GrShaderVar("hueLumColor", kHalf3_GrSLType), - GrShaderVar("satColor", kHalf3_GrSLType), - }; - const char* helpFunc = helperFunction.c_str(); - SkString setSatBody; - setSatBody.appendf("half sat = %s(satColor);" - "if (hueLumColor.r <= hueLumColor.g) {" - "if (hueLumColor.g <= hueLumColor.b) {" - "hueLumColor.rgb = %s(hueLumColor.r, hueLumColor.g, hueLumColor.b, sat);" - "} else if (hueLumColor.r <= hueLumColor.b) {" - "hueLumColor.rbg = %s(hueLumColor.r, hueLumColor.b, hueLumColor.g, sat);" - "} else {" - "hueLumColor.brg = %s(hueLumColor.b, hueLumColor.r, hueLumColor.g, sat);" - "}" - "} else if (hueLumColor.r <= hueLumColor.b) {" - "hueLumColor.grb = %s(hueLumColor.g, hueLumColor.r, hueLumColor.b, sat);" - "} else if (hueLumColor.g <= hueLumColor.b) {" - "hueLumColor.gbr = %s(hueLumColor.g, hueLumColor.b, hueLumColor.r, sat);" - "} else {" - "hueLumColor.bgr = %s(hueLumColor.b, hueLumColor.g, hueLumColor.r, sat);" - "}" - "return hueLumColor;", - getFunction.c_str(), helpFunc, helpFunc, helpFunc, helpFunc, - helpFunc, helpFunc); - fsBuilder->emitFunction(kHalf3_GrSLType, - "set_saturation", - SK_ARRAY_COUNT(setSatArgs), setSatArgs, - setSatBody.c_str(), - setSatFunction); -} - -static void emit_advanced_xfermode_code(GrGLSLFragmentBuilder* fsBuilder, const char* srcColor, - const char* dstColor, const char* outputColor, - SkBlendMode mode) { - SkASSERT(srcColor); - SkASSERT(dstColor); - SkASSERT(outputColor); - // These all perform src-over on the alpha channel. - fsBuilder->codeAppendf("%s.a = %s.a + (1.0 - %s.a) * %s.a;", - outputColor, srcColor, srcColor, dstColor); - - switch (mode) { - case SkBlendMode::kOverlay: - // Overlay is Hard-Light with the src and dst reversed - hard_light(fsBuilder, outputColor, dstColor, srcColor); - break; - case SkBlendMode::kDarken: - fsBuilder->codeAppendf("%s.rgb = min((1.0 - %s.a) * %s.rgb + %s.rgb, " - "(1.0 - %s.a) * %s.rgb + %s.rgb);", - outputColor, - srcColor, dstColor, srcColor, - dstColor, srcColor, dstColor); - break; - case SkBlendMode::kLighten: - fsBuilder->codeAppendf("%s.rgb = max((1.0 - %s.a) * %s.rgb + %s.rgb, " - "(1.0 - %s.a) * %s.rgb + %s.rgb);", - outputColor, - srcColor, dstColor, srcColor, - dstColor, srcColor, dstColor); - break; - case SkBlendMode::kColorDodge: - color_dodge_component(fsBuilder, outputColor, srcColor, dstColor, 'r'); - color_dodge_component(fsBuilder, outputColor, srcColor, dstColor, 'g'); - color_dodge_component(fsBuilder, outputColor, srcColor, dstColor, 'b'); - break; - case SkBlendMode::kColorBurn: - color_burn_component(fsBuilder, outputColor, srcColor, dstColor, 'r'); - color_burn_component(fsBuilder, outputColor, srcColor, dstColor, 'g'); - color_burn_component(fsBuilder, outputColor, srcColor, dstColor, 'b'); - break; - case SkBlendMode::kHardLight: - hard_light(fsBuilder, outputColor, srcColor, dstColor); - break; - case SkBlendMode::kSoftLight: - fsBuilder->codeAppendf("if (0.0 == %s.a) {", dstColor); - fsBuilder->codeAppendf("%s.rgba = %s;", outputColor, srcColor); - fsBuilder->codeAppendf("} else {"); - soft_light_component_pos_dst_alpha(fsBuilder, outputColor, srcColor, dstColor, 'r'); - soft_light_component_pos_dst_alpha(fsBuilder, outputColor, srcColor, dstColor, 'g'); - soft_light_component_pos_dst_alpha(fsBuilder, outputColor, srcColor, dstColor, 'b'); - fsBuilder->codeAppendf("}"); - break; - case SkBlendMode::kDifference: - fsBuilder->codeAppendf("%s.rgb = %s.rgb + %s.rgb -" - "2.0 * min(%s.rgb * %s.a, %s.rgb * %s.a);", - outputColor, srcColor, dstColor, srcColor, dstColor, - dstColor, srcColor); - break; - case SkBlendMode::kExclusion: - fsBuilder->codeAppendf("%s.rgb = %s.rgb + %s.rgb - " - "2.0 * %s.rgb * %s.rgb;", - outputColor, dstColor, srcColor, dstColor, srcColor); - break; - case SkBlendMode::kMultiply: - fsBuilder->codeAppendf("%s.rgb = (1.0 - %s.a) * %s.rgb + " - "(1.0 - %s.a) * %s.rgb + " - "%s.rgb * %s.rgb;", - outputColor, srcColor, dstColor, dstColor, srcColor, - srcColor, dstColor); - break; - case SkBlendMode::kHue: { - // SetLum(SetSat(S * Da, Sat(D * Sa)), Sa*Da, D*Sa) + (1 - Sa) * D + (1 - Da) * S - SkString setSat, setLum; - add_sat_function(fsBuilder, &setSat); - add_lum_function(fsBuilder, &setLum); - fsBuilder->codeAppendf("half4 dstSrcAlpha = %s * %s.a;", - dstColor, srcColor); - fsBuilder->codeAppendf("%s.rgb = %s(%s(%s.rgb * %s.a, dstSrcAlpha.rgb)," - "dstSrcAlpha.a, dstSrcAlpha.rgb);", - outputColor, setLum.c_str(), setSat.c_str(), srcColor, - dstColor); - fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;", - outputColor, srcColor, dstColor, dstColor, srcColor); - break; - } - case SkBlendMode::kSaturation: { - // SetLum(SetSat(D * Sa, Sat(S * Da)), Sa*Da, D*Sa)) + (1 - Sa) * D + (1 - Da) * S - SkString setSat, setLum; - add_sat_function(fsBuilder, &setSat); - add_lum_function(fsBuilder, &setLum); - fsBuilder->codeAppendf("half4 dstSrcAlpha = %s * %s.a;", - dstColor, srcColor); - fsBuilder->codeAppendf("%s.rgb = %s(%s(dstSrcAlpha.rgb, %s.rgb * %s.a)," - "dstSrcAlpha.a, dstSrcAlpha.rgb);", - outputColor, setLum.c_str(), setSat.c_str(), srcColor, - dstColor); - fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;", - outputColor, srcColor, dstColor, dstColor, srcColor); - break; - } - case SkBlendMode::kColor: { - // SetLum(S * Da, Sa* Da, D * Sa) + (1 - Sa) * D + (1 - Da) * S - SkString setLum; - add_lum_function(fsBuilder, &setLum); - fsBuilder->codeAppendf("half4 srcDstAlpha = %s * %s.a;", - srcColor, dstColor); - fsBuilder->codeAppendf("%s.rgb = %s(srcDstAlpha.rgb, srcDstAlpha.a, %s.rgb * %s.a);", - outputColor, setLum.c_str(), dstColor, srcColor); - fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;", - outputColor, srcColor, dstColor, dstColor, srcColor); - break; - } - case SkBlendMode::kLuminosity: { - // SetLum(D * Sa, Sa* Da, S * Da) + (1 - Sa) * D + (1 - Da) * S - SkString setLum; - add_lum_function(fsBuilder, &setLum); - fsBuilder->codeAppendf("half4 srcDstAlpha = %s * %s.a;", - srcColor, dstColor); - fsBuilder->codeAppendf("%s.rgb = %s(%s.rgb * %s.a, srcDstAlpha.a, srcDstAlpha.rgb);", - outputColor, setLum.c_str(), dstColor, srcColor); - fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;", - outputColor, srcColor, dstColor, dstColor, srcColor); - break; - } - default: - SK_ABORT("Unknown Custom Xfer mode."); - break; - } -} - -////////////////////////////////////////////////////////////////////////////// -// Porter-Duff blend helper -////////////////////////////////////////////////////////////////////////////// - -static bool append_porterduff_term(GrGLSLFragmentBuilder* fsBuilder, SkBlendModeCoeff coeff, - const char* colorName, const char* srcColorName, - const char* dstColorName, bool hasPrevious) { - if (SkBlendModeCoeff::kZero == coeff) { - return hasPrevious; - } else { - if (hasPrevious) { - fsBuilder->codeAppend(" + "); - } - fsBuilder->codeAppendf("%s", colorName); - switch (coeff) { - case SkBlendModeCoeff::kOne: - break; - case SkBlendModeCoeff::kSC: - fsBuilder->codeAppendf(" * %s", srcColorName); - break; - case SkBlendModeCoeff::kISC: - fsBuilder->codeAppendf(" * (half4(1.0) - %s)", srcColorName); - break; - case SkBlendModeCoeff::kDC: - fsBuilder->codeAppendf(" * %s", dstColorName); - break; - case SkBlendModeCoeff::kIDC: - fsBuilder->codeAppendf(" * (half4(1.0) - %s)", dstColorName); - break; - case SkBlendModeCoeff::kSA: - fsBuilder->codeAppendf(" * %s.a", srcColorName); - break; - case SkBlendModeCoeff::kISA: - fsBuilder->codeAppendf(" * (1.0 - %s.a)", srcColorName); - break; - case SkBlendModeCoeff::kDA: - fsBuilder->codeAppendf(" * %s.a", dstColorName); - break; - case SkBlendModeCoeff::kIDA: - fsBuilder->codeAppendf(" * (1.0 - %s.a)", dstColorName); - break; - default: - SK_ABORT("Unsupported Blend Coeff"); - } - return true; - } -} - -////////////////////////////////////////////////////////////////////////////// - void GrGLSLBlend::AppendMode(GrGLSLFragmentBuilder* fsBuilder, const char* srcColor, const char* dstColor, const char* outColor, SkBlendMode mode) { - - SkBlendModeCoeff srcCoeff, dstCoeff; - if (SkBlendMode_AsCoeff(mode, &srcCoeff, &dstCoeff)) { - // The only coeff mode that can go out of range is plus. - bool clamp = mode == SkBlendMode::kPlus; - - fsBuilder->codeAppendf("%s = ", outColor); - if (clamp) { - fsBuilder->codeAppend("clamp("); - } - // append src blend - bool didAppend = append_porterduff_term(fsBuilder, srcCoeff, srcColor, srcColor, dstColor, - false); - // append dst blend - if(!append_porterduff_term(fsBuilder, dstCoeff, dstColor, srcColor, dstColor, didAppend)) { - fsBuilder->codeAppend("half4(0, 0, 0, 0)"); - } - if (clamp) { - fsBuilder->codeAppend(", 0, 1);"); - } - fsBuilder->codeAppend(";"); - } else { - emit_advanced_xfermode_code(fsBuilder, srcColor, dstColor, outColor, mode); + // When and if the SkSL compiler supports inlining we could replace this with + // out = blend(mode, src, dst) where mode is a literal. + const char* name; + switch (mode) { + case SkBlendMode::kClear: name = "clear"; break; + case SkBlendMode::kSrc: name = "src"; break; + case SkBlendMode::kDst: name = "dst"; break; + case SkBlendMode::kSrcOver: name = "src_over"; break; + case SkBlendMode::kDstOver: name = "dst_over"; break; + case SkBlendMode::kSrcIn: name = "src_in"; break; + case SkBlendMode::kDstIn: name = "dst_in"; break; + case SkBlendMode::kSrcOut: name = "src_out"; break; + case SkBlendMode::kDstOut: name = "dst_out"; break; + case SkBlendMode::kSrcATop: name = "src_atop"; break; + case SkBlendMode::kDstATop: name = "dst_atop"; break; + case SkBlendMode::kXor: name = "xor"; break; + case SkBlendMode::kPlus: name = "plus"; break; + case SkBlendMode::kModulate: name = "modulate"; break; + case SkBlendMode::kScreen: name = "screen"; break; + case SkBlendMode::kOverlay: name = "overlay"; break; + case SkBlendMode::kDarken: name = "darken"; break; + case SkBlendMode::kLighten: name = "lighten"; break; + case SkBlendMode::kColorDodge: name = "color_dodge"; break; + case SkBlendMode::kColorBurn: name = "color_burn"; break; + case SkBlendMode::kHardLight: name = "hard_light"; break; + case SkBlendMode::kSoftLight: name = "soft_light"; break; + case SkBlendMode::kDifference: name = "difference"; break; + case SkBlendMode::kExclusion: name = "exclusion"; break; + case SkBlendMode::kMultiply: name = "multiply"; break; + case SkBlendMode::kHue: name = "hue"; break; + case SkBlendMode::kSaturation: name = "saturation"; break; + case SkBlendMode::kColor: name = "color"; break; + case SkBlendMode::kLuminosity: name = "luminosity"; break; } -} - -void GrGLSLBlend::AppendRegionOp(GrGLSLFragmentBuilder* fsBuilder, const char* srcColor, - const char* dstColor, const char* outColor, - SkRegion::Op regionOp) { - SkBlendModeCoeff srcCoeff, dstCoeff; - switch (regionOp) { - case SkRegion::kReplace_Op: - srcCoeff = SkBlendModeCoeff::kOne; - dstCoeff = SkBlendModeCoeff::kZero; - break; - case SkRegion::kIntersect_Op: - srcCoeff = SkBlendModeCoeff::kDC; - dstCoeff = SkBlendModeCoeff::kZero; - break; - case SkRegion::kUnion_Op: - srcCoeff = SkBlendModeCoeff::kOne; - dstCoeff = SkBlendModeCoeff::kISC; - break; - case SkRegion::kXOR_Op: - srcCoeff = SkBlendModeCoeff::kIDC; - dstCoeff = SkBlendModeCoeff::kISC; - break; - case SkRegion::kDifference_Op: - srcCoeff = SkBlendModeCoeff::kZero; - dstCoeff = SkBlendModeCoeff::kISC; - break; - case SkRegion::kReverseDifference_Op: - srcCoeff = SkBlendModeCoeff::kIDC; - dstCoeff = SkBlendModeCoeff::kZero; - break; - default: - SK_ABORT("Unsupported Op"); - // We should never get here but to make compiler happy - srcCoeff = SkBlendModeCoeff::kZero; - dstCoeff = SkBlendModeCoeff::kZero; - } - fsBuilder->codeAppendf("%s = ", outColor); - // append src blend - bool didAppend = append_porterduff_term(fsBuilder, srcCoeff, srcColor, srcColor, dstColor, - false); - // append dst blend - if(!append_porterduff_term(fsBuilder, dstCoeff, dstColor, srcColor, dstColor, didAppend)) { - fsBuilder->codeAppend("half4(0, 0, 0, 0)"); - } - fsBuilder->codeAppend(";"); + fsBuilder->codeAppendf("%s = blend_%s(%s, %s);", outColor, name, srcColor, dstColor); } diff --git a/src/gpu/glsl/GrGLSLBlend.h b/src/gpu/glsl/GrGLSLBlend.h index 69b821e158..28517488d5 100644 --- a/src/gpu/glsl/GrGLSLBlend.h +++ b/src/gpu/glsl/GrGLSLBlend.h @@ -20,9 +20,6 @@ namespace GrGLSLBlend { */ void AppendMode(GrGLSLFragmentBuilder* fsBuilder, const char* srcColor, const char* dstColor, const char* outColor, SkBlendMode mode); - - void AppendRegionOp(GrGLSLFragmentBuilder* fsBuilder, const char* srcColor, - const char* dstColor, const char* outColor, SkRegion::Op regionOp); }; #endif diff --git a/src/sksl/SkSLCompiler.cpp b/src/sksl/SkSLCompiler.cpp index 31492bd493..bc429292be 100644 --- a/src/sksl/SkSLCompiler.cpp +++ b/src/sksl/SkSLCompiler.cpp @@ -45,6 +45,10 @@ static const char* SKSL_GPU_INCLUDE = #include "sksl_gpu.inc" ; +static const char* SKSL_BLEND_INCLUDE = +#include "sksl_blend.inc" +; + static const char* SKSL_INTERP_INCLUDE = #include "sksl_interp.inc" ; @@ -258,6 +262,9 @@ Compiler::Compiler(Flags flags) std::vector> gpuIntrinsics; this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, strlen(SKSL_GPU_INCLUDE), symbols, &gpuIntrinsics, &fGpuSymbolTable); + this->processIncludeFile(Program::kFragment_Kind, SKSL_BLEND_INCLUDE, + strlen(SKSL_BLEND_INCLUDE), std::move(fGpuSymbolTable), &gpuIntrinsics, + &fGpuSymbolTable); grab_intrinsics(&gpuIntrinsics, &fGPUIntrinsics); // need to hang on to the source so that FunctionDefinition.fSource pointers in this file // remain valid diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp index e2847e7b7f..d61599c7be 100644 --- a/src/sksl/SkSLIRGenerator.cpp +++ b/src/sksl/SkSLIRGenerator.cpp @@ -132,6 +132,7 @@ static void fill_caps(const SKSL_CAPS_CLASS& caps, CAP(mustEnableSpecificAdvBlendEqs); CAP(mustDeclareFragmentShaderOutput); CAP(mustDoOpBetweenFloorAndAbs); + CAP(mustGuardDivisionEvenAfterExplicitZeroCheck); CAP(atan2ImplementedAsAtanYOverX); CAP(canUseAnyFunctionInShader); CAP(floatIs32Bits); diff --git a/src/sksl/SkSLUtil.h b/src/sksl/SkSLUtil.h index be9a74e239..7cfe368e81 100644 --- a/src/sksl/SkSLUtil.h +++ b/src/sksl/SkSLUtil.h @@ -108,6 +108,10 @@ public: return false; } + bool mustGuardDivisionEvenAfterExplicitZeroCheck() const { + return false; + } + bool mustEnableAdvBlendEqs() const { return false; } diff --git a/src/sksl/sksl_blend.inc b/src/sksl/sksl_blend.inc new file mode 100644 index 0000000000..755834bcab --- /dev/null +++ b/src/sksl/sksl_blend.inc @@ -0,0 +1,294 @@ +R"SKSL( +// Definitions of functions implementing all of the SkBlendMode blends. + +half4 blend_clear(half4 src, half4 dst) { return half4(0); } + +half4 blend_src(half4 src, half4 dst) { return src; } + +half4 blend_dst(half4 src, half4 dst) { return dst; } + +half4 blend_src_over(half4 src, half4 dst) { return src + (1 - src.a)*dst; } + +half4 blend_dst_over(half4 src, half4 dst) { return (1 - dst.a)*src + dst; } + +half4 blend_src_in(half4 src, half4 dst) { return src*dst.a; } + +half4 blend_dst_in(half4 src, half4 dst) { return src.a*dst; } + +half4 blend_src_out(half4 src, half4 dst) { return (1 - dst.a)*src; } + +half4 blend_dst_out(half4 src, half4 dst) { return (1 - src.a)*dst; } + +half4 blend_src_atop(half4 src, half4 dst) { return dst.a*src + (1 - src.a)*dst; } + +half4 blend_dst_atop(half4 src, half4 dst) { return (1 - dst.a) * src + src.a*dst; } + +half4 blend_xor(half4 src, half4 dst) { return (1 - dst.a)*src + (1 - src.a)*dst; } + +half4 blend_plus(half4 src, half4 dst) { return min(src + dst, 1); } + +half4 blend_modulate(half4 src, half4 dst) { return src*dst; } + +half4 blend_screen(half4 src, half4 dst) { return src + (1 - src)*dst; } + +half _blend_overlay_component(half sc, half sa, half dc, half da) { + if (2*dc <= da) { + return 2*sc*dc; + } + return sa*da - 2*(da - dc)*(sa - sc); +} + +half4 blend_overlay(half4 src, half4 dst) { + half4 result = half4(_blend_overlay_component(src.r, src.a, dst.r, dst.a), + _blend_overlay_component(src.g, src.a, dst.g, dst.a), + _blend_overlay_component(src.b, src.a, dst.b, dst.a), + src.a + (1 - src.a)*dst.a); + result.rgb += dst.rgb*(1 - src.a) + src.rgb*(1 - dst.a); + return result; +} + +half4 blend_darken(half4 src, half4 dst) { + half4 result = blend_src_over(src, dst); + result.rgb = min(result.rgb, (1 - dst.a)*src.rgb + dst.rgb); + return result; +} + +half4 blend_lighten(half4 src, half4 dst) { + half4 result = blend_src_over(src, dst); + result.rgb = max(result.rgb, (1 - dst.a)*src.rgb + dst.rgb); + return result; +} + +half _guarded_divide(half n, half d) { + @if (sk_Caps.mustGuardDivisionEvenAfterExplicitZeroCheck) { + return n/(d + 0.00000001); + } + return n/d; +} + +half _color_dodge_component(half sc, half sa, half dc, half da) { + if (dc == 0) { + return sc*(1 - da); + } else { + half d = sa - sc; + if (d == 0) { + return sa*da + sc*(1 - da) + dc*(1 - sa); + } + d = min(da, _guarded_divide(dc*sa, d)); + return d*sa + sc*(1 - da) + dc*(1 - sa); + } +} + +half4 blend_color_dodge(half4 src, half4 dst) { + return half4(_color_dodge_component(src.r, src.a, dst.r, dst.a), + _color_dodge_component(src.g, src.a, dst.g, dst.a), + _color_dodge_component(src.b, src.a, dst.b, dst.a), + src.a + (1 - src.a)*dst.a); +} + +half _color_burn_component(half sc, half sa, half dc, half da) { + if (da == dc) { + return sa*da + sc*(1 - da) + dc*(1 - sa); + } else if (sc == 0) { + return dc*(1 - sa); + } + half d = max(0, da - _guarded_divide((da - dc)*sa, sc)); + return d*sa + sc*(1 - da) + dc*(1 - sa); +} + +half4 blend_color_burn(half4 src, half4 dst) { + return half4(_color_burn_component(src.r, src.a, dst.r, dst.a), + _color_burn_component(src.g, src.a, dst.g, dst.a), + _color_burn_component(src.b, src.a, dst.b, dst.a), + src.a + (1 - src.a)*dst.a); +} + +half4 blend_hard_light(half4 src, half4 dst) { return blend_overlay(dst, src); } + +half _soft_light_component(half sc, half sa, half dc, half da) { + if (2*sc <= sa) { + return _guarded_divide(dc*dc*(sa - 2*sc), da) + (1 - da)*sc + dc*(-sa + 2*sc + 1); + } else if (4.0 * dc <= da) { + half DSqd = dc*dc; + half DCub = DSqd*dc; + half DaSqd = da*da; + half DaCub = DaSqd*da; + return _guarded_divide(DaSqd*(sc - dc*(3*sa - 6*sc - 1)) + 12*da*DSqd*(sa - 2*sc) + - 16*DCub * (sa - 2*sc) - DaCub*sc, DaSqd); + } + return dc*(sa - 2*sc + 1) + sc - sqrt(da*dc)*(sa - 2*sc) - da*sc; +} + +half4 blend_soft_light(half4 src, half4 dst) { + if (dst.a == 0) { + return src; + } + return half4(_soft_light_component(src.r, src.a, dst.r, dst.a), + _soft_light_component(src.g, src.a, dst.g, dst.a), + _soft_light_component(src.b, src.a, dst.b, dst.a), + src.a + (1 - src.a)*dst.a); +} + +half4 blend_difference(half4 src, half4 dst) { + return half4(src.rgb + dst.rgb - 2*min(src.rgb*dst.a, dst.rgb*src.a), + src.a + (1 - src.a)*dst.a); +} + +half4 blend_exclusion(half4 src, half4 dst) { + return half4(dst.rgb + src.rgb - 2*dst.rgb*src.rgb, src.a + (1 - src.a)*dst.a); +} + +half4 blend_multiply(half4 src, half4 dst) { + return half4((1 - src.a)*dst.rgb + (1 - dst.a)*src.rgb + src.rgb*dst.rgb, + src.a + (1 - src.a)*dst.a); +} + +half _blend_color_luminance(half3 color) { return dot(half3(0.3, 0.59, 0.11), color); } + +half3 _blend_set_color_luminance(half3 hueSatColor, half alpha, half3 lumColor) { + half lum = _blend_color_luminance(lumColor); + half3 result = lum - _blend_color_luminance(hueSatColor) + hueSatColor; + half minComp = min(min(result.r, result.g), result.b); + half maxComp = max(max(result.r, result.g), result.b); + if (minComp < 0 && lum != minComp) { + result = lum + (result - lum) * lum/(lum - minComp); + } + if (maxComp > alpha && maxComp != lum) { + return lum + ((result - lum) * (alpha - lum))/(maxComp - lum); + } + return result; +} + +half _blend_color_saturation(half3 color) { + return max(max(color.r, color.g), color.b) - min(min(color.r, color.g), color.b); +} + +half3 _blend_set_color_saturation_helper(half3 minMidMax, half sat) { + if (minMidMax.r < minMidMax.b) { + return half3(0, sat*(minMidMax.g - minMidMax.r)/(minMidMax.b - minMidMax.r), sat); + } + return half3(0); +} + +half3 _blend_set_color_saturation(half3 hueLumColor, half3 satColor) { + half sat = _blend_color_saturation(satColor); + if (hueLumColor.r <= hueLumColor.g) { + if (hueLumColor.g <= hueLumColor.b) { + hueLumColor.rgb = _blend_set_color_saturation_helper(hueLumColor.rgb, sat); + } else if (hueLumColor.r <= hueLumColor.b) { + hueLumColor.rbg = _blend_set_color_saturation_helper(hueLumColor.rbg, sat); + } else { + hueLumColor.brg = _blend_set_color_saturation_helper(hueLumColor.brg, sat); + } + } else if (hueLumColor.r <= hueLumColor.b) { + hueLumColor.grb = _blend_set_color_saturation_helper(hueLumColor.grb, sat); + } else if (hueLumColor.g <= hueLumColor.b) { + hueLumColor.gbr = _blend_set_color_saturation_helper(hueLumColor.gbr, sat); + } else { + hueLumColor.bgr = _blend_set_color_saturation_helper(hueLumColor.bgr, sat); + } + return hueLumColor; +} + +half4 blend_hue(half4 src, half4 dst) { + half alpha = dst.a*src.a; + half3 sda = src.rgb*dst.a; + half3 dsa = dst.rgb*src.a; + return half4(_blend_set_color_luminance(_blend_set_color_saturation(sda, dsa), alpha, dsa) + + dst.rgb - dsa + src.rgb - sda, + src.a + dst.a - alpha); +} + +half4 blend_saturation(half4 src, half4 dst) { + half alpha = dst.a*src.a; + half3 sda = src.rgb*dst.a; + half3 dsa = dst.rgb*src.a; + return half4(_blend_set_color_luminance(_blend_set_color_saturation(dsa, sda), alpha, dsa) + + dst.rgb - dsa + src.rgb - sda, + src.a + dst.a - alpha); +} + +half4 blend_color(half4 src, half4 dst) { + half alpha = dst.a*src.a; + half3 sda = src.rgb*dst.a; + half3 dsa = dst.rgb*src.a; + return half4(_blend_set_color_luminance(sda, alpha, dsa) + dst.rgb - dsa + src.rgb - sda, + src.a + dst.a - alpha); +} + +half4 blend_luminosity(half4 src, half4 dst) { + half alpha = dst.a*src.a; + half3 sda = src.rgb*dst.a; + half3 dsa = dst.rgb*src.a; + return half4(_blend_set_color_luminance(dsa, alpha, sda) + dst.rgb - dsa + src.rgb - sda, + src.a + dst.a - alpha); + +} + +enum class SkBlendMode { + kClear = 0, + kSrc = 1, + kDst = 2, + kSrcOver = 3, + kDstOver = 4, + kSrcIn = 5, + kDstIn = 6, + kSrcOut = 7, + kDstOut = 8, + kSrcATop = 9, + kDstATop = 10, + kXor = 11, + kPlus = 12, + kModulate = 13, + kScreen = 14, + kOverlay = 15, + kDarken = 16, + kLighten = 17, + kColorDodge = 18, + kColorBurn = 19, + kHardLight = 20, + kSoftLight = 21, + kDifference = 22, + kExclusion = 23, + kMultiply = 24, + kHue = 25, + kSaturation = 26, + kColor = 27, + kLuminosity = 28 +}; + +half4 blend(SkBlendMode mode, half4 src, half4 dst) { + switch (mode) { + case SkBlendMode::kClear: return blend_clear(src, dst); + case SkBlendMode::kSrc: return blend_src(src, dst); + case SkBlendMode::kDst: return blend_dst(src, dst); + case SkBlendMode::kSrcOver: return blend_src_over(src, dst); + case SkBlendMode::kDstOver: return blend_dst_over(src, dst); + case SkBlendMode::kSrcIn: return blend_src_in(src, dst); + case SkBlendMode::kDstIn: return blend_dst_in(src, dst); + case SkBlendMode::kSrcOut: return blend_src_out(src, dst); + case SkBlendMode::kDstOut: return blend_dst_out(src, dst); + case SkBlendMode::kSrcATop: return blend_src_atop(src, dst); + case SkBlendMode::kDstATop: return blend_dst_atop(src, dst); + case SkBlendMode::kXor: return blend_xor(src, dst); + case SkBlendMode::kPlus: return blend_plus(src, dst); + case SkBlendMode::kModulate: return blend_modulate(src, dst); + case SkBlendMode::kScreen: return blend_screen(src, dst); + case SkBlendMode::kOverlay: return blend_overlay(src, dst); + case SkBlendMode::kDarken: return blend_darken(src, dst); + case SkBlendMode::kLighten: return blend_lighten(src, dst); + case SkBlendMode::kColorDodge: return blend_color_dodge(src, dst); + case SkBlendMode::kColorBurn: return blend_color_burn(src, dst); + case SkBlendMode::kHardLight: return blend_hard_light(src, dst); + case SkBlendMode::kSoftLight: return blend_soft_light(src, dst); + case SkBlendMode::kDifference: return blend_difference(src, dst); + case SkBlendMode::kExclusion: return blend_exclusion(src, dst); + case SkBlendMode::kMultiply: return blend_multiply(src, dst); + case SkBlendMode::kHue: return blend_hue(src, dst); + case SkBlendMode::kSaturation: return blend_saturation(src, dst); + case SkBlendMode::kColor: return blend_color(src, dst); + case SkBlendMode::kLuminosity: return blend_luminosity(src, dst); + } + return half4(0); // Avoids "'blend' can exit without returning a value." +} +)SKSL" diff --git a/src/sksl/sksl_gpu.inc b/src/sksl/sksl_gpu.inc index c027c2cc33..14e8baeba7 100644 --- a/src/sksl/sksl_gpu.inc +++ b/src/sksl/sksl_gpu.inc @@ -1,4 +1,4 @@ -STRINGIFY( +R"SKSL( // defines built-in functions supported by SkSL when running on a GPU @@ -295,5 +295,4 @@ float interpolateAtOffset(float interpolant, float2 offset); float2 interpolateAtOffset(float2 interpolant, float2 offset); float3 interpolateAtOffset(float3 interpolant, float2 offset); float4 interpolateAtOffset(float4 interpolant, float2 offset); - -) +)SKSL" diff --git a/tests/ProcessorTest.cpp b/tests/ProcessorTest.cpp index 978f7efe40..aa518f995f 100644 --- a/tests/ProcessorTest.cpp +++ b/tests/ProcessorTest.cpp @@ -343,10 +343,10 @@ bool log_texture_proxy(GrContext* context, sk_sp src, SkString* bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) { // With the loss of precision of rendering into 32-bit color, then estimating the FP's output - // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01 + // from that, it is not uncommon for a valid output to differ from estimate by up to 0.03 // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little // too unforgiving). - static constexpr SkScalar kTolerance = 0.01f; + static constexpr SkScalar kTolerance = 0.03f; for (int i = 0; i < 4; i++) { if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) { return false;