[matrix-stack] avoid redundant clearing of matrix when using load_identity

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.
This commit is contained in:
Robert Bragg 2009-10-06 10:52:07 +01:00
parent 2656b569b9
commit 460025d603

View File

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