]> git.sesse.net Git - casparcg/blobdiff - core/mixer/image/image_kernel.cpp
Made MIXER CROP work more as one might expect
[casparcg] / core / mixer / image / image_kernel.cpp
index 16f494844ea3990addb7349357b34f70104e488f..4b772627789e1fe946cae45fd1b167bf7f40135e 100644 (file)
 /*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright 2013 Sveriges Television AB http://casparcg.com/\r
 *\r
-*  This file is part of CasparCG.\r
+* This file is part of CasparCG (www.casparcg.com).\r
 *\r
-*    CasparCG is free software: you can redistribute it and/or modify\r
-*    it under the terms of the GNU General Public License as published by\r
-*    the Free Software Foundation, either version 3 of the License, or\r
-*    (at your option) any later version.\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
 *\r
-*    CasparCG is distributed in the hope that it will be useful,\r
-*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-*    GNU General Public License for more details.\r
-\r
-*    You should have received a copy of the GNU General Public License\r
-*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
 *\r
+* Author: Robert Nagy, ronag89@gmail.com\r
 */\r
+\r
 #include "../../stdafx.h"\r
 \r
 #include "image_kernel.h"\r
 \r
+#include "shader/image_shader.h"\r
+#include "shader/blending_glsl.h"\r
+\r
+#include "../gpu/shader.h"\r
+#include "../gpu/device_buffer.h"\r
+#include "../gpu/ogl_device.h"\r
+\r
 #include <common/exception/exceptions.h>\r
 #include <common/gl/gl_check.h>\r
+#include <common/env.h>\r
 \r
 #include <core/video_format.h>\r
 #include <core/producer/frame/pixel_format.h>\r
-#include <core/producer/frame/image_transform.h>\r
-\r
-#include <Glee.h>\r
+#include <core/producer/frame/frame_transform.h>\r
 \r
 #include <boost/noncopyable.hpp>\r
 \r
-#include <unordered_map>\r
-\r
-namespace caspar { namespace mixer {\r
+namespace caspar { namespace core {\r
 \r
-class shader_program : boost::noncopyable\r
+// http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect\r
+bool get_line_intersection(\r
+               double p0_x, double p0_y,\r
+               double p1_x, double p1_y, \r
+               double p2_x, double p2_y,\r
+               double p3_x, double p3_y,\r
+               double& result_x, double& result_y)\r
 {\r
-       GLuint program_;\r
-public:\r
+       double s1_x = p1_x - p0_x;\r
+       double s1_y = p1_y - p0_y;\r
+       double s2_x = p3_x - p2_x;\r
+       double s2_y = p3_y - p2_y;\r
 \r
-       shader_program() : program_(0) {}\r
-       shader_program(shader_program&& other) : program_(other.program_){other.program_ = 0;}\r
-       shader_program(const std::string& vertex_source_str, const std::string& fragment_source_str) : program_(0)\r
+       double s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);\r
+       double t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);\r
+\r
+       if (s >= 0 && s <= 1 && t >= 0 && t <= 1)\r
        {\r
-               GLint success;\r
-       \r
-               const char* vertex_source = vertex_source_str.c_str();\r
-                                               \r
-               auto vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);\r
-                                       \r
-               GL(glShaderSourceARB(vertex_shader, 1, &vertex_source, NULL));\r
-               GL(glCompileShaderARB(vertex_shader));\r
+               // Collision detected\r
+               result_x = p0_x + (t * s1_x);\r
+               result_y = p0_y + (t * s1_y);\r
 \r
-               GL(glGetObjectParameterivARB(vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
-               {\r
-                       char info[2048];\r
-                       GL(glGetInfoLogARB(vertex_shader, sizeof(info), 0, info));\r
-                       GL(glDeleteObjectARB(vertex_shader));\r
-                       std::stringstream str;\r
-                       str << "Failed to compile vertex shader:" << std::endl << info << std::endl;\r
-                       BOOST_THROW_EXCEPTION(gl::gl_error() << msg_info(str.str()));\r
-               }\r
-                       \r
-               const char* fragment_source = fragment_source_str.c_str();\r
-                                               \r
-               auto fragmemt_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);\r
-                                       \r
-               GL(glShaderSourceARB(fragmemt_shader, 1, &fragment_source, NULL));\r
-               GL(glCompileShaderARB(fragmemt_shader));\r
+               return true;\r
+       }\r
 \r
-               GL(glGetObjectParameterivARB(fragmemt_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
-               {\r
-                       char info[2048];\r
-                       GL(glGetInfoLogARB(fragmemt_shader, sizeof(info), 0, info));\r
-                       GL(glDeleteObjectARB(fragmemt_shader));\r
-                       std::stringstream str;\r
-                       str << "Failed to compile fragment shader:" << std::endl << info << std::endl;\r
-                       BOOST_THROW_EXCEPTION(gl::gl_error() << msg_info(str.str()));\r
-               }\r
-                       \r
-               program_ = glCreateProgramObjectARB();\r
-                       \r
-               GL(glAttachObjectARB(program_, vertex_shader));\r
-               GL(glAttachObjectARB(program_, fragmemt_shader));\r
+       return false; // No collision\r
+}\r
 \r
-               GL(glLinkProgramARB(program_));\r
-                       \r
-               GL(glDeleteObjectARB(vertex_shader));\r
-               GL(glDeleteObjectARB(fragmemt_shader));\r
+double hypotenuse(double x1, double y1, double x2, double y2)\r
+{\r
+       auto x = x2 - x1;\r
+       auto y = y2 - y1;\r
 \r
-               GL(glGetObjectParameterivARB(program_, GL_OBJECT_LINK_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
-               {\r
-                       char info[2048];\r
-                       GL(glGetInfoLogARB(program_, sizeof(info), 0, info));\r
-                       GL(glDeleteObjectARB(program_));\r
-                       std::stringstream str;\r
-                       str << "Failed to link shader program:" << std::endl << info << std::endl;\r
-                       BOOST_THROW_EXCEPTION(gl::gl_error() << msg_info(str.str()));\r
-               }\r
-               GL(glUseProgramObjectARB(program_));\r
-               glUniform1i(glGetUniformLocation(program_, "plane[0]"), 0);\r
-               glUniform1i(glGetUniformLocation(program_, "plane[1]"), 1);\r
-               glUniform1i(glGetUniformLocation(program_, "plane[2]"), 2);\r
-               glUniform1i(glGetUniformLocation(program_, "plane[3]"), 3);\r
-       }\r
+       return std::sqrt(x * x + y * y);\r
+}\r
 \r
-       GLint get_location(const char* name)\r
-       {\r
-               GLint loc = glGetUniformLocation(program_, name);\r
-               return loc;\r
-       }\r
+double calc_q(double close_diagonal, double distant_diagonal)\r
+{\r
+       return (close_diagonal + distant_diagonal) / distant_diagonal;\r
+}\r
 \r
-       shader_program& operator=(shader_program&& other) \r
-       {\r
-               program_ = other.program_; \r
-               other.program_ = 0; \r
-               return *this;\r
-       }\r
+bool is_above_screen(double y)\r
+{\r
+       return y < 0.0;\r
+}\r
 \r
-       ~shader_program()\r
-       {\r
-               glDeleteProgram(program_);\r
-       }\r
+bool is_below_screen(double y)\r
+{\r
+       return y > 1.0;\r
+}\r
 \r
-       void use()\r
-       {       \r
-               GL(glUseProgramObjectARB(program_));            \r
-       }\r
-};\r
+bool is_left_of_screen(double x)\r
+{\r
+       return x < 0.0;\r
+}\r
+\r
+bool is_right_of_screen(double x)\r
+{\r
+       return x > 1.0;\r
+}\r
+\r
+bool is_outside_screen(\r
+               double x1, double y1,\r
+               double x2, double y2,\r
+               double x3, double y3,\r
+               double x4, double y4)\r
+{\r
+       // Every point needs to be outside the screen on the *same* side in order to be considered outside the screen.\r
+       return (is_above_screen(y1) && is_above_screen(y2) && is_above_screen(y3) && is_above_screen(y4))\r
+               || (is_below_screen(y1) && is_below_screen(y2) && is_below_screen(y3) && is_below_screen(y4))\r
+               || (is_left_of_screen(x1) && is_left_of_screen(x2) && is_left_of_screen(x3) && is_left_of_screen(x4))\r
+               || (is_right_of_screen(x1) && is_right_of_screen(x2) && is_right_of_screen(x3) && is_right_of_screen(x4));\r
+}\r
 \r
-GLubyte progressive_pattern[] = {\r
-       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\r
-       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\r
-       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\r
-       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xFF, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
-       \r
 GLubyte upper_pattern[] = {\r
        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
        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
@@ -154,155 +130,326 @@ GLubyte lower_pattern[] = {
 \r
 struct image_kernel::implementation : boost::noncopyable\r
 {      \r
-       std::unordered_map<core::pixel_format::type, shader_program> shaders_;\r
+       safe_ptr<ogl_device>    ogl_;\r
+       safe_ptr<shader>                shader_;\r
+       bool                                    blend_modes_;\r
+       bool                                    post_processing_;\r
+       bool                                    supports_texture_barrier_;\r
+                                                       \r
+       implementation(const safe_ptr<ogl_device>& ogl)\r
+               : ogl_(ogl)\r
+               , shader_(ogl_->invoke([&]{return get_image_shader(*ogl, blend_modes_, post_processing_);}))\r
+               , supports_texture_barrier_(glTextureBarrierNV != 0)\r
+       {\r
+               if (!supports_texture_barrier_)\r
+                       CASPAR_LOG(warning) << L"[image_mixer] TextureBarrierNV not supported. Post processing will not be available";\r
+       }\r
 \r
-public:\r
-       std::unordered_map<core::pixel_format::type, shader_program>& shaders()\r
+       void draw(draw_params&& params)\r
        {\r
-               GL(glEnable(GL_POLYGON_STIPPLE));\r
-               GL(glEnable(GL_BLEND));\r
-               GL(glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE));\r
+               static const double epsilon = 0.001;\r
+\r
+               CASPAR_ASSERT(params.pix_desc.planes.size() == params.textures.size());\r
+\r
+               if(params.textures.empty() || !params.background)\r
+                       return;\r
+\r
+               if(params.transform.opacity < epsilon)\r
+                       return;\r
+\r
+               auto f_p = params.transform.fill_translation;\r
+               auto f_s = params.transform.fill_scale;\r
+\r
+               // Calculate rotation\r
+               auto aspect = params.aspect_ratio;\r
+               auto angle = params.transform.angle;\r
+\r
+               auto rotate = [angle, aspect](double orig_x, double orig_y) -> boost::array<double, 2>\r
+               {\r
+                       boost::array<double, 2> result;\r
+                       result[0] = orig_x * std::cos(angle) - orig_y * std::sin(angle);\r
+                       result[1] = orig_x * std::sin(angle) + orig_y * std::cos(angle);\r
+                       result[1] *= aspect;\r
+\r
+                       return result;\r
+               };\r
+\r
+               auto anchor = params.transform.anchor;\r
+               auto crop = params.transform.crop;\r
+               auto pers = params.transform.perspective;\r
+\r
+               auto ul = rotate((-anchor[0] + pers.ul[0] + crop.ul[0]      ) * f_s[0], (-anchor[1] + pers.ul[1] + crop.ul[1]      ) * f_s[1] / aspect);\r
+               auto ur = rotate((-anchor[0] + pers.ur[0] + crop.lr[0] - 1.0) * f_s[0], (-anchor[1] + pers.ur[1] + crop.ul[1]      ) * f_s[1] / aspect);\r
+               auto lr = rotate((-anchor[0] + pers.lr[0] + crop.lr[0] - 1.0) * f_s[0], (-anchor[1] + pers.lr[1] + crop.lr[1] - 1.0) * f_s[1] / aspect);\r
+               auto ll = rotate((-anchor[0] + pers.ll[0] + crop.ul[0]      ) * f_s[0], (-anchor[1] + pers.ll[1] + crop.lr[1] - 1.0) * f_s[1] / aspect);\r
+\r
+               auto upper_left_x =  f_p[0] + ul[0];\r
+               auto upper_left_y =  f_p[1] + ul[1];\r
+               auto upper_right_x = f_p[0] + ur[0];\r
+               auto upper_right_y = f_p[1] + ur[1];\r
+               auto lower_right_x = f_p[0] + lr[0];\r
+               auto lower_right_y = f_p[1] + lr[1];\r
+               auto lower_left_x =  f_p[0] + ll[0];\r
+               auto lower_left_y =  f_p[1] + ll[1];\r
+\r
+               // Skip drawing if the QUAD will be outside the screen.\r
+               if (is_outside_screen(\r
+                                       upper_left_x, upper_left_y,\r
+                                       upper_right_x, upper_right_y,\r
+                                       lower_right_x, lower_right_y,\r
+                                       lower_left_x, lower_left_y))\r
+               {\r
+                       return;\r
+               }\r
+               \r
+               if(!std::all_of(params.textures.begin(), params.textures.end(), std::mem_fn(&device_buffer::ready)))\r
+               {\r
+                       CASPAR_LOG(trace) << L"[image_mixer] Performance warning. Host to device transfer not complete, GPU will be stalled";\r
+                       ogl_->yield(); // Try to give it some more time.\r
+               }               \r
+               \r
+               // Bind textures\r
+\r
+               for(size_t n = 0; n < params.textures.size(); ++n)\r
+                       params.textures[n]->bind(n);\r
+\r
+               if(params.local_key)\r
+                       params.local_key->bind(texture_id::local_key);\r
+               \r
+               if(params.layer_key)\r
+                       params.layer_key->bind(texture_id::layer_key);\r
+                       \r
+               // Setup shader\r
+                                                               \r
+               ogl_->use(*shader_);\r
+\r
+               shader_->set("plane[0]",                texture_id::plane0);\r
+               shader_->set("plane[1]",                texture_id::plane1);\r
+               shader_->set("plane[2]",                texture_id::plane2);\r
+               shader_->set("plane[3]",                texture_id::plane3);\r
+               shader_->set("local_key",               texture_id::local_key);\r
+               shader_->set("layer_key",               texture_id::layer_key);\r
+               shader_->set("is_hd",                   params.pix_desc.planes.at(0).height > 700 ? 1 : 0);\r
+               shader_->set("has_local_key",   bool(params.local_key));\r
+               shader_->set("has_layer_key",   bool(params.layer_key));\r
+               shader_->set("pixel_format",    params.pix_desc.pix_fmt);       \r
+               shader_->set("opacity",                 params.transform.is_key ? 1.0 : params.transform.opacity);      \r
+               shader_->set("post_processing", false);\r
+\r
+               shader_->set("chroma_mode",    params.blend_mode.chroma.key == chroma::green ? 1 : (params.blend_mode.chroma.key == chroma::blue ? 2 : 0));\r
+        shader_->set("chroma_blend",   params.blend_mode.chroma.threshold, params.blend_mode.chroma.softness);\r
+        shader_->set("chroma_spill",   params.blend_mode.chroma.spill);\r
+//        shader_->set("chroma.key",      ((params.blend_mode.chroma.key >> 24) && 0xff)/255.0f,\r
+//                                        ((params.blend_mode.chroma.key >> 16) && 0xff)/255.0f,\r
+//                                        (params.blend_mode.chroma.key & 0xff)/255.0f);\r
+//             if (params.blend_mode.chroma.key != chroma::none)\r
+//             {\r
+//                 shader_->set("chroma.threshold",    params.blend_mode.chroma.threshold);\r
+//                 shader_->set("chroma.softness",     params.blend_mode.chroma.softness);\r
+//            shader_->set("chroma.blur",         params.blend_mode.chroma.blur);\r
+//                 shader_->set("chroma.spill",        params.blend_mode.chroma.spill);\r
+//            shader_->set("chroma.show_mask",    params.blend_mode.chroma.show_mask);\r
+//             }\r
+               \r
+               // Setup blend_func             \r
+               if(params.transform.is_key)\r
+                       params.blend_mode = blend_mode::normal;\r
+\r
+               if(blend_modes_)\r
+               {\r
+                       params.background->bind(texture_id::background);\r
+\r
+                       shader_->set("background",      texture_id::background);\r
+                       shader_->set("blend_mode",      params.blend_mode.mode);\r
+                       shader_->set("keyer",           params.keyer);\r
+               }\r
+               else\r
+               {\r
+                       switch(params.keyer)\r
+                       {\r
+                       case keyer::additive:\r
+                               ogl_->blend_func(GL_ONE, GL_ONE);       \r
+                               break;\r
+                       case keyer::linear:\r
+                       default:                                \r
+                               ogl_->blend_func(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);       \r
+                       }               \r
+               }\r
+\r
+               // Setup image-adjustements\r
+               \r
+               if(params.transform.levels.min_input  > epsilon         ||\r
+                  params.transform.levels.max_input  < 1.0-epsilon     ||\r
+                  params.transform.levels.min_output > epsilon         ||\r
+                  params.transform.levels.max_output < 1.0-epsilon     ||\r
+                  std::abs(params.transform.levels.gamma - 1.0) > epsilon)\r
+               {\r
+                       shader_->set("levels", true);   \r
+                       shader_->set("min_input",       params.transform.levels.min_input);     \r
+                       shader_->set("max_input",       params.transform.levels.max_input);\r
+                       shader_->set("min_output",      params.transform.levels.min_output);\r
+                       shader_->set("max_output",      params.transform.levels.max_output);\r
+                       shader_->set("gamma",           params.transform.levels.gamma);\r
+               }\r
+               else\r
+                       shader_->set("levels", false);  \r
 \r
-               if(shaders_.empty())\r
+               if(std::abs(params.transform.brightness - 1.0) > epsilon ||\r
+                  std::abs(params.transform.saturation - 1.0) > epsilon ||\r
+                  std::abs(params.transform.contrast - 1.0)   > epsilon)\r
                {\r
-               std::string common_vertex = \r
-                       "void main()                                                                                                                    "\r
-                       "{                                                                                                                                              "\r
-                       "       gl_TexCoord[0] = gl_MultiTexCoord0;                                                                     "\r
-                       "       gl_FrontColor = gl_Color;                                                                                       "\r
-                       "       gl_Position = ftransform();                                                                                     "\r
-                       "}                                                                                                                                              ";\r
-\r
-               std::string common_fragment = \r
-                       "uniform sampler2D      plane[4];                                                                                       "\r
-                       "uniform float          gain;                                                                                           "\r
-                       "uniform bool           HD;                                                                                                     "\r
-                       "uniform bool           has_separate_key;                                                                       "\r
-                                                                                                                                                               \r
-                       // NOTE: YCbCr, ITU-R, http://www.intersil.com/data/an/an9717.pdf               \r
-                       // TODO: Support for more yuv formats might be needed.                                  \r
-                       "vec4 ycbcra_to_bgra_sd(float y, float cb, float cr, float a)                   "\r
-                       "{                                                                                                                                              "\r
-                       "       cb -= 0.5;                                                                                                                      "\r
-                       "       cr -= 0.5;                                                                                                                      "\r
-                       "       y = 1.164*(y-0.0625);                                                                                           "\r
-                       "                                                                                                                                               "\r
-                       "       vec4 color;                                                                                                                     "\r
-                       "       color.r = y + 1.596 * cr;                                                                                       "\r
-                       "       color.g = y - 0.813 * cr - 0.391 * cb;                                                          "\r
-                       "       color.b = y + 2.018 * cb;                                                                                       "\r
-                       "       color.a = a;                                                                                                            "\r
-                       "                                                                                                                                               "\r
-                       "       return color;                                                                                                           "\r
-                       "}                                                                                                                                              "                       \r
-                       "                                                                                                                                               "\r
-\r
-                       "vec4 ycbcra_to_bgra_hd(float y, float cb, float cr, float a)                   "\r
-                       "{                                                                                                                                              "\r
-                       "       cb -= 0.5;                                                                                                                      "\r
-                       "       cr -= 0.5;                                                                                                                      "\r
-                       "       y = 1.164*(y-0.0625);                                                                                           "\r
-                       "                                                                                                                                               "\r
-                       "       vec4 color;                                                                                                                     "\r
-                       "       color.r = y + 1.793 * cr;                                                                                       "\r
-                       "       color.g = y - 0.534 * cr - 0.213 * cb;                                                          "\r
-                       "       color.b = y + 2.115 * cb;                                                                                       "\r
-                       "       color.a = a;                                                                                                            "\r
-                       "                                                                                                                                               "\r
-                       "       return color;                                                                                                           "\r
-                       "}                                                                                                                                              "                       \r
-                       "                                                                                                                                               ";\r
+                       shader_->set("csb",     true);  \r
                        \r
-               shaders_[core::pixel_format::abgr] = shader_program(common_vertex, common_fragment +\r
-\r
-                       "void main()                                                                                                                    "\r
-                       "{                                                                                                                                              "\r
-                       "       vec4 abgr = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
-                       "       if(has_separate_key)                                                                                            "\r
-                       "               rgba.b = texture2D(plane[3], gl_TexCoord[0].st).r;                              "\r
-                       "       gl_FragColor = abgr.argb * gain;                                                                        "\r
-                       "}                                                                                                                                              ");\r
+                       shader_->set("brt", params.transform.brightness);       \r
+                       shader_->set("sat", params.transform.saturation);\r
+                       shader_->set("con", params.transform.contrast);\r
+               }\r
+               else\r
+                       shader_->set("csb",     false); \r
                \r
-               shaders_[core::pixel_format::argb]= shader_program(common_vertex, common_fragment +\r
-\r
-                       "void main()                                                                                                                    "       \r
-                       "{                                                                                                                                              "\r
-                       "       vec4 argb = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
-                       "       if(has_separate_key)                                                                                            "\r
-                       "               rgba.b = texture2D(plane[3], gl_TexCoord[0].st).r;                              "\r
-                       "       gl_FragColor = argb.grab * gl_Color * gain;                                                     "\r
-                       "}                                                                                                                                              ");\r
+               // Setup interlacing\r
+\r
+               if(params.transform.field_mode == core::field_mode::progressive)                        \r
+                       ogl_->disable(GL_POLYGON_STIPPLE);                      \r
+               else                    \r
+               {\r
+                       ogl_->enable(GL_POLYGON_STIPPLE);\r
+\r
+                       if(params.transform.field_mode == core::field_mode::upper)\r
+                               ogl_->stipple_pattern(upper_pattern);\r
+                       else if(params.transform.field_mode == core::field_mode::lower)\r
+                               ogl_->stipple_pattern(lower_pattern);\r
+               }\r
+\r
+               // Setup drawing area\r
                \r
-               shaders_[core::pixel_format::bgra]= shader_program(common_vertex, common_fragment +\r
-\r
-                       "void main()                                                                                                                    "\r
-                       "{                                                                                                                                              "\r
-                       "       vec4 bgra = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
-                       "       if(has_separate_key)                                                                                            "\r
-                       "               rgba.a = texture2D(plane[3], gl_TexCoord[0].st).r;                              "\r
-                       "       gl_FragColor = bgra.rgba * gl_Color * gain;                                                     "\r
-                       "}                                                                                                                                              ");\r
+               ogl_->viewport(0, 0, params.background->width(), params.background->height());\r
+                                                               \r
+               auto m_p = params.transform.clip_translation;\r
+               auto m_s = params.transform.clip_scale;\r
+\r
+               bool scissor = m_p[0] > std::numeric_limits<double>::epsilon()                  || m_p[1] > std::numeric_limits<double>::epsilon() ||\r
+                                          m_s[0] < (1.0 - std::numeric_limits<double>::epsilon())      || m_s[1] < (1.0 - std::numeric_limits<double>::epsilon());\r
+\r
+               if(scissor)\r
+               {\r
+                       double w = static_cast<double>(params.background->width());\r
+                       double h = static_cast<double>(params.background->height());\r
                \r
-               shaders_[core::pixel_format::rgba] = shader_program(common_vertex, common_fragment +\r
-\r
-                       "void main()                                                                                                                    "\r
-                       "{                                                                                                                                              "\r
-                       "       vec4 rgba = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
-                       "       if(has_separate_key)                                                                                            "\r
-                       "               rgba.a = texture2D(plane[3], gl_TexCoord[0].st).r;                              "\r
-                       "       gl_FragColor = rgba.bgra * gl_Color * gain;                                                     "\r
-                       "}                                                                                                                                              ");\r
+                       ogl_->enable(GL_SCISSOR_TEST);\r
+                       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
+               }\r
+\r
+               // Set render target\r
                \r
-               shaders_[core::pixel_format::ycbcr] = shader_program(common_vertex, common_fragment +\r
-\r
-                       "void main()                                                                                                                    "\r
-                       "{                                                                                                                                              "\r
-                       "       float y  = texture2D(plane[0], gl_TexCoord[0].st).r;                            "\r
-                       "       float cb = texture2D(plane[1], gl_TexCoord[0].st).r;                            "\r
-                       "       float cr = texture2D(plane[2], gl_TexCoord[0].st).r;                            "\r
-                       "       float a = 1.0;                                                                                                          "       \r
-                       "       if(has_separate_key)                                                                                            "\r
-                       "               a = texture2D(plane[3], gl_TexCoord[0].st).r;                                   "\r
-                       "       if(HD)                                                                                                                          "\r
-                       "               gl_FragColor = ycbcra_to_bgra_hd(y, cb, cr, a) * gl_Color * gain;"\r
-                       "       else                                                                                                                            "\r
-                       "               gl_FragColor = ycbcra_to_bgra_sd(y, cb, cr, a) * gl_Color * gain;"\r
-                       "}                                                                                                                                              ");\r
+               ogl_->attach(*params.background);\r
+               \r
+               // Perspective correction\r
+               auto ulq = 1.0;\r
+               auto urq = 1.0;\r
+               auto lrq = 1.0;\r
+               auto llq = 1.0;\r
+               double diagonal_intersection_x;\r
+               double diagonal_intersection_y;\r
+\r
+               if (get_line_intersection(\r
+                               pers.ul[0] + crop.ul[0]      , pers.ul[1] + crop.ul[1]      ,\r
+                               pers.lr[0] + crop.lr[0] - 1.0, pers.lr[1] + crop.lr[1] - 1.0,\r
+                               pers.ur[0] + crop.lr[0] - 1.0, pers.ur[1] + crop.ul[1]      ,\r
+                               pers.ll[0] + crop.ul[0]      , pers.ll[1] + crop.lr[1] - 1.0,\r
+                               diagonal_intersection_x,\r
+                               diagonal_intersection_y))\r
+               {\r
+                       // http://www.reedbeta.com/blog/2012/05/26/quadrilateral-interpolation-part-1/\r
+                       auto d0 = hypotenuse(pers.ll[0] + crop.ul[0]      , pers.ll[1] + crop.lr[1] - 1.0, diagonal_intersection_x, diagonal_intersection_y);\r
+                       auto d1 = hypotenuse(pers.lr[0] + crop.lr[0] - 1.0, pers.lr[1] + crop.lr[1] - 1.0, diagonal_intersection_x, diagonal_intersection_y);\r
+                       auto d2 = hypotenuse(pers.ur[0] + crop.lr[0] - 1.0, pers.ur[1] + crop.ul[1]      , diagonal_intersection_x, diagonal_intersection_y);\r
+                       auto d3 = hypotenuse(pers.ul[0] + crop.ul[0]      , pers.ul[1] + crop.ul[1]      , diagonal_intersection_x, diagonal_intersection_y);\r
+\r
+                       ulq = calc_q(d3, d1);\r
+                       urq = calc_q(d2, d0);\r
+                       lrq = calc_q(d1, d3);\r
+                       llq = calc_q(d0, d2);\r
+               }\r
+\r
+               // Draw\r
+               /*\r
+                       GL_TEXTURE0 are texture coordinates to the source material, what will be rendered with this call. These are always set to the whole thing.\r
+                       GL_TEXTURE1 are texture coordinates to background- / key-material, that which will have to be taken in consideration when blending. These are set to the rectangle over which the source will be rendered\r
+               */\r
+               glBegin(GL_QUADS);\r
+                       glMultiTexCoord4d(GL_TEXTURE0, crop.ul[0] * ulq, crop.ul[1] * ulq, 0, ulq); glMultiTexCoord2d(GL_TEXTURE1, upper_left_x,  upper_left_y);                glVertex2d(upper_left_x  * 2.0 - 1.0, upper_left_y  * 2.0 - 1.0);\r
+                       glMultiTexCoord4d(GL_TEXTURE0, crop.lr[0] * urq, crop.ul[1] * urq, 0, urq); glMultiTexCoord2d(GL_TEXTURE1, upper_right_x, upper_right_y);               glVertex2d(upper_right_x * 2.0 - 1.0, upper_right_y * 2.0 - 1.0);\r
+                       glMultiTexCoord4d(GL_TEXTURE0, crop.lr[0] * lrq, crop.lr[1] * lrq, 0, lrq); glMultiTexCoord2d(GL_TEXTURE1, lower_right_x, lower_right_y);               glVertex2d(lower_right_x * 2.0 - 1.0, lower_right_y * 2.0 - 1.0);\r
+                       glMultiTexCoord4d(GL_TEXTURE0, crop.ul[0] * llq, crop.lr[1] * llq, 0, llq); glMultiTexCoord2d(GL_TEXTURE1, lower_left_x,  lower_left_y);                glVertex2d(lower_left_x  * 2.0 - 1.0, lower_left_y  * 2.0 - 1.0);\r
+               glEnd();\r
                \r
-               shaders_[core::pixel_format::ycbcra] = shader_program(common_vertex, common_fragment +\r
-\r
-                       "void main()                                                                                                                    "\r
-                       "{                                                                                                                                              "\r
-                       "       float y  = texture2D(plane[0], gl_TexCoord[0].st).r;                            "\r
-                       "       float cb = texture2D(plane[1], gl_TexCoord[0].st).r;                            "\r
-                       "       float cr = texture2D(plane[2], gl_TexCoord[0].st).r;                            "\r
-                       "       float a  = texture2D(plane[3], gl_TexCoord[0].st).r;                            "\r
-                       "       if(HD)                                                                                                                          "\r
-                       "               gl_FragColor = ycbcra_to_bgra_hd(y, cb, cr, a) * gl_Color * gain;"\r
-                       "       else                                                                                                                            "\r
-                       "               gl_FragColor = ycbcra_to_bgra_sd(y, cb, cr, a) * gl_Color * gain;"\r
-                       "}                                                                                                                                              ");\r
+               // Cleanup\r
+\r
+               ogl_->disable(GL_SCISSOR_TEST);\r
+                                               \r
+               params.textures.clear();\r
+               ogl_->yield(); // Return resources to pool as early as possible.\r
+\r
+               if(blend_modes_)\r
+               {\r
+                       // http://www.opengl.org/registry/specs/NV/texture_barrier.txt\r
+                       // This allows us to use framebuffer (background) both as source and target while blending.\r
+                       glTextureBarrierNV(); \r
                }\r
-               return shaders_;\r
+       }\r
+\r
+       void post_process(\r
+                       const safe_ptr<device_buffer>& background, bool straighten_alpha)\r
+       {\r
+               bool should_post_process = \r
+                               supports_texture_barrier_\r
+                               && straighten_alpha\r
+                               && post_processing_;\r
+\r
+               if (!should_post_process)\r
+                       return;\r
+\r
+               if (!blend_modes_)\r
+                       ogl_->disable(GL_BLEND);\r
+\r
+               ogl_->disable(GL_POLYGON_STIPPLE);\r
+\r
+               ogl_->attach(*background);\r
+\r
+               background->bind(texture_id::background);\r
+\r
+               ogl_->use(*shader_);\r
+               shader_->set("background", texture_id::background);\r
+               shader_->set("post_processing", should_post_process);\r
+               shader_->set("straighten_alpha", straighten_alpha);\r
+\r
+               ogl_->viewport(0, 0, background->width(), background->height());\r
+\r
+               glBegin(GL_QUADS);\r
+                       glMultiTexCoord2d(GL_TEXTURE0, 0.0, 0.0); glVertex2d(-1.0, -1.0);\r
+                       glMultiTexCoord2d(GL_TEXTURE0, 1.0, 0.0); glVertex2d( 1.0, -1.0);\r
+                       glMultiTexCoord2d(GL_TEXTURE0, 1.0, 1.0); glVertex2d( 1.0,  1.0);\r
+                       glMultiTexCoord2d(GL_TEXTURE0, 0.0, 1.0); glVertex2d(-1.0,  1.0);\r
+               glEnd();\r
+\r
+               glTextureBarrierNV();\r
+\r
+               if (!blend_modes_)\r
+                       ogl_->enable(GL_BLEND);\r
        }\r
 };\r
 \r
-image_kernel::image_kernel() : impl_(new implementation()){}\r
+image_kernel::image_kernel(const safe_ptr<ogl_device>& ogl) : impl_(new implementation(ogl)){}\r
+void image_kernel::draw(draw_params&& params)\r
+{\r
+       impl_->draw(std::move(params));\r
+}\r
 \r
-void image_kernel::apply(const core::pixel_format_desc& pix_desc, const core::image_transform& transform, bool has_separate_key)\r
+void image_kernel::post_process(\r
+               const safe_ptr<device_buffer>& background, bool straighten_alpha)\r
 {\r
-       impl_->shaders()[pix_desc.pix_fmt].use();\r
-\r
-       GL(glUniform1f(impl_->shaders()[pix_desc.pix_fmt].get_location("gain"), static_cast<GLfloat>(transform.get_gain())));\r
-       GL(glUniform1i(impl_->shaders()[pix_desc.pix_fmt].get_location("HD"), pix_desc.planes.at(0).height > 700 ? 1 : 0));\r
-       GL(glUniform1i(impl_->shaders()[pix_desc.pix_fmt].get_location("has_separate_key"), has_separate_key ? 1 : 0));\r
-\r
-       if(transform.get_mode() == core::video_mode::upper)\r
-               glPolygonStipple(upper_pattern);\r
-       else if(transform.get_mode() == core::video_mode::lower)\r
-               glPolygonStipple(lower_pattern);\r
-       else\r
-               glPolygonStipple(progressive_pattern);\r
+       impl_->post_process(background, straighten_alpha);\r
 }\r
 \r
-}}
\ No newline at end of file
+}}\r