[gtk+/wip/otte/shader: 163/176] gsksl: Add initial support for native variables



commit b356be042e4ebc99012175765f330aa0ae3117b7
Author: Benjamin Otte <otte redhat com>
Date:   Mon Oct 23 01:31:40 2017 +0200

    gsksl: Add initial support for native variables
    
    So far, that's just gl_VertexIndex and gl_InstanceIndex.

 gsk/gskslenvironment.c           |    2 +
 gsk/gskslnativevariable.c        |   59 ++++++++++++++++++++++++
 gsk/gskslnativevariableprivate.h |   31 +++++++++++++
 gsk/gskslvariable.c              |   91 ++++++++++++++++++++++++++++++++++++++
 gsk/gskslvariableprivate.h       |    5 ++
 gsk/meson.build                  |    1 +
 6 files changed, 189 insertions(+), 0 deletions(-)
---
diff --git a/gsk/gskslenvironment.c b/gsk/gskslenvironment.c
index 6b5a0e3..e101bd0 100644
--- a/gsk/gskslenvironment.c
+++ b/gsk/gskslenvironment.c
@@ -21,6 +21,7 @@
 #include "gskslenvironmentprivate.h"
 
 #include "gskslnativefunctionprivate.h"
+#include "gskslnativevariableprivate.h"
 #include "gskslscopeprivate.h"
 
 #include <string.h>
@@ -111,6 +112,7 @@ gsk_sl_environment_create_scope (GskSlEnvironment *environment)
   scope = gsk_sl_scope_new (NULL, NULL);
 
   gsk_sl_native_functions_add (scope, environment);
+  gsk_sl_native_variables_add (scope, environment);
 
   return scope;
 }
diff --git a/gsk/gskslnativevariable.c b/gsk/gskslnativevariable.c
new file mode 100644
index 0000000..d26e894
--- /dev/null
+++ b/gsk/gskslnativevariable.c
@@ -0,0 +1,59 @@
+/* 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 "gskslnativevariableprivate.h"
+
+#include "gskslenvironmentprivate.h"
+#include "gskslqualifierprivate.h"
+#include "gskslscopeprivate.h"
+#include "gsksltypeprivate.h"
+#include "gskslvariableprivate.h"
+
+static void
+gsk_sl_native_variable_add (GskSlScope      *scope,
+                            const char      *name,
+                            GskSlScalarType  scalar,
+                            GskSpvBuiltIn    builtin)
+{
+  GskSlQualifier qualifier;
+  GskSlVariable *variable;
+
+  gsk_sl_qualifier_init (&qualifier);
+  qualifier.storage = GSK_SL_STORAGE_GLOBAL_IN;
+
+  variable = gsk_sl_variable_new_builtin (name,
+                                          gsk_sl_type_get_scalar (scalar),
+                                          &qualifier,
+                                          builtin);
+  gsk_sl_scope_add_variable (scope, variable);
+  gsk_sl_variable_unref (variable);
+}
+
+void
+gsk_sl_native_variables_add (GskSlScope       *scope,
+                             GskSlEnvironment *environment)
+{
+  if (gsk_sl_environment_get_stage (environment) == GSK_SL_SHADER_VERTEX)
+    {
+      gsk_sl_native_variable_add (scope, "gl_VertexIndex", GSK_SL_INT, GSK_SPV_BUILT_IN_VERTEX_INDEX);
+      gsk_sl_native_variable_add (scope, "gl_InstanceIndex", GSK_SL_INT, GSK_SPV_BUILT_IN_INSTANCE_INDEX);
+    }
+}
+
diff --git a/gsk/gskslnativevariableprivate.h b/gsk/gskslnativevariableprivate.h
new file mode 100644
index 0000000..f6d5a98
--- /dev/null
+++ b/gsk/gskslnativevariableprivate.h
@@ -0,0 +1,31 @@
+/* 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_NATIVE_VARIABLE_PRIVATE_H__
+#define __GSK_SL_NATIVE_VARIABLE_PRIVATE_H__
+
+#include "gsksltypesprivate.h"
+
+G_BEGIN_DECLS
+
+void                    gsk_sl_native_variables_add             (GskSlScope           *scope,
+                                                                 GskSlEnvironment     *environment);
+ 
+G_END_DECLS
+
+#endif /* __GSK_SL_NATIVE_VARIABLE_PRIVATE_H__ */
diff --git a/gsk/gskslvariable.c b/gsk/gskslvariable.c
index a9fde6e..b0f20df 100644
--- a/gsk/gskslvariable.c
+++ b/gsk/gskslvariable.c
@@ -150,6 +150,79 @@ static const GskSlVariableClass GSK_SL_VARIABLE_STANDARD = {
   gsk_sl_variable_standard_store_spv,
 };
 
+/* BUILTIN */
+
+typedef struct _GskSlVariableBuiltin GskSlVariableBuiltin;
+
+struct _GskSlVariableBuiltin {
+  GskSlVariable parent;
+
+  GskSpvBuiltIn builtin;
+};
+
+static gboolean
+gsk_sl_variable_builtin_is_direct_access_spv (const GskSlVariable *variable)
+{
+  return TRUE;
+}
+
+static guint32
+gsk_sl_variable_builtin_write_spv (const GskSlVariable *variable,
+                                   GskSpvWriter        *writer)
+{
+  const GskSlVariableBuiltin *builtin = (const GskSlVariableBuiltin *) variable;
+  guint32 result_id;
+  GskSpvStorageClass storage_class;
+
+  storage_class = gsk_sl_qualifier_get_storage_class (&variable->qualifier, variable->type);
+  result_id = gsk_spv_writer_variable (writer,
+                                       GSK_SPV_WRITER_SECTION_DEFINE,
+                                       variable->type,
+                                       storage_class,
+                                       storage_class,
+                                       0);
+
+  if (variable->name)
+    gsk_spv_writer_name (writer, result_id, variable->name);
+
+  gsk_sl_qualifier_write_spv_decorations (&variable->qualifier, writer, result_id);
+
+  gsk_spv_writer_decorate (writer, result_id, GSK_SPV_DECORATION_BUILT_IN, (guint32[1]) { builtin->builtin 
}, 1);
+
+  return result_id;
+}
+
+static guint32
+gsk_sl_variable_builtin_load_spv (GskSlVariable *variable,
+                                   GskSpvWriter  *writer)
+{
+  return gsk_spv_writer_load (writer,
+                              gsk_sl_variable_get_type (variable),
+                              gsk_spv_writer_get_id_for_variable (writer, variable),
+                              0);
+}
+
+static void
+gsk_sl_variable_builtin_store_spv (GskSlVariable *variable,
+                                    GskSpvWriter  *writer,
+                                    guint32        value)
+{
+  gsk_spv_writer_store (writer,
+                        gsk_spv_writer_get_id_for_variable (writer, variable),
+                        value,
+                        0);
+
+}
+
+static const GskSlVariableClass GSK_SL_VARIABLE_BUILTIN = {
+  sizeof (GskSlVariableBuiltin),
+  gsk_sl_variable_free,
+  gsk_sl_variable_builtin_is_direct_access_spv,
+  gsk_sl_variable_builtin_write_spv,
+  gsk_sl_variable_builtin_load_spv,
+  gsk_sl_variable_builtin_store_spv,
+};
+
 /* CONSTANT */
 
 static gboolean
@@ -351,6 +424,24 @@ gsk_sl_variable_new (const char           *name,
 }
 
 GskSlVariable *
+gsk_sl_variable_new_builtin (const char           *name,
+                             GskSlType            *type,
+                             const GskSlQualifier *qualifier,
+                             GskSpvBuiltIn         builtin)
+{
+  GskSlVariable *variable;
+
+  variable = gsk_sl_variable_alloc (&GSK_SL_VARIABLE_BUILTIN);
+
+  variable->type = gsk_sl_type_ref (type);
+  variable->qualifier = *qualifier;
+  variable->name = g_strdup (name);
+  ((GskSlVariableBuiltin *) variable)->builtin = builtin;
+
+  return variable;
+}
+
+GskSlVariable *
 gsk_sl_variable_ref (GskSlVariable *variable)
 {
   g_return_val_if_fail (variable != NULL, NULL);
diff --git a/gsk/gskslvariableprivate.h b/gsk/gskslvariableprivate.h
index 2df9759..5d59cd9 100644
--- a/gsk/gskslvariableprivate.h
+++ b/gsk/gskslvariableprivate.h
@@ -22,6 +22,7 @@
 #include <glib.h>
 
 #include "gsk/gsksltypesprivate.h"
+#include "gsk/gskspvenumsprivate.h"
 
 G_BEGIN_DECLS
 
@@ -29,6 +30,10 @@ GskSlVariable *         gsk_sl_variable_new                     (const char
                                                                  GskSlType              *type,
                                                                  const GskSlQualifier   *qualifier,
                                                                  GskSlValue             *initial_value);
+GskSlVariable *         gsk_sl_variable_new_builtin             (const char             *name,
+                                                                 GskSlType              *type,
+                                                                 const GskSlQualifier   *qualifier,
+                                                                 GskSpvBuiltIn           builtin);
 
 GskSlVariable *         gsk_sl_variable_ref                     (GskSlVariable          *variable);
 void                    gsk_sl_variable_unref                   (GskSlVariable          *variable);
diff --git a/gsk/meson.build b/gsk/meson.build
index 6302062..aac4e12 100644
--- a/gsk/meson.build
+++ b/gsk/meson.build
@@ -44,6 +44,7 @@ gsk_private_sources = files([
   'gskslfunctiontype.c',
   'gskslimagetype.c',
   'gskslnativefunction.c',
+  'gskslnativevariable.c',
   'gskslpreprocessor.c',
   'gskslprinter.c',
   'gskslqualifier.c',


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