]> git.sesse.net Git - casparcg/blob - accelerator/ogl/image/image_kernel.cpp
a259e13da3bf70eb8ecb73cccc587b21e896e6fa
[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, bool blend_modes_wanted)
64                 : ogl_(ogl)
65                 , shader_(ogl_->invoke([&]{return get_image_shader(ogl, blend_modes_, blend_modes_wanted); }))
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(static_cast<int>(texture_id::local_key));
88                 
89                 if(params.layer_key)
90                         params.layer_key->bind(static_cast<int>(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",   static_cast<bool>(params.local_key));
109                 shader_->set("has_layer_key",   static_cast<bool>(params.layer_key));
110                 shader_->set("pixel_format",    params.pix_desc.format);
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(static_cast<int>(texture_id::background));
122
123                         shader_->set("background",      texture_id::background);
124                         shader_->set("blend_mode",      params.blend_mode);
125                         shader_->set("keyer",           params.keyer);
126                 }
127                 else
128                 {
129                         GL(glEnable(GL_BLEND));
130
131                         switch(params.keyer)
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                 glDisable(GL_DEPTH_TEST);
189                                                                                 
190                 auto m_p = params.transform.clip_translation;
191                 auto m_s = params.transform.clip_scale;
192
193                 bool scissor = m_p[0] > std::numeric_limits<double>::epsilon()                  || m_p[1] > std::numeric_limits<double>::epsilon() ||
194                                            m_s[0] < (1.0 - std::numeric_limits<double>::epsilon())      || m_s[1] < (1.0 - std::numeric_limits<double>::epsilon());
195
196                 if(scissor)
197                 {
198                         double w = static_cast<double>(params.background->width());
199                         double h = static_cast<double>(params.background->height());
200                 
201                         GL(glEnable(GL_SCISSOR_TEST));
202                         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));
203                 }
204
205                 auto f_p = params.transform.fill_translation;
206                 auto f_s = params.transform.fill_scale;
207                 
208                 // Synchronize and set render target
209                                                                 
210                 if(blend_modes_)
211                 {
212                         // http://www.opengl.org/registry/specs/NV/texture_barrier.txt
213                         // This allows us to use framebuffer (background) both as source and target while blending.
214                         glTextureBarrierNV(); 
215                 }
216
217                 params.background->attach();
218                 
219                 glMatrixMode(GL_MODELVIEW);
220                 glPushMatrix();
221                         glTranslated(f_p[0], f_p[1], 0.0);
222                         glScaled(f_s[0], f_s[1], 1.0);
223
224                         switch(params.geometry.type())
225                         {
226                         case core::frame_geometry::geometry_type::quad:
227                                 {
228                                         const std::vector<float>& data = params.geometry.data();
229                                         float v_left = data[0], v_top = data[1], t_left = data[2], t_top = data[3];
230                                         float v_right = data[4], v_bottom = data[5], t_right = data[6], t_bottom = data[7];
231
232                                         glBegin(GL_QUADS);
233                                                 glMultiTexCoord2d(GL_TEXTURE0, t_left, t_top);          glVertex2d(v_left, v_top);
234                                                 glMultiTexCoord2d(GL_TEXTURE0, t_right, t_top);         glVertex2d(v_right, v_top);
235                                                 glMultiTexCoord2d(GL_TEXTURE0, t_right, t_bottom);      glVertex2d(v_right, v_bottom);
236                                                 glMultiTexCoord2d(GL_TEXTURE0, t_left, t_bottom);       glVertex2d(v_left, v_bottom);
237                                         glEnd();
238                                 }
239                                 break;
240
241                         case core::frame_geometry::geometry_type::quad_list:
242                                 {
243                                         glClientActiveTexture(GL_TEXTURE0);
244                                         
245                                         glDisableClientState(GL_EDGE_FLAG_ARRAY);
246                                         glDisableClientState(GL_COLOR_ARRAY);
247                                         glDisableClientState(GL_INDEX_ARRAY);
248                                         glDisableClientState(GL_NORMAL_ARRAY);
249
250                                         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
251                                         glEnableClientState(GL_VERTEX_ARRAY);
252                                         
253                                         glTexCoordPointer(2, GL_FLOAT, 4*sizeof(float), &(params.geometry.data().data()[2]));
254                                         glVertexPointer(2, GL_FLOAT, 4*sizeof(float), params.geometry.data().data());
255                                         
256                                         glDrawArrays(GL_QUADS, 0, (GLsizei)params.geometry.data().size()/4);    //each vertex is four floats.
257                                         
258                                         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
259                                         glDisableClientState(GL_VERTEX_ARRAY);
260                                 }
261                                 break;
262
263                         default:
264                                 break;
265                         }
266                 glPopMatrix();
267                 
268                 // Cleanup
269                 GL(glDisable(GL_SCISSOR_TEST));
270                 GL(glDisable(GL_POLYGON_STIPPLE));
271                 GL(glDisable(GL_BLEND));
272         }
273 };
274
275 image_kernel::image_kernel(const spl::shared_ptr<device>& ogl, bool blend_modes_wanted) : impl_(new impl(ogl, blend_modes_wanted)){}
276 image_kernel::~image_kernel(){}
277 void image_kernel::draw(const draw_params& params){impl_->draw(params);}
278 bool image_kernel::has_blend_modes() const{return impl_->blend_modes_;}
279
280 }}}