2010-06-15 11:44:52 -04:00
|
|
|
/*
|
|
|
|
* Cogl
|
|
|
|
*
|
|
|
|
* An object oriented GL/GLES Abstraction/Utility Layer
|
|
|
|
*
|
2013-02-26 12:52:20 -05:00
|
|
|
* Copyright (C) 2008,2009,2010,2013 Intel Corporation.
|
2010-06-15 11:44:52 -04:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Robert Bragg <robert@linux.intel.com>
|
2010-12-02 09:00:46 -05:00
|
|
|
* Neil Roberts <neil@linux.intel.com>
|
2010-06-15 11:44:52 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2011-09-23 04:43:05 -04:00
|
|
|
#include "cogl-context-private.h"
|
2013-01-20 13:47:40 -05:00
|
|
|
#include "cogl-util-gl-private.h"
|
2010-10-27 13:54:57 -04:00
|
|
|
#include "cogl-pipeline-private.h"
|
2011-09-08 19:40:06 -04:00
|
|
|
#include "cogl-pipeline-layer-private.h"
|
2010-11-19 05:44:27 -05:00
|
|
|
#include "cogl-blend-string.h"
|
2011-11-25 17:02:53 -05:00
|
|
|
#include "cogl-snippet-private.h"
|
2013-06-08 18:03:25 -04:00
|
|
|
#include "cogl-list.h"
|
2010-07-01 20:07:36 -04:00
|
|
|
|
2010-11-29 11:56:41 -05:00
|
|
|
#ifdef COGL_PIPELINE_FRAGEND_GLSL
|
2010-07-01 20:07:36 -04:00
|
|
|
|
2010-11-04 18:25:52 -04:00
|
|
|
#include "cogl-context-private.h"
|
2012-08-10 03:43:45 -04:00
|
|
|
#include "cogl-object-private.h"
|
2010-08-12 09:05:01 -04:00
|
|
|
#include "cogl-shader-private.h"
|
2010-10-15 13:00:29 -04:00
|
|
|
#include "cogl-program-private.h"
|
2011-06-30 13:34:05 -04:00
|
|
|
#include "cogl-pipeline-cache.h"
|
2012-03-06 13:21:28 -05:00
|
|
|
#include "cogl-pipeline-fragend-glsl-private.h"
|
2013-01-07 14:16:11 -05:00
|
|
|
#include "cogl-glsl-shader-private.h"
|
2010-06-15 11:44:52 -04:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
/*
|
2010-10-27 13:54:57 -04:00
|
|
|
* GL/GLES compatability defines for pipeline thingies:
|
2010-06-15 11:44:52 -04:00
|
|
|
*/
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
/* This might not be defined on GLES */
|
|
|
|
#ifndef GL_TEXTURE_3D
|
|
|
|
#define GL_TEXTURE_3D 0x806F
|
|
|
|
#endif
|
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
const CoglPipelineFragend _cogl_pipeline_glsl_backend;
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
typedef struct _UnitState
|
|
|
|
{
|
|
|
|
unsigned int sampled:1;
|
|
|
|
unsigned int combine_constant_used:1;
|
|
|
|
} UnitState;
|
|
|
|
|
2013-06-08 18:03:25 -04:00
|
|
|
typedef struct _LayerData
|
2011-08-11 06:22:35 -04:00
|
|
|
{
|
2013-06-08 18:03:25 -04:00
|
|
|
CoglList link;
|
2011-08-11 06:22:35 -04:00
|
|
|
|
|
|
|
/* Layer index for the for the previous layer. This isn't
|
|
|
|
necessarily the same as this layer's index - 1 because the
|
|
|
|
indices can have gaps. If this is the first layer then it will be
|
|
|
|
-1 */
|
|
|
|
int previous_layer_index;
|
|
|
|
|
|
|
|
CoglPipelineLayer *layer;
|
2013-06-08 18:03:25 -04:00
|
|
|
} LayerData;
|
2011-08-11 06:22:35 -04:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
typedef struct
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
|
|
|
int ref_count;
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
GLuint gl_shader;
|
2010-11-19 05:44:27 -05:00
|
|
|
GString *header, *source;
|
|
|
|
UnitState *unit_state;
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2011-08-11 06:22:35 -04:00
|
|
|
/* List of layers that we haven't generated code for yet. These are
|
|
|
|
in reverse order. As soon as we're about to generate code for
|
|
|
|
layer we'll remove it from the list so we don't generate it
|
|
|
|
again */
|
2013-06-08 18:03:25 -04:00
|
|
|
CoglList layers;
|
2011-06-30 08:39:48 -04:00
|
|
|
} CoglPipelineShaderState;
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
static CoglUserDataKey shader_state_key;
|
2010-09-14 07:13:30 -04:00
|
|
|
|
2011-08-11 06:22:35 -04:00
|
|
|
static void
|
|
|
|
ensure_layer_generated (CoglPipeline *pipeline,
|
|
|
|
int layer_num);
|
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
static CoglPipelineShaderState *
|
|
|
|
shader_state_new (int n_layers)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
CoglPipelineShaderState *shader_state;
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
shader_state = g_slice_new0 (CoglPipelineShaderState);
|
|
|
|
shader_state->ref_count = 1;
|
|
|
|
shader_state->unit_state = g_new0 (UnitState, n_layers);
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
return shader_state;
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
static CoglPipelineShaderState *
|
|
|
|
get_shader_state (CoglPipeline *pipeline)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
return cogl_object_get_user_data (COGL_OBJECT (pipeline), &shader_state_key);
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
static void
|
|
|
|
destroy_shader_state (void *user_data)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
CoglPipelineShaderState *shader_state = user_data;
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
if (--shader_state->ref_count == 0)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
if (shader_state->gl_shader)
|
|
|
|
GE( ctx, glDeleteShader (shader_state->gl_shader) );
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
g_free (shader_state->unit_state);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
g_slice_free (CoglPipelineShaderState, shader_state);
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-30 08:39:48 -04:00
|
|
|
set_shader_state (CoglPipeline *pipeline, CoglPipelineShaderState *shader_state)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
cogl_object_set_user_data (COGL_OBJECT (pipeline),
|
|
|
|
&shader_state_key,
|
|
|
|
shader_state,
|
|
|
|
destroy_shader_state);
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
static void
|
|
|
|
dirty_shader_state (CoglPipeline *pipeline)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
cogl_object_set_user_data (COGL_OBJECT (pipeline),
|
|
|
|
&shader_state_key,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
2010-12-02 09:00:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
GLuint
|
|
|
|
_cogl_pipeline_fragend_glsl_get_shader (CoglPipeline *pipeline)
|
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
CoglPipelineShaderState *shader_state = get_shader_state (pipeline);
|
2010-12-02 09:00:46 -05:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
if (shader_state)
|
|
|
|
return shader_state->gl_shader;
|
2010-12-02 09:00:46 -05:00
|
|
|
else
|
|
|
|
return 0;
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
2011-11-17 11:52:21 -05:00
|
|
|
static CoglPipelineSnippetList *
|
|
|
|
get_fragment_snippets (CoglPipeline *pipeline)
|
|
|
|
{
|
|
|
|
pipeline =
|
|
|
|
_cogl_pipeline_get_authority (pipeline,
|
|
|
|
COGL_PIPELINE_STATE_FRAGMENT_SNIPPETS);
|
|
|
|
|
|
|
|
return &pipeline->big_state->fragment_snippets;
|
|
|
|
}
|
|
|
|
|
2011-11-25 12:36:03 -05:00
|
|
|
static CoglPipelineSnippetList *
|
|
|
|
get_layer_fragment_snippets (CoglPipelineLayer *layer)
|
|
|
|
{
|
|
|
|
unsigned long state = COGL_PIPELINE_LAYER_STATE_FRAGMENT_SNIPPETS;
|
|
|
|
layer = _cogl_pipeline_layer_get_authority (layer, state);
|
|
|
|
|
|
|
|
return &layer->big_state->fragment_snippets;
|
|
|
|
}
|
|
|
|
|
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 16:56:40 -04:00
|
|
|
static CoglBool
|
2011-11-25 17:02:53 -05:00
|
|
|
has_replace_hook (CoglPipelineLayer *layer,
|
|
|
|
CoglSnippetHook hook)
|
|
|
|
{
|
2013-06-08 18:53:11 -04:00
|
|
|
GList *l;
|
2011-11-25 17:02:53 -05:00
|
|
|
|
2013-06-08 18:53:11 -04:00
|
|
|
for (l = get_layer_fragment_snippets (layer)->entries; l; l = l->next)
|
|
|
|
{
|
|
|
|
CoglSnippet *snippet = l->data;
|
|
|
|
|
|
|
|
if (snippet->hook == hook && snippet->replace)
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-11-25 17:02:53 -05:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-02-26 13:29:36 -05:00
|
|
|
static CoglBool
|
|
|
|
add_layer_declaration_cb (CoglPipelineLayer *layer,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
CoglPipelineShaderState *shader_state = user_data;
|
|
|
|
CoglTextureType texture_type =
|
|
|
|
_cogl_pipeline_layer_get_texture_type (layer);
|
|
|
|
const char *target_string;
|
|
|
|
|
|
|
|
_cogl_gl_util_get_texture_target_string (texture_type, &target_string, NULL);
|
|
|
|
|
|
|
|
g_string_append_printf (shader_state->header,
|
|
|
|
"uniform sampler%s cogl_sampler%i;\n",
|
|
|
|
target_string,
|
|
|
|
layer->index);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_layer_declarations (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineShaderState *shader_state)
|
|
|
|
{
|
|
|
|
/* We always emit sampler uniforms in case there will be custom
|
|
|
|
* layer snippets that want to sample arbitrary layers. */
|
|
|
|
|
|
|
|
_cogl_pipeline_foreach_layer_internal (pipeline,
|
|
|
|
add_layer_declaration_cb,
|
|
|
|
shader_state);
|
|
|
|
}
|
|
|
|
|
2013-02-26 12:52:20 -05:00
|
|
|
static void
|
|
|
|
add_global_declarations (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineShaderState *shader_state)
|
|
|
|
{
|
|
|
|
CoglSnippetHook hook = COGL_SNIPPET_HOOK_FRAGMENT_GLOBALS;
|
|
|
|
CoglPipelineSnippetList *snippets = get_fragment_snippets (pipeline);
|
|
|
|
|
|
|
|
/* Add the global data hooks. All of the code in these snippets is
|
|
|
|
* always added and only the declarations data is used */
|
|
|
|
|
|
|
|
_cogl_pipeline_snippet_generate_declarations (shader_state->header,
|
|
|
|
hook,
|
|
|
|
snippets);
|
|
|
|
}
|
|
|
|
|
2012-09-25 16:08:10 -04:00
|
|
|
static void
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_start (CoglPipeline *pipeline,
|
2010-06-15 11:44:52 -04:00
|
|
|
int n_layers,
|
2012-09-27 06:06:16 -04:00
|
|
|
unsigned long pipelines_difference)
|
2010-06-15 11:44:52 -04:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
CoglPipelineShaderState *shader_state;
|
2010-10-27 13:54:57 -04:00
|
|
|
CoglPipeline *authority;
|
2011-06-30 13:34:05 -04:00
|
|
|
CoglPipeline *template_pipeline = NULL;
|
2012-09-25 16:08:10 -04:00
|
|
|
CoglProgram *user_program = cogl_pipeline_get_user_program (pipeline);
|
2010-11-19 05:44:27 -05:00
|
|
|
int i;
|
2010-08-04 12:53:51 -04:00
|
|
|
|
2012-09-25 16:08:10 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2010-06-15 11:44:52 -04:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
/* Now lookup our glsl backend private state */
|
|
|
|
shader_state = get_shader_state (pipeline);
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
if (shader_state == NULL)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
/* If we don't have an associated glsl shader yet then find the
|
2010-10-15 13:00:29 -04:00
|
|
|
* glsl-authority (the oldest ancestor whose state will result in
|
2010-12-02 09:00:46 -05:00
|
|
|
* the same shader being generated as for this pipeline).
|
2010-10-15 13:00:29 -04:00
|
|
|
*
|
2010-12-02 09:00:46 -05:00
|
|
|
* We always make sure to associate new shader with the
|
2010-10-27 13:54:57 -04:00
|
|
|
* glsl-authority to maximize the chance that other pipelines can
|
2010-10-15 13:00:29 -04:00
|
|
|
* share it.
|
|
|
|
*/
|
2010-12-01 12:06:18 -05:00
|
|
|
authority = _cogl_pipeline_find_equivalent_parent
|
|
|
|
(pipeline,
|
2011-07-07 15:44:56 -04:00
|
|
|
_cogl_pipeline_get_state_for_fragment_codegen (ctx) &
|
2010-12-01 12:06:18 -05:00
|
|
|
~COGL_PIPELINE_STATE_LAYERS,
|
2011-07-07 15:44:56 -04:00
|
|
|
_cogl_pipeline_get_layer_state_for_fragment_codegen (ctx));
|
2010-12-01 12:06:18 -05:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
shader_state = get_shader_state (authority);
|
2010-10-15 13:00:29 -04:00
|
|
|
|
|
|
|
/* If we don't have an existing program associated with the
|
2010-12-02 09:00:46 -05:00
|
|
|
* glsl-authority then start generating code for a new shader...
|
2010-10-15 13:00:29 -04:00
|
|
|
*/
|
2011-06-30 08:39:48 -04:00
|
|
|
if (shader_state == NULL)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2011-06-30 13:34:05 -04:00
|
|
|
/* Check if there is already a similar cached pipeline whose
|
|
|
|
shader state we can share */
|
|
|
|
if (G_LIKELY (!(COGL_DEBUG_ENABLED
|
|
|
|
(COGL_DEBUG_DISABLE_PROGRAM_CACHES))))
|
|
|
|
{
|
|
|
|
template_pipeline =
|
|
|
|
_cogl_pipeline_cache_get_fragment_template (ctx->pipeline_cache,
|
|
|
|
authority);
|
|
|
|
|
|
|
|
shader_state = get_shader_state (template_pipeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shader_state)
|
|
|
|
shader_state->ref_count++;
|
|
|
|
else
|
|
|
|
shader_state = shader_state_new (n_layers);
|
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
set_shader_state (authority, shader_state);
|
2011-06-30 13:34:05 -04:00
|
|
|
|
|
|
|
if (template_pipeline)
|
|
|
|
{
|
|
|
|
shader_state->ref_count++;
|
|
|
|
set_shader_state (template_pipeline, shader_state);
|
|
|
|
}
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
2010-11-19 05:44:27 -05:00
|
|
|
|
|
|
|
/* If the pipeline isn't actually its own glsl-authority
|
|
|
|
* then take a reference to the program state associated
|
|
|
|
* with the glsl-authority... */
|
|
|
|
if (authority != pipeline)
|
2011-06-30 08:39:48 -04:00
|
|
|
{
|
|
|
|
shader_state->ref_count++;
|
|
|
|
set_shader_state (pipeline, shader_state);
|
|
|
|
}
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
if (user_program)
|
2012-09-27 06:06:16 -04:00
|
|
|
{
|
|
|
|
/* If the user program contains a fragment shader then we don't need
|
|
|
|
to generate one */
|
|
|
|
if (_cogl_program_has_fragment_shader (user_program))
|
2013-01-19 11:00:33 -05:00
|
|
|
{
|
|
|
|
if (shader_state->gl_shader)
|
|
|
|
{
|
|
|
|
GE( ctx, glDeleteShader (shader_state->gl_shader) );
|
|
|
|
shader_state->gl_shader = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-09-27 06:06:16 -04:00
|
|
|
}
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2013-01-19 11:00:33 -05:00
|
|
|
if (shader_state->gl_shader)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If we make it here then we have a glsl_shader_state struct
|
|
|
|
without a gl_shader either because this is the first time we've
|
|
|
|
encountered it or because the user program has changed */
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
/* We reuse two grow-only GStrings for code-gen. One string
|
|
|
|
contains the uniform and attribute declarations while the
|
|
|
|
other contains the main function. We need two strings
|
|
|
|
because we need to dynamically declare attributes as the
|
|
|
|
add_layer callback is invoked */
|
2010-12-02 13:05:22 -05:00
|
|
|
g_string_set_size (ctx->codegen_header_buffer, 0);
|
|
|
|
g_string_set_size (ctx->codegen_source_buffer, 0);
|
2011-06-30 08:39:48 -04:00
|
|
|
shader_state->header = ctx->codegen_header_buffer;
|
|
|
|
shader_state->source = ctx->codegen_source_buffer;
|
2013-06-08 18:03:25 -04:00
|
|
|
_cogl_list_init (&shader_state->layers);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2013-02-26 13:29:36 -05:00
|
|
|
add_layer_declarations (pipeline, shader_state);
|
2013-02-26 12:52:20 -05:00
|
|
|
add_global_declarations (pipeline, shader_state);
|
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
g_string_append (shader_state->source,
|
2010-11-19 05:44:27 -05:00
|
|
|
"void\n"
|
2011-11-25 08:41:13 -05:00
|
|
|
"cogl_generated_source ()\n"
|
2010-11-19 05:44:27 -05:00
|
|
|
"{\n");
|
|
|
|
|
|
|
|
for (i = 0; i < n_layers; i++)
|
2010-07-23 12:46:41 -04:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
shader_state->unit_state[i].sampled = FALSE;
|
|
|
|
shader_state->unit_state[i].combine_constant_used = FALSE;
|
2010-07-23 12:46:41 -04:00
|
|
|
}
|
2010-11-19 05:44:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-30 08:39:48 -04:00
|
|
|
add_constant_lookup (CoglPipelineShaderState *shader_state,
|
2010-11-19 05:44:27 -05:00
|
|
|
CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer,
|
|
|
|
const char *swizzle)
|
|
|
|
{
|
2011-11-25 18:16:31 -05:00
|
|
|
g_string_append_printf (shader_state->header,
|
2010-11-19 05:44:27 -05:00
|
|
|
"_cogl_layer_constant_%i.%s",
|
2012-02-10 09:17:10 -05:00
|
|
|
layer->index, swizzle);
|
2010-11-19 05:44:27 -05:00
|
|
|
}
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
static void
|
2011-08-12 11:29:30 -04:00
|
|
|
ensure_texture_lookup_generated (CoglPipelineShaderState *shader_state,
|
|
|
|
CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer)
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
|
|
|
int unit_index = _cogl_pipeline_layer_get_unit_index (layer);
|
2011-11-25 12:36:03 -05:00
|
|
|
CoglPipelineSnippetData snippet_data;
|
2012-02-10 09:50:02 -05:00
|
|
|
CoglTextureType texture_type;
|
|
|
|
const char *target_string, *tex_coord_swizzle;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-07-07 15:44:56 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2011-08-12 11:29:30 -04:00
|
|
|
if (shader_state->unit_state[unit_index].sampled)
|
|
|
|
return;
|
|
|
|
|
2012-02-10 09:50:02 -05:00
|
|
|
texture_type =
|
|
|
|
_cogl_pipeline_layer_get_texture_type (layer);
|
2013-02-26 13:29:36 -05:00
|
|
|
_cogl_gl_util_get_texture_target_string (texture_type,
|
|
|
|
&target_string,
|
|
|
|
&tex_coord_swizzle);
|
2012-02-10 09:50:02 -05:00
|
|
|
|
2011-08-12 11:29:30 -04:00
|
|
|
shader_state->unit_state[unit_index].sampled = TRUE;
|
|
|
|
|
2011-11-25 17:00:49 -05:00
|
|
|
g_string_append_printf (shader_state->header,
|
|
|
|
"vec4 cogl_texel%i;\n",
|
2012-02-09 15:39:27 -05:00
|
|
|
layer->index);
|
2011-11-25 17:00:49 -05:00
|
|
|
|
2011-08-12 11:29:30 -04:00
|
|
|
g_string_append_printf (shader_state->source,
|
2012-02-10 09:50:02 -05:00
|
|
|
" cogl_texel%i = cogl_texture_lookup%i ("
|
|
|
|
"cogl_sampler%i, ",
|
|
|
|
layer->index,
|
2012-02-09 15:39:27 -05:00
|
|
|
layer->index,
|
|
|
|
layer->index);
|
2011-08-12 11:29:30 -04:00
|
|
|
|
2012-09-23 08:32:36 -04:00
|
|
|
if (cogl_pipeline_get_layer_point_sprite_coords_enabled (pipeline,
|
2011-11-25 12:36:03 -05:00
|
|
|
layer->index))
|
2013-08-24 20:59:31 -04:00
|
|
|
g_string_append_printf (shader_state->source,
|
2013-08-24 21:19:05 -04:00
|
|
|
"vec4 (cogl_point_coord, 0.0, 1.0)");
|
2011-11-25 12:36:03 -05:00
|
|
|
else
|
|
|
|
g_string_append_printf (shader_state->source,
|
2012-09-27 06:06:16 -04:00
|
|
|
"cogl_tex_coord%i_in",
|
|
|
|
layer->index);
|
2011-06-08 13:03:58 -04:00
|
|
|
|
2011-11-25 12:36:03 -05:00
|
|
|
g_string_append (shader_state->source, ");\n");
|
2011-06-08 13:03:58 -04:00
|
|
|
|
2011-11-25 17:02:53 -05:00
|
|
|
/* There's no need to generate the real texture lookup if it's going
|
|
|
|
to be replaced */
|
|
|
|
if (!has_replace_hook (layer, COGL_SNIPPET_HOOK_TEXTURE_LOOKUP))
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
2011-11-25 17:02:53 -05:00
|
|
|
g_string_append_printf (shader_state->header,
|
|
|
|
"vec4\n"
|
2012-02-10 09:50:02 -05:00
|
|
|
"cogl_real_texture_lookup%i (sampler%s tex,\n"
|
|
|
|
" vec4 coords)\n"
|
2011-11-25 17:02:53 -05:00
|
|
|
"{\n"
|
|
|
|
" return ",
|
2012-02-10 09:50:02 -05:00
|
|
|
layer->index,
|
|
|
|
target_string);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-11-25 17:02:53 -05:00
|
|
|
if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_DISABLE_TEXTURING)))
|
|
|
|
g_string_append (shader_state->header,
|
|
|
|
"vec4 (1.0, 1.0, 1.0, 1.0);\n");
|
|
|
|
else
|
|
|
|
g_string_append_printf (shader_state->header,
|
2012-02-10 09:50:02 -05:00
|
|
|
"texture%s (tex, coords.%s);\n",
|
|
|
|
target_string, tex_coord_swizzle);
|
2011-11-25 12:36:03 -05:00
|
|
|
|
2011-11-25 17:02:53 -05:00
|
|
|
g_string_append (shader_state->header, "}\n");
|
|
|
|
}
|
2011-11-25 12:36:03 -05:00
|
|
|
|
|
|
|
/* Wrap the texture lookup in any snippets that have been hooked */
|
|
|
|
memset (&snippet_data, 0, sizeof (snippet_data));
|
|
|
|
snippet_data.snippets = get_layer_fragment_snippets (layer);
|
2011-11-25 15:54:14 -05:00
|
|
|
snippet_data.hook = COGL_SNIPPET_HOOK_TEXTURE_LOOKUP;
|
2011-11-25 12:36:03 -05:00
|
|
|
snippet_data.chain_function = g_strdup_printf ("cogl_real_texture_lookup%i",
|
2012-02-09 15:39:27 -05:00
|
|
|
layer->index);
|
2011-11-25 12:36:03 -05:00
|
|
|
snippet_data.final_name = g_strdup_printf ("cogl_texture_lookup%i",
|
2012-02-09 15:39:27 -05:00
|
|
|
layer->index);
|
2011-11-25 12:36:03 -05:00
|
|
|
snippet_data.function_prefix = g_strdup_printf ("cogl_texture_lookup_hook%i",
|
2012-02-09 15:39:27 -05:00
|
|
|
layer->index);
|
2011-11-25 12:36:03 -05:00
|
|
|
snippet_data.return_type = "vec4";
|
|
|
|
snippet_data.return_variable = "cogl_texel";
|
2012-02-10 09:50:02 -05:00
|
|
|
snippet_data.arguments = "cogl_sampler, cogl_tex_coord";
|
|
|
|
snippet_data.argument_declarations =
|
|
|
|
g_strdup_printf ("sampler%s cogl_sampler, vec4 cogl_tex_coord",
|
|
|
|
target_string);
|
2011-11-25 12:36:03 -05:00
|
|
|
snippet_data.source_buf = shader_state->header;
|
|
|
|
|
|
|
|
_cogl_pipeline_snippet_generate_code (&snippet_data);
|
|
|
|
|
|
|
|
g_free ((char *) snippet_data.chain_function);
|
|
|
|
g_free ((char *) snippet_data.final_name);
|
|
|
|
g_free ((char *) snippet_data.function_prefix);
|
2012-02-10 09:50:02 -05:00
|
|
|
g_free ((char *) snippet_data.argument_declarations);
|
2010-06-15 11:44:52 -04:00
|
|
|
}
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
static void
|
2011-06-30 08:39:48 -04:00
|
|
|
add_arg (CoglPipelineShaderState *shader_state,
|
2010-11-19 05:44:27 -05:00
|
|
|
CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer,
|
2011-08-11 06:22:35 -04:00
|
|
|
int previous_layer_index,
|
2010-12-06 16:29:56 -05:00
|
|
|
CoglPipelineCombineSource src,
|
|
|
|
CoglPipelineCombineOp operand,
|
2010-11-19 05:44:27 -05:00
|
|
|
const char *swizzle)
|
|
|
|
{
|
2011-11-25 18:16:31 -05:00
|
|
|
GString *shader_source = shader_state->header;
|
2010-11-19 05:44:27 -05:00
|
|
|
char alpha_swizzle[5] = "aaaa";
|
|
|
|
|
|
|
|
g_string_append_c (shader_source, '(');
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
if (operand == COGL_PIPELINE_COMBINE_OP_ONE_MINUS_SRC_COLOR ||
|
|
|
|
operand == COGL_PIPELINE_COMBINE_OP_ONE_MINUS_SRC_ALPHA)
|
2010-11-19 05:44:27 -05:00
|
|
|
g_string_append_printf (shader_source,
|
|
|
|
"vec4(1.0, 1.0, 1.0, 1.0).%s - ",
|
|
|
|
swizzle);
|
|
|
|
|
|
|
|
/* If the operand is reading from the alpha then replace the swizzle
|
|
|
|
with the same number of copies of the alpha */
|
2010-12-06 16:29:56 -05:00
|
|
|
if (operand == COGL_PIPELINE_COMBINE_OP_SRC_ALPHA ||
|
|
|
|
operand == COGL_PIPELINE_COMBINE_OP_ONE_MINUS_SRC_ALPHA)
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
|
|
|
alpha_swizzle[strlen (swizzle)] = '\0';
|
|
|
|
swizzle = alpha_swizzle;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (src)
|
|
|
|
{
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_TEXTURE:
|
2011-08-12 11:29:30 -04:00
|
|
|
g_string_append_printf (shader_source,
|
2011-11-25 17:00:49 -05:00
|
|
|
"cogl_texel%i.%s",
|
2012-02-09 15:39:27 -05:00
|
|
|
layer->index,
|
2011-08-12 11:29:30 -04:00
|
|
|
swizzle);
|
2010-11-19 05:44:27 -05:00
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_CONSTANT:
|
2011-06-30 08:39:48 -04:00
|
|
|
add_constant_lookup (shader_state,
|
2010-11-19 05:44:27 -05:00
|
|
|
pipeline,
|
|
|
|
layer,
|
|
|
|
swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_PREVIOUS:
|
2011-08-11 06:22:35 -04:00
|
|
|
if (previous_layer_index >= 0)
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
2011-08-11 06:22:35 -04:00
|
|
|
g_string_append_printf (shader_source,
|
2011-11-25 17:00:49 -05:00
|
|
|
"cogl_layer%i.%s",
|
2011-08-11 06:22:35 -04:00
|
|
|
previous_layer_index,
|
|
|
|
swizzle);
|
2010-11-19 05:44:27 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* flow through */
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_PRIMARY_COLOR:
|
2010-11-19 05:44:27 -05:00
|
|
|
g_string_append_printf (shader_source, "cogl_color_in.%s", swizzle);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-02-09 15:39:27 -05:00
|
|
|
{
|
|
|
|
int layer_num = src - COGL_PIPELINE_COMBINE_SOURCE_TEXTURE0;
|
|
|
|
CoglPipelineGetLayerFlags flags = COGL_PIPELINE_GET_LAYER_NO_CREATE;
|
|
|
|
CoglPipelineLayer *other_layer =
|
|
|
|
_cogl_pipeline_get_layer_with_flags (pipeline, layer_num, flags);
|
|
|
|
|
|
|
|
if (other_layer == NULL)
|
|
|
|
{
|
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 16:56:40 -04:00
|
|
|
static CoglBool warning_seen = FALSE;
|
2012-02-09 15:39:27 -05:00
|
|
|
if (!warning_seen)
|
|
|
|
{
|
|
|
|
g_warning ("The application is trying to use a texture "
|
|
|
|
"combine with a layer number that does not exist");
|
|
|
|
warning_seen = TRUE;
|
|
|
|
}
|
|
|
|
g_string_append_printf (shader_source,
|
|
|
|
"vec4 (1.0, 1.0, 1.0, 1.0).%s",
|
|
|
|
swizzle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_string_append_printf (shader_source,
|
|
|
|
"cogl_texel%i.%s",
|
|
|
|
other_layer->index,
|
|
|
|
swizzle);
|
|
|
|
}
|
2011-08-12 11:29:30 -04:00
|
|
|
break;
|
|
|
|
}
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-08-12 11:29:30 -04:00
|
|
|
g_string_append_c (shader_source, ')');
|
|
|
|
}
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-08-11 06:22:35 -04:00
|
|
|
static void
|
|
|
|
ensure_arg_generated (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer,
|
|
|
|
int previous_layer_index,
|
|
|
|
CoglPipelineCombineSource src)
|
|
|
|
{
|
2011-08-12 11:29:30 -04:00
|
|
|
CoglPipelineShaderState *shader_state = get_shader_state (pipeline);
|
|
|
|
|
2011-08-11 06:22:35 -04:00
|
|
|
switch (src)
|
|
|
|
{
|
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_PRIMARY_COLOR:
|
2011-11-25 18:16:31 -05:00
|
|
|
/* This doesn't involve any other layers */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_CONSTANT:
|
|
|
|
{
|
|
|
|
int unit_index = _cogl_pipeline_layer_get_unit_index (layer);
|
|
|
|
/* Create a sampler uniform for this layer if we haven't already */
|
|
|
|
if (!shader_state->unit_state[unit_index].combine_constant_used)
|
|
|
|
{
|
|
|
|
g_string_append_printf (shader_state->header,
|
|
|
|
"uniform vec4 _cogl_layer_constant_%i;\n",
|
2012-02-10 09:17:10 -05:00
|
|
|
layer->index);
|
2011-11-25 18:16:31 -05:00
|
|
|
shader_state->unit_state[unit_index].combine_constant_used = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2011-08-11 06:22:35 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_PREVIOUS:
|
|
|
|
if (previous_layer_index >= 0)
|
|
|
|
ensure_layer_generated (pipeline, previous_layer_index);
|
|
|
|
break;
|
2011-08-12 11:29:30 -04:00
|
|
|
|
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_TEXTURE:
|
|
|
|
ensure_texture_lookup_generated (shader_state,
|
|
|
|
pipeline,
|
|
|
|
layer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-02-09 15:39:27 -05:00
|
|
|
if (src >= COGL_PIPELINE_COMBINE_SOURCE_TEXTURE0)
|
2011-08-12 11:29:30 -04:00
|
|
|
{
|
2012-02-09 15:39:27 -05:00
|
|
|
int layer_num = src - COGL_PIPELINE_COMBINE_SOURCE_TEXTURE0;
|
|
|
|
CoglPipelineGetLayerFlags flags = COGL_PIPELINE_GET_LAYER_NO_CREATE;
|
|
|
|
CoglPipelineLayer *other_layer =
|
|
|
|
_cogl_pipeline_get_layer_with_flags (pipeline, layer_num, flags);
|
|
|
|
|
|
|
|
if (other_layer)
|
|
|
|
ensure_texture_lookup_generated (shader_state,
|
|
|
|
pipeline,
|
|
|
|
other_layer);
|
2011-08-12 11:29:30 -04:00
|
|
|
}
|
|
|
|
break;
|
2011-08-11 06:22:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-25 18:16:31 -05:00
|
|
|
static void
|
|
|
|
ensure_args_for_func (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer,
|
|
|
|
int previous_layer_index,
|
|
|
|
CoglPipelineCombineFunc function,
|
|
|
|
CoglPipelineCombineSource *src)
|
|
|
|
{
|
|
|
|
int n_args = _cogl_get_n_args_for_combine_func (function);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n_args; i++)
|
|
|
|
ensure_arg_generated (pipeline, layer, previous_layer_index, src[i]);
|
|
|
|
}
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
static void
|
|
|
|
append_masked_combine (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer,
|
2011-08-11 06:22:35 -04:00
|
|
|
int previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
const char *swizzle,
|
2010-12-06 16:29:56 -05:00
|
|
|
CoglPipelineCombineFunc function,
|
|
|
|
CoglPipelineCombineSource *src,
|
|
|
|
CoglPipelineCombineOp *op)
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
CoglPipelineShaderState *shader_state = get_shader_state (pipeline);
|
2011-11-25 18:16:31 -05:00
|
|
|
GString *shader_source = shader_state->header;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-11-25 18:16:31 -05:00
|
|
|
g_string_append_printf (shader_state->header,
|
|
|
|
" cogl_layer.%s = ",
|
2011-08-11 06:22:35 -04:00
|
|
|
swizzle);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
|
|
|
switch (function)
|
|
|
|
{
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_REPLACE:
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_MODULATE:
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
g_string_append (shader_source, " * ");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_ADD:
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
g_string_append (shader_source, " + ");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_ADD_SIGNED:
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
g_string_append (shader_source, " + ");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], swizzle);
|
|
|
|
g_string_append_printf (shader_source,
|
|
|
|
" - vec4(0.5, 0.5, 0.5, 0.5).%s",
|
|
|
|
swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_SUBTRACT:
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
g_string_append (shader_source, " - ");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_INTERPOLATE:
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
g_string_append (shader_source, " * ");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[2], op[2], swizzle);
|
|
|
|
g_string_append (shader_source, " + ");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], swizzle);
|
|
|
|
g_string_append_printf (shader_source,
|
|
|
|
" * (vec4(1.0, 1.0, 1.0, 1.0).%s - ",
|
|
|
|
swizzle);
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[2], op[2], swizzle);
|
|
|
|
g_string_append_c (shader_source, ')');
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_DOT3_RGB:
|
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_DOT3_RGBA:
|
2010-12-02 15:48:45 -05:00
|
|
|
g_string_append (shader_source, "vec4(4.0 * ((");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], "r");
|
|
|
|
g_string_append (shader_source, " - 0.5) * (");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], "r");
|
|
|
|
g_string_append (shader_source, " - 0.5) + (");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], "g");
|
|
|
|
g_string_append (shader_source, " - 0.5) * (");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], "g");
|
|
|
|
g_string_append (shader_source, " - 0.5) + (");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], "b");
|
|
|
|
g_string_append (shader_source, " - 0.5) * (");
|
2011-08-11 06:22:35 -04:00
|
|
|
add_arg (shader_state, pipeline, layer, previous_layer_index,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], "b");
|
|
|
|
g_string_append_printf (shader_source, " - 0.5))).%s", swizzle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append_printf (shader_source, ";\n");
|
|
|
|
}
|
|
|
|
|
2011-08-11 06:22:35 -04:00
|
|
|
static void
|
|
|
|
ensure_layer_generated (CoglPipeline *pipeline,
|
|
|
|
int layer_index)
|
2010-06-15 11:44:52 -04:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
CoglPipelineShaderState *shader_state = get_shader_state (pipeline);
|
2011-08-11 06:22:35 -04:00
|
|
|
CoglPipelineLayer *combine_authority;
|
|
|
|
CoglPipelineLayerBigState *big_state;
|
|
|
|
CoglPipelineLayer *layer;
|
2011-11-25 18:16:31 -05:00
|
|
|
CoglPipelineSnippetData snippet_data;
|
2011-08-11 06:22:35 -04:00
|
|
|
LayerData *layer_data;
|
|
|
|
|
|
|
|
/* Find the layer that corresponds to this layer_num */
|
2013-06-08 18:03:25 -04:00
|
|
|
_cogl_list_for_each (layer_data, &shader_state->layers, link)
|
2011-08-11 06:22:35 -04:00
|
|
|
{
|
|
|
|
layer = layer_data->layer;
|
|
|
|
|
|
|
|
if (layer->index == layer_index)
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we didn't find it then we can assume the layer has already
|
|
|
|
been generated */
|
|
|
|
return;
|
|
|
|
|
|
|
|
found:
|
|
|
|
|
|
|
|
/* Remove the layer from the list so we don't generate it again */
|
2013-06-08 18:03:25 -04:00
|
|
|
_cogl_list_remove (&layer_data->link);
|
2011-08-11 06:22:35 -04:00
|
|
|
|
|
|
|
combine_authority =
|
2010-11-19 05:44:27 -05:00
|
|
|
_cogl_pipeline_layer_get_authority (layer,
|
|
|
|
COGL_PIPELINE_LAYER_STATE_COMBINE);
|
2011-08-11 06:22:35 -04:00
|
|
|
big_state = combine_authority->big_state;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-11-25 17:00:49 -05:00
|
|
|
/* Make a global variable for the result of the layer code */
|
|
|
|
g_string_append_printf (shader_state->header,
|
|
|
|
"vec4 cogl_layer%i;\n",
|
2011-08-11 06:22:35 -04:00
|
|
|
layer_index);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-11-25 18:16:31 -05:00
|
|
|
/* Skip the layer generation if there is a snippet that replaces the
|
|
|
|
default layer code. This is important because generating this
|
|
|
|
code may cause the code for other layers to be generated and
|
|
|
|
stored in the global variable. If this code isn't actually used
|
|
|
|
then the global variables would be uninitialised and they may be
|
|
|
|
used from other layers */
|
|
|
|
if (!has_replace_hook (layer, COGL_SNIPPET_HOOK_LAYER_FRAGMENT))
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
2011-11-25 18:16:31 -05:00
|
|
|
ensure_args_for_func (pipeline,
|
|
|
|
layer,
|
|
|
|
layer_data->previous_layer_index,
|
|
|
|
big_state->texture_combine_rgb_func,
|
|
|
|
big_state->texture_combine_rgb_src);
|
|
|
|
ensure_args_for_func (pipeline,
|
|
|
|
layer,
|
|
|
|
layer_data->previous_layer_index,
|
|
|
|
big_state->texture_combine_alpha_func,
|
|
|
|
big_state->texture_combine_alpha_src);
|
|
|
|
|
|
|
|
g_string_append_printf (shader_state->header,
|
|
|
|
"vec4\n"
|
|
|
|
"cogl_real_generate_layer%i ()\n"
|
|
|
|
"{\n"
|
|
|
|
" vec4 cogl_layer;\n",
|
|
|
|
layer_index);
|
|
|
|
|
|
|
|
if (!_cogl_pipeline_layer_needs_combine_separate (combine_authority) ||
|
|
|
|
/* GL_DOT3_RGBA Is a bit weird as a GL_COMBINE_RGB function
|
|
|
|
* since if you use it, it overrides your ALPHA function...
|
|
|
|
*/
|
|
|
|
big_state->texture_combine_rgb_func ==
|
|
|
|
COGL_PIPELINE_COMBINE_FUNC_DOT3_RGBA)
|
|
|
|
append_masked_combine (pipeline,
|
|
|
|
layer,
|
|
|
|
layer_data->previous_layer_index,
|
|
|
|
"rgba",
|
|
|
|
big_state->texture_combine_rgb_func,
|
|
|
|
big_state->texture_combine_rgb_src,
|
|
|
|
big_state->texture_combine_rgb_op);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
append_masked_combine (pipeline,
|
|
|
|
layer,
|
|
|
|
layer_data->previous_layer_index,
|
|
|
|
"rgb",
|
|
|
|
big_state->texture_combine_rgb_func,
|
|
|
|
big_state->texture_combine_rgb_src,
|
|
|
|
big_state->texture_combine_rgb_op);
|
|
|
|
append_masked_combine (pipeline,
|
|
|
|
layer,
|
|
|
|
layer_data->previous_layer_index,
|
|
|
|
"a",
|
|
|
|
big_state->texture_combine_alpha_func,
|
|
|
|
big_state->texture_combine_alpha_src,
|
|
|
|
big_state->texture_combine_alpha_op);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append (shader_state->header,
|
|
|
|
" return cogl_layer;\n"
|
|
|
|
"}\n");
|
2010-11-19 05:44:27 -05:00
|
|
|
}
|
|
|
|
|
2011-11-25 18:16:31 -05:00
|
|
|
/* Wrap the layer code in any snippets that have been hooked */
|
|
|
|
memset (&snippet_data, 0, sizeof (snippet_data));
|
|
|
|
snippet_data.snippets = get_layer_fragment_snippets (layer);
|
|
|
|
snippet_data.hook = COGL_SNIPPET_HOOK_LAYER_FRAGMENT;
|
|
|
|
snippet_data.chain_function = g_strdup_printf ("cogl_real_generate_layer%i",
|
|
|
|
layer_index);
|
|
|
|
snippet_data.final_name = g_strdup_printf ("cogl_generate_layer%i",
|
|
|
|
layer_index);
|
|
|
|
snippet_data.function_prefix = g_strdup_printf ("cogl_generate_layer%i",
|
|
|
|
layer_index);
|
|
|
|
snippet_data.return_type = "vec4";
|
|
|
|
snippet_data.return_variable = "cogl_layer";
|
|
|
|
snippet_data.source_buf = shader_state->header;
|
|
|
|
|
|
|
|
_cogl_pipeline_snippet_generate_code (&snippet_data);
|
|
|
|
|
|
|
|
g_free ((char *) snippet_data.chain_function);
|
|
|
|
g_free ((char *) snippet_data.final_name);
|
|
|
|
g_free ((char *) snippet_data.function_prefix);
|
|
|
|
|
|
|
|
g_string_append_printf (shader_state->source,
|
|
|
|
" cogl_layer%i = cogl_generate_layer%i ();\n",
|
|
|
|
layer_index,
|
|
|
|
layer_index);
|
|
|
|
|
2011-08-11 06:22:35 -04:00
|
|
|
g_slice_free (LayerData, layer_data);
|
2010-06-15 11:44:52 -04:00
|
|
|
}
|
|
|
|
|
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 16:56:40 -04:00
|
|
|
static CoglBool
|
2011-08-11 06:22:35 -04:00
|
|
|
_cogl_pipeline_fragend_glsl_add_layer (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer,
|
|
|
|
unsigned long layers_difference)
|
2010-06-15 11:44:52 -04:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
CoglPipelineShaderState *shader_state = get_shader_state (pipeline);
|
2011-08-11 06:22:35 -04:00
|
|
|
LayerData *layer_data;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
if (!shader_state->source)
|
2010-11-19 05:44:27 -05:00
|
|
|
return TRUE;
|
|
|
|
|
2011-08-11 06:22:35 -04:00
|
|
|
/* Store the layers in reverse order */
|
|
|
|
layer_data = g_slice_new (LayerData);
|
|
|
|
layer_data->layer = layer;
|
|
|
|
|
2013-06-08 18:03:25 -04:00
|
|
|
if (_cogl_list_empty (&shader_state->layers))
|
|
|
|
{
|
|
|
|
layer_data->previous_layer_index = -1;
|
|
|
|
}
|
2011-08-11 06:22:35 -04:00
|
|
|
else
|
2013-06-08 18:03:25 -04:00
|
|
|
{
|
|
|
|
LayerData *first =
|
|
|
|
_cogl_container_of (shader_state->layers.next, first, link);
|
|
|
|
layer_data->previous_layer_index = first->layer->index;
|
|
|
|
}
|
2011-08-11 06:22:35 -04:00
|
|
|
|
2013-06-08 18:03:25 -04:00
|
|
|
_cogl_list_insert (&shader_state->layers, &layer_data->link);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-26 15:32:36 -04:00
|
|
|
/* GLES2 and GL3 don't have alpha testing so we need to implement it
|
|
|
|
in the shader */
|
2010-11-22 13:33:49 -05:00
|
|
|
|
2012-09-26 15:32:36 -04:00
|
|
|
#if defined(HAVE_COGL_GLES2) || defined(HAVE_COGL_GL)
|
2010-11-22 13:33:49 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
add_alpha_test_snippet (CoglPipeline *pipeline,
|
2011-06-30 08:39:48 -04:00
|
|
|
CoglPipelineShaderState *shader_state)
|
2010-11-22 13:33:49 -05:00
|
|
|
{
|
|
|
|
CoglPipelineAlphaFunc alpha_func;
|
|
|
|
|
|
|
|
alpha_func = cogl_pipeline_get_alpha_test_function (pipeline);
|
|
|
|
|
|
|
|
if (alpha_func == COGL_PIPELINE_ALPHA_FUNC_ALWAYS)
|
|
|
|
/* Do nothing */
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (alpha_func == COGL_PIPELINE_ALPHA_FUNC_NEVER)
|
|
|
|
{
|
|
|
|
/* Always discard the fragment */
|
2011-06-30 08:39:48 -04:00
|
|
|
g_string_append (shader_state->source,
|
2010-11-22 13:33:49 -05:00
|
|
|
" discard;\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For all of the other alpha functions we need a uniform for the
|
|
|
|
reference */
|
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
g_string_append (shader_state->header,
|
2010-11-22 13:33:49 -05:00
|
|
|
"uniform float _cogl_alpha_test_ref;\n");
|
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
g_string_append (shader_state->source,
|
2010-11-22 13:33:49 -05:00
|
|
|
" if (cogl_color_out.a ");
|
|
|
|
|
|
|
|
switch (alpha_func)
|
|
|
|
{
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_LESS:
|
2011-06-30 08:39:48 -04:00
|
|
|
g_string_append (shader_state->source, ">=");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_EQUAL:
|
2011-06-30 08:39:48 -04:00
|
|
|
g_string_append (shader_state->source, "!=");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_LEQUAL:
|
2011-06-30 08:39:48 -04:00
|
|
|
g_string_append (shader_state->source, ">");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_GREATER:
|
2011-06-30 08:39:48 -04:00
|
|
|
g_string_append (shader_state->source, "<=");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_NOTEQUAL:
|
2011-06-30 08:39:48 -04:00
|
|
|
g_string_append (shader_state->source, "==");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_GEQUAL:
|
2011-06-30 08:39:48 -04:00
|
|
|
g_string_append (shader_state->source, "< ");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_ALWAYS:
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_NEVER:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
g_string_append (shader_state->source,
|
2010-11-22 13:33:49 -05:00
|
|
|
" _cogl_alpha_test_ref)\n discard;\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_COGL_GLES2 */
|
|
|
|
|
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 16:56:40 -04:00
|
|
|
static CoglBool
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_end (CoglPipeline *pipeline,
|
2010-10-27 13:54:57 -04:00
|
|
|
unsigned long pipelines_difference)
|
2010-06-15 11:44:52 -04:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
CoglPipelineShaderState *shader_state = get_shader_state (pipeline);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, FALSE);
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
if (shader_state->source)
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
const char *source_strings[2];
|
|
|
|
GLint lengths[2];
|
|
|
|
GLint compile_status;
|
|
|
|
GLuint shader;
|
2011-11-25 10:31:21 -05:00
|
|
|
CoglPipelineSnippetData snippet_data;
|
2010-12-02 09:00:46 -05:00
|
|
|
|
|
|
|
COGL_STATIC_COUNTER (fragend_glsl_compile_counter,
|
2010-12-02 15:48:45 -05:00
|
|
|
"glsl fragment compile counter",
|
2010-12-02 09:00:46 -05:00
|
|
|
"Increments each time a new GLSL "
|
2010-12-02 15:48:45 -05:00
|
|
|
"fragment shader is compiled",
|
2010-12-02 09:00:46 -05:00
|
|
|
0 /* no application private data */);
|
|
|
|
COGL_COUNTER_INC (_cogl_uprof_context, fragend_glsl_compile_counter);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-08-11 06:22:35 -04:00
|
|
|
/* We only need to generate code to calculate the fragment value
|
|
|
|
for the last layer. If the value of this layer depends on any
|
|
|
|
previous layers then it will recursively generate the code
|
|
|
|
for those layers */
|
2013-06-08 18:03:25 -04:00
|
|
|
if (!_cogl_list_empty (&shader_state->layers))
|
2011-08-11 06:22:35 -04:00
|
|
|
{
|
|
|
|
CoglPipelineLayer *last_layer;
|
|
|
|
LayerData *layer_data, *tmp;
|
|
|
|
|
2013-06-08 18:03:25 -04:00
|
|
|
layer_data = _cogl_container_of (shader_state->layers.next,
|
|
|
|
layer_data,
|
|
|
|
link);
|
|
|
|
last_layer = layer_data->layer;
|
2011-08-11 06:22:35 -04:00
|
|
|
|
|
|
|
ensure_layer_generated (pipeline, last_layer->index);
|
|
|
|
g_string_append_printf (shader_state->source,
|
2011-11-25 17:00:49 -05:00
|
|
|
" cogl_color_out = cogl_layer%i;\n",
|
2011-08-11 06:22:35 -04:00
|
|
|
last_layer->index);
|
|
|
|
|
2013-06-08 18:03:25 -04:00
|
|
|
_cogl_list_for_each_safe (layer_data,
|
|
|
|
tmp,
|
|
|
|
&shader_state->layers,
|
|
|
|
link)
|
2011-08-11 06:22:35 -04:00
|
|
|
g_slice_free (LayerData, layer_data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_string_append (shader_state->source,
|
|
|
|
" cogl_color_out = cogl_color_in;\n");
|
|
|
|
|
2012-09-26 15:32:36 -04:00
|
|
|
#if defined(HAVE_COGL_GLES2) || defined (HAVE_COGL_GL)
|
2013-11-25 11:11:36 -05:00
|
|
|
if (!_cogl_has_private_feature (ctx, COGL_PRIVATE_FEATURE_ALPHA_TEST))
|
2011-06-30 08:39:48 -04:00
|
|
|
add_alpha_test_snippet (pipeline, shader_state);
|
2010-11-22 13:33:49 -05:00
|
|
|
#endif
|
|
|
|
|
2011-11-25 08:41:13 -05:00
|
|
|
/* Close the function surrounding the generated fragment processing */
|
|
|
|
g_string_append (shader_state->source, "}\n");
|
2011-11-17 11:52:21 -05:00
|
|
|
|
2011-11-25 10:31:21 -05:00
|
|
|
/* Add all of the hooks for fragment processing */
|
|
|
|
memset (&snippet_data, 0, sizeof (snippet_data));
|
|
|
|
snippet_data.snippets = get_fragment_snippets (pipeline);
|
2011-11-25 15:54:14 -05:00
|
|
|
snippet_data.hook = COGL_SNIPPET_HOOK_FRAGMENT;
|
2011-11-25 10:31:21 -05:00
|
|
|
snippet_data.chain_function = "cogl_generated_source";
|
|
|
|
snippet_data.final_name = "main";
|
|
|
|
snippet_data.function_prefix = "cogl_fragment_hook";
|
|
|
|
snippet_data.source_buf = shader_state->source;
|
|
|
|
_cogl_pipeline_snippet_generate_code (&snippet_data);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
GE_RET( shader, ctx, glCreateShader (GL_FRAGMENT_SHADER) );
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
lengths[0] = shader_state->header->len;
|
|
|
|
source_strings[0] = shader_state->header->str;
|
|
|
|
lengths[1] = shader_state->source->len;
|
|
|
|
source_strings[1] = shader_state->source->str;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2013-01-07 14:16:11 -05:00
|
|
|
_cogl_glsl_shader_set_source_with_boilerplate (ctx,
|
|
|
|
shader, GL_FRAGMENT_SHADER,
|
2013-01-19 11:00:33 -05:00
|
|
|
pipeline,
|
2013-01-07 14:16:11 -05:00
|
|
|
2, /* count */
|
|
|
|
source_strings, lengths);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
GE( ctx, glCompileShader (shader) );
|
|
|
|
GE( ctx, glGetShaderiv (shader, GL_COMPILE_STATUS, &compile_status) );
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
if (!compile_status)
|
|
|
|
{
|
|
|
|
GLint len = 0;
|
|
|
|
char *shader_log;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
GE( ctx, glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &len) );
|
2010-12-02 09:00:46 -05:00
|
|
|
shader_log = g_alloca (len);
|
2011-07-06 16:51:00 -04:00
|
|
|
GE( ctx, glGetShaderInfoLog (shader, len, &len, shader_log) );
|
2010-12-02 09:00:46 -05:00
|
|
|
g_warning ("Shader compilation failed:\n%s", shader_log);
|
2010-11-19 05:44:27 -05:00
|
|
|
}
|
|
|
|
|
2011-06-30 08:39:48 -04:00
|
|
|
shader_state->header = NULL;
|
|
|
|
shader_state->source = NULL;
|
|
|
|
shader_state->gl_shader = shader;
|
2010-11-22 13:33:49 -05:00
|
|
|
}
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-06-15 11:44:52 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-10-15 13:00:29 -04:00
|
|
|
static void
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_pre_change_notify (CoglPipeline *pipeline,
|
2010-10-27 13:54:57 -04:00
|
|
|
CoglPipelineState change,
|
2010-10-15 13:00:29 -04:00
|
|
|
const CoglColor *new_color)
|
|
|
|
{
|
2011-07-07 15:44:56 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
if ((change & _cogl_pipeline_get_state_for_fragment_codegen (ctx)))
|
2011-06-30 08:39:48 -04:00
|
|
|
dirty_shader_state (pipeline);
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
/* NB: layers are considered immutable once they have any dependants
|
|
|
|
* so although multiple pipelines can end up depending on a single
|
|
|
|
* static layer, we can guarantee that if a layer is being *changed*
|
|
|
|
* then it can only have one pipeline depending on it.
|
|
|
|
*
|
|
|
|
* XXX: Don't forget this is *pre* change, we can't read the new value
|
|
|
|
* yet!
|
|
|
|
*/
|
|
|
|
static void
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_layer_pre_change_notify (
|
2010-11-19 05:44:27 -05:00
|
|
|
CoglPipeline *owner,
|
|
|
|
CoglPipelineLayer *layer,
|
|
|
|
CoglPipelineLayerState change)
|
|
|
|
{
|
2011-07-07 15:44:56 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
if ((change & _cogl_pipeline_get_layer_state_for_fragment_codegen (ctx)))
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
2011-06-30 08:39:48 -04:00
|
|
|
dirty_shader_state (owner);
|
2010-11-19 05:44:27 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: we could be saving snippets of texture combine code along
|
|
|
|
* with each layer and then when a layer changes we would just free
|
|
|
|
* the snippet. */
|
|
|
|
}
|
|
|
|
|
2010-11-29 11:56:41 -05:00
|
|
|
const CoglPipelineFragend _cogl_pipeline_glsl_fragend =
|
2010-06-15 11:44:52 -04:00
|
|
|
{
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_start,
|
|
|
|
_cogl_pipeline_fragend_glsl_add_layer,
|
2011-08-11 06:22:35 -04:00
|
|
|
NULL, /* passthrough */
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_end,
|
|
|
|
_cogl_pipeline_fragend_glsl_pre_change_notify,
|
2010-10-27 13:54:57 -04:00
|
|
|
NULL, /* pipeline_set_parent_notify */
|
2011-06-30 08:39:48 -04:00
|
|
|
_cogl_pipeline_fragend_glsl_layer_pre_change_notify
|
2010-06-15 11:44:52 -04:00
|
|
|
};
|
|
|
|
|
2010-11-29 11:56:41 -05:00
|
|
|
#endif /* COGL_PIPELINE_FRAGEND_GLSL */
|
2010-07-01 20:07:36 -04:00
|
|
|
|