]> git.sesse.net Git - casparcg/blob - core/mixer/image/image_kernel.cpp
2.0. - Removed alpha-mode.
[casparcg] / core / mixer / image / image_kernel.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\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
10 *\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
15 \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
18 *\r
19 */\r
20 #include "../../stdafx.h"\r
21 \r
22 #include "image_kernel.h"\r
23 \r
24 #include "image_shader.h"\r
25 \r
26 #include "blending_glsl.h"\r
27 #include "../gpu/shader.h"\r
28 #include "../gpu/device_buffer.h"\r
29 #include "../gpu/ogl_device.h"\r
30 \r
31 #include <common/exception/exceptions.h>\r
32 #include <common/gl/gl_check.h>\r
33 #include <common/env.h>\r
34 \r
35 #include <core/video_format.h>\r
36 #include <core/producer/frame/pixel_format.h>\r
37 #include <core/producer/frame/image_transform.h>\r
38 \r
39 #include <GL/glew.h>\r
40 \r
41 #include <boost/noncopyable.hpp>\r
42 \r
43 #include <tbb/mutex.h>\r
44 \r
45 #include <unordered_map>\r
46 \r
47 namespace caspar { namespace core {\r
48         \r
49 GLubyte upper_pattern[] = {\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
52         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         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
54                 \r
55 GLubyte lower_pattern[] = {\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
58         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         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
60 \r
61 struct image_kernel::implementation : boost::noncopyable\r
62 {       \r
63         std::shared_ptr<shader> shader_;\r
64         bool                                    blend_modes_;\r
65                                                         \r
66         void draw(ogl_device&                                                                   ogl,\r
67                           render_item&&                                                                 item,\r
68                           const safe_ptr<device_buffer>&                                background,\r
69                           const std::shared_ptr<device_buffer>&                 local_key,                        \r
70                           const std::shared_ptr<device_buffer>&                 layer_key)\r
71         {\r
72                 static const double epsilon = 0.001;\r
73 \r
74                 CASPAR_ASSERT(item.pix_desc.planes.size() == item.textures.size());\r
75 \r
76                 if(item.textures.empty())\r
77                         return;\r
78 \r
79                 if(item.transform.get_opacity() < epsilon)\r
80                         return;\r
81                 \r
82                 // Bind textures\r
83 \r
84                 for(size_t n = 0; n < item.textures.size(); ++n)\r
85                         item.textures[n]->bind(n);\r
86 \r
87                 if(local_key)\r
88                         local_key->bind(texture_id::local_key);\r
89                 \r
90                 if(layer_key)\r
91                         layer_key->bind(texture_id::layer_key);\r
92                         \r
93                 // Setup shader\r
94 \r
95                 if(!shader_)\r
96                         shader_ = get_image_shader(ogl, blend_modes_);\r
97                                                 \r
98                 ogl.use(*shader_);\r
99 \r
100                 shader_->set("plane[0]",                texture_id::plane0);\r
101                 shader_->set("plane[1]",                texture_id::plane1);\r
102                 shader_->set("plane[2]",                texture_id::plane2);\r
103                 shader_->set("plane[3]",                texture_id::plane3);\r
104                 shader_->set("local_key",               texture_id::local_key);\r
105                 shader_->set("layer_key",               texture_id::layer_key);\r
106                 shader_->set("is_hd",                   item.pix_desc.planes.at(0).height > 700 ? 1 : 0);\r
107                 shader_->set("has_local_key",   local_key);\r
108                 shader_->set("has_layer_key",   layer_key);\r
109                 shader_->set("pixel_format",    item.pix_desc.pix_fmt); \r
110                 \r
111                 // Setup blend_func\r
112                 \r
113                 if(blend_modes_)\r
114                 {\r
115                         background->bind(6);\r
116 \r
117                         shader_->set("background",      texture_id::background);\r
118                         shader_->set("blend_mode",      item.transform.get_is_key() ? core::image_transform::blend_mode::normal : item.transform.get_blend_mode());\r
119                 }\r
120 \r
121                 // Setup image-adjustements\r
122 \r
123                 auto levels = item.transform.get_levels();\r
124 \r
125                 if(levels.min_input  > epsilon          ||\r
126                    levels.max_input  < 1.0-epsilon      ||\r
127                    levels.min_output > epsilon          ||\r
128                    levels.max_output < 1.0-epsilon      ||\r
129                    std::abs(levels.gamma - 1.0) > epsilon)\r
130                 {\r
131                         shader_->set("levels", true);   \r
132                         shader_->set("min_input", levels.min_input);    \r
133                         shader_->set("max_input", levels.max_input);\r
134                         shader_->set("min_output", levels.min_output);\r
135                         shader_->set("max_output", levels.max_output);\r
136                         shader_->set("gamma", levels.gamma);\r
137                 }\r
138                 else\r
139                         shader_->set("levels", false);  \r
140 \r
141                 if(std::abs(item.transform.get_brightness() - 1.0) > epsilon ||\r
142                    std::abs(item.transform.get_saturation() - 1.0) > epsilon ||\r
143                    std::abs(item.transform.get_contrast() - 1.0) > epsilon)\r
144                 {\r
145                         shader_->set("csb",     true);  \r
146                         \r
147                         shader_->set("brt", item.transform.get_brightness());   \r
148                         shader_->set("sat", item.transform.get_saturation());\r
149                         shader_->set("con", item.transform.get_contrast());\r
150                 }\r
151                 else\r
152                         shader_->set("csb",     false); \r
153                 \r
154                 // Setup interlacing\r
155 \r
156                 if(item.mode == core::video_mode::progressive)                  \r
157                         ogl.disable(GL_POLYGON_STIPPLE);                        \r
158                 else                    \r
159                 {\r
160                         ogl.enable(GL_POLYGON_STIPPLE);\r
161 \r
162                         if(item.mode == core::video_mode::upper)\r
163                                 ogl.stipple_pattern(upper_pattern);\r
164                         else if(item.mode == core::video_mode::lower)\r
165                                 ogl.stipple_pattern(lower_pattern);\r
166                 }\r
167 \r
168                 // Setup drawing area\r
169                 \r
170                 ogl.viewport(0, 0, background->width(), background->height());\r
171 \r
172                 GL(glColor4d(item.transform.get_gain(), item.transform.get_gain(), item.transform.get_gain(), item.transform.get_opacity()));\r
173                                                 \r
174                 auto m_p = item.transform.get_clip_translation();\r
175                 auto m_s = item.transform.get_clip_scale();\r
176 \r
177                 bool scissor = m_p[0] > std::numeric_limits<double>::epsilon()           || m_p[1] > std::numeric_limits<double>::epsilon() &&\r
178                                            m_s[0] < 1.0 - std::numeric_limits<double>::epsilon() || m_s[1] < 1.0 - std::numeric_limits<double>::epsilon();\r
179 \r
180                 if(scissor)\r
181                 {\r
182                         double w = static_cast<double>(background->width());\r
183                         double h = static_cast<double>(background->height());\r
184                 \r
185                         ogl.enable(GL_SCISSOR_TEST);\r
186                         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
187                 }\r
188 \r
189                 auto f_p = item.transform.get_fill_translation();\r
190                 auto f_s = item.transform.get_fill_scale();\r
191                 \r
192                 // Set render target\r
193                 \r
194                 ogl.attach(*background);\r
195                 \r
196                 // Draw\r
197 \r
198                 glBegin(GL_QUADS);\r
199                         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
200                         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
201                         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
202                         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
203                 glEnd();\r
204 \r
205                 // Cleanup\r
206 \r
207                 ogl.disable(GL_SCISSOR_TEST);   \r
208                                 \r
209                 item.textures.clear();\r
210                 ogl.yield(); // Return resources to pool as early as possible.\r
211 \r
212                 if(!blend_modes_)\r
213                         glTextureBarrierNV(); // This allows us to use framebuffer (background) both as source and target while blending.\r
214         }\r
215 };\r
216 \r
217 image_kernel::image_kernel() : impl_(new implementation()){}\r
218 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
219 {\r
220         impl_->draw(ogl, std::move(item), background, local_key, layer_key);\r
221 }\r
222 \r
223 bool operator==(const render_item& lhs, const render_item& rhs)\r
224 {\r
225         return lhs.textures == rhs.textures && lhs.transform == rhs.transform && lhs.tag == rhs.tag && lhs.mode == rhs.mode;\r
226 }\r
227 \r
228 }}