[gtk+/wip/otte/shader: 320/367] gsksl: Introduce GskSlCompiler



commit 8fa034d1b4e5339223eb814ba962e0406cc925bc
Author: Benjamin Otte <otte redhat com>
Date:   Sun Sep 24 01:09:41 2017 +0200

    gsksl: Introduce GskSlCompiler
    
    This is the object that is used to create programs from. It also holds
    (or will hold) all the settings necessary to parse GLSL - like defines
    or include directories.

 gsk/gsk.h                      |    1 +
 gsk/gskpixelshader.c           |    6 ++-
 gsk/gskslcompiler.c            |   80 ++++++++++++++++++++++++++++++++++++++++
 gsk/gskslcompiler.h            |   43 +++++++++++++++++++++
 gsk/gskslpreprocessor.c        |    6 ++-
 gsk/gskslpreprocessorprivate.h |    3 +-
 gsk/gskslprogram.c             |   26 +------------
 gsk/gskslprogram.h             |    4 --
 gsk/gskslprogramprivate.h      |   32 ++++++++++++++++
 gsk/gsktypes.h                 |    1 +
 gsk/meson.build                |    1 +
 gtk/glsl.c                     |   17 +++++---
 12 files changed, 183 insertions(+), 37 deletions(-)
---
diff --git a/gsk/gsk.h b/gsk/gsk.h
index b68c1df..0b9cbd3 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/gskslcompiler.h>
 #include <gsk/gskslprogram.h>
 #include <gsk/gsktexture.h>
 
diff --git a/gsk/gskpixelshader.c b/gsk/gskpixelshader.c
index 06aa598..6debd85 100644
--- a/gsk/gskpixelshader.c
+++ b/gsk/gskpixelshader.c
@@ -34,6 +34,7 @@
 #include "gskpixelshaderprivate.h"
 
 #include "gskdebugprivate.h"
+#include "gskslcompiler.h"
 #include "gskslprogram.h"
 
 #include "gdk/gdkinternals.h"
@@ -143,12 +144,15 @@ gsk_pixel_shader_new_for_data (GBytes             *source,
                                GskShaderErrorFunc  error_func,
                                gpointer            error_data)
 {
+  GskSlCompiler *compiler;
   GskPixelShader *shader;
   GskSlProgram *program;
 
   g_return_val_if_fail (source != NULL, NULL);
 
-  program = gsk_sl_program_new (source, NULL);
+  compiler = gsk_sl_compiler_new ();
+  program = gsk_sl_compiler_compile (compiler, source);
+  g_object_unref (compiler);
   if (program == NULL)
     return NULL;
 
diff --git a/gsk/gskslcompiler.c b/gsk/gskslcompiler.c
new file mode 100644
index 0000000..66ad1da
--- /dev/null
+++ b/gsk/gskslcompiler.c
@@ -0,0 +1,80 @@
+/* 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 "gskslcompiler.h"
+
+#include "gskslpreprocessorprivate.h"
+#include "gskslprogramprivate.h"
+
+struct _GskSlCompiler {
+  GObject parent_instance;
+};
+
+G_DEFINE_TYPE (GskSlCompiler, gsk_sl_compiler, G_TYPE_OBJECT)
+
+static void
+gsk_sl_compiler_dispose (GObject *object)
+{
+  //GskSlCompiler *compiler = GSK_SL_COMPILER (object);
+
+  G_OBJECT_CLASS (gsk_sl_compiler_parent_class)->dispose (object);
+}
+
+static void
+gsk_sl_compiler_class_init (GskSlCompilerClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = gsk_sl_compiler_dispose;
+}
+
+static void
+gsk_sl_compiler_init (GskSlCompiler *compiler)
+{
+}
+
+GskSlCompiler *
+gsk_sl_compiler_new (void)
+{
+  return g_object_new (GSK_TYPE_SL_COMPILER, NULL);
+}
+
+GskSlProgram *
+gsk_sl_compiler_compile (GskSlCompiler *compiler,
+                         GBytes        *source)
+{
+  GskSlPreprocessor *preproc;
+  GskSlProgram *program;
+
+  program = g_object_new (GSK_TYPE_SL_PROGRAM, NULL);
+
+  preproc = gsk_sl_preprocessor_new (compiler, source);
+
+  if (!gsk_sl_program_parse (program, preproc))
+    {
+      g_object_unref (program);
+      program = NULL;
+    }
+
+  gsk_sl_preprocessor_unref (preproc);
+
+  return program;
+}
+
diff --git a/gsk/gskslcompiler.h b/gsk/gskslcompiler.h
new file mode 100644
index 0000000..910897d
--- /dev/null
+++ b/gsk/gskslcompiler.h
@@ -0,0 +1,43 @@
+/* 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_COMPILER_H__
+#define __GSK_SL_COMPILER_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_COMPILER (gsk_sl_compiler_get_type ())
+
+G_DECLARE_FINAL_TYPE (GskSlCompiler, gsk_sl_compiler, GSK, SL_COMPILER, GObject)
+
+GDK_AVAILABLE_IN_3_92
+GskSlCompiler *         gsk_sl_compiler_new                     (void);
+
+GDK_AVAILABLE_IN_3_92
+GskSlProgram *          gsk_sl_compiler_compile                 (GskSlCompiler       *compiler,
+                                                                 GBytes              *source);
+
+G_END_DECLS
+
+#endif /* __GSK_SL_COMPILER_H__ */
diff --git a/gsk/gskslpreprocessor.c b/gsk/gskslpreprocessor.c
index d20985e..eec8d0b 100644
--- a/gsk/gskslpreprocessor.c
+++ b/gsk/gskslpreprocessor.c
@@ -26,6 +26,7 @@ struct _GskSlPreprocessor
 {
   int ref_count;
 
+  GskSlCompiler *compiler;
   GskSlTokenizer *tokenizer;
   GskCodeLocation location;
   GskSlToken token;
@@ -48,13 +49,15 @@ gsk_sl_preprocessor_error_func (GskSlTokenizer        *parser,
 }
 
 GskSlPreprocessor *
-gsk_sl_preprocessor_new (GBytes *source)
+gsk_sl_preprocessor_new (GskSlCompiler *compiler,
+                         GBytes        *source)
 {
   GskSlPreprocessor *preproc;
 
   preproc = g_slice_new0 (GskSlPreprocessor);
 
   preproc->ref_count = 1;
+  preproc->compiler = g_object_ref (compiler);
   preproc->tokenizer = gsk_sl_tokenizer_new (source, 
                                             gsk_sl_preprocessor_error_func,
                                             preproc,
@@ -85,6 +88,7 @@ gsk_sl_preprocessor_unref (GskSlPreprocessor *preproc)
 
   gsk_sl_tokenizer_unref (preproc->tokenizer);
   gsk_sl_token_clear (&preproc->token);
+  g_object_unref (preproc->compiler);
 
   g_slice_free (GskSlPreprocessor, preproc);
 }
diff --git a/gsk/gskslpreprocessorprivate.h b/gsk/gskslpreprocessorprivate.h
index a2c2adf..02991c5 100644
--- a/gsk/gskslpreprocessorprivate.h
+++ b/gsk/gskslpreprocessorprivate.h
@@ -25,7 +25,8 @@
 
 G_BEGIN_DECLS
 
-GskSlPreprocessor *     gsk_sl_preprocessor_new                 (GBytes              *source);
+GskSlPreprocessor *     gsk_sl_preprocessor_new                 (GskSlCompiler       *compiler,
+                                                                 GBytes              *source);
 
 GskSlPreprocessor *     gsk_sl_preprocessor_ref                 (GskSlPreprocessor   *preproc);
 void                    gsk_sl_preprocessor_unref               (GskSlPreprocessor   *preproc);
diff --git a/gsk/gskslprogram.c b/gsk/gskslprogram.c
index af52a39..507112b 100644
--- a/gsk/gskslprogram.c
+++ b/gsk/gskslprogram.c
@@ -18,7 +18,7 @@
 
 #include "config.h"
 
-#include "gskslprogram.h"
+#include "gskslprogramprivate.h"
 
 #include "gskslnodeprivate.h"
 #include "gskslpreprocessorprivate.h"
@@ -62,7 +62,7 @@ gsk_sl_program_init (GskSlProgram *program)
   program->scope = gsk_sl_scope_new (NULL);
 }
 
-static gboolean
+gboolean
 gsk_sl_program_parse (GskSlProgram      *program,
                       GskSlPreprocessor *preproc)
 {
@@ -89,28 +89,6 @@ gsk_sl_program_parse (GskSlProgram      *program,
   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)
diff --git a/gsk/gskslprogram.h b/gsk/gskslprogram.h
index 0a83679..6549d0d 100644
--- a/gsk/gskslprogram.h
+++ b/gsk/gskslprogram.h
@@ -32,10 +32,6 @@ G_BEGIN_DECLS
 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
diff --git a/gsk/gskslprogramprivate.h b/gsk/gskslprogramprivate.h
new file mode 100644
index 0000000..9b24b8e
--- /dev/null
+++ b/gsk/gskslprogramprivate.h
@@ -0,0 +1,32 @@
+/* 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_PRIVATE_H__
+#define __GSK_SL_PROGRAM_PRIVATE_H__
+
+#include "gsk/gskslprogram.h"
+#include "gsk/gsksltypesprivate.h"
+
+G_BEGIN_DECLS
+
+gboolean                gsk_sl_program_parse                    (GskSlProgram           *program,
+                                                                 GskSlPreprocessor      *preproc);
+
+G_END_DECLS
+
+#endif /* __GSK_SL_PROGRAM_PRIVATE_H__ */
diff --git a/gsk/gsktypes.h b/gsk/gsktypes.h
index 152ef77..7323b93 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 _GskSlCompiler           GskSlCompiler;
 typedef struct _GskSlProgram            GskSlProgram;
 typedef struct _GskTexture              GskTexture;
 
diff --git a/gsk/meson.build b/gsk/meson.build
index 5f716b9..17b82cc 100644
--- a/gsk/meson.build
+++ b/gsk/meson.build
@@ -19,6 +19,7 @@ gsk_public_sources = files([
   'gskrendernode.c',
   'gskrendernodeimpl.c',
   'gskroundedrect.c',
+  'gskslcompiler.c',
   'gskslprogram.c',
   'gsktexture.c',
 ])
diff --git a/gtk/glsl.c b/gtk/glsl.c
index 78e7cfe..fcb9a56 100644
--- a/gtk/glsl.c
+++ b/gtk/glsl.c
@@ -45,7 +45,8 @@ bytes_new_from_file (const char  *filename,
 }
 
 static gboolean
-compile (GOutputStream *output,
+compile (GskSlCompiler *compiler,
+         GOutputStream *output,
          const char    *filename)
 {
   GBytes *bytes;
@@ -60,7 +61,7 @@ compile (GOutputStream *output,
       return FALSE;
     }
 
-  program = gsk_sl_program_new (bytes, NULL);
+  program = gsk_sl_compiler_compile (compiler, bytes);
   g_bytes_unref (bytes);
   if (program == NULL)
     return FALSE;
@@ -82,7 +83,8 @@ compile (GOutputStream *output,
 }
 
 static gboolean
-dump (GOutputStream *output,
+dump (GskSlCompiler *compiler,
+      GOutputStream *output,
       const char    *filename)
 {
   GBytes *bytes;
@@ -98,7 +100,7 @@ dump (GOutputStream *output,
       return FALSE;
     }
 
-  program = gsk_sl_program_new (bytes, NULL);
+  program = gsk_sl_compiler_compile (compiler, bytes);
   g_bytes_unref (bytes);
   if (program == NULL)
     return FALSE;
@@ -137,6 +139,7 @@ main (int argc, char *argv[])
   char **filenames = NULL;
   char *output_file = NULL;
   gboolean print = FALSE;
+  GskSlCompiler *compiler;
   const GOptionEntry entries[] = {
     { "print", 'p', 0, G_OPTION_ARG_NONE, &print, "Print instead of compiling", NULL },
     { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output_file, "Output filename", "FILE" },
@@ -152,6 +155,7 @@ main (int argc, char *argv[])
 
   gtk_init ();
 
+  compiler = gsk_sl_compiler_new ();
   ctx = g_option_context_new (NULL);
   g_option_context_add_main_entries (ctx, entries, NULL);
 
@@ -202,9 +206,9 @@ main (int argc, char *argv[])
   for (i = 0; success && filenames[i] != NULL; i++)
     {
       if (print)
-        success = dump (output, filenames[i]);
+        success = dump (compiler, output, filenames[i]);
       else
-        success = compile (output, filenames[i]);
+        success = compile (compiler, output, filenames[i]);
     }
 
  if (!g_output_stream_close (output, NULL, &error))
@@ -214,6 +218,7 @@ main (int argc, char *argv[])
     success = FALSE;
   }
 
+  g_object_unref (compiler);
   g_strfreev (filenames);
 
   return success ? EXIT_SUCCESS : EXIT_FAILURE;


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