cogl/matrix: Calculate inverse using graphene matrices

Turns out inverting a matrix was the largest chunk of the CoglMatrix
code. By switching to Graphene, a lot of it can go away. The inverse
is still cached in the CoglMatrix struct itself, to preserve the
optimization.

However, switching to graphene_matrix_t to calculate the inverse has
a challenge: float precision. We had to work around it here, and it
needs an explanation.

The way to detect whether a matrix is invertible or not (i.e.
whether it's not a "singular" matrix, or not) is by checking
if the determinant equals 0. So far, so good.

Both graphene_matrix_t and CoglMatrix use single-precision
floats to store their 4x4 matrices. Graphene uses vectorized
operations to optimize determinant calculation, while Cogl
tries to keep track of the matrix type and has special-purpose
determinant functions for different matrix types (the most
common one being a 3D matrix).

Cogl, however, has a fundamentally flawed check for whether
the matrix is invertible or not. Have a look:

```
float det;

…

if (det*det < 1e-25)
   return FALSE;
```

Notice that 1e-25 is *way* smaller than FLT_EPSILON. This
check is fundamentally flawed.

"In practice, what does it break?", the reader might ask.
Well, in this case, the answer is opposite of that: Cogl
inverts matrices that should not be invertible. Let's see
an example: the model-view-projection of a 4K monitor. It
looks like this:

```
| +0,002693 +0,000000 +0,000000 +0,000000 |
| +0,000000 -0,002693 +0,000000 +0,000000 |
| +0,000000 +0,000000 +0,002693 +0,000000 |
| -5,169809 +2,908017 -5,036834 +1,000000 |
```

The determinant of this matrix is -0.000000019530306557.
It evidently is smaller than FLT_EPSILON. In this situation,
Cogl would happily calculate the inverse matrix, whereas
Graphene (correctly) bails out and thinks it's a singular
matrix.

This commit works around that by exploiting the maths around
it. The basis of it is:

  inverse(scalar * M) = (1/scalar) * M'

which can be extrapolated to:

  inverse(M) = scalar * inverse(scalar * M) = M'

In other words, scaling the to-be-inversed matrix, then
scaling the inverse matrix by the same factor, gives us
the desired inverse. In this commit, the scale is calculated
as 1 / (smallest value in the diagonal).

I'm sorry for everyone that has to read through this :(

https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1439
This commit is contained in:
Georges Basile Stavracas Neto 2020-09-10 14:28:04 -03:00
parent cb733f8fbc
commit ada5e67f7e

View File

@ -112,28 +112,6 @@ enum CoglMatrixType {
COGL_MATRIX_N_TYPES
} ;
#define DEG2RAD (G_PI/180.0)
/* Dot product of two 2-element vectors */
#define DOT2(A,B) ( (A)[0]*(B)[0] + (A)[1]*(B)[1] )
/* Dot product of two 3-element vectors */
#define DOT3(A,B) ( (A)[0]*(B)[0] + (A)[1]*(B)[1] + (A)[2]*(B)[2] )
#define CROSS3(N, U, V) \
do { \
(N)[0] = (U)[1]*(V)[2] - (U)[2]*(V)[1]; \
(N)[1] = (U)[2]*(V)[0] - (U)[0]*(V)[2]; \
(N)[2] = (U)[0]*(V)[1] - (U)[1]*(V)[0]; \
} while (0)
#define SUB_3V(DST, SRCA, SRCB) \
do { \
(DST)[0] = (SRCA)[0] - (SRCB)[0]; \
(DST)[1] = (SRCA)[1] - (SRCB)[1]; \
(DST)[2] = (SRCA)[2] - (SRCB)[2]; \
} while (0)
#define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
/*
@ -337,686 +315,6 @@ cogl_debug_matrix_print (const CoglMatrix *matrix)
*/
#define MAT(m,r,c) (m)[(c)*4+(r)]
/*
* Swaps the values of two floating pointer variables.
*
* Used by invert_matrix_general() to swap the row pointers.
*/
#define SWAP_ROWS(a, b) { float *_tmp = a; (a)=(b); (b)=_tmp; }
/*
* Compute inverse of 4x4 transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
*
* \author
* Code contributed by Jacques Leroy jle@star.be
*
* Calculates the inverse matrix by performing the gaussian matrix reduction
* with partial pivoting followed by back/substitution with the loops manually
* unrolled.
*/
static gboolean
invert_matrix_general (CoglMatrix *matrix)
{
const float *m = (float *)matrix;
float *out = matrix->inv;
float wtmp[4][8];
float m0, m1, m2, m3, s;
float *r0, *r1, *r2, *r3;
r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
r0[0] = MAT (m, 0, 0), r0[1] = MAT (m, 0, 1),
r0[2] = MAT (m, 0, 2), r0[3] = MAT (m, 0, 3),
r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
r1[0] = MAT (m, 1, 0), r1[1] = MAT (m, 1, 1),
r1[2] = MAT (m, 1, 2), r1[3] = MAT (m, 1, 3),
r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
r2[0] = MAT (m, 2, 0), r2[1] = MAT (m, 2, 1),
r2[2] = MAT (m, 2, 2), r2[3] = MAT (m, 2, 3),
r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
r3[0] = MAT (m, 3, 0), r3[1] = MAT (m, 3, 1),
r3[2] = MAT (m, 3, 2), r3[3] = MAT (m, 3, 3),
r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
/* choose pivot - or die */
if (fabsf (r3[0]) > fabsf (r2[0]))
SWAP_ROWS (r3, r2);
if (fabsf (r2[0]) > fabsf (r1[0]))
SWAP_ROWS (r2, r1);
if (fabsf (r1[0]) > fabsf (r0[0]))
SWAP_ROWS (r1, r0);
if (0.0 == r0[0])
return FALSE;
/* eliminate first variable */
m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
s = r0[4];
if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
s = r0[5];
if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
s = r0[6];
if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
s = r0[7];
if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
/* choose pivot - or die */
if (fabsf (r3[1]) > fabsf (r2[1]))
SWAP_ROWS (r3, r2);
if (fabsf (r2[1]) > fabsf (r1[1]))
SWAP_ROWS (r2, r1);
if (0.0 == r1[1])
return FALSE;
/* eliminate second variable */
m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
/* choose pivot - or die */
if (fabsf (r3[2]) > fabsf (r2[2]))
SWAP_ROWS (r3, r2);
if (0.0 == r2[2])
return FALSE;
/* eliminate third variable */
m3 = r3[2] / r2[2];
r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
r3[7] -= m3 * r2[7];
/* last check */
if (0.0 == r3[3])
return FALSE;
s = 1.0f / r3[3]; /* now back substitute row 3 */
r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
m2 = r2[3]; /* now back substitute row 2 */
s = 1.0f / r2[2];
r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
m1 = r1[3];
r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
m0 = r0[3];
r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
m1 = r1[2]; /* now back substitute row 1 */
s = 1.0f / r1[1];
r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
m0 = r0[2];
r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
m0 = r0[1]; /* now back substitute row 0 */
s = 1.0f / r0[0];
r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
MAT (out, 0, 0) = r0[4]; MAT (out, 0, 1) = r0[5],
MAT (out, 0, 2) = r0[6]; MAT (out, 0, 3) = r0[7],
MAT (out, 1, 0) = r1[4]; MAT (out, 1, 1) = r1[5],
MAT (out, 1, 2) = r1[6]; MAT (out, 1, 3) = r1[7],
MAT (out, 2, 0) = r2[4]; MAT (out, 2, 1) = r2[5],
MAT (out, 2, 2) = r2[6]; MAT (out, 2, 3) = r2[7],
MAT (out, 3, 0) = r3[4]; MAT (out, 3, 1) = r3[5],
MAT (out, 3, 2) = r3[6]; MAT (out, 3, 3) = r3[7];
return TRUE;
}
#undef SWAP_ROWS
/*
* Compute inverse of a general 3d transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
*
* \author Adapted from graphics gems II.
*
* Calculates the inverse of the upper left by first calculating its
* determinant and multiplying it to the symmetric adjust matrix of each
* element. Finally deals with the translation part by transforming the
* original translation vector using by the calculated submatrix inverse.
*/
static gboolean
invert_matrix_3d_general (CoglMatrix *matrix)
{
const float *in = (float *)matrix;
float *out = matrix->inv;
float pos, neg, t;
float det;
/* Calculate the determinant of upper left 3x3 submatrix and
* determine if the matrix is singular.
*/
pos = neg = 0.0;
t = MAT (in,0,0) * MAT (in,1,1) * MAT (in,2,2);
if (t >= 0.0) pos += t; else neg += t;
t = MAT (in,1,0) * MAT (in,2,1) * MAT (in,0,2);
if (t >= 0.0) pos += t; else neg += t;
t = MAT (in,2,0) * MAT (in,0,1) * MAT (in,1,2);
if (t >= 0.0) pos += t; else neg += t;
t = -MAT (in,2,0) * MAT (in,1,1) * MAT (in,0,2);
if (t >= 0.0) pos += t; else neg += t;
t = -MAT (in,1,0) * MAT (in,0,1) * MAT (in,2,2);
if (t >= 0.0) pos += t; else neg += t;
t = -MAT (in,0,0) * MAT (in,2,1) * MAT (in,1,2);
if (t >= 0.0) pos += t; else neg += t;
det = pos + neg;
if (det*det < 1e-25)
return FALSE;
det = 1.0f / det;
MAT (out,0,0) =
( (MAT (in, 1, 1)*MAT (in, 2, 2) - MAT (in, 2, 1)*MAT (in, 1, 2) )*det);
MAT (out,0,1) =
(- (MAT (in, 0, 1)*MAT (in, 2, 2) - MAT (in, 2, 1)*MAT (in, 0, 2) )*det);
MAT (out,0,2) =
( (MAT (in, 0, 1)*MAT (in, 1, 2) - MAT (in, 1, 1)*MAT (in, 0, 2) )*det);
MAT (out,1,0) =
(- (MAT (in,1,0)*MAT (in,2,2) - MAT (in,2,0)*MAT (in,1,2) )*det);
MAT (out,1,1) =
( (MAT (in,0,0)*MAT (in,2,2) - MAT (in,2,0)*MAT (in,0,2) )*det);
MAT (out,1,2) =
(- (MAT (in,0,0)*MAT (in,1,2) - MAT (in,1,0)*MAT (in,0,2) )*det);
MAT (out,2,0) =
( (MAT (in,1,0)*MAT (in,2,1) - MAT (in,2,0)*MAT (in,1,1) )*det);
MAT (out,2,1) =
(- (MAT (in,0,0)*MAT (in,2,1) - MAT (in,2,0)*MAT (in,0,1) )*det);
MAT (out,2,2) =
( (MAT (in,0,0)*MAT (in,1,1) - MAT (in,1,0)*MAT (in,0,1) )*det);
/* Do the translation part */
MAT (out,0,3) = - (MAT (in, 0, 3) * MAT (out, 0, 0) +
MAT (in, 1, 3) * MAT (out, 0, 1) +
MAT (in, 2, 3) * MAT (out, 0, 2) );
MAT (out,1,3) = - (MAT (in, 0, 3) * MAT (out, 1, 0) +
MAT (in, 1, 3) * MAT (out, 1, 1) +
MAT (in, 2, 3) * MAT (out, 1, 2) );
MAT (out,2,3) = - (MAT (in, 0, 3) * MAT (out, 2 ,0) +
MAT (in, 1, 3) * MAT (out, 2, 1) +
MAT (in, 2, 3) * MAT (out, 2, 2) );
return TRUE;
}
/*
* Compute inverse of a 3d transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
*
* If the matrix is not an angle preserving matrix then calls
* invert_matrix_3d_general for the actual calculation. Otherwise calculates
* the inverse matrix analyzing and inverting each of the scaling, rotation and
* translation parts.
*/
static gboolean
invert_matrix_3d (CoglMatrix *matrix)
{
const float *in = (float *)matrix;
float *out = matrix->inv;
memcpy (out, identity, 16 * sizeof (float));
if (!TEST_MAT_FLAGS(matrix, MAT_FLAGS_ANGLE_PRESERVING))
return invert_matrix_3d_general (matrix);
if (matrix->flags & MAT_FLAG_UNIFORM_SCALE)
{
float scale = (MAT (in, 0, 0) * MAT (in, 0, 0) +
MAT (in, 0, 1) * MAT (in, 0, 1) +
MAT (in, 0, 2) * MAT (in, 0, 2));
if (scale == 0.0)
return FALSE;
scale = 1.0f / scale;
/* Transpose and scale the 3 by 3 upper-left submatrix. */
MAT (out, 0, 0) = scale * MAT (in, 0, 0);
MAT (out, 1, 0) = scale * MAT (in, 0, 1);
MAT (out, 2, 0) = scale * MAT (in, 0, 2);
MAT (out, 0, 1) = scale * MAT (in, 1, 0);
MAT (out, 1, 1) = scale * MAT (in, 1, 1);
MAT (out, 2, 1) = scale * MAT (in, 1, 2);
MAT (out, 0, 2) = scale * MAT (in, 2, 0);
MAT (out, 1, 2) = scale * MAT (in, 2, 1);
MAT (out, 2, 2) = scale * MAT (in, 2, 2);
}
else if (matrix->flags & MAT_FLAG_ROTATION)
{
/* Transpose the 3 by 3 upper-left submatrix. */
MAT (out, 0, 0) = MAT (in, 0, 0);
MAT (out, 1, 0) = MAT (in, 0, 1);
MAT (out, 2, 0) = MAT (in, 0, 2);
MAT (out, 0, 1) = MAT (in, 1, 0);
MAT (out, 1, 1) = MAT (in, 1, 1);
MAT (out, 2, 1) = MAT (in, 1, 2);
MAT (out, 0, 2) = MAT (in, 2, 0);
MAT (out, 1, 2) = MAT (in, 2, 1);
MAT (out, 2, 2) = MAT (in, 2, 2);
}
else
{
/* pure translation */
memcpy (out, identity, 16 * sizeof (float));
MAT (out, 0, 3) = - MAT (in, 0, 3);
MAT (out, 1, 3) = - MAT (in, 1, 3);
MAT (out, 2, 3) = - MAT (in, 2, 3);
return TRUE;
}
if (matrix->flags & MAT_FLAG_TRANSLATION)
{
/* Do the translation part */
MAT (out,0,3) = - (MAT (in, 0, 3) * MAT (out, 0, 0) +
MAT (in, 1, 3) * MAT (out, 0, 1) +
MAT (in, 2, 3) * MAT (out, 0, 2) );
MAT (out,1,3) = - (MAT (in, 0, 3) * MAT (out, 1, 0) +
MAT (in, 1, 3) * MAT (out, 1, 1) +
MAT (in, 2, 3) * MAT (out, 1, 2) );
MAT (out,2,3) = - (MAT (in, 0, 3) * MAT (out, 2, 0) +
MAT (in, 1, 3) * MAT (out, 2, 1) +
MAT (in, 2, 3) * MAT (out, 2, 2) );
}
else
MAT (out, 0, 3) = MAT (out, 1, 3) = MAT (out, 2, 3) = 0.0;
return TRUE;
}
/*
* Compute inverse of an identity transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: always %TRUE.
*
* Simply copies identity into CoglMatrix::inv.
*/
static gboolean
invert_matrix_identity (CoglMatrix *matrix)
{
memcpy (matrix->inv, identity, 16 * sizeof (float));
return TRUE;
}
/*
* Compute inverse of a no-rotation 3d transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
*
* Calculates the
*/
static gboolean
invert_matrix_3d_no_rotation (CoglMatrix *matrix)
{
const float *in = (float *)matrix;
float *out = matrix->inv;
if (MAT (in,0,0) == 0 || MAT (in,1,1) == 0 || MAT (in,2,2) == 0)
return FALSE;
memcpy (out, identity, 16 * sizeof (float));
MAT (out,0,0) = 1.0f / MAT (in,0,0);
MAT (out,1,1) = 1.0f / MAT (in,1,1);
MAT (out,2,2) = 1.0f / MAT (in,2,2);
if (matrix->flags & MAT_FLAG_TRANSLATION)
{
MAT (out,0,3) = - (MAT (in,0,3) * MAT (out,0,0));
MAT (out,1,3) = - (MAT (in,1,3) * MAT (out,1,1));
MAT (out,2,3) = - (MAT (in,2,3) * MAT (out,2,2));
}
return TRUE;
}
/*
* Compute inverse of a no-rotation 2d transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
*
* Calculates the inverse matrix by applying the inverse scaling and
* translation to the identity matrix.
*/
static gboolean
invert_matrix_2d_no_rotation (CoglMatrix *matrix)
{
const float *in = (float *)matrix;
float *out = matrix->inv;
if (MAT (in, 0, 0) == 0 || MAT (in, 1, 1) == 0)
return FALSE;
memcpy (out, identity, 16 * sizeof (float));
MAT (out, 0, 0) = 1.0f / MAT (in, 0, 0);
MAT (out, 1, 1) = 1.0f / MAT (in, 1, 1);
if (matrix->flags & MAT_FLAG_TRANSLATION)
{
MAT (out, 0, 3) = - (MAT (in, 0, 3) * MAT (out, 0, 0));
MAT (out, 1, 3) = - (MAT (in, 1, 3) * MAT (out, 1, 1));
}
return TRUE;
}
#if 0
/* broken */
static gboolean
invert_matrix_perspective (CoglMatrix *matrix)
{
const float *in = matrix;
float *out = matrix->inv;
if (MAT (in,2,3) == 0)
return FALSE;
memcpy( out, identity, 16 * sizeof(float) );
MAT (out, 0, 0) = 1.0f / MAT (in, 0, 0);
MAT (out, 1, 1) = 1.0f / MAT (in, 1, 1);
MAT (out, 0, 3) = MAT (in, 0, 2);
MAT (out, 1, 3) = MAT (in, 1, 2);
MAT (out,2,2) = 0;
MAT (out,2,3) = -1;
MAT (out,3,2) = 1.0f / MAT (in,2,3);
MAT (out,3,3) = MAT (in,2,2) * MAT (out,3,2);
return TRUE;
}
#endif
/*
* Matrix inversion function pointer type.
*/
typedef gboolean (*inv_mat_func)(CoglMatrix *matrix);
/*
* Table of the matrix inversion functions according to the matrix type.
*/
static inv_mat_func inv_mat_tab[7] = {
invert_matrix_general,
invert_matrix_identity,
invert_matrix_3d_no_rotation,
#if 0
/* Don't use this function for now - it fails when the projection matrix
* is premultiplied by a translation (ala Chromium's tilesort SPU).
*/
invert_matrix_perspective,
#else
invert_matrix_general,
#endif
invert_matrix_3d, /* lazy! */
invert_matrix_2d_no_rotation,
invert_matrix_3d
};
#define ZERO(x) (1<<x)
#define ONE(x) (1<<(x+16))
#define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
#define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
#define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
ZERO(1) | ZERO(9) | \
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define MASK_2D ( ZERO(8) | \
ZERO(9) | \
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
ZERO(1) | ZERO(9) | \
ZERO(2) | ZERO(6) | \
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define MASK_3D ( \
\
\
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
ZERO(1) | ZERO(13) |\
ZERO(2) | ZERO(6) | \
ZERO(3) | ZERO(7) | ZERO(15) )
#define SQ(x) ((x)*(x))
/*
* Determine type and flags from scratch.
*
* This is expensive enough to only want to do it once.
*/
static void
analyse_from_scratch (CoglMatrix *matrix)
{
const float *m = (float *)matrix;
unsigned int mask = 0;
unsigned int i;
for (i = 0 ; i < 16 ; i++)
{
if (m[i] == 0.0) mask |= (1<<i);
}
if (m[0] == 1.0f) mask |= (1<<16);
if (m[5] == 1.0f) mask |= (1<<21);
if (m[10] == 1.0f) mask |= (1<<26);
if (m[15] == 1.0f) mask |= (1<<31);
matrix->flags &= ~MAT_FLAGS_GEOMETRY;
/* Check for translation - no-one really cares
*/
if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
matrix->flags |= MAT_FLAG_TRANSLATION;
/* Do the real work
*/
if (mask == (unsigned int) MASK_IDENTITY)
matrix->type = COGL_MATRIX_TYPE_IDENTITY;
else if ((mask & MASK_2D_NO_ROT) == (unsigned int) MASK_2D_NO_ROT)
{
matrix->type = COGL_MATRIX_TYPE_2D_NO_ROT;
if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
matrix->flags |= MAT_FLAG_GENERAL_SCALE;
}
else if ((mask & MASK_2D) == (unsigned int) MASK_2D)
{
float mm = DOT2 (m, m);
float m4m4 = DOT2 (m+4,m+4);
float mm4 = DOT2 (m,m+4);
matrix->type = COGL_MATRIX_TYPE_2D;
/* Check for scale */
if (SQ (mm-1) > SQ (1e-6) ||
SQ (m4m4-1) > SQ (1e-6))
matrix->flags |= MAT_FLAG_GENERAL_SCALE;
/* Check for rotation */
if (SQ (mm4) > SQ (1e-6))
matrix->flags |= MAT_FLAG_GENERAL_3D;
else
matrix->flags |= MAT_FLAG_ROTATION;
}
else if ((mask & MASK_3D_NO_ROT) == (unsigned int) MASK_3D_NO_ROT)
{
matrix->type = COGL_MATRIX_TYPE_3D_NO_ROT;
/* Check for scale */
if (SQ (m[0]-m[5]) < SQ (1e-6) &&
SQ (m[0]-m[10]) < SQ (1e-6))
{
if (SQ (m[0]-1.0) > SQ (1e-6))
matrix->flags |= MAT_FLAG_UNIFORM_SCALE;
}
else
matrix->flags |= MAT_FLAG_GENERAL_SCALE;
}
else if ((mask & MASK_3D) == (unsigned int) MASK_3D)
{
float c1 = DOT3 (m,m);
float c2 = DOT3 (m+4,m+4);
float c3 = DOT3 (m+8,m+8);
float d1 = DOT3 (m, m+4);
float cp[3];
matrix->type = COGL_MATRIX_TYPE_3D;
/* Check for scale */
if (SQ (c1-c2) < SQ (1e-6) && SQ (c1-c3) < SQ (1e-6))
{
if (SQ (c1-1.0) > SQ (1e-6))
matrix->flags |= MAT_FLAG_UNIFORM_SCALE;
/* else no scale at all */
}
else
matrix->flags |= MAT_FLAG_GENERAL_SCALE;
/* Check for rotation */
if (SQ (d1) < SQ (1e-6))
{
CROSS3 ( cp, m, m+4);
SUB_3V ( cp, cp, (m+8));
if (LEN_SQUARED_3FV(cp) < SQ(1e-6))
matrix->flags |= MAT_FLAG_ROTATION;
else
matrix->flags |= MAT_FLAG_GENERAL_3D;
}
else
matrix->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
}
else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0f)
{
matrix->type = COGL_MATRIX_TYPE_PERSPECTIVE;
matrix->flags |= MAT_FLAG_GENERAL;
}
else
{
matrix->type = COGL_MATRIX_TYPE_GENERAL;
matrix->flags |= MAT_FLAG_GENERAL;
}
}
/*
* Analyze a matrix given that its flags are accurate.
*
* This is the more common operation, hopefully.
*/
static void
analyse_from_flags (CoglMatrix *matrix)
{
const float *m = (float *)matrix;
if (TEST_MAT_FLAGS(matrix, 0))
matrix->type = COGL_MATRIX_TYPE_IDENTITY;
else if (TEST_MAT_FLAGS(matrix, (MAT_FLAG_TRANSLATION |
MAT_FLAG_UNIFORM_SCALE |
MAT_FLAG_GENERAL_SCALE)))
{
if ( m[10] == 1.0f && m[14] == 0.0f )
matrix->type = COGL_MATRIX_TYPE_2D_NO_ROT;
else
matrix->type = COGL_MATRIX_TYPE_3D_NO_ROT;
}
else if (TEST_MAT_FLAGS (matrix, MAT_FLAGS_3D))
{
if ( m[ 8]==0.0f
&& m[ 9]==0.0f
&& m[2]==0.0f && m[6]==0.0f && m[10]==1.0f && m[14]==0.0f)
{
matrix->type = COGL_MATRIX_TYPE_2D;
}
else
matrix->type = COGL_MATRIX_TYPE_3D;
}
else if ( m[4]==0.0f && m[12]==0.0f
&& m[1]==0.0f && m[13]==0.0f
&& m[2]==0.0f && m[6]==0.0f
&& m[3]==0.0f && m[7]==0.0f && m[11]==-1.0f && m[15]==0.0f)
{
matrix->type = COGL_MATRIX_TYPE_PERSPECTIVE;
}
else
matrix->type = COGL_MATRIX_TYPE_GENERAL;
}
/*
* Analyze and update the type and flags of a matrix.
*
* If the matrix type is dirty then calls either analyse_from_scratch() or
* analyse_from_flags() to determine its type, according to whether the flags
* are dirty or not, respectively. If the matrix has an inverse and it's dirty
* then calls matrix_invert(). Finally clears the dirty flags.
*/
static void
_cogl_matrix_update_type_and_flags (CoglMatrix *matrix)
{
if (matrix->flags & MAT_DIRTY_TYPE)
{
if (matrix->flags & MAT_DIRTY_FLAGS)
analyse_from_scratch (matrix);
else
analyse_from_flags (matrix);
}
matrix->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE);
}
/*
* Compute inverse of a transformation matrix.
*
@ -1029,23 +327,58 @@ _cogl_matrix_update_type_and_flags (CoglMatrix *matrix)
* given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag,
* and copies the identity matrix into CoglMatrix::inv.
*/
static inline gboolean
calculate_inverse (CoglMatrix *matrix)
{
graphene_matrix_t inverse;
graphene_matrix_t scaled;
graphene_matrix_t m;
gboolean invertible;
float pivot = G_MAXFLOAT;
float v[16];
float scale;
cogl_matrix_to_graphene_matrix (matrix, &m);
graphene_matrix_to_float (&m, v);
pivot = MIN (pivot, v[0]);
pivot = MIN (pivot, v[5]);
pivot = MIN (pivot, v[10]);
pivot = MIN (pivot, v[15]);
scale = 1.f / pivot;
graphene_matrix_init_scale (&scaled, scale, scale, scale);
/* Float precision is a limiting factor */
graphene_matrix_multiply (&m, &scaled, &m);
invertible = graphene_matrix_inverse (&m, &inverse);
if (invertible)
graphene_matrix_multiply (&scaled, &inverse, &inverse);
else
graphene_matrix_init_identity (&inverse);
graphene_matrix_to_float (&inverse, matrix->inv);
return invertible;
}
static gboolean
_cogl_matrix_update_inverse (CoglMatrix *matrix)
{
if (matrix->flags & MAT_DIRTY_FLAGS ||
matrix->flags & MAT_DIRTY_INVERSE)
{
_cogl_matrix_update_type_and_flags (matrix);
if (inv_mat_tab[matrix->type](matrix))
if (calculate_inverse (matrix))
matrix->flags &= ~MAT_FLAG_SINGULAR;
else
{
matrix->flags |= MAT_FLAG_SINGULAR;
memcpy (matrix->inv, identity, 16 * sizeof (float));
}
matrix->flags |= MAT_FLAG_SINGULAR;
matrix->flags &= ~MAT_DIRTY_INVERSE;
matrix->flags &= ~(MAT_DIRTY_FLAGS |
MAT_DIRTY_TYPE |
MAT_DIRTY_INVERSE);
}
if (matrix->flags & MAT_FLAG_SINGULAR)