[gtk+/wip/otte/shader: 18/150] gsksl: Split out GskSlProgram



commit 72fb88563f10c6a4ef32e96f43db9339ce322491
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 89e41ed..eed3d28 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;
@@ -1301,7 +1218,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;
@@ -1347,13 +1264,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)
 {
@@ -1385,7 +1300,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);
@@ -1430,8 +1345,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;
@@ -1449,7 +1363,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;
           }
 
@@ -1547,35 +1461,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;
@@ -1583,7 +1494,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;
 
@@ -1603,7 +1514,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);
@@ -1632,8 +1543,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;
@@ -1641,7 +1551,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;
 
@@ -1659,7 +1569,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);
@@ -1683,8 +1593,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;
@@ -1692,7 +1601,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;
 
@@ -1710,7 +1619,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);
@@ -1733,8 +1642,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;
@@ -1742,7 +1650,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;
 
@@ -1764,7 +1672,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);
@@ -1787,8 +1695,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;
@@ -1796,7 +1703,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;
 
@@ -1814,7 +1721,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);
@@ -1830,15 +1737,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;
 
@@ -1852,7 +1758,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);
@@ -1875,15 +1781,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;
 
@@ -1897,7 +1802,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);
@@ -1920,15 +1825,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;
 
@@ -1942,7 +1846,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);
@@ -1965,15 +1869,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;
 
@@ -1987,7 +1890,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);
@@ -2020,15 +1923,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;
 
@@ -2042,7 +1944,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);
@@ -2075,15 +1977,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;
 
@@ -2097,7 +1998,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);
@@ -2130,24 +2031,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;
 
@@ -2179,7 +2078,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);
@@ -2188,7 +2087,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);
@@ -2199,17 +2098,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)
 {
@@ -2230,7 +2127,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);
@@ -2238,8 +2135,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;
@@ -2247,7 +2144,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;
 
@@ -2255,8 +2152,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))
@@ -2267,7 +2163,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)
     {
@@ -2358,14 +2254,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);
             }
 
@@ -2391,7 +2287,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);
@@ -2426,7 +2322,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
@@ -2438,49 +2334,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 *
@@ -2513,22 +2375,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 77fdfd5..5f716b9 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]