summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNeil Roberts <neil@linux.intel.com>2013-02-26 17:52:20 +0000
committerNeil Roberts <neil@linux.intel.com>2013-02-27 14:43:55 +0000
commit956d39ac30a9013f56d237d216a2971008918d1a (patch)
tree0842fdee25a0689d3c1c6651ab0fd8b84cc7ffbd /tests
parentbd1e3e7642e23a863728157e3d8226d24f9906d8 (diff)
downloadcogl-956d39ac30a9013f56d237d216a2971008918d1a.tar.gz
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)
Diffstat (limited to 'tests')
-rw-r--r--tests/conform/test-snippets.c114
1 files changed, 111 insertions, 3 deletions
diff --git a/tests/conform/test-snippets.c b/tests/conform/test-snippets.c
index 51bd090a..f27af80d 100644
--- a/tests/conform/test-snippets.c
+++ b/tests/conform/test-snippets.c
@@ -6,7 +6,7 @@
typedef struct _TestState
{
- int padding;
+ int fb_width, fb_height;
} TestState;
typedef void (* SnippetTestFunc) (TestState *state);
@@ -533,6 +533,109 @@ test_vertex_transform_hook (TestState *state)
}
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)
{
CoglPipeline *pipeline;
@@ -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);