]> git.sesse.net Git - casparcg/blobdiff - core/mixer/image/image_kernel.cpp
2.0. image_mixer: Another shader optimization. Use mix() instead of manual instructions.
[casparcg] / core / mixer / image / image_kernel.cpp
index 7fa19793ce12867a6db1022febf98af1b419f80c..b550ef17427c7479961e7a3dd6de6292c556eb44 100644 (file)
@@ -20,6 +20,9 @@
 #include "../../stdafx.h"\r
 \r
 #include "image_kernel.h"\r
+#include "blending_glsl.h"\r
+#include "../gpu/shader.h"\r
+#include "../gpu/device_buffer.h"\r
 \r
 #include <common/exception/exceptions.h>\r
 #include <common/gl/gl_check.h>\r
 #include <unordered_map>\r
 \r
 namespace caspar { namespace core {\r
-\r
-class shader_program : boost::noncopyable\r
-{\r
-       GLuint program_;\r
-public:\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
-       {\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
-\r
-               GL(glGetObjectParameterivARB(vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
-               {\r
-                       char info[2048];\r
-                       GL(glGetInfoLogARB(vertex_shader, sizeof(info), 0, info));\r
-                       GL(glDeleteObjectARB(vertex_shader));\r
-                       std::stringstream str;\r
-                       str << "Failed to compile vertex shader:" << std::endl << info << std::endl;\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));\r
-               }\r
-                       \r
-               const char* fragment_source = fragment_source_str.c_str();\r
-                                               \r
-               auto fragmemt_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);\r
-                                       \r
-               GL(glShaderSourceARB(fragmemt_shader, 1, &fragment_source, NULL));\r
-               GL(glCompileShaderARB(fragmemt_shader));\r
-\r
-               GL(glGetObjectParameterivARB(fragmemt_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
-               {\r
-                       char info[2048];\r
-                       GL(glGetInfoLogARB(fragmemt_shader, sizeof(info), 0, info));\r
-                       GL(glDeleteObjectARB(fragmemt_shader));\r
-                       std::stringstream str;\r
-                       str << "Failed to compile fragment shader:" << std::endl << info << std::endl;\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));\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
-\r
-               GL(glGetObjectParameterivARB(program_, GL_OBJECT_LINK_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
-               {\r
-                       char info[2048];\r
-                       GL(glGetInfoLogARB(program_, sizeof(info), 0, info));\r
-                       GL(glDeleteObjectARB(program_));\r
-                       std::stringstream str;\r
-                       str << "Failed to link shader program:" << std::endl << info << std::endl;\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));\r
-               }\r
-               GL(glUseProgramObjectARB(program_));\r
-               glUniform1i(glGetUniformLocation(program_, "plane[0]"), 0);\r
-               glUniform1i(glGetUniformLocation(program_, "plane[1]"), 1);\r
-               glUniform1i(glGetUniformLocation(program_, "plane[2]"), 2);\r
-               glUniform1i(glGetUniformLocation(program_, "plane[3]"), 3);\r
-               glUniform1i(glGetUniformLocation(program_, "plane[4]"), 4);\r
-       }\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
-\r
-       ~shader_program()\r
+struct image_kernel::implementation : boost::noncopyable\r
+{      \r
+       std::unique_ptr<shader> shader_;\r
+       \r
+       implementation()\r
        {\r
-               glDeleteProgram(program_);\r
-       }\r
-\r
-       void use()\r
-       {       \r
-               GL(glUseProgramObjectARB(program_));            \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
+\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
        }\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
-               \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::unordered_map<core::pixel_format::type, shader_program> shaders_;\r
-\r
-public:\r
-       std::unordered_map<core::pixel_format::type, shader_program>& shaders()\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
+                         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_POLYGON_STIPPLE));\r
-               GL(glEnable(GL_BLEND));\r
-               GL(glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE));\r
-\r
-               if(shaders_.empty())\r
-               {\r
-               std::string common_vertex = \r
-                       "void main()                                                                                                                    "\r
-                       "{                                                                                                                                              "\r
-                       "       gl_TexCoord[0] = gl_MultiTexCoord0;                                                                     "\r
-                       "       gl_TexCoord[1] = gl_MultiTexCoord1;                                                                     "\r
-                       "       gl_FrontColor = gl_Color;                                                                                       "\r
-                       "       gl_Position = ftransform();                                                                                     "\r
-                       "}                                                                                                                                              ";\r
-\r
-               std::string common_fragment = \r
-                       "uniform sampler2D      plane[5];                                                                                       "\r
-                       "uniform float          gain;                                                                                           "\r
-                       "uniform bool           HD;                                                                                                     "\r
-                       "uniform bool           local_key;                                                                                      "\r
-                       "uniform bool           layer_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
+               GL(glEnable(GL_TEXTURE_2D));\r
                        \r
-               shaders_[core::pixel_format::gray] = shader_program(common_vertex, common_fragment +\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
+               }\r
+               else if(transform.get_mode() == core::video_mode::lower)\r
+                       GL(glClear(GL_DEPTH_BUFFER_BIT));               \r
 \r
-                       "void main()                                                                                                                    "\r
-                       "{                                                                                                                                              "\r
-                       "       vec4 rgba = vec4(texture2D(plane[0], gl_TexCoord[0].st).rrr, 1.0);      "\r
-                       "       if(local_key)                                                                                                           "\r
-                       "               rgba.a = texture2D(plane[3], gl_TexCoord[1].st).r;                              "\r
-                       "       if(layer_key)                                                                                                           "\r
-                       "               rgba.a *= texture2D(plane[4], gl_TexCoord[1].st).r;                             "\r
-                       "       gl_FragColor = rgba * gain;                                                                                     "\r
-                       "}                                                                                                                                              ");\r
+               // Bind textures\r
 \r
-               shaders_[core::pixel_format::abgr] = shader_program(common_vertex, common_fragment +\r
+               for(size_t n = 0; n < planes.size(); ++n)\r
+                       planes[n]->bind(n);\r
 \r
-                       "void main()                                                                                                                    "\r
-                       "{                                                                                                                                              "\r
-                       "       vec4 abgr = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
-                       "       if(local_key)                                                                                                           "\r
-                       "               abgr.b = texture2D(plane[3], gl_TexCoord[1].st).r;                              "\r
-                       "       if(layer_key)                                                                                                           "\r
-                       "               abgr.b *= texture2D(plane[4], gl_TexCoord[1].st).r;                             "\r
-                       "       gl_FragColor = abgr.argb * gain;                                                                        "\r
-                       "}                                                                                                                                              ");\r
+               if(local_key)\r
+                       local_key->bind(4);\r
                \r
-               shaders_[core::pixel_format::argb]= shader_program(common_vertex, common_fragment +\r
+               if(layer_key)\r
+                       layer_key->bind(5);\r
+\r
+               background->bind(6);\r
+\r
+               // Setup shader\r
+\r
+               shader_->use(); \r
+\r
+               shader_->set("plane[0]",                0);\r
+               shader_->set("plane[1]",                1);\r
+               shader_->set("plane[2]",                2);\r
+               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("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
+               {\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
+               else\r
+                       shader_->set("levels", false);  \r
 \r
-                       "void main()                                                                                                                    "       \r
-                       "{                                                                                                                                              "\r
-                       "       vec4 argb = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
-                       "       if(local_key)                                                                                                           "\r
-                       "               argb.b = texture2D(plane[3], gl_TexCoord[1].st).r;                              "\r
-                       "       if(layer_key)                                                                                                           "\r
-                       "               argb.b *= texture2D(plane[4], gl_TexCoord[1].st).r;                             "\r
-                       "       gl_FragColor = argb.grab * gl_Color * gain;                                                     "\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
+               {\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
+               }\r
+               else\r
+                       shader_->set("csb",     false); \r
                \r
-               shaders_[core::pixel_format::bgra]= shader_program(common_vertex, common_fragment +\r
+               // Setup drawing area\r
 \r
-                       "void main()                                                                                                                    "\r
-                       "{                                                                                                                                              "\r
-                       "       vec4 bgra = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
-                       "       if(local_key)                                                                                                           "\r
-                       "               bgra.a = texture2D(plane[3], gl_TexCoord[1].st).r;                              "\r
-                       "       if(layer_key)                                                                                                           "\r
-                       "               bgra.a *= texture2D(plane[4], gl_TexCoord[1].st).r;                             "\r
-                       "       gl_FragColor = bgra.rgba * gl_Color * gain;                                                     "\r
-                       "}                                                                                                                                              ");\r
-               \r
-               shaders_[core::pixel_format::rgba] = shader_program(common_vertex, common_fragment +\r
+               GL(glColor4d(1.0, 1.0, 1.0, transform.get_opacity()));\r
+               GL(glViewport(0, 0, width, height));\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
 \r
-                       "void main()                                                                                                                    "\r
-                       "{                                                                                                                                              "\r
-                       "       vec4 rgba = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
-                       "       if(local_key)                                                                                                           "\r
-                       "               rgba.a = texture2D(plane[3], gl_TexCoord[1].st).r;                              "\r
-                       "       if(layer_key)                                                                                                           "\r
-                       "               rgba.a *= texture2D(plane[4], gl_TexCoord[1].st).r;                             "\r
-                       "       gl_FragColor = rgba.bgra * gl_Color * gain;                                                     "\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
                \r
-               shaders_[core::pixel_format::ycbcr] = shader_program(common_vertex, common_fragment +\r
+               // Draw\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(local_key)                                                                                                           "\r
-                       "               a = texture2D(plane[3], gl_TexCoord[1].st).r;                                   "\r
-                       "       if(layer_key)                                                                                                           "\r
-                       "               a *= texture2D(plane[4], gl_TexCoord[1].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
-               \r
-               shaders_[core::pixel_format::ycbcra] = shader_program(common_vertex, common_fragment +\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
+               glEnd();\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(local_key)                                                                                                           "\r
-                       "               a = texture2D(plane[3], gl_TexCoord[1].st).r;                                   "\r
-                       "       else                                                                                                                            "\r
-                       "               a  = texture2D(plane[3], gl_TexCoord[0].st).r;                                  "\r
-                       "       if(layer_key)                                                                                                           "\r
-                       "               a *= texture2D(plane[4], gl_TexCoord[1].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
-               }\r
-               return shaders_;\r
+               GL(glDisable(GL_SCISSOR_TEST)); \r
+               GL(glDisable(GL_DEPTH_TEST));   \r
        }\r
 };\r
 \r
 image_kernel::image_kernel() : impl_(new implementation()){}\r
 \r
-void image_kernel::draw(size_t width, size_t height, const core::pixel_format_desc& pix_desc, const core::image_transform& transform, bool local_key, bool layer_key)\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
 {\r
-       GL(glEnable(GL_TEXTURE_2D));\r
-       GL(glDisable(GL_DEPTH_TEST));   \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("local_key"), local_key ? 1 : 0));\r
-       GL(glUniform1i(impl_->shaders()[pix_desc.pix_fmt].get_location("layer_key"), layer_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
-                       \r
-       GL(glColor4d(1.0, 1.0, 1.0, transform.get_opacity()));\r
-       GL(glViewport(0, 0, width, height));\r
-                                               \r
-       auto m_p = transform.get_key_translation();\r
-       auto m_s = transform.get_key_scale();\r
-       double w = static_cast<double>(width);\r
-       double h = static_cast<double>(height);\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
-                       \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
-       GL(glDisable(GL_SCISSOR_TEST)); \r
+       impl_->draw(width, height, pix_desc, transform, planes, background, local_key, layer_key);\r
 }\r
 \r
 }}
\ No newline at end of file