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.
This commit is contained in:
Benjamin Otte
2017-09-24 01:09:41 +02:00
parent eee9142397
commit f8299719b4
12 changed files with 183 additions and 37 deletions

View File

@@ -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>

View File

@@ -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;

80
gsk/gskslcompiler.c Normal file
View File

@@ -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;
}

43
gsk/gskslcompiler.h Normal file
View File

@@ -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__ */

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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)

View File

@@ -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);

32
gsk/gskslprogramprivate.h Normal file
View File

@@ -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__ */

View File

@@ -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;

View File

@@ -19,6 +19,7 @@ gsk_public_sources = files([
'gskrendernode.c',
'gskrendernodeimpl.c',
'gskroundedrect.c',
'gskslcompiler.c',
'gskslprogram.c',
'gsktexture.c',
])

View File

@@ -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;