2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
20 #include "../../stdafx.h"
\r
22 #include "image_kernel.h"
\r
24 #include "blending_glsl.h"
\r
25 #include "../gpu/shader.h"
\r
26 #include "../gpu/device_buffer.h"
\r
27 #include "../gpu/ogl_device.h"
\r
29 #include <common/exception/exceptions.h>
\r
30 #include <common/gl/gl_check.h>
\r
31 #include <common/env.h>
\r
33 #include <core/video_format.h>
\r
34 #include <core/producer/frame/pixel_format.h>
\r
35 #include <core/producer/frame/image_transform.h>
\r
37 #include <GL/glew.h>
\r
39 #include <boost/noncopyable.hpp>
\r
41 #include <tbb/mutex.h>
\r
43 #include <unordered_map>
\r
45 namespace caspar { namespace core {
\r
47 GLubyte upper_pattern[] = {
\r
48 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
\r
49 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
\r
50 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
\r
51 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00};
\r
53 GLubyte lower_pattern[] = {
\r
54 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
\r
55 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
\r
56 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
\r
57 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff};
\r
59 struct image_kernel::implementation : boost::noncopyable
\r
61 std::unique_ptr<shader> shader_;
\r
64 implementation() : blend_modes_(true)
\r
68 std::string get_blend_color_func()
\r
72 get_adjustement_glsl()
\r
80 "vec3 get_blend_color(vec3 back, vec3 fore) \n"
\r
82 " switch(blend_mode) \n"
\r
84 " case 0: return BlendNormal(back, fore); \n"
\r
85 " case 1: return BlendLighten(back, fore); \n"
\r
86 " case 2: return BlendDarken(back, fore); \n"
\r
87 " case 3: return BlendMultiply(back, fore); \n"
\r
88 " case 4: return BlendAverage(back, fore); \n"
\r
89 " case 5: return BlendAdd(back, fore); \n"
\r
90 " case 6: return BlendSubstract(back, fore); \n"
\r
91 " case 7: return BlendDifference(back, fore); \n"
\r
92 " case 8: return BlendNegation(back, fore); \n"
\r
93 " case 9: return BlendExclusion(back, fore); \n"
\r
94 " case 10: return BlendScreen(back, fore); \n"
\r
95 " case 11: return BlendOverlay(back, fore); \n"
\r
96 //" case 12: return BlendSoftLight(back, fore); \n"
\r
97 " case 13: return BlendHardLight(back, fore); \n"
\r
98 " case 14: return BlendColorDodge(back, fore); \n"
\r
99 " case 15: return BlendColorBurn(back, fore); \n"
\r
100 " case 16: return BlendLinearDodge(back, fore); \n"
\r
101 " case 17: return BlendLinearBurn(back, fore); \n"
\r
102 " case 18: return BlendLinearLight(back, fore); \n"
\r
103 " case 19: return BlendVividLight(back, fore); \n"
\r
104 " case 20: return BlendPinLight(back, fore); \n"
\r
105 " case 21: return BlendHardMix(back, fore); \n"
\r
106 " case 22: return BlendReflect(back, fore); \n"
\r
107 " case 23: return BlendGlow(back, fore); \n"
\r
108 " case 24: return BlendPhoenix(back, fore); \n"
\r
109 " case 25: return BlendHue(back, fore); \n"
\r
110 " case 26: return BlendSaturation(back, fore); \n"
\r
111 " case 27: return BlendColor(back, fore); \n"
\r
112 " case 28: return BlendLuminosity(back, fore); \n"
\r
114 " return BlendNormal(back, fore); \n"
\r
117 "vec4 blend(vec4 fore) \n"
\r
119 " vec4 back = texture2D(background, gl_TexCoord[1].st); \n"
\r
120 " fore.rgb = get_blend_color(back.rgb, fore.rgb); \n"
\r
121 " return vec4(mix(back.bgr, fore.rgb, fore.a), back.a + fore.a); \n"
\r
125 std::string get_simple_blend_color_func()
\r
129 get_adjustement_glsl()
\r
133 "vec4 blend(vec4 fore) \n"
\r
139 std::string get_vertex()
\r
145 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
\r
146 " gl_TexCoord[1] = gl_MultiTexCoord1; \n"
\r
147 " gl_FrontColor = gl_Color; \n"
\r
148 " gl_Position = ftransform(); \n"
\r
152 std::string get_fragment()
\r
157 "uniform sampler2D background; \n"
\r
158 "uniform sampler2D plane[4]; \n"
\r
159 "uniform sampler2D local_key; \n"
\r
160 "uniform sampler2D layer_key; \n"
\r
162 "uniform bool is_hd; \n"
\r
163 "uniform bool has_local_key; \n"
\r
164 "uniform bool has_layer_key; \n"
\r
165 "uniform int blend_mode; \n"
\r
166 "uniform int alpha_mode; \n"
\r
167 "uniform int pixel_format; \n"
\r
169 "uniform bool levels; \n"
\r
170 "uniform float min_input; \n"
\r
171 "uniform float max_input; \n"
\r
172 "uniform float gamma; \n"
\r
173 "uniform float min_output; \n"
\r
174 "uniform float max_output; \n"
\r
176 "uniform bool csb; \n"
\r
177 "uniform float brt; \n"
\r
178 "uniform float sat; \n"
\r
179 "uniform float con; \n"
\r
184 (blend_modes_ ? get_blend_color_func() : get_simple_blend_color_func())
\r
189 "//http://slouken.blogspot.com/2011/02/mpeg-acceleration-with-glsl.html \n"
\r
190 "vec4 ycbcra_to_rgba_sd(float y, float cb, float cr, float a) \n"
\r
192 " // YUV offset \n"
\r
193 " const vec3 offset = vec3(-0.0625, -0.5, -0.5); \n"
\r
195 " // RGB coefficients \n"
\r
196 " const vec3 Rcoeff = vec3(1.164, 0.000, 1.596); \n"
\r
197 " const vec3 Gcoeff = vec3(1.164, -0.391, -0.813); \n"
\r
198 " const vec3 Bcoeff = vec3(1.164, 2.018, 0.000); \n"
\r
200 " vec3 yuv = vec3(y, cr, cb); \n"
\r
203 " yuv += offset; \n"
\r
204 " rgba.r = dot(yuv, Rcoeff); \n"
\r
205 " rgba.g = dot(yuv, Gcoeff); \n"
\r
206 " rgba.b = dot(yuv, Bcoeff); \n"
\r
212 "vec4 ycbcra_to_rgba_hd(float y, float cb, float cr, float a) \n"
\r
214 " // YUV offset \n"
\r
215 " const vec3 offset = vec3(-0.0625, -0.5, -0.5); \n"
\r
217 " // RGB coefficients \n"
\r
218 " const vec3 Rcoeff = vec3(1.164, 0.000, 1.793); \n"
\r
219 " const vec3 Gcoeff = vec3(1.164, -0.213, -0.534); \n"
\r
220 " const vec3 Bcoeff = vec3(1.164, 2.115, 0.000); \n"
\r
222 " vec3 yuv = vec3(y, cr, cb); \n"
\r
225 " yuv += offset; \n"
\r
226 " rgba.r = dot(yuv, Rcoeff); \n"
\r
227 " rgba.g = dot(yuv, Gcoeff); \n"
\r
228 " rgba.b = dot(yuv, Bcoeff); \n"
\r
234 "vec4 ycbcra_to_rgba(float y, float cb, float cr, float a) \n"
\r
237 " return ycbcra_to_rgba_hd(y, cb, cr, a); \n"
\r
239 " return ycbcra_to_rgba_sd(y, cb, cr, a); \n"
\r
242 "vec4 get_rgba_color() \n"
\r
244 " switch(pixel_format) \n"
\r
246 " case 0: //gray \n"
\r
247 " return vec4(texture2D(plane[0], gl_TexCoord[0].st).rrr, 1.0); \n"
\r
248 " case 1: //bgra, \n"
\r
249 " return texture2D(plane[0], gl_TexCoord[0].st).bgra; \n"
\r
250 " case 2: //rgba, \n"
\r
251 " return texture2D(plane[0], gl_TexCoord[0].st).rgba; \n"
\r
252 " case 3: //argb, \n"
\r
253 " return texture2D(plane[0], gl_TexCoord[0].st).argb; \n"
\r
254 " case 4: //abgr, \n"
\r
255 " return texture2D(plane[0], gl_TexCoord[0].st).gbar; \n"
\r
256 " case 5: //ycbcr, \n"
\r
258 " float y = texture2D(plane[0], gl_TexCoord[0].st).r; \n"
\r
259 " float cb = texture2D(plane[1], gl_TexCoord[0].st).r; \n"
\r
260 " float cr = texture2D(plane[2], gl_TexCoord[0].st).r; \n"
\r
261 " return ycbcra_to_rgba(y, cb, cr, 1.0); \n"
\r
263 " case 6: //ycbcra \n"
\r
265 " float y = texture2D(plane[0], gl_TexCoord[0].st).r; \n"
\r
266 " float cb = texture2D(plane[1], gl_TexCoord[0].st).r; \n"
\r
267 " float cr = texture2D(plane[2], gl_TexCoord[0].st).r; \n"
\r
268 " float a = texture2D(plane[3], gl_TexCoord[0].st).r; \n"
\r
269 " return ycbcra_to_rgba(y, cb, cr, a); \n"
\r
271 " case 7: //luma \n"
\r
273 " vec3 y3 = texture2D(plane[0], gl_TexCoord[0].st).rrr; \n"
\r
274 " return vec4((y3-0.065)/0.859, 1.0); \n"
\r
277 " return vec4(0.0, 0.0, 0.0, 0.0); \n"
\r
282 " vec4 color = get_rgba_color(); \n"
\r
284 " color.rgb = LevelsControl(color.rgb, min_input, max_input, gamma, min_output, max_output); \n"
\r
286 " color.rgb = ContrastSaturationBrightness(color.rgb, brt, sat, con); \n"
\r
287 " if(has_local_key) \n"
\r
288 " color.a *= texture2D(local_key, gl_TexCoord[1].st).r; \n"
\r
289 " if(has_layer_key) \n"
\r
290 " color.a *= texture2D(layer_key, gl_TexCoord[1].st).r; \n"
\r
291 " color *= gl_Color; \n"
\r
292 " color = blend(color); \n"
\r
293 " gl_FragColor = color.bgra; \n"
\r
297 void init_shader(ogl_device& ogl)
\r
301 blend_modes_ = glTextureBarrierNV ? env::properties().get("configuration.mixers.blend-modes", false) : false;
\r
302 shader_.reset(new shader(get_vertex(), get_fragment()));
\r
306 CASPAR_LOG_CURRENT_EXCEPTION();
\r
307 CASPAR_LOG(warning) << "Failed to compile shader. Trying to compile without blend-modes.";
\r
309 blend_modes_ = false;
\r
310 shader_.reset(new shader(get_vertex(), get_fragment()));
\r
313 ogl.enable(GL_TEXTURE_2D);
\r
317 ogl.enable(GL_BLEND);
\r
318 GL(glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE));
\r
319 CASPAR_LOG(info) << L"[shader] Blend-modes are disabled.";
\r
323 void draw(ogl_device& ogl,
\r
324 render_item&& item,
\r
325 const safe_ptr<device_buffer>& background,
\r
326 const std::shared_ptr<device_buffer>& local_key,
\r
327 const std::shared_ptr<device_buffer>& layer_key)
\r
329 static const double epsilon = 0.001;
\r
331 CASPAR_ASSERT(item.pix_desc.planes.size() == item.textures.size());
\r
333 if(item.textures.empty())
\r
336 if(item.transform.get_opacity() < epsilon)
\r
342 if(item.mode == core::video_mode::progressive)
\r
343 ogl.disable(GL_POLYGON_STIPPLE);
\r
346 ogl.enable(GL_POLYGON_STIPPLE);
\r
348 if(item.mode == core::video_mode::upper)
\r
349 ogl.stipple_pattern(upper_pattern);
\r
350 else if(item.mode == core::video_mode::lower)
\r
351 ogl.stipple_pattern(lower_pattern);
\r
356 for(size_t n = 0; n < item.textures.size(); ++n)
\r
357 item.textures[n]->bind(n);
\r
360 local_key->bind(4);
\r
363 layer_key->bind(5);
\r
369 shader_->set("plane[0]", 0);
\r
370 shader_->set("plane[1]", 1);
\r
371 shader_->set("plane[2]", 2);
\r
372 shader_->set("plane[3]", 3);
\r
373 shader_->set("local_key", 4);
\r
374 shader_->set("layer_key", 5);
\r
375 shader_->set("is_hd", item.pix_desc.planes.at(0).height > 700 ? 1 : 0);
\r
376 shader_->set("has_local_key", local_key ? 1 : 0);
\r
377 shader_->set("has_layer_key", layer_key ? 1 : 0);
\r
378 shader_->set("pixel_format", item.pix_desc.pix_fmt);
\r
380 // Setup blend_func
\r
384 background->bind(6);
\r
386 shader_->set("background", 6);
\r
387 shader_->set("blend_mode", item.transform.get_is_key() ? core::image_transform::blend_mode::normal : item.transform.get_blend_mode());
\r
388 shader_->set("alpha_mode", item.transform.get_alpha_mode());
\r
391 auto levels = item.transform.get_levels();
\r
393 if(levels.min_input > epsilon ||
\r
394 levels.max_input < 1.0-epsilon ||
\r
395 levels.min_output > epsilon ||
\r
396 levels.max_output < 1.0-epsilon ||
\r
397 std::abs(levels.gamma - 1.0) > epsilon)
\r
399 shader_->set("levels", true);
\r
400 shader_->set("min_input", levels.min_input);
\r
401 shader_->set("max_input", levels.max_input);
\r
402 shader_->set("min_output", levels.min_output);
\r
403 shader_->set("max_output", levels.max_output);
\r
404 shader_->set("gamma", levels.gamma);
\r
407 shader_->set("levels", false);
\r
409 if(std::abs(item.transform.get_brightness() - 1.0) > epsilon ||
\r
410 std::abs(item.transform.get_saturation() - 1.0) > epsilon ||
\r
411 std::abs(item.transform.get_contrast() - 1.0) > epsilon)
\r
413 shader_->set("csb", true);
\r
415 shader_->set("brt", item.transform.get_brightness());
\r
416 shader_->set("sat", item.transform.get_saturation());
\r
417 shader_->set("con", item.transform.get_contrast());
\r
420 shader_->set("csb", false);
\r
422 // Setup drawing area
\r
424 ogl.viewport(0, 0, background->width(), background->height());
\r
426 GL(glColor4d(item.transform.get_gain(), item.transform.get_gain(), item.transform.get_gain(), item.transform.get_opacity()));
\r
428 auto m_p = item.transform.get_clip_translation();
\r
429 auto m_s = item.transform.get_clip_scale();
\r
431 bool scissor = m_p[0] > std::numeric_limits<double>::epsilon() || m_p[1] > std::numeric_limits<double>::epsilon() &&
\r
432 m_s[0] < 1.0 - std::numeric_limits<double>::epsilon() || m_s[1] < 1.0 - std::numeric_limits<double>::epsilon();
\r
436 double w = static_cast<double>(background->width());
\r
437 double h = static_cast<double>(background->height());
\r
439 ogl.enable(GL_SCISSOR_TEST);
\r
440 ogl.scissor(static_cast<size_t>(m_p[0]*w), static_cast<size_t>(m_p[1]*h), static_cast<size_t>(m_s[0]*w), static_cast<size_t>(m_s[1]*h));
\r
443 auto f_p = item.transform.get_fill_translation();
\r
444 auto f_s = item.transform.get_fill_scale();
\r
449 glMultiTexCoord2d(GL_TEXTURE0, 0.0, 0.0); glMultiTexCoord2d(GL_TEXTURE1, f_p[0] , f_p[1] ); glVertex2d( f_p[0] *2.0-1.0, f_p[1] *2.0-1.0);
\r
450 glMultiTexCoord2d(GL_TEXTURE0, 1.0, 0.0); glMultiTexCoord2d(GL_TEXTURE1, (f_p[0]+f_s[0]), f_p[1] ); glVertex2d((f_p[0]+f_s[0])*2.0-1.0, f_p[1] *2.0-1.0);
\r
451 glMultiTexCoord2d(GL_TEXTURE0, 1.0, 1.0); glMultiTexCoord2d(GL_TEXTURE1, (f_p[0]+f_s[0]), (f_p[1]+f_s[1])); glVertex2d((f_p[0]+f_s[0])*2.0-1.0, (f_p[1]+f_s[1])*2.0-1.0);
\r
452 glMultiTexCoord2d(GL_TEXTURE0, 0.0, 1.0); glMultiTexCoord2d(GL_TEXTURE1, f_p[0] , (f_p[1]+f_s[1])); glVertex2d( f_p[0] *2.0-1.0, (f_p[1]+f_s[1])*2.0-1.0);
\r
455 ogl.disable(GL_SCISSOR_TEST);
\r
457 item.textures.clear();
\r
458 ogl.yield(); // Return resources to pool as early as possible.
\r
461 glTextureBarrierNV(); // This allows us to use framebuffer both as source and target while blending.
\r
465 image_kernel::image_kernel() : impl_(new implementation()){}
\r
466 void image_kernel::draw(ogl_device& ogl, render_item&& item, const safe_ptr<device_buffer>& background, const std::shared_ptr<device_buffer>& local_key, const std::shared_ptr<device_buffer>& layer_key)
\r
468 impl_->draw(ogl, std::move(item), background, local_key, layer_key);
\r
471 bool operator==(const render_item& lhs, const render_item& rhs)
\r
473 return lhs.textures == rhs.textures && lhs.transform == rhs.transform && lhs.tag == rhs.tag && lhs.mode == rhs.mode;
\r