[gtk+/wip/otte/shader: 6/51] gsksl: Split out GskSlProgram



commit b4226e8e8ec9eebb77696b1d6d7ab77ba23e6af2
Author: Benjamin Otte <otte redhat com>
Date:   Sat Sep 23 22:34:36 2017 +0200

    gsksl: Split out GskSlProgram
    
    And turn it into public API.

 gsk/gsk.h                   |    1 +
 gsk/gskpixelshader.c        |    9 +-
 gsk/gskpixelshaderprivate.h |    4 +-
 gsk/gskslnode.c             |  296 ++++++++++--------------------------------
 gsk/gskslnodeprivate.h      |    9 +-
 gsk/gskslprogram.c          |  170 +++++++++++++++++++++++++
 gsk/gskslprogram.h          |   47 +++++++
 gsk/gsktypes.h              |    1 +
 gsk/meson.build             |    1 +
 gtk/glsl.c                  |   22 ++--
 10 files changed, 309 insertions(+), 251 deletions(-)
---
diff --git a/gsk/gsk.h b/gsk/gsk.h
index 7b2743c..b68c1df 100644
--- a/gsk/gsk.h
+++ b/gsk/gsk.h
@@ -25,6 +25,7 @@
 #include <gsk/gskrenderer.h>
 #include <gsk/gskrendernode.h>
 #include <gsk/gskroundedrect.h>
+#include <gsk/gskslprogram.h>
 #include <gsk/gsktexture.h>
 
 #include <gsk/gsktypes.h>
diff --git a/gsk/gskpixelshader.c b/gsk/gskpixelshader.c
index cec22c0..06aa598 100644
--- a/gsk/gskpixelshader.c
+++ b/gsk/gskpixelshader.c
@@ -34,6 +34,7 @@
 #include "gskpixelshaderprivate.h"
 
 #include "gskdebugprivate.h"
+#include "gskslprogram.h"
 
 #include "gdk/gdkinternals.h"
 
@@ -97,7 +98,7 @@ gsk_pixel_shader_dispose (GObject *object)
 {    
   GskPixelShader *self = GSK_PIXEL_SHADER (object);
 
-  gsk_sl_node_unref (self->program);
+  g_object_unref (self->program);
 
   G_OBJECT_CLASS (gsk_pixel_shader_parent_class)->dispose (object);
 }
@@ -143,11 +144,11 @@ gsk_pixel_shader_new_for_data (GBytes             *source,
                                gpointer            error_data)
 {
   GskPixelShader *shader;
-  GskSlNode *program;
+  GskSlProgram *program;
 
   g_return_val_if_fail (source != NULL, NULL);
 
-  program = gsk_sl_node_new_program (source, NULL);
+  program = gsk_sl_program_new (source, NULL);
   if (program == NULL)
     return NULL;
 
@@ -165,7 +166,7 @@ gsk_pixel_shader_print (GskPixelShader *shader,
   g_return_if_fail (GSK_IS_PIXEL_SHADER (shader));
   g_return_if_fail (string != NULL);
 
-  gsk_sl_node_print (shader->program, string);
+  gsk_sl_program_print (shader->program, string);
 }
 
 char *
diff --git a/gsk/gskpixelshaderprivate.h b/gsk/gskpixelshaderprivate.h
index a10ba06..014a30f 100644
--- a/gsk/gskpixelshaderprivate.h
+++ b/gsk/gskpixelshaderprivate.h
@@ -3,8 +3,6 @@
 
 #include "gskpixelshader.h"
 
-#include "gskslnodeprivate.h"
-
 G_BEGIN_DECLS
 
 #define GSK_PIXEL_SHADER_CLASS(klass)            (G_TYPE_CHECK_CLASS_CAST ((klass), GSK_TYPE_TEXTURE, 
GskPixelShaderClass))
@@ -15,7 +13,7 @@ struct _GskPixelShader
 {
   GObject parent_instance;
 
-  GskSlNode *program;
+  GskSlProgram *program;
 
   guint n_textures;
 };
diff --git a/gsk/gskslnode.c b/gsk/gskslnode.c
index 2319741..238edcb 100644
--- a/gsk/gskslnode.c
+++ b/gsk/gskslnode.c
@@ -166,89 +166,6 @@ static const GskSlNodeClass GSK_SL_NODE_FUNCTION = {
   gsk_sl_node_function_write_spv
 };
 
-/* PROGRAM */
-
-typedef struct _GskSlNodeProgram GskSlNodeProgram;
-
-struct _GskSlNodeProgram {
-  GskSlNode parent;
-
-  GskSlScope *scope;
-  GSList *declarations;
-  GSList *functions;
-};
-
-static void
-gsk_sl_node_program_free (GskSlNode *node)
-{
-  GskSlNodeProgram *program = (GskSlNodeProgram *) node;
-
-  g_slist_free (program->declarations);
-  g_slist_free (program->functions);
-  gsk_sl_scope_unref (program->scope);
-
-  g_slice_free (GskSlNodeProgram, program);
-}
-
-static void
-gsk_sl_node_program_print (GskSlNode *node,
-                           GString   *string)
-{
-  GskSlNodeProgram *program = (GskSlNodeProgram *) node;
-  GSList *l;
-
-  for (l = program->declarations; l; l = l->next)
-    gsk_sl_node_print (l->data, string);
-
-  for (l = program->functions; l; l = l->next)
-    {
-      if (l != program->functions || program->declarations != NULL)
-        g_string_append (string, "\n");
-      gsk_sl_node_print (l->data, string);
-    }
-}
-
-static GskSlType *
-gsk_sl_node_program_get_return_type (GskSlNode *node)
-{
-  return NULL;
-}
-
-static gboolean
-gsk_sl_node_program_is_constant (GskSlNode *node)
-{
-  return TRUE;
-}
-
-static guint32
-gsk_sl_node_program_write_spv (const GskSlNode *node,
-                               GskSpvWriter    *writer)
-{
-  GskSlNodeProgram *program = (GskSlNodeProgram *) node;
-  GSList *l;
-
-  for (l = program->declarations; l; l = l->next)
-    gsk_sl_node_write_spv (l->data, writer);
-
-  for (l = program->functions; l; l = l->next)
-    {
-      guint32 id = gsk_sl_node_write_spv (l->data, writer);
-
-      if (g_str_equal (((GskSlNodeFunction *) l->data)->name, "main"))
-        gsk_spv_writer_set_entry_point (writer, id);
-    }
-
-  return 0;
-}
-
-static const GskSlNodeClass GSK_SL_NODE_PROGRAM = {
-  gsk_sl_node_program_free,
-  gsk_sl_node_program_print,
-  gsk_sl_node_program_get_return_type,
-  gsk_sl_node_program_is_constant,
-  gsk_sl_node_program_write_spv
-};
-
 /* ASSIGNMENT */
 
 typedef struct _GskSlNodeAssignment GskSlNodeAssignment;
@@ -1299,7 +1216,7 @@ static const GskSlNodeClass GSK_SL_NODE_CONSTANT = {
 /* API */
 
 static GskSlNodeFunction *
-gsk_sl_node_parse_function_prototype (GskSlNodeProgram  *program,
+gsk_sl_node_parse_function_prototype (GskSlScope        *scope,
                                       GskSlPreprocessor *stream)
 {
   GskSlType *type;
@@ -1345,13 +1262,11 @@ gsk_sl_node_parse_function_prototype (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_assignment_expression (GskSlNodeProgram  *program,
-                                         GskSlScope        *scope,
+gsk_sl_node_parse_assignment_expression (GskSlScope        *scope,
                                          GskSlPreprocessor *stream);
 
 static GskSlNode *
-gsk_sl_node_parse_constructor_call (GskSlNodeProgram  *program,
-                                    GskSlScope        *scope,
+gsk_sl_node_parse_constructor_call (GskSlScope        *scope,
                                     GskSlPreprocessor *stream,
                                     GskSlType         *type)
 {
@@ -1383,7 +1298,7 @@ gsk_sl_node_parse_constructor_call (GskSlNodeProgram  *program,
       arguments = g_ptr_array_new ();
       while (TRUE)
         {
-          GskSlNode *node = gsk_sl_node_parse_assignment_expression (program, scope, stream);
+          GskSlNode *node = gsk_sl_node_parse_assignment_expression (scope, stream);
 
           if (node != NULL)
             g_ptr_array_add (arguments, node);
@@ -1428,8 +1343,7 @@ gsk_sl_node_parse_constructor_call (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_primary_expression (GskSlNodeProgram  *program,
-                                      GskSlScope        *scope,
+gsk_sl_node_parse_primary_expression (GskSlScope        *scope,
                                       GskSlPreprocessor *stream)
 {
   GskSlNodeConstant *constant;
@@ -1447,7 +1361,7 @@ gsk_sl_node_parse_primary_expression (GskSlNodeProgram  *program,
         if (decl == NULL)
           {
             gsk_sl_preprocessor_error (stream, "No variable named \"%s\".", token->str);
-            gsk_sl_preprocessor_consume (stream, (GskSlNode *) program);
+            gsk_sl_preprocessor_consume (stream, NULL);
             return NULL;
           }
 
@@ -1545,35 +1459,32 @@ gsk_sl_node_parse_primary_expression (GskSlNodeProgram  *program,
         if (type == NULL)
           return NULL;
 
-        return gsk_sl_node_parse_constructor_call (program, scope, stream, type);
+        return gsk_sl_node_parse_constructor_call (scope, stream, type);
       }
 
     default:
       gsk_sl_preprocessor_error (stream, "Expected an expression.");
-      gsk_sl_preprocessor_consume (stream, (GskSlNode *) program);
+      gsk_sl_preprocessor_consume (stream, NULL);
       return NULL;
   }
 }
 
 static GskSlNode *
-gsk_sl_node_parse_postfix_expression (GskSlNodeProgram  *program,
-                                      GskSlScope        *scope,
+gsk_sl_node_parse_postfix_expression (GskSlScope        *scope,
                                       GskSlPreprocessor *stream)
 {
-  return gsk_sl_node_parse_primary_expression (program, scope, stream);
+  return gsk_sl_node_parse_primary_expression (scope, stream);
 }
 
 static GskSlNode *
-gsk_sl_node_parse_unary_expression (GskSlNodeProgram  *program,
-                                    GskSlScope        *scope,
+gsk_sl_node_parse_unary_expression (GskSlScope        *scope,
                                     GskSlPreprocessor *stream)
 {
-  return gsk_sl_node_parse_postfix_expression (program, scope, stream);
+  return gsk_sl_node_parse_postfix_expression (scope, stream);
 }
 
 static GskSlNode *
-gsk_sl_node_parse_multiplicative_expression (GskSlNodeProgram  *program,
-                                             GskSlScope        *scope,
+gsk_sl_node_parse_multiplicative_expression (GskSlScope        *scope,
                                              GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
@@ -1581,7 +1492,7 @@ gsk_sl_node_parse_multiplicative_expression (GskSlNodeProgram  *program,
   GskSlNodeOperation *operation;
   GskSlOperation op;
 
-  node = gsk_sl_node_parse_unary_expression (program, scope, stream);
+  node = gsk_sl_node_parse_unary_expression (scope, stream);
   if (node == NULL)
     return NULL;
 
@@ -1601,7 +1512,7 @@ gsk_sl_node_parse_multiplicative_expression (GskSlNodeProgram  *program,
       operation->left = node;
       operation->op = op;
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) operation);
-      operation->right = gsk_sl_node_parse_unary_expression (program, scope, stream);
+      operation->right = gsk_sl_node_parse_unary_expression (scope, stream);
       if (operation->right == NULL)
         {
           gsk_sl_node_ref (node);
@@ -1630,8 +1541,7 @@ gsk_sl_node_parse_multiplicative_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_additive_expression (GskSlNodeProgram  *program,
-                                       GskSlScope        *scope,
+gsk_sl_node_parse_additive_expression (GskSlScope        *scope,
                                        GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
@@ -1639,7 +1549,7 @@ gsk_sl_node_parse_additive_expression (GskSlNodeProgram  *program,
   GskSlNodeOperation *operation;
   GskSlOperation op;
 
-  node = gsk_sl_node_parse_multiplicative_expression (program, scope, stream);
+  node = gsk_sl_node_parse_multiplicative_expression (scope, stream);
   if (node == NULL)
     return NULL;
 
@@ -1657,7 +1567,7 @@ gsk_sl_node_parse_additive_expression (GskSlNodeProgram  *program,
       operation->left = node;
       operation->op = op;
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) operation);
-      operation->right = gsk_sl_node_parse_additive_expression (program, scope, stream);
+      operation->right = gsk_sl_node_parse_additive_expression (scope, stream);
       if (operation->right == NULL)
         {
           gsk_sl_node_ref (node);
@@ -1681,8 +1591,7 @@ gsk_sl_node_parse_additive_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_shift_expression (GskSlNodeProgram  *program,
-                                    GskSlScope        *scope,
+gsk_sl_node_parse_shift_expression (GskSlScope        *scope,
                                     GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
@@ -1690,7 +1599,7 @@ gsk_sl_node_parse_shift_expression (GskSlNodeProgram  *program,
   GskSlNodeOperation *operation;
   GskSlOperation op;
 
-  node = gsk_sl_node_parse_additive_expression (program, scope, stream);
+  node = gsk_sl_node_parse_additive_expression (scope, stream);
   if (node == NULL)
     return NULL;
 
@@ -1708,7 +1617,7 @@ gsk_sl_node_parse_shift_expression (GskSlNodeProgram  *program,
       operation->left = node;
       operation->op = op;
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) operation);
-      operation->right = gsk_sl_node_parse_additive_expression (program, scope, stream);
+      operation->right = gsk_sl_node_parse_additive_expression (scope, stream);
       if (operation->right == NULL)
         {
           gsk_sl_node_ref (node);
@@ -1731,8 +1640,7 @@ gsk_sl_node_parse_shift_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_relational_expression (GskSlNodeProgram  *program,
-                                         GskSlScope        *scope,
+gsk_sl_node_parse_relational_expression (GskSlScope        *scope,
                                          GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
@@ -1740,7 +1648,7 @@ gsk_sl_node_parse_relational_expression (GskSlNodeProgram  *program,
   GskSlNodeOperation *operation;
   GskSlOperation op;
 
-  node = gsk_sl_node_parse_shift_expression (program, scope, stream);
+  node = gsk_sl_node_parse_shift_expression (scope, stream);
   if (node == NULL)
     return NULL;
 
@@ -1762,7 +1670,7 @@ gsk_sl_node_parse_relational_expression (GskSlNodeProgram  *program,
       operation->left = node;
       operation->op = op;
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) operation);
-      operation->right = gsk_sl_node_parse_shift_expression (program, scope, stream);
+      operation->right = gsk_sl_node_parse_shift_expression (scope, stream);
       if (operation->right == NULL)
         {
           gsk_sl_node_ref (node);
@@ -1785,8 +1693,7 @@ gsk_sl_node_parse_relational_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_equality_expression (GskSlNodeProgram  *program,
-                                       GskSlScope        *scope,
+gsk_sl_node_parse_equality_expression (GskSlScope        *scope,
                                        GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
@@ -1794,7 +1701,7 @@ gsk_sl_node_parse_equality_expression (GskSlNodeProgram  *program,
   GskSlNodeOperation *operation;
   GskSlOperation op;
 
-  node = gsk_sl_node_parse_relational_expression (program, scope, stream);
+  node = gsk_sl_node_parse_relational_expression (scope, stream);
   if (node == NULL)
     return NULL;
 
@@ -1812,7 +1719,7 @@ gsk_sl_node_parse_equality_expression (GskSlNodeProgram  *program,
       operation->left = node;
       operation->op = op;
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) operation);
-      operation->right = gsk_sl_node_parse_relational_expression (program, scope, stream);
+      operation->right = gsk_sl_node_parse_relational_expression (scope, stream);
       if (operation->right == NULL)
         {
           gsk_sl_node_ref (node);
@@ -1828,15 +1735,14 @@ gsk_sl_node_parse_equality_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_and_expression (GskSlNodeProgram  *program,
-                                  GskSlScope        *scope,
+gsk_sl_node_parse_and_expression (GskSlScope        *scope,
                                   GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
   GskSlNode *node;
   GskSlNodeOperation *operation;
 
-  node = gsk_sl_node_parse_equality_expression (program, scope, stream);
+  node = gsk_sl_node_parse_equality_expression (scope, stream);
   if (node == NULL)
     return NULL;
 
@@ -1850,7 +1756,7 @@ gsk_sl_node_parse_and_expression (GskSlNodeProgram  *program,
       operation->left = node;
       operation->op = GSK_SL_OPERATION_AND;
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) operation);
-      operation->right = gsk_sl_node_parse_equality_expression (program, scope, stream);
+      operation->right = gsk_sl_node_parse_equality_expression (scope, stream);
       if (operation->right == NULL)
         {
           gsk_sl_node_ref (node);
@@ -1873,15 +1779,14 @@ gsk_sl_node_parse_and_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_xor_expression (GskSlNodeProgram  *program,
-                                  GskSlScope        *scope,
+gsk_sl_node_parse_xor_expression (GskSlScope        *scope,
                                   GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
   GskSlNode *node;
   GskSlNodeOperation *operation;
 
-  node = gsk_sl_node_parse_and_expression (program, scope, stream);
+  node = gsk_sl_node_parse_and_expression (scope, stream);
   if (node == NULL)
     return NULL;
 
@@ -1895,7 +1800,7 @@ gsk_sl_node_parse_xor_expression (GskSlNodeProgram  *program,
       operation->left = node;
       operation->op = GSK_SL_OPERATION_XOR;
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) operation);
-      operation->right = gsk_sl_node_parse_and_expression (program, scope, stream);
+      operation->right = gsk_sl_node_parse_and_expression (scope, stream);
       if (operation->right == NULL)
         {
           gsk_sl_node_ref (node);
@@ -1918,15 +1823,14 @@ gsk_sl_node_parse_xor_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_or_expression (GskSlNodeProgram  *program,
-                                 GskSlScope        *scope,
+gsk_sl_node_parse_or_expression (GskSlScope        *scope,
                                  GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
   GskSlNode *node;
   GskSlNodeOperation *operation;
 
-  node = gsk_sl_node_parse_xor_expression (program, scope, stream);
+  node = gsk_sl_node_parse_xor_expression (scope, stream);
   if (node == NULL)
     return NULL;
 
@@ -1940,7 +1844,7 @@ gsk_sl_node_parse_or_expression (GskSlNodeProgram  *program,
       operation->left = node;
       operation->op = GSK_SL_OPERATION_OR;
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) operation);
-      operation->right = gsk_sl_node_parse_xor_expression (program, scope, stream);
+      operation->right = gsk_sl_node_parse_xor_expression (scope, stream);
       if (operation->right == NULL)
         {
           gsk_sl_node_ref (node);
@@ -1963,15 +1867,14 @@ gsk_sl_node_parse_or_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_logical_and_expression (GskSlNodeProgram  *program,
-                                          GskSlScope        *scope,
+gsk_sl_node_parse_logical_and_expression (GskSlScope        *scope,
                                           GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
   GskSlNode *node;
   GskSlNodeOperation *operation;
 
-  node = gsk_sl_node_parse_or_expression (program, scope, stream);
+  node = gsk_sl_node_parse_or_expression (scope, stream);
   if (node == NULL)
     return NULL;
 
@@ -1985,7 +1888,7 @@ gsk_sl_node_parse_logical_and_expression (GskSlNodeProgram  *program,
       operation->left = node;
       operation->op = GSK_SL_OPERATION_LOGICAL_AND;
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) operation);
-      operation->right = gsk_sl_node_parse_or_expression (program, scope, stream);
+      operation->right = gsk_sl_node_parse_or_expression (scope, stream);
       if (operation->right == NULL)
         {
           gsk_sl_node_ref (node);
@@ -2018,15 +1921,14 @@ gsk_sl_node_parse_logical_and_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_logical_xor_expression (GskSlNodeProgram  *program,
-                                          GskSlScope        *scope,
+gsk_sl_node_parse_logical_xor_expression (GskSlScope        *scope,
                                           GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
   GskSlNode *node;
   GskSlNodeOperation *operation;
 
-  node = gsk_sl_node_parse_logical_and_expression (program, scope, stream);
+  node = gsk_sl_node_parse_logical_and_expression (scope, stream);
   if (node == NULL)
     return NULL;
 
@@ -2040,7 +1942,7 @@ gsk_sl_node_parse_logical_xor_expression (GskSlNodeProgram  *program,
       operation->left = node;
       operation->op = GSK_SL_OPERATION_LOGICAL_XOR;
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) operation);
-      operation->right = gsk_sl_node_parse_logical_and_expression (program, scope, stream);
+      operation->right = gsk_sl_node_parse_logical_and_expression (scope, stream);
       if (operation->right == NULL)
         {
           gsk_sl_node_ref (node);
@@ -2073,15 +1975,14 @@ gsk_sl_node_parse_logical_xor_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_logical_or_expression (GskSlNodeProgram  *program,
-                                         GskSlScope        *scope,
+gsk_sl_node_parse_logical_or_expression (GskSlScope        *scope,
                                          GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
   GskSlNode *node;
   GskSlNodeOperation *operation;
 
-  node = gsk_sl_node_parse_logical_xor_expression (program, scope, stream);
+  node = gsk_sl_node_parse_logical_xor_expression (scope, stream);
   if (node == NULL)
     return NULL;
 
@@ -2095,7 +1996,7 @@ gsk_sl_node_parse_logical_or_expression (GskSlNodeProgram  *program,
       operation->left = node;
       operation->op = GSK_SL_OPERATION_LOGICAL_OR;
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) operation);
-      operation->right = gsk_sl_node_parse_logical_xor_expression (program, scope, stream);
+      operation->right = gsk_sl_node_parse_logical_xor_expression (scope, stream);
       if (operation->right == NULL)
         {
           gsk_sl_node_ref (node);
@@ -2128,24 +2029,22 @@ gsk_sl_node_parse_logical_or_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_conditional_expression (GskSlNodeProgram  *program,
-                                          GskSlScope        *scope,
+gsk_sl_node_parse_conditional_expression (GskSlScope        *scope,
                                           GskSlPreprocessor *stream)
 {
   /* XXX: support conditionals */
-  return gsk_sl_node_parse_logical_or_expression (program, scope, stream);
+  return gsk_sl_node_parse_logical_or_expression (scope, stream);
 }
 
 static GskSlNode *
-gsk_sl_node_parse_assignment_expression (GskSlNodeProgram  *program,
-                                         GskSlScope        *scope,
+gsk_sl_node_parse_assignment_expression (GskSlScope        *scope,
                                          GskSlPreprocessor *stream)
 {
   const GskSlToken *token;
   GskSlNode *lvalue;
   GskSlNodeAssignment *assign;
 
-  lvalue = gsk_sl_node_parse_conditional_expression (program, scope, stream);
+  lvalue = gsk_sl_node_parse_conditional_expression (scope, stream);
   if (lvalue == NULL)
     return NULL;
 
@@ -2177,7 +2076,7 @@ gsk_sl_node_parse_assignment_expression (GskSlNodeProgram  *program,
       gsk_sl_preprocessor_consume (stream, lvalue);
       gsk_sl_node_unref (lvalue);
 
-      return gsk_sl_node_parse_assignment_expression (program, scope, stream);
+      return gsk_sl_node_parse_assignment_expression (scope, stream);
     }
 
   assign = gsk_sl_node_new (GskSlNodeAssignment, &GSK_SL_NODE_ASSIGNMENT);
@@ -2186,7 +2085,7 @@ gsk_sl_node_parse_assignment_expression (GskSlNodeProgram  *program,
 
   gsk_sl_preprocessor_consume (stream, (GskSlNode *) assign);
 
-  assign->rvalue = gsk_sl_node_parse_assignment_expression (program, scope, stream);
+  assign->rvalue = gsk_sl_node_parse_assignment_expression (scope, stream);
   if (assign->rvalue == NULL)
     {
       gsk_sl_node_unref ((GskSlNode *) assign);
@@ -2197,17 +2096,15 @@ gsk_sl_node_parse_assignment_expression (GskSlNodeProgram  *program,
 }
 
 static GskSlNode *
-gsk_sl_node_parse_expression (GskSlNodeProgram  *program,
-                              GskSlScope        *scope,
+gsk_sl_node_parse_expression (GskSlScope        *scope,
                               GskSlPreprocessor *stream)
 {
   /* XXX: Allow comma here */
-  return gsk_sl_node_parse_assignment_expression (program, scope, stream);
+  return gsk_sl_node_parse_assignment_expression (scope, stream);
 }
 
 static GskSlNode *
-gsk_sl_node_parse_declaration (GskSlNodeProgram  *program,
-                               GskSlScope        *scope,
+gsk_sl_node_parse_declaration (GskSlScope        *scope,
                                GskSlPreprocessor *stream,
                                GskSlPointerType  *type)
 {
@@ -2228,7 +2125,7 @@ gsk_sl_node_parse_declaration (GskSlNodeProgram  *program,
   if (gsk_sl_token_is (token, GSK_SL_TOKEN_EQUAL))
     {
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) declaration);
-      declaration->initial = gsk_sl_node_parse_assignment_expression (program, scope, stream);
+      declaration->initial = gsk_sl_node_parse_assignment_expression (scope, stream);
     }
 
   gsk_sl_scope_add_variable (scope, declaration->name, (GskSlNode *) declaration);
@@ -2236,8 +2133,8 @@ gsk_sl_node_parse_declaration (GskSlNodeProgram  *program,
   return (GskSlNode *) declaration;
 }
 
-static gboolean
-gsk_sl_node_parse_function_definition (GskSlNodeProgram  *program,
+GskSlNode *
+gsk_sl_node_parse_function_definition (GskSlScope        *scope,
                                        GskSlPreprocessor *stream)
 {
   GskSlNodeFunction *function;
@@ -2245,7 +2142,7 @@ gsk_sl_node_parse_function_definition (GskSlNodeProgram  *program,
   GskSlNode *node;
   gboolean result = TRUE;
 
-  function = gsk_sl_node_parse_function_prototype (program, stream);
+  function = gsk_sl_node_parse_function_prototype (scope, stream);
   if (function == NULL)
     return FALSE;
 
@@ -2253,8 +2150,7 @@ gsk_sl_node_parse_function_definition (GskSlNodeProgram  *program,
   if (gsk_sl_token_is (token, GSK_SL_TOKEN_SEMICOLON))
     {
       gsk_sl_preprocessor_consume (stream, (GskSlNode *) function);
-      program->functions = g_slist_prepend (program->functions, function);
-      return TRUE;
+      return (GskSlNode *) function;
     }
 
   if (!gsk_sl_token_is (token, GSK_SL_TOKEN_LEFT_BRACE))
@@ -2265,7 +2161,7 @@ gsk_sl_node_parse_function_definition (GskSlNodeProgram  *program,
     }
   gsk_sl_preprocessor_consume (stream, (GskSlNode *) function);
 
-  function->scope = gsk_sl_scope_new (program->scope);
+  function->scope = gsk_sl_scope_new (scope);
 
   while (TRUE)
     {
@@ -2356,14 +2252,14 @@ gsk_sl_node_parse_function_definition (GskSlNodeProgram  *program,
 
           if (token->type == GSK_SL_TOKEN_LEFT_BRACE)
             {
-              node = gsk_sl_node_parse_constructor_call (program, function->scope, stream, type);
+              node = gsk_sl_node_parse_constructor_call (function->scope, stream, type);
             }
           else
             {
               GskSlPointerType *pointer_type;
           
               pointer_type = gsk_sl_pointer_type_new (type, flags | GSK_SL_POINTER_TYPE_LOCAL);
-              node = gsk_sl_node_parse_declaration (program, function->scope, stream, pointer_type);
+              node = gsk_sl_node_parse_declaration (function->scope, stream, pointer_type);
               gsk_sl_pointer_type_unref (pointer_type);
             }
 
@@ -2389,7 +2285,7 @@ gsk_sl_node_parse_function_definition (GskSlNodeProgram  *program,
           token = gsk_sl_preprocessor_get (stream);
           if (!gsk_sl_token_is (token, GSK_SL_TOKEN_SEMICOLON))
             {
-              return_node->value = gsk_sl_node_parse_expression (program, function->scope, stream);
+              return_node->value = gsk_sl_node_parse_expression (function->scope, stream);
               if (return_node->value == NULL)
                 {
                   gsk_sl_node_unref ((GskSlNode *) return_node);
@@ -2424,7 +2320,7 @@ gsk_sl_node_parse_function_definition (GskSlNodeProgram  *program,
         break;
 
       default:
-        node = gsk_sl_node_parse_expression (program, function->scope, stream);
+        node = gsk_sl_node_parse_expression (function->scope, stream);
         if (node)
           function->statements = g_slist_append (function->statements, node);
         else
@@ -2436,49 +2332,15 @@ gsk_sl_node_parse_function_definition (GskSlNodeProgram  *program,
 out:
   gsk_sl_preprocessor_consume (stream, (GskSlNode *) function);
 
-  program->functions = g_slist_append (program->functions, function);
-  return result;
-}
-
-static gboolean
-gsk_sl_node_parse_program (GskSlNodeProgram  *program,
-                           GskSlPreprocessor *stream)
-{
-  const GskSlToken *token;
-  gboolean result = TRUE;
-
-  for (token = gsk_sl_preprocessor_get (stream);
-       !gsk_sl_token_is (token, GSK_SL_TOKEN_EOF);
-       token = gsk_sl_preprocessor_get (stream))
+  if (result)
     {
-      if (!gsk_sl_node_parse_function_definition (program, stream))
-        {
-          gsk_sl_preprocessor_consume (stream, (GskSlNode *) program);
-          result = FALSE;
-        }
+      return (GskSlNode *) function;
+    }
+  else
+    {
+      gsk_sl_node_unref ((GskSlNode *) function);
+      return NULL;
     }
-
-  return result;
-}
-
-
-GskSlNode *
-gsk_sl_node_new_program (GBytes  *source,
-                         GError **error)
-{
-  GskSlPreprocessor *stream;
-  GskSlNodeProgram *program;
-
-  program = gsk_sl_node_new (GskSlNodeProgram, &GSK_SL_NODE_PROGRAM);
-  program->scope = gsk_sl_scope_new (NULL);
-
-  stream = gsk_sl_preprocessor_new (source);
-
-  gsk_sl_node_parse_program (program, stream);
-
-  gsk_sl_preprocessor_unref (stream);
-
-  return (GskSlNode *) program;
 }
 
 GskSlNode *
@@ -2511,22 +2373,6 @@ gsk_sl_node_print (GskSlNode *node,
   node->class->print (node, string);
 }
 
-GBytes *
-gsk_sl_node_compile (GskSlNode *node)
-{
-  GskSpvWriter *writer;
-  GBytes *bytes;
-
-  writer = gsk_spv_writer_new ();
-
-  gsk_sl_node_write_spv (node, writer);
-  bytes = gsk_spv_writer_write (writer);
-
-  gsk_spv_writer_unref (writer);
-
-  return bytes;
-}
-
 GskSlType *
 gsk_sl_node_get_return_type (GskSlNode *node)
 {
diff --git a/gsk/gskslnodeprivate.h b/gsk/gskslnodeprivate.h
index 43e7d54..ae99dcb 100644
--- a/gsk/gskslnodeprivate.h
+++ b/gsk/gskslnodeprivate.h
@@ -43,19 +43,14 @@ struct _GskSlNodeClass {
                                                                  GskSpvWriter        *writer);
 };
 
-GDK_AVAILABLE_IN_3_92
-GskSlNode *             gsk_sl_node_new_program                 (GBytes              *source,
-                                                                 GError             **error);
+GskSlNode *             gsk_sl_node_parse_function_definition   (GskSlScope          *scope,
+                                                                 GskSlPreprocessor   *preproc);
 
 GskSlNode *             gsk_sl_node_ref                         (GskSlNode           *node);
-GDK_AVAILABLE_IN_3_92
 void                    gsk_sl_node_unref                       (GskSlNode           *node);
 
-GDK_AVAILABLE_IN_3_92
 void                    gsk_sl_node_print                       (GskSlNode           *node,
                                                                  GString             *string);
-GDK_AVAILABLE_IN_3_92
-GBytes *                gsk_sl_node_compile                     (GskSlNode           *node);
 GskSlType *             gsk_sl_node_get_return_type             (GskSlNode           *node);
 gboolean                gsk_sl_node_is_constant                 (GskSlNode           *node);
 
diff --git a/gsk/gskslprogram.c b/gsk/gskslprogram.c
new file mode 100644
index 0000000..af52a39
--- /dev/null
+++ b/gsk/gskslprogram.c
@@ -0,0 +1,170 @@
+/* GTK - The GIMP Toolkit
+ *   
+ * Copyright © 2017 Benjamin Otte <otte gnome org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "gskslprogram.h"
+
+#include "gskslnodeprivate.h"
+#include "gskslpreprocessorprivate.h"
+#include "gskslscopeprivate.h"
+#include "gsksltokenizerprivate.h"
+#include "gskspvwriterprivate.h"
+
+struct _GskSlProgram {
+  GObject parent_instance;
+
+  GskSlScope *scope;
+  GSList *declarations;
+  GSList *functions;
+};
+
+G_DEFINE_TYPE (GskSlProgram, gsk_sl_program, G_TYPE_OBJECT)
+
+static void
+gsk_sl_program_dispose (GObject *object)
+{
+  GskSlProgram *program = GSK_SL_PROGRAM (object);
+
+  g_slist_free (program->declarations);
+  g_slist_free (program->functions);
+  gsk_sl_scope_unref (program->scope);
+
+  G_OBJECT_CLASS (gsk_sl_program_parent_class)->dispose (object);
+}
+
+static void
+gsk_sl_program_class_init (GskSlProgramClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = gsk_sl_program_dispose;
+}
+
+static void
+gsk_sl_program_init (GskSlProgram *program)
+{
+  program->scope = gsk_sl_scope_new (NULL);
+}
+
+static gboolean
+gsk_sl_program_parse (GskSlProgram      *program,
+                      GskSlPreprocessor *preproc)
+{
+  const GskSlToken *token;
+  gboolean result = TRUE;
+
+  for (token = gsk_sl_preprocessor_get (preproc);
+       !gsk_sl_token_is (token, GSK_SL_TOKEN_EOF);
+       token = gsk_sl_preprocessor_get (preproc))
+    {
+      GskSlNode *node = gsk_sl_node_parse_function_definition (program->scope, preproc);
+
+      if (node)
+        {
+          program->functions = g_slist_append (program->functions, node);
+        }
+      else
+        {
+          gsk_sl_preprocessor_consume (preproc, (GskSlNode *) program);
+          result = FALSE;
+        }
+    }
+
+  return result;
+}
+
+GskSlProgram *
+gsk_sl_program_new (GBytes  *source,
+                    GError **error)
+{
+  GskSlPreprocessor *preproc;
+  GskSlProgram *program;
+
+  program = g_object_new (GSK_TYPE_SL_PROGRAM, NULL);
+
+  preproc = gsk_sl_preprocessor_new (source);
+
+  if (!gsk_sl_program_parse (program, preproc))
+    {
+      g_object_unref (program);
+      program = NULL;
+    }
+
+  gsk_sl_preprocessor_unref (preproc);
+
+  return program;
+}
+
+void
+gsk_sl_program_print (GskSlProgram *program,
+                      GString      *string)
+{
+  GSList *l;
+
+  g_return_if_fail (GSK_IS_SL_PROGRAM (program));
+  g_return_if_fail (string != NULL);
+
+  for (l = program->declarations; l; l = l->next)
+    gsk_sl_node_print (l->data, string);
+
+  for (l = program->functions; l; l = l->next)
+    {
+      if (l != program->functions || program->declarations != NULL)
+        g_string_append (string, "\n");
+      gsk_sl_node_print (l->data, string);
+    }
+}
+
+static void
+gsk_sl_program_write_spv (GskSlProgram *program,
+                          GskSpvWriter *writer)
+{
+  GSList *l;
+
+  for (l = program->declarations; l; l = l->next)
+    gsk_sl_node_write_spv (l->data, writer);
+
+#if 0
+  for (l = program->functions; l; l = l->next)
+    {
+      guint32 id = gsk_sl_node_write_spv (l->data, writer);
+
+      if (g_str_equal (((GskSlNodeFunction *) l->data)->name, "main"))
+        gsk_spv_writer_set_entry_point (writer, id);
+    }
+#endif
+}
+
+GBytes *
+gsk_sl_program_to_spirv (GskSlProgram *program)
+{
+  GskSpvWriter *writer;
+  GBytes *bytes;
+
+  g_return_val_if_fail (GSK_IS_SL_PROGRAM (program), NULL);
+
+  writer = gsk_spv_writer_new ();
+
+  gsk_sl_program_write_spv (program, writer);
+  bytes = gsk_spv_writer_write (writer);
+
+  gsk_spv_writer_unref (writer);
+
+  return bytes;
+}
diff --git a/gsk/gskslprogram.h b/gsk/gskslprogram.h
new file mode 100644
index 0000000..0a83679
--- /dev/null
+++ b/gsk/gskslprogram.h
@@ -0,0 +1,47 @@
+/* GTK - The GIMP Toolkit
+ *
+ * Copyright © 2017 Benjamin Otte <otte gnome org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GSK_SL_PROGRAM_H__
+#define __GSK_SL_PROGRAM_H__
+
+#if !defined (__GSK_H_INSIDE__) && !defined (GSK_COMPILATION)
+#error "Only <gsk/gsk.h> can be included directly."
+#endif
+
+#include <gsk/gsktypes.h>
+
+G_BEGIN_DECLS
+
+#define GSK_TYPE_SL_PROGRAM (gsk_sl_program_get_type ())
+
+G_DECLARE_FINAL_TYPE (GskSlProgram, gsk_sl_program, GSK, SL_PROGRAM, GObject)
+
+GDK_AVAILABLE_IN_3_92
+GskSlProgram *          gsk_sl_program_new                      (GBytes              *source,
+                                                                 GError             **error);
+
+GDK_AVAILABLE_IN_3_92
+void                    gsk_sl_program_print                    (GskSlProgram        *program,
+                                                                 GString             *string);
+GDK_AVAILABLE_IN_3_92
+GBytes *                gsk_sl_program_to_spirv                 (GskSlProgram        *program);
+
+
+G_END_DECLS
+
+#endif /* __GSK_SL_PROGRAM_H__ */
diff --git a/gsk/gsktypes.h b/gsk/gsktypes.h
index ff958ab..152ef77 100644
--- a/gsk/gsktypes.h
+++ b/gsk/gsktypes.h
@@ -29,6 +29,7 @@
 typedef struct _GskCodeLocation         GskCodeLocation;
 typedef struct _GskPixelShader          GskPixelShader;
 typedef struct _GskRenderer             GskRenderer;
+typedef struct _GskSlProgram            GskSlProgram;
 typedef struct _GskTexture              GskTexture;
 
 struct _GskCodeLocation
diff --git a/gsk/meson.build b/gsk/meson.build
index 77e988d..b221bf1 100644
--- a/gsk/meson.build
+++ b/gsk/meson.build
@@ -19,6 +19,7 @@ gsk_public_sources = files([
   'gskrendernode.c',
   'gskrendernodeimpl.c',
   'gskroundedrect.c',
+  'gskslprogram.c',
   'gsktexture.c',
 ])
 
diff --git a/gtk/glsl.c b/gtk/glsl.c
index ec5c0ee..78e7cfe 100644
--- a/gtk/glsl.c
+++ b/gtk/glsl.c
@@ -29,8 +29,6 @@
 #include <unistd.h>
 #endif
 
-#include "gsk/gskslnodeprivate.h"
-
 static GBytes *
 bytes_new_from_file (const char  *filename,
                      GError     **error)
@@ -51,7 +49,7 @@ compile (GOutputStream *output,
          const char    *filename)
 {
   GBytes *bytes;
-  GskSlNode *program;
+  GskSlProgram *program;
   GError *error = NULL;
 
   bytes = bytes_new_from_file (filename, &error);
@@ -62,23 +60,23 @@ compile (GOutputStream *output,
       return FALSE;
     }
 
-  program = gsk_sl_node_new_program (bytes, NULL);
+  program = gsk_sl_program_new (bytes, NULL);
   g_bytes_unref (bytes);
   if (program == NULL)
     return FALSE;
 
-  bytes = gsk_sl_node_compile (program);
+  bytes = gsk_sl_program_to_spirv (program);
   if (!g_output_stream_write_all (output, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), NULL, 
NULL, &error))
     {
       g_print (error->message);
       g_error_free (error);
       g_bytes_unref (bytes);
-      gsk_sl_node_unref (program);
+      g_object_unref (program);
       return FALSE;
     }
 
   g_bytes_unref (bytes);
-  gsk_sl_node_unref (program);
+  g_object_unref (program);
 
   return TRUE;
 }
@@ -89,7 +87,7 @@ dump (GOutputStream *output,
 {
   GBytes *bytes;
   GString *string;
-  GskSlNode *program;
+  GskSlProgram *program;
   GError *error = NULL;
 
   bytes = bytes_new_from_file (filename, &error);
@@ -100,24 +98,24 @@ dump (GOutputStream *output,
       return FALSE;
     }
 
-  program = gsk_sl_node_new_program (bytes, NULL);
+  program = gsk_sl_program_new (bytes, NULL);
   g_bytes_unref (bytes);
   if (program == NULL)
     return FALSE;
 
   string = g_string_new (NULL);
-  gsk_sl_node_print (program, string);
+  gsk_sl_program_print (program, string);
   if (!g_output_stream_write_all (output, string->str, string->len, NULL, NULL, &error))
     {
       g_print (error->message);
       g_error_free (error);
       g_string_free (string, TRUE);
-      gsk_sl_node_unref (program);
+      g_object_unref (program);
       return FALSE;
     }
 
   g_string_free (string, TRUE);
-  gsk_sl_node_unref (program);
+  g_object_unref (program);
 
   return TRUE;
 }



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