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_kernel.h"
\r
26 #include "image_shader.h"
\r
27 #include "blending_glsl.h"
\r
29 #include "../util/shader.h"
\r
30 #include "../util/device_buffer.h"
\r
31 #include "../util/device.h"
\r
33 #include <common/except.h>
\r
34 #include <common/gl/gl_check.h>
\r
35 #include <common/env.h>
\r
37 #include <core/video_format.h>
\r
38 #include <core/frame/pixel_format.h>
\r
39 #include <core/frame/frame_transform.h>
\r
41 #include <boost/noncopyable.hpp>
\r
43 namespace caspar { namespace accelerator { namespace ogl {
\r
45 GLubyte upper_pattern[] = {
\r
46 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
47 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
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
51 GLubyte lower_pattern[] = {
\r
52 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
53 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
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
57 struct image_kernel::impl : boost::noncopyable
\r
59 spl::shared_ptr<device> ogl_;
\r
60 spl::shared_ptr<shader> shader_;
\r
63 impl(const spl::shared_ptr<device>& ogl)
\r
65 , shader_(ogl_->invoke([&]{return get_image_shader(blend_modes_);}))
\r
69 void draw(draw_params&& params)
\r
71 static const double epsilon = 0.001;
\r
73 CASPAR_ASSERT(params.pix_desc.planes.size() == params.textures.size());
\r
75 if(params.textures.empty() || !params.background)
\r
78 if(params.transform.opacity < epsilon)
\r
83 for(int n = 0; n < params.textures.size(); ++n)
\r
84 params.textures[n]->bind(n);
\r
86 if(params.local_key)
\r
87 params.local_key->bind(texture_id::local_key);
\r
89 if(params.layer_key)
\r
90 params.layer_key->bind(texture_id::layer_key);
\r
96 shader_->set("plane[0]", texture_id::plane0);
\r
97 shader_->set("plane[1]", texture_id::plane1);
\r
98 shader_->set("plane[2]", texture_id::plane2);
\r
99 shader_->set("plane[3]", texture_id::plane3);
\r
100 shader_->set("local_key", texture_id::local_key);
\r
101 shader_->set("layer_key", texture_id::layer_key);
\r
102 shader_->set("is_hd", params.pix_desc.planes.at(0).height > 700 ? 1 : 0);
\r
103 shader_->set("has_local_key", params.local_key);
\r
104 shader_->set("has_layer_key", params.layer_key);
\r
105 shader_->set("pixel_format", params.pix_desc.format.value());
\r
106 shader_->set("opacity", params.transform.is_key ? 1.0 : params.transform.opacity);
\r
108 // Setup blend_func
\r
110 if(params.transform.is_key)
\r
111 params.blend_mode = core::blend_mode::normal;
\r
115 params.background->bind(6);
\r
117 shader_->set("background", texture_id::background);
\r
118 shader_->set("blend_mode", params.blend_mode.value());
\r
119 shader_->set("keyer", params.keyer.value());
\r
123 GL(glEnable(GL_BLEND));
\r
125 switch(params.keyer.value())
\r
127 case keyer::additive:
\r
128 GL(glBlendFunc(GL_ONE, GL_ONE));
\r
130 case keyer::linear:
\r
132 GL(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
\r
136 // Setup image-adjustements
\r
138 if(params.transform.levels.min_input > epsilon ||
\r
139 params.transform.levels.max_input < 1.0-epsilon ||
\r
140 params.transform.levels.min_output > epsilon ||
\r
141 params.transform.levels.max_output < 1.0-epsilon ||
\r
142 std::abs(params.transform.levels.gamma - 1.0) > epsilon)
\r
144 shader_->set("levels", true);
\r
145 shader_->set("min_input", params.transform.levels.min_input);
\r
146 shader_->set("max_input", params.transform.levels.max_input);
\r
147 shader_->set("min_output", params.transform.levels.min_output);
\r
148 shader_->set("max_output", params.transform.levels.max_output);
\r
149 shader_->set("gamma", params.transform.levels.gamma);
\r
152 shader_->set("levels", false);
\r
154 if(std::abs(params.transform.brightness - 1.0) > epsilon ||
\r
155 std::abs(params.transform.saturation - 1.0) > epsilon ||
\r
156 std::abs(params.transform.contrast - 1.0) > epsilon)
\r
158 shader_->set("csb", true);
\r
160 shader_->set("brt", params.transform.brightness);
\r
161 shader_->set("sat", params.transform.saturation);
\r
162 shader_->set("con", params.transform.contrast);
\r
165 shader_->set("csb", false);
\r
167 // Setup interlacing
\r
169 if(params.transform.field_mode != core::field_mode::progressive)
\r
171 GL(glEnable(GL_POLYGON_STIPPLE));
\r
173 if(params.transform.field_mode == core::field_mode::upper)
\r
174 glPolygonStipple(upper_pattern);
\r
175 else if(params.transform.field_mode == core::field_mode::lower)
\r
176 glPolygonStipple(lower_pattern);
\r
179 // Setup drawing area
\r
181 GL(glViewport(0, 0, params.background->width(), params.background->height()));
\r
183 auto m_p = params.transform.clip_translation;
\r
184 auto m_s = params.transform.clip_scale;
\r
186 bool scissor = m_p[0] > std::numeric_limits<double>::epsilon() || m_p[1] > std::numeric_limits<double>::epsilon() ||
\r
187 m_s[0] < (1.0 - std::numeric_limits<double>::epsilon()) || m_s[1] < (1.0 - std::numeric_limits<double>::epsilon());
\r
191 double w = static_cast<double>(params.background->width());
\r
192 double h = static_cast<double>(params.background->height());
\r
194 GL(glEnable(GL_SCISSOR_TEST));
\r
195 glScissor(static_cast<int>(m_p[0]*w), static_cast<int>(m_p[1]*h), static_cast<int>(m_s[0]*w), static_cast<int>(m_s[1]*h));
\r
198 auto f_p = params.transform.fill_translation;
\r
199 auto f_s = params.transform.fill_scale;
\r
201 // Synchronize and set render target
\r
205 // http://www.opengl.org/registry/specs/NV/texture_barrier.txt
\r
206 // This allows us to use framebuffer (background) both as source and target while blending.
\r
207 glTextureBarrierNV();
\r
210 params.background->attach();
\r
215 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
216 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
217 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
218 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
223 GL(glDisable(GL_SCISSOR_TEST));
\r
224 GL(glDisable(GL_POLYGON_STIPPLE));
\r
225 GL(glDisable(GL_BLEND));
\r
229 image_kernel::image_kernel(const spl::shared_ptr<device>& ogl) : impl_(new impl(ogl)){}
\r
230 void image_kernel::draw(draw_params&& params)
\r
232 impl_->draw(std::move(params));
\r
234 bool image_kernel::has_blend_modes() const
\r
236 return impl_->blend_modes_;
\r