cogl/matrix: Rename and change cogl_matrix_get_array()

Rename cogl_matrix_get_array() to cogl_matrix_to_float(), and
make it copy the floats to an out argument instead of returning
a pointer to the casted CoglMatrix struct.

The naming change is specifically made to match graphene's,
and ease the transition.

https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1439
This commit is contained in:
Georges Basile Stavracas Neto 2020-09-11 09:36:04 -03:00
parent 3324fbb1e3
commit 51094de8c4
8 changed files with 51 additions and 29 deletions

View File

@ -39,10 +39,15 @@ cogl_matrix_progress (const GValue *a,
const CoglMatrix *matrix2 = g_value_get_boxed (b); const CoglMatrix *matrix2 = g_value_get_boxed (b);
graphene_matrix_t m1, m2, interpolated; graphene_matrix_t m1, m2, interpolated;
CoglMatrix res; CoglMatrix res;
float fm1[16];
float fm2[16];
float v[16]; float v[16];
graphene_matrix_init_from_float (&m1, cogl_matrix_get_array (matrix1)); cogl_matrix_to_float (matrix1, fm1);
graphene_matrix_init_from_float (&m2, cogl_matrix_get_array (matrix2)); cogl_matrix_to_float (matrix2, fm2);
graphene_matrix_init_from_float (&m1, fm1);
graphene_matrix_init_from_float (&m2, fm2);
graphene_matrix_interpolate (&m1, &m2, progress, &interpolated); graphene_matrix_interpolate (&m1, &m2, progress, &interpolated);
graphene_matrix_to_float (&interpolated, v); graphene_matrix_to_float (&interpolated, v);

View File

@ -824,9 +824,12 @@ add_uniform:
* Finally, a uniform named "map" and containing a matrix can be set using: * Finally, a uniform named "map" and containing a matrix can be set using:
* *
* |[<!-- language="C" --> * |[<!-- language="C" -->
* float v[16];
*
* cogl_matrix_to_float (&matrix, v);
* clutter_shader_effect_set_uniform (effect, "map", * clutter_shader_effect_set_uniform (effect, "map",
* CLUTTER_TYPE_SHADER_MATRIX, 1, * CLUTTER_TYPE_SHADER_MATRIX,
* cogl_matrix_get_array (&matrix)); * 1, v);
* ]| * ]|
* *
* Since: 1.4 * Since: 1.4

View File

@ -586,10 +586,11 @@ cogl_matrix_free (CoglMatrix *matrix)
g_slice_free (CoglMatrix, matrix); g_slice_free (CoglMatrix, matrix);
} }
const float * void
cogl_matrix_get_array (const CoglMatrix *matrix) cogl_matrix_to_float (const CoglMatrix *matrix,
float *out_array)
{ {
return (float *)matrix; memcpy ((CoglMatrix *) matrix, out_array, 16 * sizeof (float));
} }
float float

View File

@ -480,15 +480,17 @@ cogl_matrix_init_from_matrix (CoglMatrix *matrix,
const CoglMatrix *source); const CoglMatrix *source);
/** /**
* cogl_matrix_get_array: * cogl_matrix_to_float:
* @matrix: A 4x4 transformation matrix * @matrix: A 4x4 transformation matrix
* @out_array: (array fixed-size=16) (out caller-allocates): return location
* for an array of floating point values. The array must be capable of
* holding at least 16 values.
* *
* Casts @matrix to a float array which can be directly passed to OpenGL. * Casts @matrix to a float array which can be directly passed to OpenGL.
*
* Return value: a pointer to the float array
*/ */
COGL_EXPORT const float * COGL_EXPORT void
cogl_matrix_get_array (const CoglMatrix *matrix); cogl_matrix_to_float (const CoglMatrix *matrix,
float *out_array);
/** /**
* cogl_matrix_get_value: * cogl_matrix_get_value:

View File

@ -716,7 +716,7 @@ cogl_pipeline_set_uniform_int (CoglPipeline *pipeline,
* If @transpose is %FALSE then the matrix is expected to be in * If @transpose is %FALSE then the matrix is expected to be in
* column-major order or if it is %TRUE then the matrix is in * column-major order or if it is %TRUE then the matrix is in
* row-major order. You can pass a #CoglMatrix by calling by passing * row-major order. You can pass a #CoglMatrix by calling by passing
* the result of cogl_matrix_get_array() in @value and setting * the result of cogl_matrix_to_float() in @value and setting
* @transpose to %FALSE. * @transpose to %FALSE.
* *
* Since: 2.0 * Since: 2.0

View File

@ -432,10 +432,10 @@ update_constants_cb (CoglPipeline *pipeline,
(state->update_all || unit_state->dirty_texture_matrix)) (state->update_all || unit_state->dirty_texture_matrix))
{ {
const CoglMatrix *matrix; const CoglMatrix *matrix;
const float *array; float array[16];
matrix = _cogl_pipeline_get_layer_matrix (pipeline, layer_index); matrix = _cogl_pipeline_get_layer_matrix (pipeline, layer_index);
array = cogl_matrix_get_array (matrix); cogl_matrix_to_float (matrix, array);
GE (ctx, glUniformMatrix4fv (unit_state->texture_matrix_uniform, GE (ctx, glUniformMatrix4fv (unit_state->texture_matrix_uniform,
1, FALSE, array)); 1, FALSE, array));
unit_state->dirty_texture_matrix = FALSE; unit_state->dirty_texture_matrix = FALSE;
@ -1033,6 +1033,8 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
if (modelview_changed || projection_changed) if (modelview_changed || projection_changed)
{ {
float v[16];
if (program_state->mvp_uniform != -1) if (program_state->mvp_uniform != -1)
need_modelview = need_projection = TRUE; need_modelview = need_projection = TRUE;
else else
@ -1060,16 +1062,22 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
} }
if (projection_changed && program_state->projection_uniform != -1) if (projection_changed && program_state->projection_uniform != -1)
{
cogl_matrix_to_float (&projection, v);
GE (ctx, glUniformMatrix4fv (program_state->projection_uniform, GE (ctx, glUniformMatrix4fv (program_state->projection_uniform,
1, /* count */ 1, /* count */
FALSE, /* transpose */ FALSE, /* transpose */
cogl_matrix_get_array (&projection))); v));
}
if (modelview_changed && program_state->modelview_uniform != -1) if (modelview_changed && program_state->modelview_uniform != -1)
{
cogl_matrix_to_float (&modelview,v);
GE (ctx, glUniformMatrix4fv (program_state->modelview_uniform, GE (ctx, glUniformMatrix4fv (program_state->modelview_uniform,
1, /* count */ 1, /* count */
FALSE, /* transpose */ FALSE, /* transpose */
cogl_matrix_get_array (&modelview))); v));
}
if (program_state->mvp_uniform != -1) if (program_state->mvp_uniform != -1)
{ {
@ -1078,11 +1086,12 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
avoiding the matrix multiplication */ avoiding the matrix multiplication */
if (cogl_matrix_entry_is_identity (modelview_entry)) if (cogl_matrix_entry_is_identity (modelview_entry))
{ {
cogl_matrix_to_float (&projection, v);
GE (ctx, GE (ctx,
glUniformMatrix4fv (program_state->mvp_uniform, glUniformMatrix4fv (program_state->mvp_uniform,
1, /* count */ 1, /* count */
FALSE, /* transpose */ FALSE, /* transpose */
cogl_matrix_get_array (&projection))); v));
} }
else else
{ {
@ -1091,11 +1100,13 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
cogl_matrix_multiply (&combined, cogl_matrix_multiply (&combined,
&projection, &projection,
&modelview); &modelview);
cogl_matrix_to_float (&combined, v);
GE (ctx, GE (ctx,
glUniformMatrix4fv (program_state->mvp_uniform, glUniformMatrix4fv (program_state->mvp_uniform,
1, /* count */ 1, /* count */
FALSE, /* transpose */ FALSE, /* transpose */
cogl_matrix_get_array (&combined))); v));
} }
} }
} }

View File

@ -242,9 +242,7 @@ paint_matrix_pipeline (CoglPipeline *pipeline)
cogl_matrix_transpose (matrices + 3); cogl_matrix_transpose (matrices + 3);
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
memcpy (matrix_floats + i * 16, cogl_matrix_to_float (&matrices[i], &matrix_floats[i * 16]);
cogl_matrix_get_array (matrices + i),
sizeof (float) * 16);
/* Set the first three matrices as transposed */ /* Set the first three matrices as transposed */
uniform_location = uniform_location =

View File

@ -492,6 +492,7 @@ test_vertex_transform_hook (TestState *state)
CoglSnippet *snippet; CoglSnippet *snippet;
CoglMatrix identity_matrix; CoglMatrix identity_matrix;
CoglMatrix matrix; CoglMatrix matrix;
float v[16];
int location; int location;
/* Test the vertex transform hook */ /* Test the vertex transform hook */
@ -513,12 +514,13 @@ test_vertex_transform_hook (TestState *state)
/* Copy the current projection matrix to a uniform */ /* Copy the current projection matrix to a uniform */
cogl_framebuffer_get_projection_matrix (test_fb, &matrix); cogl_framebuffer_get_projection_matrix (test_fb, &matrix);
location = cogl_pipeline_get_uniform_location (pipeline, "pmat"); location = cogl_pipeline_get_uniform_location (pipeline, "pmat");
cogl_matrix_to_float (&matrix, v);
cogl_pipeline_set_uniform_matrix (pipeline, cogl_pipeline_set_uniform_matrix (pipeline,
location, location,
4, /* dimensions */ 4, /* dimensions */
1, /* count */ 1, /* count */
FALSE, /* don't transpose */ FALSE, /* don't transpose */
cogl_matrix_get_array (&matrix)); v);
/* Replace the real projection matrix with the identity. This should /* Replace the real projection matrix with the identity. This should
mess up the drawing unless the snippet replacement is working */ mess up the drawing unless the snippet replacement is working */