diff --git a/gsk/gsk.h b/gsk/gsk.h index b68c1dff5b..0b9cbd3b7a 100644 --- a/gsk/gsk.h +++ b/gsk/gsk.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include diff --git a/gsk/gskpixelshader.c b/gsk/gskpixelshader.c index 06aa5986be..6debd854a6 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 0000000000..66ad1da6f3 --- /dev/null +++ b/gsk/gskslcompiler.c @@ -0,0 +1,80 @@ +/* GTK - The GIMP Toolkit + * + * Copyright © 2017 Benjamin Otte + * + * 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 . + */ + +#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 0000000000..910897dbfd --- /dev/null +++ b/gsk/gskslcompiler.h @@ -0,0 +1,43 @@ +/* GTK - The GIMP Toolkit + * + * Copyright © 2017 Benjamin Otte + * + * 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 . + */ + +#ifndef __GSK_SL_COMPILER_H__ +#define __GSK_SL_COMPILER_H__ + +#if !defined (__GSK_H_INSIDE__) && !defined (GSK_COMPILATION) +#error "Only can be included directly." +#endif + +#include + +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 d20985e62a..eec8d0b7d3 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 a2c2adf42f..02991c5c3a 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 af52a3995b..507112b388 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 0a836798e5..6549d0d246 100644 --- a/gsk/gskslprogram.h +++ b/gsk/gskslprogram.h @@ -31,10 +31,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); diff --git a/gsk/gskslprogramprivate.h b/gsk/gskslprogramprivate.h new file mode 100644 index 0000000000..9b24b8eda5 --- /dev/null +++ b/gsk/gskslprogramprivate.h @@ -0,0 +1,32 @@ +/* GTK - The GIMP Toolkit + * + * Copyright © 2017 Benjamin Otte + * + * 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 . + */ + +#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 152ef77195..7323b939b7 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 5f716b98b3..17b82cc838 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 4eacf94307..16be6201ac 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)) @@ -215,6 +219,7 @@ main (int argc, char *argv[]) } g_object_unref (output); + g_object_unref (compiler); g_strfreev (filenames); return success ? EXIT_SUCCESS : EXIT_FAILURE;