2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../../stdafx.h"
\r
24 #include "image_shader.h"
\r
26 #include "../util/shader.h"
\r
27 #include "../util/context.h"
\r
29 #include "blending_glsl.h"
\r
31 #include <common/gl/gl_check.h>
\r
32 #include <common/env.h>
\r
34 #include <tbb/mutex.h>
\r
36 namespace caspar { namespace accelerator { namespace ogl {
\r
38 std::shared_ptr<shader> g_shader;
\r
39 tbb::mutex g_shader_mutex;
\r
40 bool g_blend_modes = false;
\r
42 std::string get_blend_color_func()
\r
46 get_adjustement_glsl()
\r
54 "vec3 get_blend_color(vec3 back, vec3 fore) \n"
\r
56 " switch(blend_mode) \n"
\r
58 " case 0: return BlendNormal(back, fore); \n"
\r
59 " case 1: return BlendLighten(back, fore); \n"
\r
60 " case 2: return BlendDarken(back, fore); \n"
\r
61 " case 3: return BlendMultiply(back, fore); \n"
\r
62 " case 4: return BlendAverage(back, fore); \n"
\r
63 " case 5: return BlendAdd(back, fore); \n"
\r
64 " case 6: return BlendSubstract(back, fore); \n"
\r
65 " case 7: return BlendDifference(back, fore); \n"
\r
66 " case 8: return BlendNegation(back, fore); \n"
\r
67 " case 9: return BlendExclusion(back, fore); \n"
\r
68 " case 10: return BlendScreen(back, fore); \n"
\r
69 " case 11: return BlendOverlay(back, fore); \n"
\r
70 //" case 12: return BlendSoftLight(back, fore); \n"
\r
71 " case 13: return BlendHardLight(back, fore); \n"
\r
72 " case 14: return BlendColorDodge(back, fore); \n"
\r
73 " case 15: return BlendColorBurn(back, fore); \n"
\r
74 " case 16: return BlendLinearDodge(back, fore); \n"
\r
75 " case 17: return BlendLinearBurn(back, fore); \n"
\r
76 " case 18: return BlendLinearLight(back, fore); \n"
\r
77 " case 19: return BlendVividLight(back, fore); \n"
\r
78 " case 20: return BlendPinLight(back, fore); \n"
\r
79 " case 21: return BlendHardMix(back, fore); \n"
\r
80 " case 22: return BlendReflect(back, fore); \n"
\r
81 " case 23: return BlendGlow(back, fore); \n"
\r
82 " case 24: return BlendPhoenix(back, fore); \n"
\r
83 " case 25: return BlendHue(back, fore); \n"
\r
84 " case 26: return BlendSaturation(back, fore); \n"
\r
85 " case 27: return BlendColor(back, fore); \n"
\r
86 " case 28: return BlendLuminosity(back, fore); \n"
\r
88 " return BlendNormal(back, fore); \n"
\r
91 "vec4 blend(vec4 fore) \n"
\r
93 " vec4 back = texture2D(background, gl_TexCoord[1].st).bgra; \n"
\r
94 " if(blend_mode != 0) \n"
\r
95 " fore.rgb = get_blend_color(back.rgb/(back.a+0.0000001), fore.rgb/(fore.a+0.0000001))*fore.a;\n"
\r
96 " switch(keyer) \n"
\r
98 " case 1: return fore + back; // additive \n"
\r
99 " default: return fore + (1.0-fore.a)*back; // linear \n"
\r
104 std::string get_simple_blend_color_func()
\r
108 get_adjustement_glsl()
\r
112 "vec4 blend(vec4 fore) \n"
\r
118 std::string get_vertex()
\r
124 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
\r
125 " gl_TexCoord[1] = gl_MultiTexCoord1; \n"
\r
126 " gl_Position = ftransform(); \n"
\r
130 std::string get_fragment(bool blend_modes)
\r
135 "uniform sampler2D background; \n"
\r
136 "uniform sampler2D plane[4]; \n"
\r
137 "uniform sampler2D local_key; \n"
\r
138 "uniform sampler2D layer_key; \n"
\r
140 "uniform bool is_hd; \n"
\r
141 "uniform bool has_local_key; \n"
\r
142 "uniform bool has_layer_key; \n"
\r
143 "uniform int blend_mode; \n"
\r
144 "uniform int keyer; \n"
\r
145 "uniform int pixel_format; \n"
\r
147 "uniform float opacity; \n"
\r
148 "uniform bool levels; \n"
\r
149 "uniform float min_input; \n"
\r
150 "uniform float max_input; \n"
\r
151 "uniform float gamma; \n"
\r
152 "uniform float min_output; \n"
\r
153 "uniform float max_output; \n"
\r
155 "uniform bool csb; \n"
\r
156 "uniform float brt; \n"
\r
157 "uniform float sat; \n"
\r
158 "uniform float con; \n"
\r
163 (blend_modes ? get_blend_color_func() : get_simple_blend_color_func())
\r
168 "vec4 ycbcra_to_rgba_sd(float Y, float Cb, float Cr, float A) \n"
\r
171 " rgba.b = (1.164*(Y*255 - 16) + 1.596*(Cr*255 - 128))/255; \n"
\r
172 " rgba.g = (1.164*(Y*255 - 16) - 0.813*(Cr*255 - 128) - 0.391*(Cb*255 - 128))/255;\n"
\r
173 " rgba.r = (1.164*(Y*255 - 16) + 2.018*(Cb*255 - 128))/255; \n"
\r
175 " return rgba; \n"
\r
178 "vec4 ycbcra_to_rgba_hd(float Y, float Cb, float Cr, float A) \n"
\r
181 " rgba.b = (1.164*(Y*255 - 16) + 1.793*(Cr*255 - 128))/255; \n"
\r
182 " rgba.g = (1.164*(Y*255 - 16) - 0.534*(Cr*255 - 128) - 0.213*(Cb*255 - 128))/255;\n"
\r
183 " rgba.r = (1.164*(Y*255 - 16) + 2.115*(Cb*255 - 128))/255; \n"
\r
188 "vec4 ycbcra_to_rgba(float y, float cb, float cr, float a) \n"
\r
191 " return ycbcra_to_rgba_hd(y, cb, cr, a); \n"
\r
193 " return ycbcra_to_rgba_sd(y, cb, cr, a); \n"
\r
196 "vec4 get_rgba_color() \n"
\r
198 " switch(pixel_format) \n"
\r
200 " case 0: //gray \n"
\r
201 " return vec4(texture2D(plane[0], gl_TexCoord[0].st).rrr, 1.0); \n"
\r
202 " case 1: //bgra, \n"
\r
203 " return texture2D(plane[0], gl_TexCoord[0].st).bgra; \n"
\r
204 " case 2: //rgba, \n"
\r
205 " return texture2D(plane[0], gl_TexCoord[0].st).rgba; \n"
\r
206 " case 3: //argb, \n"
\r
207 " return texture2D(plane[0], gl_TexCoord[0].st).argb; \n"
\r
208 " case 4: //abgr, \n"
\r
209 " return texture2D(plane[0], gl_TexCoord[0].st).gbar; \n"
\r
210 " case 5: //ycbcr, \n"
\r
212 " float y = texture2D(plane[0], gl_TexCoord[0].st).r; \n"
\r
213 " float cb = texture2D(plane[1], gl_TexCoord[0].st).r; \n"
\r
214 " float cr = texture2D(plane[2], gl_TexCoord[0].st).r; \n"
\r
215 " return ycbcra_to_rgba(y, cb, cr, 1.0); \n"
\r
217 " case 6: //ycbcra \n"
\r
219 " float y = texture2D(plane[0], gl_TexCoord[0].st).r; \n"
\r
220 " float cb = texture2D(plane[1], gl_TexCoord[0].st).r; \n"
\r
221 " float cr = texture2D(plane[2], gl_TexCoord[0].st).r; \n"
\r
222 " float a = texture2D(plane[3], gl_TexCoord[0].st).r; \n"
\r
223 " return ycbcra_to_rgba(y, cb, cr, a); \n"
\r
225 " case 7: //luma \n"
\r
227 " vec3 y3 = texture2D(plane[0], gl_TexCoord[0].st).rrr; \n"
\r
228 " return vec4((y3-0.065)/0.859, 1.0); \n"
\r
231 " return vec4(0.0, 0.0, 0.0, 0.0); \n"
\r
236 " vec4 color = get_rgba_color(); \n"
\r
238 " color.rgb = LevelsControl(color.rgb, min_input, max_input, gamma, min_output, max_output); \n"
\r
240 " color.rgb = ContrastSaturationBrightness(color.rgb, brt, sat, con); \n"
\r
241 " if(has_local_key) \n"
\r
242 " color *= texture2D(local_key, gl_TexCoord[1].st).r; \n"
\r
243 " if(has_layer_key) \n"
\r
244 " color *= texture2D(layer_key, gl_TexCoord[1].st).r; \n"
\r
245 " color *= opacity; \n"
\r
246 " color = blend(color); \n"
\r
247 " gl_FragColor = color.bgra; \n"
\r
251 spl::shared_ptr<shader> get_image_shader(bool& blend_modes)
\r
253 tbb::mutex::scoped_lock lock(g_shader_mutex);
\r
257 blend_modes = g_blend_modes;
\r
258 return spl::make_shared_ptr(g_shader);
\r
263 g_blend_modes = glTextureBarrierNV ? env::properties().get(L"configuration.blend-modes", false) : false;
\r
264 g_shader.reset(new shader(get_vertex(), get_fragment(g_blend_modes)));
\r
268 CASPAR_LOG_CURRENT_EXCEPTION();
\r
269 CASPAR_LOG(warning) << "Failed to compile shader. Trying to compile without blend-modes.";
\r
271 g_blend_modes = false;
\r
272 g_shader.reset(new shader(get_vertex(), get_fragment(g_blend_modes)));
\r
275 //if(!g_blend_modes)
\r
277 // ogl.blend_func(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
\r
278 // CASPAR_LOG(info) << L"[shader] Blend-modes are disabled.";
\r
281 blend_modes = g_blend_modes;
\r
282 return spl::make_shared_ptr(g_shader);
\r