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
This commit is contained in:
Georges Basile Stavracas Neto 2020-09-11 14:41:53 -03:00
parent 1f38494a7a
commit eaa795a266
2 changed files with 6 additions and 89 deletions

View File

@ -47,21 +47,12 @@ 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;
_COGL_MATRIX_DEBUG_PRINT (result);
}
@ -69,11 +60,6 @@ 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);
}
/*
@ -99,7 +85,8 @@ cogl_debug_matrix_print (const CoglMatrix *matrix)
*/
static inline gboolean
calculate_inverse (CoglMatrix *matrix)
calculate_inverse (const CoglMatrix *matrix,
CoglMatrix *inverse)
{
graphene_matrix_t scaled;
graphene_matrix_t m;
@ -122,50 +109,20 @@ calculate_inverse (CoglMatrix *matrix)
/* Float precision is a limiting factor */
graphene_matrix_multiply (&m, &scaled, &m);
invertible = graphene_matrix_inverse (&m, &matrix->inv);
invertible = graphene_matrix_inverse (&m, &inverse->m);
if (invertible)
graphene_matrix_multiply (&scaled, &matrix->inv, &matrix->inv);
graphene_matrix_multiply (&scaled, &inverse->m, &inverse->m);
else
graphene_matrix_init_identity (&matrix->inv);
graphene_matrix_init_identity (&inverse->m);
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
@ -177,16 +134,11 @@ 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;
_COGL_MATRIX_DEBUG_PRINT (matrix);
}
@ -210,9 +162,6 @@ 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,
@ -220,9 +169,6 @@ cogl_matrix_frustum (CoglMatrix *matrix,
z_near, z_far);
graphene_matrix_multiply (&frustum, &matrix->m, &matrix->m);
flags |= COGL_MATRIX_FLAG_DIRTY_INVERSE;
matrix->flags = flags;
_COGL_MATRIX_DEBUG_PRINT (matrix);
}
@ -255,9 +201,6 @@ cogl_matrix_orthographic (CoglMatrix *matrix,
float far)
{
graphene_matrix_t ortho;
unsigned long flags;
flags = matrix->flags;
graphene_matrix_init_ortho (&ortho,
left, right,
@ -265,8 +208,6 @@ cogl_matrix_orthographic (CoglMatrix *matrix,
near, far);
graphene_matrix_multiply (&ortho, &matrix->m, &matrix->m);
matrix->flags = flags | COGL_MATRIX_FLAG_DIRTY_INVERSE;
_COGL_MATRIX_DEBUG_PRINT (matrix);
}
@ -277,15 +218,10 @@ 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;
_COGL_MATRIX_DEBUG_PRINT (matrix);
}
@ -300,7 +236,6 @@ 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;
_COGL_MATRIX_DEBUG_PRINT (matrix);
}
@ -309,7 +244,6 @@ void
cogl_matrix_init_identity (CoglMatrix *matrix)
{
graphene_matrix_init_identity (&matrix->m);
matrix->flags = COGL_MATRIX_FLAG_DIRTY_INVERSE;
_COGL_MATRIX_DEBUG_PRINT (matrix);
}
@ -320,8 +254,6 @@ cogl_matrix_init_translation (CoglMatrix *matrix,
float tz)
{
graphene_matrix_init_translate (&matrix->m, &GRAPHENE_POINT3D_INIT (tx, ty, tz));
matrix->flags = COGL_MATRIX_FLAG_DIRTY_INVERSE;
_COGL_MATRIX_DEBUG_PRINT (matrix);
}
@ -339,7 +271,6 @@ 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;
}
void
@ -361,7 +292,6 @@ _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;
}
void
@ -748,7 +678,6 @@ cogl_matrix_look_at (CoglMatrix *matrix,
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;
cogl_matrix_multiply (matrix, matrix, &look_at);
}
@ -779,7 +708,6 @@ cogl_matrix_skew_xy (CoglMatrix *matrix,
graphene_matrix_skew_xy (&skew, factor);
graphene_matrix_multiply (&skew, &matrix->m, &matrix->m);
matrix->flags |= COGL_MATRIX_FLAG_DIRTY_INVERSE;
_COGL_MATRIX_DEBUG_PRINT (matrix);
}
@ -793,7 +721,6 @@ cogl_matrix_skew_xz (CoglMatrix *matrix,
graphene_matrix_skew_xz (&skew, factor);
graphene_matrix_multiply (&skew, &matrix->m, &matrix->m);
matrix->flags |= COGL_MATRIX_FLAG_DIRTY_INVERSE;
_COGL_MATRIX_DEBUG_PRINT (matrix);
}
@ -807,7 +734,6 @@ cogl_matrix_skew_yz (CoglMatrix *matrix,
graphene_matrix_skew_yz (&skew, factor);
graphene_matrix_multiply (&skew, &matrix->m, &matrix->m);
matrix->flags |= COGL_MATRIX_FLAG_DIRTY_INVERSE;
_COGL_MATRIX_DEBUG_PRINT (matrix);
}

View File

@ -82,9 +82,6 @@ struct _CoglMatrix
{
/*< private >*/
graphene_matrix_t m;
graphene_matrix_t COGL_PRIVATE (inv);
unsigned long COGL_PRIVATE (flags);
};
/**
@ -524,12 +521,6 @@ cogl_matrix_free (CoglMatrix *matrix);
* Gets the inverse transform of a given matrix and uses it to initialize
* a new #CoglMatrix.
*
* <note>Although the first parameter is annotated as const to indicate
* that the transform it represents isn't modified this function may
* technically save a copy of the inverse transform within the given
* #CoglMatrix so that subsequent requests for the inverse transform may
* avoid costly inversion calculations.</note>
*
* Return value: %TRUE if the inverse was successfully calculated or %FALSE
* for degenerate transformations that can't be inverted (in this case the
* @inverse matrix will simply be initialized with the identity matrix)