gsksl: Add initial support for native variables

So far, that's just gl_VertexIndex and gl_InstanceIndex.
This commit is contained in:
Benjamin Otte
2017-10-23 01:31:40 +02:00
parent ac95c0606a
commit 59f415dfe2
6 changed files with 189 additions and 0 deletions

View File

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

59
gsk/gskslnativevariable.c Normal file
View File

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

View File

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

View File

@@ -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
@@ -350,6 +423,24 @@ gsk_sl_variable_new (const char *name,
return variable;
}
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)
{

View File

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

View File

@@ -44,6 +44,7 @@ gsk_private_sources = files([
'gskslfunctiontype.c',
'gskslimagetype.c',
'gskslnativefunction.c',
'gskslnativevariable.c',
'gskslpreprocessor.c',
'gskslprinter.c',
'gskslqualifier.c',