fragend-glsl: Don't generate the default texture lookup if replaced

Previously the function containing the default texture lookup is
always generated regardless of whether there is a snippet with a
replace string which would cause it not be used. Now this snippets are
all scanned to check for replace strings before generating the texture
lookup.

Reviewed-by: Robert Bragg <robert@linux.intel.com>
This commit is contained in:
Neil Roberts 2011-11-25 22:02:53 +00:00 committed by Robert Bragg
parent 272c6567fa
commit 4581ce5f15
2 changed files with 90 additions and 57 deletions

View File

@ -37,6 +37,7 @@
#include "cogl-pipeline-layer-private.h" #include "cogl-pipeline-layer-private.h"
#include "cogl-shader-private.h" #include "cogl-shader-private.h"
#include "cogl-blend-string.h" #include "cogl-blend-string.h"
#include "cogl-snippet-private.h"
#ifdef COGL_PIPELINE_FRAGEND_GLSL #ifdef COGL_PIPELINE_FRAGEND_GLSL
@ -200,6 +201,19 @@ get_layer_fragment_snippets (CoglPipelineLayer *layer)
return &layer->big_state->fragment_snippets; return &layer->big_state->fragment_snippets;
} }
static gboolean
has_replace_hook (CoglPipelineLayer *layer,
CoglSnippetHook hook)
{
CoglPipelineSnippet *snippet;
COGL_LIST_FOREACH (snippet, get_layer_fragment_snippets (layer), list_node)
if (snippet->snippet->hook == hook && snippet->snippet->replace)
return TRUE;
return FALSE;
}
static gboolean static gboolean
_cogl_pipeline_fragend_glsl_start (CoglPipeline *pipeline, _cogl_pipeline_fragend_glsl_start (CoglPipeline *pipeline,
int n_layers, int n_layers,
@ -374,9 +388,7 @@ ensure_texture_lookup_generated (CoglPipelineShaderState *shader_state,
CoglPipeline *pipeline, CoglPipeline *pipeline,
CoglPipelineLayer *layer) CoglPipelineLayer *layer)
{ {
CoglHandle texture;
int unit_index = _cogl_pipeline_layer_get_unit_index (layer); int unit_index = _cogl_pipeline_layer_get_unit_index (layer);
const char *target_string, *tex_coord_swizzle;
CoglPipelineSnippetData snippet_data; CoglPipelineSnippetData snippet_data;
_COGL_GET_CONTEXT (ctx, NO_RETVAL); _COGL_GET_CONTEXT (ctx, NO_RETVAL);
@ -415,7 +427,12 @@ ensure_texture_lookup_generated (CoglPipelineShaderState *shader_state,
g_string_append (shader_state->source, ");\n"); g_string_append (shader_state->source, ");\n");
texture = _cogl_pipeline_layer_get_texture (layer); /* 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))
{
CoglHandle texture = _cogl_pipeline_layer_get_texture (layer);
const char *target_string, *tex_coord_swizzle;
if (texture == NULL) if (texture == NULL)
{ {
@ -481,6 +498,7 @@ ensure_texture_lookup_generated (CoglPipelineShaderState *shader_state,
target_string, unit_index, tex_coord_swizzle); target_string, unit_index, tex_coord_swizzle);
g_string_append (shader_state->header, "}\n"); g_string_append (shader_state->header, "}\n");
}
/* Wrap the texture lookup in any snippets that have been hooked */ /* Wrap the texture lookup in any snippets that have been hooked */
memset (&snippet_data, 0, sizeof (snippet_data)); memset (&snippet_data, 0, sizeof (snippet_data));

View File

@ -232,6 +232,20 @@ paint (TestState *state)
cogl_object_unref (snippet); cogl_object_unref (snippet);
/* Check replacing the texture lookup hook */
snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_TEXTURE_LOOKUP, NULL, NULL);
cogl_snippet_set_replace (snippet, "cogl_texel = vec4 (0.0, 0.0, 1.0, 0.0);");
pipeline = create_texture_pipeline ();
cogl_pipeline_add_layer_snippet (pipeline, 0, snippet);
cogl_push_source (pipeline);
cogl_rectangle_with_texture_coords (90, 0, 100, 10,
0, 0, 0, 0);
cogl_pop_source ();
cogl_object_unref (pipeline);
cogl_object_unref (snippet);
/* Test replacing a previous snippet */ /* Test replacing a previous snippet */
pipeline = create_texture_pipeline (); pipeline = create_texture_pipeline ();
@ -302,6 +316,7 @@ validate_result (void)
test_utils_check_pixel (65, 5, 0x00ff00ff); test_utils_check_pixel (65, 5, 0x00ff00ff);
test_utils_check_pixel (75, 5, 0x808000ff); test_utils_check_pixel (75, 5, 0x808000ff);
test_utils_check_pixel (85, 5, 0x00ffffff); test_utils_check_pixel (85, 5, 0x00ffffff);
test_utils_check_pixel (95, 5, 0x0000ffff);
test_utils_check_pixel (105, 5, 0xff0000ff); test_utils_check_pixel (105, 5, 0xff0000ff);
} }