Add fragment and vertex snippet hooks for global declarations

This adds hook points to add global function and variable declarations
to either the fragment or vertex shader. The declarations can then be
used by subsequent snippets. Only the ‘declarations’ string of the
snippet is used and the code is directly put in the global scope near
the top of the shader.

The reason this is necessary rather than just adding a normal snippet
with the declarations is that for the other hooks Cogl assumes that
the snippets are independent of each other. That means if a snippet
has a replace string then it will assume that it doesn't even need to
generate the code for earlier hooks which means the global
declarations would be lost.

Reviewed-by: Robert Bragg <robert@linux.intel.com>

(cherry picked from commit ebb82d5b0bc30487b7101dc66b769160b40f92ca)
This commit is contained in:
Neil Roberts 2013-02-26 17:52:20 +00:00
parent bd1e3e7642
commit 956d39ac30
6 changed files with 204 additions and 8 deletions

View File

@ -3,7 +3,7 @@
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2011 Intel Corporation.
* Copyright (C) 2011, 2013 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -89,6 +89,11 @@ typedef struct
void
_cogl_pipeline_snippet_generate_code (const CoglPipelineSnippetData *data);
void
_cogl_pipeline_snippet_generate_declarations (GString *declarations_buf,
CoglSnippetHook hook,
CoglPipelineSnippetList *list);
void
_cogl_pipeline_snippet_list_free (CoglPipelineSnippetList *list);

View File

@ -3,7 +3,7 @@
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2011 Intel Corporation.
* Copyright (C) 2011, 2013 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -183,6 +183,23 @@ _cogl_pipeline_snippet_generate_code (const CoglPipelineSnippetData *data)
}
}
void
_cogl_pipeline_snippet_generate_declarations (GString *declarations_buf,
CoglSnippetHook hook,
CoglPipelineSnippetList *snippets)
{
CoglPipelineSnippet *snippet;
COGL_LIST_FOREACH (snippet, snippets, list_node)
if (snippet->snippet->hook == hook)
{
const char *source;
if ((source = cogl_snippet_get_declarations (snippet->snippet)))
g_string_append (declarations_buf, source);
}
}
static void
_cogl_pipeline_snippet_free (CoglPipelineSnippet *pipeline_snippet)
{

View File

@ -3,7 +3,7 @@
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2011 Intel Corporation.
* Copyright (C) 2011, 2013 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -312,6 +312,12 @@ typedef struct _CoglSnippet CoglSnippet;
to within a pipeline. */
/**
* CoglSnippetHook:
* @COGL_SNIPPET_HOOK_VERTEX_GLOBALS: A hook for declaring global data
* that can be shared with all other snippets that are on a vertex
* hook.
* @COGL_SNIPPET_HOOK_FRAGMENT_GLOBALS: A hook for declaring global
* data wthat can be shared with all other snippets that are on a
* fragment hook.
* @COGL_SNIPPET_HOOK_VERTEX: A hook for the entire vertex processing
* stage of the pipeline.
* @COGL_SNIPPET_HOOK_VERTEX_TRANSFORM: A hook for the vertex transformation.
@ -330,6 +336,30 @@ typedef struct _CoglSnippet CoglSnippet;
*
* <glosslist>
* <glossentry>
* <glossterm>%COGL_SNIPPET_HOOK_VERTEX_GLOBALS</glossterm>
* <glossdef>
* <para>
* Adds a shader snippet at the beginning of the global section of the
* shader for the vertex processing. Any declarations here can be
* shared with all other snippets that are attached to a vertex hook.
* Only the declarations string is used and the other strings are
* ignored.
* </para>
* </glossdef>
* </glossentry>
* <glossentry>
* <glossterm>%COGL_SNIPPET_HOOK_FRAGMENT_GLOBALS</glossterm>
* <glossdef>
* <para>
* Adds a shader snippet at the beginning of the global section of the
* shader for the fragment processing. Any declarations here can be
* shared with all other snippets that are attached to a fragment
* hook. Only the declarations string is used and the other strings
* are ignored.
* </para>
* </glossdef>
* </glossentry>
* <glossentry>
* <glossterm>%COGL_SNIPPET_HOOK_VERTEX</glossterm>
* <glossdef>
* <para>
@ -552,9 +582,11 @@ typedef enum {
/* Per pipeline vertex hooks */
COGL_SNIPPET_HOOK_VERTEX = 0,
COGL_SNIPPET_HOOK_VERTEX_TRANSFORM,
COGL_SNIPPET_HOOK_VERTEX_GLOBALS,
/* Per pipeline fragment hooks */
COGL_SNIPPET_HOOK_FRAGMENT = 2048,
COGL_SNIPPET_HOOK_FRAGMENT_GLOBALS,
/* Per layer vertex hooks */
COGL_SNIPPET_HOOK_TEXTURE_COORD_TRANSFORM = 4096,

View File

@ -3,7 +3,7 @@
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2008,2009,2010 Intel Corporation.
* Copyright (C) 2008,2009,2010,2013 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -207,6 +207,21 @@ has_replace_hook (CoglPipelineLayer *layer,
return FALSE;
}
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);
}
static void
_cogl_pipeline_fragend_glsl_start (CoglPipeline *pipeline,
int n_layers,
@ -315,6 +330,8 @@ _cogl_pipeline_fragend_glsl_start (CoglPipeline *pipeline,
shader_state->source = ctx->codegen_source_buffer;
COGL_LIST_INIT (&shader_state->layers);
add_global_declarations (pipeline, shader_state);
g_string_append (shader_state->source,
"void\n"
"cogl_generated_source ()\n"

View File

@ -3,7 +3,7 @@
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2010 Intel Corporation.
* Copyright (C) 2010,2013 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -140,6 +140,21 @@ get_layer_vertex_snippets (CoglPipelineLayer *layer)
return &layer->big_state->vertex_snippets;
}
static void
add_global_declarations (CoglPipeline *pipeline,
CoglPipelineShaderState *shader_state)
{
CoglSnippetHook hook = COGL_SNIPPET_HOOK_VERTEX_GLOBALS;
CoglPipelineSnippetList *snippets = get_vertex_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);
}
static void
_cogl_pipeline_vertend_glsl_start (CoglPipeline *pipeline,
int n_layers,
@ -236,6 +251,8 @@ _cogl_pipeline_vertend_glsl_start (CoglPipeline *pipeline,
shader_state->header = ctx->codegen_header_buffer;
shader_state->source = ctx->codegen_source_buffer;
add_global_declarations (pipeline, shader_state);
g_string_append (shader_state->source,
"void\n"
"cogl_generated_source ()\n"

View File

@ -6,7 +6,7 @@
typedef struct _TestState
{
int padding;
int fb_width, fb_height;
} TestState;
typedef void (* SnippetTestFunc) (TestState *state);
@ -532,6 +532,109 @@ test_vertex_transform_hook (TestState *state)
test_utils_check_pixel (test_fb, 155, 5, 0xff00ffff);
}
static void
test_global_vertex_hook (TestState *state)
{
CoglPipeline *pipeline;
CoglSnippet *snippet;
pipeline = cogl_pipeline_new (test_ctx);
/* Creates a function in the global declarations hook which is used
* by a subsequent snippet. The subsequent snippets replace any
* previous snippets but this shouldn't prevent the global
* declarations from being generated */
snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_VERTEX_GLOBALS,
/* declarations */
"float\n"
"multiply_by_two (float number)\n"
"{\n"
" return number * 2.0;\n"
"}\n",
/* post */
"This string shouldn't be used so "
"we can safely put garbage in here.");
cogl_snippet_set_pre (snippet,
"This string shouldn't be used so "
"we can safely put garbage in here.");
cogl_snippet_set_replace (snippet,
"This string shouldn't be used so "
"we can safely put garbage in here.");
cogl_pipeline_add_snippet (pipeline, snippet);
cogl_object_unref (snippet);
snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_VERTEX,
NULL, /* declarations */
NULL /* replace */);
cogl_snippet_set_replace (snippet,
"cogl_color_out.r = multiply_by_two (0.5);\n"
"cogl_color_out.gba = vec3 (0.0, 0.0, 1.0);\n"
"cogl_position_out = cogl_position_in;\n");
cogl_pipeline_add_snippet (pipeline, snippet);
cogl_object_unref (snippet);
cogl_framebuffer_draw_rectangle (test_fb,
pipeline,
-1, 1,
10.0f * 2.0f / state->fb_width - 1.0f,
10.0f * 2.0f / state->fb_height - 1.0f);
cogl_object_unref (pipeline);
test_utils_check_pixel (test_fb, 5, 5, 0xff0000ff);
}
static void
test_global_fragment_hook (TestState *state)
{
CoglPipeline *pipeline;
CoglSnippet *snippet;
pipeline = cogl_pipeline_new (test_ctx);
/* Creates a function in the global declarations hook which is used
* by a subsequent snippet. The subsequent snippets replace any
* previous snippets but this shouldn't prevent the global
* declarations from being generated */
snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_FRAGMENT_GLOBALS,
/* declarations */
"float\n"
"multiply_by_four (float number)\n"
"{\n"
" return number * 4.0;\n"
"}\n",
/* post */
"This string shouldn't be used so "
"we can safely put garbage in here.");
cogl_snippet_set_pre (snippet,
"This string shouldn't be used so "
"we can safely put garbage in here.");
cogl_snippet_set_replace (snippet,
"This string shouldn't be used so "
"we can safely put garbage in here.");
cogl_pipeline_add_snippet (pipeline, snippet);
cogl_object_unref (snippet);
snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_FRAGMENT,
NULL, /* declarations */
NULL /* replace */);
cogl_snippet_set_replace (snippet,
"cogl_color_out.r = multiply_by_four (0.25);\n"
"cogl_color_out.gba = vec3 (0.0, 0.0, 1.0);\n");
cogl_pipeline_add_snippet (pipeline, snippet);
cogl_object_unref (snippet);
cogl_framebuffer_draw_rectangle (test_fb,
pipeline,
0, 0, 10, 10);
cogl_object_unref (pipeline);
test_utils_check_pixel (test_fb, 5, 5, 0xff0000ff);
}
static void
test_snippet_order (TestState *state)
{
@ -668,6 +771,8 @@ tests[] =
test_modify_vertex_layer,
test_replace_vertex_layer,
test_vertex_transform_hook,
test_global_fragment_hook,
test_global_vertex_hook,
test_snippet_order,
test_naming_texture_units,
test_snippet_properties
@ -693,10 +798,13 @@ test_snippets (void)
{
TestState state;
state.fb_width = cogl_framebuffer_get_width (test_fb);
state.fb_height = cogl_framebuffer_get_height (test_fb);
cogl_framebuffer_orthographic (test_fb,
0, 0,
cogl_framebuffer_get_width (test_fb),
cogl_framebuffer_get_height (test_fb),
state.fb_width,
state.fb_height,
-1,
100);