[matrix-stack] avoid redundant clearing of matrix when using load_identity
authorRobert Bragg <robert@linux.intel.com>
Tue, 6 Oct 2009 09:52:07 +0000 (10:52 +0100)
committerRobert Bragg <robert@linux.intel.com>
Tue, 20 Oct 2009 11:32:49 +0000 (12:32 +0100)
The journal always uses an identity matrix since it uses software
transformation.  Currently it manually uses glLoadMatrix since previous
experimentation showed that the cogl-matrix-stack gave bad performance, but
it would be nice to fix performance so we only have to care about one path
for loading matrices.

For the common case where we do:
cogl_matrix_stack_push()
cogl_matrix_stack_load_identity()
we were effectively initializing the matrix 3 times. Once due to use of
g_slice_new0, then we had a cogl_matrix_init_identity in
_cogl_matrix_state_new for good measure, and then finally in
cogl_matrix_stack_load_identity we did another cogl_matrix_init_identity.

We don't use g_slice_new0 anymore, _cogl_matrix_state_new is documented as
not initializing the matrix (instead _cogl_matrix_stack_top_mutable now
takes a boolean to choose if new stack entries should be initialised) and so
we now only initialize once in cogl_matrix_stack_load_identity.

clutter/cogl/cogl/cogl-matrix-stack.c

index 39e4e84..c581b2d 100644 (file)
@@ -56,18 +56,14 @@ struct _CoglMatrixStack
   CoglMatrixState *flushed_state;
 };
 
-
+/* XXX: this doesn't initialize the matrix! */
 static CoglMatrixState*
 _cogl_matrix_state_new (void)
 {
   CoglMatrixState *state;
 
-  state = g_slice_new0 (CoglMatrixState);
-
-  /* load identity */
-  cogl_matrix_init_identity (&state->matrix);
-
-  /* state->push_count defaults to 0 */
+  state = g_slice_new (CoglMatrixState);
+  state->push_count = 0;
 
   return state;
 }
@@ -85,8 +81,9 @@ _cogl_matrix_stack_top (CoglMatrixStack *stack)
   return stack->stack->data;
 }
 
-static CoglMatrixState*
-_cogl_matrix_stack_top_mutable (CoglMatrixStack *stack)
+static CoglMatrixState *
+_cogl_matrix_stack_top_mutable (CoglMatrixStack *stack,
+                                gboolean initialize)
 {
   CoglMatrixState *state;
   CoglMatrixState *new_top;
@@ -100,16 +97,15 @@ _cogl_matrix_stack_top_mutable (CoglMatrixStack *stack)
 
   new_top = _cogl_matrix_state_new ();
 
-  new_top->matrix = state->matrix;
-
-  if (stack->flushed_state == state)
+  if (initialize)
     {
-      stack->flushed_state = new_top;
+      new_top->matrix = state->matrix;
+
+      if (stack->flushed_state == state)
+        stack->flushed_state = new_top;
     }
 
-  stack->stack =
-    g_slist_prepend (stack->stack,
-                     new_top);
+  stack->stack = g_slist_prepend (stack->stack, new_top);
 
   return new_top;
 }
@@ -121,10 +117,11 @@ _cogl_matrix_stack_new (void)
   CoglMatrixState *state;
 
   stack = g_slice_new0 (CoglMatrixStack);
+
   state = _cogl_matrix_state_new ();
-  stack->stack =
-    g_slist_prepend (stack->stack,
-                     state);
+  cogl_matrix_init_identity (&state->matrix);
+
+  stack->stack = g_slist_prepend (stack->stack, state);
 
   return stack;
 }
@@ -195,7 +192,8 @@ _cogl_matrix_stack_load_identity (CoglMatrixStack *stack)
 {
   CoglMatrixState *state;
 
-  state = _cogl_matrix_stack_top_mutable (stack);
+  /* XXX: In this case an uninitialized top is acceptable */
+  state = _cogl_matrix_stack_top_mutable (stack, FALSE);
   cogl_matrix_init_identity (&state->matrix);
 
   /* mark dirty */
@@ -210,7 +208,7 @@ _cogl_matrix_stack_scale (CoglMatrixStack *stack,
 {
   CoglMatrixState *state;
 
-  state = _cogl_matrix_stack_top_mutable (stack);
+  state = _cogl_matrix_stack_top_mutable (stack, TRUE);
   cogl_matrix_scale (&state->matrix, x, y, z);
   /* mark dirty */
   stack->flushed_state = NULL;
@@ -224,7 +222,7 @@ _cogl_matrix_stack_translate (CoglMatrixStack *stack,
 {
   CoglMatrixState *state;
 
-  state = _cogl_matrix_stack_top_mutable (stack);
+  state = _cogl_matrix_stack_top_mutable (stack, TRUE);
   cogl_matrix_translate (&state->matrix, x, y, z);
   /* mark dirty */
   stack->flushed_state = NULL;
@@ -239,7 +237,7 @@ _cogl_matrix_stack_rotate (CoglMatrixStack *stack,
 {
   CoglMatrixState *state;
 
-  state = _cogl_matrix_stack_top_mutable (stack);
+  state = _cogl_matrix_stack_top_mutable (stack, TRUE);
   cogl_matrix_rotate (&state->matrix, angle, x, y, z);
   /* mark dirty */
   stack->flushed_state = NULL;
@@ -251,7 +249,7 @@ _cogl_matrix_stack_multiply (CoglMatrixStack  *stack,
 {
   CoglMatrixState *state;
 
-  state = _cogl_matrix_stack_top_mutable (stack);
+  state = _cogl_matrix_stack_top_mutable (stack, TRUE);
   cogl_matrix_multiply (&state->matrix, &state->matrix, matrix);
   /* mark dirty */
   stack->flushed_state = NULL;
@@ -268,7 +266,7 @@ _cogl_matrix_stack_frustum (CoglMatrixStack *stack,
 {
   CoglMatrixState *state;
 
-  state = _cogl_matrix_stack_top_mutable (stack);
+  state = _cogl_matrix_stack_top_mutable (stack, TRUE);
   cogl_matrix_frustum (&state->matrix,
                        left, right, bottom, top,
                        z_near, z_far);
@@ -285,7 +283,7 @@ _cogl_matrix_stack_perspective (CoglMatrixStack *stack,
 {
   CoglMatrixState *state;
 
-  state = _cogl_matrix_stack_top_mutable (stack);
+  state = _cogl_matrix_stack_top_mutable (stack, TRUE);
   cogl_matrix_perspective (&state->matrix,
                            fov_y, aspect, z_near, z_far);
   /* mark dirty */
@@ -303,7 +301,7 @@ _cogl_matrix_stack_ortho (CoglMatrixStack *stack,
 {
   CoglMatrixState *state;
 
-  state = _cogl_matrix_stack_top_mutable (stack);
+  state = _cogl_matrix_stack_top_mutable (stack, TRUE);
   cogl_matrix_ortho (&state->matrix,
                      left, right, bottom, top, z_near, z_far);
   /* mark dirty */
@@ -327,7 +325,7 @@ _cogl_matrix_stack_set (CoglMatrixStack  *stack,
 {
   CoglMatrixState *state;
 
-  state = _cogl_matrix_stack_top_mutable (stack);
+  state = _cogl_matrix_stack_top_mutable (stack, TRUE);
   state->matrix = *matrix;
   /* mark dirty */
   stack->flushed_state = NULL;