[Mesa-dev] [PATCH 4/6] mesa: optimize _math_matrix_set_identity() and return a bool result

Brian Paul brianp at vmware.com
Fri Jul 17 17:48:41 PDT 2015


Skip memcpy() calls, etc. if the matrix is already the identity.  Return
true/false to indicate if we're really changing the matrix or not.
---
 src/mesa/math/m_matrix.c | 23 +++++++++++++++--------
 src/mesa/math/m_matrix.h |  2 +-
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/src/mesa/math/m_matrix.c b/src/mesa/math/m_matrix.c
index ecf564c..37c7612 100644
--- a/src/mesa/math/m_matrix.c
+++ b/src/mesa/math/m_matrix.c
@@ -1132,17 +1132,24 @@ _math_matrix_viewport(GLmatrix *m, const double scale[3],
  *
  * Copies ::Identity into \p GLmatrix::m, and into GLmatrix::inv if not NULL.
  * Sets the matrix type to identity, and clear the dirty flags.
+ * \return true if prev matrix wasn't identity, false otherwise
  */
-void
-_math_matrix_set_identity( GLmatrix *mat )
+bool
+_math_matrix_set_identity(GLmatrix *mat)
 {
-   memcpy( mat->m, Identity, 16*sizeof(GLfloat) );
-   memcpy( mat->inv, Identity, 16*sizeof(GLfloat) );
+   if (mat->type != MATRIX_IDENTITY || mat->flags) {
+      memcpy(mat->m, Identity, 16 * sizeof(GLfloat));
+      memcpy(mat->inv, Identity, 16 * sizeof(GLfloat));
+
+      mat->type = MATRIX_IDENTITY;
+      mat->flags = 0;
 
-   mat->type = MATRIX_IDENTITY;
-   mat->flags &= ~(MAT_DIRTY_FLAGS|
-		   MAT_DIRTY_TYPE|
-		   MAT_DIRTY_INVERSE);
+      return true;
+   }
+   else {
+      /* no change */
+      return false;
+   }
 }
 
 /*@}*/
diff --git a/src/mesa/math/m_matrix.h b/src/mesa/math/m_matrix.h
index 778d716..0bb63c8 100644
--- a/src/mesa/math/m_matrix.h
+++ b/src/mesa/math/m_matrix.h
@@ -125,7 +125,7 @@ extern void
 _math_matrix_viewport( GLmatrix *m, const double scale[3],
                        const double translate[3], double depthMax );
 
-extern void
+extern bool
 _math_matrix_set_identity( GLmatrix *dest );
 
 extern void
-- 
1.9.1



More information about the mesa-dev mailing list