]> git.sesse.net Git - casparcg/blobdiff - core/mixer/image/image_kernel.cpp
2.0. - blend_modes are re-enabled.
[casparcg] / core / mixer / image / image_kernel.cpp
index b550ef17427c7479961e7a3dd6de6292c556eb44..2777bb3c4546465af7421cf19d30bfed071a7b42 100644 (file)
 #include "../../stdafx.h"\r
 \r
 #include "image_kernel.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 <GL/glew.h>\r
+\r
 #include <boost/noncopyable.hpp>\r
 \r
+#include <tbb/mutex.h>\r
+\r
 #include <unordered_map>\r
 \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
 struct image_kernel::implementation : boost::noncopyable\r
 {      \r
        std::unique_ptr<shader> shader_;\r
-       \r
-       implementation()\r
+       bool                                    blend_modes_;\r
+                       \r
+       implementation() : blend_modes_(true)\r
        {\r
-               std::string vertex = \r
-                       "void main()                                                                                                                                            \n"\r
-                       "{                                                                                                                                                                      \n"\r
-                       "       gl_TexCoord[0] = gl_MultiTexCoord0;                                                                                             \n"\r
-                       "       gl_TexCoord[1] = gl_MultiTexCoord1;                                                                                             \n"\r
-                       "       gl_FrontColor  = gl_Color;                                                                                                              \n"\r
-                       "       gl_Position    = ftransform();                                                                                                  \n"\r
-                       "}                                                                                                                                                                      \n";\r
-\r
-               std::string fragment = std::string() +\r
-                       "uniform sampler2D      background;                                                                                                             \n"\r
-                       "uniform sampler2D      plane[4];                                                                                                               \n"\r
-                       "uniform sampler2D      local_key;                                                                                                              \n"\r
-                       "uniform sampler2D      layer_key;                                                                                                              \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "uniform bool           is_hd;                                                                                                                  \n"\r
-                       "uniform bool           has_local_key;                                                                                                  \n"\r
-                       "uniform bool           has_layer_key;                                                                                                  \n"\r
-                       "uniform int            blend_mode;                                                                                                             \n"\r
-                       "uniform int            alpha_mode;                                                                                                             \n"\r
-                       "uniform int            interlace_mode;                                                                                                 \n"\r
-                       "uniform int            pixel_format;                                                                                                   \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "uniform float          gain;                                                                                                                   \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "uniform bool           levels;                                                                                                                 \n"\r
-                       "uniform float          min_input;                                                                                                              \n"\r
-                       "uniform float          max_input;                                                                                                              \n"\r
-                       "uniform float          gamma;                                                                                                                  \n"\r
-                       "uniform float          min_output;                                                                                                             \n"\r
-                       "uniform float          max_output;                                                                                                             \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "uniform bool           csb;                                                                                                                    \n"\r
-                       "uniform float          brt;                                                                                                                    \n"\r
-                       "uniform float          sat;                                                                                                                    \n"\r
-                       "uniform float          con;                                                                                                                    \n"\r
-                       "                                                                                                                                                                       \n"\r
-\r
-                       +\r
-\r
-                       get_blend_glsl()\r
+       }\r
+\r
+       std::string get_blend_color_func()\r
+       {\r
+               return \r
+                       \r
+               get_adjustement_glsl()\r
+               \r
+               +\r
+\r
+               get_blend_glsl()\r
+               \r
+               +\r
                        \r
-                       +\r
-\r
-                       "vec3 get_blend_color(vec3 back, vec3 fore)                                                                                     \n"\r
-                       "{                                                                                                                                                                      \n"\r
-                       "       switch(blend_mode)                                                                                                                              \n"\r
-                       "       {                                                                                                                                                               \n"\r
-                       "       case  0: return BlendNormal(back, fore);                                                                                \n"\r
-                       "       case  1: return BlendLighten(back, fore);                                                                               \n"\r
-                       "       case  2: return BlendDarken(back, fore);                                                                                \n"\r
-                       "       case  3: return BlendMultiply(back, fore);                                                                              \n"\r
-                       "       case  4: return BlendAverage(back, fore);                                                                               \n"\r
-                       "       case  5: return BlendAdd(back, fore);                                                                                   \n"\r
-                       "       case  6: return BlendSubstract(back, fore);                                                                             \n"\r
-                       "       case  7: return BlendDifference(back, fore);                                                                    \n"\r
-                       "       case  8: return BlendNegation(back, fore);                                                                              \n"\r
-                       "       case  9: return BlendExclusion(back, fore);                                                                             \n"\r
-                       "       case 10: return BlendScreen(back, fore);                                                                                \n"\r
-                       "       case 11: return BlendOverlay(back, fore);                                                                               \n"\r
-                       //"     case 12: return BlendSoftLight(back, fore);                                                                             \n"\r
-                       "       case 13: return BlendHardLight(back, fore);                                                                             \n"\r
-                       "       case 14: return BlendColorDodge(back, fore);                                                                    \n"\r
-                       "       case 15: return BlendColorBurn(back, fore);                                                                             \n"\r
-                       "       case 16: return BlendLinearDodge(back, fore);                                                                   \n"\r
-                       "       case 17: return BlendLinearBurn(back, fore);                                                                    \n"\r
-                       "       case 18: return BlendLinearLight(back, fore);                                                                   \n"\r
-                       "       case 19: return BlendVividLight(back, fore);                                                                    \n"\r
-                       "       case 20: return BlendPinLight(back, fore);                                                                              \n"\r
-                       "       case 21: return BlendHardMix(back, fore);                                                                               \n"\r
-                       "       case 22: return BlendReflect(back, fore);                                                                               \n"\r
-                       "       case 23: return BlendGlow(back, fore);                                                                                  \n"\r
-                       "       case 24: return BlendPhoenix(back, fore);                                                                               \n"\r
-                       "       case 25: return BlendHue(back, fore);                                                                                   \n"\r
-                       "       case 26: return BlendSaturation(back, fore);                                                                    \n"\r
-                       "       case 27: return BlendColor(back, fore);                                                                                 \n"\r
-                       "       case 28: return BlendLuminosity(back, fore);                                                                    \n"\r
-                       "       }                                                                                                                                                               \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "       return BlendNormal(back, fore);                                                                                                 \n"\r
-                       "}                                                                                                                                                                      \n"\r
-                       "                                                                                                                                                                       \n"                                                                                                                                                       \r
-                       "vec4 blend_color(vec4 fore)                                                                                                            \n"\r
-                       "{                                                                                                                                                                      \n"\r
-                       "   vec4 back = texture2D(background, gl_TexCoord[1].st);                                                       \n"\r
-                       "   if(levels)                                                                                                                                          \n"\r
-                       "               fore.rgb = LevelsControl(fore.rgb, min_input, max_input, gamma, min_output, max_output); \n"\r
-                       "       if(csb)                                                                                                                                                 \n"\r
-                       "               fore.rgb = ContrastSaturationBrightness(fore.rgb, brt, sat, con);                       \n"\r
-                       "   fore.rgb = get_blend_color(back.bgr, fore.rgb);                                                                     \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "       return vec4(mix(back.rgb, fore.rgb, fore.a), back.a + fore.a);                                  \n"\r
-                       "}                                                                                                                                                                      \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "// NOTE: YCbCr, ITU-R, http://www.intersil.com/data/an/an9717.pdf                                      \n"\r
-                       "// TODO: Support for more yuv formats might be needed.                                                         \n"\r
-                       "vec4 ycbcra_to_rgba_sd(float y, float cb, float cr, float a)                                           \n"\r
-                       "{                                                                                                                                                                      \n"\r
-                       "       cb -= 0.5;                                                                                                                                              \n"\r
-                       "       cr -= 0.5;                                                                                                                                              \n"\r
-                       "       y = 1.164*(y-0.0625);                                                                                                                   \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "       vec4 color;                                                                                                                                             \n"\r
-                       "       color.r = y + 2.018 * cb;                                                                                                               \n"\r
-                       "       color.b = y + 1.596 * cr;                                                                                                               \n"\r
-                       "       color.g = y - 0.813 * cr - 0.391 * cb;                                                                                  \n"\r
-                       "       color.a = a;                                                                                                                                    \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "       return color;                                                                                                                                   \n"\r
-                       "}                                                                                                                                                                      \n"                     \r
-                       "                                                                                                                                                                       \n"\r
-                       "vec4 ycbcra_to_rgba_hd(float y, float cb, float cr, float a)                                           \n"\r
-                       "{                                                                                                                                                                      \n"\r
-                       "       cb -= 0.5;                                                                                                                                              \n"\r
-                       "       cr -= 0.5;                                                                                                                                              \n"\r
-                       "       y = 1.164*(y-0.0625);                                                                                                                   \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "       vec4 color;                                                                                                                                             \n"\r
-                       "       color.r = y + 2.115 * cb;                                                                                                               \n"\r
-                       "       color.b = y + 1.793 * cr;                                                                                                               \n"\r
-                       "       color.g = y - 0.534 * cr - 0.213 * cb;                                                                                  \n"\r
-                       "       color.a = a;                                                                                                                                    \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "       return color;                                                                                                                                   \n"\r
-                       "}                                                                                                                                                                      \n"                     \r
-                       "                                                                                                                                                                       \n"                     \r
-                       "vec4 ycbcra_to_rgba(float y, float cb, float cr, float a)                                                      \n"\r
-                       "{                                                                                                                                                                      \n"\r
-                       "       if(is_hd)                                                                                                                                               \n"\r
-                       "               return ycbcra_to_rgba_hd(y, cb, cr, a);                                                                         \n"\r
-                       "       else                                                                                                                                                    \n"\r
-                       "               return ycbcra_to_rgba_sd(y, cb, cr, a);                                                                         \n"\r
-                       "}                                                                                                                                                                      \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "vec4 get_rgba_color()                                                                                                                          \n"\r
-                       "{                                                                                                                                                                      \n"\r
-                       "       switch(pixel_format)                                                                                                                    \n"\r
-                       "       {                                                                                                                                                               \n"\r
-                       "       case 0:         //gray                                                                                                                          \n"\r
-                       "               return vec4(texture2D(plane[0], gl_TexCoord[0].st).rrr, 1.0);                           \n"\r
-                       "       case 1:         //bgra,                                                                                                                         \n"\r
-                       "               return texture2D(plane[0], gl_TexCoord[0].st).bgra;                                                     \n"\r
-                       "       case 2:         //rgba,                                                                                                                         \n"\r
-                       "               return texture2D(plane[0], gl_TexCoord[0].st).rgba;                                                     \n"\r
-                       "       case 3:         //argb,                                                                                                                         \n"\r
-                       "               return texture2D(plane[0], gl_TexCoord[0].st).argb;                                                     \n"\r
-                       "       case 4:         //abgr,                                                                                                                         \n"\r
-                       "               return texture2D(plane[0], gl_TexCoord[0].st).gbar;                                                     \n"\r
-                       "       case 5:         //ycbcr,                                                                                                                        \n"\r
-                       "               {                                                                                                                                                       \n"\r
-                       "                       float y  = texture2D(plane[0], gl_TexCoord[0].st).r;                                    \n"\r
-                       "                       float cb = texture2D(plane[1], gl_TexCoord[0].st).r;                                    \n"\r
-                       "                       float cr = texture2D(plane[2], gl_TexCoord[0].st).r;                                    \n"\r
-                       "                       return ycbcra_to_rgba(y, cb, cr, 1.0);                                                                  \n"\r
-                       "               }                                                                                                                                                       \n"\r
-                       "       case 6:         //ycbcra                                                                                                                        \n"\r
-                       "               {                                                                                                                                                       \n"\r
-                       "                       float y  = texture2D(plane[0], gl_TexCoord[0].st).r;                                    \n"\r
-                       "                       float cb = texture2D(plane[1], gl_TexCoord[0].st).r;                                    \n"\r
-                       "                       float cr = texture2D(plane[2], gl_TexCoord[0].st).r;                                    \n"\r
-                       "                       float a  = texture2D(plane[3], gl_TexCoord[0].st).r;                                    \n"\r
-                       "                       return ycbcra_to_rgba(y, cb, cr, a);                                                                    \n"\r
-                       "               }                                                                                                                                                       \n"\r
-                       "       }                                                                                                                                                               \n"\r
-                       "       return vec4(0.0, 0.0, 0.0, 0.0);                                                                                                \n"\r
-                       "}                                                                                                                                                                      \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "void main()                                                                                                                                            \n"\r
-                       "{                                                                                                                                                                      \n"\r
-                       "   switch(interlace_mode)                                                                                                                      \n"\r
-                       "       {                                                                                                                                                               \n"\r
-                       "       case 1: // lower                                                                                                                                \n"\r
-                       "               {                                                                                                                                                       \n"\r
-                       "                       bool odd = mod(floor(gl_FragCoord.y), 2.0) > 0.5;                                               \n"\r
-                       "                       if(!odd)                                                                                                                                \n"\r
-                       "                               discard;                                                                                                                        \n"\r
-                       "                       break;                                                                                                                                  \n"\r
-                       "               }                                                                                                                                                       \n"\r
-                       "       case 2: //upper                                                                                                                                 \n"\r
-                       "               {                                                                                                                                                       \n"\r
-                       "                       bool odd = mod(floor(gl_FragCoord.y), 2.0) > 0.5;                                               \n"\r
-                       "                       if(odd)                                                                                                                                 \n"\r
-                       "                               discard;                                                                                                                        \n"\r
-                       "                       break;                                                                                                                                  \n"\r
-                       "               }                                                                                                                                                       \n"\r
-                       "       }                                                                                                                                                               \n"\r
-                       "                                                                                                                                                                       \n"\r
-                       "       vec4 color = get_rgba_color();                                                                                                  \n"\r
-                       "       if(has_local_key)                                                                                                                               \n"\r
-                       "               color.a *= texture2D(local_key, gl_TexCoord[1].st).r;                                           \n"\r
-                       "       if(has_layer_key)                                                                                                                               \n"\r
-                       "               color.a *= texture2D(layer_key, gl_TexCoord[1].st).r;                                           \n"\r
-                       "       gl_FragColor = blend_color(color.bgra * gl_Color * gain);                                               \n"\r
-                       "}                                                                                                                                                                      \n"\r
-                       ;\r
-\r
-                       shader_.reset(new shader(vertex, fragment));\r
+               "vec3 get_blend_color(vec3 back, vec3 fore)                                                                                     \n"\r
+               "{                                                                                                                                                                      \n"\r
+               "       switch(blend_mode)                                                                                                                              \n"\r
+               "       {                                                                                                                                                               \n"\r
+               "       case  0: return BlendNormal(back, fore);                                                                                \n"\r
+               "       case  1: return BlendLighten(back, fore);                                                                               \n"\r
+               "       case  2: return BlendDarken(back, fore);                                                                                \n"\r
+               "       case  3: return BlendMultiply(back, fore);                                                                              \n"\r
+               "       case  4: return BlendAverage(back, fore);                                                                               \n"\r
+               "       case  5: return BlendAdd(back, fore);                                                                                   \n"\r
+               "       case  6: return BlendSubstract(back, fore);                                                                             \n"\r
+               "       case  7: return BlendDifference(back, fore);                                                                    \n"\r
+               "       case  8: return BlendNegation(back, fore);                                                                              \n"\r
+               "       case  9: return BlendExclusion(back, fore);                                                                             \n"\r
+               "       case 10: return BlendScreen(back, fore);                                                                                \n"\r
+               "       case 11: return BlendOverlay(back, fore);                                                                               \n"\r
+               //"     case 12: return BlendSoftLight(back, fore);                                                                             \n"\r
+               "       case 13: return BlendHardLight(back, fore);                                                                             \n"\r
+               "       case 14: return BlendColorDodge(back, fore);                                                                    \n"\r
+               "       case 15: return BlendColorBurn(back, fore);                                                                             \n"\r
+               "       case 16: return BlendLinearDodge(back, fore);                                                                   \n"\r
+               "       case 17: return BlendLinearBurn(back, fore);                                                                    \n"\r
+               "       case 18: return BlendLinearLight(back, fore);                                                                   \n"\r
+               "       case 19: return BlendVividLight(back, fore);                                                                    \n"\r
+               "       case 20: return BlendPinLight(back, fore);                                                                              \n"\r
+               "       case 21: return BlendHardMix(back, fore);                                                                               \n"\r
+               "       case 22: return BlendReflect(back, fore);                                                                               \n"\r
+               "       case 23: return BlendGlow(back, fore);                                                                                  \n"\r
+               "       case 24: return BlendPhoenix(back, fore);                                                                               \n"\r
+               "       case 25: return BlendHue(back, fore);                                                                                   \n"\r
+               "       case 26: return BlendSaturation(back, fore);                                                                    \n"\r
+               "       case 27: return BlendColor(back, fore);                                                                                 \n"\r
+               "       case 28: return BlendLuminosity(back, fore);                                                                    \n"\r
+               "       }                                                                                                                                                               \n"\r
+               "       return BlendNormal(back, fore);                                                                                                 \n"\r
+               "}                                                                                                                                                                      \n"\r
+               "                                                                                                                                                                       \n"                                                                                                                                                       \r
+               "vec4 blend(vec4 fore)                                                                                                                          \n"\r
+               "{                                                                                                                                                                      \n"\r
+               "   vec4 back = texture2D(background, gl_TexCoord[1].st);                                                       \n"\r
+               "       fore.rgb = get_blend_color(back.rgb, fore.rgb);                                                                 \n"\r
+               "       return vec4(mix(back.bgr, fore.rgb, fore.a), back.a + fore.a);                                  \n"\r
+               "}                                                                                                                                                                      \n";\r
        }\r
+               \r
+       std::string get_simple_blend_color_func()\r
+       {\r
+               return  \r
+               \r
+               get_adjustement_glsl()\r
+                       \r
+               +\r
+\r
+               "vec4 blend(vec4 fore)                                                                                                                          \n"\r
+               "{                                                                                                                                                                      \n"\r
+               "       return fore;                                                                                                                                    \n"\r
+               "}                                                                                                                                                                      \n";\r
+       }\r
+\r
+       std::string get_vertex()\r
+       {\r
+               return \r
+\r
+               "void main()                                                                                                                                            \n"\r
+               "{                                                                                                                                                                      \n"\r
+               "       gl_TexCoord[0] = gl_MultiTexCoord0;                                                                                             \n"\r
+               "       gl_TexCoord[1] = gl_MultiTexCoord1;                                                                                             \n"\r
+               "       gl_FrontColor  = gl_Color;                                                                                                              \n"\r
+               "       gl_Position    = ftransform();                                                                                                  \n"\r
+               "}                                                                                                                                                                      \n";\r
+       }\r
+\r
+       std::string get_fragment()\r
+       {\r
+               return\r
+\r
+               "#version 120                                                                                                                                           \n"\r
+               "uniform sampler2D      background;                                                                                                             \n"\r
+               "uniform sampler2D      plane[4];                                                                                                               \n"\r
+               "uniform sampler2D      local_key;                                                                                                              \n"\r
+               "uniform sampler2D      layer_key;                                                                                                              \n"\r
+               "                                                                                                                                                                       \n"\r
+               "uniform bool           is_hd;                                                                                                                  \n"\r
+               "uniform bool           has_local_key;                                                                                                  \n"\r
+               "uniform bool           has_layer_key;                                                                                                  \n"\r
+               "uniform int            blend_mode;                                                                                                             \n"\r
+               "uniform int            alpha_mode;                                                                                                             \n"\r
+               "uniform int            pixel_format;                                                                                                   \n"\r
+               "                                                                                                                                                                       \n"\r
+               "uniform bool           levels;                                                                                                                 \n"\r
+               "uniform float          min_input;                                                                                                              \n"\r
+               "uniform float          max_input;                                                                                                              \n"\r
+               "uniform float          gamma;                                                                                                                  \n"\r
+               "uniform float          min_output;                                                                                                             \n"\r
+               "uniform float          max_output;                                                                                                             \n"\r
+               "                                                                                                                                                                       \n"\r
+               "uniform bool           csb;                                                                                                                    \n"\r
+               "uniform float          brt;                                                                                                                    \n"\r
+               "uniform float          sat;                                                                                                                    \n"\r
+               "uniform float          con;                                                                                                                    \n"\r
+               "                                                                                                                                                                       \n"     \r
+\r
+               +\r
+               \r
+               (blend_modes_ ? get_blend_color_func() : get_simple_blend_color_func())\r
 \r
+               +\r
        \r
-       void draw(size_t                                                                                width, \r
-                         size_t                                                                                height, \r
-                         const core::pixel_format_desc&                                pix_desc, \r
-                         const core::image_transform&                                  transform, \r
-                         const std::vector<safe_ptr<device_buffer>>&   planes, \r
+               "                                                                                                                                                                       \n"\r
+               "//http://slouken.blogspot.com/2011/02/mpeg-acceleration-with-glsl.html                         \n"\r
+               "vec4 ycbcra_to_rgba_sd(float y, float cb, float cr, float a)                                           \n"\r
+               "{                                                                                                                                                                      \n"\r
+               "       // YUV offset                                                                                                                                   \n"\r
+               "       const vec3 offset = vec3(-0.0625, -0.5, -0.5);                                                                  \n"\r
+               "                                                                                                                                                                       \n"\r
+               "       // RGB coefficients                                                                                                                     \n"\r
+               "       const vec3 Rcoeff = vec3(1.164,  0.000,  1.596);                                                                \n"\r
+               "       const vec3 Gcoeff = vec3(1.164, -0.391, -0.813);                                                                \n"\r
+               "       const vec3 Bcoeff = vec3(1.164,  2.018,  0.000);                                                                \n"\r
+               "                                                                                                                                                                       \n"\r
+               "       vec3 yuv = vec3(y, cr, cb);                                                                                                             \n"\r
+               "   vec4 rgba;                                                                                                                                          \n"\r
+               "                                                                                                                                                                       \n"\r
+               "       yuv += offset;                                                                                                                                  \n"\r
+               "       rgba.r = dot(yuv, Rcoeff);                                                                                                              \n"\r
+               "       rgba.g = dot(yuv, Gcoeff);                                                                                                              \n"\r
+               "       rgba.b = dot(yuv, Bcoeff);                                                                                                              \n"\r
+               "       rgba.a = a;                                                                                                                                             \n"\r
+               "                                                                                                                                                                       \n"\r
+               "       return rgba;                                                                                                                                    \n"\r
+               "}                                                                                                                                                                      \n"                     \r
+               "                                                                                                                                                                       \n"\r
+               "vec4 ycbcra_to_rgba_hd(float y, float cb, float cr, float a)                                           \n"\r
+               "{                                                                                                                                                                      \n"\r
+               "       // YUV offset                                                                                                                                   \n"\r
+               "       const vec3 offset = vec3(-0.0625, -0.5, -0.5);                                                                  \n"\r
+               "                                                                                                                                                                       \n"\r
+               "       // RGB coefficients                                                                                                                     \n"\r
+               "       const vec3 Rcoeff = vec3(1.164,  0.000,  1.793);                                                                \n"\r
+               "       const vec3 Gcoeff = vec3(1.164, -0.213, -0.534);                                                                \n"\r
+               "       const vec3 Bcoeff = vec3(1.164,  2.115,  0.000);                                                                \n"\r
+               "                                                                                                                                                                       \n"\r
+               "       vec3 yuv = vec3(y, cr, cb);                                                                                                             \n"\r
+               "   vec4 rgba;                                                                                                                                          \n"\r
+               "                                                                                                                                                                       \n"\r
+               "       yuv += offset;                                                                                                                                  \n"\r
+               "       rgba.r = dot(yuv, Rcoeff);                                                                                                              \n"\r
+               "       rgba.g = dot(yuv, Gcoeff);                                                                                                              \n"\r
+               "       rgba.b = dot(yuv, Bcoeff);                                                                                                              \n"\r
+               "       rgba.a = a;                                                                                                                                             \n"\r
+               "                                                                                                                                                                       \n"\r
+               "       return rgba;                                                                                                                                    \n"\r
+               "}                                                                                                                                                                      \n"                     \r
+               "                                                                                                                                                                       \n"             \r
+               "vec4 ycbcra_to_rgba(float y, float cb, float cr, float a)                                                      \n"\r
+               "{                                                                                                                                                                      \n"\r
+               "       if(is_hd)                                                                                                                                               \n"\r
+               "               return ycbcra_to_rgba_hd(y, cb, cr, a);                                                                         \n"\r
+               "       else                                                                                                                                                    \n"\r
+               "               return ycbcra_to_rgba_sd(y, cb, cr, a);                                                                         \n"\r
+               "}                                                                                                                                                                      \n"\r
+               "                                                                                                                                                                       \n"\r
+               "vec4 get_rgba_color()                                                                                                                          \n"\r
+               "{                                                                                                                                                                      \n"\r
+               "       switch(pixel_format)                                                                                                                    \n"\r
+               "       {                                                                                                                                                               \n"\r
+               "       case 0:         //gray                                                                                                                          \n"\r
+               "               return vec4(texture2D(plane[0], gl_TexCoord[0].st).rrr, 1.0);                           \n"\r
+               "       case 1:         //bgra,                                                                                                                         \n"\r
+               "               return texture2D(plane[0], gl_TexCoord[0].st).bgra;                                                     \n"\r
+               "       case 2:         //rgba,                                                                                                                         \n"\r
+               "               return texture2D(plane[0], gl_TexCoord[0].st).rgba;                                                     \n"\r
+               "       case 3:         //argb,                                                                                                                         \n"\r
+               "               return texture2D(plane[0], gl_TexCoord[0].st).argb;                                                     \n"\r
+               "       case 4:         //abgr,                                                                                                                         \n"\r
+               "               return texture2D(plane[0], gl_TexCoord[0].st).gbar;                                                     \n"\r
+               "       case 5:         //ycbcr,                                                                                                                        \n"\r
+               "               {                                                                                                                                                       \n"\r
+               "                       float y  = texture2D(plane[0], gl_TexCoord[0].st).r;                                    \n"\r
+               "                       float cb = texture2D(plane[1], gl_TexCoord[0].st).r;                                    \n"\r
+               "                       float cr = texture2D(plane[2], gl_TexCoord[0].st).r;                                    \n"\r
+               "                       return ycbcra_to_rgba(y, cb, cr, 1.0);                                                                  \n"\r
+               "               }                                                                                                                                                       \n"\r
+               "       case 6:         //ycbcra                                                                                                                        \n"\r
+               "               {                                                                                                                                                       \n"\r
+               "                       float y  = texture2D(plane[0], gl_TexCoord[0].st).r;                                    \n"\r
+               "                       float cb = texture2D(plane[1], gl_TexCoord[0].st).r;                                    \n"\r
+               "                       float cr = texture2D(plane[2], gl_TexCoord[0].st).r;                                    \n"\r
+               "                       float a  = texture2D(plane[3], gl_TexCoord[0].st).r;                                    \n"\r
+               "                       return ycbcra_to_rgba(y, cb, cr, a);                                                                    \n"\r
+               "               }                                                                                                                                                       \n"\r
+               "       case 7:         //luma                                                                                                                          \n"\r
+               "               {                                                                                                                                                       \n"\r
+               "                       vec3 y3 = texture2D(plane[0], gl_TexCoord[0].st).rrr;                                   \n"\r
+               "                       return vec4((y3-0.065)/0.859, 1.0);                                                                             \n"\r
+               "               }                                                                                                                                                       \n"\r
+               "       }                                                                                                                                                               \n"\r
+               "       return vec4(0.0, 0.0, 0.0, 0.0);                                                                                                \n"\r
+               "}                                                                                                                                                                      \n"\r
+               "                                                                                                                                                                       \n"\r
+               "void main()                                                                                                                                            \n"\r
+               "{                                                                                                                                                                      \n"\r
+               "       vec4 color = get_rgba_color();                                                                                                  \n"\r
+               "   if(levels)                                                                                                                                          \n"\r
+               "               color.rgb = LevelsControl(color.rgb, min_input, max_input, gamma, min_output, max_output); \n"\r
+               "       if(csb)                                                                                                                                                 \n"\r
+               "               color.rgb = ContrastSaturationBrightness(color.rgb, brt, sat, con);                     \n"\r
+               "       if(has_local_key)                                                                                                                               \n"\r
+               "               color.a *= texture2D(local_key, gl_TexCoord[1].st).r;                                           \n"\r
+               "       if(has_layer_key)                                                                                                                               \n"\r
+               "               color.a *= texture2D(layer_key, gl_TexCoord[1].st).r;                                           \n"\r
+               "   color *= gl_Color;                                                                                                                          \n"\r
+               "       color = blend(color);                                                                                                                   \n"\r
+               "       gl_FragColor = color.bgra;                                                                                                              \n"\r
+               "}                                                                                                                                                                      \n";\r
+       }\r
+\r
+       void init_shader(ogl_device& ogl)\r
+       {\r
+               try\r
+               {                               \r
+                       blend_modes_  = glTextureBarrierNV ? env::properties().get("configuration.mixers.blend-modes", false) : false;\r
+                       shader_.reset(new shader(get_vertex(), get_fragment()));\r
+               }\r
+               catch(...)\r
+               {\r
+                       CASPAR_LOG_CURRENT_EXCEPTION();\r
+                       CASPAR_LOG(warning) << "Failed to compile shader. Trying to compile without blend-modes.";\r
+                               \r
+                       blend_modes_ = false;\r
+                       shader_.reset(new shader(get_vertex(), get_fragment()));\r
+               }\r
+                                               \r
+               ogl.enable(GL_TEXTURE_2D);\r
+\r
+               if(!blend_modes_)\r
+               {\r
+                       ogl.enable(GL_BLEND);\r
+                       GL(glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE));\r
+                       CASPAR_LOG(info) << L"[shader] Blend-modes are disabled.";\r
+               }\r
+       }\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
-               GL(glEnable(GL_TEXTURE_2D));\r
+               static const double epsilon = 0.001;\r
+\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(!shader_)\r
+                       init_shader(ogl);\r
                        \r
-               // Setup depth for interlacing and explicit z-culling\r
-               double z = 0.0;\r
-               if(transform.get_mode() == core::video_mode::upper)\r
-               {       \r
-                       GL(glEnable(GL_DEPTH_TEST));    \r
-                       z = 1.0;\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
+                       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
-               else if(transform.get_mode() == core::video_mode::lower)\r
-                       GL(glClear(GL_DEPTH_BUFFER_BIT));               \r
 \r
                // Bind textures\r
 \r
-               for(size_t n = 0; n < planes.size(); ++n)\r
-                       planes[n]->bind(n);\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(4);\r
                \r
                if(layer_key)\r
                        layer_key->bind(5);\r
-\r
-               background->bind(6);\r
-\r
+                               \r
                // Setup shader\r
 \r
-               shader_->use(); \r
+               ogl.use(*shader_);\r
 \r
                shader_->set("plane[0]",                0);\r
                shader_->set("plane[1]",                1);\r
@@ -287,23 +372,29 @@ struct image_kernel::implementation : boost::noncopyable
                shader_->set("plane[3]",                3);\r
                shader_->set("local_key",               4);\r
                shader_->set("layer_key",               5);\r
-               shader_->set("background",              6);\r
-               shader_->set("gain",                    transform.get_gain());\r
-               shader_->set("is_hd",                   pix_desc.planes.at(0).height > 700 ? 1 : 0);\r
+               shader_->set("is_hd",                   item.pix_desc.planes.at(0).height > 700 ? 1 : 0);\r
                shader_->set("has_local_key",   local_key ? 1 : 0);\r
                shader_->set("has_layer_key",   layer_key ? 1 : 0);\r
-               shader_->set("blend_mode",              transform.get_blend_mode());\r
-               shader_->set("alpha_mode",              transform.get_alpha_mode());\r
-               shader_->set("interlace_mode",  transform.get_mode());\r
-               shader_->set("pixel_format",    pix_desc.pix_fmt);      \r
-\r
-               auto levels = transform.get_levels();\r
-\r
-               if(levels.min_input  > 0.001 ||\r
-                  levels.max_input  < 0.999 ||\r
-                  levels.min_output > 0.001 ||\r
-                  levels.max_output < 0.999 ||\r
-                  std::abs(levels.gamma - 1.0) > 0.001)\r
+               shader_->set("pixel_format",    item.pix_desc.pix_fmt); \r
+               \r
+               // Setup blend_func\r
+               \r
+               if(blend_modes_)\r
+               {\r
+                       background->bind(6);\r
+\r
+                       shader_->set("background",              6);\r
+                       shader_->set("blend_mode",              item.transform.get_is_key() ? core::image_transform::blend_mode::normal : item.transform.get_blend_mode());\r
+                       shader_->set("alpha_mode",              item.transform.get_alpha_mode());\r
+               }\r
+\r
+               auto levels = item.transform.get_levels();\r
+\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
                        shader_->set("levels", true);   \r
                        shader_->set("min_input", levels.min_input);    \r
@@ -315,55 +406,71 @@ struct image_kernel::implementation : boost::noncopyable
                else\r
                        shader_->set("levels", false);  \r
 \r
-               if(std::abs(transform.get_brightness() - 1.0) > 0.001 ||\r
-                  std::abs(transform.get_saturation() - 1.0) > 0.001 ||\r
-                  std::abs(transform.get_contrast() - 1.0) > 0.001)\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", transform.get_brightness());        \r
-                       shader_->set("sat", transform.get_saturation());\r
-                       shader_->set("con", transform.get_contrast());\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 drawing area\r
+               \r
+               ogl.viewport(0, 0, background->width(), background->height());\r
 \r
-               GL(glColor4d(1.0, 1.0, 1.0, transform.get_opacity()));\r
-               GL(glViewport(0, 0, width, height));\r
+               GL(glColor4d(item.transform.get_gain(), item.transform.get_gain(), item.transform.get_gain(), item.transform.get_opacity()));\r
                                                \r
-               auto m_p = transform.get_clip_translation();\r
-               auto m_s = transform.get_clip_scale();\r
-               double w = static_cast<double>(width);\r
-               double h = static_cast<double>(height);\r
+               auto m_p = item.transform.get_clip_translation();\r
+               auto m_s = item.transform.get_clip_scale();\r
 \r
-               GL(glEnable(GL_SCISSOR_TEST));\r
-               GL(glScissor(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
-               auto f_p = transform.get_fill_translation();\r
-               auto f_s = transform.get_fill_scale();\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>(background->width());\r
+                       double h = static_cast<double>(background->height());\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
                // Draw\r
 \r
                glBegin(GL_QUADS);\r
-                       glMultiTexCoord2d(GL_TEXTURE0, 0.0, 0.0); glMultiTexCoord2d(GL_TEXTURE1,  f_p[0]        ,  f_p[1]        );             glVertex3d( f_p[0]        *2.0-1.0,  f_p[1]        *2.0-1.0, z);\r
-                       glMultiTexCoord2d(GL_TEXTURE0, 1.0, 0.0); glMultiTexCoord2d(GL_TEXTURE1, (f_p[0]+f_s[0]),  f_p[1]        );             glVertex3d((f_p[0]+f_s[0])*2.0-1.0,  f_p[1]        *2.0-1.0, z);\r
-                       glMultiTexCoord2d(GL_TEXTURE0, 1.0, 1.0); glMultiTexCoord2d(GL_TEXTURE1, (f_p[0]+f_s[0]), (f_p[1]+f_s[1]));             glVertex3d((f_p[0]+f_s[0])*2.0-1.0, (f_p[1]+f_s[1])*2.0-1.0, z);\r
-                       glMultiTexCoord2d(GL_TEXTURE0, 0.0, 1.0); glMultiTexCoord2d(GL_TEXTURE1,  f_p[0]        , (f_p[1]+f_s[1]));             glVertex3d( f_p[0]        *2.0-1.0, (f_p[1]+f_s[1])*2.0-1.0, z);\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
-               GL(glDisable(GL_SCISSOR_TEST)); \r
-               GL(glDisable(GL_DEPTH_TEST));   \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(!blend_modes_)\r
+                       glTextureBarrierNV(); // This allows us to use framebuffer both as source and target while blending.\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::draw(size_t width, size_t height, const core::pixel_format_desc& pix_desc, const core::image_transform& transform, const std::vector<safe_ptr<device_buffer>>& planes, \r
-                                                         const safe_ptr<device_buffer>& background, const std::shared_ptr<device_buffer>& local_key, const std::shared_ptr<device_buffer>& layer_key)\r
+bool operator==(const render_item& lhs, const render_item& rhs)\r
 {\r
-       impl_->draw(width, height, pix_desc, transform, planes, background, local_key, layer_key);\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