/** * shell_glsl_quad_add_glsl_snippet: * @quad: a #ShellGLSLQuad * @hook: where to insert the code * @declarations: GLSL declarations * @code: GLSL code * @is_replace: wheter Cogl code should be replaced by the custom shader * * Adds a GLSL snippet to the pipeline used for drawing the actor texture. * See #CoglSnippet for details. * * This is only valid inside the a call to the build_pipeline() virtual * function. */ void shell_glsl_quad_add_glsl_snippet (ShellGLSLQuad *quad, ShellSnippetHook hook, const char *declarations, const char *code, gboolean is_replace) { ShellGLSLQuadClass *klass = SHELL_GLSL_QUAD_GET_CLASS (quad); CoglSnippet *snippet; g_return_if_fail (klass->base_pipeline != NULL); if (is_replace) { snippet = cogl_snippet_new ((CoglSnippetHook)hook, declarations, NULL); cogl_snippet_set_replace (snippet, code); } else { snippet = cogl_snippet_new ((CoglSnippetHook)hook, declarations, code); } if (hook == SHELL_SNIPPET_HOOK_VERTEX || hook == SHELL_SNIPPET_HOOK_FRAGMENT) cogl_pipeline_add_snippet (klass->base_pipeline, snippet); else cogl_pipeline_add_layer_snippet (klass->base_pipeline, 0, snippet); cogl_object_unref (snippet); }
CoglSnippet * meta_wayland_egl_stream_create_snippet (void) { CoglSnippet *snippet; snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_TEXTURE_LOOKUP, "uniform samplerExternalOES tex_external;", NULL); cogl_snippet_set_replace (snippet, "cogl_texel = texture2D (tex_external,\n" " cogl_tex_coord.xy);"); return snippet; }
static void paint (TestState *state) { CoglPipeline *pipeline = cogl_pipeline_new (test_ctx); CoglTexture *tex; CoglError *error = NULL; CoglSnippet *snippet; cogl_framebuffer_clear4f (test_fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1); /* Set the primary vertex color as red */ cogl_pipeline_set_color4f (pipeline, 1, 0, 0, 1); /* Override the vertex color in the texture environment with a constant green color provided by a texture */ tex = create_dummy_texture (); cogl_pipeline_set_layer_texture (pipeline, 0, tex); cogl_object_unref (tex); if (!cogl_pipeline_set_layer_combine (pipeline, 0, "RGBA=REPLACE(TEXTURE)", &error)) { g_warning ("Error setting layer combine: %s", error->message); g_assert_not_reached (); } /* Set up a dummy vertex shader that does nothing but the usual modelview-projection transform */ snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_VERTEX, NULL, /* no declarations */ NULL); /* no "post" code */ cogl_snippet_set_replace (snippet, " cogl_position_out = " "cogl_modelview_projection_matrix * " "cogl_position_in;\n" " cogl_color_out = cogl_color_in;\n" " cogl_tex_coord0_out = cogl_tex_coord_in;\n"); /* Draw something without the snippet */ cogl_framebuffer_draw_rectangle (test_fb, pipeline, 0, 0, 50, 50); /* Draw it again using the snippet. It should look exactly the same */ cogl_pipeline_add_snippet (pipeline, snippet); cogl_object_unref (snippet); cogl_framebuffer_draw_rectangle (test_fb, pipeline, 50, 0, 100, 50); cogl_object_unref (pipeline); }
static CoglPipeline * create_pipeline_for_shader (TestState *state, const char *declarations, const char *fragment_source) { CoglPipeline *pipeline = cogl_pipeline_new (test_ctx); CoglSnippet *snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_FRAGMENT, declarations, NULL); cogl_snippet_set_replace (snippet, fragment_source); cogl_pipeline_add_snippet (pipeline, snippet); cogl_object_unref (snippet); return pipeline; }
static void test_short_verts (TestState *state, int offset_x, int offset_y) { CoglAttribute *attributes[1]; CoglAttributeBuffer *buffer; CoglPipeline *pipeline, *pipeline2; CoglSnippet *snippet; static const ShortVert short_verts[] = { { -10, -10 }, { -1, -10 }, { -5, -1 } }; pipeline = cogl_pipeline_new (test_ctx); snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_VERTEX_TRANSFORM, "attribute vec2 pos;", NULL); cogl_snippet_set_replace (snippet, "cogl_position_out = " "cogl_modelview_projection_matrix * " "vec4 (pos, 0.0, 1.0);"); cogl_pipeline_add_snippet (pipeline, snippet); cogl_object_unref (snippet); cogl_pipeline_set_color4ub (pipeline, 255, 0, 0, 255); buffer = cogl_attribute_buffer_new (test_ctx, sizeof (short_verts), short_verts); attributes[0] = cogl_attribute_new (buffer, "pos", sizeof (ShortVert), G_STRUCT_OFFSET (ShortVert, x), 2, /* n_components */ COGL_ATTRIBUTE_TYPE_SHORT); cogl_framebuffer_push_matrix (test_fb); cogl_framebuffer_translate (test_fb, offset_x + 10.0f, offset_y + 10.0f, 0.0f); cogl_framebuffer_draw_attributes (test_fb, pipeline, COGL_VERTICES_MODE_TRIANGLES, 0, /* first_vertex */ 3, /* n_vertices */ attributes, 1 /* n_attributes */); cogl_framebuffer_pop_matrix (test_fb); cogl_object_unref (attributes[0]); /* Test again treating the attribute as unsigned */ attributes[0] = cogl_attribute_new (buffer, "pos", sizeof (ShortVert), G_STRUCT_OFFSET (ShortVert, x), 2, /* n_components */ COGL_ATTRIBUTE_TYPE_UNSIGNED_SHORT); pipeline2 = cogl_pipeline_copy (pipeline); cogl_pipeline_set_color4ub (pipeline2, 0, 255, 0, 255); cogl_framebuffer_push_matrix (test_fb); cogl_framebuffer_translate (test_fb, offset_x + 10.0f - 65525.0f, offset_y - 65525, 0.0f); cogl_framebuffer_draw_attributes (test_fb, pipeline2, COGL_VERTICES_MODE_TRIANGLES, 0, /* first_vertex */ 3, /* n_vertices */ attributes, 1 /* n_attributes */); cogl_framebuffer_pop_matrix (test_fb); cogl_object_unref (attributes[0]); cogl_object_unref (pipeline2); cogl_object_unref (pipeline); cogl_object_unref (buffer); test_utils_check_pixel (test_fb, offset_x + 5, offset_y + 5, 0xff0000ff); test_utils_check_pixel (test_fb, offset_x + 15, offset_y + 5, 0x00ff00ff); }
static void st_background_effect_init (StBackgroundEffect *self) { CoglContext *ctx; StBackgroundEffectClass *klass = ST_BACKGROUND_EFFECT_GET_CLASS (self); if (G_UNLIKELY (klass->base_pipeline == NULL)) { ctx = clutter_backend_get_cogl_context (clutter_get_default_backend ()); klass->base_pipeline = cogl_pipeline_new (ctx); } self->pipeline0 = cogl_pipeline_copy (klass->base_pipeline); self->pipeline1 = cogl_pipeline_copy (klass->base_pipeline); self->pipeline2 = cogl_pipeline_copy (klass->base_pipeline); self->pipeline3 = cogl_pipeline_copy (klass->base_pipeline); self->pipeline4 = cogl_pipeline_copy (klass->base_pipeline); CoglSnippet *snippet; snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_TEXTURE_LOOKUP, box_blur_glsl_declarations, NULL); cogl_snippet_set_replace (snippet, box_blur_glsl_shader); cogl_pipeline_add_layer_snippet (self->pipeline0, 0, snippet); cogl_pipeline_add_layer_snippet (self->pipeline1, 0, snippet); cogl_pipeline_add_layer_snippet (self->pipeline3, 0, snippet); cogl_object_unref (snippet); cogl_pipeline_set_layer_wrap_mode (self->pipeline0, 0, COGL_MATERIAL_WRAP_MODE_CLAMP_TO_EDGE); cogl_pipeline_set_layer_wrap_mode (self->pipeline0, 1, COGL_PIPELINE_WRAP_MODE_REPEAT); cogl_pipeline_set_layer_wrap_mode (self->pipeline1, 0, COGL_MATERIAL_WRAP_MODE_CLAMP_TO_EDGE); cogl_pipeline_set_layer_wrap_mode (self->pipeline2, 0, COGL_MATERIAL_WRAP_MODE_CLAMP_TO_EDGE); cogl_pipeline_set_layer_wrap_mode (self->pipeline3, 0, COGL_MATERIAL_WRAP_MODE_CLAMP_TO_EDGE); cogl_pipeline_set_layer_wrap_mode (self->pipeline4, 0, COGL_MATERIAL_WRAP_MODE_CLAMP_TO_EDGE); cogl_pipeline_set_cull_face_mode (self->pipeline0, COGL_PIPELINE_CULL_FACE_MODE_NONE); cogl_pipeline_set_cull_face_mode (self->pipeline1, COGL_PIPELINE_CULL_FACE_MODE_NONE); cogl_pipeline_set_cull_face_mode (self->pipeline2, COGL_PIPELINE_CULL_FACE_MODE_NONE); cogl_pipeline_set_cull_face_mode (self->pipeline3, COGL_PIPELINE_CULL_FACE_MODE_NONE); cogl_pipeline_set_cull_face_mode (self->pipeline4, COGL_PIPELINE_CULL_FACE_MODE_NONE); cogl_pipeline_set_layer_filters (self->pipeline0, 0, COGL_PIPELINE_FILTER_LINEAR, COGL_PIPELINE_FILTER_LINEAR); cogl_pipeline_set_layer_filters (self->pipeline0, 1, COGL_PIPELINE_FILTER_NEAREST, COGL_PIPELINE_FILTER_NEAREST); cogl_pipeline_set_layer_filters (self->pipeline1, 0, COGL_PIPELINE_FILTER_LINEAR, COGL_PIPELINE_FILTER_LINEAR); cogl_pipeline_set_layer_filters (self->pipeline2, 0, COGL_PIPELINE_FILTER_LINEAR, COGL_PIPELINE_FILTER_LINEAR); cogl_pipeline_set_layer_filters (self->pipeline3, 0, COGL_PIPELINE_FILTER_LINEAR, COGL_PIPELINE_FILTER_LINEAR); cogl_pipeline_set_layer_filters (self->pipeline4, 0, COGL_PIPELINE_FILTER_LINEAR, COGL_PIPELINE_FILTER_LINEAR); cogl_pipeline_set_layer_null_texture (self->pipeline0, 0, COGL_TEXTURE_TYPE_2D); cogl_pipeline_set_layer_null_texture (self->pipeline1, 0, COGL_TEXTURE_TYPE_2D); cogl_pipeline_set_layer_null_texture (self->pipeline2, 0, COGL_TEXTURE_TYPE_2D); cogl_pipeline_set_layer_null_texture (self->pipeline3, 0, COGL_TEXTURE_TYPE_2D); cogl_pipeline_set_layer_null_texture (self->pipeline4, 0, COGL_TEXTURE_TYPE_2D); self->pixel_step_uniform0 = cogl_pipeline_get_uniform_location (self->pipeline0, "pixel_step"); self->BumpTex_uniform = cogl_pipeline_get_uniform_location (self->pipeline0, "BumpTex"); self->bump_step_uniform = cogl_pipeline_get_uniform_location (self->pipeline0, "bump_step"); self->pixel_step_uniform1 = cogl_pipeline_get_uniform_location (self->pipeline1, "pixel_step"); self->pixel_step_uniform2 = cogl_pipeline_get_uniform_location (self->pipeline3, "pixel_step"); cogl_pipeline_set_blend (self->pipeline0, "RGBA = ADD(SRC_COLOR, DST_COLOR*0)", NULL); cogl_pipeline_set_blend (self->pipeline1, "RGBA = ADD (SRC_COLOR*DST_COLOR[A], DST_COLOR*(1-DST_COLOR[A]))", NULL); cogl_pipeline_set_color4ub (self->pipeline1, 0xff, 0xff, 0xff, 0xff); cogl_pipeline_set_alpha_test_function (self->pipeline2, COGL_PIPELINE_ALPHA_FUNC_LESS, 0.004f); cogl_pipeline_set_color_mask (self->pipeline2, COGL_COLOR_MASK_ALPHA); cogl_pipeline_set_blend (self->pipeline2, "RGBA = ADD(SRC_COLOR, DST_COLOR*0)", NULL); cogl_pipeline_set_alpha_test_function (self->pipeline3, COGL_PIPELINE_ALPHA_FUNC_GEQUAL, 0.004f); cogl_pipeline_set_color_mask (self->pipeline3, COGL_COLOR_MASK_ALL); cogl_pipeline_set_blend (self->pipeline3, "RGBA = ADD (SRC_COLOR, DST_COLOR*(1-SRC_COLOR[A]))", NULL); cogl_pipeline_set_alpha_test_function (self->pipeline4, COGL_PIPELINE_ALPHA_FUNC_GEQUAL, 0.004f); cogl_pipeline_set_color_mask (self->pipeline4, COGL_COLOR_MASK_ALL); cogl_pipeline_set_blend (self->pipeline4, "RGBA = ADD (SRC_COLOR, DST_COLOR*(1-SRC_COLOR[A]))", NULL); self->bg_texture = NULL; self->bg_sub_texture = NULL; self->bumpmap_location = "/usr/share/cinnamon/bumpmaps/frost.png"; self->bg_bumpmap = st_cogl_texture_new_from_file_wrapper (self->bumpmap_location, COGL_TEXTURE_NO_SLICING, COGL_PIXEL_FORMAT_RGBA_8888_PRE); if (self->bg_bumpmap != NULL) { self->bumptex_width_i = cogl_texture_get_width (self->bg_bumpmap); self->bumptex_height_i = cogl_texture_get_height (self->bg_bumpmap); cogl_pipeline_set_layer_texture (self->pipeline0, 1, self->bg_bumpmap); } else { cogl_pipeline_set_layer_null_texture (self->pipeline0, 1, COGL_TEXTURE_TYPE_2D); } cogl_pipeline_set_layer_combine (self->pipeline0,1, "RGBA = REPLACE (PREVIOUS)", NULL); self->old_time = 0; self->opacity = 0; }
static CoglPipeline * create_1d_gaussian_blur_pipeline (RutContext *ctx, int n_taps) { static GHashTable *pipeline_cache = NULL; CoglPipeline *pipeline; CoglSnippet *snippet; GString *shader; CoglDepthState depth_state; int i; /* initialize the pipeline cache. The shaders are only dependent on the * number of taps, not the sigma, so we cache the corresponding pipelines * in a hash table 'n_taps' => 'pipeline' */ if (G_UNLIKELY (pipeline_cache == NULL)) { pipeline_cache = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, /* key destroy notify */ (GDestroyNotify) cogl_object_unref); } pipeline = g_hash_table_lookup (pipeline_cache, GINT_TO_POINTER (n_taps)); if (pipeline) return cogl_object_ref (pipeline); shader = g_string_new (NULL); g_string_append_printf (shader, "uniform vec2 pixel_step;\n" "uniform float factors[%i];\n", n_taps); snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_TEXTURE_LOOKUP, shader->str, NULL /* post */); g_string_set_size (shader, 0); pipeline = cogl_pipeline_new (ctx->cogl_context); cogl_pipeline_set_layer_null_texture (pipeline, 0, /* layer_num */ COGL_TEXTURE_TYPE_2D); cogl_pipeline_set_layer_wrap_mode (pipeline, 0, /* layer_num */ COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE); cogl_pipeline_set_layer_filters (pipeline, 0, /* layer_num */ COGL_PIPELINE_FILTER_NEAREST, COGL_PIPELINE_FILTER_NEAREST); for (i = 0; i < n_taps; i++) { g_string_append (shader, "cogl_texel "); if (i == 0) g_string_append (shader, "="); else g_string_append (shader, "+="); g_string_append_printf (shader, " texture2D (cogl_sampler, " "cogl_tex_coord.st"); if (i != (n_taps - 1) / 2) g_string_append_printf (shader, " + pixel_step * %f", (float) (i - ((n_taps - 1) / 2))); g_string_append_printf (shader, ") * factors[%i];\n", i); } cogl_snippet_set_replace (snippet, shader->str); g_string_free (shader, TRUE); cogl_pipeline_add_layer_snippet (pipeline, 0, snippet); cogl_object_unref (snippet); cogl_pipeline_set_blend (pipeline, "RGBA=ADD(SRC_COLOR, 0)", NULL); cogl_depth_state_init (&depth_state); cogl_depth_state_set_write_enabled (&depth_state, FALSE); cogl_depth_state_set_test_enabled (&depth_state, FALSE); cogl_pipeline_set_depth_state (pipeline, &depth_state, NULL); g_hash_table_insert (pipeline_cache, GINT_TO_POINTER (n_taps), pipeline); return pipeline; }
static void do_test (CoglBool check_orientation, CoglBool use_glsl) { int fb_width = cogl_framebuffer_get_width (test_fb); int fb_height = cogl_framebuffer_get_height (test_fb); CoglPrimitive *prim; CoglError *error = NULL; CoglTexture2D *tex_2d; CoglPipeline *pipeline, *solid_pipeline; int tex_height; cogl_framebuffer_orthographic (test_fb, 0, 0, /* x_1, y_1 */ fb_width, /* x_2 */ fb_height /* y_2 */, -1, 100 /* near/far */); cogl_framebuffer_clear4f (test_fb, COGL_BUFFER_BIT_COLOR, 1.0f, 1.0f, 1.0f, 1.0f); /* If we're not checking the orientation of the point sprite then * we'll set the height of the texture to 1 so that the vertical * orientation does not matter */ if (check_orientation) tex_height = 2; else tex_height = 1; tex_2d = cogl_texture_2d_new_from_data (test_ctx, 2, tex_height, /* width/height */ COGL_PIXEL_FORMAT_RGB_888, COGL_PIXEL_FORMAT_ANY, 6, /* row stride */ tex_data, &error); g_assert (tex_2d != NULL); g_assert (error == NULL); pipeline = cogl_pipeline_new (test_ctx); cogl_pipeline_set_layer_texture (pipeline, 0, tex_2d); cogl_pipeline_set_layer_filters (pipeline, 0, /* layer_index */ COGL_PIPELINE_FILTER_NEAREST, COGL_PIPELINE_FILTER_NEAREST); cogl_pipeline_set_point_size (pipeline, POINT_SIZE); /* If we're using GLSL then we don't need to enable point sprite * coords and we can just directly reference cogl_point_coord in the * snippet */ if (use_glsl) { CoglSnippet *snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_TEXTURE_LOOKUP, NULL, /* declarations */ NULL /* post */); static const char source[] = " cogl_texel = texture2D (cogl_sampler, cogl_point_coord);\n"; cogl_snippet_set_replace (snippet, source); /* Keep a reference to the original pipeline because there is no * way to remove a snippet in order to recreate the solid * pipeline */ solid_pipeline = cogl_pipeline_copy (pipeline); cogl_pipeline_add_layer_snippet (pipeline, 0, snippet); cogl_object_unref (snippet); } else { CoglBool res = cogl_pipeline_set_layer_point_sprite_coords_enabled (pipeline, /* layer_index */ 0, /* enable */ TRUE, &error); g_assert (res == TRUE); g_assert (error == NULL); solid_pipeline = cogl_pipeline_copy (pipeline); res = cogl_pipeline_set_layer_point_sprite_coords_enabled (solid_pipeline, /* layer_index */ 0, /* enable */ FALSE, &error); g_assert (res == TRUE); g_assert (error == NULL); } prim = cogl_primitive_new_p2t2 (test_ctx, COGL_VERTICES_MODE_POINTS, 1, /* n_vertices */ &point); cogl_primitive_draw (prim, test_fb, pipeline); /* Render the primitive again without point sprites to make sure disabling it works */ cogl_framebuffer_push_matrix (test_fb); cogl_framebuffer_translate (test_fb, POINT_SIZE * 2, /* x */ 0.0f, /* y */ 0.0f /* z */); cogl_primitive_draw (prim, test_fb, solid_pipeline); cogl_framebuffer_pop_matrix (test_fb); cogl_object_unref (prim); cogl_object_unref (solid_pipeline); cogl_object_unref (pipeline); cogl_object_unref (tex_2d); test_utils_check_pixel (test_fb, POINT_SIZE - POINT_SIZE / 4, POINT_SIZE - POINT_SIZE / 4, 0x0000ffff); test_utils_check_pixel (test_fb, POINT_SIZE + POINT_SIZE / 4, POINT_SIZE - POINT_SIZE / 4, 0x00ff00ff); test_utils_check_pixel (test_fb, POINT_SIZE - POINT_SIZE / 4, POINT_SIZE + POINT_SIZE / 4, check_orientation ? 0x00ffffff : 0x0000ffff); test_utils_check_pixel (test_fb, POINT_SIZE + POINT_SIZE / 4, POINT_SIZE + POINT_SIZE / 4, check_orientation ? 0xff0000ff : 0x00ff00ff); /* When rendering without the point sprites all of the texture coordinates should be 0,0 so it should get the top-left texel which is blue */ test_utils_check_region (test_fb, POINT_SIZE * 3 - POINT_SIZE / 2 + 1, POINT_SIZE - POINT_SIZE / 2 + 1, POINT_SIZE - 2, POINT_SIZE - 2, 0x0000ffff); if (cogl_test_verbose ()) g_print ("OK\n"); }