[gtk+/wip/otte/shader: 144/200] gsksltype: Add gsk_sl_type_value_equal()



commit 31db74ae360685647c6d6c6c12301e8c12efae02
Author: Benjamin Otte <otte redhat com>
Date:   Tue Oct 3 05:32:11 2017 +0200

    gsksltype: Add gsk_sl_type_value_equal()
    
    Use this in gsk_sl_value_equal().

 gsk/gsksltype.c        |  176 ++++++++++++++++++++++++++++++++++++++++++++++--
 gsk/gsksltypeprivate.h |    3 +
 gsk/gskslvalue.c       |    3 +-
 3 files changed, 174 insertions(+), 8 deletions(-)
---
diff --git a/gsk/gsksltype.c b/gsk/gsksltype.c
index 4b47aeb..677d4f5 100644
--- a/gsk/gsksltype.c
+++ b/gsk/gsksltype.c
@@ -67,6 +67,9 @@ struct _GskSlTypeClass {
   void                  (* print_value)                         (const GskSlType     *type,
                                                                  GskSlPrinter        *printer,
                                                                  gconstpointer        value);
+  gboolean              (* value_equal)                         (const GskSlType     *type,
+                                                                 gconstpointer        a,
+                                                                 gconstpointer        b);
   guint32               (* write_value_spv)                     (GskSlType           *type,
                                                                  GskSpvWriter        *writer,
                                                                  gconstpointer        value);
@@ -300,20 +303,57 @@ bool_to_bool (gpointer target, gconstpointer source)
   *(guint32 *) target = *(const guint32 *) source;
 }
 
+static gboolean
+void_equal (gconstpointer a, gconstpointer b)
+{
+  return FALSE;
+}
+
+static gboolean
+int_equal (gconstpointer a, gconstpointer b)
+{
+  return *(const gint32 *) a == *(const gint32 *) b;
+}
+
+static gboolean
+uint_equal (gconstpointer a, gconstpointer b)
+{
+  return *(const guint32 *) a == *(const guint32 *) b;
+}
+
+static gboolean
+float_equal (gconstpointer a, gconstpointer b)
+{
+  return *(const float *) a == *(const float *) b;
+}
+
+static gboolean
+double_equal (gconstpointer a, gconstpointer b)
+{
+  return *(const double *) a == *(const double *) b;
+}
+
+static gboolean
+bool_equal (gconstpointer a, gconstpointer b)
+{
+  return !*(const guint32 *) a == !*(const guint32 *) b;
+}
+
 #define CONVERSIONS(name) { NULL, name ## _to_float, name ## _to_double, name ## _to_int, name ## _to_uint, 
name ## _to_bool }
 struct {
   const char *name;
   gsize size;
   void (* print_value) (GskSlPrinter *printer, gconstpointer value);
+  gboolean (* value_equal) (gconstpointer a, gconstpointer b);
   void (* convert_value[N_SCALAR_TYPES]) (gpointer target, gconstpointer source);
   guint32 (* write_value_spv) (GskSpvWriter *writer, gconstpointer value);
 } scalar_infos[] = {
-  [GSK_SL_VOID] =   { "void",   0, print_void,   { NULL, },            write_void_spv, },
-  [GSK_SL_FLOAT] =  { "float",  4, print_float,  CONVERSIONS (float),  write_float_spv },
-  [GSK_SL_DOUBLE] = { "double", 8, print_double, CONVERSIONS (double), write_double_spv },
-  [GSK_SL_INT] =    { "int",    4, print_int,    CONVERSIONS (int),    write_int_spv },
-  [GSK_SL_UINT] =   { "uint",   4, print_uint,   CONVERSIONS (uint),   write_uint_spv },
-  [GSK_SL_BOOL] =   { "bool",   4, print_bool,   CONVERSIONS (bool),   write_bool_spv }
+  [GSK_SL_VOID] =   { "void",   0, print_void,   void_equal,   { NULL, },            write_void_spv, },
+  [GSK_SL_FLOAT] =  { "float",  4, print_float,  float_equal,  CONVERSIONS (float),  write_float_spv },
+  [GSK_SL_DOUBLE] = { "double", 8, print_double, double_equal, CONVERSIONS (double), write_double_spv },
+  [GSK_SL_INT] =    { "int",    4, print_int,    int_equal,    CONVERSIONS (int),    write_int_spv },
+  [GSK_SL_UINT] =   { "uint",   4, print_uint,   uint_equal,   CONVERSIONS (uint),   write_uint_spv },
+  [GSK_SL_BOOL] =   { "bool",   4, print_bool,   bool_equal,   CONVERSIONS (bool),   write_bool_spv }
 };
 #undef SIMPLE_CONVERSION
 #undef CONVERSIONS
@@ -432,6 +472,14 @@ gsk_sl_type_void_print_value (const GskSlType *type,
   g_assert_not_reached ();
 }
 
+static gboolean
+gsk_sl_type_void_value_equal (const GskSlType *type,
+                              gconstpointer    a,
+                              gconstpointer    b)
+{
+  return FALSE;
+}
+
 static guint32
 gsk_sl_type_void_write_value_spv (GskSlType     *type,
                                   GskSpvWriter  *writer,
@@ -456,6 +504,7 @@ static const GskSlTypeClass GSK_SL_TYPE_VOID = {
   gsk_sl_type_void_can_convert,
   gsk_sl_type_void_write_spv,
   gsk_sl_type_void_print_value,
+  gsk_sl_type_void_value_equal,
   gsk_sl_type_void_write_value_spv
 };
 
@@ -630,6 +679,16 @@ gsk_sl_type_scalar_print_value (const GskSlType *type,
   scalar_infos[scalar->scalar].print_value (printer, value);
 }
 
+static gboolean
+gsk_sl_type_scalar_value_equal (const GskSlType *type,
+                                gconstpointer    a,
+                                gconstpointer    b)
+{
+  GskSlTypeScalar *scalar = (GskSlTypeScalar *) type;
+
+  return scalar_infos[scalar->scalar].value_equal (a, b);
+}
+
 static guint32
 gsk_sl_type_scalar_write_value_spv (GskSlType     *type,
                                     GskSpvWriter  *writer,
@@ -654,6 +713,7 @@ static const GskSlTypeClass GSK_SL_TYPE_SCALAR = {
   gsk_sl_type_scalar_can_convert,
   gsk_sl_type_scalar_write_spv,
   gsk_sl_type_scalar_print_value,
+  gsk_sl_type_scalar_value_equal,
   gsk_sl_type_scalar_write_value_spv
 };
 
@@ -803,6 +863,29 @@ gsk_sl_type_vector_print_value (const GskSlType *type,
   gsk_sl_printer_append (printer, ")");
 }
 
+static gboolean
+gsk_sl_type_vector_value_equal (const GskSlType *type,
+                                gconstpointer    a,
+                                gconstpointer    b)
+{
+  GskSlTypeVector *vector = (GskSlTypeVector *) type;
+  guint i;
+  const guchar *adata, *bdata;
+
+  adata = a;
+  bdata = b;
+
+  for (i = 0; i < vector->length; i++)
+    {
+      if (!scalar_infos[vector->scalar].value_equal (adata, bdata))
+        return FALSE;
+      adata += scalar_infos[vector->scalar].size;
+      bdata += scalar_infos[vector->scalar].size;
+    }
+
+  return TRUE;
+}
+
 static guint32
 gsk_sl_type_vector_write_value_spv (GskSlType     *type,
                                     GskSpvWriter  *writer,
@@ -851,6 +934,7 @@ static const GskSlTypeClass GSK_SL_TYPE_VECTOR = {
   gsk_sl_type_vector_can_convert,
   gsk_sl_type_vector_write_spv,
   gsk_sl_type_vector_print_value,
+  gsk_sl_type_vector_value_equal,
   gsk_sl_type_vector_write_value_spv
 };
 
@@ -1002,6 +1086,29 @@ gsk_sl_type_matrix_print_value (const GskSlType *type,
   gsk_sl_printer_append (printer, ")");
 }
 
+static gboolean
+gsk_sl_type_matrix_value_equal (const GskSlType *type,
+                                gconstpointer    a,
+                                gconstpointer    b)
+{
+  GskSlTypeMatrix *matrix = (GskSlTypeMatrix *) type;
+  guint i;
+  const guchar *adata, *bdata;
+
+  adata = a;
+  bdata = b;
+
+  for (i = 0; i < matrix->rows * matrix->columns; i++)
+    {
+      if (!scalar_infos[matrix->scalar].value_equal (adata, bdata))
+        return FALSE;
+      adata += scalar_infos[matrix->scalar].size;
+      bdata += scalar_infos[matrix->scalar].size;
+    }
+
+  return TRUE;
+}
+
 static guint32
 gsk_sl_type_matrix_write_value_spv (GskSlType     *type,
                                     GskSpvWriter  *writer,
@@ -1050,6 +1157,7 @@ static const GskSlTypeClass GSK_SL_TYPE_MATRIX = {
   gsk_sl_type_matrix_can_convert,
   gsk_sl_type_matrix_write_spv,
   gsk_sl_type_matrix_print_value,
+  gsk_sl_type_matrix_value_equal,
   gsk_sl_type_matrix_write_value_spv
 };
 
@@ -1201,6 +1309,29 @@ gsk_sl_type_struct_print_value (const GskSlType *type,
   gsk_sl_printer_append (printer, ")");
 }
 
+static gboolean
+gsk_sl_type_struct_value_equal (const GskSlType *type,
+                                gconstpointer    a,
+                                gconstpointer    b)
+{
+  GskSlTypeStruct *struc = (GskSlTypeStruct *) type;
+  guint i;
+  const guchar *adata, *bdata;
+
+  adata = a;
+  bdata = b;
+
+  for (i = 0; i < struc->n_members; i++)
+    {
+      if (!gsk_sl_type_value_equal (struc->members[i].type,
+                                    adata + struc->members[i].offset,
+                                    bdata + struc->members[i].offset))
+        return FALSE;
+    }
+
+  return TRUE;
+}
+
 static guint32
 gsk_sl_type_struct_write_value_spv (GskSlType     *type,
                                     GskSpvWriter  *writer,
@@ -1246,6 +1377,7 @@ static const GskSlTypeClass GSK_SL_TYPE_STRUCT = {
   gsk_sl_type_struct_can_convert,
   gsk_sl_type_struct_write_spv,
   gsk_sl_type_struct_print_value,
+  gsk_sl_type_struct_value_equal,
   gsk_sl_type_struct_write_value_spv
 };
 
@@ -1403,6 +1535,29 @@ gsk_sl_type_block_print_value (const GskSlType *type,
   gsk_sl_printer_append (printer, ")");
 }
 
+static gboolean
+gsk_sl_type_block_value_equal (const GskSlType *type,
+                               gconstpointer    a,
+                               gconstpointer    b)
+{
+  GskSlTypeBlock *block = (GskSlTypeBlock *) type;
+  guint i;
+  const guchar *adata, *bdata;
+
+  adata = a;
+  bdata = b;
+
+  for (i = 0; i < block->n_members; i++)
+    {
+      if (!gsk_sl_type_value_equal (block->members[i].type,
+                                    adata + block->members[i].offset,
+                                    bdata + block->members[i].offset))
+        return FALSE;
+    }
+
+  return TRUE;
+}
+
 static guint32
 gsk_sl_type_block_write_value_spv (GskSlType     *type,
                                    GskSpvWriter  *writer,
@@ -1448,6 +1603,7 @@ static const GskSlTypeClass GSK_SL_TYPE_BLOCK = {
   gsk_sl_type_block_can_convert,
   gsk_sl_type_block_write_spv,
   gsk_sl_type_block_print_value,
+  gsk_sl_type_block_value_equal,
   gsk_sl_type_block_write_value_spv
 };
 
@@ -2250,6 +2406,14 @@ gsk_sl_type_print_value (const GskSlType *type,
   type->class->print_value (type, printer, value);
 }
 
+gboolean
+gsk_sl_type_value_equal (const GskSlType *type,
+                         gconstpointer    a,
+                         gconstpointer    b)
+{
+  return type->class->value_equal (type, a, b);
+}
+
 guint32
 gsk_sl_type_write_value_spv (GskSlType     *type,
                              GskSpvWriter  *writer,
diff --git a/gsk/gsksltypeprivate.h b/gsk/gsksltypeprivate.h
index 3ea19e0..488faed 100644
--- a/gsk/gsksltypeprivate.h
+++ b/gsk/gsksltypeprivate.h
@@ -84,6 +84,9 @@ guint32                 gsk_sl_type_write_spv                   (GskSlType
 void                    gsk_sl_type_print_value                 (const GskSlType     *type,
                                                                  GskSlPrinter        *printer,
                                                                  gconstpointer        value);
+gboolean                gsk_sl_type_value_equal                 (const GskSlType     *type,
+                                                                 gconstpointer        a,
+                                                                 gconstpointer        b);
 guint32                 gsk_sl_type_write_value_spv             (GskSlType           *type,
                                                                  GskSpvWriter        *writer,
                                                                  gconstpointer        value);
diff --git a/gsk/gskslvalue.c b/gsk/gskslvalue.c
index defac16..68cc95b 100644
--- a/gsk/gskslvalue.c
+++ b/gsk/gskslvalue.c
@@ -318,8 +318,7 @@ gsk_sl_value_equal (gconstpointer a_,
   if (!gsk_sl_type_equal (a->type, b->type))
     return FALSE;
 
-  /* XXX: This is wrong */
-  return memcmp (a->data, b->data, gsk_sl_type_get_size (a->type)) == 0;
+  return gsk_sl_type_value_equal (a->type, a->data, b->data);
 }
 
 guint


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