2009-05-10 23:40:41 +00:00
|
|
|
/*
|
|
|
|
* Cogl
|
|
|
|
*
|
2014-02-22 01:28:54 +00:00
|
|
|
* A Low Level GPU Graphics and Utilities API
|
2009-05-10 23:40:41 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Intel Corporation.
|
|
|
|
*
|
2014-02-22 01:28:54 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation
|
|
|
|
* files (the "Software"), to deal in the Software without
|
|
|
|
* restriction, including without limitation the rights to use, copy,
|
|
|
|
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
* of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
2010-03-01 12:56:10 +00:00
|
|
|
*
|
|
|
|
*
|
2009-05-10 23:40:41 +00:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Robert Bragg <robert@linux.intel.com>
|
|
|
|
*/
|
|
|
|
|
2016-05-05 14:21:51 +00:00
|
|
|
#include "cogl-config.h"
|
2009-05-10 23:40:41 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2010-11-04 22:25:52 +00:00
|
|
|
#include "cogl-context-private.h"
|
2009-05-10 23:40:41 +00:00
|
|
|
#include "cogl-debug.h"
|
|
|
|
#include "cogl-blend-string.h"
|
|
|
|
|
|
|
|
typedef enum _ParserState
|
|
|
|
{
|
|
|
|
PARSER_STATE_EXPECT_DEST_CHANNELS,
|
|
|
|
PARSER_STATE_SCRAPING_DEST_CHANNELS,
|
|
|
|
PARSER_STATE_EXPECT_FUNCTION_NAME,
|
|
|
|
PARSER_STATE_SCRAPING_FUNCTION_NAME,
|
|
|
|
PARSER_STATE_EXPECT_ARG_START,
|
|
|
|
PARSER_STATE_EXPECT_STATEMENT_END
|
|
|
|
} ParserState;
|
|
|
|
|
|
|
|
typedef enum _ParserArgState
|
|
|
|
{
|
|
|
|
PARSER_ARG_STATE_START,
|
|
|
|
PARSER_ARG_STATE_EXPECT_MINUS,
|
|
|
|
PARSER_ARG_STATE_EXPECT_COLOR_SRC_NAME,
|
|
|
|
PARSER_ARG_STATE_SCRAPING_COLOR_SRC_NAME,
|
|
|
|
PARSER_ARG_STATE_MAYBE_COLOR_MASK,
|
|
|
|
PARSER_ARG_STATE_SCRAPING_MASK,
|
|
|
|
PARSER_ARG_STATE_MAYBE_MULT,
|
|
|
|
PARSER_ARG_STATE_EXPECT_OPEN_PAREN,
|
|
|
|
PARSER_ARG_STATE_EXPECT_FACTOR,
|
|
|
|
PARSER_ARG_STATE_MAYBE_SRC_ALPHA_SATURATE,
|
|
|
|
PARSER_ARG_STATE_MAYBE_MINUS,
|
|
|
|
PARSER_ARG_STATE_EXPECT_CLOSE_PAREN,
|
|
|
|
PARSER_ARG_STATE_EXPECT_END
|
|
|
|
} ParserArgState;
|
|
|
|
|
|
|
|
|
|
|
|
#define DEFINE_COLOR_SOURCE(NAME, NAME_LEN) \
|
2009-06-16 20:29:21 +00:00
|
|
|
{/*.type = */COGL_BLEND_STRING_COLOR_SOURCE_ ## NAME, \
|
|
|
|
/*.name = */#NAME, \
|
|
|
|
/*.name_len = */NAME_LEN}
|
2009-05-10 23:40:41 +00:00
|
|
|
|
|
|
|
static CoglBlendStringColorSourceInfo blending_color_sources[] = {
|
|
|
|
DEFINE_COLOR_SOURCE (SRC_COLOR, 9),
|
|
|
|
DEFINE_COLOR_SOURCE (DST_COLOR, 9),
|
|
|
|
DEFINE_COLOR_SOURCE (CONSTANT, 8)
|
|
|
|
};
|
|
|
|
|
|
|
|
static CoglBlendStringColorSourceInfo tex_combine_color_sources[] = {
|
|
|
|
DEFINE_COLOR_SOURCE (TEXTURE, 7),
|
|
|
|
/* DEFINE_COLOR_SOURCE (TEXTURE_N, *) - handled manually */
|
|
|
|
DEFINE_COLOR_SOURCE (PRIMARY, 7),
|
|
|
|
DEFINE_COLOR_SOURCE (CONSTANT, 8),
|
|
|
|
DEFINE_COLOR_SOURCE (PREVIOUS, 8)
|
|
|
|
};
|
|
|
|
|
|
|
|
static CoglBlendStringColorSourceInfo tex_combine_texture_n_color_source = {
|
2009-06-16 20:29:21 +00:00
|
|
|
/*.type = */COGL_BLEND_STRING_COLOR_SOURCE_TEXTURE_N,
|
|
|
|
/*.name = */"TEXTURE_N",
|
|
|
|
/*.name_len = */0
|
2009-05-10 23:40:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#undef DEFINE_COLOR_SOURCE
|
|
|
|
|
|
|
|
#define DEFINE_FUNCTION(NAME, NAME_LEN, ARGC) \
|
2009-06-16 20:29:21 +00:00
|
|
|
{ /*.type = */COGL_BLEND_STRING_FUNCTION_ ## NAME, \
|
|
|
|
/*.name = */#NAME, \
|
|
|
|
/*.name_len = */NAME_LEN, \
|
|
|
|
/*.argc = */ARGC }
|
2009-05-10 23:40:41 +00:00
|
|
|
|
|
|
|
/* NB: These must be sorted so any name that's a subset of another
|
|
|
|
* comes later than the longer name. */
|
|
|
|
static CoglBlendStringFunctionInfo tex_combine_functions[] = {
|
|
|
|
DEFINE_FUNCTION (REPLACE, 7, 1),
|
|
|
|
DEFINE_FUNCTION (MODULATE, 8, 2),
|
|
|
|
DEFINE_FUNCTION (ADD_SIGNED, 10, 2),
|
|
|
|
DEFINE_FUNCTION (ADD, 3, 2),
|
|
|
|
DEFINE_FUNCTION (INTERPOLATE, 11, 3),
|
|
|
|
DEFINE_FUNCTION (SUBTRACT, 8, 2),
|
|
|
|
DEFINE_FUNCTION (DOT3_RGBA, 9, 2),
|
|
|
|
DEFINE_FUNCTION (DOT3_RGB, 8, 2)
|
|
|
|
};
|
|
|
|
|
|
|
|
static CoglBlendStringFunctionInfo blend_functions[] = {
|
|
|
|
DEFINE_FUNCTION (ADD, 3, 2)
|
|
|
|
};
|
|
|
|
|
|
|
|
#undef DEFINE_FUNCTION
|
|
|
|
|
2012-08-31 18:28:27 +00:00
|
|
|
uint32_t
|
2009-07-20 11:47:53 +00:00
|
|
|
cogl_blend_string_error_quark (void)
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
return g_quark_from_static_string ("cogl-blend-string-error-quark");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_blend_string_split_rgba_statement (CoglBlendStringStatement *statement,
|
|
|
|
CoglBlendStringStatement *rgb,
|
|
|
|
CoglBlendStringStatement *a)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memcpy (rgb, statement, sizeof (CoglBlendStringStatement));
|
|
|
|
memcpy (a, statement, sizeof (CoglBlendStringStatement));
|
|
|
|
|
|
|
|
rgb->mask = COGL_BLEND_STRING_CHANNEL_MASK_RGB;
|
|
|
|
a->mask = COGL_BLEND_STRING_CHANNEL_MASK_ALPHA;
|
|
|
|
|
|
|
|
for (i = 0; i < statement->function->argc; i++)
|
|
|
|
{
|
|
|
|
CoglBlendStringArgument *arg = &statement->args[i];
|
|
|
|
CoglBlendStringArgument *rgb_arg = &rgb->args[i];
|
|
|
|
CoglBlendStringArgument *a_arg = &a->args[i];
|
|
|
|
|
|
|
|
if (arg->source.mask == COGL_BLEND_STRING_CHANNEL_MASK_RGBA)
|
|
|
|
{
|
|
|
|
rgb_arg->source.mask = COGL_BLEND_STRING_CHANNEL_MASK_RGB;
|
|
|
|
a_arg->source.mask = COGL_BLEND_STRING_CHANNEL_MASK_ALPHA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg->factor.is_color &&
|
|
|
|
arg->factor.source.mask == COGL_BLEND_STRING_CHANNEL_MASK_RGBA)
|
|
|
|
{
|
|
|
|
rgb_arg->factor.source.mask = COGL_BLEND_STRING_CHANNEL_MASK_RGB;
|
|
|
|
a_arg->factor.source.mask = COGL_BLEND_STRING_CHANNEL_MASK_ALPHA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:04:47 +00:00
|
|
|
static gboolean
|
2009-05-10 23:40:41 +00:00
|
|
|
validate_tex_combine_statements (CoglBlendStringStatement *statements,
|
|
|
|
int n_statements,
|
2019-06-18 06:02:10 +00:00
|
|
|
GError **error)
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
const char *error_string;
|
|
|
|
CoglBlendStringError detail = COGL_BLEND_STRING_ERROR_INVALID_ERROR;
|
|
|
|
|
|
|
|
for (i = 0; i < n_statements; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < statements[i].function->argc; j++)
|
|
|
|
{
|
|
|
|
CoglBlendStringArgument *arg = &statements[i].args[j];
|
|
|
|
if (arg->source.is_zero)
|
|
|
|
{
|
|
|
|
error_string = "You can't use the constant '0' as a texture "
|
|
|
|
"combine argument";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!arg->factor.is_one)
|
|
|
|
{
|
|
|
|
error_string = "Argument factors are only relevant to blending "
|
|
|
|
"not texture combining";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
error:
|
2019-06-18 06:02:10 +00:00
|
|
|
g_set_error (error, COGL_BLEND_STRING_ERROR, detail,
|
|
|
|
"Invalid texture combine string: %s", error_string);
|
2009-05-10 23:40:41 +00:00
|
|
|
|
2011-01-24 14:28:00 +00:00
|
|
|
if (COGL_DEBUG_ENABLED (COGL_DEBUG_BLEND_STRINGS))
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
g_debug ("Invalid texture combine string: %s",
|
|
|
|
error_string);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:04:47 +00:00
|
|
|
static gboolean
|
2009-05-10 23:40:41 +00:00
|
|
|
validate_blend_statements (CoglBlendStringStatement *statements,
|
|
|
|
int n_statements,
|
2019-06-18 06:02:10 +00:00
|
|
|
GError **error)
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
const char *error_string;
|
|
|
|
CoglBlendStringError detail = COGL_BLEND_STRING_ERROR_INVALID_ERROR;
|
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, 0);
|
|
|
|
|
|
|
|
for (i = 0; i < n_statements; i++)
|
|
|
|
for (j = 0; j < statements[i].function->argc; j++)
|
|
|
|
{
|
|
|
|
CoglBlendStringArgument *arg = &statements[i].args[j];
|
|
|
|
|
|
|
|
if (arg->source.is_zero)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((j == 0 &&
|
|
|
|
arg->source.info->type !=
|
|
|
|
COGL_BLEND_STRING_COLOR_SOURCE_SRC_COLOR)
|
|
|
|
|| (j == 1 &&
|
|
|
|
arg->source.info->type !=
|
|
|
|
COGL_BLEND_STRING_COLOR_SOURCE_DST_COLOR))
|
|
|
|
{
|
|
|
|
error_string = "For blending you must always use SRC_COLOR "
|
|
|
|
"for arg0 and DST_COLOR for arg1";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
error:
|
2019-06-18 06:02:10 +00:00
|
|
|
g_set_error (error, COGL_BLEND_STRING_ERROR, detail,
|
|
|
|
"Invalid blend string: %s", error_string);
|
2009-05-10 23:40:41 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:04:47 +00:00
|
|
|
static gboolean
|
2009-05-10 23:40:41 +00:00
|
|
|
validate_statements_for_context (CoglBlendStringStatement *statements,
|
|
|
|
int n_statements,
|
|
|
|
CoglBlendStringContext context,
|
2019-06-18 06:02:10 +00:00
|
|
|
GError **error)
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
const char *error_string;
|
|
|
|
|
|
|
|
if (n_statements == 1)
|
|
|
|
{
|
|
|
|
if (statements[0].mask == COGL_BLEND_STRING_CHANNEL_MASK_ALPHA)
|
|
|
|
{
|
|
|
|
error_string = "You need to also give a blend statement for the RGB"
|
|
|
|
"channels";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else if (statements[0].mask == COGL_BLEND_STRING_CHANNEL_MASK_RGB)
|
|
|
|
{
|
|
|
|
error_string = "You need to also give a blend statement for the "
|
|
|
|
"Alpha channel";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context == COGL_BLEND_STRING_CONTEXT_BLENDING)
|
|
|
|
return validate_blend_statements (statements, n_statements, error);
|
|
|
|
else
|
|
|
|
return validate_tex_combine_statements (statements, n_statements, error);
|
|
|
|
|
|
|
|
error:
|
2019-06-18 06:02:10 +00:00
|
|
|
g_set_error (error, COGL_BLEND_STRING_ERROR,
|
|
|
|
COGL_BLEND_STRING_ERROR_INVALID_ERROR,
|
|
|
|
"Invalid %s string: %s",
|
|
|
|
context == COGL_BLEND_STRING_CONTEXT_BLENDING ?
|
|
|
|
"blend" : "texture combine",
|
|
|
|
error_string);
|
2009-05-10 23:40:41 +00:00
|
|
|
|
2011-01-24 14:28:00 +00:00
|
|
|
if (COGL_DEBUG_ENABLED (COGL_DEBUG_BLEND_STRINGS))
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
g_debug ("Invalid %s string: %s",
|
|
|
|
context == COGL_BLEND_STRING_CONTEXT_BLENDING ?
|
|
|
|
"blend" : "texture combine",
|
|
|
|
error_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_argument (CoglBlendStringArgument *arg)
|
|
|
|
{
|
|
|
|
const char *mask_names[] = {
|
|
|
|
"RGB",
|
|
|
|
"A",
|
|
|
|
"RGBA"
|
|
|
|
};
|
|
|
|
|
|
|
|
g_print (" Arg:\n");
|
|
|
|
g_print (" is zero = %s\n", arg->source.is_zero ? "yes" : "no");
|
|
|
|
if (!arg->source.is_zero)
|
|
|
|
{
|
|
|
|
g_print (" color source = %s\n", arg->source.info->name);
|
|
|
|
g_print (" one minus = %s\n", arg->source.one_minus ? "yes" : "no");
|
|
|
|
g_print (" mask = %s\n", mask_names[arg->source.mask]);
|
|
|
|
g_print (" texture = %d\n", arg->source.texture);
|
|
|
|
g_print ("\n");
|
|
|
|
g_print (" factor is_one = %s\n", arg->factor.is_one ? "yes" : "no");
|
|
|
|
g_print (" factor is_src_alpha_saturate = %s\n",
|
|
|
|
arg->factor.is_src_alpha_saturate ? "yes" : "no");
|
|
|
|
g_print (" factor is_color = %s\n", arg->factor.is_color ? "yes" : "no");
|
|
|
|
if (arg->factor.is_color)
|
|
|
|
{
|
|
|
|
g_print (" factor color:is zero = %s\n",
|
|
|
|
arg->factor.source.is_zero ? "yes" : "no");
|
|
|
|
g_print (" factor color:color source = %s\n",
|
|
|
|
arg->factor.source.info->name);
|
|
|
|
g_print (" factor color:one minus = %s\n",
|
|
|
|
arg->factor.source.one_minus ? "yes" : "no");
|
|
|
|
g_print (" factor color:mask = %s\n",
|
|
|
|
mask_names[arg->factor.source.mask]);
|
|
|
|
g_print (" factor color:texture = %d\n",
|
|
|
|
arg->factor.source.texture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_statement (int num, CoglBlendStringStatement *statement)
|
|
|
|
{
|
|
|
|
const char *mask_names[] = {
|
|
|
|
"RGB",
|
|
|
|
"A",
|
|
|
|
"RGBA"
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
g_print ("Statement %d:\n", num);
|
|
|
|
g_print (" Destination channel mask = %s\n",
|
|
|
|
mask_names[statement->mask]);
|
|
|
|
g_print (" Function = %s\n", statement->function->name);
|
|
|
|
for (i = 0; i < statement->function->argc; i++)
|
|
|
|
print_argument (&statement->args[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const CoglBlendStringFunctionInfo *
|
|
|
|
get_function_info (const char *mark,
|
|
|
|
const char *p,
|
|
|
|
CoglBlendStringContext context)
|
|
|
|
{
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 20:56:40 +00:00
|
|
|
size_t len = p - mark;
|
2009-05-10 23:40:41 +00:00
|
|
|
CoglBlendStringFunctionInfo *functions;
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 20:56:40 +00:00
|
|
|
size_t array_len;
|
2009-05-10 23:40:41 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (context == COGL_BLEND_STRING_CONTEXT_BLENDING)
|
|
|
|
{
|
|
|
|
functions = blend_functions;
|
|
|
|
array_len = G_N_ELEMENTS (blend_functions);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
functions = tex_combine_functions;
|
|
|
|
array_len = G_N_ELEMENTS (tex_combine_functions);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < array_len; i++)
|
|
|
|
{
|
|
|
|
if (len >= functions[i].name_len
|
|
|
|
&& strncmp (mark, functions[i].name, functions[i].name_len) == 0)
|
|
|
|
return &functions[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const CoglBlendStringColorSourceInfo *
|
|
|
|
get_color_src_info (const char *mark,
|
|
|
|
const char *p,
|
|
|
|
CoglBlendStringContext context)
|
|
|
|
{
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 20:56:40 +00:00
|
|
|
size_t len = p - mark;
|
2009-05-10 23:40:41 +00:00
|
|
|
CoglBlendStringColorSourceInfo *sources;
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 20:56:40 +00:00
|
|
|
size_t array_len;
|
2009-05-10 23:40:41 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (context == COGL_BLEND_STRING_CONTEXT_BLENDING)
|
|
|
|
{
|
|
|
|
sources = blending_color_sources;
|
|
|
|
array_len = G_N_ELEMENTS (blending_color_sources);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sources = tex_combine_color_sources;
|
|
|
|
array_len = G_N_ELEMENTS (tex_combine_color_sources);
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:04:11 +00:00
|
|
|
if (len >= 8 &&
|
|
|
|
strncmp (mark, "TEXTURE_", 8) == 0 &&
|
|
|
|
g_ascii_isdigit (mark[8]))
|
|
|
|
{
|
|
|
|
return &tex_combine_texture_n_color_source;
|
|
|
|
}
|
|
|
|
|
2009-05-10 23:40:41 +00:00
|
|
|
for (i = 0; i < array_len; i++)
|
|
|
|
{
|
|
|
|
if (len >= sources[i].name_len
|
|
|
|
&& strncmp (mark, sources[i].name, sources[i].name_len) == 0)
|
|
|
|
return &sources[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:04:47 +00:00
|
|
|
static gboolean
|
2009-05-10 23:40:41 +00:00
|
|
|
is_symbol_char (const char c)
|
|
|
|
{
|
|
|
|
return (g_ascii_isalpha (c) || c == '_') ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:04:47 +00:00
|
|
|
static gboolean
|
2009-09-02 15:27:22 +00:00
|
|
|
is_alphanum_char (const char c)
|
|
|
|
{
|
|
|
|
return (g_ascii_isalnum (c) || c == '_') ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:04:47 +00:00
|
|
|
static gboolean
|
2009-05-10 23:40:41 +00:00
|
|
|
parse_argument (const char *string, /* original user string */
|
|
|
|
const char **ret_p, /* start of argument IN:OUT */
|
|
|
|
const CoglBlendStringStatement *statement,
|
|
|
|
int current_arg,
|
|
|
|
CoglBlendStringArgument *arg, /* OUT */
|
|
|
|
CoglBlendStringContext context,
|
2019-06-18 06:02:10 +00:00
|
|
|
GError **error)
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
const char *p = *ret_p;
|
2011-02-15 12:31:12 +00:00
|
|
|
const char *mark = NULL;
|
|
|
|
const char *error_string = NULL;
|
2009-05-10 23:40:41 +00:00
|
|
|
ParserArgState state = PARSER_ARG_STATE_START;
|
2018-11-24 12:04:47 +00:00
|
|
|
gboolean parsing_factor = FALSE;
|
|
|
|
gboolean implicit_factor_brace = FALSE;
|
2009-05-10 23:40:41 +00:00
|
|
|
|
|
|
|
arg->source.is_zero = FALSE;
|
|
|
|
arg->source.info = NULL;
|
|
|
|
arg->source.texture = 0;
|
|
|
|
arg->source.one_minus = FALSE;
|
|
|
|
arg->source.mask = statement->mask;
|
|
|
|
|
|
|
|
arg->factor.is_one = FALSE;
|
|
|
|
arg->factor.is_color = FALSE;
|
|
|
|
arg->factor.is_src_alpha_saturate = FALSE;
|
|
|
|
|
|
|
|
arg->factor.source.is_zero = FALSE;
|
|
|
|
arg->factor.source.info = NULL;
|
|
|
|
arg->factor.source.texture = 0;
|
|
|
|
arg->factor.source.one_minus = FALSE;
|
|
|
|
arg->factor.source.mask = statement->mask;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (g_ascii_isspace (*p))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*p == '\0')
|
|
|
|
{
|
|
|
|
error_string = "Unexpected end of string while parsing argument";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case PARSER_ARG_STATE_START:
|
|
|
|
if (*p == '1')
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_MINUS;
|
|
|
|
else if (*p == '0')
|
|
|
|
{
|
|
|
|
arg->source.is_zero = TRUE;
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_END;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p--; /* backtrack */
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_COLOR_SRC_NAME;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case PARSER_ARG_STATE_EXPECT_MINUS:
|
|
|
|
if (*p != '-')
|
|
|
|
{
|
|
|
|
error_string = "expected a '-' following the 1";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
arg->source.one_minus = TRUE;
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_COLOR_SRC_NAME;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case PARSER_ARG_STATE_EXPECT_COLOR_SRC_NAME:
|
|
|
|
if (!is_symbol_char (*p))
|
|
|
|
{
|
|
|
|
error_string = "expected a color source name";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
state = PARSER_ARG_STATE_SCRAPING_COLOR_SRC_NAME;
|
|
|
|
mark = p;
|
|
|
|
if (parsing_factor)
|
|
|
|
arg->factor.is_color = TRUE;
|
|
|
|
|
2020-01-09 16:54:28 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2009-05-10 23:40:41 +00:00
|
|
|
case PARSER_ARG_STATE_SCRAPING_COLOR_SRC_NAME:
|
|
|
|
if (!is_symbol_char (*p))
|
|
|
|
{
|
|
|
|
CoglBlendStringColorSource *source =
|
|
|
|
parsing_factor ? &arg->factor.source : &arg->source;
|
|
|
|
source->info = get_color_src_info (mark, p, context);
|
|
|
|
if (!source->info)
|
|
|
|
{
|
|
|
|
error_string = "Unknown color source name";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (source->info->type ==
|
|
|
|
COGL_BLEND_STRING_COLOR_SOURCE_TEXTURE_N)
|
|
|
|
{
|
|
|
|
char *endp;
|
|
|
|
source->texture =
|
|
|
|
strtoul (&mark[strlen ("TEXTURE_")], &endp, 10);
|
|
|
|
if (mark == endp)
|
|
|
|
{
|
|
|
|
error_string = "invalid texture number given with "
|
|
|
|
"TEXTURE_N color source";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
p = endp;
|
|
|
|
}
|
|
|
|
state = PARSER_ARG_STATE_MAYBE_COLOR_MASK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2020-01-09 16:54:28 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2009-05-10 23:40:41 +00:00
|
|
|
case PARSER_ARG_STATE_MAYBE_COLOR_MASK:
|
|
|
|
if (*p != '[')
|
|
|
|
{
|
|
|
|
p--; /* backtrack */
|
|
|
|
if (!parsing_factor)
|
|
|
|
state = PARSER_ARG_STATE_MAYBE_MULT;
|
|
|
|
else
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_END;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
state = PARSER_ARG_STATE_SCRAPING_MASK;
|
|
|
|
mark = p;
|
|
|
|
|
2020-01-09 16:54:28 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2009-05-10 23:40:41 +00:00
|
|
|
case PARSER_ARG_STATE_SCRAPING_MASK:
|
|
|
|
if (*p == ']')
|
|
|
|
{
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 20:56:40 +00:00
|
|
|
size_t len = p - mark;
|
2009-05-10 23:40:41 +00:00
|
|
|
CoglBlendStringColorSource *source =
|
|
|
|
parsing_factor ? &arg->factor.source : &arg->source;
|
|
|
|
|
|
|
|
if (len == 5 && strncmp (mark, "[RGBA", len) == 0)
|
|
|
|
{
|
|
|
|
if (statement->mask != COGL_BLEND_STRING_CHANNEL_MASK_RGBA)
|
|
|
|
{
|
|
|
|
error_string = "You can't use an RGBA color mask if the "
|
|
|
|
"statement hasn't also got an RGBA= mask";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
source->mask = COGL_BLEND_STRING_CHANNEL_MASK_RGBA;
|
|
|
|
}
|
|
|
|
else if (len == 4 && strncmp (mark, "[RGB", len) == 0)
|
|
|
|
source->mask = COGL_BLEND_STRING_CHANNEL_MASK_RGB;
|
|
|
|
else if (len == 2 && strncmp (mark, "[A", len) == 0)
|
|
|
|
source->mask = COGL_BLEND_STRING_CHANNEL_MASK_ALPHA;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error_string = "Expected a channel mask of [RGBA]"
|
|
|
|
"[RGB] or [A]";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (parsing_factor)
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_CLOSE_PAREN;
|
|
|
|
else
|
|
|
|
state = PARSER_ARG_STATE_MAYBE_MULT;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case PARSER_ARG_STATE_EXPECT_OPEN_PAREN:
|
|
|
|
if (*p != '(')
|
|
|
|
{
|
2011-09-01 16:16:23 +00:00
|
|
|
if (is_alphanum_char (*p))
|
|
|
|
{
|
|
|
|
p--; /* compensate for implicit brace and ensure this
|
|
|
|
* char gets considered part of the blend factor */
|
|
|
|
implicit_factor_brace = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error_string = "Expected '(' around blend factor or alpha "
|
|
|
|
"numeric character for blend factor name";
|
|
|
|
goto error;
|
|
|
|
}
|
2009-05-10 23:40:41 +00:00
|
|
|
}
|
2011-09-01 16:16:23 +00:00
|
|
|
else
|
|
|
|
implicit_factor_brace = FALSE;
|
2009-05-10 23:40:41 +00:00
|
|
|
parsing_factor = TRUE;
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_FACTOR;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case PARSER_ARG_STATE_EXPECT_FACTOR:
|
|
|
|
if (*p == '1')
|
|
|
|
state = PARSER_ARG_STATE_MAYBE_MINUS;
|
|
|
|
else if (*p == '0')
|
|
|
|
{
|
|
|
|
arg->source.is_zero = TRUE;
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_CLOSE_PAREN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state = PARSER_ARG_STATE_MAYBE_SRC_ALPHA_SATURATE;
|
|
|
|
mark = p;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case PARSER_ARG_STATE_MAYBE_SRC_ALPHA_SATURATE:
|
|
|
|
if (!is_symbol_char (*p))
|
|
|
|
{
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 20:56:40 +00:00
|
|
|
size_t len = p - mark;
|
2009-05-10 23:40:41 +00:00
|
|
|
if (len >= strlen ("SRC_ALPHA_SATURATE") &&
|
|
|
|
strncmp (mark, "SRC_ALPHA_SATURATE", len) == 0)
|
|
|
|
{
|
|
|
|
arg->factor.is_src_alpha_saturate = TRUE;
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_CLOSE_PAREN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_COLOR_SRC_NAME;
|
|
|
|
p = mark - 1; /* backtrack */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case PARSER_ARG_STATE_MAYBE_MINUS:
|
|
|
|
if (*p == '-')
|
|
|
|
{
|
2011-09-01 16:16:23 +00:00
|
|
|
if (implicit_factor_brace)
|
|
|
|
{
|
|
|
|
error_string = "Expected ( ) braces around blend factor with "
|
|
|
|
"a subtraction";
|
|
|
|
goto error;
|
|
|
|
}
|
2009-05-10 23:40:41 +00:00
|
|
|
arg->factor.source.one_minus = TRUE;
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_COLOR_SRC_NAME;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arg->factor.is_one = TRUE;
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_CLOSE_PAREN;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case PARSER_ARG_STATE_EXPECT_CLOSE_PAREN:
|
2011-09-01 16:16:23 +00:00
|
|
|
if (implicit_factor_brace)
|
|
|
|
{
|
|
|
|
p--;
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_END;
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-10 23:40:41 +00:00
|
|
|
if (*p != ')')
|
|
|
|
{
|
|
|
|
error_string = "Expected closing parenthesis after blend factor";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_END;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case PARSER_ARG_STATE_MAYBE_MULT:
|
|
|
|
if (*p == '*')
|
|
|
|
{
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_OPEN_PAREN;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
arg->factor.is_one = TRUE;
|
|
|
|
state = PARSER_ARG_STATE_EXPECT_END;
|
|
|
|
|
2020-01-09 16:54:28 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2009-05-10 23:40:41 +00:00
|
|
|
case PARSER_ARG_STATE_EXPECT_END:
|
|
|
|
if (*p != ',' && *p != ')')
|
|
|
|
{
|
|
|
|
error_string = "expected , or )";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret_p = p - 1;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (p++);
|
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
int offset = p - string;
|
2019-06-18 06:02:10 +00:00
|
|
|
g_set_error (error,
|
|
|
|
COGL_BLEND_STRING_ERROR,
|
|
|
|
COGL_BLEND_STRING_ERROR_ARGUMENT_PARSE_ERROR,
|
|
|
|
"Syntax error for argument %d at offset %d: %s",
|
|
|
|
current_arg,
|
|
|
|
offset,
|
|
|
|
error_string);
|
2009-05-10 23:40:41 +00:00
|
|
|
|
2011-01-24 14:28:00 +00:00
|
|
|
if (COGL_DEBUG_ENABLED (COGL_DEBUG_BLEND_STRINGS))
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
g_debug ("Syntax error for argument %d at offset %d: %s",
|
|
|
|
current_arg, offset, error_string);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_cogl_blend_string_compile (const char *string,
|
|
|
|
CoglBlendStringContext context,
|
|
|
|
CoglBlendStringStatement *statements,
|
2019-06-18 06:02:10 +00:00
|
|
|
GError **error)
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
const char *p = string;
|
2009-06-05 15:46:12 +00:00
|
|
|
const char *mark = NULL;
|
2009-05-10 23:40:41 +00:00
|
|
|
const char *error_string;
|
|
|
|
ParserState state = PARSER_STATE_EXPECT_DEST_CHANNELS;
|
|
|
|
CoglBlendStringStatement *statement = statements;
|
|
|
|
int current_statement = 0;
|
|
|
|
int current_arg = 0;
|
2009-06-05 15:46:12 +00:00
|
|
|
int remaining_argc = 0;
|
2009-05-10 23:40:41 +00:00
|
|
|
|
|
|
|
#if 0
|
2011-01-24 14:28:00 +00:00
|
|
|
COGL_DEBUG_SET_FLAG (COGL_DEBUG_BLEND_STRINGS);
|
2009-05-10 23:40:41 +00:00
|
|
|
#endif
|
|
|
|
|
2011-01-24 14:28:00 +00:00
|
|
|
if (COGL_DEBUG_ENABLED (COGL_DEBUG_BLEND_STRINGS))
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
COGL_NOTE (BLEND_STRINGS, "Compiling %s string:\n%s\n",
|
|
|
|
context == COGL_BLEND_STRING_CONTEXT_BLENDING ?
|
|
|
|
"blend" : "texture combine",
|
|
|
|
string);
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (g_ascii_isspace (*p))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*p == '\0')
|
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case PARSER_STATE_EXPECT_DEST_CHANNELS:
|
|
|
|
if (current_statement != 0)
|
|
|
|
goto finished;
|
|
|
|
error_string = "Empty statement";
|
|
|
|
goto error;
|
|
|
|
case PARSER_STATE_SCRAPING_DEST_CHANNELS:
|
|
|
|
error_string = "Expected an '=' following the destination "
|
|
|
|
"channel mask";
|
|
|
|
goto error;
|
|
|
|
case PARSER_STATE_EXPECT_FUNCTION_NAME:
|
|
|
|
error_string = "Expected a function name";
|
|
|
|
goto error;
|
|
|
|
case PARSER_STATE_SCRAPING_FUNCTION_NAME:
|
|
|
|
error_string = "Expected parenthesis after the function name";
|
|
|
|
goto error;
|
|
|
|
case PARSER_STATE_EXPECT_ARG_START:
|
|
|
|
error_string = "Expected to find the start of an argument";
|
|
|
|
goto error;
|
|
|
|
case PARSER_STATE_EXPECT_STATEMENT_END:
|
|
|
|
error_string = "Expected closing parenthesis for statement";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case PARSER_STATE_EXPECT_DEST_CHANNELS:
|
|
|
|
mark = p;
|
|
|
|
state = PARSER_STATE_SCRAPING_DEST_CHANNELS;
|
|
|
|
|
2020-01-09 16:54:28 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2009-05-10 23:40:41 +00:00
|
|
|
case PARSER_STATE_SCRAPING_DEST_CHANNELS:
|
|
|
|
if (*p != '=')
|
|
|
|
continue;
|
|
|
|
if (strncmp (mark, "RGBA", 4) == 0)
|
|
|
|
statement->mask = COGL_BLEND_STRING_CHANNEL_MASK_RGBA;
|
|
|
|
else if (strncmp (mark, "RGB", 3) == 0)
|
|
|
|
statement->mask = COGL_BLEND_STRING_CHANNEL_MASK_RGB;
|
|
|
|
else if (strncmp (mark, "A", 1) == 0)
|
|
|
|
statement->mask = COGL_BLEND_STRING_CHANNEL_MASK_ALPHA;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error_string = "Unknown destination channel mask; "
|
|
|
|
"expected RGBA=, RGB= or A=";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
state = PARSER_STATE_EXPECT_FUNCTION_NAME;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case PARSER_STATE_EXPECT_FUNCTION_NAME:
|
|
|
|
mark = p;
|
|
|
|
state = PARSER_STATE_SCRAPING_FUNCTION_NAME;
|
|
|
|
|
2020-01-09 16:54:28 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2009-05-10 23:40:41 +00:00
|
|
|
case PARSER_STATE_SCRAPING_FUNCTION_NAME:
|
|
|
|
if (*p != '(')
|
|
|
|
{
|
2009-09-02 15:27:22 +00:00
|
|
|
if (!is_alphanum_char (*p))
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
error_string = "non alpha numeric character in function"
|
|
|
|
"name";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
statement->function = get_function_info (mark, p, context);
|
|
|
|
if (!statement->function)
|
|
|
|
{
|
|
|
|
error_string = "Unknown function name";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
remaining_argc = statement->function->argc;
|
|
|
|
current_arg = 0;
|
|
|
|
state = PARSER_STATE_EXPECT_ARG_START;
|
|
|
|
|
2020-01-09 16:54:28 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2009-05-10 23:40:41 +00:00
|
|
|
case PARSER_STATE_EXPECT_ARG_START:
|
|
|
|
if (*p != '(' && *p != ',')
|
|
|
|
continue;
|
|
|
|
if (remaining_argc)
|
|
|
|
{
|
|
|
|
p++; /* parse_argument expects to see the first char of the arg */
|
|
|
|
if (!parse_argument (string, &p, statement,
|
|
|
|
current_arg, &statement->args[current_arg],
|
|
|
|
context, error))
|
|
|
|
return 0;
|
|
|
|
current_arg++;
|
|
|
|
remaining_argc--;
|
|
|
|
}
|
|
|
|
if (!remaining_argc)
|
|
|
|
state = PARSER_STATE_EXPECT_STATEMENT_END;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case PARSER_STATE_EXPECT_STATEMENT_END:
|
|
|
|
if (*p != ')')
|
|
|
|
{
|
|
|
|
error_string = "Expected end of statement";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
state = PARSER_STATE_EXPECT_DEST_CHANNELS;
|
|
|
|
if (current_statement++ == 1)
|
|
|
|
goto finished;
|
|
|
|
statement = &statements[current_statement];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (p++);
|
|
|
|
|
|
|
|
finished:
|
|
|
|
|
2011-01-24 14:28:00 +00:00
|
|
|
if (COGL_DEBUG_ENABLED (COGL_DEBUG_BLEND_STRINGS))
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
if (current_statement > 0)
|
|
|
|
print_statement (0, &statements[0]);
|
|
|
|
if (current_statement > 1)
|
|
|
|
print_statement (1, &statements[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validate_statements_for_context (statements,
|
|
|
|
current_statement,
|
|
|
|
context,
|
|
|
|
error))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return current_statement;
|
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
int offset = p - string;
|
2019-06-18 06:02:10 +00:00
|
|
|
g_set_error (error,
|
|
|
|
COGL_BLEND_STRING_ERROR,
|
|
|
|
COGL_BLEND_STRING_ERROR_PARSE_ERROR,
|
|
|
|
"Syntax error at offset %d: %s",
|
|
|
|
offset,
|
|
|
|
error_string);
|
2009-05-10 23:40:41 +00:00
|
|
|
|
2011-01-24 14:28:00 +00:00
|
|
|
if (COGL_DEBUG_ENABLED (COGL_DEBUG_BLEND_STRINGS))
|
2009-05-10 23:40:41 +00:00
|
|
|
{
|
|
|
|
g_debug ("Syntax error at offset %d: %s",
|
|
|
|
offset, error_string);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INTERNAL TESTING CODE ...
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct _TestString
|
|
|
|
{
|
|
|
|
const char *string;
|
|
|
|
CoglBlendStringContext context;
|
|
|
|
};
|
|
|
|
|
2012-03-06 18:21:28 +00:00
|
|
|
/* FIXME: this should probably be moved to a unit test */
|
|
|
|
int
|
|
|
|
_cogl_blend_string_test (void);
|
|
|
|
|
2009-05-10 23:40:41 +00:00
|
|
|
int
|
|
|
|
_cogl_blend_string_test (void)
|
|
|
|
{
|
|
|
|
struct _TestString strings[] = {
|
|
|
|
{" A = MODULATE ( TEXTURE[RGB], PREVIOUS[A], PREVIOUS[A] ) ",
|
|
|
|
COGL_BLEND_STRING_CONTEXT_TEXTURE_COMBINE },
|
|
|
|
{" RGB = MODULATE ( TEXTURE[RGB], PREVIOUS[A] ) ",
|
|
|
|
COGL_BLEND_STRING_CONTEXT_TEXTURE_COMBINE },
|
|
|
|
{"A=ADD(TEXTURE[A],PREVIOUS[RGB])",
|
|
|
|
COGL_BLEND_STRING_CONTEXT_TEXTURE_COMBINE },
|
|
|
|
{"A=ADD(TEXTURE[A],PREVIOUS[RGB])",
|
|
|
|
COGL_BLEND_STRING_CONTEXT_TEXTURE_COMBINE },
|
|
|
|
|
|
|
|
{"RGBA = ADD(SRC_COLOR*(SRC_COLOR[A]), DST_COLOR*(1-SRC_COLOR[A]))",
|
|
|
|
COGL_BLEND_STRING_CONTEXT_BLENDING },
|
|
|
|
{"RGB = ADD(SRC_COLOR, DST_COLOR*(0))",
|
|
|
|
COGL_BLEND_STRING_CONTEXT_BLENDING },
|
|
|
|
{"RGB = ADD(SRC_COLOR, 0)",
|
|
|
|
COGL_BLEND_STRING_CONTEXT_BLENDING },
|
|
|
|
{"RGB = ADD()",
|
|
|
|
COGL_BLEND_STRING_CONTEXT_BLENDING },
|
|
|
|
{"RGB = ADD(SRC_COLOR, 0, DST_COLOR)",
|
|
|
|
COGL_BLEND_STRING_CONTEXT_BLENDING },
|
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
|
2019-06-18 06:02:10 +00:00
|
|
|
GError *error = NULL;
|
2009-05-10 23:40:41 +00:00
|
|
|
for (i = 0; strings[i].string; i++)
|
|
|
|
{
|
|
|
|
CoglBlendStringStatement statements[2];
|
|
|
|
int count = _cogl_blend_string_compile (strings[i].string,
|
|
|
|
strings[i].context,
|
|
|
|
statements,
|
|
|
|
&error);
|
|
|
|
if (!count)
|
|
|
|
{
|
|
|
|
g_print ("Failed to parse string:\n%s\n%s\n",
|
|
|
|
strings[i].string,
|
|
|
|
error->message);
|
2019-06-18 06:02:10 +00:00
|
|
|
g_error_free (error);
|
2009-05-10 23:40:41 +00:00
|
|
|
error = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
g_print ("Original:\n");
|
|
|
|
g_print ("%s\n", strings[i].string);
|
|
|
|
if (count > 0)
|
|
|
|
print_statement (0, &statements[0]);
|
|
|
|
if (count > 1)
|
|
|
|
print_statement (1, &statements[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|