-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG (www.casparcg.com).\r
-*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
-*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-* GNU General Public License for more details.\r
-*\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-* Author: Robert Nagy, ronag89@gmail.com\r
-*/\r
-\r
-#pragma once\r
-\r
-static std::string get_adjustement_glsl()\r
-{\r
- return \r
- "\n /* "\r
- "\n ** Contrast, saturation, brightness "\r
- "\n ** Code of this function is from TGM's shader pack "\r
- "\n ** http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057 "\r
- "\n */ "\r
- "\n "\r
- "\n vec3 ContrastSaturationBrightness(vec3 color, float brt, float sat, float con) "\r
- "\n { "\r
- "\n const float AvgLumR = 0.5; "\r
- "\n const float AvgLumG = 0.5; "\r
- "\n const float AvgLumB = 0.5; "\r
- "\n "\r
- "\n const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721); "\r
- "\n "\r
- "\n vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB); "\r
- "\n vec3 brtColor = color * brt; "\r
- "\n vec3 intensity = vec3(dot(brtColor, LumCoeff)); "\r
- "\n vec3 satColor = mix(intensity, brtColor, sat); "\r
- "\n vec3 conColor = mix(AvgLumin, satColor, con); "\r
- "\n return conColor; "\r
- "\n } "\r
- "\n "\r
- "\n /* "\r
- "\n ** Gamma correction "\r
- "\n ** Details: http://blog.mouaif.org/2009/01/22/photoshop-gamma-correction-shader/ "\r
- "\n */ "\r
- "\n "\r
- "\n#define GammaCorrection(color, gamma) pow(color, vec3(1.0 / gamma)) \n "\r
- "\n "\r
- "\n /* "\r
- "\n ** Levels control (input (+gamma), output) "\r
- "\n ** Details: http://blog.mouaif.org/2009/01/28/levels-control-shader/ "\r
- "\n */ "\r
- "\n "\r
- "\n#define LevelsControlInputRange(color, minInput, maxInput) min(max(color - vec3(minInput), vec3(0.0)) / (vec3(maxInput) - vec3(minInput)), vec3(1.0)) \n "\r
- "\n#define LevelsControlInput(color, minInput, gamma, maxInput) GammaCorrection(LevelsControlInputRange(color, minInput, maxInput), gamma) \n " \r
- "\n#define LevelsControlOutputRange(color, minOutput, maxOutput) mix(vec3(minOutput), vec3(maxOutput), color) \n "\r
- "\n#define LevelsControl(color, minInput, gamma, maxInput, minOutput, maxOutput) LevelsControlOutputRange(LevelsControlInput(color, minInput, gamma, maxInput), minOutput, maxOutput) \n "\r
- ;\r
-}\r
-\r
-static std::string get_blend_glsl()\r
-{\r
- static std::string glsl = \r
- "\n /* "\r
- "\n ** Photoshop & misc math "\r
- "\n ** Blending modes, RGB/HSL/Contrast/Desaturate, levels control "\r
- "\n ** "\r
- "\n ** Romain Dura | Romz "\r
- "\n ** Blog: http://blog.mouaif.org "\r
- "\n ** Post: http://blog.mouaif.org/?p=94 "\r
- "\n */ "\r
- "\n "\r
- "\n "\r
- "\n /* "\r
- "\n ** Desaturation "\r
- "\n */ "\r
- "\n "\r
- "\n vec4 Desaturate(vec3 color, float Desaturation) "\r
- "\n { "\r
- "\n vec3 grayXfer = vec3(0.3, 0.59, 0.11); "\r
- "\n vec3 gray = vec3(dot(grayXfer, color)); "\r
- "\n return vec4(mix(color, gray, Desaturation), 1.0); "\r
- "\n } "\r
- "\n "\r
- "\n "\r
- "\n /* "\r
- "\n ** Hue, saturation, luminance "\r
- "\n */ "\r
- "\n "\r
- "\n vec3 RGBToHSL(vec3 color) "\r
- "\n { "\r
- "\n vec3 hsl; "\r
- "\n "\r
- "\n float fmin = min(min(color.r, color.g), color.b); "\r
- "\n float fmax = max(max(color.r, color.g), color.b); "\r
- "\n float delta = fmax - fmin; "\r
- "\n "\r
- "\n hsl.z = (fmax + fmin) / 2.0; "\r
- "\n "\r
- "\n if (delta == 0.0) "\r
- "\n { "\r
- "\n hsl.x = 0.0; "\r
- "\n hsl.y = 0.0; "\r
- "\n } "\r
- "\n else "\r
- "\n { "\r
- "\n if (hsl.z < 0.5) "\r
- "\n hsl.y = delta / (fmax + fmin); "\r
- "\n else "\r
- "\n hsl.y = delta / (2.0 - fmax - fmin); "\r
- "\n "\r
- "\n float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta; "\r
- "\n float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta; "\r
- "\n float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta; "\r
- "\n "\r
- "\n if (color.r == fmax ) "\r
- "\n hsl.x = deltaB - deltaG; "\r
- "\n else if (color.g == fmax) "\r
- "\n hsl.x = (1.0 / 3.0) + deltaR - deltaB; "\r
- "\n else if (color.b == fmax) "\r
- "\n hsl.x = (2.0 / 3.0) + deltaG - deltaR; "\r
- "\n "\r
- "\n if (hsl.x < 0.0) "\r
- "\n hsl.x += 1.0; "\r
- "\n else if (hsl.x > 1.0) "\r
- "\n hsl.x -= 1.0; "\r
- "\n } "\r
- "\n "\r
- "\n return hsl; "\r
- "\n } "\r
- "\n "\r
- "\n float HueToRGB(float f1, float f2, float hue) "\r
- "\n { "\r
- "\n if (hue < 0.0) "\r
- "\n hue += 1.0; "\r
- "\n else if (hue > 1.0) "\r
- "\n hue -= 1.0; "\r
- "\n float res; "\r
- "\n if ((6.0 * hue) < 1.0) "\r
- "\n res = f1 + (f2 - f1) * 6.0 * hue; "\r
- "\n else if ((2.0 * hue) < 1.0) "\r
- "\n res = f2; "\r
- "\n else if ((3.0 * hue) < 2.0) "\r
- "\n res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0; "\r
- "\n else "\r
- "\n res = f1; "\r
- "\n return res; "\r
- "\n } "\r
- "\n "\r
- "\n vec3 HSLToRGB(vec3 hsl) "\r
- "\n { "\r
- "\n vec3 rgb; "\r
- "\n "\r
- "\n if (hsl.y == 0.0) "\r
- "\n rgb = vec3(hsl.z); "\r
- "\n else "\r
- "\n { "\r
- "\n float f2; "\r
- "\n "\r
- "\n if (hsl.z < 0.5) "\r
- "\n f2 = hsl.z * (1.0 + hsl.y); "\r
- "\n else "\r
- "\n f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z); "\r
- "\n "\r
- "\n float f1 = 2.0 * hsl.z - f2; "\r
- "\n "\r
- "\n rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0)); "\r
- "\n rgb.g = HueToRGB(f1, f2, hsl.x); "\r
- "\n rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0)); "\r
- "\n } "\r
- "\n "\r
- "\n return rgb; "\r
- "\n } "\r
- "\n "\r
- "\n "\r
- "\n "\r
- "\n "\r
- "\n /* "\r
- "\n ** Float blending modes "\r
- "\n ** Adapted from here: http://www.nathanm.com/photoshop-blending-math/ "\r
- "\n ** But I modified the HardMix (wrong condition), Overlay, SoftLight, ColorDodge, ColorBurn, VividLight, PinLight (inverted layers) ones to have correct results "\r
- "\n */ "\r
- "\n \n "\r
- "\n#define BlendLinearDodgef BlendAddf \n "\r
- "\n#define BlendLinearBurnf BlendSubstractf \n "\r
- "\n#define BlendAddf(base, blend) min(base + blend, 1.0) \n "\r
- "\n#define BlendSubstractf(base, blend) max(base + blend - 1.0, 0.0) \n "\r
- "\n#define BlendLightenf(base, blend) max(blend, base) \n "\r
- "\n#define BlendDarkenf(base, blend) min(blend, base) \n "\r
- "\n#define BlendLinearLightf(base, blend) (blend < 0.5 ? BlendLinearBurnf(base, (2.0 * blend)) : BlendLinearDodgef(base, (2.0 * (blend - 0.5)))) \n "\r
- "\n#define BlendScreenf(base, blend) (1.0 - ((1.0 - base) * (1.0 - blend))) \n "\r
- "\n#define BlendOverlayf(base, blend) (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend))) \n "\r
- "\n#define BlendSoftLightf(base, blend) ((blend < 0.5) ? (2.0 * base * blend + base * base * (1.0 - 2.0 * blend)) : (sqrt(base) * (2.0 * blend - 1.0) + 2.0 * base * (1.0 - blend))) \n "\r
- "\n#define BlendColorDodgef(base, blend) ((blend == 1.0) ? blend : min(base / (1.0 - blend), 1.0)) \n "\r
- "\n#define BlendColorBurnf(base, blend) ((blend == 0.0) ? blend : max((1.0 - ((1.0 - base) / blend)), 0.0)) \n "\r
- "\n#define BlendVividLightf(base, blend) ((blend < 0.5) ? BlendColorBurnf(base, (2.0 * blend)) : BlendColorDodgef(base, (2.0 * (blend - 0.5)))) \n "\r
- "\n#define BlendPinLightf(base, blend) ((blend < 0.5) ? BlendDarkenf(base, (2.0 * blend)) : BlendLightenf(base, (2.0 *(blend - 0.5)))) \n "\r
- "\n#define BlendHardMixf(base, blend) ((BlendVividLightf(base, blend) < 0.5) ? 0.0 : 1.0) \n "\r
- "\n#define BlendReflectf(base, blend) ((blend == 1.0) ? blend : min(base * base / (1.0 - blend), 1.0)) \n "\r
- "\n "\r
- "\n "\r
- "\n /* "\r
- "\n ** Vector3 blending modes "\r
- "\n */ "\r
- "\n "\r
- "\n#define Blend(base, blend, funcf) vec3(funcf(base.r, blend.r), funcf(base.g, blend.g), funcf(base.b, blend.b)) \n "\r
- "\n \n "\r
- "\n#define BlendNormal(base, blend) (blend) \n "\r
- "\n#define BlendLighten BlendLightenf \n "\r
- "\n#define BlendDarken BlendDarkenf \n "\r
- "\n#define BlendMultiply(base, blend) (base * blend) \n "\r
- "\n#define BlendAverage(base, blend) ((base + blend) / 2.0) \n "\r
- "\n#define BlendAdd(base, blend) min(base + blend, vec3(1.0)) \n "\r
- "\n#define BlendSubstract(base, blend) max(base + blend - vec3(1.0), vec3(0.0)) \n "\r
- "\n#define BlendDifference(base, blend) abs(base - blend) \n "\r
- "\n#define BlendNegation(base, blend) (vec3(1.0) - abs(vec3(1.0) - base - blend)) \n "\r
- "\n#define BlendExclusion(base, blend) (base + blend - 2.0 * base * blend) \n "\r
- "\n#define BlendScreen(base, blend) Blend(base, blend, BlendScreenf) \n "\r
- "\n#define BlendOverlay(base, blend) Blend(base, blend, BlendOverlayf) \n "\r
- "\n#define BlendSoftLight(base, blend) Blend(base, blend, BlendSoftLightf) \n "\r
- "\n#define BlendHardLight(base, blend) BlendOverlay(blend, base) \n "\r
- "\n#define BlendColorDodge(base, blend) Blend(base, blend, BlendColorDodgef) \n "\r
- "\n#define BlendColorBurn(base, blend) Blend(base, blend, BlendColorBurnf) \n "\r
- "\n#define BlendLinearDodge BlendAdd \n "\r
- "\n#define BlendLinearBurn BlendSubstract \n "\r
- "\n#define BlendLinearLight(base, blend) Blend(base, blend, BlendLinearLightf) \n "\r
- "\n#define BlendVividLight(base, blend) Blend(base, blend, BlendVividLightf) \n "\r
- "\n#define BlendPinLight(base, blend) Blend(base, blend, BlendPinLightf) \n "\r
- "\n#define BlendHardMix(base, blend) Blend(base, blend, BlendHardMixf) \n "\r
- "\n#define BlendReflect(base, blend) Blend(base, blend, BlendReflectf) \n "\r
- "\n#define BlendGlow(base, blend) BlendReflect(blend, base) \n "\r
- "\n#define BlendPhoenix(base, blend) (min(base, blend) - max(base, blend) + vec3(1.0)) \n "\r
- "\n#define BlendOpacity(base, blend, F, O) (F(base, blend) * O + blend * (1.0 - O)) \n "\r
- "\n "\r
- "\n "\r
- "\n vec3 BlendHue(vec3 base, vec3 blend) "\r
- "\n { "\r
- "\n vec3 baseHSL = RGBToHSL(base); "\r
- "\n return HSLToRGB(vec3(RGBToHSL(blend).r, baseHSL.g, baseHSL.b)); "\r
- "\n } "\r
- "\n "\r
- "\n vec3 BlendSaturation(vec3 base, vec3 blend) "\r
- "\n { "\r
- "\n vec3 baseHSL = RGBToHSL(base); "\r
- "\n return HSLToRGB(vec3(baseHSL.r, RGBToHSL(blend).g, baseHSL.b)); "\r
- "\n } "\r
- "\n "\r
- "\n vec3 BlendColor(vec3 base, vec3 blend) "\r
- "\n { "\r
- "\n vec3 blendHSL = RGBToHSL(blend); "\r
- "\n return HSLToRGB(vec3(blendHSL.r, blendHSL.g, RGBToHSL(base).b)); "\r
- "\n } "\r
- "\n "\r
- "\n vec3 BlendLuminosity(vec3 base, vec3 blend) "\r
- "\n { "\r
- "\n vec3 baseHSL = RGBToHSL(base); "\r
- "\n return HSLToRGB(vec3(baseHSL.r, baseHSL.g, RGBToHSL(blend).b)); "\r
- "\n } "\r
- "\n "\r
- ;\r
- \r
- return glsl;\r
-}
\ No newline at end of file
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#pragma once
+
+static std::string get_adjustement_glsl()
+{
+ return R"shader(
+ /*
+ ** Contrast, saturation, brightness
+ ** Code of this function is from TGM's shader pack
+ ** http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057
+ */
+
+ vec3 ContrastSaturationBrightness(vec4 color, float brt, float sat, float con)
+ {
+ const float AvgLumR = 0.5;
+ const float AvgLumG = 0.5;
+ const float AvgLumB = 0.5;
+
+ vec3 LumCoeff = is_hd
+ ? vec3(0.0722, 0.7152, 0.2126)
+ : vec3(0.114, 0.587, 0.299);
+
+ if (color.a > 0.0)
+ color.rgb /= color.a;
+
+ vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB);
+ vec3 brtColor = color.rgb * brt;
+ vec3 intensity = vec3(dot(brtColor, LumCoeff));
+ vec3 satColor = mix(intensity, brtColor, sat);
+ vec3 conColor = mix(AvgLumin, satColor, con);
+
+ conColor.rgb *= color.a;
+
+ return conColor;
+ }
+
+ /*
+ ** Gamma correction
+ ** Details: http://blog.mouaif.org/2009/01/22/photoshop-gamma-correction-shader/
+ */
+ #define GammaCorrection(color, gamma) pow(color, vec3(1.0 / gamma))
+
+ /*
+ ** Levels control (input (+gamma), output)
+ ** Details: http://blog.mouaif.org/2009/01/28/levels-control-shader/
+ */
+
+ #define LevelsControlInputRange(color, minInput, maxInput) min(max(color - vec3(minInput), vec3(0.0)) / (vec3(maxInput) - vec3(minInput)), vec3(1.0))
+ #define LevelsControlInput(color, minInput, gamma, maxInput) GammaCorrection(LevelsControlInputRange(color, minInput, maxInput), gamma)
+ #define LevelsControlOutputRange(color, minOutput, maxOutput) mix(vec3(minOutput), vec3(maxOutput), color)
+ #define LevelsControl(color, minInput, gamma, maxInput, minOutput, maxOutput) LevelsControlOutputRange(LevelsControlInput(color, minInput, gamma, maxInput), minOutput, maxOutput)
+ )shader";
+}
+
+static std::string get_blend_glsl()
+{
+ static std::string glsl = R"shader(
+ /*
+ ** Photoshop & misc math
+ ** Blending modes, RGB/HSL/Contrast/Desaturate, levels control
+ **
+ ** Romain Dura | Romz
+ ** Blog: http://blog.mouaif.org
+ ** Post: http://blog.mouaif.org/?p=94
+ */
+
+
+ /*
+ ** Desaturation
+ */
+
+ vec4 Desaturate(vec3 color, float Desaturation)
+ {
+ vec3 grayXfer = vec3(0.3, 0.59, 0.11);
+ vec3 gray = vec3(dot(grayXfer, color));
+ return vec4(mix(color, gray, Desaturation), 1.0);
+ }
+
+
+ /*
+ ** Hue, saturation, luminance
+ */
+
+ vec3 RGBToHSL(vec3 color)
+ {
+ vec3 hsl;
+
+ float fmin = min(min(color.r, color.g), color.b);
+ float fmax = max(max(color.r, color.g), color.b);
+ float delta = fmax - fmin;
+
+ hsl.z = (fmax + fmin) / 2.0;
+
+ if (delta == 0.0)
+ {
+ hsl.x = 0.0;
+ hsl.y = 0.0;
+ }
+ else
+ {
+ if (hsl.z < 0.5)
+ hsl.y = delta / (fmax + fmin);
+ else
+ hsl.y = delta / (2.0 - fmax - fmin);
+
+ float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta;
+ float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta;
+ float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta;
+
+ if (color.r == fmax )
+ hsl.x = deltaB - deltaG;
+ else if (color.g == fmax)
+ hsl.x = (1.0 / 3.0) + deltaR - deltaB;
+ else if (color.b == fmax)
+ hsl.x = (2.0 / 3.0) + deltaG - deltaR;
+
+ if (hsl.x < 0.0)
+ hsl.x += 1.0;
+ else if (hsl.x > 1.0)
+ hsl.x -= 1.0;
+ }
+
+ return hsl;
+ }
+
+ float HueToRGB(float f1, float f2, float hue)
+ {
+ if (hue < 0.0)
+ hue += 1.0;
+ else if (hue > 1.0)
+ hue -= 1.0;
+ float res;
+ if ((6.0 * hue) < 1.0)
+ res = f1 + (f2 - f1) * 6.0 * hue;
+ else if ((2.0 * hue) < 1.0)
+ res = f2;
+ else if ((3.0 * hue) < 2.0)
+ res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0;
+ else
+ res = f1;
+ return res;
+ }
+
+ vec3 HSLToRGB(vec3 hsl)
+ {
+ vec3 rgb;
+
+ if (hsl.y == 0.0)
+ rgb = vec3(hsl.z);
+ else
+ {
+ float f2;
+
+ if (hsl.z < 0.5)
+ f2 = hsl.z * (1.0 + hsl.y);
+ else
+ f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z);
+
+ float f1 = 2.0 * hsl.z - f2;
+
+ rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0));
+ rgb.g = HueToRGB(f1, f2, hsl.x);
+ rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0));
+ }
+
+ return rgb;
+ }
+
+
+
+
+ /*
+ ** Float blending modes
+ ** Adapted from here: http://www.nathanm.com/photoshop-blending-math/
+ ** But I modified the HardMix (wrong condition), Overlay, SoftLight, ColorDodge, ColorBurn, VividLight, PinLight (inverted layers) ones to have correct results
+ */
+
+ #define BlendLinearDodgef BlendAddf
+ #define BlendLinearBurnf BlendSubstractf
+ #define BlendAddf(base, blend) min(base + blend, 1.0)
+ #define BlendSubstractf(base, blend) max(base + blend - 1.0, 0.0)
+ #define BlendLightenf(base, blend) max(blend, base)
+ #define BlendDarkenf(base, blend) min(blend, base)
+ #define BlendLinearLightf(base, blend) (blend < 0.5 ? BlendLinearBurnf(base, (2.0 * blend)) : BlendLinearDodgef(base, (2.0 * (blend - 0.5))))
+ #define BlendScreenf(base, blend) (1.0 - ((1.0 - base) * (1.0 - blend)))
+ #define BlendOverlayf(base, blend) (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend)))
+ #define BlendSoftLightf(base, blend) ((blend < 0.5) ? (2.0 * base * blend + base * base * (1.0 - 2.0 * blend)) : (sqrt(base) * (2.0 * blend - 1.0) + 2.0 * base * (1.0 - blend)))
+ #define BlendColorDodgef(base, blend) ((blend == 1.0) ? blend : min(base / (1.0 - blend), 1.0))
+ #define BlendColorBurnf(base, blend) ((blend == 0.0) ? blend : max((1.0 - ((1.0 - base) / blend)), 0.0))
+ #define BlendVividLightf(base, blend) ((blend < 0.5) ? BlendColorBurnf(base, (2.0 * blend)) : BlendColorDodgef(base, (2.0 * (blend - 0.5))))
+ #define BlendPinLightf(base, blend) ((blend < 0.5) ? BlendDarkenf(base, (2.0 * blend)) : BlendLightenf(base, (2.0 *(blend - 0.5))))
+ #define BlendHardMixf(base, blend) ((BlendVividLightf(base, blend) < 0.5) ? 0.0 : 1.0)
+ #define BlendReflectf(base, blend) ((blend == 1.0) ? blend : min(base * base / (1.0 - blend), 1.0))
+
+
+ /*
+ ** Vector3 blending modes
+ */
+
+ #define Blend(base, blend, funcf) vec3(funcf(base.r, blend.r), funcf(base.g, blend.g), funcf(base.b, blend.b))
+
+ #define BlendNormal(base, blend) (blend)
+ #define BlendLighten BlendLightenf
+ #define BlendDarken BlendDarkenf
+ #define BlendMultiply(base, blend) (base * blend)
+ #define BlendAverage(base, blend) ((base + blend) / 2.0)
+ #define BlendAdd(base, blend) min(base + blend, vec3(1.0))
+ #define BlendSubstract(base, blend) max(base + blend - vec3(1.0), vec3(0.0))
+ #define BlendDifference(base, blend) abs(base - blend)
+ #define BlendNegation(base, blend) (vec3(1.0) - abs(vec3(1.0) - base - blend))
+ #define BlendExclusion(base, blend) (base + blend - 2.0 * base * blend)
+ #define BlendScreen(base, blend) Blend(base, blend, BlendScreenf)
+ #define BlendOverlay(base, blend) Blend(base, blend, BlendOverlayf)
+ #define BlendSoftLight(base, blend) Blend(base, blend, BlendSoftLightf)
+ #define BlendHardLight(base, blend) BlendOverlay(blend, base)
+ #define BlendColorDodge(base, blend) Blend(base, blend, BlendColorDodgef)
+ #define BlendColorBurn(base, blend) Blend(base, blend, BlendColorBurnf)
+ #define BlendLinearDodge BlendAdd
+ #define BlendLinearBurn BlendSubstract
+ #define BlendLinearLight(base, blend) Blend(base, blend, BlendLinearLightf)
+ #define BlendVividLight(base, blend) Blend(base, blend, BlendVividLightf)
+ #define BlendPinLight(base, blend) Blend(base, blend, BlendPinLightf)
+ #define BlendHardMix(base, blend) Blend(base, blend, BlendHardMixf)
+ #define BlendReflect(base, blend) Blend(base, blend, BlendReflectf)
+ #define BlendGlow(base, blend) BlendReflect(blend, base)
+ #define BlendPhoenix(base, blend) (min(base, blend) - max(base, blend) + vec3(1.0))
+ #define BlendOpacity(base, blend, F, O) (F(base, blend) * O + blend * (1.0 - O))
+
+
+ vec3 BlendHue(vec3 base, vec3 blend)
+ {
+ vec3 baseHSL = RGBToHSL(base);
+ return HSLToRGB(vec3(RGBToHSL(blend).r, baseHSL.g, baseHSL.b));
+ }
+
+ vec3 BlendSaturation(vec3 base, vec3 blend)
+ {
+ vec3 baseHSL = RGBToHSL(base);
+ return HSLToRGB(vec3(baseHSL.r, RGBToHSL(blend).g, baseHSL.b));
+ }
+
+ vec3 BlendColor(vec3 base, vec3 blend)
+ {
+ vec3 blendHSL = RGBToHSL(blend);
+ return HSLToRGB(vec3(blendHSL.r, blendHSL.g, RGBToHSL(base).b));
+ }
+
+ vec3 BlendLuminosity(vec3 base, vec3 blend)
+ {
+ vec3 baseHSL = RGBToHSL(base);
+ return HSLToRGB(vec3(baseHSL.r, baseHSL.g, RGBToHSL(blend).b));
+ }
+
+ )shader";
+
+ return glsl;
+}
+
+static std::string get_chroma_glsl()
+{
+ static std::string glsl = R"shader(
+ // Chroma keying
+ // Author: Tim Eves <timseves@googlemail.com>
+ //
+ // This implements the Chroma key algorithm described in the paper:
+ // 'Software Chroma Keying in an Imersive Virtual Environment'
+ // by F. van den Bergh & V. Lalioti
+ // but as a pixel shader algorithm.
+ //
+ vec4 grey_xfer = is_hd
+ ? vec4(0.2126, 0.7152, 0.0722, 0)
+ : vec4(0.299, 0.587, 0.114, 0);
+
+ // This allows us to implement the paper's alphaMap curve in software
+ // rather than a largeish array
+ float alpha_map(float d)
+ {
+ return 1.0 - smoothstep(1.0, chroma_softness, d);
+ }
+
+ // http://stackoverflow.com/questions/15095909/from-rgb-to-hsv-in-opengl-glsl
+ vec3 rgb2hsv(vec3 c)
+ {
+ vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
+ vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
+ vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
+
+ float d = q.x - min(q.w, q.y);
+ float e = 1.0e-10;
+ return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
+ }
+
+ // From the same page
+ vec3 hsv2rgb(vec3 c)
+ {
+ vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
+ vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
+ return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
+ }
+
+ float AngleDiff(float angle1, float angle2)
+ {
+ return 0.5 - abs(abs(angle1 - angle2) - 0.5);
+ }
+
+ float AngleDiffDirectional(float angle1, float angle2)
+ {
+ float diff = angle1 - angle2;
+
+ return diff < -0.5
+ ? diff + 1.0
+ : (diff > 0.5 ? diff - 1.0 : diff);
+ }
+
+ float Distance(float actual, float target)
+ {
+ return min(0.0, target - actual);
+ }
+
+ float ColorDistance(vec3 hsv)
+ {
+ float hueDiff = AngleDiff(hsv.x, chroma_target_hue) * 2;
+ float saturationDiff = Distance(hsv.y, chroma_min_saturation);
+ float brightnessDiff = Distance(hsv.z, chroma_min_brightness);
+
+ float saturationBrightnessScore = max(brightnessDiff, saturationDiff);
+ float hueScore = hueDiff - chroma_hue_width;
+
+ return -hueScore * saturationBrightnessScore;
+ }
+
+ vec3 supress_spill(vec3 c)
+ {
+ float hue = c.x;
+ float diff = AngleDiffDirectional(hue, chroma_target_hue);
+ float distance = abs(diff) / chroma_spill_suppress;
+
+ if (distance < 1)
+ {
+ c.x = diff < 0
+ ? chroma_target_hue - chroma_spill_suppress
+ : chroma_target_hue + chroma_spill_suppress;
+ c.y *= min(1.0, distance + chroma_spill_suppress_saturation);
+ }
+
+ return c;
+ }
+
+ // Key on any color
+ vec4 ChromaOnCustomColor(vec4 c)
+ {
+ vec3 hsv = rgb2hsv(c.rgb);
+ float distance = ColorDistance(hsv);
+ float d = distance * -2.0 + 1.0;
+ vec4 suppressed = vec4(hsv2rgb(supress_spill(hsv)), 1.0);
+ float alpha = alpha_map(d);
+
+ suppressed *= alpha;
+
+ return chroma_show_mask ? vec4(suppressed.a, suppressed.a, suppressed.a, 1) : suppressed;
+ }
+ )shader";
+
+ return glsl;
+}