[gtk+/wip/otte/shader: 24/98] gsksltype: Add gsk_sl_type_get_matching()



commit b7c71bd86f27e2cd243802b468fbecc789a10c2a
Author: Benjamin Otte <otte redhat com>
Date:   Wed Oct 4 16:57:32 2017 +0200

    gsksltype: Add gsk_sl_type_get_matching()
    
    Returns the matching basic type for a different scalar type, ie returns
    bvec3 for boolean vec3 or dmat3x4 for double mat3x4.
    
    If the function is called when no matching type exists, it will explode
    in your face.

 gsk/gskslexpression.c  |   10 ++++------
 gsk/gsksltype.c        |   18 ++++++++++++++++++
 gsk/gsksltypeprivate.h |    2 ++
 gsk/gskslvalue.c       |   13 +------------
 4 files changed, 25 insertions(+), 18 deletions(-)
---
diff --git a/gsk/gskslexpression.c b/gsk/gskslexpression.c
index 0d31506..3347abb 100644
--- a/gsk/gskslexpression.c
+++ b/gsk/gskslexpression.c
@@ -496,18 +496,16 @@ gsk_sl_expression_multiplication_write_spv (const GskSlExpression *expression,
   left_id = gsk_sl_expression_write_spv (multiplication->left, writer);
   if (gsk_sl_type_get_scalar_type (ltype) != gsk_sl_type_get_scalar_type (multiplication->type))
     {
-      GskSlType *new_type = gsk_sl_type_get_matrix (gsk_sl_type_get_scalar_type (multiplication->type),
-                                                    gsk_sl_type_get_length (ltype),
-                                                    gsk_sl_type_get_length (gsk_sl_type_get_index_type 
(ltype)));
+      GskSlType *new_type = gsk_sl_type_get_matching (ltype, gsk_sl_type_get_scalar_type 
(multiplication->type));
       left_id = gsk_spv_writer_add_conversion (writer, left_id, ltype, new_type);
+      ltype = new_type;
     }
   right_id = gsk_sl_expression_write_spv (multiplication->right, writer);
   if (gsk_sl_type_get_scalar_type (rtype) != gsk_sl_type_get_scalar_type (multiplication->type))
     {
-      GskSlType *new_type = gsk_sl_type_get_matrix (gsk_sl_type_get_scalar_type (multiplication->type),
-                                                    gsk_sl_type_get_length (rtype),
-                                                    gsk_sl_type_get_length (gsk_sl_type_get_index_type 
(rtype)));
+      GskSlType *new_type = gsk_sl_type_get_matching (rtype, gsk_sl_type_get_scalar_type 
(multiplication->type));
       right_id = gsk_spv_writer_add_conversion (writer, right_id, rtype, new_type);
+      rtype = new_type;
     }
 
   result_type_id = gsk_spv_writer_get_id_for_type (writer, multiplication->type);
diff --git a/gsk/gsksltype.c b/gsk/gsksltype.c
index 2b65db7..00130e8 100644
--- a/gsk/gsksltype.c
+++ b/gsk/gsksltype.c
@@ -1882,6 +1882,24 @@ gsk_sl_type_get_builtin (GskSlBuiltinType builtin)
 }
 
 GskSlType *
+gsk_sl_type_get_matching (GskSlType       *type,
+                          GskSlScalarType  scalar)
+{
+  if (gsk_sl_type_is_scalar (type))
+    return gsk_sl_type_get_scalar (scalar);
+  else if (gsk_sl_type_is_vector (type))
+    return gsk_sl_type_get_vector (scalar, gsk_sl_type_get_length (type));
+  else if (gsk_sl_type_is_matrix (type))
+    return gsk_sl_type_get_matrix (scalar,
+                                   gsk_sl_type_get_length (type),
+                                   gsk_sl_type_get_length (gsk_sl_type_get_index_type (type)));
+  else
+    {
+      g_return_val_if_reached (gsk_sl_type_get_void ());
+    }
+}
+
+GskSlType *
 gsk_sl_type_new_parse (GskSlScope        *scope,
                        GskSlPreprocessor *preproc)
 {
diff --git a/gsk/gsksltypeprivate.h b/gsk/gsksltypeprivate.h
index 488faed..dae1059 100644
--- a/gsk/gsksltypeprivate.h
+++ b/gsk/gsksltypeprivate.h
@@ -39,6 +39,8 @@ GskSlType *             gsk_sl_type_get_matrix                  (GskSlScalarType
                                                                  guint                columns,
                                                                  guint                rows);
 GskSlType *             gsk_sl_type_get_builtin                 (GskSlBuiltinType     builtin);
+GskSlType *             gsk_sl_type_get_matching                (GskSlType           *type,
+                                                                 GskSlScalarType      scalar);
 
 GskSlType *             gsk_sl_type_ref                         (GskSlType           *type);
 void                    gsk_sl_type_unref                       (GskSlType           *type);
diff --git a/gsk/gskslvalue.c b/gsk/gskslvalue.c
index 68cc95b..f930f98 100644
--- a/gsk/gskslvalue.c
+++ b/gsk/gskslvalue.c
@@ -197,18 +197,7 @@ gsk_sl_value_convert_components (GskSlValue      *source,
   if (sscalar == scalar)
     return source;
 
-  if (gsk_sl_type_is_scalar (source->type))
-    result_type = gsk_sl_type_get_scalar (scalar);
-  else if (gsk_sl_type_is_vector (source->type))
-    result_type = gsk_sl_type_get_vector (scalar, gsk_sl_type_get_length (source->type));
-  else if (gsk_sl_type_is_matrix (source->type))
-    result_type = gsk_sl_type_get_matrix (scalar,
-                                          gsk_sl_type_get_length (source->type),
-                                          gsk_sl_type_get_length (gsk_sl_type_get_index_type 
(source->type)));
-  else
-    {
-      g_return_val_if_reached (NULL);
-    }
+  result_type = gsk_sl_type_get_matching (source->type, scalar);
 
   result = gsk_sl_value_new (result_type);
 


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