[vlc-commits] [Git][videolan/vlc][master] opengl: fix matrix dimensions convention

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Tue Oct 19 11:49:53 UTC 2021



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
397e4a3e by Romain Vimont at 2021-10-19T11:35:30+00:00
opengl: fix matrix dimensions convention

A m×n matrix contains m rows and n columns.

My usage (m columns and n rows) did not match the common convention, so
what I called 3x2 matrices were in fact 2x3 matrices.

- - - - -


6 changed files:

- modules/video_output/opengl/filter_mock.c
- modules/video_output/opengl/gl_util.h
- modules/video_output/opengl/interop.h
- modules/video_output/opengl/interop_android.c
- modules/video_output/opengl/sampler.c
- modules/video_output/opengl/sampler.h


Changes:

=====================================
modules/video_output/opengl/filter_mock.c
=====================================
@@ -189,7 +189,7 @@ DrawMask(struct vlc_gl_filter *filter, const struct vlc_gl_input_meta *meta)
     const float *mtx = sampler->pic_to_tex_matrix;
     assert(mtx);
 
-    /* Expand the 3x2 matrix to 3x3 to store it in a mat3 uniform (for better
+    /* Expand the 2x3 matrix to 3x3 to store it in a mat3 uniform (for better
      * compatibility). Both are in column-major order. */
     float pic_to_tex[] = { mtx[0], mtx[1], 0,
                            mtx[2], mtx[3], 0,


=====================================
modules/video_output/opengl/gl_util.h
=====================================
@@ -43,7 +43,7 @@ static const float MATRIX3_IDENTITY[3*3] = {
 };
 
 /* In column-major order */
-static const float MATRIX3x2_IDENTITY[3*2] = {
+static const float MATRIX2x3_IDENTITY[2*3] = {
     1, 0,
     0, 1,
     0, 0,


=====================================
modules/video_output/opengl/interop.h
=====================================
@@ -76,7 +76,7 @@ struct vlc_gl_interop_ops {
      *
      * This function pointer can be NULL. If it is set, it may return NULL.
      *
-     * Otherwise, it must return a 3x2 matrix, as an array of 6 floats in
+     * Otherwise, it must return a 2x3 matrix, as an array of 6 floats in
      * column-major order.
      *
      * This transform matrix maps 2D homogeneous texture coordinates of the
@@ -88,7 +88,7 @@ struct vlc_gl_interop_ops {
      * freed before the module is closed.
      *
      * \param interop the OpenGL interop
-     * \return a 3x2 transformation matrix (possibly NULL)
+     * \return a 2x3 transformation matrix (possibly NULL)
      */
     const float *
     (*get_transform_matrix)(const struct vlc_gl_interop *interop);


=====================================
modules/video_output/opengl/interop_android.c
=====================================
@@ -34,7 +34,7 @@
 
 struct priv
 {
-    float mtx_3x2[3*2];
+    float mtx_2x3[2*3];
     const float *transform_mtx;
 
     bool stex_attached;
@@ -43,7 +43,7 @@ struct priv
 };
 
 static void
-ReductMatrix(float *mtx_3x2, const float *mtx_4x4)
+ReductMatrix(float *mtx_2x3, const float *mtx_4x4)
 {
     /*
      * The transform matrix provided by Android is 4x4:
@@ -53,7 +53,7 @@ ReductMatrix(float *mtx_3x2, const float *mtx_4x4)
      * the form (s, t, 0, 1). Similarly, the third row is never used either,
      * since only the two first coordinates of the output vector are kept.
      *
-     *       mat_4x4        mat_3x2
+     *       mat_4x4        mat_2x3
      *
      *     / a b . c \
      *     | d e . f | --> / a b c \
@@ -61,14 +61,14 @@ ReductMatrix(float *mtx_3x2, const float *mtx_4x4)
      *     \ . . . . /
      */
 
-#define MTX4(COL,ROW) mtx_4x4[(COL)*4 + (ROW)]
-#define MTX3(COL,ROW) mtx_3x2[(COL)*2 + (ROW)]
+#define MTX4(ROW,COL) mtx_4x4[(COL)*4 + (ROW)]
+#define MTX3(ROW,COL) mtx_2x3[(COL)*2 + (ROW)]
     MTX3(0,0) = MTX4(0,0); // a
-    MTX3(1,0) = MTX4(1,0); // b
-    MTX3(2,0) = MTX4(3,0); // c
-    MTX3(0,1) = MTX4(0,1); // d
+    MTX3(0,1) = MTX4(0,1); // b
+    MTX3(0,2) = MTX4(0,3); // c
+    MTX3(1,0) = MTX4(1,0); // d
     MTX3(1,1) = MTX4(1,1); // e
-    MTX3(2,1) = MTX4(3,1); // f
+    MTX3(1,2) = MTX4(1,3); // f
 #undef MTX4
 #undef MTX3
 }
@@ -144,8 +144,8 @@ tc_anop_update(struct vlc_gl_interop *interop, GLuint *textures,
         goto error;
     }
 
-    ReductMatrix(priv->mtx_3x2, mtx_4x4);
-    priv->transform_mtx = priv->mtx_3x2;
+    ReductMatrix(priv->mtx_2x3, mtx_4x4);
+    priv->transform_mtx = priv->mtx_2x3;
 
     interop->vt->ActiveTexture(GL_TEXTURE0);
     interop->vt->BindTexture(interop->tex_target, textures[0]);


=====================================
modules/video_output/opengl/sampler.c
=====================================
@@ -96,10 +96,10 @@ struct vlc_gl_sampler_priv {
 
     /* All matrices below are stored in column-major order. */
 
-    float mtx_orientation[3*2];
-    float mtx_coords_map[3*2];
+    float mtx_orientation[2*3];
+    float mtx_coords_map[2*3];
 
-    float mtx_transform[3*2];
+    float mtx_transform[2*3];
     bool mtx_transform_defined;
 
     /**
@@ -118,7 +118,7 @@ struct vlc_gl_sampler_priv {
      *
      * It is stored in column-major order: [a, d, b, e, c, f].
      */
-    float mtx_all[3*2];
+    float mtx_all[2*3];
     bool mtx_all_defined;
     bool mtx_all_has_changed; /* since the previous picture */
 };
@@ -535,7 +535,7 @@ opengl_init_swizzle(struct vlc_gl_sampler *sampler,
 }
 
 static void
-InitOrientationMatrix(float matrix[static 3*2], video_orientation_t orientation)
+InitOrientationMatrix(float matrix[static 2*3], video_orientation_t orientation)
 {
 /**
  * / C0R0  C1R0  C3R0 \
@@ -1091,8 +1091,8 @@ CreateSampler(struct vlc_gl_interop *interop, struct vlc_gl_t *gl,
     assert(!interop || interop->tex_count == tex_count);
 
     /* This might be updated in UpdatePicture for non-direct samplers */
-    memcpy(&priv->mtx_coords_map, MATRIX3x2_IDENTITY,
-           sizeof(MATRIX3x2_IDENTITY));
+    memcpy(&priv->mtx_coords_map, MATRIX2x3_IDENTITY,
+           sizeof(MATRIX2x3_IDENTITY));
 
     if (interop)
     {
@@ -1170,12 +1170,12 @@ vlc_gl_sampler_Delete(struct vlc_gl_sampler *sampler)
 }
 
 /**
- * Compute out = a * b, as if the 3x2 matrices were expanded to 3x3 with
+ * Compute out = a * b, as if the 2x3 matrices were expanded to 3x3 with
  *  [0 0 1] as the last row.
  */
 static void
-MatrixMultiply(float out[static 3*2],
-               const float a[static 3*2], const float b[static 3*2])
+MatrixMultiply(float out[static 2*3],
+               const float a[static 2*3], const float b[static 2*3])
 {
     /* All matrices are stored in column-major order. */
     for (unsigned i = 0; i < 3; ++i)
@@ -1191,7 +1191,7 @@ MatrixMultiply(float out[static 3*2],
 static void
 UpdateMatrixAll(struct vlc_gl_sampler_priv *priv)
 {
-    float tmp[3*2];
+    float tmp[2*3];
 
     float *out = priv->mtx_transform_defined ? tmp : priv->mtx_all;
     /* out = mtx_coords_map * mtx_orientation */
@@ -1333,7 +1333,7 @@ vlc_gl_sampler_UpdateTextures(struct vlc_gl_sampler *sampler, GLuint textures[],
 
     if (!priv->mtx_all_defined)
     {
-        memcpy(priv->mtx_all, MATRIX3x2_IDENTITY, sizeof(MATRIX3x2_IDENTITY));
+        memcpy(priv->mtx_all, MATRIX2x3_IDENTITY, sizeof(MATRIX2x3_IDENTITY));
         priv->mtx_all_defined = true;
         priv->mtx_all_has_changed = true;
 
@@ -1364,15 +1364,15 @@ vlc_gl_sampler_PicToTexCoords(struct vlc_gl_sampler *sampler,
 {
     struct vlc_gl_sampler_priv *priv = PRIV(sampler);
     const float *mtx = priv->mtx_all;
-#define MTX(col,row) mtx[(col*2)+row]
+#define MTX(ROW,COL) mtx[(COL)*2+(ROW)]
     for (unsigned i = 0; i < coords_count; ++i)
     {
         /* Store the coordinates, in case the transform must be applied in
          * place (i.e. with pic_coords == tex_coords_out) */
         float x = pic_coords[0];
         float y = pic_coords[1];
-        tex_coords_out[0] = MTX(0,0) * x + MTX(1,0) * y + MTX(2,0);
-        tex_coords_out[1] = MTX(0,1) * x + MTX(1,1) * y + MTX(2,1);
+        tex_coords_out[0] = MTX(0,0) * x + MTX(0,1) * y + MTX(0,2);
+        tex_coords_out[1] = MTX(1,0) * x + MTX(1,1) * y + MTX(1,2);
         pic_coords += 2;
         tex_coords_out += 2;
     }


=====================================
modules/video_output/opengl/sampler.h
=====================================
@@ -63,7 +63,7 @@ struct vlc_gl_sampler {
     /**
      * Matrix to convert from picture coordinates to texture coordinates
      *
-     * The matrix is 3x2 and is stored in column-major order:
+     * The matrix is 2x3 and is stored in column-major order:
      *
      *     / a b c \
      *     \ d e f /



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/397e4a3ecc337db120557a86d0688519382ab35b

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/397e4a3ecc337db120557a86d0688519382ab35b
You're receiving this email because of your account on code.videolan.org.




More information about the vlc-commits mailing list