Extract indices array and make some offset constants.
[dcpomatic.git] / src / wx / gl_video_view.cc
index 7c26ae6169780660c66ada2c5017a5b4d15b2ee3..49a30d7334ab67c605c02a5fe58745fc2b16e151 100644 (file)
@@ -115,7 +115,7 @@ GLVideoView::~GLVideoView ()
                _thread.join ();
        } catch (...) {}
 
-       glDeleteTextures (1, &_texture);
+       glDeleteTextures (1, &_video_texture);
 }
 
 void
@@ -189,12 +189,22 @@ static constexpr char fragment_source[] =
 "in vec2 TexCoord;\n"
 "\n"
 "uniform sampler2D texture_sampler;\n"
-"uniform int draw_border;\n"
+/* type = 0: draw border
+ * type = 1: draw XYZ image
+ * type = 2: draw RGB image
+ */
+"uniform int type = 0;\n"
 "uniform vec4 border_colour;\n"
+"uniform mat4 colour_conversion;\n"
 "\n"
 "out vec4 FragColor;\n"
 "\n"
 "vec4 cubic(float x)\n"
+"\n"
+"#define IN_GAMMA 2.2\n"
+"#define OUT_GAMMA 0.384615385\n"       //  1 /  2.6
+"#define DCI_COEFFICIENT 0.91655528\n"  // 48 / 53.37
+"\n"
 "{\n"
 "    float x2 = x * x;\n"
 "    float x3 = x2 * x;\n"
@@ -241,10 +251,23 @@ static constexpr char fragment_source[] =
 "\n"
 "void main()\n"
 "{\n"
-"      if (draw_border == 1) {\n"
-"              FragColor = border_colour;\n"
-"      } else {\n"
-"              FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
+"      switch (type) {\n"
+"              case 0:\n"
+"                      FragColor = border_colour;\n"
+"                      break;\n"
+"              case 1:\n"
+"                      FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
+"                      FragColor.x = pow(FragColor.x, IN_GAMMA) / DCI_COEFFICIENT;\n"
+"                      FragColor.y = pow(FragColor.y, IN_GAMMA) / DCI_COEFFICIENT;\n"
+"                      FragColor.z = pow(FragColor.z, IN_GAMMA) / DCI_COEFFICIENT;\n"
+"                      FragColor = colour_conversion * FragColor;\n"
+"                      FragColor.x = pow(FragColor.x, OUT_GAMMA);\n"
+"                      FragColor.y = pow(FragColor.y, OUT_GAMMA);\n"
+"                      FragColor.z = pow(FragColor.z, OUT_GAMMA);\n"
+"                      break;\n"
+"              case 2:\n"
+"                      FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
+"                      break;\n"
 "      }\n"
 "}\n";
 
@@ -262,6 +285,22 @@ GLVideoView::ensure_context ()
        }
 }
 
+
+/* Offset of video texture triangles in indices */
+static constexpr int indices_video_texture = 0;
+/* Offset of border lines in indices */
+static constexpr int indices_border = 6;
+
+static constexpr unsigned int indices[] = {
+       0, 1, 3, // video texture triangle #1
+       1, 2, 3, // video texture triangle #2
+       4, 5,    // border line #1
+       5, 6,    // border line #2
+       6, 7,    // border line #3
+       7, 4,    // border line #4
+};
+
+
 void
 GLVideoView::setup_shaders ()
 {
@@ -289,15 +328,6 @@ GLVideoView::setup_shaders ()
        get_information (GL_VERSION);
        get_information (GL_SHADING_LANGUAGE_VERSION);
 
-       unsigned int indices[] = {
-               0, 1, 3, // texture triangle #1
-               1, 2, 3, // texture triangle #2
-               4, 5,    // border line #1
-               5, 6,    // border line #2
-               6, 7,    // border line #3
-               7, 4,    // border line #4
-       };
-
        glGenVertexArrays(1, &_vao);
        check_gl_error ("glGenVertexArrays");
        GLuint vbo;
@@ -383,10 +413,23 @@ GLVideoView::setup_shaders ()
 
        glUseProgram (program);
 
-       _draw_border = glGetUniformLocation (program, "draw_border");
+       _fragment_type = glGetUniformLocation (program, "type");
        check_gl_error ("glGetUniformLocation");
        set_border_colour (program);
 
+       auto conversion = dcp::ColourConversion::rec709_to_xyz();
+       boost::numeric::ublas::matrix<double> matrix = conversion.xyz_to_rgb ();
+       GLfloat gl_matrix[] = {
+               static_cast<float>(matrix(0, 0)), static_cast<float>(matrix(0, 1)), static_cast<float>(matrix(0, 2)), 0.0f,
+               static_cast<float>(matrix(1, 0)), static_cast<float>(matrix(1, 1)), static_cast<float>(matrix(1, 2)), 0.0f,
+               static_cast<float>(matrix(2, 0)), static_cast<float>(matrix(2, 1)), static_cast<float>(matrix(2, 2)), 0.0f,
+               0.0f, 0.0f, 0.0f, 1.0f
+               };
+
+       auto colour_conversion = glGetUniformLocation (program, "colour_conversion");
+       check_gl_error ("glGetUniformLocation");
+       glUniformMatrix4fv (colour_conversion, 1, GL_TRUE, gl_matrix);
+
        glLineWidth (2.0f);
 }
 
@@ -421,14 +464,14 @@ GLVideoView::draw (Position<int>, dcp::Size)
        glViewport (0, 0, width, height);
        check_gl_error ("glViewport");
 
-       glBindTexture(GL_TEXTURE_2D, _texture);
+       glBindTexture(GL_TEXTURE_2D, _video_texture);
        glBindVertexArray(_vao);
        check_gl_error ("glBindVertexArray");
-       glUniform1i(_draw_border, 0);
-       glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
+       glUniform1i(_fragment_type, _optimise_for_j2k ? 1 : 2);
+       glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_video_texture));
        if (_viewer->outline_content()) {
-               glUniform1i(_draw_border, 1);
-               glDrawElements (GL_LINES, 8, GL_UNSIGNED_INT, reinterpret_cast<void*>(6 * sizeof(int)));
+               glUniform1i(_fragment_type, 1);
+               glDrawElements (GL_LINES, 8, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_border * sizeof(int)));
                check_gl_error ("glDrawElements");
        }
 
@@ -440,30 +483,35 @@ GLVideoView::draw (Position<int>, dcp::Size)
 
 
 void
-GLVideoView::set_image (shared_ptr<const Image> image)
+GLVideoView::set_image (shared_ptr<const PlayerVideo> pv)
 {
-       if (!image) {
-               _size = optional<dcp::Size>();
-               return;
-       }
-
+       auto image = _optimise_for_j2k ? pv->raw_image() : pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true);
 
-       DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
        DCPOMATIC_ASSERT (!image->aligned());
 
+       /** If _optimise_for_j2k is true we render a XYZ image, doing the colourspace
+        *  conversion, scaling and video range conversion in the GL shader.
+        *  Othewise we render a RGB image without any shader-side processing.
+        */
+
+       /* XXX: video range conversion */
+       /* XXX: subs */
+
        if (image->size() != _size) {
                _have_storage = false;
        }
 
        _size = image->size ();
-       glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
+       glPixelStorei (GL_UNPACK_ALIGNMENT, _optimise_for_j2k ? 2 : 1);
        check_gl_error ("glPixelStorei");
 
+       auto const format = _optimise_for_j2k ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
+
        if (_have_storage) {
-               glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
+               glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, format, image->data()[0]);
                check_gl_error ("glTexSubImage2D");
        } else {
-               glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
+               glTexImage2D (GL_TEXTURE_2D, 0, _optimise_for_j2k ? GL_RGBA12 : GL_RGBA8, _size->width, _size->height, 0, GL_RGB, format, image->data()[0]);
                check_gl_error ("glTexImage2D");
 
                auto const canvas_size = _canvas_size.load();
@@ -517,6 +565,7 @@ GLVideoView::set_image (shared_ptr<const Image> image)
        check_gl_error ("glTexParameterf");
 }
 
+
 void
 GLVideoView::start ()
 {
@@ -566,7 +615,7 @@ GLVideoView::set_image_and_draw ()
 {
        auto pv = player_video().first;
        if (pv) {
-               set_image (pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true));
+               set_image (pv);
                draw (pv->inter_position(), pv->inter_size());
                _viewer->image_changed (pv);
        }
@@ -619,9 +668,9 @@ try
        _vsync_enabled = true;
 #endif
 
-       glGenTextures (1, &_texture);
+       glGenTextures (1, &_video_texture);
        check_gl_error ("glGenTextures");
-       glBindTexture (GL_TEXTURE_2D, _texture);
+       glBindTexture (GL_TEXTURE_2D, _video_texture);
        check_gl_error ("glBindTexture");
 
        while (true) {