]> git.sesse.net Git - casparcg/blob - accelerator/ogl/image/image_kernel.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / accelerator / ogl / image / image_kernel.cpp
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 #include "../../stdafx.h"
23
24 #include "image_kernel.h"
25
26 #include "image_shader.h"
27 #include "blending_glsl.h"
28
29 #include "../util/shader.h"
30 #include "../util/texture.h"
31 #include "../util/device.h"
32
33 #include <common/except.h>
34 #include <common/gl/gl_check.h>
35 #include <common/env.h>
36
37 #include <core/video_format.h>
38 #include <core/frame/pixel_format.h>
39 #include <core/frame/frame_transform.h>
40
41 #include <boost/lexical_cast.hpp>
42
43 namespace caspar { namespace accelerator { namespace ogl {
44         
45 GLubyte upper_pattern[] = {
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,
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,
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,
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};
50                 
51 GLubyte lower_pattern[] = {
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, 
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,
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,
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};
56
57 struct image_kernel::impl
58 {       
59         spl::shared_ptr<device> ogl_;
60         spl::shared_ptr<shader> shader_;
61         bool                                    blend_modes_;
62                                                         
63         impl(const spl::shared_ptr<device>& ogl)
64                 : ogl_(ogl)
65                 , shader_(ogl_->invoke([&]{return get_image_shader(blend_modes_);}))
66         {
67         }
68
69         void draw(draw_params params)
70         {
71                 static const double epsilon = 0.001;            
72                 
73                 CASPAR_ASSERT(params.pix_desc.planes.size() == params.textures.size());
74
75                 if(params.textures.empty() || !params.background)
76                         return;
77
78                 if(params.transform.opacity < epsilon)
79                         return;
80                                 
81                 // Bind textures
82
83                 for(int n = 0; n < params.textures.size(); ++n)
84                         params.textures[n]->bind(n);
85
86                 if(params.local_key)
87                         params.local_key->bind(texture_id::local_key);
88                 
89                 if(params.layer_key)
90                         params.layer_key->bind(texture_id::layer_key);
91                         
92                 // Setup shader
93                                                                 
94                 shader_->use();
95
96                 shader_->set("plane[0]",                texture_id::plane0);
97                 shader_->set("plane[1]",                texture_id::plane1);
98                 shader_->set("plane[2]",                texture_id::plane2);
99                 shader_->set("plane[3]",                texture_id::plane3);
100                 for(int n = 0; n < params.textures.size(); ++n)
101                         shader_->set("plane_size[" + boost::lexical_cast<std::string>(n) + "]", 
102                                                  static_cast<float>(params.textures[n]->width()), 
103                                                  static_cast<float>(params.textures[n]->height()));
104
105                 shader_->set("local_key",               texture_id::local_key);
106                 shader_->set("layer_key",               texture_id::layer_key);
107                 shader_->set("is_hd",                   params.pix_desc.planes.at(0).height > 700 ? 1 : 0);
108                 shader_->set("has_local_key",   params.local_key);
109                 shader_->set("has_layer_key",   params.layer_key);
110                 shader_->set("pixel_format",    params.pix_desc.format.value());        
111                 shader_->set("opacity",                 params.transform.is_key ? 1.0 : params.transform.opacity);      
112                                 
113
114                 // Setup blend_func
115                 
116                 if(params.transform.is_key)
117                         params.blend_mode = core::blend_mode::normal;
118
119                 if(blend_modes_)
120                 {
121                         params.background->bind(texture_id::background);
122
123                         shader_->set("background",      texture_id::background);
124                         shader_->set("blend_mode",      params.blend_mode.value());
125                         shader_->set("keyer",           params.keyer.value());
126                 }
127                 else
128                 {
129                         GL(glEnable(GL_BLEND));
130
131                         switch(params.keyer.value())
132                         {
133                         case keyer::additive:
134                                 GL(glBlendFunc(GL_ONE, GL_ONE));        
135                                 break;
136                         case keyer::linear:
137                         default:                        
138                                 GL(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
139                         }               
140                 }
141
142                 // Setup image-adjustements
143                 
144                 if(params.transform.levels.min_input  > epsilon         ||
145                    params.transform.levels.max_input  < 1.0-epsilon     ||
146                    params.transform.levels.min_output > epsilon         ||
147                    params.transform.levels.max_output < 1.0-epsilon     ||
148                    std::abs(params.transform.levels.gamma - 1.0) > epsilon)
149                 {
150                         shader_->set("levels", true);   
151                         shader_->set("min_input",       params.transform.levels.min_input);     
152                         shader_->set("max_input",       params.transform.levels.max_input);
153                         shader_->set("min_output",      params.transform.levels.min_output);
154                         shader_->set("max_output",      params.transform.levels.max_output);
155                         shader_->set("gamma",           params.transform.levels.gamma);
156                 }
157                 else
158                         shader_->set("levels", false);  
159
160                 if(std::abs(params.transform.brightness - 1.0) > epsilon ||
161                    std::abs(params.transform.saturation - 1.0) > epsilon ||
162                    std::abs(params.transform.contrast - 1.0)   > epsilon)
163                 {
164                         shader_->set("csb",     true);  
165                         
166                         shader_->set("brt", params.transform.brightness);       
167                         shader_->set("sat", params.transform.saturation);
168                         shader_->set("con", params.transform.contrast);
169                 }
170                 else
171                         shader_->set("csb",     false); 
172                 
173                 // Setup interlacing
174                 
175                 if(params.transform.field_mode != core::field_mode::progressive)        
176                 {
177                         GL(glEnable(GL_POLYGON_STIPPLE));
178
179                         if(params.transform.field_mode == core::field_mode::upper)
180                                 glPolygonStipple(upper_pattern);
181                         else if(params.transform.field_mode == core::field_mode::lower)
182                                 glPolygonStipple(lower_pattern);
183                 }
184
185                 // Setup drawing area
186                 
187                 GL(glViewport(0, 0, params.background->width(), params.background->height()));
188                                                                                 
189                 auto m_p = params.transform.clip_translation;
190                 auto m_s = params.transform.clip_scale;
191
192                 bool scissor = m_p[0] > std::numeric_limits<double>::epsilon()                  || m_p[1] > std::numeric_limits<double>::epsilon() ||
193                                            m_s[0] < (1.0 - std::numeric_limits<double>::epsilon())      || m_s[1] < (1.0 - std::numeric_limits<double>::epsilon());
194
195                 if(scissor)
196                 {
197                         double w = static_cast<double>(params.background->width());
198                         double h = static_cast<double>(params.background->height());
199                 
200                         GL(glEnable(GL_SCISSOR_TEST));
201                         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));
202                 }
203
204                 auto f_p = params.transform.fill_translation;
205                 auto f_s = params.transform.fill_scale;
206                 
207                 // Synchronize and set render target
208                                                                 
209                 if(blend_modes_)
210                 {
211                         // http://www.opengl.org/registry/specs/NV/texture_barrier.txt
212                         // This allows us to use framebuffer (background) both as source and target while blending.
213                         glTextureBarrierNV(); 
214                 }
215
216                 params.background->attach();
217                 
218                 // Draw
219                                 
220                 glBegin(GL_QUADS);
221                         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);
222                         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);
223                         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);
224                         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);
225                 glEnd();
226                 
227                 // Cleanup
228                 
229                 GL(glDisable(GL_SCISSOR_TEST));
230                 GL(glDisable(GL_POLYGON_STIPPLE));
231                 GL(glDisable(GL_BLEND));
232         }
233 };
234
235 image_kernel::image_kernel(const spl::shared_ptr<device>& ogl) : impl_(new impl(ogl)){}
236 image_kernel::~image_kernel(){}
237 void image_kernel::draw(const draw_params& params){impl_->draw(params);}
238 bool image_kernel::has_blend_modes() const{return impl_->blend_modes_;}
239
240 }}}