static CoglPipeline * create_two_layer_pipeline (void) { CoglPipeline *pipeline = cogl_pipeline_new (test_ctx); CoglColor color; /* The pipeline is initially black */ cogl_pipeline_set_color4ub (pipeline, 0, 0, 0, 255); /* The first layer adds a full red component */ cogl_color_init_from_4ub (&color, 255, 0, 0, 255); cogl_pipeline_set_layer_combine_constant (pipeline, 0, &color); cogl_pipeline_set_layer_combine (pipeline, 0, /* layer_num */ "RGBA=ADD(PREVIOUS,CONSTANT)", NULL); /* The second layer adds a full green component */ cogl_color_init_from_4ub (&color, 0, 255, 0, 255); cogl_pipeline_set_layer_combine_constant (pipeline, 1, &color); cogl_pipeline_set_layer_combine (pipeline, 1, /* layer_num */ "RGBA=ADD(PREVIOUS,CONSTANT)", NULL); return pipeline; }
static void test_multi_texture (TestState *state) { CoglPipeline *pipeline; CoglTexture3D *tex_3d; CoglTexture2D *tex_2d; uint8_t tex_data[4]; cogl_framebuffer_clear4f (test_fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1); /* Tests a pipeline that is using multi-texturing to combine a 3D texture with a 2D texture. The texture from another layer is sampled with TEXTURE_? just to pick up a specific bug that was happening with the ARBfp fragend */ pipeline = cogl_pipeline_new (test_ctx); tex_data[0] = 0xff; tex_data[1] = 0x00; tex_data[2] = 0x00; tex_data[3] = 0xff; tex_2d = cogl_texture_2d_new_from_data (test_ctx, 1, 1, /* width/height */ COGL_PIXEL_FORMAT_RGBA_8888_PRE, 4, /* rowstride */ tex_data, NULL); cogl_pipeline_set_layer_texture (pipeline, 0, tex_2d); tex_data[0] = 0x00; tex_data[1] = 0xff; tex_data[2] = 0x00; tex_data[3] = 0xff; tex_3d = cogl_texture_3d_new_from_data (test_ctx, 1, 1, 1, /* width/height/depth */ COGL_PIXEL_FORMAT_RGBA_8888_PRE, 4, /* rowstride */ 4, /* image_stride */ tex_data, NULL); cogl_pipeline_set_layer_texture (pipeline, 1, tex_3d); cogl_pipeline_set_layer_combine (pipeline, 0, "RGBA = REPLACE(PREVIOUS)", NULL); cogl_pipeline_set_layer_combine (pipeline, 1, "RGBA = ADD(TEXTURE_0, TEXTURE_1)", NULL); cogl_framebuffer_draw_rectangle (test_fb, pipeline, 0, 0, 10, 10); test_utils_check_pixel (test_fb, 5, 5, 0xffff00ff); cogl_object_unref (tex_2d); cogl_object_unref (tex_3d); cogl_object_unref (pipeline); }
CoglPipeline * _st_create_shadow_pipeline (StShadow *shadow_spec, CoglTexture *src_texture) { ClutterBackend *backend = clutter_get_default_backend (); CoglContext *ctx = clutter_backend_get_cogl_context (backend); static CoglPipeline *shadow_pipeline_template = NULL; CoglPipeline *pipeline; CoglTexture *texture; guchar *pixels_in, *pixels_out; gint width_in, height_in, rowstride_in; gint width_out, height_out, rowstride_out; g_return_val_if_fail (shadow_spec != NULL, NULL); g_return_val_if_fail (src_texture != NULL, NULL); width_in = cogl_texture_get_width (src_texture); height_in = cogl_texture_get_height (src_texture); rowstride_in = (width_in + 3) & ~3; pixels_in = g_malloc0 (rowstride_in * height_in); cogl_texture_get_data (src_texture, COGL_PIXEL_FORMAT_A_8, rowstride_in, pixels_in); pixels_out = blur_pixels (pixels_in, width_in, height_in, rowstride_in, shadow_spec->blur, &width_out, &height_out, &rowstride_out); g_free (pixels_in); texture = COGL_TEXTURE (cogl_texture_2d_new_from_data (ctx, width_out, height_out, COGL_PIXEL_FORMAT_A_8, rowstride_out, pixels_out, NULL)); g_free (pixels_out); if (G_UNLIKELY (shadow_pipeline_template == NULL)) { CoglContext *ctx = clutter_backend_get_cogl_context (clutter_get_default_backend ()); shadow_pipeline_template = cogl_pipeline_new (ctx); /* We set up the pipeline to blend the shadow texture with the combine * constant, but defer setting the latter until painting, so that we can * take the actor's overall opacity into account. */ cogl_pipeline_set_layer_combine (shadow_pipeline_template, 0, "RGBA = MODULATE (CONSTANT, TEXTURE[A])", NULL); } pipeline = cogl_pipeline_copy (shadow_pipeline_template); cogl_pipeline_set_layer_texture (pipeline, 0, texture); cogl_object_unref (texture); return pipeline; }
static void setup_pipeline_from_cache_entry (CoglGstVideoSink *sink, CoglPipeline *pipeline, SnippetCacheEntry *cache_entry, int n_layers) { CoglGstVideoSinkPrivate *priv = sink->priv; if (cache_entry) { int i; /* The global sampling function gets added to both the fragment * and vertex stages. The hope is that the GLSL compiler will * easily remove the dead code if it's not actually used */ cogl_pipeline_add_snippet (pipeline, cache_entry->vertex_snippet); cogl_pipeline_add_snippet (pipeline, cache_entry->fragment_snippet); /* Set all of the layers to just directly copy from the previous * layer so that it won't redundantly generate code to sample * the intermediate textures */ for (i = 0; i < n_layers; i++) cogl_pipeline_set_layer_combine (pipeline, priv->custom_start + i, "RGBA=REPLACE(PREVIOUS)", NULL); if (priv->default_sample) cogl_pipeline_add_layer_snippet (pipeline, priv->custom_start + n_layers - 1, cache_entry->default_sample_snippet); } priv->frame_dirty = TRUE; }
static CoglPipeline * get_base_texture_alpha_pipeline (CoglPangoPipelineCache *cache) { if (cache->base_texture_alpha_pipeline == NULL) { CoglPipeline *pipeline; pipeline = cogl_pipeline_copy (get_base_texture_rgba_pipeline (cache)); cache->base_texture_alpha_pipeline = pipeline; /* The default combine mode of materials is to modulate (A x B) * the texture RGBA channels with the RGBA channels of the * previous layer (which in our case is just the font color) * * Since the RGB for an alpha texture is defined as 0, this gives us: * * result.rgb = color.rgb * 0 * result.a = color.a * texture.a * * What we want is premultiplied rgba values: * * result.rgba = color.rgb * texture.a * result.a = color.a * texture.a */ cogl_pipeline_set_layer_combine (pipeline, 0, /* layer */ "RGBA = MODULATE (PREVIOUS, TEXTURE[A])", NULL); } return cache->base_texture_alpha_pipeline; }
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 void create_pipeline (CoglTexture **tex_out, CoglPipeline **pipeline_out) { CoglTexture2D *tex; CoglPipeline *pipeline; static const uint8_t tex_data[] = { 0x00, 0x44, 0x88, 0xcc }; tex = cogl_texture_2d_new_from_data (test_ctx, 2, 2, /* width/height */ COGL_PIXEL_FORMAT_A_8, /* format */ COGL_PIXEL_FORMAT_ANY, /* int. format */ 2, /* rowstride */ tex_data, NULL); pipeline = cogl_pipeline_new (test_ctx); cogl_pipeline_set_layer_filters (pipeline, 0, /* layer */ COGL_PIPELINE_FILTER_NEAREST, COGL_PIPELINE_FILTER_NEAREST); cogl_pipeline_set_layer_wrap_mode (pipeline, 0, /* layer */ COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE); /* This is the layer combine used by cogl-pango */ cogl_pipeline_set_layer_combine (pipeline, 0, /* layer */ "RGBA = MODULATE (PREVIOUS, TEXTURE[A])", NULL); cogl_pipeline_set_layer_texture (pipeline, 0, /* layer */ COGL_TEXTURE (tex)); *pipeline_out = pipeline; *tex_out = COGL_TEXTURE (tex); }
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 void test_tex_combine (TestState *state, int x, int y, uint32_t tex0_color, uint32_t tex1_color, uint32_t combine_constant, const char *combine_string, uint32_t expected_result) { CoglTexture *tex0, *tex1; /* combine constant - when applicable */ uint8_t Cr = MASK_RED (combine_constant); uint8_t Cg = MASK_GREEN (combine_constant); uint8_t Cb = MASK_BLUE (combine_constant); uint8_t Ca = MASK_ALPHA (combine_constant); CoglColor combine_const_color; CoglPipeline *pipeline; CoglBool status; CoglError *error = NULL; int y_off; int x_off; tex0 = make_texture (tex0_color); tex1 = make_texture (tex1_color); pipeline = cogl_pipeline_new (test_ctx); cogl_pipeline_set_color4ub (pipeline, 0x80, 0x80, 0x80, 0x80); cogl_pipeline_set_blend (pipeline, "RGBA = ADD (SRC_COLOR, 0)", NULL); cogl_pipeline_set_layer_texture (pipeline, 0, tex0); cogl_pipeline_set_layer_combine (pipeline, 0, "RGBA = REPLACE (TEXTURE)", NULL); cogl_pipeline_set_layer_texture (pipeline, 1, tex1); status = cogl_pipeline_set_layer_combine (pipeline, 1, combine_string, &error); if (!status) { /* It's not strictly a test failure; you need a more capable GPU or * driver to test this texture combine string. */ g_debug ("Failed to test texture combine string %s: %s", combine_string, error->message); } cogl_color_init_from_4ub (&combine_const_color, Cr, Cg, Cb, Ca); cogl_pipeline_set_layer_combine_constant (pipeline, 1, &combine_const_color); cogl_framebuffer_draw_rectangle (test_fb, pipeline, x * QUAD_WIDTH, y * QUAD_WIDTH, x * QUAD_WIDTH + QUAD_WIDTH, y * QUAD_WIDTH + QUAD_WIDTH); cogl_object_unref (pipeline); cogl_object_unref (tex0); cogl_object_unref (tex1); /* See what we got... */ y_off = y * QUAD_WIDTH + (QUAD_WIDTH / 2); x_off = x * QUAD_WIDTH + (QUAD_WIDTH / 2); if (cogl_test_verbose ()) { g_print ("test_tex_combine (%d, %d):\n%s\n", x, y, combine_string); g_print (" texture 0 color = 0x%08lX\n", (unsigned long)tex0_color); g_print (" texture 1 color = 0x%08lX\n", (unsigned long)tex1_color); if (combine_constant != TEX_CONSTANT_UNUSED) g_print (" combine constant = %02x, %02x, %02x, %02x\n", Cr, Cg, Cb, Ca); else g_print (" combine constant = UNUSED\n"); } test_utils_check_pixel (test_fb, x_off, y_off, expected_result); }
static void paint (TestState *state) { CoglPipeline *pipeline = cogl_pipeline_new (test_ctx); CoglTexture *tex; CoglColor color; CoglError *error = NULL; CoglHandle shader, program; cogl_color_init_from_4ub (&color, 0, 0, 0, 255); cogl_clear (&color, COGL_BUFFER_BIT_COLOR); /* Set the primary vertex color as red */ cogl_color_set_from_4ub (&color, 0xff, 0x00, 0x00, 0xff); cogl_pipeline_set_color (pipeline, &color); /* 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 fixed function transform */ shader = cogl_create_shader (COGL_SHADER_TYPE_VERTEX); cogl_shader_source (shader, "void\n" "main ()\n" "{\n" " cogl_position_out = " "cogl_modelview_projection_matrix * " "cogl_position_in;\n" " cogl_color_out = cogl_color_in;\n" " cogl_tex_coord_out[0] = cogl_tex_coord_in;\n" "}\n"); cogl_shader_compile (shader); if (!cogl_shader_is_compiled (shader)) { char *log = cogl_shader_get_info_log (shader); g_warning ("Shader compilation failed:\n%s", log); g_free (log); g_assert_not_reached (); } program = cogl_create_program (); cogl_program_attach_shader (program, shader); cogl_program_link (program); cogl_handle_unref (shader); /* Draw something without the program */ cogl_set_source (pipeline); cogl_rectangle (0, 0, 50, 50); /* Draw it again using the program. It should look exactly the same */ cogl_pipeline_set_user_program (pipeline, program); cogl_handle_unref (program); cogl_rectangle (50, 0, 100, 50); cogl_pipeline_set_user_program (pipeline, COGL_INVALID_HANDLE); cogl_object_unref (pipeline); }
/* Reads back the contents of a texture by rendering it to the framebuffer * and reading back the resulting pixels. * * NB: Normally this approach isn't normally used since we can just use * glGetTexImage, but may be used as a fallback in some circumstances. */ gboolean _cogl_texture_draw_and_read (CoglHandle handle, CoglBitmap *target_bmp, GLuint target_gl_format, GLuint target_gl_type) { int bpp; CoglFramebuffer *framebuffer; int viewport[4]; CoglBitmap *alpha_bmp; CoglMatrixStack *projection_stack; CoglMatrixStack *modelview_stack; int target_width = _cogl_bitmap_get_width (target_bmp); int target_height = _cogl_bitmap_get_height (target_bmp); int target_rowstride = _cogl_bitmap_get_rowstride (target_bmp); _COGL_GET_CONTEXT (ctx, FALSE); bpp = _cogl_get_format_bpp (COGL_PIXEL_FORMAT_RGBA_8888); framebuffer = _cogl_get_draw_buffer (); /* Viewport needs to have some size and be inside the window for this */ _cogl_framebuffer_get_viewport4fv (framebuffer, viewport); if (viewport[0] < 0 || viewport[1] < 0 || viewport[2] <= 0 || viewport[3] <= 0) return FALSE; /* Setup orthographic projection into current viewport (0,0 in top-left * corner to draw the texture upside-down so we match the way cogl_read_pixels * works) */ projection_stack = _cogl_framebuffer_get_projection_stack (framebuffer); _cogl_matrix_stack_push (projection_stack); _cogl_matrix_stack_load_identity (projection_stack); _cogl_matrix_stack_ortho (projection_stack, 0, (float)(viewport[2]), (float)(viewport[3]), 0, (float)(0), (float)(100)); modelview_stack = _cogl_framebuffer_get_modelview_stack (framebuffer); _cogl_matrix_stack_push (modelview_stack); _cogl_matrix_stack_load_identity (modelview_stack); /* Direct copy operation */ if (ctx->texture_download_pipeline == COGL_INVALID_HANDLE) { ctx->texture_download_pipeline = cogl_pipeline_new (); cogl_pipeline_set_blend (ctx->texture_download_pipeline, "RGBA = ADD (SRC_COLOR, 0)", NULL); } cogl_push_source (ctx->texture_download_pipeline); cogl_pipeline_set_layer_texture (ctx->texture_download_pipeline, 0, handle); cogl_pipeline_set_layer_combine (ctx->texture_download_pipeline, 0, /* layer */ "RGBA = REPLACE (TEXTURE)", NULL); cogl_pipeline_set_layer_filters (ctx->texture_download_pipeline, 0, COGL_PIPELINE_FILTER_NEAREST, COGL_PIPELINE_FILTER_NEAREST); do_texture_draw_and_read (handle, target_bmp, viewport); /* Check whether texture has alpha and framebuffer not */ /* FIXME: For some reason even if ALPHA_BITS is 8, the framebuffer still doesn't seem to have an alpha buffer. This might be just a PowerVR issue. GLint r_bits, g_bits, b_bits, a_bits; GE( glGetIntegerv (GL_ALPHA_BITS, &a_bits) ); GE( glGetIntegerv (GL_RED_BITS, &r_bits) ); GE( glGetIntegerv (GL_GREEN_BITS, &g_bits) ); GE( glGetIntegerv (GL_BLUE_BITS, &b_bits) ); printf ("R bits: %d\n", r_bits); printf ("G bits: %d\n", g_bits); printf ("B bits: %d\n", b_bits); printf ("A bits: %d\n", a_bits); */ if ((cogl_texture_get_format (handle) & COGL_A_BIT)/* && a_bits == 0*/) { guint8 *srcdata; guint8 *dstdata; guint8 *srcpixel; guint8 *dstpixel; int x,y; int alpha_rowstride = bpp * target_width; if ((dstdata = _cogl_bitmap_map (target_bmp, COGL_BUFFER_ACCESS_WRITE, COGL_BUFFER_MAP_HINT_DISCARD)) == NULL) return FALSE; srcdata = g_malloc (alpha_rowstride * target_height); /* Create temp bitmap for alpha values */ alpha_bmp = _cogl_bitmap_new_from_data (srcdata, COGL_PIXEL_FORMAT_RGBA_8888, target_width, target_height, alpha_rowstride, (CoglBitmapDestroyNotify) g_free, NULL); /* Draw alpha values into RGB channels */ cogl_pipeline_set_layer_combine (ctx->texture_download_pipeline, 0, /* layer */ "RGBA = REPLACE (TEXTURE[A])", NULL); do_texture_draw_and_read (handle, alpha_bmp, viewport); /* Copy temp R to target A */ for (y=0; y<target_height; ++y) { for (x=0; x<target_width; ++x) { srcpixel = srcdata + x*bpp; dstpixel = dstdata + x*bpp; dstpixel[3] = srcpixel[0]; } srcdata += alpha_rowstride; dstdata += target_rowstride; } _cogl_bitmap_unmap (target_bmp); cogl_object_unref (alpha_bmp); } /* Restore old state */ _cogl_matrix_stack_pop (modelview_stack); _cogl_matrix_stack_pop (projection_stack); /* restore the original pipeline */ cogl_pop_source (); return TRUE; }
static CoglPipeline * get_entity_color_pipeline (RigEngine *engine, RutEntity *entity, RutComponent *geometry, CoglBool blended) { CoglSnippet *snippet; CoglDepthState depth_state; RutMaterial *material; CoglTexture *texture = NULL; CoglTexture *normal_map = NULL; CoglTexture *alpha_mask = NULL; CoglPipeline *pipeline; CoglFramebuffer *shadow_fb; if (blended) pipeline = rut_entity_get_pipeline_cache (entity, CACHE_SLOT_COLOR_BLENDED); else pipeline = rut_entity_get_pipeline_cache (entity, CACHE_SLOT_COLOR_UNBLENDED); if (pipeline) { cogl_object_ref (pipeline); goto FOUND; } pipeline = cogl_pipeline_new (engine->ctx->cogl_context); material = rut_entity_get_component (entity, RUT_COMPONENT_TYPE_MATERIAL); if (material) { RutAsset *texture_asset = rut_material_get_texture_asset (material); RutAsset *normal_map_asset = rut_material_get_normal_map_asset (material); RutAsset *alpha_mask_asset = rut_material_get_alpha_mask_asset (material); if (texture_asset) texture = rut_asset_get_texture (texture_asset); if (texture) cogl_pipeline_set_layer_texture (pipeline, 1, texture); if (normal_map_asset) normal_map = rut_asset_get_texture (normal_map_asset); if (alpha_mask_asset) alpha_mask = rut_asset_get_texture (alpha_mask_asset); } #if 0 /* NB: Our texture colours aren't premultiplied */ cogl_pipeline_set_blend (pipeline, "RGB = ADD(SRC_COLOR*(SRC_COLOR[A]), DST_COLOR*(1-SRC_COLOR[A]))" "A = ADD(SRC_COLOR, DST_COLOR*(1-SRC_COLOR[A]))", NULL); #endif #if 0 if (rut_object_get_type (geometry) == &rut_shape_type) rut_geometry_component_update_pipeline (geometry, pipeline); pipeline = cogl_pipeline_new (rut_cogl_context); #endif cogl_pipeline_set_color4f (pipeline, 0.8f, 0.8f, 0.8f, 1.f); /* enable depth testing */ cogl_depth_state_init (&depth_state); cogl_depth_state_set_test_enabled (&depth_state, TRUE); if (blended) cogl_depth_state_set_write_enabled (&depth_state, FALSE); cogl_pipeline_set_depth_state (pipeline, &depth_state, NULL); /* Vertex shader setup for lighting */ cogl_pipeline_add_snippet (pipeline, engine->lighting_vertex_snippet); if (normal_map) cogl_pipeline_add_snippet (pipeline, engine->normal_map_vertex_snippet); if (rut_entity_get_receive_shadow (entity)) cogl_pipeline_add_snippet (pipeline, engine->shadow_mapping_vertex_snippet); /* and fragment shader */ /* XXX: ideally we wouldn't have to rely on conditionals + discards * in the fragment shader to differentiate blended and unblended * regions and instead we should let users mark out opaque regions * in geometry. */ cogl_pipeline_add_snippet (pipeline, blended ? engine->blended_discard_snippet : engine->unblended_discard_snippet); cogl_pipeline_add_snippet (pipeline, engine->unpremultiply_snippet); if (material) { if (alpha_mask) { /* We don't want this layer to be automatically modulated with the * previous layers so we set its combine mode to "REPLACE" so it * will be skipped past and we can sample its texture manually */ cogl_pipeline_set_layer_combine (pipeline, 2, "RGBA=REPLACE(PREVIOUS)", NULL); cogl_pipeline_set_layer_texture (pipeline, 2, alpha_mask); cogl_pipeline_add_snippet (pipeline, engine->alpha_mask_snippet); } if (normal_map) { /* We don't want this layer to be automatically modulated with the * previous layers so we set its combine mode to "REPLACE" so it * will be skipped past and we can sample its texture manually */ cogl_pipeline_set_layer_combine (pipeline, 5, "RGBA=REPLACE(PREVIOUS)", NULL); cogl_pipeline_set_layer_texture (pipeline, 5, normal_map); snippet = engine->normal_map_fragment_snippet; } else { snippet = engine->material_lighting_snippet; } } else { snippet = engine->simple_lighting_snippet; } cogl_pipeline_add_snippet (pipeline, snippet); if (rut_entity_get_receive_shadow (entity)) { /* Hook the shadow map sampling */ cogl_pipeline_set_layer_texture (pipeline, 7, engine->shadow_map); /* For debugging the shadow mapping... */ //cogl_pipeline_set_layer_texture (pipeline, 7, engine->shadow_color); //cogl_pipeline_set_layer_texture (pipeline, 7, engine->gradient); /* We don't want this layer to be automatically modulated with the * previous layers so we set its combine mode to "REPLACE" so it * will be skipped past and we can sample its texture manually */ cogl_pipeline_set_layer_combine (pipeline, 7, "RGBA=REPLACE(PREVIOUS)", NULL); /* Handle shadow mapping */ cogl_pipeline_add_snippet (pipeline, engine->shadow_mapping_fragment_snippet); } cogl_pipeline_add_snippet (pipeline, engine->premultiply_snippet); if (rut_object_get_type (geometry) == &rut_shape_type) { CoglTexture *shape_texture; if (rut_shape_get_shaped (RUT_SHAPE (geometry))) { shape_texture = rut_shape_get_shape_texture (RUT_SHAPE (geometry)); cogl_pipeline_set_layer_texture (pipeline, 0, shape_texture); } rut_shape_add_reshaped_callback (RUT_SHAPE (geometry), reshape_cb, NULL, NULL); } else if (rut_object_get_type (geometry) == &rut_diamond_type) rut_diamond_apply_mask (RUT_DIAMOND (geometry), pipeline); if (!blended) { cogl_pipeline_set_blend (pipeline, "RGBA = ADD (SRC_COLOR, 0)", NULL); rut_entity_set_pipeline_cache (entity, CACHE_SLOT_COLOR_UNBLENDED, pipeline); } else { rut_entity_set_pipeline_cache (entity, CACHE_SLOT_COLOR_BLENDED, pipeline); } FOUND: /* FIXME: there's lots to optimize about this! */ shadow_fb = COGL_FRAMEBUFFER (engine->shadow_fb); /* update uniforms in pipelines */ { CoglMatrix light_shadow_matrix, light_projection; CoglMatrix model_transform; const float *light_matrix; int location; cogl_framebuffer_get_projection_matrix (shadow_fb, &light_projection); /* XXX: This is pretty bad that we are having to do this. It would * be nicer if cogl exposed matrix-stacks publicly so we could * maintain the entity model_matrix incrementally as we traverse * the scenegraph. */ rut_graphable_get_transform (entity, &model_transform); get_light_modelviewprojection (&model_transform, engine->light, &light_projection, &light_shadow_matrix); light_matrix = cogl_matrix_get_array (&light_shadow_matrix); location = cogl_pipeline_get_uniform_location (pipeline, "light_shadow_matrix"); cogl_pipeline_set_uniform_matrix (pipeline, location, 4, 1, FALSE, light_matrix); } return pipeline; }
static CoglPipeline * get_entity_mask_pipeline (RigEngine *engine, RutEntity *entity, RutComponent *geometry) { CoglPipeline *pipeline; pipeline = rut_entity_get_pipeline_cache (entity, CACHE_SLOT_SHADOW); if (pipeline) return cogl_object_ref (pipeline); /* TODO: move into init() somewhere */ if (G_UNLIKELY (!engine->dof_pipeline_template)) { CoglPipeline *pipeline; CoglDepthState depth_state; CoglSnippet *snippet; pipeline = cogl_pipeline_new (engine->ctx->cogl_context); cogl_pipeline_set_color_mask (pipeline, COGL_COLOR_MASK_ALPHA); cogl_pipeline_set_blend (pipeline, "RGBA=ADD(SRC_COLOR, 0)", NULL); cogl_depth_state_init (&depth_state); cogl_depth_state_set_test_enabled (&depth_state, TRUE); cogl_pipeline_set_depth_state (pipeline, &depth_state, NULL); snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_VERTEX, /* definitions */ "uniform float dof_focal_distance;\n" "uniform float dof_depth_of_field;\n" "varying float dof_blur;\n", //"varying vec4 world_pos;\n", /* compute the amount of bluriness we want */ "vec4 world_pos = cogl_modelview_matrix * cogl_position_in;\n" //"world_pos = cogl_modelview_matrix * cogl_position_in;\n" "dof_blur = 1.0 - clamp (abs (world_pos.z - dof_focal_distance) /\n" " dof_depth_of_field, 0.0, 1.0);\n" ); cogl_pipeline_add_snippet (pipeline, snippet); cogl_object_unref (snippet); /* This was used to debug the focal distance and bluriness amount in the DoF * effect: */ #if 0 cogl_pipeline_set_color_mask (pipeline, COGL_COLOR_MASK_ALL); snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_FRAGMENT, "varying vec4 world_pos;\n" "varying float dof_blur;", "cogl_color_out = vec4(dof_blur,0,0,1);\n" //"cogl_color_out = vec4(1.0, 0.0, 0.0, 1.0);\n" //"if (world_pos.z < -30.0) cogl_color_out = vec4(0,1,0,1);\n" //"if (abs (world_pos.z + 30.f) < 0.1) cogl_color_out = vec4(0,1,0,1);\n" "cogl_color_out.a = dof_blur;\n" //"cogl_color_out.a = 1.0;\n" ); cogl_pipeline_add_snippet (pipeline, snippet); cogl_object_unref (snippet); #endif engine->dof_pipeline_template = pipeline; } /* TODO: move into init() somewhere */ if (G_UNLIKELY (!engine->dof_diamond_pipeline)) { CoglPipeline *dof_diamond_pipeline = cogl_pipeline_copy (engine->dof_pipeline_template); CoglSnippet *snippet; cogl_pipeline_set_layer_texture (dof_diamond_pipeline, 0, engine->ctx->circle_texture); snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_FRAGMENT, /* declarations */ "varying float dof_blur;", /* post */ "if (cogl_color_out.a <= 0.0)\n" " discard;\n" "\n" "cogl_color_out.a = dof_blur;\n"); cogl_pipeline_add_snippet (dof_diamond_pipeline, snippet); cogl_object_unref (snippet); engine->dof_diamond_pipeline = dof_diamond_pipeline; } /* TODO: move into init() somewhere */ if (G_UNLIKELY (!engine->dof_unshaped_pipeline)) { CoglPipeline *dof_unshaped_pipeline = cogl_pipeline_copy (engine->dof_pipeline_template); CoglSnippet *snippet; snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_FRAGMENT, /* declarations */ "varying float dof_blur;", /* post */ "if (cogl_color_out.a < 0.25)\n" " discard;\n" "\n" "cogl_color_out.a = dof_blur;\n"); cogl_pipeline_add_snippet (dof_unshaped_pipeline, snippet); cogl_object_unref (snippet); engine->dof_unshaped_pipeline = dof_unshaped_pipeline; } /* TODO: move into init() somewhere */ if (G_UNLIKELY (!engine->dof_pipeline)) { CoglPipeline *dof_pipeline = cogl_pipeline_copy (engine->dof_pipeline_template); CoglSnippet *snippet; /* store the bluriness in the alpha channel */ snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_FRAGMENT, "varying float dof_blur;", "cogl_color_out.a = dof_blur;\n" ); cogl_pipeline_add_snippet (dof_pipeline, snippet); cogl_object_unref (snippet); engine->dof_pipeline = dof_pipeline; } if (rut_object_get_type (geometry) == &rut_diamond_type) { pipeline = cogl_object_ref (engine->dof_diamond_pipeline); } else if (rut_object_get_type (geometry) == &rut_shape_type) { RutMaterial *material = rut_entity_get_component (entity, RUT_COMPONENT_TYPE_MATERIAL); pipeline = cogl_pipeline_copy (engine->dof_unshaped_pipeline); if (rut_shape_get_shaped (RUT_SHAPE (geometry))) { CoglTexture *shape_texture = rut_shape_get_shape_texture (RUT_SHAPE (geometry)); cogl_pipeline_set_layer_texture (pipeline, 0, shape_texture); } if (material) { RutAsset *texture_asset = rut_material_get_texture_asset (material); RutAsset *alpha_mask_asset = rut_material_get_alpha_mask_asset (material); if (texture_asset) cogl_pipeline_set_layer_texture (pipeline, 1, rut_asset_get_texture (texture_asset)); if (alpha_mask_asset) { /* We don't want this layer to be automatically modulated with the * previous layers so we set its combine mode to "REPLACE" so it * will be skipped past and we can sample its texture manually */ cogl_pipeline_set_layer_combine (pipeline, 2, "RGBA=REPLACE(PREVIOUS)", NULL); cogl_pipeline_set_layer_texture (pipeline, 2, rut_asset_get_texture (alpha_mask_asset)); cogl_pipeline_add_snippet (pipeline, engine->alpha_mask_snippet); } } } else pipeline = cogl_object_ref (engine->dof_pipeline); rut_entity_set_pipeline_cache (entity, CACHE_SLOT_SHADOW, pipeline); return pipeline; }