[vlc-devel] [PATCH] OpenGL: display only the left eye of stereo video

Adrien Maglo magsoft at videolan.org
Tue Nov 14 14:49:48 CET 2017


This allows to display an usable view of 360 stereo video.
---
 modules/video_output/opengl/converter.h        |  1 +
 modules/video_output/opengl/fragment_shaders.c | 10 ++++--
 modules/video_output/opengl/vout_helper.c      | 43 ++++++++++++++++++++++++++
 3 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/modules/video_output/opengl/converter.h b/modules/video_output/opengl/converter.h
index efb423ae69..d0976e9666 100644
--- a/modules/video_output/opengl/converter.h
+++ b/modules/video_output/opengl/converter.h
@@ -155,6 +155,7 @@ typedef struct {
     PFNGLUNIFORMMATRIX3FVPROC        UniformMatrix3fv;
     PFNGLUNIFORMMATRIX2FVPROC        UniformMatrix2fv;
     PFNGLUNIFORM4FVPROC              Uniform4fv;
+    PFNGLUNIFORM2FVPROC              Uniform2fv;
     PFNGLUNIFORM4FPROC               Uniform4f;
     PFNGLUNIFORM3FPROC               Uniform3f;
     PFNGLUNIFORM2FPROC               Uniform2f;
diff --git a/modules/video_output/opengl/fragment_shaders.c b/modules/video_output/opengl/fragment_shaders.c
index c3fe2d85b7..2e9a7cf583 100644
--- a/modules/video_output/opengl/fragment_shaders.c
+++ b/modules/video_output/opengl/fragment_shaders.c
@@ -472,6 +472,8 @@ xyz12_shader_init(opengl_tex_converter_t *tc)
         "#version %u\n"
         "%s"
         "uniform sampler2D Texture0;"
+        "uniform vec2 StereoCoefs;"
+        "uniform vec2 StereoOffsets;"
         "uniform vec4 xyz_gamma = vec4(2.6);"
         "uniform vec4 rgb_gamma = vec4(1.0/2.2);"
         /* WARN: matrix Is filled column by column (not row !) */
@@ -486,7 +488,7 @@ xyz12_shader_init(opengl_tex_converter_t *tc)
         "void main()"
         "{ "
         " vec4 v_in, v_out;"
-        " v_in  = texture2D(Texture0, TexCoord0);"
+        " v_in  = texture2D(Texture0, TexCoord0 * StereoCoefs + StereoOffsets);"
         " v_in = pow(v_in, xyz_gamma);"
         " v_out = matrix_xyz_rgb * v_in ;"
         " v_out = pow(v_out, rgb_gamma) ;"
@@ -602,6 +604,8 @@ opengl_fragment_shader_init_impl(opengl_tex_converter_t *tc, GLenum tex_target,
 #define ADDF(x, ...) vlc_memstream_printf(&ms, x, ##__VA_ARGS__)
 
     ADDF("#version %u\n%s", tc->glsl_version, tc->glsl_precision_header);
+    ADD("uniform vec2 StereoCoefs;\n");
+    ADD("uniform vec2 StereoOffsets;\n");
 
     for (unsigned i = 0; i < tc->tex_count; ++i)
         ADDF("uniform %s Texture%u;\n"
@@ -666,7 +670,7 @@ opengl_fragment_shader_init_impl(opengl_tex_converter_t *tc, GLenum tex_target,
         if (swizzle)
         {
             size_t swizzle_count = strlen(swizzle);
-            ADDF(" colors = %s(Texture%u, %s%u);\n", lookup, i, coord_name, i);
+            ADDF(" colors = %s(Texture%u, %s%u * StereoCoefs + StereoOffsets);\n", lookup, i, coord_name, i);
             for (unsigned j = 0; j < swizzle_count; ++j)
             {
                 ADDF(" val = colors.%c;\n"
@@ -678,7 +682,7 @@ opengl_fragment_shader_init_impl(opengl_tex_converter_t *tc, GLenum tex_target,
         }
         else
         {
-            ADDF(" vec4 color%u = %s(Texture%u, %s%u);\n",
+            ADDF(" vec4 color%u = %s(Texture%u, %s%u * StereoCoefs + StereoOffsets);\n",
                  color_idx, lookup, i, coord_name, i);
             color_idx++;
             assert(color_idx <= PICTURE_PLANE_MAX);
diff --git a/modules/video_output/opengl/vout_helper.c b/modules/video_output/opengl/vout_helper.c
index ddba24076c..fa1e2f9b2b 100644
--- a/modules/video_output/opengl/vout_helper.c
+++ b/modules/video_output/opengl/vout_helper.c
@@ -104,6 +104,8 @@ struct prgm
         GLfloat YRotMatrix[16];
         GLfloat XRotMatrix[16];
         GLfloat ZoomMatrix[16];
+        GLfloat StereoCoefs[2];
+        GLfloat StereoOffsets[2];
     } var;
 
     struct { /* UniformLocation */
@@ -113,6 +115,8 @@ struct prgm
         GLint YRotMatrix;
         GLint XRotMatrix;
         GLint ZoomMatrix;
+        GLint StereoCoefs;
+        GLint StereoOffsets;
     } uloc;
     struct { /* AttribLocation */
         GLint MultiTexCoord[3];
@@ -287,6 +291,33 @@ static void getViewpointMatrixes(vout_display_opengl_t *vgl,
     }
 }
 
+
+static void getStereoParams(vout_display_opengl_t *vgl, struct prgm *prgm)
+{
+    float *StereoCoefs = prgm->var.StereoCoefs;
+    float *StereoOffsets = prgm->var.StereoOffsets;
+
+    switch (vgl->fmt.multiview_mode)
+    {
+    case MULTIVIEW_STEREO_TB:
+        // Left eye.
+        StereoCoefs[0] = 1; StereoCoefs[1] = 0.5;
+        StereoOffsets[0] = 0; StereoOffsets[1] = 0;
+        break;
+    case MULTIVIEW_STEREO_SBS:
+        // Left eye.
+        StereoCoefs[0] = 0.5; StereoCoefs[1] = 1;
+        StereoOffsets[0] = 0; StereoOffsets[1] = 0;
+        break;
+    default:
+        // Everything
+        StereoCoefs[0] = 1; StereoCoefs[1] = 1;
+        StereoOffsets[0] = 0; StereoOffsets[1] = 0;
+        break;
+    }
+}
+
+
 static void getOrientationTransformMatrix(video_orientation_t orientation,
                                           GLfloat matrix[static 16])
 {
@@ -521,6 +552,8 @@ opengl_link_program(struct prgm *prgm)
     GET_ULOC(YRotMatrix, "YRotMatrix");
     GET_ULOC(XRotMatrix, "XRotMatrix");
     GET_ULOC(ZoomMatrix, "ZoomMatrix");
+    GET_ULOC(StereoCoefs, "StereoCoefs");
+    GET_ULOC(StereoOffsets, "StereoOffsets");
 
     GET_ALOC(VertexPosition, "VertexPosition");
     GET_ALOC(MultiTexCoord[0], "MultiTexCoord0");
@@ -795,6 +828,7 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
     GET_PROC_ADDR(UniformMatrix3fv);
     GET_PROC_ADDR(UniformMatrix2fv);
     GET_PROC_ADDR(Uniform4fv);
+    GET_PROC_ADDR(Uniform2fv);
     GET_PROC_ADDR(Uniform4f);
     GET_PROC_ADDR(Uniform3f);
     GET_PROC_ADDR(Uniform2f);
@@ -1556,6 +1590,10 @@ static void DrawWithShaders(vout_display_opengl_t *vgl, struct prgm *prgm)
     vgl->vt.UniformMatrix4fv(prgm->uloc.ZoomMatrix, 1, GL_FALSE,
                              prgm->var.ZoomMatrix);
 
+    getStereoParams(vgl, prgm);
+    vgl->vt.Uniform2fv(prgm->uloc.StereoCoefs, 1, prgm->var.StereoCoefs);
+    vgl->vt.Uniform2fv(prgm->uloc.StereoOffsets, 1, prgm->var.StereoOffsets);
+
     glDrawElements(GL_TRIANGLES, vgl->nb_indices, GL_UNSIGNED_SHORT, 0);
 }
 
@@ -1689,6 +1727,11 @@ int vout_display_opengl_Display(vout_display_opengl_t *vgl,
         vgl->vt.UniformMatrix4fv(prgm->uloc.ZoomMatrix, 1, GL_FALSE,
                                  prgm->var.ZoomMatrix);
 
+        prgm->var.StereoCoefs[0] = 1.f; prgm->var.StereoCoefs[1] = 1.f;
+        prgm->var.StereoOffsets[0] = 0.f; prgm->var.StereoOffsets[1] = 0.f;
+        vgl->vt.Uniform2fv(prgm->uloc.StereoCoefs, 1, prgm->var.StereoCoefs);
+        vgl->vt.Uniform2fv(prgm->uloc.StereoOffsets, 1, prgm->var.StereoOffsets);
+
         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
     }
     glDisable(GL_BLEND);
-- 
2.14.1



More information about the vlc-devel mailing list