]> git.sesse.net Git - casparcg/blob - core/mixer/image/image_kernel.cpp
2.0. Fixed mix transition.
[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, draw_params&& params)\r
65         {\r
66                 static const double epsilon = 0.001;\r
67 \r
68                 CASPAR_ASSERT(params.pix_desc.planes.size() == params.textures.size());\r
69 \r
70                 if(params.textures.empty() || !params.background)\r
71                         return;\r
72 \r
73                 if(params.transform.opacity < epsilon)\r
74                         return;\r
75                 \r
76                 if(!std::all_of(params.textures.begin(), params.textures.end(), std::mem_fn(&device_buffer::ready)))\r
77                 {\r
78                         CASPAR_LOG(warning) << L"[image_mixer] Performance warning. Host to device transfer not complete, GPU will be stalled";\r
79                         ogl.yield(); // Try to give it some more time.\r
80                 }               \r
81                 \r
82                 // Bind textures\r
83 \r
84                 for(size_t n = 0; n < params.textures.size(); ++n)\r
85                         params.textures[n]->bind(n);\r
86 \r
87                 if(params.local_key)\r
88                         params.local_key->bind(texture_id::local_key);\r
89                 \r
90                 if(params.layer_key)\r
91                         params.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",                   params.pix_desc.planes.at(0).height > 700 ? 1 : 0);\r
107                 shader_->set("has_local_key",   params.local_key);\r
108                 shader_->set("has_layer_key",   params.layer_key);\r
109                 shader_->set("pixel_format",    params.pix_desc.pix_fmt);       \r
110                 shader_->set("opacity",                 params.transform.is_key ? 1.0 : params.transform.opacity);      \r
111                 \r
112                 // Setup blend_func\r
113                 \r
114                 if(params.transform.is_key)\r
115                         params.blend_mode = blend_mode::normal;\r
116 \r
117                 if(blend_modes_)\r
118                 {\r
119                         params.background->bind(6);\r
120 \r
121                         shader_->set("background",      texture_id::background);\r
122                         shader_->set("blend_mode",      params.blend_mode);\r
123                 }\r
124                 else\r
125                 {\r
126                         switch(params.blend_mode)\r
127                         {\r
128                         case blend_mode::mix:\r
129                                 ogl.blend_func(GL_ONE, GL_ONE); \r
130                                 break;\r
131                         default:                                \r
132                                 ogl.blend_func(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); \r
133                         }               \r
134                 }\r
135 \r
136                 // Setup image-adjustements\r
137                 \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
143                 {\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
150                 }\r
151                 else\r
152                         shader_->set("levels", false);  \r
153 \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
157                 {\r
158                         shader_->set("csb",     true);  \r
159                         \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
163                 }\r
164                 else\r
165                         shader_->set("csb",     false); \r
166                 \r
167                 // Setup interlacing\r
168 \r
169                 if(params.transform.field_mode == core::field_mode::progressive)                        \r
170                         ogl.disable(GL_POLYGON_STIPPLE);                        \r
171                 else                    \r
172                 {\r
173                         ogl.enable(GL_POLYGON_STIPPLE);\r
174 \r
175                         if(params.transform.field_mode == core::field_mode::upper)\r
176                                 ogl.stipple_pattern(upper_pattern);\r
177                         else if(params.transform.field_mode == core::field_mode::lower)\r
178                                 ogl.stipple_pattern(lower_pattern);\r
179                 }\r
180 \r
181                 // Setup drawing area\r
182                 \r
183                 ogl.viewport(0, 0, params.background->width(), params.background->height());\r
184                                                                 \r
185                 auto m_p = params.transform.clip_translation;\r
186                 auto m_s = params.transform.clip_scale;\r
187 \r
188                 bool scissor = m_p[0] > std::numeric_limits<double>::epsilon()                  || m_p[1] > std::numeric_limits<double>::epsilon() ||\r
189                                            m_s[0] < (1.0 - std::numeric_limits<double>::epsilon())      || m_s[1] < (1.0 - std::numeric_limits<double>::epsilon());\r
190 \r
191                 if(scissor)\r
192                 {\r
193                         double w = static_cast<double>(params.background->width());\r
194                         double h = static_cast<double>(params.background->height());\r
195                 \r
196                         ogl.enable(GL_SCISSOR_TEST);\r
197                         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
198                 }\r
199 \r
200                 auto f_p = params.transform.fill_translation;\r
201                 auto f_s = params.transform.fill_scale;\r
202                 \r
203                 // Set render target\r
204                 \r
205                 ogl.attach(*params.background);\r
206                 \r
207                 // Draw\r
208 \r
209                 glBegin(GL_QUADS);\r
210                         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
211                         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
212                         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
213                         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
214                 glEnd();\r
215 \r
216                 // Cleanup\r
217 \r
218                 ogl.disable(GL_SCISSOR_TEST);   \r
219                                 \r
220                 params.textures.clear();\r
221                 ogl.yield(); // Return resources to pool as early as possible.\r
222 \r
223                 if(blend_modes_)\r
224                 {\r
225                         // http://www.opengl.org/registry/specs/NV/texture_barrier.txt\r
226                         // This allows us to use framebuffer (background) both as source and target while blending.\r
227                         glTextureBarrierNV(); \r
228                 }\r
229         }\r
230 };\r
231 \r
232 image_kernel::image_kernel() : impl_(new implementation()){}\r
233 void image_kernel::draw(ogl_device& ogl, draw_params&& params)\r
234 {\r
235         impl_->draw(ogl, std::move(params));\r
236 }\r
237 \r
238 }}