]> git.sesse.net Git - casparcg/blob - core/mixer/image/image_kernel.cpp
2.0. mixer: Refactored transforms, merged image and audio transform.
[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/frame_transform.h>\r
38 \r
39 #include <GL/glew.h>\r
40 \r
41 #include <boost/noncopyable.hpp>\r
42 \r
43 #include <unordered_map>\r
44 \r
45 namespace caspar { namespace core {\r
46         \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
52                 \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
58 \r
59 struct image_kernel::implementation : boost::noncopyable\r
60 {       \r
61         std::shared_ptr<shader> shader_;\r
62         bool                                    blend_modes_;\r
63                                                         \r
64         void draw(ogl_device&                                                                   ogl,\r
65                           render_item&&                                                                 item,\r
66                           device_buffer&                                                                background,\r
67                           const std::shared_ptr<device_buffer>&                 local_key,                        \r
68                           const std::shared_ptr<device_buffer>&                 layer_key)\r
69         {\r
70                 static const double epsilon = 0.001;\r
71 \r
72                 CASPAR_ASSERT(item.pix_desc.planes.size() == item.textures.size());\r
73 \r
74                 if(item.textures.empty())\r
75                         return;\r
76 \r
77                 if(item.transform.opacity < epsilon)\r
78                         return;\r
79                 \r
80                 if(!std::all_of(item.textures.begin(), item.textures.end(), std::mem_fn(&device_buffer::ready)))\r
81                 {\r
82                         CASPAR_LOG(warning) << L"[image_mixer] Performance warning. Host to device transfer not complete, GPU will be stalled";\r
83                         ogl.yield(); // Try to give it some more time.\r
84                 }               \r
85                 \r
86                 // Bind textures\r
87 \r
88                 for(size_t n = 0; n < item.textures.size(); ++n)\r
89                         item.textures[n]->bind(n);\r
90 \r
91                 if(local_key)\r
92                         local_key->bind(texture_id::local_key);\r
93                 \r
94                 if(layer_key)\r
95                         layer_key->bind(texture_id::layer_key);\r
96                         \r
97                 // Setup shader\r
98 \r
99                 if(!shader_)\r
100                         shader_ = get_image_shader(ogl, blend_modes_);\r
101                                                 \r
102                 ogl.use(*shader_);\r
103 \r
104                 shader_->set("plane[0]",                texture_id::plane0);\r
105                 shader_->set("plane[1]",                texture_id::plane1);\r
106                 shader_->set("plane[2]",                texture_id::plane2);\r
107                 shader_->set("plane[3]",                texture_id::plane3);\r
108                 shader_->set("local_key",               texture_id::local_key);\r
109                 shader_->set("layer_key",               texture_id::layer_key);\r
110                 shader_->set("is_hd",                   item.pix_desc.planes.at(0).height > 700 ? 1 : 0);\r
111                 shader_->set("has_local_key",   local_key);\r
112                 shader_->set("has_layer_key",   layer_key);\r
113                 shader_->set("pixel_format",    item.pix_desc.pix_fmt); \r
114                 shader_->set("opacity",                 item.transform.is_key ? 1.0 : item.transform.opacity);  \r
115                 \r
116                 // Setup blend_func\r
117                 \r
118                 if(item.transform.is_key)\r
119                         item.blend_mode = blend_mode::normal;\r
120 \r
121                 if(blend_modes_)\r
122                 {\r
123                         background.bind(6);\r
124 \r
125                         shader_->set("background",      texture_id::background);\r
126                         shader_->set("blend_mode",      item.blend_mode);\r
127                 }\r
128                 else\r
129                 {\r
130                         switch(item.blend_mode)\r
131                         {\r
132                         case blend_mode::replace:                       \r
133                                 ogl.blend_func_separate(GL_ONE, GL_ZERO, GL_ONE, GL_ONE);\r
134                                 break;\r
135                         case blend_mode::normal:\r
136                         default:\r
137                                 ogl.blend_func_separate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);\r
138                                 break;\r
139                         }\r
140                 }\r
141 \r
142                 // Setup image-adjustements\r
143                 \r
144                 if(item.transform.levels.min_input  > epsilon           ||\r
145                    item.transform.levels.max_input  < 1.0-epsilon       ||\r
146                    item.transform.levels.min_output > epsilon           ||\r
147                    item.transform.levels.max_output < 1.0-epsilon       ||\r
148                    std::abs(item.transform.levels.gamma - 1.0) > epsilon)\r
149                 {\r
150                         shader_->set("levels", true);   \r
151                         shader_->set("min_input",       item.transform.levels.min_input);       \r
152                         shader_->set("max_input",       item.transform.levels.max_input);\r
153                         shader_->set("min_output",      item.transform.levels.min_output);\r
154                         shader_->set("max_output",      item.transform.levels.max_output);\r
155                         shader_->set("gamma",           item.transform.levels.gamma);\r
156                 }\r
157                 else\r
158                         shader_->set("levels", false);  \r
159 \r
160                 if(std::abs(item.transform.brightness - 1.0) > epsilon ||\r
161                    std::abs(item.transform.saturation - 1.0) > epsilon ||\r
162                    std::abs(item.transform.contrast - 1.0)   > epsilon)\r
163                 {\r
164                         shader_->set("csb",     true);  \r
165                         \r
166                         shader_->set("brt", item.transform.brightness); \r
167                         shader_->set("sat", item.transform.saturation);\r
168                         shader_->set("con", item.transform.contrast);\r
169                 }\r
170                 else\r
171                         shader_->set("csb",     false); \r
172                 \r
173                 // Setup interlacing\r
174 \r
175                 if(item.transform.field_mode == core::field_mode::progressive)                  \r
176                         ogl.disable(GL_POLYGON_STIPPLE);                        \r
177                 else                    \r
178                 {\r
179                         ogl.enable(GL_POLYGON_STIPPLE);\r
180 \r
181                         if(item.transform.field_mode == core::field_mode::upper)\r
182                                 ogl.stipple_pattern(upper_pattern);\r
183                         else if(item.transform.field_mode == core::field_mode::lower)\r
184                                 ogl.stipple_pattern(lower_pattern);\r
185                 }\r
186 \r
187                 // Setup drawing area\r
188                 \r
189                 ogl.viewport(0, 0, background.width(), background.height());\r
190                                                                 \r
191                 auto m_p = item.transform.clip_translation;\r
192                 auto m_s = item.transform.clip_scale;\r
193 \r
194                 bool scissor = m_p[0] > std::numeric_limits<double>::epsilon()           || m_p[1] > std::numeric_limits<double>::epsilon() &&\r
195                                            m_s[0] < 1.0 - std::numeric_limits<double>::epsilon() || m_s[1] < 1.0 - std::numeric_limits<double>::epsilon();\r
196 \r
197                 if(scissor)\r
198                 {\r
199                         double w = static_cast<double>(background.width());\r
200                         double h = static_cast<double>(background.height());\r
201                 \r
202                         ogl.enable(GL_SCISSOR_TEST);\r
203                         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
204                 }\r
205 \r
206                 auto f_p = item.transform.fill_translation;\r
207                 auto f_s = item.transform.fill_scale;\r
208                 \r
209                 // Set render target\r
210                 \r
211                 ogl.attach(background);\r
212                 \r
213                 // Draw\r
214 \r
215                 glBegin(GL_QUADS);\r
216                         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
217                         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
218                         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
219                         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
220                 glEnd();\r
221 \r
222                 // Cleanup\r
223 \r
224                 ogl.disable(GL_SCISSOR_TEST);   \r
225                                 \r
226                 item.textures.clear();\r
227                 ogl.yield(); // Return resources to pool as early as possible.\r
228 \r
229                 if(blend_modes_)\r
230                 {\r
231                         // http://www.opengl.org/registry/specs/NV/texture_barrier.txt\r
232                         // This allows us to use framebuffer (background) both as source and target while blending.\r
233                         glTextureBarrierNV(); \r
234                 }\r
235         }\r
236 };\r
237 \r
238 image_kernel::image_kernel() : impl_(new implementation()){}\r
239 void image_kernel::draw(ogl_device& ogl, \r
240                                                 render_item&& item, \r
241                                                 device_buffer& background,\r
242                                                 const std::shared_ptr<device_buffer>& local_key, \r
243                                                 const std::shared_ptr<device_buffer>& layer_key)\r
244 {\r
245         impl_->draw(ogl, std::move(item), background, local_key, layer_key);\r
246 }\r
247 \r
248 bool operator==(const render_item& lhs, const render_item& rhs)\r
249 {\r
250         return lhs.textures == rhs.textures && lhs.transform == rhs.transform;\r
251 }\r
252 \r
253 }}