[mutter/gbsneto/graphene-matrix] cogl/matrix: Remove cached inverse and flags



commit fc59f66badc839abf1ff629590d2694f8c8a071e
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Fri Sep 11 14:41:53 2020 -0300

    cogl/matrix: Remove cached inverse and flags
    
    Remove the cached inverse, and dirty flags, and typedef CoglMatrix to
    graphene_matrix_t itself. I preverved the type for this commit to help
    reducing the commit size, next commits will remove the CoglMatrix type.
    
    https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1439

 cogl/cogl/cogl-matrix.c | 140 ++++++++++++------------------------------------
 cogl/cogl/cogl-matrix.h |  31 -----------
 cogl/cogl/cogl-types.h  |   3 +-
 3 files changed, 35 insertions(+), 139 deletions(-)
---
diff --git a/cogl/cogl/cogl-matrix.c b/cogl/cogl/cogl-matrix.c
index 926a84c958..12c4755eb2 100644
--- a/cogl/cogl/cogl-matrix.c
+++ b/cogl/cogl/cogl-matrix.c
@@ -47,33 +47,19 @@ COGL_GTYPE_DEFINE_BOXED (Matrix, matrix,
                          cogl_matrix_copy,
                          cogl_matrix_free);
 
-enum CoglMatrixFlags
-{
-  COGL_MATRIX_FLAG_NONE = 0,
-  COGL_MATRIX_FLAG_SINGULAR = 1 << 0,
-  COGL_MATRIX_FLAG_DIRTY_INVERSE = 1 << 1,
-};
-
 void
 cogl_matrix_multiply (CoglMatrix *result,
                      const CoglMatrix *a,
                      const CoglMatrix *b)
 {
-  graphene_matrix_multiply (&b->m, &a->m, &result->m);
-  result->flags = a->flags | b->flags | COGL_MATRIX_FLAG_DIRTY_INVERSE;
-
+  graphene_matrix_multiply (b, a, result);
   _COGL_MATRIX_DEBUG_PRINT (result);
 }
 
 void
 _cogl_matrix_prefix_print (const char *prefix, const CoglMatrix *matrix)
 {
-  graphene_matrix_print (&matrix->m);
-  g_print ("%sInverse: \n", prefix);
-  if (!(matrix->flags & COGL_MATRIX_FLAG_DIRTY_INVERSE))
-    graphene_matrix_print (&matrix->inv);
-  else
-    g_print ("%s  - not available\n", prefix);
+  graphene_matrix_print (matrix);
 }
 
 /*
@@ -101,7 +87,8 @@ cogl_debug_matrix_print (const CoglMatrix *matrix)
 #define SCALE 1000.F
 
 static inline gboolean
-calculate_inverse (CoglMatrix *matrix)
+calculate_inverse (const CoglMatrix *matrix,
+                   CoglMatrix       *inverse)
 {
   graphene_matrix_t scaled;
   graphene_matrix_t m;
@@ -110,53 +97,23 @@ calculate_inverse (CoglMatrix *matrix)
   graphene_matrix_init_scale (&scaled, SCALE, SCALE, SCALE);
 
   // Float precision is a limiting factor
-  graphene_matrix_init_from_matrix (&m, &matrix->m);
+  graphene_matrix_init_from_matrix (&m, matrix);
   graphene_matrix_multiply (&m, &scaled, &m);
 
-  invertible = graphene_matrix_inverse (&m, &matrix->inv);
+  invertible = graphene_matrix_inverse (&m, inverse);
 
   if (invertible)
-    graphene_matrix_multiply (&scaled, &matrix->inv, &matrix->inv);
+    graphene_matrix_multiply (&scaled, inverse, inverse);
   else
-    graphene_matrix_init_identity (&matrix->inv);
+    graphene_matrix_init_identity (inverse);
 
   return invertible;
 }
 
-static gboolean
-_cogl_matrix_update_inverse (CoglMatrix *matrix)
-{
-  if (matrix->flags & COGL_MATRIX_FLAG_DIRTY_INVERSE)
-    {
-      if (calculate_inverse (matrix))
-        matrix->flags &= ~COGL_MATRIX_FLAG_SINGULAR;
-      else
-        matrix->flags |= COGL_MATRIX_FLAG_SINGULAR;
-
-      matrix->flags &= ~COGL_MATRIX_FLAG_DIRTY_INVERSE;
-    }
-
-  if (matrix->flags & COGL_MATRIX_FLAG_SINGULAR)
-    return FALSE;
-  else
-    return TRUE;
-}
-
 gboolean
 cogl_matrix_get_inverse (const CoglMatrix *matrix, CoglMatrix *inverse)
 {
-  if (_cogl_matrix_update_inverse ((CoglMatrix *)matrix))
-    {
-      graphene_matrix_init_from_matrix (&inverse->m, &matrix->inv);
-      graphene_matrix_init_from_matrix (&inverse->inv, &matrix->m);
-      inverse->flags = COGL_MATRIX_FLAG_NONE;
-      return TRUE;
-    }
-  else
-    {
-      cogl_matrix_init_identity (inverse);
-      return FALSE;
-    }
+  return calculate_inverse (matrix, inverse);
 }
 
 void
@@ -168,15 +125,10 @@ cogl_matrix_rotate (CoglMatrix *matrix,
 {
   graphene_matrix_t rotation;
   graphene_vec3_t axis;
-  unsigned long flags;
-
-  flags = matrix->flags;
 
   graphene_vec3_init (&axis, x, y, z);
   graphene_matrix_init_rotate (&rotation, angle, &axis);
-  graphene_matrix_multiply (&rotation, &matrix->m, &matrix->m);
-
-  matrix->flags = flags |= COGL_MATRIX_FLAG_DIRTY_INVERSE;
+  graphene_matrix_multiply (&rotation, matrix, matrix);
 
   _COGL_MATRIX_DEBUG_PRINT (matrix);
 }
@@ -201,18 +153,12 @@ cogl_matrix_frustum (CoglMatrix *matrix,
                      float       z_far)
 {
   graphene_matrix_t frustum;
-  unsigned long flags;
-
-  flags = matrix->flags;
 
   graphene_matrix_init_frustum (&frustum,
                                 left, right,
                                 bottom, top,
                                 z_near, z_far);
-  graphene_matrix_multiply (&frustum, &matrix->m, &matrix->m);
-
-  flags |= COGL_MATRIX_FLAG_DIRTY_INVERSE;
-  matrix->flags = flags;
+  graphene_matrix_multiply (&frustum, matrix, matrix);
 
   _COGL_MATRIX_DEBUG_PRINT (matrix);
 }
@@ -246,17 +192,12 @@ cogl_matrix_orthographic (CoglMatrix *matrix,
                           float far)
 {
   graphene_matrix_t ortho;
-  unsigned long flags;
-
-  flags = matrix->flags;
 
   graphene_matrix_init_ortho (&ortho,
                               left, right,
                               top, bottom,
                               near, far);
-  graphene_matrix_multiply (&ortho, &matrix->m, &matrix->m);
-
-  matrix->flags = flags | COGL_MATRIX_FLAG_DIRTY_INVERSE;
+  graphene_matrix_multiply (&ortho, matrix, matrix);
 
   _COGL_MATRIX_DEBUG_PRINT (matrix);
 }
@@ -268,14 +209,9 @@ cogl_matrix_scale (CoglMatrix *matrix,
                   float sz)
 {
   graphene_matrix_t scale;
-  unsigned long flags;
-
-  flags = matrix->flags;
 
   graphene_matrix_init_scale (&scale, sx, sy, sz);
-  graphene_matrix_multiply (&scale, &matrix->m, &matrix->m);
-
-  matrix->flags = flags | COGL_MATRIX_FLAG_DIRTY_INVERSE;
+  graphene_matrix_multiply (&scale, matrix, matrix);
 
   _COGL_MATRIX_DEBUG_PRINT (matrix);
 }
@@ -290,8 +226,7 @@ cogl_matrix_translate (CoglMatrix *matrix,
 
   graphene_matrix_init_translate (&translation,
                                   &GRAPHENE_POINT3D_INIT (x, y, z));
-  graphene_matrix_multiply (&translation, &matrix->m, &matrix->m);
-  matrix->flags |= COGL_MATRIX_FLAG_DIRTY_INVERSE;
+  graphene_matrix_multiply (&translation, matrix, matrix);
 
   _COGL_MATRIX_DEBUG_PRINT (matrix);
 }
@@ -299,8 +234,7 @@ cogl_matrix_translate (CoglMatrix *matrix,
 void
 cogl_matrix_init_identity (CoglMatrix *matrix)
 {
-  graphene_matrix_init_identity (&matrix->m);
-  matrix->flags = COGL_MATRIX_FLAG_DIRTY_INVERSE;
+  graphene_matrix_init_identity (matrix);
   _COGL_MATRIX_DEBUG_PRINT (matrix);
 }
 
@@ -310,9 +244,7 @@ cogl_matrix_init_translation (CoglMatrix *matrix,
                               float       ty,
                               float       tz)
 {
-  graphene_matrix_init_translate (&matrix->m, &GRAPHENE_POINT3D_INIT (tx, ty, tz));
-  matrix->flags = COGL_MATRIX_FLAG_DIRTY_INVERSE;
-
+  graphene_matrix_init_translate (matrix, &GRAPHENE_POINT3D_INIT (tx, ty, tz));
   _COGL_MATRIX_DEBUG_PRINT (matrix);
 }
 
@@ -329,8 +261,7 @@ cogl_matrix_init_translation (CoglMatrix *matrix,
 static void
 _cogl_matrix_init_from_array (CoglMatrix *matrix, const float *array)
 {
-  graphene_matrix_init_from_float (&matrix->m, array);
-  matrix->flags = COGL_MATRIX_FLAG_DIRTY_INVERSE;
+  graphene_matrix_init_from_float (matrix, array);
 }
 
 void
@@ -351,16 +282,15 @@ void
 _cogl_matrix_init_from_matrix_without_inverse (CoglMatrix *matrix,
                                                const CoglMatrix *src)
 {
-  graphene_matrix_init_from_matrix (&matrix->m, &src->m);
-  matrix->flags = src->flags | COGL_MATRIX_FLAG_DIRTY_INVERSE;
+  graphene_matrix_init_from_matrix (matrix, src);
 }
 
 void
 cogl_matrix_init_from_euler (CoglMatrix *matrix,
                              const graphene_euler_t *euler)
 {
-  graphene_matrix_init_identity (&matrix->m);
-  graphene_matrix_rotate_euler (&matrix->m, euler);
+  graphene_matrix_init_identity (matrix);
+  graphene_matrix_rotate_euler (matrix, euler);
 }
 
 void
@@ -429,7 +359,7 @@ cogl_matrix_equal (const void *v1, const void *v2)
   g_return_val_if_fail (v1 != NULL, FALSE);
   g_return_val_if_fail (v2 != NULL, FALSE);
 
-  return graphene_matrix_equal_fast (&a->m, &b->m);
+  return graphene_matrix_equal_fast (a, b);
 }
 
 CoglMatrix *
@@ -451,7 +381,7 @@ void
 cogl_matrix_to_float (const CoglMatrix *matrix,
                       float            *out_array)
 {
-  graphene_matrix_to_float (&matrix->m, out_array);
+  graphene_matrix_to_float (matrix, out_array);
 }
 
 float
@@ -459,7 +389,7 @@ cogl_matrix_get_value (const CoglMatrix *matrix,
                        unsigned int      row,
                        unsigned int      column)
 {
-  return graphene_matrix_get_value (&matrix->m, column, row);
+  return graphene_matrix_get_value (matrix, column, row);
 }
 
 void
@@ -472,7 +402,7 @@ cogl_matrix_transform_point (const CoglMatrix *matrix,
   graphene_vec4_t p;
 
   graphene_vec4_init (&p, *x, *y, *z, *w);
-  graphene_matrix_transform_vec4 (&matrix->m, &p, &p);
+  graphene_matrix_transform_vec4 (matrix, &p, &p);
 
   *x = graphene_vec4_get_x (&p);
   *y = graphene_vec4_get_y (&p);
@@ -509,7 +439,7 @@ init_matrix_rows (const CoglMatrix *matrix,
   graphene_matrix_t m;
   unsigned int i;
 
-  graphene_matrix_transpose (&matrix->m, &m);
+  graphene_matrix_transpose (matrix, &m);
 
   for (i = 0; i < n_rows; i++)
     graphene_matrix_get_row (&m, i, &rows[i]);
@@ -714,7 +644,7 @@ cogl_matrix_project_points (const CoglMatrix *matrix,
 gboolean
 cogl_matrix_is_identity (const CoglMatrix *matrix)
 {
-  return graphene_matrix_is_identity (&matrix->m);
+  return graphene_matrix_is_identity (matrix);
 }
 
 void
@@ -738,8 +668,7 @@ cogl_matrix_look_at (CoglMatrix *matrix,
   graphene_vec3_init (&center, object_x, object_y, object_z);
   graphene_vec3_init (&up, world_up_x, world_up_y, world_up_z);
 
-  graphene_matrix_init_look_at (&look_at.m, &eye, &center, &up);
-  look_at.flags = COGL_MATRIX_FLAG_DIRTY_INVERSE;
+  graphene_matrix_init_look_at (&look_at, &eye, &center, &up);
 
   cogl_matrix_multiply (matrix, matrix, &look_at);
 }
@@ -748,10 +677,10 @@ void
 cogl_matrix_transpose (CoglMatrix *matrix)
 {
   /* We don't need to do anything if the matrix is the identity matrix */
-  if (graphene_matrix_is_identity (&matrix->m))
+  if (graphene_matrix_is_identity (matrix))
     return;
 
-  graphene_matrix_transpose (&matrix->m, &matrix->m);
+  graphene_matrix_transpose (matrix, matrix);
 }
 
 GType
@@ -768,9 +697,8 @@ cogl_matrix_skew_xy (CoglMatrix *matrix,
 
   graphene_matrix_init_identity (&skew);
   graphene_matrix_skew_xy (&skew, factor);
-  graphene_matrix_multiply (&skew, &matrix->m, &matrix->m);
+  graphene_matrix_multiply (&skew, matrix, matrix);
 
-  matrix->flags |= COGL_MATRIX_FLAG_DIRTY_INVERSE;
   _COGL_MATRIX_DEBUG_PRINT (matrix);
 }
 
@@ -782,9 +710,8 @@ cogl_matrix_skew_xz (CoglMatrix *matrix,
 
   graphene_matrix_init_identity (&skew);
   graphene_matrix_skew_xz (&skew, factor);
-  graphene_matrix_multiply (&skew, &matrix->m, &matrix->m);
+  graphene_matrix_multiply (&skew, matrix, matrix);
 
-  matrix->flags |= COGL_MATRIX_FLAG_DIRTY_INVERSE;
   _COGL_MATRIX_DEBUG_PRINT (matrix);
 }
 
@@ -796,14 +723,13 @@ cogl_matrix_skew_yz (CoglMatrix *matrix,
 
   graphene_matrix_init_identity (&skew);
   graphene_matrix_skew_yz (&skew, factor);
-  graphene_matrix_multiply (&skew, &matrix->m, &matrix->m);
+  graphene_matrix_multiply (&skew, matrix, matrix);
 
-  matrix->flags |= COGL_MATRIX_FLAG_DIRTY_INVERSE;
   _COGL_MATRIX_DEBUG_PRINT (matrix);
 }
 
 const graphene_matrix_t *
 cogl_matrix_get_graphene_matrix (const CoglMatrix *matrix)
 {
-  return &matrix->m;
+  return matrix;
 }
diff --git a/cogl/cogl/cogl-matrix.h b/cogl/cogl/cogl-matrix.h
index 347584dfe5..f61d011714 100644
--- a/cogl/cogl/cogl-matrix.h
+++ b/cogl/cogl/cogl-matrix.h
@@ -56,37 +56,6 @@ G_BEGIN_DECLS
  * be used for direct manipulation of these matrices.
  */
 
-/**
- * CoglMatrix:
- *
- * A CoglMatrix holds a 4x4 transform matrix. It uses #graphene_matrix_t
- * internally which means it is compatible with what OpenGL expects.
- *
- * A CoglMatrix can represent transforms such as, rotations, scaling,
- * translation, sheering, and linear projections. You can combine these
- * transforms by multiplying multiple matrices in the order you want them
- * applied.
- *
- * The transformation of a vertex (x, y, z, w) by a CoglMatrix is given by:
- *
- * |[
- *   x_new = xx * x + xy * y + xz * z + xw * w
- *   y_new = yx * x + yy * y + yz * z + yw * w
- *   z_new = zx * x + zy * y + zz * z + zw * w
- *   w_new = wx * x + wy * y + wz * z + ww * w
- * ]|
- *
- * Where w is normally 1
- */
-struct _CoglMatrix
-{
-  /*< private >*/
-  graphene_matrix_t m;
-
-  graphene_matrix_t COGL_PRIVATE (inv);
-  unsigned long  COGL_PRIVATE (flags);
-};
-
 /**
  * cogl_matrix_init_identity:
  * @matrix: A 4x4 transformation matrix
diff --git a/cogl/cogl/cogl-types.h b/cogl/cogl/cogl-types.h
index 35871703d6..6fc9663686 100644
--- a/cogl/cogl/cogl-types.h
+++ b/cogl/cogl/cogl-types.h
@@ -40,6 +40,7 @@
 
 #include <cogl/cogl-defines.h>
 #include <cogl/cogl-macros.h>
+#include <graphene.h>
 
 #include <glib.h>
 #include <glib-object.h>
@@ -87,7 +88,7 @@ cogl_handle_get_type (void) G_GNUC_CONST;
 
 /* We forward declare this in cogl-types to avoid circular dependencies
  * between cogl-matrix.h and cogl-quaterion.h */
-typedef struct _CoglMatrix      CoglMatrix;
+typedef graphene_matrix_t CoglMatrix;
 
 /**
  * CoglAngle:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]