[gtk+/wip/otte/shader: 230/269] gskslfunction: Move native function details to native function code



commit d030529543db88df98960a5873e7d9a75f463deb
Author: Benjamin Otte <otte redhat com>
Date:   Fri Oct 13 20:24:17 2017 +0200

    gskslfunction: Move native function details to native function code
    
    Instead of specifying the GskSlNativeFunction struct as the interface,
    just have a more verbose constructor for native functins.
    
    This will allow refactoring at will in the native function code.

 gsk/gskslfunction.c              |   65 +++++++++++++++++++++++++++++++------
 gsk/gskslfunctionprivate.h       |    7 +++-
 gsk/gskslnativefunction.c        |   51 +++++++++++++++++++++++++++++-
 gsk/gskslnativefunctionprivate.h |    8 -----
 4 files changed, 110 insertions(+), 21 deletions(-)
---
diff --git a/gsk/gskslfunction.c b/gsk/gskslfunction.c
index 122e810..6f7964a 100644
--- a/gsk/gskslfunction.c
+++ b/gsk/gskslfunction.c
@@ -189,7 +189,12 @@ typedef struct _GskSlFunctionNative GskSlFunctionNative;
 struct _GskSlFunctionNative {
   GskSlFunction parent;
 
-  const GskSlNativeFunction *native;
+  char *name;
+  GskSlFunctionType *type;
+  void (* get_constant) (gpointer *retval, gpointer *arguments, gpointer user_data);
+  guint32 (* write_spv) (GskSpvWriter *writer, guint32 *arguments, gpointer user_data);
+  gpointer user_data;
+  GDestroyNotify destroy;
 };
 
 static void
@@ -197,6 +202,12 @@ gsk_sl_function_native_free (GskSlFunction *function)
 {
   GskSlFunctionNative *native = (GskSlFunctionNative *) function;
 
+  if (native->destroy)
+    native->destroy (native->user_data);
+
+  gsk_sl_function_type_unref (native->type);
+  g_free (native->name);
+
   g_slice_free (GskSlFunctionNative, native);
 }
 
@@ -205,7 +216,7 @@ gsk_sl_function_native_get_return_type (const GskSlFunction *function)
 {
   const GskSlFunctionNative *native = (const GskSlFunctionNative *) function;
 
-  return gsk_sl_type_get_builtin (native->native->return_type);
+  return gsk_sl_function_type_get_return_type (native->type);
 }
 
 static const char *
@@ -213,7 +224,7 @@ gsk_sl_function_native_get_name (const GskSlFunction *function)
 {
   const GskSlFunctionNative *native = (const GskSlFunctionNative *) function;
 
-  return native->native->name;
+  return native->name;
 }
 
 static gsize
@@ -221,7 +232,7 @@ gsk_sl_function_native_get_n_arguments (const GskSlFunction *function)
 {
   const GskSlFunctionNative *native = (const GskSlFunctionNative *) function;
 
-  return native->native->n_arguments;
+  return gsk_sl_function_type_get_n_arguments (native->type);
 }
 
 static GskSlType *
@@ -230,7 +241,7 @@ gsk_sl_function_native_get_argument_type (const GskSlFunction *function,
 {
   const GskSlFunctionNative *native = (const GskSlFunctionNative *) function;
 
-  return gsk_sl_type_get_builtin (native->native->argument_types[i]);
+  return gsk_sl_function_type_get_argument_type (native->type, i);
 }
 
 static GskSlValue *
@@ -238,7 +249,26 @@ gsk_sl_function_native_get_constant (const GskSlFunction  *function,
                                      GskSlValue          **values,
                                      gsize                 n_values)
 {
-  return NULL;
+  const GskSlFunctionNative *native = (const GskSlFunctionNative *) function;
+  gpointer data[gsk_sl_function_type_get_n_arguments (native->type)];
+  GskSlValue *result;
+  gsize i;
+
+  if (native->get_constant == NULL)
+    return NULL;
+
+  result = gsk_sl_value_new (gsk_sl_function_type_get_return_type (native->type));
+
+  for (i = 0; i < n_values; i++)
+    {
+      data[i] = gsk_sl_value_get_data (values[i]);
+    }
+  
+  native->get_constant (gsk_sl_value_get_data (result),
+                        data,
+                        native->user_data);
+
+  return result;
 }
 
 static void
@@ -253,7 +283,7 @@ gsk_sl_function_native_write_spv (const GskSlFunction *function,
                                   GskSpvWriterFunc     initializer,
                                   gpointer             initializer_data)
 {
-  g_assert (initializer != NULL);
+  g_assert (initializer == NULL);
 
   return 0;
 }
@@ -263,9 +293,9 @@ gsk_sl_function_native_write_call_spv (GskSlFunction *function,
                                        GskSpvWriter  *writer,
                                        guint32       *arguments)
 {
-  g_assert_not_reached ();
+  const GskSlFunctionNative *native = (const GskSlFunctionNative *) function;
 
-  return 0;
+  return native->write_spv (writer, arguments, native->user_data);
 }
 
 static const GskSlFunctionClass GSK_SL_FUNCTION_NATIVE = {
@@ -511,13 +541,26 @@ gsk_sl_function_new_constructor (GskSlType *type)
 }
 
 GskSlFunction *
-gsk_sl_function_new_native (const GskSlNativeFunction *native)
+gsk_sl_function_new_native (const char        *name,
+                            GskSlFunctionType *type,
+                            void               (* get_constant) (gpointer *retval, gpointer *arguments, 
gpointer user_data),
+                            guint32            (* write_spv) (GskSpvWriter *writer, guint32 *arguments, 
gpointer user_data),
+                            gpointer           user_data,
+                            GDestroyNotify     destroy)
 {
   GskSlFunctionNative *function;
 
+  g_return_val_if_fail (name != NULL, NULL);
+  g_return_val_if_fail (write_spv != NULL, NULL);
+
   function = gsk_sl_function_new (GskSlFunctionNative, &GSK_SL_FUNCTION_NATIVE);
 
-  function->native = native;
+  function->name = g_strdup (name);
+  function->type = gsk_sl_function_type_ref (type);
+  function->get_constant = get_constant;
+  function->write_spv = write_spv;
+  function->user_data = user_data;
+  function->destroy = destroy;
 
   return &function->parent;
 }
diff --git a/gsk/gskslfunctionprivate.h b/gsk/gskslfunctionprivate.h
index 0890e4b..a81bdfd 100644
--- a/gsk/gskslfunctionprivate.h
+++ b/gsk/gskslfunctionprivate.h
@@ -26,7 +26,12 @@
 G_BEGIN_DECLS
 
 GskSlFunction *         gsk_sl_function_new_constructor         (GskSlType              *type);
-GskSlFunction *         gsk_sl_function_new_native              (const GskSlNativeFunction *native);
+GskSlFunction *         gsk_sl_function_new_native              (const char             *name,
+                                                                 GskSlFunctionType      *type,
+                                                                 void                    (* get_constant) 
(gpointer *retval, gpointer *arguments, gpointer user_data),
+                                                                 guint32                 (* write_spv) 
(GskSpvWriter *writer, guint32 *arguments, gpointer user_data),
+                                                                 gpointer                user_data,
+                                                                 GDestroyNotify          destroy);
 GskSlFunction *         gsk_sl_function_new_parse               (GskSlScope             *scope,
                                                                  GskSlPreprocessor      *stream,
                                                                  GskSlType              *return_type,
diff --git a/gsk/gskslnativefunction.c b/gsk/gskslnativefunction.c
index 3283a3e..426de0e 100644
--- a/gsk/gskslnativefunction.c
+++ b/gsk/gskslnativefunction.c
@@ -22,8 +22,18 @@
 
 #include "gskslenvironmentprivate.h"
 #include "gskslfunctionprivate.h"
+#include "gskslfunctiontypeprivate.h"
 #include "gskslscopeprivate.h"
+#include "gsksltypeprivate.h"
 
+struct _GskSlNativeFunction
+{
+  const char *name;
+  GskSlBuiltinType return_type;
+  gsize n_arguments;
+  const GskSlBuiltinType *argument_types;
+};
+ 
 #define NATIVE1(type, name, arg1) \
   { name, GSK_SL_BUILTIN_ ## type, 1, (GskSlBuiltinType[1]) { GSK_SL_BUILTIN_ ## arg1 } }
 #define NATIVE2(type, name, arg1, arg2) \
@@ -419,6 +429,33 @@ static const GskSlNativeFunction gsk_glsl_functions_150[] = {
   { NULL }
 };
 
+static GskSlFunctionType *
+gsk_sl_native_functions_construct_type (const GskSlNativeFunction *native)
+{
+  GskSlFunctionType *type;
+  gsize i;
+
+  type = gsk_sl_function_type_new (gsk_sl_type_get_builtin (native->return_type));
+  for (i = 0; i < native->n_arguments; i++)
+    {
+      type = gsk_sl_function_type_add_argument (type,
+                                                GSK_SL_STORAGE_PARAMETER_IN,
+                                                gsk_sl_type_get_builtin (native->argument_types[i]));
+    }
+
+  return type;
+}
+
+static guint32
+gsk_sl_native_functions_write_spv_unimplemented (GskSpvWriter *writer,
+                                                 guint32      *arguments,
+                                                 gpointer      user_data)
+{
+  g_assert_not_reached ();
+
+  return 0;
+}
+
 static void
 gsk_sl_native_functions_add_list (GskSlScope                *scope,
                                   const GskSlNativeFunction *functions)
@@ -427,9 +464,21 @@ gsk_sl_native_functions_add_list (GskSlScope                *scope,
 
   for (i = 0; functions[i].name; i++)
     {
-      GskSlFunction *function = gsk_sl_function_new_native (&functions[i]);
+      GskSlFunctionType *type;
+      GskSlFunction *function;
+
+      type = gsk_sl_native_functions_construct_type (&functions[i]);
+
+      function = gsk_sl_function_new_native (functions[i].name,
+                                             type,
+                                             NULL,
+                                             gsk_sl_native_functions_write_spv_unimplemented,
+                                             (gpointer) &functions[i],
+                                             NULL);
       gsk_sl_scope_add_function (scope, function);
+
       gsk_sl_function_unref (function);
+      gsk_sl_function_type_unref (type);
     }
 }
 
diff --git a/gsk/gskslnativefunctionprivate.h b/gsk/gskslnativefunctionprivate.h
index 02a3ae3..53aa4bc 100644
--- a/gsk/gskslnativefunctionprivate.h
+++ b/gsk/gskslnativefunctionprivate.h
@@ -25,14 +25,6 @@
 
 G_BEGIN_DECLS
 
-struct _GskSlNativeFunction
-{
-  const char *name;
-  GskSlBuiltinType return_type;
-  gsize n_arguments;
-  const GskSlBuiltinType *argument_types;
-};
- 
 void                    gsk_sl_native_functions_add             (GskSlScope           *scope,
                                                                  GskSlEnvironment     *environment);
  


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