]> git.sesse.net Git - casparcg/blob - accelerator/ogl/image/blending_glsl.h
* Merged chroma key feature, but removed unsupported parameters and color names....
[casparcg] / accelerator / ogl / image / blending_glsl.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #pragma once
23
24 static std::string get_adjustement_glsl()
25 {
26         return R"shader(
27                         /*
28                         ** Contrast, saturation, brightness
29                         ** Code of this function is from TGM's shader pack
30                         ** http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057
31                         */
32
33                         vec3 ContrastSaturationBrightness(vec3 color, float brt, float sat, float con)
34                         {
35                                 const float AvgLumR = 0.5;
36                                 const float AvgLumG = 0.5;
37                                 const float AvgLumB = 0.5;
38
39                                 const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721);
40
41                                 vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB);
42                                 vec3 brtColor = color * brt;
43                                 vec3 intensity = vec3(dot(brtColor, LumCoeff));
44                                 vec3 satColor = mix(intensity, brtColor, sat);
45                                 vec3 conColor = mix(AvgLumin, satColor, con);
46                                 return conColor;
47                         }
48
49                         /*
50                         ** Gamma correction
51                         ** Details: http://blog.mouaif.org/2009/01/22/photoshop-gamma-correction-shader/
52                         */
53                         #define GammaCorrection(color, gamma)                                                           pow(color, vec3(1.0 / gamma))
54
55                         /*
56                         ** Levels control (input (+gamma), output)
57                         ** Details: http://blog.mouaif.org/2009/01/28/levels-control-shader/
58                         */
59
60                         #define LevelsControlInputRange(color, minInput, maxInput)                              min(max(color - vec3(minInput), vec3(0.0)) / (vec3(maxInput) - vec3(minInput)), vec3(1.0))
61                         #define LevelsControlInput(color, minInput, gamma, maxInput)                            GammaCorrection(LevelsControlInputRange(color, minInput, maxInput), gamma)
62                         #define LevelsControlOutputRange(color, minOutput, maxOutput)                   mix(vec3(minOutput), vec3(maxOutput), color)
63                         #define LevelsControl(color, minInput, gamma, maxInput, minOutput, maxOutput)   LevelsControlOutputRange(LevelsControlInput(color, minInput, gamma, maxInput), minOutput, maxOutput)
64         )shader";
65 }
66
67 static std::string get_blend_glsl()
68 {
69         static std::string glsl = R"shader(
70                         /*
71                         ** Photoshop & misc math
72                         ** Blending modes, RGB/HSL/Contrast/Desaturate, levels control
73                         **
74                         ** Romain Dura | Romz
75                         ** Blog: http://blog.mouaif.org
76                         ** Post: http://blog.mouaif.org/?p=94
77                         */
78
79
80                         /*
81                         ** Desaturation
82                         */
83
84                         vec4 Desaturate(vec3 color, float Desaturation)
85                         {
86                                 vec3 grayXfer = vec3(0.3, 0.59, 0.11);
87                                 vec3 gray = vec3(dot(grayXfer, color));
88                                 return vec4(mix(color, gray, Desaturation), 1.0);
89                         }
90
91
92                         /*
93                         ** Hue, saturation, luminance
94                         */
95
96                         vec3 RGBToHSL(vec3 color)
97                         {
98                                 vec3 hsl;
99
100                                 float fmin = min(min(color.r, color.g), color.b);
101                                 float fmax = max(max(color.r, color.g), color.b);
102                                 float delta = fmax - fmin;
103
104                                 hsl.z = (fmax + fmin) / 2.0;
105
106                                 if (delta == 0.0)
107                                 {
108                                         hsl.x = 0.0;
109                                         hsl.y = 0.0;
110                                 }
111                                 else
112                                 {
113                                         if (hsl.z < 0.5)
114                                                 hsl.y = delta / (fmax + fmin);
115                                         else
116                                                 hsl.y = delta / (2.0 - fmax - fmin);
117
118                                         float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta;
119                                         float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta;
120                                         float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta;
121
122                                         if (color.r == fmax )
123                                                 hsl.x = deltaB - deltaG;
124                                         else if (color.g == fmax)
125                                                 hsl.x = (1.0 / 3.0) + deltaR - deltaB;
126                                         else if (color.b == fmax)
127                                                 hsl.x = (2.0 / 3.0) + deltaG - deltaR;
128
129                                         if (hsl.x < 0.0)
130                                                 hsl.x += 1.0;
131                                         else if (hsl.x > 1.0)
132                                                 hsl.x -= 1.0;
133                                 }
134
135                                 return hsl;
136                         }
137
138                         float HueToRGB(float f1, float f2, float hue)
139                         {
140                                 if (hue < 0.0)
141                                         hue += 1.0;
142                                 else if (hue > 1.0)
143                                         hue -= 1.0;
144                                 float res;
145                                 if ((6.0 * hue) < 1.0)
146                                         res = f1 + (f2 - f1) * 6.0 * hue;
147                                 else if ((2.0 * hue) < 1.0)
148                                         res = f2;
149                                 else if ((3.0 * hue) < 2.0)
150                                         res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0;
151                                 else
152                                         res = f1;
153                                 return res;
154                         }
155
156                         vec3 HSLToRGB(vec3 hsl)
157                         {
158                                 vec3 rgb;
159
160                                 if (hsl.y == 0.0)
161                                         rgb = vec3(hsl.z);
162                                 else
163                                 {
164                                         float f2;
165
166                                         if (hsl.z < 0.5)
167                                                 f2 = hsl.z * (1.0 + hsl.y);
168                                         else
169                                                 f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z);
170
171                                         float f1 = 2.0 * hsl.z - f2;
172
173                                         rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0));
174                                         rgb.g = HueToRGB(f1, f2, hsl.x);
175                                         rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0));
176                                 }
177
178                                 return rgb;
179                         }
180
181
182
183
184                         /*
185                         ** Float blending modes
186                         ** Adapted from here: http://www.nathanm.com/photoshop-blending-math/
187                         ** But I modified the HardMix (wrong condition), Overlay, SoftLight, ColorDodge, ColorBurn, VividLight, PinLight (inverted layers) ones to have correct results
188                         */
189
190                         #define BlendLinearDodgef                                       BlendAddf
191                         #define BlendLinearBurnf                                        BlendSubstractf
192                         #define BlendAddf(base, blend)                          min(base + blend, 1.0)
193                         #define BlendSubstractf(base, blend)            max(base + blend - 1.0, 0.0)
194                         #define BlendLightenf(base, blend)              max(blend, base)
195                         #define BlendDarkenf(base, blend)                       min(blend, base)
196                         #define BlendLinearLightf(base, blend)  (blend < 0.5 ? BlendLinearBurnf(base, (2.0 * blend)) : BlendLinearDodgef(base, (2.0 * (blend - 0.5))))
197                         #define BlendScreenf(base, blend)                       (1.0 - ((1.0 - base) * (1.0 - blend)))
198                         #define BlendOverlayf(base, blend)              (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend)))
199                         #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)))
200                         #define BlendColorDodgef(base, blend)           ((blend == 1.0) ? blend : min(base / (1.0 - blend), 1.0))
201                         #define BlendColorBurnf(base, blend)            ((blend == 0.0) ? blend : max((1.0 - ((1.0 - base) / blend)), 0.0))
202                         #define BlendVividLightf(base, blend)           ((blend < 0.5) ? BlendColorBurnf(base, (2.0 * blend)) : BlendColorDodgef(base, (2.0 * (blend - 0.5))))
203                         #define BlendPinLightf(base, blend)             ((blend < 0.5) ? BlendDarkenf(base, (2.0 * blend)) : BlendLightenf(base, (2.0 *(blend - 0.5))))
204                         #define BlendHardMixf(base, blend)              ((BlendVividLightf(base, blend) < 0.5) ? 0.0 : 1.0)
205                         #define BlendReflectf(base, blend)              ((blend == 1.0) ? blend : min(base * base / (1.0 - blend), 1.0))
206
207
208                         /*
209                         ** Vector3 blending modes
210                         */
211
212                         #define Blend(base, blend, funcf)                       vec3(funcf(base.r, blend.r), funcf(base.g, blend.g), funcf(base.b, blend.b))
213
214                         #define BlendNormal(base, blend)                        (blend)
215                         #define BlendLighten                                            BlendLightenf
216                         #define BlendDarken                                             BlendDarkenf
217                         #define BlendMultiply(base, blend)              (base * blend)
218                         #define BlendAverage(base, blend)                       ((base + blend) / 2.0)
219                         #define BlendAdd(base, blend)                           min(base + blend, vec3(1.0))
220                         #define BlendSubstract(base, blend)             max(base + blend - vec3(1.0), vec3(0.0))
221                         #define BlendDifference(base, blend)            abs(base - blend)
222                         #define BlendNegation(base, blend)              (vec3(1.0) - abs(vec3(1.0) - base - blend))
223                         #define BlendExclusion(base, blend)             (base + blend - 2.0 * base * blend)
224                         #define BlendScreen(base, blend)                        Blend(base, blend, BlendScreenf)
225                         #define BlendOverlay(base, blend)                       Blend(base, blend, BlendOverlayf)
226                         #define BlendSoftLight(base, blend)             Blend(base, blend, BlendSoftLightf)
227                         #define BlendHardLight(base, blend)             BlendOverlay(blend, base)
228                         #define BlendColorDodge(base, blend)            Blend(base, blend, BlendColorDodgef)
229                         #define BlendColorBurn(base, blend)             Blend(base, blend, BlendColorBurnf)
230                         #define BlendLinearDodge                                        BlendAdd
231                         #define BlendLinearBurn                                 BlendSubstract
232                         #define BlendLinearLight(base, blend)           Blend(base, blend, BlendLinearLightf)
233                         #define BlendVividLight(base, blend)            Blend(base, blend, BlendVividLightf)
234                         #define BlendPinLight(base, blend)              Blend(base, blend, BlendPinLightf)
235                         #define BlendHardMix(base, blend)                       Blend(base, blend, BlendHardMixf)
236                         #define BlendReflect(base, blend)                       Blend(base, blend, BlendReflectf)
237                         #define BlendGlow(base, blend)                  BlendReflect(blend, base)
238                         #define BlendPhoenix(base, blend)                       (min(base, blend) - max(base, blend) + vec3(1.0))
239                         #define BlendOpacity(base, blend, F, O)         (F(base, blend) * O + blend * (1.0 - O))
240
241
242                         vec3 BlendHue(vec3 base, vec3 blend)
243                         {
244                                 vec3 baseHSL = RGBToHSL(base);
245                                 return HSLToRGB(vec3(RGBToHSL(blend).r, baseHSL.g, baseHSL.b));
246                         }
247
248                         vec3 BlendSaturation(vec3 base, vec3 blend)
249                         {
250                                 vec3 baseHSL = RGBToHSL(base);
251                                 return HSLToRGB(vec3(baseHSL.r, RGBToHSL(blend).g, baseHSL.b));
252                         }
253
254                         vec3 BlendColor(vec3 base, vec3 blend)
255                         {
256                                 vec3 blendHSL = RGBToHSL(blend);
257                                 return HSLToRGB(vec3(blendHSL.r, blendHSL.g, RGBToHSL(base).b));
258                         }
259
260                         vec3 BlendLuminosity(vec3 base, vec3 blend)
261                         {
262                                 vec3 baseHSL = RGBToHSL(base);
263                                 return HSLToRGB(vec3(baseHSL.r, baseHSL.g, RGBToHSL(blend).b));
264                         }
265
266         )shader";
267
268         return glsl;
269 }
270
271 static std::string get_chroma_glsl()
272 {
273         static std::string glsl = R"shader(
274                 // Chroma keying
275                 // Author: Tim Eves <timseves@googlemail.com>
276                 //
277                 // This implements the Chroma key algorithm described in the paper:
278                 //      'Software Chroma Keying in an Imersive Virtual Environment'
279                 //      by F. van den Bergh & V. Lalioti
280                 // but as a pixel shader algorithm.
281                 //
282
283                 float       chroma_blend_w = chroma_blend.y - chroma_blend.x;
284                 const vec4  grey_xfer  = vec4(0.3, 0.59, 0.11, 0.0);
285
286                 float fma(float a, float b, float c) { return a*b + c; }
287
288                 // This allows us to implement the paper's alphaMap curve in software
289                 // rather than a largeish array
290                 float alpha_map(float d)
291                 {
292                     return 1.0-smoothstep(chroma_blend.x, chroma_blend.y, d);
293                 }
294
295                 vec4 supress_spill(vec4 c, float d)
296                 {
297                     float ds = smoothstep(chroma_spill, 1.0, d/chroma_blend.y);
298                     float gl = dot(grey_xfer, c);
299                     return mix(c, vec4(vec3(gl*gl), gl), ds);
300                 }
301
302                 // Key on green
303                 vec4 ChromaOnGreen(vec4 c)
304                 {
305                     float d = fma(2.0, c.g, -c.r - c.b)/2.0;
306                     c *= alpha_map(d);
307                     return supress_spill(c, d);
308                 }
309
310                 //Key on blue
311                 vec4 ChromaOnBlue(vec4 c)
312                 {
313                     float d = fma(2.0, c.b, -c.r - c.g)/2.0;
314                     c *= alpha_map(d);
315                     return supress_spill(c, d);
316                 }
317         )shader";
318
319         return glsl;
320 }