[gtk+/wip/otte/shader: 101/176] gskspvwriter: Allow writing a function with initializer



commit f5753d51c6c134eec9c2e851fec304965c956a65
Author: Benjamin Otte <otte redhat com>
Date:   Sat Oct 7 21:13:18 2017 +0200

    gskspvwriter: Allow writing a function with initializer
    
    That way, we can initialize global variables.
    
    We're also a lot closer to emitting code in the way glslc does.

 gsk/gskslfunction.c        |   29 ++++++++++++++++++++++-------
 gsk/gskslfunctionprivate.h |    4 +++-
 gsk/gskslprogram.c         |   22 +++++++++++++++++-----
 gsk/gsksltypesprivate.h    |    1 +
 gsk/gskspvwriter.c         |   17 +++++++++++++++--
 gsk/gskspvwriterprivate.h  |    4 ++++
 6 files changed, 62 insertions(+), 15 deletions(-)
---
diff --git a/gsk/gskslfunction.c b/gsk/gskslfunction.c
index d4da987..fc7d1c1 100644
--- a/gsk/gskslfunction.c
+++ b/gsk/gskslfunction.c
@@ -56,7 +56,9 @@ struct _GskSlFunctionClass {
   void                  (* print)                               (const GskSlFunction    *function,
                                                                  GskSlPrinter           *printer);
   guint32               (* write_spv)                           (const GskSlFunction    *function,
-                                                                 GskSpvWriter           *writer);
+                                                                 GskSpvWriter           *writer,
+                                                                 GskSpvWriterFunc        initializer,
+                                                                 gpointer                initializer_data);
   guint32               (* write_call_spv)                      (GskSlFunction          *function,
                                                                  GskSpvWriter           *writer,
                                                                  guint32                *arguments);
@@ -146,8 +148,12 @@ gsk_sl_function_constructor_print (const GskSlFunction *function,
 
 static guint32
 gsk_sl_function_constructor_write_spv (const GskSlFunction *function,
-                                       GskSpvWriter        *writer)
+                                       GskSpvWriter        *writer,
+                                       GskSpvWriterFunc     initializer,
+                                       gpointer             initializer_data)
 {
+  g_assert (initializer == NULL);
+
   return 0;
 }
 
@@ -240,8 +246,12 @@ gsk_sl_function_native_print (const GskSlFunction *function,
 
 static guint32
 gsk_sl_function_native_write_spv (const GskSlFunction *function,
-                                  GskSpvWriter        *writer)
+                                  GskSpvWriter        *writer,
+                                  GskSpvWriterFunc     initializer,
+                                  gpointer             initializer_data)
 {
+  g_assert (initializer != NULL);
+
   return 0;
 }
 
@@ -378,7 +388,9 @@ gsk_sl_function_declared_print (const GskSlFunction *function,
 
 static guint32
 gsk_sl_function_declared_write_spv (const GskSlFunction *function,
-                                    GskSpvWriter        *writer)
+                                    GskSpvWriter        *writer,
+                                    GskSpvWriterFunc     initializer,
+                                    gpointer             initializer_data)
 {
   GskSlFunctionDeclared *declared = (GskSlFunctionDeclared *) function;
   guint32 return_type_id, function_type_id, function_id;
@@ -407,7 +419,8 @@ gsk_sl_function_declared_write_spv (const GskSlFunction *function,
     }
 
   /* add function body */
-  gsk_spv_writer_label (writer);
+  if (initializer)
+    initializer (writer, initializer_data);
 
   gsk_sl_statement_write_spv (declared->statement, writer);
 
@@ -659,9 +672,11 @@ gsk_sl_function_print (const GskSlFunction *function,
 
 guint32
 gsk_sl_function_write_spv (const GskSlFunction *function,
-                           GskSpvWriter        *writer)
+                           GskSpvWriter        *writer,
+                           GskSpvWriterFunc     initializer,
+                           gpointer             initializer_data)
 {
-  return function->class->write_spv (function, writer);
+  return function->class->write_spv (function, writer, initializer, initializer_data);
 }
 
 guint32
diff --git a/gsk/gskslfunctionprivate.h b/gsk/gskslfunctionprivate.h
index 9752d44..0890e4b 100644
--- a/gsk/gskslfunctionprivate.h
+++ b/gsk/gskslfunctionprivate.h
@@ -47,7 +47,9 @@ GskSlValue *            gsk_sl_function_get_constant            (const GskSlFunc
                                                                  GskSlValue            **values,
                                                                  gsize                   n_values);
 guint32                 gsk_sl_function_write_spv               (const GskSlFunction    *function,
-                                                                 GskSpvWriter           *writer);
+                                                                 GskSpvWriter           *writer,
+                                                                 GskSpvWriterFunc        initializer,
+                                                                 gpointer                initializer_data);
 guint32                 gsk_sl_function_write_call_spv          (GskSlFunction          *function,
                                                                  GskSpvWriter           *writer,
                                                                  guint32                *arguments);
diff --git a/gsk/gskslprogram.c b/gsk/gskslprogram.c
index 16e1906..1885a82 100644
--- a/gsk/gskslprogram.c
+++ b/gsk/gskslprogram.c
@@ -256,20 +256,32 @@ gsk_sl_program_print (GskSlProgram *program,
 }
 
 static void
-gsk_sl_program_write_spv (GskSlProgram *program,
-                          GskSpvWriter *writer)
+gsk_sl_program_write_spv_initializer (GskSpvWriter *writer,
+                                      gpointer      data)
 {
+  GskSlProgram *program = data;
   GSList *l;
 
   for (l = program->variables; l; l = l->next)
     gsk_spv_writer_get_id_for_variable (writer, l->data);
+}
+
+static void
+gsk_sl_program_write_spv (GskSlProgram *program,
+                          GskSpvWriter *writer)
+{
+  GSList *l;
 
   for (l = program->functions; l; l = l->next)
     {
-      gsk_spv_writer_get_id_for_function (writer, l->data);
-
       if (g_str_equal (gsk_sl_function_get_name (l->data), "main"))
-        gsk_spv_writer_set_entry_point (writer, l->data);
+        {
+          gsk_spv_writer_write_function (writer,
+                                         l->data,
+                                         gsk_sl_program_write_spv_initializer,
+                                         program);
+          gsk_spv_writer_set_entry_point (writer, l->data);
+        }
     }
 }
 
diff --git a/gsk/gsksltypesprivate.h b/gsk/gsksltypesprivate.h
index ff379cc..c1f34b9 100644
--- a/gsk/gsksltypesprivate.h
+++ b/gsk/gsksltypesprivate.h
@@ -37,6 +37,7 @@ typedef struct _GskSlValue              GskSlValue;
 typedef struct _GskSlVariable           GskSlVariable;
 
 typedef struct _GskSpvWriter            GskSpvWriter;
+typedef void (* GskSpvWriterFunc)       (GskSpvWriter *, gpointer);
 
 typedef enum {
   GSK_SL_VOID,
diff --git a/gsk/gskspvwriter.c b/gsk/gskspvwriter.c
index 945ce58..156459f 100644
--- a/gsk/gskspvwriter.c
+++ b/gsk/gskspvwriter.c
@@ -325,15 +325,28 @@ guint32
 gsk_spv_writer_get_id_for_function (GskSpvWriter  *writer,
                                     GskSlFunction *function)
 {
-  GskSpvCodeBlock *block;
   guint32 result;
 
   result = GPOINTER_TO_UINT (g_hash_table_lookup (writer->functions, function));
   if (result != 0)
     return result;
+  
+  return gsk_spv_writer_write_function (writer, function, NULL, NULL);
+}
+
+guint32
+gsk_spv_writer_write_function (GskSpvWriter     *writer,
+                               GskSlFunction    *function,
+                               GskSpvWriterFunc  initializer,
+                               gpointer          initializer_data)
+{
+  GskSpvCodeBlock *block;
+  guint32 result;
+
+  g_assert (g_hash_table_lookup (writer->functions, function) == NULL);
 
   gsk_spv_writer_push_new_code_block (writer);
-  result = gsk_sl_function_write_spv (function, writer);
+  result = gsk_sl_function_write_spv (function, writer, initializer, initializer_data);
   g_hash_table_insert (writer->functions, gsk_sl_function_ref (function), GUINT_TO_POINTER (result));
   block = gsk_spv_writer_pop_code_block (writer);
   writer->pending_blocks = g_slist_prepend (writer->pending_blocks, block);
diff --git a/gsk/gskspvwriterprivate.h b/gsk/gskspvwriterprivate.h
index d1153f0..f411472 100644
--- a/gsk/gskspvwriterprivate.h
+++ b/gsk/gskspvwriterprivate.h
@@ -71,6 +71,10 @@ guint32                 gsk_spv_writer_get_id_for_variable      (GskSpvWriter
                                                                  GskSlVariable          *variable);
 guint32                 gsk_spv_writer_get_id_for_function      (GskSpvWriter           *writer,
                                                                  GskSlFunction          *function);
+guint32                 gsk_spv_writer_write_function           (GskSpvWriter           *writer,
+                                                                 GskSlFunction          *function,
+                                                                 GskSpvWriterFunc        initializer,
+                                                                 gpointer                intializer_data);
 
 guint32                 gsk_spv_writer_make_id                  (GskSpvWriter           *writer);
 GArray *                gsk_spv_writer_get_bytes                (GskSpvWriter           *writer,


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