[gtk+/wip/otte/shader: 10/55] gsksltype: Add gsk_sl_type_get_index_stride()



commit cfe724e611aa421ad29723d65301d7dea136b4b3
Author: Benjamin Otte <otte redhat com>
Date:   Wed Sep 27 03:12:55 2017 +0200

    gsksltype: Add gsk_sl_type_get_index_stride()
    
    This allows operations on GskSlValues that are arrays, because one can
    just do code like:
      data = gsk_sl_value_get_data (value);
      stride = gsk_sl_value_get_index_stride (type);
      for (i = 0; i < gsk_sl_value_get_length (type); i++)
        {
          do_stuff_with_data (data + i * stride);
        }

 gsk/gsksltype.c        |   32 ++++++++++++++++++++++++++++++++
 gsk/gsksltypeprivate.h |    1 +
 gsk/gskslvalue.c       |    4 ++--
 3 files changed, 35 insertions(+), 2 deletions(-)
---
diff --git a/gsk/gsksltype.c b/gsk/gsksltype.c
index b469c0a..60101c5 100644
--- a/gsk/gsksltype.c
+++ b/gsk/gsksltype.c
@@ -44,6 +44,7 @@ struct _GskSlTypeClass {
   const char *          (* get_name)                            (GskSlType           *type);
   GskSlScalarType       (* get_scalar_type)                     (GskSlType           *type);
   GskSlType *           (* get_index_type)                      (GskSlType           *type);
+  gsize                 (* get_index_stride)                    (GskSlType           *type);
   guint                 (* get_length)                          (GskSlType           *type);
   gsize                 (* get_size)                            (GskSlType           *type);
   gboolean              (* can_convert)                         (GskSlType           *target,
@@ -343,6 +344,12 @@ gsk_sl_type_scalar_get_index_type (GskSlType *type)
   return NULL;
 }
 
+static gsize
+gsk_sl_type_scalar_get_index_stride (GskSlType *type)
+{
+  return 0;
+}
+
 static guint
 gsk_sl_type_scalar_get_length (GskSlType *type)
 {
@@ -466,6 +473,7 @@ static const GskSlTypeClass GSK_SL_TYPE_SCALAR = {
   gsk_sl_type_scalar_get_name,
   gsk_sl_type_scalar_get_scalar_type,
   gsk_sl_type_scalar_get_index_type,
+  gsk_sl_type_scalar_get_index_stride,
   gsk_sl_type_scalar_get_length,
   gsk_sl_type_scalar_get_size,
   gsk_sl_type_scalar_can_convert,
@@ -516,6 +524,14 @@ gsk_sl_type_vector_get_index_type (GskSlType *type)
   return gsk_sl_type_get_scalar (vector->scalar);
 }
 
+static gsize
+gsk_sl_type_vector_get_index_stride (GskSlType *type)
+{
+  GskSlTypeVector *vector = (GskSlTypeVector *) type;
+
+  return scalar_infos[vector->scalar].size;
+}
+
 static guint
 gsk_sl_type_vector_get_length (GskSlType *type)
 {
@@ -630,6 +646,7 @@ static const GskSlTypeClass GSK_SL_TYPE_VECTOR = {
   gsk_sl_type_vector_get_name,
   gsk_sl_type_vector_get_scalar_type,
   gsk_sl_type_vector_get_index_type,
+  gsk_sl_type_vector_get_index_stride,
   gsk_sl_type_vector_get_length,
   gsk_sl_type_vector_get_size,
   gsk_sl_type_vector_can_convert,
@@ -681,6 +698,14 @@ gsk_sl_type_matrix_get_index_type (GskSlType *type)
   return gsk_sl_type_get_vector (matrix->scalar, matrix->rows);
 }
 
+static gsize
+gsk_sl_type_matrix_get_index_stride (GskSlType *type)
+{
+  GskSlTypeMatrix *matrix = (GskSlTypeMatrix *) type;
+
+  return scalar_infos[matrix->scalar].size * matrix->rows;
+}
+
 static guint
 gsk_sl_type_matrix_get_length (GskSlType *type)
 {
@@ -796,6 +821,7 @@ static const GskSlTypeClass GSK_SL_TYPE_MATRIX = {
   gsk_sl_type_matrix_get_name,
   gsk_sl_type_matrix_get_scalar_type,
   gsk_sl_type_matrix_get_index_type,
+  gsk_sl_type_matrix_get_index_stride,
   gsk_sl_type_matrix_get_length,
   gsk_sl_type_matrix_get_size,
   gsk_sl_type_matrix_can_convert,
@@ -1118,6 +1144,12 @@ gsk_sl_type_get_index_type (const GskSlType *type)
   return type->class->get_index_type (type);
 }
 
+gsize
+gsk_sl_type_get_index_stride (const GskSlType *type)
+{
+  return type->class->get_index_stride (type);
+}
+
 guint
 gsk_sl_type_get_length (const GskSlType *type)
 {
diff --git a/gsk/gsksltypeprivate.h b/gsk/gsksltypeprivate.h
index 7b3a757..dcadc3b 100644
--- a/gsk/gsksltypeprivate.h
+++ b/gsk/gsksltypeprivate.h
@@ -52,6 +52,7 @@ gboolean                gsk_sl_type_is_matrix                   (const GskSlType
 const char *            gsk_sl_type_get_name                    (const GskSlType     *type);
 GskSlScalarType         gsk_sl_type_get_scalar_type             (const GskSlType     *type);
 GskSlType *             gsk_sl_type_get_index_type              (const GskSlType     *type);
+gsize                   gsk_sl_type_get_index_stride            (const GskSlType     *type);
 guint                   gsk_sl_type_get_length                  (const GskSlType     *type);
 gsize                   gsk_sl_type_get_size                    (const GskSlType     *type);
 gboolean                gsk_sl_scalar_type_can_convert          (GskSlScalarType      target,
diff --git a/gsk/gskslvalue.c b/gsk/gskslvalue.c
index b1eacda..ae00ff8 100644
--- a/gsk/gskslvalue.c
+++ b/gsk/gskslvalue.c
@@ -113,8 +113,8 @@ gsk_sl_value_new_convert (GskSlValue *source,
       result = gsk_sl_value_new (new_type);
       sdata = source->data;
       ddata = result->data;
-      sstride = gsk_sl_type_get_size (source->type) / n;
-      dstride = gsk_sl_type_get_size (new_type) / n;
+      sstride = gsk_sl_type_get_index_stride (source->type);
+      dstride = gsk_sl_type_get_index_stride (new_type);
       for (i = 0; i < n; i++)
         {
           gsk_sl_scalar_type_convert_value (gsk_sl_type_get_scalar_type (new_type),


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