[mutter/gbsneto/graphene-matrix: 22/57] cogl/matrix: Rename and change cogl_matrix_get_array()




commit c42c0d6a756e5cb96710f5b11ed558f9e29efe50
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Fri Sep 11 09:36:04 2020 -0300

    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

 clutter/clutter/clutter-shader-effect.c          |  7 +++--
 cogl/cogl/cogl-matrix.c                          |  7 +++--
 cogl/cogl/cogl-matrix.h                          | 12 ++++----
 cogl/cogl/cogl-pipeline-state.h                  |  2 +-
 cogl/cogl/driver/gl/cogl-pipeline-progend-glsl.c | 35 ++++++++++++++++--------
 cogl/tests/conform/test-pipeline-uniforms.c      |  4 +--
 cogl/tests/conform/test-snippets.c               |  4 ++-
 7 files changed, 44 insertions(+), 27 deletions(-)
---
diff --git a/clutter/clutter/clutter-shader-effect.c b/clutter/clutter/clutter-shader-effect.c
index be15d32b10..92526b47ee 100644
--- a/clutter/clutter/clutter-shader-effect.c
+++ b/clutter/clutter/clutter-shader-effect.c
@@ -824,9 +824,12 @@ add_uniform:
  * Finally, a uniform named "map" and containing a matrix can be set using:
  *
  * |[<!-- language="C" -->
+ *   float v[16];
+ *
+ *   cogl_matrix_to_float (&matrix, v);
  *   clutter_shader_effect_set_uniform (effect, "map",
- *                                      CLUTTER_TYPE_SHADER_MATRIX, 1,
- *                                      cogl_matrix_get_array (&matrix));
+ *                                      CLUTTER_TYPE_SHADER_MATRIX,
+ *                                      1, v);
  * ]|
  *
  * Since: 1.4
diff --git a/cogl/cogl/cogl-matrix.c b/cogl/cogl/cogl-matrix.c
index f635f70bc8..da611a3e88 100644
--- a/cogl/cogl/cogl-matrix.c
+++ b/cogl/cogl/cogl-matrix.c
@@ -577,10 +577,11 @@ cogl_matrix_free (CoglMatrix *matrix)
   g_slice_free (CoglMatrix, matrix);
 }
 
-const float *
-cogl_matrix_get_array (const CoglMatrix *matrix)
+void
+cogl_matrix_to_float (const CoglMatrix *matrix,
+                      float            *out_array)
 {
-  return (float *)matrix;
+  memcpy (matrix, out_array, 16 * sizeof (float));
 }
 
 float
diff --git a/cogl/cogl/cogl-matrix.h b/cogl/cogl/cogl-matrix.h
index 120d9a7365..b7d396b614 100644
--- a/cogl/cogl/cogl-matrix.h
+++ b/cogl/cogl/cogl-matrix.h
@@ -480,15 +480,17 @@ cogl_matrix_init_from_matrix (CoglMatrix       *matrix,
                               const CoglMatrix *source);
 
 /**
- * cogl_matrix_get_array:
+ * cogl_matrix_to_float:
  * @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.
- *
- * Return value: a pointer to the float array
  */
-COGL_EXPORT const float *
-cogl_matrix_get_array (const CoglMatrix *matrix);
+COGL_EXPORT void
+cogl_matrix_to_float (const CoglMatrix *matrix,
+                      float            *out_array);
 
 /**
  * cogl_matrix_get_value:
diff --git a/cogl/cogl/cogl-pipeline-state.h b/cogl/cogl/cogl-pipeline-state.h
index ceec55fc98..1745ae0499 100644
--- a/cogl/cogl/cogl-pipeline-state.h
+++ b/cogl/cogl/cogl-pipeline-state.h
@@ -716,7 +716,7 @@ cogl_pipeline_set_uniform_int (CoglPipeline *pipeline,
  * 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
  * 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.
  *
  * Since: 2.0
diff --git a/cogl/cogl/driver/gl/cogl-pipeline-progend-glsl.c 
b/cogl/cogl/driver/gl/cogl-pipeline-progend-glsl.c
index b9b6048fd9..e00db212f4 100644
--- a/cogl/cogl/driver/gl/cogl-pipeline-progend-glsl.c
+++ b/cogl/cogl/driver/gl/cogl-pipeline-progend-glsl.c
@@ -432,10 +432,10 @@ update_constants_cb (CoglPipeline *pipeline,
       (state->update_all || unit_state->dirty_texture_matrix))
     {
       const CoglMatrix *matrix;
-      const float *array;
+      float array[16];
 
       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,
                                    1, FALSE, array));
       unit_state->dirty_texture_matrix = FALSE;
@@ -1033,6 +1033,8 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
 
   if (modelview_changed || projection_changed)
     {
+      float v[16];
+
       if (program_state->mvp_uniform != -1)
         need_modelview = need_projection = TRUE;
       else
@@ -1060,16 +1062,22 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
         }
 
       if (projection_changed && program_state->projection_uniform != -1)
-        GE (ctx, glUniformMatrix4fv (program_state->projection_uniform,
-                                     1, /* count */
-                                     FALSE, /* transpose */
-                                     cogl_matrix_get_array (&projection)));
+        {
+          cogl_matrix_to_float (&projection, v);
+          GE (ctx, glUniformMatrix4fv (program_state->projection_uniform,
+                                       1, /* count */
+                                       FALSE, /* transpose */
+                                       v));
+        }
 
       if (modelview_changed && program_state->modelview_uniform != -1)
-        GE (ctx, glUniformMatrix4fv (program_state->modelview_uniform,
-                                     1, /* count */
-                                     FALSE, /* transpose */
-                                     cogl_matrix_get_array (&modelview)));
+        {
+          cogl_matrix_to_float (&modelview,v);
+          GE (ctx, glUniformMatrix4fv (program_state->modelview_uniform,
+                                       1, /* count */
+                                       FALSE, /* transpose */
+                                       v));
+        }
 
       if (program_state->mvp_uniform != -1)
         {
@@ -1078,11 +1086,12 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
              avoiding the matrix multiplication */
           if (cogl_matrix_entry_is_identity (modelview_entry))
             {
+              cogl_matrix_to_float (&projection, v);
               GE (ctx,
                   glUniformMatrix4fv (program_state->mvp_uniform,
                                       1, /* count */
                                       FALSE, /* transpose */
-                                      cogl_matrix_get_array (&projection)));
+                                      v));
             }
           else
             {
@@ -1091,11 +1100,13 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
               cogl_matrix_multiply (&combined,
                                     &projection,
                                     &modelview);
+              cogl_matrix_to_float (&combined, v);
+
               GE (ctx,
                   glUniformMatrix4fv (program_state->mvp_uniform,
                                       1, /* count */
                                       FALSE, /* transpose */
-                                      cogl_matrix_get_array (&combined)));
+                                      v));
             }
         }
     }
diff --git a/cogl/tests/conform/test-pipeline-uniforms.c b/cogl/tests/conform/test-pipeline-uniforms.c
index a02f5d4464..fe08cd0536 100644
--- a/cogl/tests/conform/test-pipeline-uniforms.c
+++ b/cogl/tests/conform/test-pipeline-uniforms.c
@@ -242,9 +242,7 @@ paint_matrix_pipeline (CoglPipeline *pipeline)
   cogl_matrix_transpose (matrices + 3);
 
   for (i = 0; i < 4; i++)
-    memcpy (matrix_floats + i * 16,
-            cogl_matrix_get_array (matrices + i),
-            sizeof (float) * 16);
+    cogl_matrix_to_float (&matrices[i], &matrix_floats[i * 16]);
 
   /* Set the first three matrices as transposed */
   uniform_location =
diff --git a/cogl/tests/conform/test-snippets.c b/cogl/tests/conform/test-snippets.c
index 9b19b221bf..ea0582dc82 100644
--- a/cogl/tests/conform/test-snippets.c
+++ b/cogl/tests/conform/test-snippets.c
@@ -492,6 +492,7 @@ test_vertex_transform_hook (TestState *state)
   CoglSnippet *snippet;
   CoglMatrix identity_matrix;
   CoglMatrix matrix;
+  float v[16];
   int location;
 
   /* Test the vertex transform hook */
@@ -513,12 +514,13 @@ test_vertex_transform_hook (TestState *state)
   /* Copy the current projection matrix to a uniform */
   cogl_framebuffer_get_projection_matrix (test_fb, &matrix);
   location = cogl_pipeline_get_uniform_location (pipeline, "pmat");
+  cogl_matrix_to_float (&matrix, v);
   cogl_pipeline_set_uniform_matrix (pipeline,
                                     location,
                                     4, /* dimensions */
                                     1, /* count */
                                     FALSE, /* don't transpose */
-                                    cogl_matrix_get_array (&matrix));
+                                    v);
 
   /* Replace the real projection matrix with the identity. This should
      mess up the drawing unless the snippet replacement is working */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]