]> git.sesse.net Git - casparcg/blobdiff - core/mixer/image/image_kernel.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / mixer / image / image_kernel.cpp
index 16f494844ea3990addb7349357b34f70104e488f..bd75820e058b4d51ccf19540ed62e6f5af763d27 100644 (file)
 \r
 #include "image_kernel.h"\r
 \r
+#include "image_shader.h"\r
+\r
+#include "blending_glsl.h"\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 <GL/glew.h>\r
 \r
 #include <boost/noncopyable.hpp>\r
 \r
 #include <unordered_map>\r
 \r
-namespace caspar { namespace mixer {\r
-\r
-class shader_program : boost::noncopyable\r
-{\r
-       GLuint program_;\r
-public:\r
+namespace caspar { namespace core {\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
+       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
+               \r
+GLubyte lower_pattern[] = {\r
+       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
+       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
+       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
+       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
 \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
+struct image_kernel::implementation : boost::noncopyable\r
+{      \r
+       std::shared_ptr<shader> shader_;\r
+       bool                                    advanced_blend_modes_;\r
+                                                       \r
+       void draw(ogl_device&                                                                   ogl,\r
+                         render_item&&                                                                 item,\r
+                         const safe_ptr<device_buffer>&                                background,\r
+                         const std::shared_ptr<device_buffer>&                 local_key,                        \r
+                         const std::shared_ptr<device_buffer>&                 layer_key)\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
+               static const double epsilon = 0.001;\r
 \r
-               GL(glGetObjectParameterivARB(vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
+               CASPAR_ASSERT(item.pix_desc.planes.size() == item.textures.size());\r
+\r
+               if(item.textures.empty())\r
+                       return;\r
+\r
+               if(item.transform.get_opacity() < epsilon)\r
+                       return;\r
+               \r
+               if(!std::all_of(item.textures.begin(), item.textures.end(), std::mem_fn(&device_buffer::ready)))\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
+                       CASPAR_LOG(warning) << 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 < item.textures.size(); ++n)\r
+                       item.textures[n]->bind(n);\r
+\r
+               if(local_key)\r
+                       local_key->bind(texture_id::local_key);\r
+               \r
+               if(layer_key)\r
+                       layer_key->bind(texture_id::layer_key);\r
                        \r
-               const char* fragment_source = fragment_source_str.c_str();\r
+               // Setup shader\r
+\r
+               if(!shader_)\r
+                       shader_ = get_image_shader(ogl, advanced_blend_modes_);\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
+               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",                   item.pix_desc.planes.at(0).height > 700 ? 1 : 0);\r
+               shader_->set("has_local_key",   local_key);\r
+               shader_->set("has_layer_key",   layer_key);\r
+               shader_->set("pixel_format",    item.pix_desc.pix_fmt); \r
+               \r
+               // Setup blend_func\r
+               \r
+               if(advanced_blend_modes_)\r
+               {\r
+                       background->bind(6);\r
 \r
-               GL(glGetObjectParameterivARB(fragmemt_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
+                       shader_->set("background",      texture_id::background);\r
+                       shader_->set("blend_mode",      item.transform.get_is_key() ? core::image_transform::blend_mode::normal : item.transform.get_blend_mode());\r
+               }\r
+               else\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
+                       switch(item.transform.get_blend_mode())\r
+                       {\r
+                       case image_transform::blend_mode::add:                  \r
+                               ogl.blend_func_separate(GL_ONE, GL_ONE, GL_ONE, GL_ONE);\r
+                               break;\r
+                       case image_transform::blend_mode::replace:                      \r
+                               ogl.blend_func_separate(GL_ONE, GL_ZERO, GL_ONE, GL_ONE);\r
+                               break;\r
+                       case image_transform::blend_mode::screen:\r
+                               ogl.blend_func_separate(GL_ONE, GL_ONE_MINUS_SRC_COLOR, GL_ONE, GL_ONE);\r
+                       default:\r
+                               ogl.blend_func_separate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);\r
+                               break;\r
+                       }\r
                }\r
-                       \r
-               program_ = glCreateProgramObjectARB();\r
-                       \r
-               GL(glAttachObjectARB(program_, vertex_shader));\r
-               GL(glAttachObjectARB(program_, fragmemt_shader));\r
 \r
-               GL(glLinkProgramARB(program_));\r
-                       \r
-               GL(glDeleteObjectARB(vertex_shader));\r
-               GL(glDeleteObjectARB(fragmemt_shader));\r
+               // Setup image-adjustements\r
+\r
+               auto levels = item.transform.get_levels();\r
 \r
-               GL(glGetObjectParameterivARB(program_, GL_OBJECT_LINK_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
+               if(levels.min_input  > epsilon          ||\r
+                  levels.max_input  < 1.0-epsilon      ||\r
+                  levels.min_output > epsilon          ||\r
+                  levels.max_output < 1.0-epsilon      ||\r
+                  std::abs(levels.gamma - 1.0) > epsilon)\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
+                       shader_->set("levels", true);   \r
+                       shader_->set("min_input", levels.min_input);    \r
+                       shader_->set("max_input", levels.max_input);\r
+                       shader_->set("min_output", levels.min_output);\r
+                       shader_->set("max_output", levels.max_output);\r
+                       shader_->set("gamma", levels.gamma);\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
+               else\r
+                       shader_->set("levels", false);  \r
 \r
-       GLint get_location(const char* name)\r
-       {\r
-               GLint loc = glGetUniformLocation(program_, name);\r
-               return loc;\r
-       }\r
-\r
-       shader_program& operator=(shader_program&& other) \r
-       {\r
-               program_ = other.program_; \r
-               other.program_ = 0; \r
-               return *this;\r
-       }\r
+               if(std::abs(item.transform.get_brightness() - 1.0) > epsilon ||\r
+                  std::abs(item.transform.get_saturation() - 1.0) > epsilon ||\r
+                  std::abs(item.transform.get_contrast() - 1.0) > epsilon)\r
+               {\r
+                       shader_->set("csb",     true);  \r
+                       \r
+                       shader_->set("brt", item.transform.get_brightness());   \r
+                       shader_->set("sat", item.transform.get_saturation());\r
+                       shader_->set("con", item.transform.get_contrast());\r
+               }\r
+               else\r
+                       shader_->set("csb",     false); \r
+               \r
+               // Setup interlacing\r
 \r
-       ~shader_program()\r
-       {\r
-               glDeleteProgram(program_);\r
-       }\r
+               if(item.mode == core::video_mode::progressive)                  \r
+                       ogl.disable(GL_POLYGON_STIPPLE);                        \r
+               else                    \r
+               {\r
+                       ogl.enable(GL_POLYGON_STIPPLE);\r
 \r
-       void use()\r
-       {       \r
-               GL(glUseProgramObjectARB(program_));            \r
-       }\r
-};\r
+                       if(item.mode == core::video_mode::upper)\r
+                               ogl.stipple_pattern(upper_pattern);\r
+                       else if(item.mode == core::video_mode::lower)\r
+                               ogl.stipple_pattern(lower_pattern);\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
-       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
+               // Setup drawing area\r
                \r
-GLubyte lower_pattern[] = {\r
-       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
-       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
-       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
-       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
+               ogl.viewport(0, 0, background->width(), background->height());\r
 \r
-struct image_kernel::implementation : boost::noncopyable\r
-{      \r
-       std::unordered_map<core::pixel_format::type, shader_program> shaders_;\r
+               GL(glColor4d(item.transform.get_gain(), item.transform.get_gain(), item.transform.get_gain(), item.transform.get_is_key() ? 1.0 : item.transform.get_opacity()));\r
+                                               \r
+               auto m_p = item.transform.get_clip_translation();\r
+               auto m_s = item.transform.get_clip_scale();\r
 \r
-public:\r
-       std::unordered_map<core::pixel_format::type, shader_program>& shaders()\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
+               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(shaders_.empty())\r
+               if(scissor)\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
-                       \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
-               \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
+                       double w = static_cast<double>(background->width());\r
+                       double h = static_cast<double>(background->height());\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.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
+               auto f_p = item.transform.get_fill_translation();\r
+               auto f_s = item.transform.get_fill_scale();\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
+               // 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(*background);\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
+               // Draw\r
+\r
+               glBegin(GL_QUADS);\r
+                       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
+                       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
+                       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
+                       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
+               glEnd();\r
+\r
+               // Cleanup\r
+\r
+               ogl.disable(GL_SCISSOR_TEST);   \r
+                               \r
+               item.textures.clear();\r
+               ogl.yield(); // Return resources to pool as early as possible.\r
+\r
+               if(advanced_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
 \r
 image_kernel::image_kernel() : impl_(new implementation()){}\r
+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
+{\r
+       impl_->draw(ogl, std::move(item), background, local_key, layer_key);\r
+}\r
 \r
-void image_kernel::apply(const core::pixel_format_desc& pix_desc, const core::image_transform& transform, bool has_separate_key)\r
+bool operator==(const render_item& lhs, const render_item& rhs)\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
+       return lhs.textures == rhs.textures && lhs.transform == rhs.transform && lhs.tag == rhs.tag && lhs.mode == rhs.mode;\r
 }\r
 \r
 }}
\ No newline at end of file