diff --git a/README b/README index 5f0518e66..222c28cc3 100644 --- a/README +++ b/README @@ -199,6 +199,58 @@ Release Notes for Clutter 1.0 API. The change removed the private (yet publicly exported) and the already deprecated ClutterFixed API. + Cogl API changes for Clutter 1.0 + -------------------------------- + +* All drawing functions now use a source material to determine how geometry is + filled. The source material is set via cogl_set_source. Or the convenience + functions cogl_set_source_color and cogl_set_source_texture. + + "drawing functions" include: cogl_rectangle, cogl_texture_rectangle, + cogl_texture_multiple_rectangles, cogl_texture_polygon (though the + cogl_texture_* funcs have been renamed; see below for details), + cogl_path_fill/stroke and cogl_vertex_buffer_draw*. + + cogl_texture_rectangle, cogl_texture_multiple_rectangles and + cogl_texture_polygon no longer take a texture handle; instead the current + source material is referenced. The functions have also been renamed to: + cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords + and cogl_polygon respectively. + + Most code that previously did: + cogl_texture_rectangle (tex_handle, x, y,...); + needs to be changed to now do: + cogl_set_source_texture (tex_handle); + cogl_rectangle_with_texture_coords (x, y,....); + + In the less likely case where you were blending your source texture with a + color like: + cogl_set_source_color4ub (r,g,b,a); /* (where r,g,b,a isn't just white) */ + cogl_texture_rectangle (tex_handle, x, y,...); + you will need your own material to do that: + material = cogl_material_new (); + cogl_material_set_color4ub (r,g,b,a); + cogl_material_set_layer (material, 0, tex_handle)); + cogl_set_source_material (material); + + Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use + cogl_rectangle_with_texure_coords since these are the coordinates that + cogl_rectangle will use. + + For cogl_texture_polygon; as well as dropping the texture handle, the + n_vertices and vertices arguments were transposed for consistency. So + code previously written as: + cogl_texture_polygon (tex_handle, 3, verts, TRUE); + need to be written as: + cogl_set_source_texture (tex_handle); + cogl_polygon (verts, 3, TRUE); + +* A CoglMesh type and utility API has been added; this is currently used to + support describing texture matrices. + +* cogl_alpha_func has been removed, since this is now controlled using the + material API via cogl_material_set_alpha_test_function () + Release Notes for Clutter 0.8 ------------------------------- diff --git a/clutter/clutter-clone-texture.c b/clutter/clutter-clone-texture.c index f79dd673b..cde7e26bd 100644 --- a/clutter/clutter-clone-texture.c +++ b/clutter/clutter-clone-texture.c @@ -61,9 +61,11 @@ G_DEFINE_TYPE (ClutterCloneTexture, #define CLUTTER_CLONE_TEXTURE_GET_PRIVATE(obj) \ (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CLUTTER_TYPE_CLONE_TEXTURE, ClutterCloneTexturePrivate)) + struct _ClutterCloneTexturePrivate { ClutterTexture *parent_texture; + CoglHandle material; guint repeat_x : 1; guint repeat_y : 1; }; @@ -145,6 +147,7 @@ clutter_clone_texture_paint (ClutterActor *self) CoglHandle cogl_texture; ClutterFixed t_w, t_h; guint tex_width, tex_height; + CoglHandle cogl_material; priv = CLUTTER_CLONE_TEXTURE (self)->priv; @@ -177,8 +180,8 @@ clutter_clone_texture_paint (ClutterActor *self) CLUTTER_TEXTURE_IN_CLONE_PAINT); } - cogl_set_source_color4ub (255, 255, 255, - clutter_actor_get_paint_opacity (self)); + cogl_material_set_color4ub (priv->material, 0xff, 0xff, 0xff, + clutter_actor_get_paint_opacity (self)); clutter_actor_get_allocation_coords (self, &x_1, &y_1, &x_2, &y_2); @@ -206,11 +209,13 @@ clutter_clone_texture_paint (ClutterActor *self) else t_h = 1.0; + cogl_material_set_layer (priv->material, 0, cogl_texture); + cogl_set_source (priv->material); /* Parent paint translated us into position */ - cogl_texture_rectangle (cogl_texture, 0, 0, - (float)(x_2 - x_1), - (float)(y_2 - y_1), - 0, 0, t_w, t_h); + cogl_rectangle_with_texture_coords (0, 0, + (float) (x_2 - x_1), + (float) (y_2 - y_1), + 0, 0, t_w, t_h); } static void @@ -388,6 +393,7 @@ clutter_clone_texture_init (ClutterCloneTexture *self) self->priv = priv = CLUTTER_CLONE_TEXTURE_GET_PRIVATE (self); priv->parent_texture = NULL; + priv->material = cogl_material_new (); } /** diff --git a/clutter/clutter-main.c b/clutter/clutter-main.c index 35d0afce0..1cb21fa25 100644 --- a/clutter/clutter-main.c +++ b/clutter/clutter-main.c @@ -1163,18 +1163,6 @@ clutter_init_real (GError **error) if (!_clutter_backend_post_parse (backend, error)) return CLUTTER_INIT_ERROR_BACKEND; - /* - * Resolution requires display to be open, so can only be queried after - * the post_parse hooks run. - */ - ctx->font_map = COGL_PANGO_FONT_MAP (cogl_pango_font_map_new ()); - - resolution = clutter_backend_get_resolution (ctx->backend); - cogl_pango_font_map_set_resolution (ctx->font_map, resolution); - cogl_pango_font_map_set_use_mipmapping (ctx->font_map, TRUE); - - clutter_text_direction = clutter_get_text_direction (); - /* Stage will give us a GL Context etc */ stage = clutter_stage_get_default (); if (!stage) @@ -1207,6 +1195,21 @@ clutter_init_real (GError **error) * start issueing cogl commands */ + /* + * Resolution requires display to be open, so can only be queried after + * the post_parse hooks run. + * + * NB: cogl_pango requires a Cogl context. + */ + ctx->font_map = COGL_PANGO_FONT_MAP (cogl_pango_font_map_new ()); + + resolution = clutter_backend_get_resolution (ctx->backend); + cogl_pango_font_map_set_resolution (ctx->font_map, resolution); + cogl_pango_font_map_set_use_mipmapping (ctx->font_map, TRUE); + + clutter_text_direction = clutter_get_text_direction (); + + /* Figure out framebuffer masks used for pick */ cogl_get_bitmasks (&ctx->fb_r_mask, &ctx->fb_g_mask, &ctx->fb_b_mask, NULL); diff --git a/clutter/clutter-texture.c b/clutter/clutter-texture.c index dae07ca42..413662127 100644 --- a/clutter/clutter-texture.c +++ b/clutter/clutter-texture.c @@ -79,7 +79,8 @@ struct _ClutterTexturePrivate gint height; gint max_tile_waste; ClutterTextureQuality filter_quality; - CoglHandle texture; + CoglHandle material; + CoglHandle fbo_texture; gboolean no_slice; ClutterActor *fbo_source; @@ -116,6 +117,9 @@ enum PROP_REPEAT_X, PROP_FILTER_QUALITY, PROP_COGL_TEXTURE, +#if EXPOSE_COGL_MATERIAL_PROP + PROP_COGL_MATERIAL, +#endif PROP_FILENAME, PROP_KEEP_ASPECT_RATIO, PROP_LOAD_ASYNC @@ -186,11 +190,7 @@ texture_free_gl_resources (ClutterTexture *texture) CLUTTER_MARK(); - if (priv->texture != COGL_INVALID_HANDLE) - { - cogl_texture_unref (priv->texture); - priv->texture = COGL_INVALID_HANDLE; - } + cogl_material_remove_layer (priv->material, 0); } static void @@ -202,7 +202,7 @@ clutter_texture_unrealize (ClutterActor *actor) texture = CLUTTER_TEXTURE(actor); priv = texture->priv; - if (priv->texture == COGL_INVALID_HANDLE) + if (priv->material == COGL_INVALID_HANDLE) return; /* there's no need to read the pixels back when unrealizing inside @@ -263,8 +263,8 @@ clutter_texture_realize (ClutterActor *actor) /* Handle FBO's */ - if (priv->texture != COGL_INVALID_HANDLE) - cogl_texture_unref (priv->texture); + if (priv->fbo_texture != COGL_INVALID_HANDLE) + cogl_texture_unref (priv->fbo_texture); if (!priv->no_slice) max_waste = priv->max_tile_waste; @@ -272,17 +272,17 @@ clutter_texture_realize (ClutterActor *actor) if (priv->filter_quality == CLUTTER_TEXTURE_QUALITY_HIGH) flags |= COGL_TEXTURE_AUTO_MIPMAP; - priv->texture = + priv->fbo_texture = cogl_texture_new_with_size (priv->width, priv->height, max_waste, flags, COGL_PIXEL_FORMAT_RGBA_8888); - cogl_texture_set_filters (priv->texture, + cogl_texture_set_filters (priv->fbo_texture, clutter_texture_quality_to_cogl_min_filter (priv->filter_quality), clutter_texture_quality_to_cogl_mag_filter (priv->filter_quality)); - priv->fbo_handle = cogl_offscreen_new_to_texture (priv->texture); + priv->fbo_handle = cogl_offscreen_new_to_texture (priv->fbo_texture); if (priv->fbo_handle == COGL_INVALID_HANDLE) { @@ -590,8 +590,8 @@ clutter_texture_paint (ClutterActor *self) clutter_actor_get_name (self) ? clutter_actor_get_name (self) : "unknown"); - cogl_set_source_color4ub (255, 255, 255, - clutter_actor_get_paint_opacity (self)); + cogl_material_set_color4ub (priv->material, 0xff, 0xff, 0xff, + clutter_actor_get_paint_opacity (self)); clutter_actor_get_allocation_coords (self, &x_1, &y_1, &x_2, &y_2); @@ -613,10 +613,11 @@ clutter_texture_paint (ClutterActor *self) t_h = 1.0; /* Paint will have translated us */ - cogl_texture_rectangle (priv->texture, 0, 0, - (float)(x_2 - x_1), - (float)(y_2 - y_1), - 0, 0, t_w, t_h); + cogl_set_source (priv->material); + cogl_rectangle_with_texture_coords (0, 0, + (float) (x_2 - x_1), + (float) (y_2 - y_1), + 0, 0, t_w, t_h); } /* @@ -734,6 +735,12 @@ clutter_texture_set_property (GObject *object, clutter_texture_set_cogl_texture (texture, (CoglHandle) g_value_get_boxed (value)); break; +#if EXPOSE_COGL_MATERIAL_PROP + case PROP_COGL_MATERIAL: + clutter_texture_set_cogl_material + (texture, (CoglHandle) g_value_get_boxed (value)); + break; +#endif case PROP_FILENAME: clutter_texture_set_from_file (texture, g_value_get_string (value), @@ -762,6 +769,7 @@ clutter_texture_get_property (GObject *object, { ClutterTexture *texture; ClutterTexturePrivate *priv; + CoglHandle cogl_texture; texture = CLUTTER_TEXTURE(object); priv = texture->priv; @@ -772,10 +780,11 @@ clutter_texture_get_property (GObject *object, g_value_set_int (value, clutter_texture_get_max_tile_waste (texture)); break; case PROP_PIXEL_FORMAT: - if (priv->texture == COGL_INVALID_HANDLE) + cogl_texture = clutter_texture_get_cogl_texture (texture); + if (cogl_texture == COGL_INVALID_HANDLE) g_value_set_int (value, COGL_PIXEL_FORMAT_ANY); else - g_value_set_int (value, cogl_texture_get_format (priv->texture)); + g_value_set_int (value, cogl_texture_get_format (cogl_texture)); break; case PROP_SYNC_SIZE: g_value_set_boolean (value, priv->sync_actor_size); @@ -792,6 +801,11 @@ clutter_texture_get_property (GObject *object, case PROP_COGL_TEXTURE: g_value_set_boxed (value, clutter_texture_get_cogl_texture (texture)); break; +#if EXPOSE_COGL_MATERIAL_PROP + case PROP_COGL_MATERIAL: + g_value_set_boxed (value, clutter_texture_get_cogl_material (texture)); + break; +#endif case PROP_NO_SLICE: g_value_set_boolean (value, priv->no_slice); break; @@ -909,6 +923,17 @@ clutter_texture_class_init (ClutterTextureClass *klass) CLUTTER_TYPE_TEXTURE_HANDLE, G_PARAM_READWRITE)); +#if EXPOSE_COGL_MATERIAL_PROP + g_object_class_install_property + (gobject_class, PROP_COGL_MATERIAL, + g_param_spec_boxed ("cogl-material", + "COGL Material", + "The underlying COGL material handle used to draw " + "this actor", + CLUTTER_TYPE_MATERIAL_HANDLE, + G_PARAM_READWRITE)); +#endif + g_object_class_install_property (gobject_class, PROP_FILENAME, g_param_spec_string ("filename", @@ -1073,7 +1098,8 @@ clutter_texture_init (ClutterTexture *self) priv->repeat_x = FALSE; priv->repeat_y = FALSE; priv->sync_actor_size = TRUE; - priv->texture = COGL_INVALID_HANDLE; + priv->material = cogl_material_new (); + priv->fbo_texture = COGL_INVALID_HANDLE; priv->fbo_handle = COGL_INVALID_HANDLE; priv->local_data = NULL; priv->keep_aspect_ratio = FALSE; @@ -1085,6 +1111,7 @@ clutter_texture_save_to_local_data (ClutterTexture *texture) ClutterTexturePrivate *priv; int bpp; CoglPixelFormat pixel_format; + CoglHandle cogl_texture; priv = texture->priv; @@ -1094,12 +1121,14 @@ clutter_texture_save_to_local_data (ClutterTexture *texture) priv->local_data = NULL; } - if (priv->texture == COGL_INVALID_HANDLE) + if (priv->material == COGL_INVALID_HANDLE) return; - priv->local_data_width = cogl_texture_get_width (priv->texture); - priv->local_data_height = cogl_texture_get_height (priv->texture); - pixel_format = cogl_texture_get_format (priv->texture); + cogl_texture = clutter_texture_get_cogl_texture (texture); + + priv->local_data_width = cogl_texture_get_width (cogl_texture); + priv->local_data_height = cogl_texture_get_height (cogl_texture); + pixel_format = cogl_texture_get_format (cogl_texture); priv->local_data_has_alpha = pixel_format & COGL_A_BIT; bpp = priv->local_data_has_alpha ? 4 : 3; @@ -1115,7 +1144,7 @@ clutter_texture_save_to_local_data (ClutterTexture *texture) priv->local_data = g_malloc (priv->local_data_rowstride * priv->local_data_height); - if (cogl_texture_get_data (priv->texture, + if (cogl_texture_get_data (cogl_texture, priv->local_data_has_alpha ? COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888, @@ -1150,6 +1179,58 @@ clutter_texture_load_from_local_data (ClutterTexture *texture) priv->local_data = NULL; } +#if EXPOSE_COGL_MATERIAL_PROP +/** + * clutter_texture_get_cogl_material: + * @texture: A #ClutterTexture + * + * Returns a handle to the underlying COGL material used for drawing + * the actor. No extra reference is taken so if you need to keep the + * handle then you should call cogl_material_ref on it. + * + * Since: 1.0 + * + * Return value: COGL material handle + */ +CoglHandle +clutter_texture_get_cogl_material (ClutterTexture *texture) +{ + return texture->priv->material; +} + +/** + * clutter_texture_set_cogl_material: + * @texture: A #ClutterTexture + * @cogl_material: A CoglHandle for a material + * + * Replaces the underlying COGL texture drawn by this actor with + * @cogl_tex. A reference to the texture is taken so if the handle is + * no longer needed it should be deref'd with cogl_texture_unref. + * + * Since: 0.8 + * + */ +void +clutter_texture_set_cogl_material (ClutterTexture *texture, + CoglHandle cogl_material) +{ + CoglHandle cogl_texture; + + /* This */ + if (texture->priv->material) + cogl_material_unref (texture->priv->material); + + texture->priv->material = cogl_material; + + /* XXX: We are re-asserting the first layer of the new material to ensure the + * priv state is in sync with the contents of the material. */ + cogl_texture = clutter_texture_get_cogl_texture (texture); + clutter_texture_set_cogl_texture (texture, cogl_texture); + /* XXX: If we add support for more material layers, this will need + * extending */ +} +#endif + /** * clutter_texture_get_cogl_texture * @texture: A #ClutterTexture @@ -1165,9 +1246,17 @@ clutter_texture_load_from_local_data (ClutterTexture *texture) CoglHandle clutter_texture_get_cogl_texture (ClutterTexture *texture) { + const GList *layers; + int n_layers; + g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), COGL_INVALID_HANDLE); - return texture->priv->texture; + layers = cogl_material_get_layers (texture->priv->material); + n_layers = g_list_length ((GList *)layers); + if (n_layers == 0) + return COGL_INVALID_HANDLE; + + return cogl_material_layer_get_texture (layers->data); } /** @@ -1207,7 +1296,8 @@ clutter_texture_set_cogl_texture (ClutterTexture *texture, /* Remove old texture */ texture_free_gl_resources (texture); /* Use the new texture */ - priv->texture = cogl_tex; + + cogl_material_set_layer (priv->material, 0, cogl_tex); size_change = width != priv->width || height != priv->height; priv->width = width; @@ -1715,11 +1805,12 @@ clutter_texture_set_filter_quality (ClutterTexture *texture, if (filter_quality != old_quality) { + CoglHandle cogl_texture = clutter_texture_get_cogl_texture (texture); priv->filter_quality = filter_quality; /* Is this actually needed - causes problems with TFP mipmaps */ - if (priv->texture != COGL_INVALID_HANDLE) - cogl_texture_set_filters (priv->texture, + if (cogl_texture != COGL_INVALID_HANDLE) + cogl_texture_set_filters (cogl_texture, clutter_texture_quality_to_cogl_min_filter (priv->filter_quality), clutter_texture_quality_to_cogl_mag_filter (priv->filter_quality)); @@ -1787,15 +1878,17 @@ clutter_texture_set_max_tile_waste (ClutterTexture *texture, gint max_tile_waste) { ClutterTexturePrivate *priv; + CoglHandle cogl_texture; g_return_if_fail (CLUTTER_IS_TEXTURE (texture)); priv = texture->priv; + cogl_texture = clutter_texture_get_cogl_texture (texture); /* There's no point in changing the max_tile_waste if the texture has already been created because it will be overridden with the value from the texture handle */ - if (priv->texture == COGL_INVALID_HANDLE) + if (cogl_texture == COGL_INVALID_HANDLE) priv->max_tile_waste = max_tile_waste; } @@ -1815,17 +1908,19 @@ gint clutter_texture_get_max_tile_waste (ClutterTexture *texture) { ClutterTexturePrivate *priv; + CoglHandle cogl_texture; g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), 0); priv = texture->priv; + cogl_texture = clutter_texture_get_cogl_texture (texture); - if (priv->texture == COGL_INVALID_HANDLE) + if (cogl_texture == COGL_INVALID_HANDLE) return texture->priv->max_tile_waste; else /* If we have a valid texture handle then use the value from that instead */ - return cogl_texture_get_max_waste (texture->priv->texture); + return cogl_texture_get_max_waste (cogl_texture); } /** @@ -1939,6 +2034,7 @@ clutter_texture_set_area_from_rgb_data (ClutterTexture *texture, { ClutterTexturePrivate *priv; CoglPixelFormat source_format; + CoglHandle cogl_texture; priv = texture->priv; @@ -1971,7 +2067,8 @@ clutter_texture_set_area_from_rgb_data (ClutterTexture *texture, clutter_actor_realize (CLUTTER_ACTOR (texture)); - if (priv->texture == COGL_INVALID_HANDLE) + cogl_texture = clutter_texture_get_cogl_texture (texture); + if (cogl_texture == COGL_INVALID_HANDLE) { g_set_error (error, CLUTTER_TEXTURE_ERROR, CLUTTER_TEXTURE_ERROR_BAD_FORMAT, @@ -1979,7 +2076,7 @@ clutter_texture_set_area_from_rgb_data (ClutterTexture *texture, return FALSE; } - if (!cogl_texture_set_region (priv->texture, + if (!cogl_texture_set_region (cogl_texture, 0, 0, x, y, width, height, width, height, @@ -2027,17 +2124,18 @@ on_fbo_source_size_change (GObject *object, if (priv->filter_quality == CLUTTER_TEXTURE_QUALITY_HIGH) flags |= COGL_TEXTURE_AUTO_MIPMAP; - priv->texture = cogl_texture_new_with_size (MAX (priv->width, 1), - MAX (priv->height, 1), - -1, - flags, - COGL_PIXEL_FORMAT_RGBA_8888); + priv->fbo_texture = + cogl_texture_new_with_size (MAX (priv->width, 1), + MAX (priv->height, 1), + -1, + flags, + COGL_PIXEL_FORMAT_RGBA_8888); - cogl_texture_set_filters (priv->texture, + cogl_texture_set_filters (priv->fbo_texture, clutter_texture_quality_to_cogl_min_filter (priv->filter_quality), clutter_texture_quality_to_cogl_mag_filter (priv->filter_quality)); - priv->fbo_handle = cogl_offscreen_new_to_texture (priv->texture); + priv->fbo_handle = cogl_offscreen_new_to_texture (priv->fbo_texture); if (priv->fbo_handle == COGL_INVALID_HANDLE) { @@ -2281,3 +2379,22 @@ clutter_texture_handle_get_type (void) return our_type; } + +#if EXPOSE_COGL_MATERIAL_PROP +GType +clutter_material_handle_get_type (void) +{ + static GType our_type = 0; + + if (G_UNLIKELY (!our_type)) + { + our_type = + g_boxed_type_register_static (I_("ClutterMaterialHandle"), + (GBoxedCopyFunc) cogl_material_ref, + (GBoxedFreeFunc) cogl_material_unref); + } + + return our_type; +} +#endif + diff --git a/clutter/clutter-texture.h b/clutter/clutter-texture.h index 3f20413e8..aa76debf3 100644 --- a/clutter/clutter-texture.h +++ b/clutter/clutter-texture.h @@ -33,6 +33,8 @@ G_BEGIN_DECLS +#define USE_COGL_MATERIAL 1 + #define CLUTTER_TYPE_TEXTURE (clutter_texture_get_type ()) #define CLUTTER_TEXTURE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLUTTER_TYPE_TEXTURE, ClutterTexture)) #define CLUTTER_TEXTURE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_TEXTURE, ClutterTextureClass)) @@ -41,6 +43,9 @@ G_BEGIN_DECLS #define CLUTTER_TEXTURE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_TEXTURE, ClutterTextureClass)) #define CLUTTER_TYPE_TEXTURE_HANDLE (clutter_texture_handle_get_type ()) +#if USE_COGL_MATERIAL +#define CLUTTER_TYPE_MATERIAL_HANDLE (clutter_material_handle_get_type ()) +#endif /** * ClutterTextureError: @@ -136,6 +141,9 @@ typedef enum { /*< prefix=CLUTTER_TEXTURE_QUALITY >*/ GType clutter_texture_get_type (void) G_GNUC_CONST; GType clutter_texture_handle_get_type (void) G_GNUC_CONST; +#if USE_COGL_MATERIAL +GType clutter_material_handle_get_type (void) G_GNUC_CONST; +#endif ClutterActor * clutter_texture_new (void); ClutterActor * clutter_texture_new_from_file (const gchar *filename, @@ -182,6 +190,11 @@ gint clutter_texture_get_max_tile_waste (ClutterTexture CoglHandle clutter_texture_get_cogl_texture (ClutterTexture *texture); void clutter_texture_set_cogl_texture (ClutterTexture *texture, CoglHandle cogl_tex); +#if USE_COGL_MATERIAL +CoglHandle clutter_texture_get_cogl_material (ClutterTexture *texture); +void clutter_texture_set_cogl_material (ClutterTexture *texture, + CoglHandle cogl_material); +#endif G_END_DECLS diff --git a/clutter/cogl/cogl-material.h b/clutter/cogl/cogl-material.h new file mode 100644 index 000000000..915e447e5 --- /dev/null +++ b/clutter/cogl/cogl-material.h @@ -0,0 +1,837 @@ +#if !defined(__COGL_H_INSIDE__) && !defined(CLUTTER_COMPILATION) +#error "Only can be included directly." +#endif + +#ifndef __COGL_MATERIAL_H__ +#define __COGL_MATERIAL_H__ + +G_BEGIN_DECLS + +#include +#include + +/** + * SECTION:cogl-material + * @short_description: Fuctions for creating and manipulating materials + * + * COGL allows creating and manipulating materials used to fill in + * geometry. Materials may simply be lighting attributes (such as an + * ambient and diffuse colour) or might represent one or more textures + * blended together. + */ + + +/** + * cogl_material_new: + * + * Allocates and initializes a blank white material + * + * Returns: a handle to the new material + */ +CoglHandle cogl_material_new (void); + +/** + * cogl_material_ref: + * @handle: a @CoglHandle. + * + * Increment the reference count for a cogl material. + * + * Returns: the @handle. + * + * Since 1.0 + */ +CoglHandle cogl_material_ref (CoglHandle handle); + +/** + * cogl_material_unref: + * @handle: a @CoglHandle. + * + * Decrement the reference count for a cogl material. + * + * Since 1.0 + */ +void cogl_material_unref (CoglHandle handle); + + + +/** + * cogl_material_set_color: + * @material: A CoglMaterial object + * @color: The components of the color + * + * This is the basic color of the material, used when no lighting is enabled. + * + * The default value is (1.0, 1.0, 1.0, 1.0) + * + * Since 1.0 + */ +void cogl_material_set_color (CoglHandle material, const CoglColor *color); + +/** + * cogl_material_set_color: + * @material: A CoglMaterial object + * @red: The red component + * @green: The green component + * @blue: The blue component + * @alpha: The alpha component + * + * This is the basic color of the material, used when no lighting is enabled. + * + * The default value is (1.0, 1.0, 1.0, 1.0) + * + * Since 1.0 + */ +void cogl_material_set_color4ub (CoglHandle handle, + guint8 red, + guint8 green, + guint8 blue, + guint8 alpha); + +/** + * cogl_material_get_color: + * @material: A CoglMaterial object + * @color: The location to store the color + * + * This retrieves the current material color. + * + * Since 1.0 + */ +void cogl_material_get_color (CoglHandle handle, CoglColor *color); + +/** + * cogl_material_set_ambient: + * @material: A CoglMaterial object + * @ambient: The components of the desired ambient color + * + * Exposing the standard OpenGL lighting model; this function sets + * the material's ambient color. The ambient color affects the overall + * color of the object. Since the diffuse color will be intense when + * the light hits the surface directly, the ambient will most aparent + * where the light hits at a slant. + * + * The default value is (0.2, 0.2, 0.2, 1.0) + * + * Since 1.0 + */ +void cogl_material_set_ambient (CoglHandle material, + const CoglColor *ambient); + +/** + * cogl_material_get_ambient: + * @material: A CoglMaterial object + * @ambient: The location to store the ambient color + * + * This retrieves the materials current ambient color. + * + * Since 1.0 + */ +void cogl_material_get_ambient (CoglHandle handle, CoglColor *ambient); + +/** + * cogl_material_set_diffuse: + * @material: A CoglMaterial object + * @diffuse: The components of the desired diffuse color + * + * Exposing the standard OpenGL lighting model; this function sets + * the material's diffuse color. The diffuse color is most intense + * where the light hits the surface directly; perpendicular to the + * surface. + * + * The default value is (0.8, 0.8, 0.8, 1.0) + * + * Since 1.0 + */ +void cogl_material_set_diffuse (CoglHandle material, + const CoglColor *diffuse); + +/** + * cogl_material_get_diffuse: + * @material: A CoglMaterial object + * @diffuse: The location to store the diffuse color + * + * This retrieves the materials current diffuse color. + * + * Since 1.0 + */ +void cogl_material_get_diffuse (CoglHandle handle, CoglColor *diffuse); + +/** + * cogl_material_set_ambient_and_diffuse: + * @material: A CoglMaterial object + * @color: The components of the desired ambient and diffuse colors + * + * This is a convenience for setting the diffuse and ambient color + * of the material at the same time. + * + * The default ambient color is (0.2, 0.2, 0.2, 1.0) + * The default diffuse color is (0.8, 0.8, 0.8, 1.0) + * + * Since 1.0 + */ +void cogl_material_set_ambient_and_diffuse (CoglHandle material, + const CoglColor *color); + +/** + * cogl_material_set_specular: + * @material: A CoglMaterial object + * @specular: The components of the desired specular color + * + * Exposing the standard OpenGL lighting model; this function sets + * the material's specular color. The intensity of the specular color + * depends on the viewport position, and is brightest along the lines + * of reflection. + * + * The default value is (0.0, 0.0, 0.0, 1.0) + * + * Since 1.0 + */ +void cogl_material_set_specular (CoglHandle material, + const CoglColor *specular); + +/** + * cogl_material_get_specular: + * @material: A CoglMaterial object + * @specular: The location to store the specular color + * + * This retrieves the materials current specular color. + * + * Since 1.0 + */ +void cogl_material_get_specular (CoglHandle handle, CoglColor *specular); + +/** + * cogl_material_set_shininess: + * @material: A CoglMaterial object + * shininess: The desired shininess; range: [0.0, 1.0] + * + * This function sets the materials shininess which determines how + * specular highlights are calculated. A higher shininess will produce + * smaller brigher highlights. + * + * The default value is 0.0 + * + * Since 1.0 + */ +void cogl_material_set_shininess (CoglHandle material, + float shininess); +/** + * cogl_material_get_shininess: + * @material: A CoglMaterial object + * + * This retrieves the materials current emission color. + * + * Return value: The materials current shininess value + * + * Since 1.0 + */ +float cogl_material_get_shininess (CoglHandle handle); + +/** + * cogl_material_set_emission: + * @material: A CoglMaterial object + * @emission: The components of the desired emissive color + * + * Exposing the standard OpenGL lighting model; this function sets + * the material's emissive color. It will look like the surface is + * a light source emitting this color. + * + * The default value is (0.0, 0.0, 0.0, 1.0) + * + * Since 1.0 + */ +void cogl_material_set_emission (CoglHandle material, + const CoglColor *emission); + +/** + * cogl_material_get_emission: + * @material: A CoglMaterial object + * @emission: The location to store the emission color + * + * This retrieves the materials current emission color. + * + * Since 1.0 + */ +void cogl_material_get_emission (CoglHandle handle, CoglColor *emission); + +/** + * CoglMaterialAlphaFunc: + * @COGL_MATERIAL_ALPHA_FUNC_NEVER: Never let the fragment through. + * @COGL_MATERIAL_ALPHA_FUNC_LESS: Let the fragment through if the incoming + * alpha value is less than the reference alpha + * value. + * @COGL_MATERIAL_ALPHA_FUNC_EQUAL: Let the fragment through if the incoming + * alpha value equals the reference alpha + * value. + * @COGL_MATERIAL_ALPHA_FUNC_LEQUAL: Let the fragment through if the incoming + * alpha value is less than or equal to the + * reference alpha value. + * @COGL_MATERIAL_ALPHA_FUNC_GREATER: Let the fragment through if the incoming + * alpha value is greater than the reference + * alpha value. + * @COGL_MATERIAL_ALPHA_FUNC_NOTEQUAL: Let the fragment through if the incoming + * alpha value does not equal the reference + * alpha value. + * @COGL_MATERIAL_ALPHA_FUNC_GEQUAL: Let the fragment through if the incoming + * alpha value is greater than or equal to the + * reference alpha value. + * @COGL_MATERIAL_ALPHA_FUNC_ALWAYS: Always let the fragment through. + * + * Alpha testing happens before blending primitives with the framebuffer and + * gives an opportunity to discard fragments based on a comparison with the + * incoming alpha value and a reference alpha value. The #CoglMaterialAlphaFunc + * determines how the comparison is done. + */ +typedef enum _CoglMaterialAlphaFunc +{ + COGL_MATERIAL_ALPHA_FUNC_NEVER = GL_NEVER, + COGL_MATERIAL_ALPHA_FUNC_LESS = GL_LESS, + COGL_MATERIAL_ALPHA_FUNC_EQUAL = GL_EQUAL, + COGL_MATERIAL_ALPHA_FUNC_LEQUAL = GL_LEQUAL, + COGL_MATERIAL_ALPHA_FUNC_GREATER = GL_GREATER, + COGL_MATERIAL_ALPHA_FUNC_NOTEQUAL = GL_NOTEQUAL, + COGL_MATERIAL_ALPHA_FUNC_GEQUAL = GL_GEQUAL, + COGL_MATERIAL_ALPHA_FUNC_ALWAYS = GL_ALWAYS +} CoglMaterialAlphaFunc; + +/** + * cogl_material_set_alpha_test_function: + * @material: A CoglMaterial object + * @alpha_func: A @CoglMaterialAlphaFunc constant + * @alpha_reference: A reference point that the chosen alpha function uses + * to compare incoming fragments to. + * + * Before a primitive is blended with the framebuffer, it goes through an + * alpha test stage which lets you discard fragments based on the current + * alpha value. This function lets you change the function used to evaluate + * the alpha channel, and thus determine which fragments are discarded + * and which continue on to the blending stage. + * + * The default is COGL_MATERIAL_ALPHA_FUNC_ALWAYS + * + * Since 1.0 + */ +void cogl_material_set_alpha_test_function (CoglHandle material, + CoglMaterialAlphaFunc alpha_func, + float alpha_reference); + +/** + * CoglMaterialBlendFactor: + * @COGL_MATERIAL_BLEND_FACTOR_ZERO: (0, 0, 0, 0) + * @COGL_MATERIAL_BLEND_FACTOR_ONE: (1, 1, 1, 1) + * @COGL_MATERIAL_BLEND_FACTOR_SRC_COLOR: (Rs, Gs, Bs, As) + * @COGL_MATERIAL_BLEND_FACTOR_DST_COLOR: (Rd, Gd, Bd, Ad) + * @COGL_MATERIAL_BLEND_FACTOR_ONE_MINUS_SRC_COLOR: (1-Rs, 1-Gs, 1-Bs, 1-As) + * @COGL_MATERIAL_BLEND_FACTOR_ONE_MINUS_DST_COLOR: (1-Rd, 1-Gd, 1-Bd, 1-Ad) + * @COGL_MATERIAL_BLEND_FACTOR_SRC_ALPHA: (As, As, As, As) + * @COGL_MATERIAL_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA: (1-As, 1-As, 1-As, 1-As) + * @COGL_MATERIAL_BLEND_FACTOR_DST_ALPHA: (Ad, Ad, Ad, Ad) + * @COGL_MATERIAL_BLEND_FACTOR_ONE_MINUS_DST_ALPHA: (1-Ad, 1-Ad, 1-Ad, 1-Ad) + * @COGL_MATERIAL_BLEND_FACTOR_SRC_ALPHA_SATURATE: (f,f,f,1) where f=MIN(As,1-Ad) + * + * Blending occurs after the alpha test function, and combines fragments with + * the framebuffer. + * + * A fixed function is used to determine the blended color, which is based on + * the incoming source color of your fragment (Rs, Gs, Bs, As), a source + * factor (Sr, Sg, Sb, Sa), a destination color (Rd, Rg, Rb, Ra) and + * a destination factor (Dr, Dg, Db, Da), and is given by these equations: + * + * + * R = Rs*Sr + Rd*Dr + * G = Gs*Sg + Gd*Dg + * B = Bs*Sb + Bd*Db + * A = As*Sa + Ad*Da + * + * + * All factors have a range [0, 1] + * + * The factors are selected with the following constants: + */ +typedef enum _CoglMaterialBlendFactor +{ + COGL_MATERIAL_BLEND_FACTOR_ZERO = GL_ZERO, + COGL_MATERIAL_BLEND_FACTOR_ONE = GL_ONE, + COGL_MATERIAL_BLEND_FACTOR_SRC_COLOR = GL_SRC_COLOR, + COGL_MATERIAL_BLEND_FACTOR_DST_COLOR = GL_DST_COLOR, + COGL_MATERIAL_BLEND_FACTOR_ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR, + COGL_MATERIAL_BLEND_FACTOR_ONE_MINUS_DST_COLOR = GL_ONE_MINUS_DST_COLOR, + COGL_MATERIAL_BLEND_FACTOR_SRC_ALPHA = GL_SRC_ALPHA, + COGL_MATERIAL_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA, + COGL_MATERIAL_BLEND_FACTOR_DST_ALPHA = GL_DST_ALPHA, + COGL_MATERIAL_BLEND_FACTOR_ONE_MINUS_DST_ALPHA = GL_ONE_MINUS_DST_ALPHA, + COGL_MATERIAL_BLEND_FACTOR_SRC_ALPHA_SATURATE = GL_SRC_ALPHA_SATURATE, +} CoglMaterialBlendFactor; + +/** + * cogl_material_set_blend_factors: + * @material: A CoglMaterial object + * @src_factor: Chooses the @CoglMaterialBlendFactor you want plugged in to + * the blend equation. + * @dst_factor: Chooses the @CoglMaterialBlendFactor you want plugged in to + * the blend equation. + * + * This function lets you control how primitives using this material will get + * blended with the contents of your framebuffer. The blended RGBA components + * are calculated like this: + * + * (RsSr+RdDr, GsSg+GdDg, BsSb+BsSb, AsSa+AdDa) + * + * Where (Rs,Gs,Bs,As) represents your source - material- color, + * (Rd,Gd,Bd,Ad) represents your destination - framebuffer - color, + * (Sr,Sg,Sb,Sa) represents your source blend factor and + * (Dr,Dg,Db,Da) represents you destination blend factor. + * + * All factors lie in the range [0,1] and incoming color components are also + * normalized to the range [0,1] + * + * Since 1.0 + */ +void cogl_material_set_blend_factors (CoglHandle material, + CoglMaterialBlendFactor src_factor, + CoglMaterialBlendFactor dst_factor); + +/** + * cogl_material_set_layer: + * @material: A CoglMaterial object + * + * In addition to the standard OpenGL lighting model a Cogl material may have + * one or more layers comprised of textures that can be blended together in + * order, with a number of different texture combine modes. This function + * defines a new texture layer. + * + * The index values of multiple layers do not have to be consecutive; it is + * only their relative order that is important. + * + * XXX: In the future, we may define other types of material layers, such + * as purely GLSL based layers. + * + * Since 1.0 + */ +void cogl_material_set_layer (CoglHandle material, + gint layer_index, + CoglHandle texture); + +/** + * cogl_material_add_texture: + * @material: A CoglMaterial object + * @layer_index: Specifies the layer you want to remove + * + * This function removes a layer from your material + */ +void cogl_material_remove_layer (CoglHandle material, + gint layer_index); + +/** + * CoglMaterialLayerCombineFunc: + * @COGL_MATERIAL_LAYER_COMBINE_FUNC_REPLACE: Arg0 + * @COGL_MATERIAL_LAYER_COMBINE_FUNC_MODULATE: Arg0 x Arg1 + * @COGL_MATERIAL_LAYER_COMBINE_FUNC_ADD: Arg0 + Arg1 + * @COGL_MATERIAL_LAYER_COMBINE_FUNC_ADD_SIGNED: Arg0 + Arg1 - 0.5 + * @COGL_MATERIAL_LAYER_COMBINE_FUNC_INTERPOLATE: Arg0 x Arg + Arg1 x (1-Arg2) + * @COGL_MATERIAL_LAYER_COMBINE_FUNC_SUBTRACT: Arg0 - Arg1 + * @COGL_MATERIAL_LAYER_COMBINE_FUNC_DOT3_RGB: 4 x ((Arg0r - 0.5) x (Arg1r - 0.5)) + + * @COGL_MATERIAL_LAYER_COMBINE_FUNC_DOT3_RGBA: ((Arg0b - 0.5) x (Arg1b - 0.5)) + + * + * A material may comprise of 1 or more layers that can be combined using a + * number of different functions. By default layers are modulated, which is + * to say the components of the current source layer S are simply multipled + * together with the combined results of the previous layer P like this: + * + * + * (Rs*Rp, Gs*Gp, Bs*Bp, As*Ap) + * + * + * For more advanced techniques, Cogl exposes the fixed function texture + * combining capabilities of your GPU to give you greater control. + */ +typedef enum _CoglMaterialLayerCombineFunc +{ + COGL_MATERIAL_LAYER_COMBINE_FUNC_REPLACE = GL_REPLACE, + COGL_MATERIAL_LAYER_COMBINE_FUNC_MODULATE = GL_MODULATE, + COGL_MATERIAL_LAYER_COMBINE_FUNC_ADD = GL_ADD, + COGL_MATERIAL_LAYER_COMBINE_FUNC_ADD_SIGNED = GL_ADD_SIGNED, + COGL_MATERIAL_LAYER_COMBINE_FUNC_INTERPOLATE = GL_INTERPOLATE, + COGL_MATERIAL_LAYER_COMBINE_FUNC_SUBTRACT = GL_SUBTRACT, + COGL_MATERIAL_LAYER_COMBINE_FUNC_DOT3_RGB = GL_DOT3_RGB, + COGL_MATERIAL_LAYER_COMBINE_FUNC_DOT3_RGBA = GL_DOT3_RGBA +} CoglMaterialLayerCombineFunc; + +/** + * CoglMaterialLayerCombineChannels: + * @COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGB: Modify the function or argument + * src/op for the RGB components of a + * layer + * @COGL_MATERIAL_LAYER_COMBINE_CHANNELS_ALPHA: Modify the function or argument + * src/op for the Alpha component of a + * layer + * @COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGBA: Modify the function or argument + * src/op for all the components of a + * layer + * + * Cogl optionally lets you describe 2 seperate combine modes for a single + * layer; 1 for the RGB components, and 1 for the Alpha component, so in this + * case you would repeat the 3 steps documented with the + * @cogl_material_set_layer_combine_function function for each channel + * selector. + * + * (Note: you can't have different modes for each channel, so if you need more + * control you will need to use a glsl fragment shader) + */ +typedef enum _CoglMaterialLayerCombineChannels +{ + COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGB, + COGL_MATERIAL_LAYER_COMBINE_CHANNELS_ALPHA, + COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGBA +} CoglMaterialLayerCombineChannels; + +/** + * cogl_material_set_layer_combine_function: + * @material: A CoglMaterial object + * @layer_index: Specifies the layer whos combine mode you want to modify + * @channels: Specifies which channels combine mode you want to modify + * (RGB, ALPHA, or RGBA) + * @func: Specifies the function you want to use for combining fragments + * of the specified layer with the results of previously combined + * layers. + * + * There are three basic steps to describing how a layer should be combined: + * + * + * Choose a function. + * + * + * Specify the source color for each argument of the chosen function. (Note + * the functions don't all take the same number of arguments) + * + * + * Specify an operator for each argument that can modify the corresponding + * source color before the function is applied. + * + * + * + * Cogl optionally lets you describe 2 seperate combine modes for a single + * layer; 1 for the RGB components, and 1 for the Alpha component, so in this + * case you would repeat the 3 steps for each channel selector. + * + * (Note: you can't have different modes for each channel, so if you need more + * control you will need to use a glsl fragment shader) + * + * For example here is how you could elect to use the ADD function for all + * components of layer 1 in your material: + * + * //Step 1: Choose a function. Note the ADD function takes 2 arguments... + * cogl_material_set_layer_combine_function (material, + * 1, + * COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGBA) + * COGL_MATERIAL_LAYER_COMBINE_FUNC_ADD); + * //Step 2: Specify the source color for the 2 ADD function arguments... + * cogl_material_set_layer_combine_arg_src (material, + * 1,//layer index + * 0,//argument index + * COGL_MATERIAL_LAYER_COMBINE_SRC_PREVIOUS); + * cogl_material_set_layer_combine_arg_src (material, + * 1,//layer index + * 1,//argument index + * COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGBA) + * COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE); + * //Step 3: Specify the operators used to modify the arguments... + * cogl_material_set_layer_combine_arg_op (material, + * 1,//layer index + * 0,//argument index + * COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGBA, + * COGL_MATERIAL_LAYER_COMBINE_OP_SRC_COLOR); + * cogl_material_set_layer_combine_arg_op (material, + * 1,//layer index + * 1,//argument index + * COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGBA, + * COGL_MATERIAL_LAYER_COMBINE_OP_SRC_COLOR); + * + */ +void cogl_material_set_layer_combine_function (CoglHandle material, + gint layer_index, + CoglMaterialLayerCombineChannels channels, + CoglMaterialLayerCombineFunc func); + +/** + * CoglMaterialLayerCombineSrc: + * @COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE: The fragment color of the current texture layer + * @COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE0: The fragment color of texture unit 0 + * @COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE1: The fragment color of texture unit 1 + * @COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE2: The fragment color of texture unit 2..7 + * @COGL_MATERIAL_LAYER_COMBINE_SRC_CONSTANT: A fixed constant color (TODO: no API yet to specify the actual color!) + * @COGL_MATERIAL_LAYER_COMBINE_SRC_PRIMARY_COLOR: The basic color of the primitive ignoring texturing + * @COGL_MATERIAL_LAYER_COMBINE_SRC_PREVIOUS: The result of combining all previous layers + * + * Note for the constants @COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE0..n the + * numbers may not correspond to the indices you choose for your layers since + * your layer indices don't need to be contiguous. If you need to use these + * it would probably be sensible to ensure the layer indices do infact + * correspond. + */ +typedef enum _CoglMaterialLayerCombineSrc +{ + COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE = GL_TEXTURE, + + /* Can we find a nicer way... */ + COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE0 = GL_TEXTURE0, + COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE1 = GL_TEXTURE1, + COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE2 = GL_TEXTURE2, + COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE3 = GL_TEXTURE3, + COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE4 = GL_TEXTURE4, + COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE5 = GL_TEXTURE5, + COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE6 = GL_TEXTURE6, + COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE7 = GL_TEXTURE7, + /* .. who would ever need more than 8 texture layers.. :-) */ + + COGL_MATERIAL_LAYER_COMBINE_SRC_CONSTANT = GL_CONSTANT, + COGL_MATERIAL_LAYER_COMBINE_SRC_PRIMARY_COLOR = GL_PRIMARY_COLOR, + COGL_MATERIAL_LAYER_COMBINE_SRC_PREVIOUS = GL_PREVIOUS +} CoglMaterialLayerCombineSrc; + +/** + * cogl_material_set_layer_combine_arg_src: + * @material: A CoglMaterial object + * @layer_index: + * @argument: + * @channels: + * @src: + * + */ +void cogl_material_set_layer_combine_arg_src (CoglHandle material, + gint layer_index, + gint argument, + CoglMaterialLayerCombineChannels channels, + CoglMaterialLayerCombineSrc src); + +typedef enum _CoglMaterialLayerCombineOp +{ + COGL_MATERIAL_LAYER_COMBINE_OP_SRC_COLOR = GL_SRC_COLOR, + COGL_MATERIAL_LAYER_COMBINE_OP_ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR, + + COGL_MATERIAL_LAYER_COMBINE_OP_SRC_ALPHA = GL_SRC_ALPHA, + COGL_MATERIAL_LAYER_COMBINE_OP_ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA +} CoglMaterialLayerCombineOp; + +/** + * cogl_material_set_layer_combine_arg_op: + * @material: A CoglMaterial object + * @layer_index: + * @argument: + * @channels: + * @op: + * + */ +void cogl_material_set_layer_combine_arg_op (CoglHandle material, + gint layer_index, + gint argument, + CoglMaterialLayerCombineChannels channels, + CoglMaterialLayerCombineOp op); + +/* TODO: */ +#if 0 + I think it would be be really neat to support a simple string description + of the fixed function texture combine modes exposed above. I think we can + consider this stuff to be set in stone from the POV that more advanced + texture combine functions are catered for with GLSL, so it seems reasonable + to find a concise string representation that can represent all the above + modes in a *much* more readable/useable fashion. I think somthing like + this would be quite nice: + + "MODULATE(TEXTURE[RGB], PREVIOUS[A])" + "ADD(TEXTURE[A],PREVIOUS[RGB])" + "INTERPOLATE(TEXTURE[1-A], PREVIOUS[RGB])" + +void cogl_material_set_layer_rgb_combine (CoglHandle material + gint layer_index, + const char *combine_description); +void cogl_material_set_layer_alpha_combine (CoglHandle material + gint layer_index, + const char *combine_description); +#endif + +/** + * cogl_material_set_layer_matrix: + * @material: A CoglMaterial object + * + * This function lets you set a matrix that can be used to e.g. translate + * and rotate a single layer of a material used to fill your geometry. + */ +void cogl_material_set_layer_matrix (CoglHandle material, + gint layer_index, + CoglMatrix *matrix); + +/** + * cogl_material_get_cogl_enable_flags: + * @material: A CoglMaterial object + * + * This determines what flags need to be passed to cogl_enable before + * this material can be used. Normally you shouldn't need to use this + * function directly since Cogl will do this internally, but if you are + * developing custom primitives directly with OpenGL you may want to use + * this. + * + * Note: This API is hopfully just a stop-gap solution. Ideally + * cogl_enable will be replaced. + */ +/* TODO: find a nicer solution! */ +gulong +cogl_material_get_cogl_enable_flags (CoglHandle handle); + +/** + * cogl_material_get_layers: + * @material: A CoglMaterial object + * + * This function lets you access a materials internal list of layers + * for iteration. + * + * Note: Normally you shouldn't need to use this function directly since + * Cogl will do this internally, but if you are developing custom primitives + * directly with OpenGL, you will need to iterate the layers that you want + * to texture with. + * + * Note: This function may return more layers than OpenGL can use at once + * so it's your responsability limit yourself to + * CGL_MAX_COMBINED_TEXTURE_IMAGE_UNITS. + * + * Note: It's a bit out of the ordinary to return a const GList *, but it + * was considered sensible to try and avoid list manipulation for every + * primitive emitted in a scene, every frame. + */ +const GList *cogl_material_get_layers (CoglHandle material_handle); + +/** + * CoglMaterialLayerType: + * @COGL_MATERIAL_LAYER_TYPE_TEXTURE: The layer represents a CoglTexture + */ +typedef enum _CoglMaterialLayerType +{ + COGL_MATERIAL_LAYER_TYPE_TEXTURE +} CoglMaterialLayerType; + +/** + * cogl_material_layer_get_type: + * @material: A CoglMaterial object + * + * Currently there is only one type of layer defined: + * COGL_MATERIAL_LAYER_TYPE_TEXTURE, but considering we may add purely GLSL + * based layers in the future, you should write code that checks the type + * first. + * + * Note: Normally you shouldn't need to use this function directly since + * Cogl will do this internally, but if you are developing custom primitives + * directly with OpenGL, you will need to iterate the layers that you want + * to texture with, and thus should be checking the layer types. + */ +CoglMaterialLayerType cogl_material_layer_get_type (CoglHandle layer_handle); + +/** + * cogl_material_layer_get_texture: + * @layer_handle: A CoglMaterial layer object + * + * This lets you extract a CoglTexture handle for a specific layer. Normally + * you shouldn't need to use this function directly since Cogl will do this + * internally, but if you are developing custom primitives directly with + * OpenGL you may need this. + * + * Note: In the future, we may support purely GLSL based layers which will + * likley return COGL_INVALID_HANDLE if you try to get the texture. + * Considering this, you should always call cogl_material_layer_get_type + * first, to check it is of type COGL_MATERIAL_LAYER_TYPE_TEXTURE. + */ +CoglHandle cogl_material_layer_get_texture (CoglHandle layer_handle); + +/** + * CoglMaterialLayerFlags: + * @COGL_MATERIAL_LAYER_FLAG_USER_MATRIX: Means the user has supplied a + * custom texture matrix. + */ +typedef enum _CoglMaterialLayerFlags +{ + COGL_MATERIAL_LAYER_FLAG_HAS_USER_MATRIX = 1L<<0 +} CoglMaterialLayerFlags; +/* XXX: NB: if you add flags here you will need to update + * CoglMaterialLayerPrivFlags!!! */ + +/** + * cogl_material_layer_get_flags: + * @layer_handle: A CoglMaterial layer object + * + * This lets you get a number of flag attributes about the layer. Normally + * you shouldn't need to use this function directly since Cogl will do this + * internally, but if you are developing custom primitives directly with + * OpenGL you may need this. + */ +gulong cogl_material_layer_get_flags (CoglHandle layer_handle); + +/** + * CoglMaterialFlushOption: + * @COGL_MATERIAL_FLUSH_FALLBACK_MASK: Follow this by a guin32 mask + * of the layers that can't be supported with the user supplied texture + * and need to be replaced with fallback textures. (1 = fallback, and the + * least significant bit = layer 0) + * @COGL_MATERIAL_FLUSH_DISABLE_MASK: Follow this by a guint32 mask + * of the layers that you want to completly disable texturing for + * (1 = fallback, and the least significant bit = layer 0) + * @COGL_MATERIAL_FLUSH_LAYER0_OVERRIDE: Follow this by a GLuint OpenGL texture + * name to override the texture used for layer 0 of the material. This is + * intended for dealing with sliced textures where you will need to point + * to each of the texture slices in turn when drawing your geometry. + * Passing a value of 0 is the same as not passing the option at all. + */ +typedef enum _CoglMaterialFlushOption +{ + COGL_MATERIAL_FLUSH_FALLBACK_MASK = 1, + COGL_MATERIAL_FLUSH_DISABLE_MASK, + COGL_MATERIAL_FLUSH_LAYER0_OVERRIDE, +} CoglMaterialFlushOption; + +/** + * cogl_material_flush_gl_state: + * @material: A CoglMaterial object + * @...: A NULL terminated list of (CoglMaterialFlushOption, data) pairs + * + * This function commits the state of the specified CoglMaterial - including + * the texture state for all the layers - to the OpenGL[ES] driver. + * + * Normally you shouldn't need to use this function directly, but if you + * are developing a custom primitive using raw OpenGL that works with + * CoglMaterials, then you may want to use this function. + * + * Since 1.0 + */ +void cogl_material_flush_gl_state (CoglHandle material, + ...) G_GNUC_NULL_TERMINATED; + +/** + * cogl_set_source: + * @material: A CoglMaterial object + * + * This function sets the source material that will be used to fill + * subsequent geometry emitted via the cogl API. + * + * Note: in the future we may add the ability to set a front facing + * material, and a back facing material, in which case this function + * will set both to the same. + * + * Since 1.0 + */ +/* XXX: This doesn't really belong to the cogl-material API, it should + * move to cogl.h */ +void cogl_set_source (CoglHandle material); + +/** + * cogl_set_source_texture: + * @texture_handle: The Cogl texture you want as your source + * + * This is a convenience function for creating a material with the first + * layer set to #texture_handle and setting that material as the source with + * cogl_set_source. + * + * Since 1.0 + */ +void cogl_set_source_texture (CoglHandle texture_handle); + +G_END_DECLS + +#endif /* __COGL_MATERIAL_H__ */ + diff --git a/clutter/cogl/cogl-matrix.h b/clutter/cogl/cogl-matrix.h new file mode 100644 index 000000000..08c4a7009 --- /dev/null +++ b/clutter/cogl/cogl-matrix.h @@ -0,0 +1,146 @@ +#ifndef __COGL_MATRIX_H +#define __COGL_MATRIX_H + +#include + +G_BEGIN_DECLS + +/** + * SECTION:cogl-matrix + * @short_description: Fuctions for initializing and manipulating 4x4 + * matrices. + * + * Matrices are used in Cogl to describe affine model-view transforms and + * texture transforms, and projective transforms. This exposes a utility API + * that can be used for direct manipulation of these matrices. + */ + + +/** + * CoglMatrix: + * + * A CoglMatrix holds a 4x4 transform matrix. This is a single precision, + * column-major matrix which means it is compatible with what OpenGL expects. + * + * A CoglMatix can represent transforms such as, rotations, scaling, + * translation, sheering, and linear projections. You can combine these + * transforms by multiplying multiple matrices in the order you want them + * applied. + * + * The transformation of a vertex (x, y, z, w) by a CoglMatrix is given by: + * + * x_new = xx * x + xy * y + xz * z + xw * w + * y_new = yx * x + yy * y + yz * z + yw * w + * z_new = zx * x + zy * y + zz * z + zw * w + * w_new = wx * x + wy * y + wz * z + ww * w + * + * Where w is normally 1 + */ +typedef struct _CoglMatrix { + /* column 0 */ + float xx; + float yx; + float zx; + float wx; + + /* column 1 */ + float xy; + float yy; + float zy; + float wy; + + /* column 2 */ + float xz; + float yz; + float zz; + float wz; + + /* column 3 */ + float xw; + float yw; + float zw; + float ww; + + /*< private >*/ + + /* Note: we may want to extend this later with private flags + * and a cache of the inverse transform matrix. */ +} CoglMatrix; + +/** + * cogl_matrix_init_identity: + * @matrix: A 4x4 transformation matrix + * + * Resets matrix to the identity matrix: + * + * .xx=1; .xy=0; .xz=0; .xw=0; + * .yx=0; .yy=1; .yz=0; .yw=0; + * .zx=0; .zy=0; .zz=1; .zw=0; + * .wx=0; .wy=0; .wz=0; .ww=1; + * + */ +void cogl_matrix_init_identity (CoglMatrix *matrix); + +/** + * cogl_matrix_multiply: + * @result: The address of a 4x4 matrix to store the result in + * @a: A 4x4 transformation matrix + * @b: A 4x4 transformation matrix + * + * This function multiples the two supplied matricies together and stores + * the result in #result + */ +void cogl_matrix_multiply (CoglMatrix *result, + const CoglMatrix *a, + const CoglMatrix *b); + +/** + * cogl_matrix_rotate: + * @matrix: A 4x4 transformation matrix + * @angle: The angle you want to rotate in degrees + * @x: X component of your rotation vector + * @y: Y component of your rotation vector + * @z: Z component of your rotation vector + * + * This function multiples your matrix with a rotation matrix that applies + * a rotation of #angle degrees around the specified 3D vector. + */ +void cogl_matrix_rotate (CoglMatrix *matrix, + float angle, + float x, + float y, + float z); + +/* cogl_matrix_translate: + * @matrix: A 4x4 transformation matrix + * @x: The X translation you want to apply + * @y: The Y translation you want to apply + * @z: The Z translation you want to apply + * + * This function multiples your matrix with a transform matrix that translates + * along the X, Y and Z axis. + */ +void cogl_matrix_translate (CoglMatrix *matrix, + float x, + float y, + float z); + +/** + * cogl_matrix_scale: + * @matrix: A 4x4 transformation matrix + * @sx: The X scale factor + * @sy: The Y scale factor + * @sz: The Z scale factor + * + * This function multiples your matrix with a transform matrix that scales + * along the X, Y and Z axis. + */ +void cogl_matrix_scale (CoglMatrix *matrix, + float sx, + float sy, + float sz); + +G_END_DECLS + +#endif /* __COGL_MATRIX_H */ + diff --git a/clutter/cogl/cogl-path.h b/clutter/cogl/cogl-path.h index aa378647a..d1433fe4d 100644 --- a/clutter/cogl/cogl-path.h +++ b/clutter/cogl/cogl-path.h @@ -57,8 +57,7 @@ G_BEGIN_DECLS * @width: Width of the rectangle * @height: Height of the rectangle * - * Fills a rectangle at the given coordinates with the current - * drawing color in a highly optimizied fashion. + * Fills a rectangle at the given coordinates with the current source material **/ void cogl_rectangle (float x, float y, diff --git a/clutter/cogl/cogl-texture.h b/clutter/cogl/cogl-texture.h index d23ead636..b35b22f36 100644 --- a/clutter/cogl/cogl-texture.h +++ b/clutter/cogl/cogl-texture.h @@ -359,68 +359,15 @@ CoglHandle cogl_texture_ref (CoglHandle handle); */ void cogl_texture_unref (CoglHandle handle); -/** - * cogl_texture_rectangle: - * @handle: a @CoglHandle. - * @x1: x coordinate upper left on screen. - * @y1: y coordinate upper left on screen. - * @x2: x coordinate lower right on screen. - * @y2: y coordinate lower right on screen. - * @tx1: x part of texture coordinate to use for upper left pixel - * @ty1: y part of texture coordinate to use for upper left pixel - * @tx2: x part of texture coordinate to use for lower right pixel - * @ty2: y part of texture coordinate to use for left pixel - * - * Draw a rectangle from a texture to the display, to draw the entire - * texture pass in @tx1=0.0 @ty1=0.0 @tx2=1.0 @ty2=1.0. - */ -void cogl_texture_rectangle (CoglHandle handle, - float x1, - float y1, - float x2, - float y2, - float tx1, - float ty1, - float tx2, - float ty2); - -/** - * cogl_texture_polygon: - * @handle: A CoglHandle for a texture - * @n_vertices: The length of the vertices array - * @vertices: An array of #CoglTextureVertex structs - * @use_color: %TRUE if the color member of #CoglTextureVertex should be used - * - * Draws a polygon from a texture with the given model and texture - * coordinates. This can be used to draw arbitrary shapes textured - * with a COGL texture. If @use_color is %TRUE then the current COGL - * color will be changed for each vertex using the value specified in - * the color member of #CoglTextureVertex. This can be used for - * example to make the texture fade out by setting the alpha value of - * the color. - * - * All of the texture coordinates must be in the range [0,1] and - * repeating the texture is not supported. - * - * Because of the way this function is implemented it will currently - * only work if either the texture is not sliced or the backend is not - * OpenGL ES and the minifying and magnifying functions are both set - * to CGL_NEAREST. - */ -void cogl_texture_polygon (CoglHandle handle, - guint n_vertices, - CoglTextureVertex *vertices, - gboolean use_color); - /** * cogl_bitmap_new_from_file: * @filename: the file to load. * @error: a #GError or %NULL. * - * Load an image file from disk. This function can be safely called from + * Load an image file from disk. This function can be safely called from * within a thread. * - * Returns: A #CoglBitmap to the new loaded image data, or %NULL if loading + * Returns: A #CoglBitmap to the new loaded image data, or %NULL if loading * the image failed. * * Since: 1.0 @@ -452,27 +399,110 @@ gboolean cogl_bitmap_get_size_from_file (const gchar *filename, void cogl_bitmap_free (CoglBitmap *bmp); /** - * cogl_texture_multiple_rectangles: - * @handle: a @CoglHandle. + * cogl_rectangle_with_texture_coords: + * @x1: x coordinate upper left on screen. + * @y1: y coordinate upper left on screen. + * @x2: x coordinate lower right on screen. + * @y2: y coordinate lower right on screen. + * @tx1: x part of texture coordinate to use for upper left pixel + * @ty1: y part of texture coordinate to use for upper left pixel + * @tx2: x part of texture coordinate to use for lower right pixel + * @ty2: y part of texture coordinate to use for left pixel + * + * Draw a rectangle using the current material and supply texture coordinates + * to be used for the first texture layer of the material. To draw the entire + * texture pass in @tx1=0.0 @ty1=0.0 @tx2=1.0 @ty2=1.0. + * + * Since 1.0 + */ +void cogl_rectangle_with_texture_coords (float x1, + float y1, + float x2, + float y2, + float tx1, + float ty1, + float tx2, + float ty2); + +/** + * cogl_rectangle_with_multitexture_coords: + * @x1: x coordinate upper left on screen. + * @y1: y coordinate upper left on screen. + * @x2: x coordinate lower right on screen. + * @y2: y coordinate lower right on screen. + * @tex_coords: An array containing groups of 4 float values: + * [tx1, ty1, tx2, ty2] that are interpreted as two texture coordinates; one + * for the upper left texel, and one for the lower right texel. Each value + * should be between 0.0 and 1.0, where the coordinate (0.0, 0.0) represents + * the top left of the texture, and (1.0, 1.0) the bottom right. + * @tex_coords_len: The length of the tex_coords array. (e.g. for one layer + * and one group of texture coordinates, this would be 4) + * + * This function draws a rectangle using the current source material to + * texture or fill with. As a material may contain multiple texture layers + * this interface lets you supply texture coordinates for each layer of the + * material. + * + * The first pair of coordinates are for the first layer (with the smallest + * layer index) and if you supply less texture coordinates than there are + * layers in the current source material then default texture coordinates + * (0.0, 0.0, 1.0, 1.0) are generated. + * + * Since 1.0 + */ +void cogl_rectangle_with_multitexture_coords (float x1, + float y1, + float x2, + float y2, + const float *tex_coords, + gint tex_coords_len); + +/** + * cogl_rectangles_with_texture_coords: * @verts: an array of vertices * @n_rects: number of rectangles to draw * * Draws a series of rectangles in the same way that - * cogl_texture_rectangle() does. In some situations it can give a + * cogl_rectangle_with_texture_coords() does. In some situations it can give a * significant performance boost to use this function rather than - * calling cogl_texture_rectangle() separately for each rectangle. + * calling cogl_rectangle_with_texture_coords() separately for each rectangle. * * @verts should point to an array of #floats with * @n_rects * 8 elements. Each group of 8 values corresponds to the * parameters x1, y1, x2, y2, tx1, ty1, tx2 and ty2 and have the same - * meaning as in cogl_texture_rectangle(). + * meaning as in cogl_rectangle_with_texture_coords(). * * Since: 0.8.6 */ -void cogl_texture_multiple_rectangles - (CoglHandle handle, - const float *verts, - guint n_rects); +void cogl_rectangles_with_texture_coords (const float *verts, + guint n_rects); + +/** + * cogl_polygon: + * @vertices: An array of #CoglTextureVertex structs + * @n_vertices: The length of the vertices array + * @use_color: %TRUE if the color member of #CoglTextureVertex should be used + * + * Draws a convex polygon using the current source material to fill / texture + * with according to the texture coordinates passed. + * + * If @use_color is %TRUE then the color will be changed for each vertex using + * the value specified in the color member of #CoglTextureVertex. This can be + * used for example to make the texture fade out by setting the alpha value of + * the color. + * + * All of the texture coordinates must be in the range [0,1] and repeating the + * texture is not supported. + * + * Because of the way this function is implemented it will currently only work + * if either the texture is not sliced or the backend is not OpenGL ES and the + * minifying and magnifying functions are both set to CGL_NEAREST. + * + * Since 1.0 + */ +void cogl_polygon (CoglTextureVertex *vertices, + guint n_vertices, + gboolean use_color); G_END_DECLS diff --git a/clutter/cogl/cogl.h.in b/clutter/cogl/cogl.h.in index 5fa6399b1..ff35f33d6 100644 --- a/clutter/cogl/cogl.h.in +++ b/clutter/cogl/cogl.h.in @@ -32,10 +32,13 @@ #include +#include +#include #include #include #include #include +#include #include #include #include @@ -402,19 +405,6 @@ void cogl_enable_depth_test (gboolean setting); */ void cogl_enable_backface_culling (gboolean setting); -/** - * cogl_alpha_func: - * @func: the comparison function to use, one of CGL_NEVER, CGL_LESS, - * CGL_EQUAL, CGL_LEQUAL, CGL_GREATER, CGL_NOTEQUAL, CGL_GEQUAL and GL_ALWAYS. - * @ref: reference value. - * - * Changes the alpha test to use the specified function specified in @func, - * comparing with the value in @ref. The default function is CGL_ALWAYS the - * initial reference value is 1.0. - */ -void cogl_alpha_func (COGLenum func, - float ref); - /** * cogl_fog_set: * @fog_color: The color of the fog diff --git a/clutter/cogl/common/Makefile.am b/clutter/cogl/common/Makefile.am index d883dadba..50e21b83e 100644 --- a/clutter/cogl/common/Makefile.am +++ b/clutter/cogl/common/Makefile.am @@ -31,5 +31,7 @@ libclutter_cogl_common_la_SOURCES = \ cogl-clip-stack.c \ cogl-fixed.c \ cogl-color.c \ - cogl-vertex-buffer-private.h \ - cogl-vertex-buffer.c + cogl-vertex-buffer-private.h \ + cogl-vertex-buffer.c \ + cogl-matrix.c \ + cogl-material.c diff --git a/clutter/cogl/common/cogl-handle.h b/clutter/cogl/common/cogl-handle.h index 79deb7f8c..1cbb8a013 100644 --- a/clutter/cogl/common/cogl-handle.h +++ b/clutter/cogl/common/cogl-handle.h @@ -57,6 +57,12 @@ #define COGL_HANDLE_DEFINE(TypeName, type_name, handle_array) \ \ + static CoglHandle * \ + _cogl_##type_name##_handle_from_pointer (Cogl##TypeName *obj) \ + { \ + return (CoglHandle)obj; \ + } \ + \ static gint \ _cogl_##type_name##_handle_find (CoglHandle handle) \ { \ diff --git a/clutter/cogl/common/cogl-material-private.h b/clutter/cogl/common/cogl-material-private.h new file mode 100644 index 000000000..b4c36e78d --- /dev/null +++ b/clutter/cogl/common/cogl-material-private.h @@ -0,0 +1,99 @@ +#ifndef __COGL_MATERIAL_PRIVATE_H +#define __COGL_MATERIAL_PRIVATE_H + +#include "cogl-material.h" +#include "cogl-matrix.h" + +#include + +typedef struct _CoglMaterial CoglMaterial; +typedef struct _CoglMaterialLayer CoglMaterialLayer; + +/* XXX: I don't think gtk-doc supports having private enums so these aren't + * bundled in with CoglMaterialLayerFlags */ +typedef enum _CoglMaterialLayerPrivFlags +{ + /* Ref: CoglMaterialLayerFlags + COGL_MATERIAL_LAYER_FLAG_HAS_USER_MATRIX = 1L<<0 + */ + COGL_MATERIAL_LAYER_FLAG_DIRTY = 1L<<1, + COGL_MATERIAL_LAYER_FLAG_DEFAULT_COMBINE = 1L<<2 +} CoglMaterialLayerPrivFlags; + +/* For tracking the state of a layer that's been flushed to OpenGL */ +typedef struct _CoglLayerInfo +{ + CoglHandle handle; + gulong flags; + GLenum gl_target; + GLuint gl_texture; + gboolean fallback; + gboolean disabled; + gboolean layer0_overridden; +} CoglLayerInfo; + +struct _CoglMaterialLayer +{ + guint ref_count; + guint index; /*!< lowest index is blended first then others + on top */ + gulong flags; + CoglHandle texture; /*!< The texture for this layer, or COGL_INVALID_HANDLE + for an empty layer */ + + /* Determines how the color of individual texture fragments + * are calculated. */ + CoglMaterialLayerCombineFunc texture_combine_rgb_func; + CoglMaterialLayerCombineSrc texture_combine_rgb_src[3]; + CoglMaterialLayerCombineOp texture_combine_rgb_op[3]; + + CoglMaterialLayerCombineFunc texture_combine_alpha_func; + CoglMaterialLayerCombineSrc texture_combine_alpha_src[3]; + CoglMaterialLayerCombineOp texture_combine_alpha_op[3]; + + /* TODO: Support purely GLSL based material layers */ + + CoglMatrix matrix; +}; + +typedef enum _CoglMaterialFlags +{ + COGL_MATERIAL_FLAG_ENABLE_BLEND = 1L<<0, + COGL_MATERIAL_FLAG_SHOWN_SAMPLER_WARNING = 1L<<1, + COGL_MATERIAL_FLAG_DIRTY = 1L<<2, + COGL_MATERIAL_FLAG_LAYERS_DIRTY = 1L<<3, + COGL_MATERIAL_FLAG_DEFAULT_COLOR = 1L<<4, + COGL_MATERIAL_FLAG_DEFAULT_GL_MATERIAL = 1L<<5, + COGL_MATERIAL_FLAG_DEFAULT_ALPHA_FUNC = 1L<<6, + COGL_MATERIAL_FLAG_DEFAULT_BLEND_FUNC = 1L<<7 +} CoglMaterialFlags; + +struct _CoglMaterial +{ + guint ref_count; + + gulong flags; + + /* If no lighting is enabled; this is the basic material color */ + GLfloat unlit[4]; + + /* Standard OpenGL lighting model attributes */ + GLfloat ambient[4]; + GLfloat diffuse[4]; + GLfloat specular[4]; + GLfloat emission[4]; + GLfloat shininess; + + /* Determines what fragments are discarded based on their alpha */ + CoglMaterialAlphaFunc alpha_func; + GLfloat alpha_func_reference; + + /* Determines how this material is blended with other primitives */ + CoglMaterialBlendFactor blend_src_factor; + CoglMaterialBlendFactor blend_dst_factor; + + GList *layers; +}; + +#endif /* __COGL_MATERIAL_PRIVATE_H */ + diff --git a/clutter/cogl/common/cogl-material.c b/clutter/cogl/common/cogl-material.c new file mode 100644 index 000000000..925e77af6 --- /dev/null +++ b/clutter/cogl/common/cogl-material.c @@ -0,0 +1,1152 @@ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "cogl.h" +#include "cogl-internal.h" +#include "cogl-context.h" +#include "cogl-handle.h" + +#include "cogl-material-private.h" + +#include +#include + +/* + * GL/GLES compatability defines for material thingies: + */ + +#ifdef HAVE_COGL_GLES2 +#define glAlphaFunc cogl_wrap_glAlphaFunc +#endif + +static void _cogl_material_free (CoglMaterial *tex); +static void _cogl_material_layer_free (CoglMaterialLayer *layer); + +COGL_HANDLE_DEFINE (Material, material, material_handles); +COGL_HANDLE_DEFINE (MaterialLayer, + material_layer, + material_layer_handles); + +CoglHandle +cogl_material_new (void) +{ + /* Create new - blank - material */ + CoglMaterial *material = g_new0 (CoglMaterial, 1); + GLfloat *unlit = material->unlit; + GLfloat *ambient = material->ambient; + GLfloat *diffuse = material->diffuse; + GLfloat *specular = material->specular; + GLfloat *emission = material->emission; + + material->ref_count = 1; + COGL_HANDLE_DEBUG_NEW (material, material); + + /* Use the same defaults as the GL spec... */ + unlit[0] = 1.0; unlit[1] = 1.0; unlit[2] = 1.0; unlit[3] = 1.0; + material->flags |= COGL_MATERIAL_FLAG_DEFAULT_COLOR; + + /* Use the same defaults as the GL spec... */ + ambient[0] = 0.2; ambient[1] = 0.2; ambient[2] = 0.2; ambient[3] = 1.0; + diffuse[0] = 0.8; diffuse[1] = 0.8; diffuse[2] = 0.8; diffuse[3] = 1.0; + specular[0] = 0; specular[1] = 0; specular[2] = 0; specular[3] = 1.0; + emission[0] = 0; emission[1] = 0; emission[2] = 0; emission[3] = 1.0; + material->flags |= COGL_MATERIAL_FLAG_DEFAULT_GL_MATERIAL; + + /* Use the same defaults as the GL spec... */ + material->alpha_func = COGL_MATERIAL_ALPHA_FUNC_ALWAYS; + material->alpha_func_reference = 0.0; + material->flags |= COGL_MATERIAL_FLAG_DEFAULT_ALPHA_FUNC; + + /* Not the same as the GL default, but seems saner... */ + material->blend_src_factor = COGL_MATERIAL_BLEND_FACTOR_SRC_ALPHA; + material->blend_dst_factor = COGL_MATERIAL_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; + material->flags |= COGL_MATERIAL_FLAG_DEFAULT_BLEND_FUNC; + + material->layers = NULL; + + return _cogl_material_handle_new (material); +} + +static void +_cogl_material_free (CoglMaterial *material) +{ + /* Frees material resources but its handle is not + released! Do that separately before this! */ + + g_list_foreach (material->layers, + (GFunc)cogl_material_layer_unref, NULL); + g_free (material); +} + +void +cogl_material_get_color (CoglHandle handle, + CoglColor *color) +{ + CoglMaterial *material; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + + cogl_color_set_from_4f (color, + material->unlit[0], + material->unlit[1], + material->unlit[2], + material->unlit[3]); +} + +void +cogl_material_set_color (CoglHandle handle, + const CoglColor *unlit_color) +{ + CoglMaterial *material; + GLfloat unlit[4]; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + + unlit[0] = cogl_color_get_red_float (unlit_color); + unlit[1] = cogl_color_get_green_float (unlit_color); + unlit[2] = cogl_color_get_blue_float (unlit_color); + unlit[3] = cogl_color_get_alpha_float (unlit_color); + if (memcmp (unlit, material->unlit, sizeof (unlit)) == 0) + return; + + memcpy (material->unlit, unlit, sizeof (unlit)); + + if (unlit[0] == 1.0 && + unlit[1] == 1.0 && + unlit[2] == 1.0 && + unlit[3] == 1.0) + material->flags |= COGL_MATERIAL_FLAG_DEFAULT_COLOR; + + if (unlit[3] != 1.0) + material->flags |= COGL_MATERIAL_FLAG_ENABLE_BLEND; + + material->flags |= COGL_MATERIAL_FLAG_DIRTY; + material->flags &= ~COGL_MATERIAL_FLAG_DEFAULT_COLOR; +} + +void +cogl_material_set_color4ub (CoglHandle handle, + guint8 red, + guint8 green, + guint8 blue, + guint8 alpha) +{ + CoglColor color; + cogl_color_set_from_4ub (&color, red, green, blue, alpha); + cogl_material_set_color (handle, &color); +} + +void +cogl_material_get_ambient (CoglHandle handle, + CoglColor *ambient) +{ + CoglMaterial *material; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + + cogl_color_set_from_4f (ambient, + material->ambient[0], + material->ambient[1], + material->ambient[2], + material->ambient[3]); +} + +void +cogl_material_set_ambient (CoglHandle handle, + const CoglColor *ambient_color) +{ + CoglMaterial *material; + GLfloat *ambient; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + + ambient = material->ambient; + ambient[0] = cogl_color_get_red_float (ambient_color); + ambient[1] = cogl_color_get_green_float (ambient_color); + ambient[2] = cogl_color_get_blue_float (ambient_color); + ambient[3] = cogl_color_get_alpha_float (ambient_color); + + material->flags |= COGL_MATERIAL_FLAG_DIRTY; + material->flags &= ~COGL_MATERIAL_FLAG_DEFAULT_GL_MATERIAL; +} + +void +cogl_material_get_diffuse (CoglHandle handle, + CoglColor *diffuse) +{ + CoglMaterial *material; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + + cogl_color_set_from_4f (diffuse, + material->diffuse[0], + material->diffuse[1], + material->diffuse[2], + material->diffuse[3]); +} + +void +cogl_material_set_diffuse (CoglHandle handle, + const CoglColor *diffuse_color) +{ + CoglMaterial *material; + GLfloat *diffuse; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + + diffuse = material->diffuse; + diffuse[0] = cogl_color_get_red_float (diffuse_color); + diffuse[1] = cogl_color_get_green_float (diffuse_color); + diffuse[2] = cogl_color_get_blue_float (diffuse_color); + diffuse[3] = cogl_color_get_alpha_float (diffuse_color); + + material->flags |= COGL_MATERIAL_FLAG_DIRTY; + material->flags &= ~COGL_MATERIAL_FLAG_DEFAULT_GL_MATERIAL; +} + +void +cogl_material_set_ambient_and_diffuse (CoglHandle handle, + const CoglColor *color) +{ + cogl_material_set_ambient (handle, color); + cogl_material_set_diffuse (handle, color); +} + +void +cogl_material_get_specular (CoglHandle handle, + CoglColor *specular) +{ + CoglMaterial *material; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + + cogl_color_set_from_4f (specular, + material->specular[0], + material->specular[1], + material->specular[2], + material->specular[3]); +} + +void +cogl_material_set_specular (CoglHandle handle, + const CoglColor *specular_color) +{ + CoglMaterial *material; + GLfloat *specular; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + + specular = material->specular; + specular[0] = cogl_color_get_red_float (specular_color); + specular[1] = cogl_color_get_green_float (specular_color); + specular[2] = cogl_color_get_blue_float (specular_color); + specular[3] = cogl_color_get_alpha_float (specular_color); + + material->flags |= COGL_MATERIAL_FLAG_DIRTY; + material->flags &= ~COGL_MATERIAL_FLAG_DEFAULT_GL_MATERIAL; +} + +float +cogl_material_get_shininess (CoglHandle handle) +{ + CoglMaterial *material; + + g_return_val_if_fail (cogl_is_material (handle), 0); + + material = _cogl_material_pointer_from_handle (handle); + + return material->shininess; +} + +void +cogl_material_set_shininess (CoglHandle handle, + float shininess) +{ + CoglMaterial *material; + + g_return_if_fail (cogl_is_material (handle)); + + if (shininess < 0.0 || shininess > 1.0) + g_warning ("Out of range shininess %f supplied for material\n", + shininess); + + material = _cogl_material_pointer_from_handle (handle); + + material->shininess = (GLfloat)shininess * 128.0; + + material->flags |= COGL_MATERIAL_FLAG_DIRTY; + material->flags &= ~COGL_MATERIAL_FLAG_DEFAULT_GL_MATERIAL; +} + +void +cogl_material_get_emission (CoglHandle handle, + CoglColor *emission) +{ + CoglMaterial *material; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + + cogl_color_set_from_4f (emission, + material->emission[0], + material->emission[1], + material->emission[2], + material->emission[3]); +} + +void +cogl_material_set_emission (CoglHandle handle, + const CoglColor *emission_color) +{ + CoglMaterial *material; + GLfloat *emission; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + + emission = material->emission; + emission[0] = cogl_color_get_red_float (emission_color); + emission[1] = cogl_color_get_green_float (emission_color); + emission[2] = cogl_color_get_blue_float (emission_color); + emission[3] = cogl_color_get_alpha_float (emission_color); + + material->flags |= COGL_MATERIAL_FLAG_DIRTY; + material->flags &= ~COGL_MATERIAL_FLAG_DEFAULT_GL_MATERIAL; +} + +void +cogl_material_set_alpha_test_function (CoglHandle handle, + CoglMaterialAlphaFunc alpha_func, + float alpha_reference) +{ + CoglMaterial *material; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + material->alpha_func = alpha_func; + material->alpha_func_reference = (GLfloat)alpha_reference; + + material->flags |= COGL_MATERIAL_FLAG_DIRTY; + material->flags &= ~COGL_MATERIAL_FLAG_DEFAULT_ALPHA_FUNC; +} + +void +cogl_material_set_blend_factors (CoglHandle handle, + CoglMaterialBlendFactor src_factor, + CoglMaterialBlendFactor dst_factor) +{ + CoglMaterial *material; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + material->blend_src_factor = src_factor; + material->blend_dst_factor = dst_factor; + + material->flags |= COGL_MATERIAL_FLAG_DIRTY; + material->flags &= ~COGL_MATERIAL_FLAG_DEFAULT_BLEND_FUNC; +} + +/* Asserts that a layer corresponding to the given index exists. If no + * match is found, then a new empty layer is added. + */ +static CoglMaterialLayer * +_cogl_material_get_layer (CoglMaterial *material, + gint index, + gboolean create_if_not_found) +{ + CoglMaterialLayer *layer; + GList *tmp; + CoglHandle layer_handle; + + for (tmp = material->layers; tmp != NULL; tmp = tmp->next) + { + layer = + _cogl_material_layer_pointer_from_handle ((CoglHandle)tmp->data); + if (layer->index == index) + return layer; + + /* The layers are always sorted, so at this point we know this layer + * doesn't exist */ + if (layer->index > index) + break; + } + /* NB: if we now insert a new layer before tmp, that will maintain order. + */ + + if (!create_if_not_found) + return NULL; + + layer = g_new0 (CoglMaterialLayer, 1); + + layer->ref_count = 1; + layer->index = index; + layer->flags = COGL_MATERIAL_LAYER_FLAG_DEFAULT_COMBINE; + layer->texture = COGL_INVALID_HANDLE; + + /* Choose the same default combine mode as OpenGL: + * MODULATE(PREVIOUS[RGBA],TEXTURE[RGBA]) */ + layer->texture_combine_rgb_func = COGL_MATERIAL_LAYER_COMBINE_FUNC_MODULATE; + layer->texture_combine_rgb_src[0] = COGL_MATERIAL_LAYER_COMBINE_SRC_PREVIOUS; + layer->texture_combine_rgb_src[1] = COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE; + layer->texture_combine_rgb_op[0] = COGL_MATERIAL_LAYER_COMBINE_OP_SRC_COLOR; + layer->texture_combine_rgb_op[1] = COGL_MATERIAL_LAYER_COMBINE_OP_SRC_COLOR; + layer->texture_combine_alpha_func = + COGL_MATERIAL_LAYER_COMBINE_FUNC_MODULATE; + layer->texture_combine_alpha_src[0] = + COGL_MATERIAL_LAYER_COMBINE_SRC_PREVIOUS; + layer->texture_combine_alpha_src[1] = + COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE; + layer->texture_combine_alpha_op[0] = + COGL_MATERIAL_LAYER_COMBINE_OP_SRC_ALPHA; + layer->texture_combine_alpha_op[1] = + COGL_MATERIAL_LAYER_COMBINE_OP_SRC_ALPHA; + + cogl_matrix_init_identity (&layer->matrix); + + layer_handle = _cogl_material_layer_handle_new (layer); + /* Note: see comment after for() loop above */ + material->layers = + g_list_insert_before (material->layers, tmp, layer_handle); + + return layer; +} + +void +cogl_material_set_layer (CoglHandle material_handle, + gint layer_index, + CoglHandle texture_handle) +{ + CoglMaterial *material; + CoglMaterialLayer *layer; + int n_layers; + + g_return_if_fail (cogl_is_material (material_handle)); + g_return_if_fail (cogl_is_texture (texture_handle)); + + material = _cogl_material_pointer_from_handle (material_handle); + layer = _cogl_material_get_layer (material_handle, layer_index, TRUE); + + /* XXX: If we expose manual control over ENABLE_BLEND, we'll add + * a flag to know when it's user configured, so we don't trash it */ + if (cogl_texture_get_format (texture_handle) & COGL_A_BIT) + material->flags |= COGL_MATERIAL_FLAG_ENABLE_BLEND; + + n_layers = g_list_length (material->layers); + if (n_layers >= CGL_MAX_COMBINED_TEXTURE_IMAGE_UNITS) + { + if (!(material->flags & COGL_MATERIAL_FLAG_SHOWN_SAMPLER_WARNING)) + { + g_warning ("Your hardware does not have enough texture samplers" + "to handle this many texture layers"); + material->flags |= COGL_MATERIAL_FLAG_SHOWN_SAMPLER_WARNING; + } + /* Note: We always make a best effort attempt to display as many + * layers as possible, so this isn't an _error_ */ + /* Note: in the future we may support enabling/disabling layers + * too, so it may become valid to add more than + * MAX_COMBINED_TEXTURE_IMAGE_UNITS layers. */ + } + + cogl_texture_ref (texture_handle); + + if (layer->texture) + cogl_texture_unref (layer->texture); + + layer->texture = texture_handle; + layer->flags |= COGL_MATERIAL_LAYER_FLAG_DIRTY; + material->flags |= COGL_MATERIAL_FLAG_LAYERS_DIRTY; +} + +void +cogl_material_set_layer_combine_function ( + CoglHandle handle, + gint layer_index, + CoglMaterialLayerCombineChannels channels, + CoglMaterialLayerCombineFunc func) +{ + CoglMaterial *material; + CoglMaterialLayer *layer; + gboolean set_alpha_func = FALSE; + gboolean set_rgb_func = FALSE; + + g_return_if_fail (cogl_is_material (handle)); + + material = _cogl_material_pointer_from_handle (handle); + layer = _cogl_material_get_layer (material, layer_index, TRUE); + + if (channels == COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGBA) + set_alpha_func = set_rgb_func = TRUE; + else if (channels == COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGB) + set_rgb_func = TRUE; + else if (channels == COGL_MATERIAL_LAYER_COMBINE_CHANNELS_ALPHA) + set_alpha_func = TRUE; + + if (set_rgb_func) + layer->texture_combine_rgb_func = func; + if (set_alpha_func) + layer->texture_combine_alpha_func = func; + + material->flags |= COGL_MATERIAL_FLAG_LAYERS_DIRTY; + layer->flags |= COGL_MATERIAL_LAYER_FLAG_DIRTY; + layer->flags &= ~COGL_MATERIAL_LAYER_FLAG_DEFAULT_COMBINE; +} + +void +cogl_material_set_layer_combine_arg_src ( + CoglHandle handle, + gint layer_index, + gint argument, + CoglMaterialLayerCombineChannels channels, + CoglMaterialLayerCombineSrc src) +{ + CoglMaterial *material; + CoglMaterialLayer *layer; + gboolean set_arg_alpha_src = FALSE; + gboolean set_arg_rgb_src = FALSE; + + g_return_if_fail (cogl_is_material (handle)); + g_return_if_fail (argument >=0 && argument <= 3); + + material = _cogl_material_pointer_from_handle (handle); + layer = _cogl_material_get_layer (material, layer_index, TRUE); + + if (channels == COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGBA) + set_arg_alpha_src = set_arg_rgb_src = TRUE; + else if (channels == COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGB) + set_arg_rgb_src = TRUE; + else if (channels == COGL_MATERIAL_LAYER_COMBINE_CHANNELS_ALPHA) + set_arg_alpha_src = TRUE; + + if (set_arg_rgb_src) + layer->texture_combine_rgb_src[argument] = src; + if (set_arg_alpha_src) + layer->texture_combine_alpha_src[argument] = src; + + material->flags |= COGL_MATERIAL_FLAG_LAYERS_DIRTY; + layer->flags |= COGL_MATERIAL_LAYER_FLAG_DIRTY; + layer->flags &= ~COGL_MATERIAL_LAYER_FLAG_DEFAULT_COMBINE; +} + +void +cogl_material_set_layer_combine_arg_op ( + CoglHandle material_handle, + gint layer_index, + gint argument, + CoglMaterialLayerCombineChannels channels, + CoglMaterialLayerCombineOp op) +{ + CoglMaterial *material; + CoglMaterialLayer *layer; + gboolean set_arg_alpha_op = FALSE; + gboolean set_arg_rgb_op = FALSE; + + g_return_if_fail (cogl_is_material (material_handle)); + g_return_if_fail (argument >=0 && argument <= 3); + + material = _cogl_material_pointer_from_handle (material_handle); + layer = _cogl_material_get_layer (material, layer_index, TRUE); + + if (channels == COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGBA) + set_arg_alpha_op = set_arg_rgb_op = TRUE; + else if (channels == COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGB) + set_arg_rgb_op = TRUE; + else if (channels == COGL_MATERIAL_LAYER_COMBINE_CHANNELS_ALPHA) + set_arg_alpha_op = TRUE; + + if (set_arg_rgb_op) + layer->texture_combine_rgb_op[argument] = op; + if (set_arg_alpha_op) + layer->texture_combine_alpha_op[argument] = op; + + material->flags |= COGL_MATERIAL_FLAG_LAYERS_DIRTY; + layer->flags |= COGL_MATERIAL_LAYER_FLAG_DIRTY; + layer->flags &= ~COGL_MATERIAL_LAYER_FLAG_DEFAULT_COMBINE; +} + +void +cogl_material_set_layer_matrix (CoglHandle material_handle, + gint layer_index, + CoglMatrix *matrix) +{ + CoglMaterial *material; + CoglMaterialLayer *layer; + + g_return_if_fail (cogl_is_material (material_handle)); + + material = _cogl_material_pointer_from_handle (material_handle); + layer = _cogl_material_get_layer (material, layer_index, TRUE); + + layer->matrix = *matrix; + + material->flags |= COGL_MATERIAL_FLAG_LAYERS_DIRTY; + layer->flags |= COGL_MATERIAL_LAYER_FLAG_DIRTY; + layer->flags |= COGL_MATERIAL_LAYER_FLAG_HAS_USER_MATRIX; + layer->flags &= ~COGL_MATERIAL_LAYER_FLAG_DEFAULT_COMBINE; +} + +static void +_cogl_material_layer_free (CoglMaterialLayer *layer) +{ + cogl_texture_unref (layer->texture); + g_free (layer); +} + +void +cogl_material_remove_layer (CoglHandle material_handle, + gint layer_index) +{ + CoglMaterial *material; + CoglMaterialLayer *layer; + GList *tmp; + + g_return_if_fail (cogl_is_material (material_handle)); + + material = _cogl_material_pointer_from_handle (material_handle); + material->flags &= ~COGL_MATERIAL_FLAG_ENABLE_BLEND; + for (tmp = material->layers; tmp != NULL; tmp = tmp->next) + { + layer = tmp->data; + if (layer->index == layer_index) + { + CoglHandle handle = + _cogl_material_layer_handle_from_pointer (layer); + cogl_material_layer_unref (handle); + material->layers = g_list_remove (material->layers, layer); + continue; + } + + /* XXX: If we expose manual control over ENABLE_BLEND, we'll add + * a flag to know when it's user configured, so we don't trash it */ + if (cogl_texture_get_format (layer->texture) & COGL_A_BIT) + material->flags |= COGL_MATERIAL_FLAG_ENABLE_BLEND; + } + + if (material->unlit[3] != 1.0) + material->flags |= COGL_MATERIAL_FLAG_ENABLE_BLEND; + material->flags |= COGL_MATERIAL_FLAG_LAYERS_DIRTY; +} + +/* XXX: This API is hopfully just a stop-gap solution. Ideally cogl_enable + * will be replaced. */ +gulong +cogl_material_get_cogl_enable_flags (CoglHandle material_handle) +{ + CoglMaterial *material; + gulong enable_flags = 0; + + _COGL_GET_CONTEXT (ctx, 0); + + g_return_val_if_fail (cogl_is_material (material_handle), 0); + + material = _cogl_material_pointer_from_handle (material_handle); + + /* Enable blending if the geometry has an associated alpha color, + * or the material wants blending enabled. */ + if (material->flags & COGL_MATERIAL_FLAG_ENABLE_BLEND + || ctx->color_alpha < 255) + enable_flags |= COGL_ENABLE_BLEND; + + return enable_flags; +} + +/* It's a bit out of the ordinary to return a const GList *, but it's + * probably sensible to try and avoid list manipulation for every + * primitive emitted in a scene, every frame. + * + * Alternativly; we could either add a _foreach function, or maybe + * a function that gets a passed a buffer (that may be stack allocated) + * by the caller. + */ +const GList * +cogl_material_get_layers (CoglHandle material_handle) +{ + CoglMaterial *material; + + g_return_val_if_fail (cogl_is_material (material_handle), NULL); + + material = _cogl_material_pointer_from_handle (material_handle); + + return material->layers; +} + +CoglMaterialLayerType +cogl_material_layer_get_type (CoglHandle layer_handle) +{ + return COGL_MATERIAL_LAYER_TYPE_TEXTURE; +} + +CoglHandle +cogl_material_layer_get_texture (CoglHandle layer_handle) +{ + CoglMaterialLayer *layer; + + g_return_val_if_fail (cogl_is_material_layer (layer_handle), + COGL_INVALID_HANDLE); + + layer = _cogl_material_layer_pointer_from_handle (layer_handle); + return layer->texture; +} + +gulong +cogl_material_layer_get_flags (CoglHandle layer_handle) +{ + CoglMaterialLayer *layer; + + g_return_val_if_fail (cogl_is_material_layer (layer_handle), 0); + + layer = _cogl_material_layer_pointer_from_handle (layer_handle); + + return layer->flags & COGL_MATERIAL_LAYER_FLAG_HAS_USER_MATRIX; +} + +static guint +get_n_args_for_combine_func (CoglMaterialLayerCombineFunc func) +{ + switch (func) + { + case COGL_MATERIAL_LAYER_COMBINE_FUNC_REPLACE: + return 1; + case COGL_MATERIAL_LAYER_COMBINE_FUNC_MODULATE: + case COGL_MATERIAL_LAYER_COMBINE_FUNC_ADD: + case COGL_MATERIAL_LAYER_COMBINE_FUNC_ADD_SIGNED: + case COGL_MATERIAL_LAYER_COMBINE_FUNC_SUBTRACT: + case COGL_MATERIAL_LAYER_COMBINE_FUNC_DOT3_RGB: + case COGL_MATERIAL_LAYER_COMBINE_FUNC_DOT3_RGBA: + return 2; + case COGL_MATERIAL_LAYER_COMBINE_FUNC_INTERPOLATE: + return 3; + } + return 0; +} + +static void +_cogl_material_layer_flush_gl_sampler_state (CoglMaterialLayer *layer, + CoglLayerInfo *gl_layer_info) +{ + int n_rgb_func_args; + int n_alpha_func_args; + + if (!(gl_layer_info && + gl_layer_info->flags & COGL_MATERIAL_LAYER_FLAG_DEFAULT_COMBINE && + layer->flags & COGL_MATERIAL_LAYER_FLAG_DEFAULT_COMBINE)) + { + GE (glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE)); + + /* Set the combiner functions... */ + GE (glTexEnvi (GL_TEXTURE_ENV, + GL_COMBINE_RGB, + layer->texture_combine_rgb_func)); + GE (glTexEnvi (GL_TEXTURE_ENV, + GL_COMBINE_ALPHA, + layer->texture_combine_alpha_func)); + + /* + * Setup the function arguments... + */ + + /* For the RGB components... */ + n_rgb_func_args = + get_n_args_for_combine_func (layer->texture_combine_rgb_func); + + GE (glTexEnvi (GL_TEXTURE_ENV, GL_SRC0_RGB, + layer->texture_combine_rgb_src[0])); + GE (glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND0_RGB, + layer->texture_combine_rgb_op[0])); + if (n_rgb_func_args > 1) + { + GE (glTexEnvi (GL_TEXTURE_ENV, GL_SRC1_RGB, + layer->texture_combine_rgb_src[1])); + GE (glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND1_RGB, + layer->texture_combine_rgb_op[1])); + } + if (n_rgb_func_args > 2) + { + GE (glTexEnvi (GL_TEXTURE_ENV, GL_SRC2_RGB, + layer->texture_combine_rgb_src[2])); + GE (glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND2_RGB, + layer->texture_combine_rgb_op[2])); + } + + /* For the Alpha component */ + n_alpha_func_args = + get_n_args_for_combine_func (layer->texture_combine_alpha_func); + + GE (glTexEnvi (GL_TEXTURE_ENV, GL_SRC0_ALPHA, + layer->texture_combine_alpha_src[0])); + GE (glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, + layer->texture_combine_alpha_op[0])); + if (n_alpha_func_args > 1) + { + GE (glTexEnvi (GL_TEXTURE_ENV, GL_SRC1_ALPHA, + layer->texture_combine_alpha_src[1])); + GE (glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, + layer->texture_combine_alpha_op[1])); + } + if (n_alpha_func_args > 2) + { + GE (glTexEnvi (GL_TEXTURE_ENV, GL_SRC2_ALPHA, + layer->texture_combine_alpha_src[2])); + GE (glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, + layer->texture_combine_alpha_op[2])); + } + } + + if (gl_layer_info && + (gl_layer_info->flags & COGL_MATERIAL_LAYER_FLAG_HAS_USER_MATRIX || + layer->flags & COGL_MATERIAL_LAYER_FLAG_HAS_USER_MATRIX)) + { + GE (glMatrixMode (GL_TEXTURE)); + GE (glLoadMatrixf ((GLfloat *)&layer->matrix)); + GE (glMatrixMode (GL_MODELVIEW)); + } +} + +/** + * _cogl_material_flush_layers_gl_state: + * @fallback_mask: is a bitmask of the material layers that need to be + * replaced with the default, fallback textures. The fallback textures are + * fully transparent textures so they hopefully wont contribute to the + * texture combining. + * + * The intention of fallbacks is to try and preserve + * the number of layers the user is expecting so that texture coordinates + * they gave will mostly still correspond to the textures they intended, and + * have a fighting chance of looking close to their originally intended + * result. + * + * @disable_mask: is a bitmask of the material layers that will simply have + * texturing disabled. It's only really intended for disabling all layers + * > X; i.e. we'd expect to see a contiguous run of 0 starting from the LSB + * and at some point the remaining bits flip to 1. It might work to disable + * arbitrary layers; though I'm not sure a.t.m how OpenGL would take to + * that. + * + * The intention of the disable_mask is for emitting geometry when the user + * hasn't supplied enough texture coordinates for all the layers and it's + * not possible to auto generate default texture coordinates for those + * layers. + * + * @layer0_override_texture: forcibly tells us to bind this GL texture name for + * layer 0 instead of plucking the gl_texture from the CoglTexture of layer + * 0. + * + * The intention of this is for any geometry that supports sliced textures. + * The code will can iterate each of the slices and re-flush the material + * forcing the GL texture of each slice in turn. + * + * XXX: It might also help if we could specify a texture matrix for code + * dealing with slicing that would be multiplied with the users own matrix. + * + * Normaly texture coords in the range [0, 1] refer to the extents of the + * texture, but when your GL texture represents a slice of the real texture + * (from the users POV) then a texture matrix would be a neat way of + * transforming the mapping for each slice. + * + * Currently for textured rectangles we manually calculate the texture + * coords for each slice based on the users given coords, but this solution + * isn't ideal, and can't be used with CoglVertexBuffers. + */ +static void +_cogl_material_flush_layers_gl_state (CoglMaterial *material, + guint32 fallback_mask, + guint32 disable_mask, + GLuint layer0_override_texture) +{ + GList *tmp; + int i; + + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + for (tmp = material->layers, i = 0; tmp != NULL; tmp = tmp->next, i++) + { + CoglHandle layer_handle = (CoglHandle)tmp->data; + CoglMaterialLayer *layer = + _cogl_material_layer_pointer_from_handle (layer_handle); + CoglLayerInfo *gl_layer_info = NULL; + CoglLayerInfo new_gl_layer_info; + CoglHandle tex_handle; + GLuint gl_texture; + GLenum gl_target; +#ifdef HAVE_COGL_GLES2 + GLenum gl_internal_format; +#endif + + new_gl_layer_info.layer0_overridden = + layer0_override_texture ? TRUE : FALSE; + new_gl_layer_info.fallback = + (fallback_mask & (1<current_layers->len) + { + gl_layer_info = + &g_array_index (ctx->current_layers, CoglLayerInfo, i); + + if (gl_layer_info->handle == layer_handle && + !(layer->flags & COGL_MATERIAL_LAYER_FLAG_DIRTY) && + (gl_layer_info->layer0_overridden + == new_gl_layer_info.layer0_overridden) && + (gl_layer_info->fallback + == new_gl_layer_info.fallback) && + (gl_layer_info->disabled + == new_gl_layer_info.disabled)) + continue; + } + + tex_handle = layer->texture; + cogl_texture_get_gl_texture (tex_handle, &gl_texture, &gl_target); + + if (new_gl_layer_info.layer0_overridden) + gl_texture = layer0_override_texture; + else if (new_gl_layer_info.fallback) + { + if (gl_target == GL_TEXTURE_2D) + tex_handle = ctx->default_gl_texture_2d_tex; +#ifdef HAVE_COGL_GL + else if (gl_target == GL_TEXTURE_RECTANGLE_ARB) + tex_handle = ctx->default_gl_texture_rect_tex; +#endif + else + { + g_warning ("We don't have a default texture we can use to fill " + "in for an invalid material layer, since it was " + "using an unsupported texture target "); + /* might get away with this... */ + tex_handle = ctx->default_gl_texture_2d_tex; + } + cogl_texture_get_gl_texture (tex_handle, &gl_texture, NULL); + } + +#ifdef HAVE_COGL_GLES2 + { + CoglTexture *tex = + _cogl_texture_pointer_from_handle (tex_handle); + gl_internal_format = tex->gl_intformat; + } +#endif + + GE (glActiveTexture (GL_TEXTURE0 + i)); + + if (!gl_layer_info + || gl_layer_info->gl_target != gl_target + || gl_layer_info->gl_texture != gl_texture) + { +#ifdef HAVE_COGL_GLES2 + cogl_gles2_wrapper_bind_texture (gl_target, + gl_texture, + gl_internal_format); +#else + GE (glBindTexture (gl_target, gl_texture)); +#endif + } + + /* Disable the previous target if it was different */ + if (gl_layer_info && + gl_layer_info->gl_target != gl_target && + !gl_layer_info->disabled) + GE (glDisable (gl_layer_info->gl_target)); + + /* Enable/Disable the new target */ + if (!new_gl_layer_info.disabled) + { + if (!(gl_layer_info && + gl_layer_info->gl_target == gl_target && + !gl_layer_info->disabled)) + GE (glEnable (gl_target)); + } + else + { + if (!(gl_layer_info && + gl_layer_info->gl_target == gl_target && + gl_layer_info->disabled)) + GE (glDisable (gl_target)); + } + + _cogl_material_layer_flush_gl_sampler_state (layer, gl_layer_info); + + new_gl_layer_info.handle = layer_handle; + new_gl_layer_info.flags = layer->flags; + new_gl_layer_info.gl_target = gl_target; + new_gl_layer_info.gl_texture = gl_texture; + + if (i < ctx->current_layers->len) + *gl_layer_info = new_gl_layer_info; + else + g_array_append_val (ctx->current_layers, new_gl_layer_info); + + layer->flags &= ~COGL_MATERIAL_LAYER_FLAG_DIRTY; + + if ((i+1) >= CGL_MAX_COMBINED_TEXTURE_IMAGE_UNITS) + break; + } + + /* Disable additional texture units that may have previously been in use.. */ + for (; i < ctx->current_layers->len; i++) + { + CoglLayerInfo *gl_layer_info = + &g_array_index (ctx->current_layers, CoglLayerInfo, i); + + if (!gl_layer_info->disabled) + { + GE (glActiveTexture (GL_TEXTURE0 + i)); + GE (glDisable (gl_layer_info->gl_target)); + gl_layer_info->disabled = TRUE; + } + } + + material->flags &= ~COGL_MATERIAL_FLAG_DIRTY; +} + +static void +_cogl_material_flush_base_gl_state (CoglMaterial *material) +{ + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + if (!(ctx->current_material_flags & COGL_MATERIAL_FLAG_DEFAULT_COLOR + && material->flags & COGL_MATERIAL_FLAG_DEFAULT_COLOR)) + { + /* GLES doesn't have glColor4fv... */ + GE (glColor4f (material->unlit[0], + material->unlit[1], + material->unlit[2], + material->unlit[3])); + } + + if (!(ctx->current_material_flags & COGL_MATERIAL_FLAG_DEFAULT_GL_MATERIAL + && material->flags & COGL_MATERIAL_FLAG_DEFAULT_GL_MATERIAL)) + { + /* FIXME - we only need to set these if lighting is enabled... */ + GE (glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, material->ambient)); + GE (glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, material->diffuse)); + GE (glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, material->specular)); + GE (glMaterialfv (GL_FRONT_AND_BACK, GL_EMISSION, material->emission)); + GE (glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, &material->shininess)); + } + + if (!(ctx->current_material_flags & COGL_MATERIAL_FLAG_DEFAULT_ALPHA_FUNC + && material->flags & COGL_MATERIAL_FLAG_DEFAULT_ALPHA_FUNC)) + { + /* NB: Currently the Cogl defines are compatible with the GL ones: */ + GE (glAlphaFunc (material->alpha_func, material->alpha_func_reference)); + } + + if (!(ctx->current_material_flags & COGL_MATERIAL_FLAG_DEFAULT_BLEND_FUNC + && material->flags & COGL_MATERIAL_FLAG_DEFAULT_BLEND_FUNC)) + { + GE (glBlendFunc (material->blend_src_factor, material->blend_dst_factor)); + } +} + +void +cogl_material_flush_gl_state (CoglHandle handle, ...) +{ + CoglMaterial *material; + va_list ap; + CoglMaterialFlushOption option; + guint32 fallback_layers = 0; + guint32 disable_layers = 0; + GLuint layer0_override_texture = 0; + + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + material = _cogl_material_pointer_from_handle (handle); + + if (ctx->current_material == material && + !(material->flags & COGL_MATERIAL_FLAG_DIRTY) && + !(material->flags & COGL_MATERIAL_FLAG_LAYERS_DIRTY)) + return; + + if (ctx->current_material != material || + material->flags & COGL_MATERIAL_FLAG_DIRTY) + _cogl_material_flush_base_gl_state (material); + + if (ctx->current_material != material || + material->flags & COGL_MATERIAL_FLAG_LAYERS_DIRTY) + { + va_start (ap, handle); + while ((option = va_arg (ap, CoglMaterialFlushOption))) + { + if (option == COGL_MATERIAL_FLUSH_FALLBACK_MASK) + fallback_layers = va_arg (ap, guint32); + else if (option == COGL_MATERIAL_FLUSH_DISABLE_MASK) + disable_layers = va_arg (ap, guint32); + else if (option == COGL_MATERIAL_FLUSH_LAYER0_OVERRIDE) + layer0_override_texture = va_arg (ap, GLuint); + } + va_end (ap); + + _cogl_material_flush_layers_gl_state (material, + fallback_layers, + disable_layers, + layer0_override_texture); + } + + /* NB: we have to take a reference so that next time + * cogl_material_flush_gl_state is called, we can compare the incomming + * material pointer with ctx->current_material + */ + cogl_material_ref (handle); + cogl_material_unref (ctx->current_material); + + ctx->current_material = handle; + ctx->current_material_flags = material->flags; +} + + + +/* TODO: Should go in cogl.c, but that implies duplication which is also + * not ideal. */ +void +cogl_set_source (CoglHandle material_handle) +{ + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + g_return_if_fail (cogl_is_material (material_handle)); + + if (ctx->source_material == material_handle) + return; + + cogl_material_ref (material_handle); + + if (ctx->source_material) + cogl_material_unref (ctx->source_material); + + ctx->source_material = material_handle; +} +/* TODO: add cogl_set_front_source (), and cogl_set_back_source () */ + +void +cogl_set_source_texture (CoglHandle texture_handle) +{ + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + CoglColor white; + + cogl_material_set_layer (ctx->default_material, 0, texture_handle); + cogl_color_set_from_4ub (&white, 0xff, 0xff, 0xff, 0xff); + cogl_material_set_color (ctx->default_material, &white); + cogl_set_source (ctx->default_material); +} + diff --git a/clutter/cogl/common/cogl-matrix.c b/clutter/cogl/common/cogl-matrix.c new file mode 100644 index 000000000..8786da570 --- /dev/null +++ b/clutter/cogl/common/cogl-matrix.c @@ -0,0 +1,138 @@ +#include + +#include +#include + +void +cogl_matrix_init_identity (CoglMatrix *matrix) +{ + matrix->xx = 1; matrix->xy = 0; matrix->xz = 0; matrix->xw = 0; + matrix->yx = 0; matrix->yy = 1; matrix->yz = 0; matrix->yw = 0; + matrix->zx = 0; matrix->zy = 0; matrix->zz = 1; matrix->zw = 0; + matrix->wx = 0; matrix->wy = 0; matrix->wz = 0; matrix->ww = 1; +} + +void +cogl_matrix_multiply (CoglMatrix *result, + const CoglMatrix *a, + const CoglMatrix *b) +{ + CoglMatrix r; + + /* row 0 */ + r.xx = a->xx * b->xx + a->xy * b->yx + a->xz * b->zx + a->xw * b->wx; + r.xy = a->xx * b->xy + a->xy * b->yy + a->xz * b->zy + a->xw * b->wy; + r.xz = a->xx * b->xz + a->xy * b->yz + a->xz * b->zz + a->xw * b->wz; + r.xw = a->xx * b->xw + a->xy * b->yw + a->xz * b->zw + a->xw * b->ww; + + /* row 1 */ + r.yx = a->yx * b->xx + a->yy * b->yx + a->yz * b->zx + a->yw * b->wx; + r.yy = a->yx * b->xy + a->yy * b->yy + a->yz * b->zy + a->yw * b->wy; + r.yz = a->yx * b->xz + a->yy * b->yz + a->yz * b->zz + a->yw * b->wz; + r.yw = a->yx * b->xw + a->yy * b->yw + a->yz * b->zw + a->yw * b->ww; + + /* row 2 */ + r.zx = a->zx * b->xx + a->zy * b->yx + a->zz * b->zx + a->zw * b->wx; + r.zy = a->zx * b->xy + a->zy * b->yy + a->zz * b->zy + a->zw * b->wy; + r.zz = a->zx * b->xz + a->zy * b->yz + a->zz * b->zz + a->zw * b->wz; + r.zw = a->zx * b->xw + a->zy * b->yw + a->zz * b->zw + a->zw * b->ww; + + /* row 3 */ + r.wx = a->wx * b->xx + a->wy * b->yx + a->wz * b->zx + a->ww * b->wx; + r.wy = a->wx * b->xy + a->wy * b->yy + a->wz * b->zy + a->ww * b->wy; + r.wz = a->wx * b->xz + a->wy * b->yz + a->wz * b->zz + a->ww * b->wz; + r.ww = a->wx * b->xw + a->wy * b->yw + a->wz * b->zw + a->ww * b->ww; + + /* The idea was that having this unrolled; it might be easier for the + * compiler to vectorize, but that's probably not true. Mesa does it + * using a single for (i=0; i<4; i++) approach, may that's better... + */ + + *result = r; +} + +/** + * cogl_3dmatrix_rotate: + * matrix: A 3D Affine transformation matrix + * angle: The angle in degrees you want to rotate by + * x: The X component of your rotation vector + * y: The Y component of your rotation vector + * z: The Z component of your rotation vector + * + * The matrix is multiplied with a rotation matrix representing a rotation + * of angle degress around the vector (x,y,z) + */ +void +cogl_matrix_rotate (CoglMatrix *matrix, + float angle, + float x, + float y, + float z) +{ + CoglMatrix rotation; + CoglMatrix result; + angle *= G_PI / 180.0f; + float c = cosf (angle); + float s = sinf (angle); + + rotation.xx = x * x * (1.0f - c) + c; + rotation.yx = y * x * (1.0f - c) + z * s; + rotation.zx = x * z * (1.0f - c) - y * s; + rotation.wx = 0.0f; + + rotation.xy = x * y * (1.0f - c) - z * s; + rotation.yy = y * y * (1.0f - c) + c; + rotation.zy = y * z * (1.0f - c) + x * s; + rotation.wy = 0.0f; + + rotation.xz = x * z * (1.0f - c) + y * s; + rotation.yz = y * z * (1.0f - c) - x * s; + rotation.zz = z * z * (1.0f - c) + c; + rotation.wz = 0.0f; + + rotation.xw = 0.0f; + rotation.yw = 0.0f; + rotation.zw = 0.0f; + rotation.ww = 1.0f; + + cogl_matrix_multiply (&result, matrix, &rotation); + *matrix = result; +} + +void +cogl_matrix_translate (CoglMatrix *matrix, + float x, + float y, + float z) +{ + matrix->xw = matrix->xx * x + matrix->xy * y + matrix->xz * z + matrix->xw; + matrix->yw = matrix->yx * x + matrix->yy * y + matrix->yz * z + matrix->yw; + matrix->zw = matrix->zx * x + matrix->zy * y + matrix->zz * z + matrix->zw; + matrix->ww = matrix->wx * x + matrix->wy * y + matrix->wz * z + matrix->ww; +} + +void +cogl_matrix_scale (CoglMatrix *matrix, + float sx, + float sy, + float sz) +{ + matrix->xx *= sx; matrix->xy *= sy; matrix->xz *= sz; + matrix->yx *= sx; matrix->yy *= sy; matrix->yz *= sz; + matrix->zx *= sx; matrix->zy *= sy; matrix->zz *= sz; + matrix->wx *= sx; matrix->wy *= sy; matrix->wz *= sz; +} + +#if 0 +gboolean +cogl_matrix_invert (CoglMatrix *matrix) +{ + /* TODO */ + /* Note: It might be nice to also use the flag based tricks that mesa does + * to alow it to track the type of transformations a matrix represents + * so it can use various assumptions to optimise the inversion. + */ +} +#endif + + diff --git a/clutter/cogl/common/cogl-primitives.c b/clutter/cogl/common/cogl-primitives.c index 88526cd58..40843fc2b 100644 --- a/clutter/cogl/common/cogl-primitives.c +++ b/clutter/cogl/common/cogl-primitives.c @@ -43,19 +43,18 @@ void _cogl_path_add_node (gboolean new_sub_path, float y); void _cogl_path_fill_nodes (); void _cogl_path_stroke_nodes (); -void _cogl_rectangle (float x, - float y, - float width, - float height); + void cogl_rectangle (float x, float y, float width, float height) { - cogl_clip_ensure (); - - _cogl_rectangle (x, y, width, height); + cogl_rectangle_with_multitexture_coords (x, y, + x+width, + y+height, + NULL, + 0); } void diff --git a/clutter/cogl/common/cogl-vertex-buffer.c b/clutter/cogl/common/cogl-vertex-buffer.c index 45833cdd6..0b617244c 100644 --- a/clutter/cogl/common/cogl-vertex-buffer.c +++ b/clutter/cogl/common/cogl-vertex-buffer.c @@ -134,6 +134,7 @@ #include "cogl-context.h" #include "cogl-handle.h" #include "cogl-vertex-buffer-private.h" +#include "cogl-texture-private.h" #define PAD_FOR_ALIGNMENT(VAR, TYPE_SIZE) \ (VAR = TYPE_SIZE + ((VAR - 1) & ~(TYPE_SIZE - 1))) @@ -1416,12 +1417,15 @@ get_gl_type_from_attribute_flags (CoglVertexBufferAttribFlags flags) static void enable_state_for_drawing_attributes_buffer (CoglVertexBuffer *buffer) { - GList *tmp; - GLenum gl_type; - GLuint generic_index = 0; - gulong enable_flags = COGL_ENABLE_BLEND; - /* FIXME: I don't think it's appropriate to force enable - * GL_BLEND here. */ + GList *tmp; + GLenum gl_type; + GLuint generic_index = 0; + gulong enable_flags = 0; + guint max_texcoord_attrib_unit = 0; + const GList *layers; + guint32 fallback_mask = 0; + guint32 disable_mask = ~0; + int i; _COGL_GET_CONTEXT (ctx, NO_RETVAL); @@ -1460,17 +1464,16 @@ enable_state_for_drawing_attributes_buffer (CoglVertexBuffer *buffer) (const GLvoid *)attribute->u.vbo_offset)); break; case COGL_VERTEX_BUFFER_ATTRIB_FLAG_TEXTURE_COORD_ARRAY: - /* FIXME: set the active texture unit */ - /* NB: Cogl currently manages unit 0 */ - enable_flags |= (COGL_ENABLE_TEXCOORD_ARRAY - | COGL_ENABLE_TEXTURE_2D); - /* FIXME: I don't think it's appropriate to force enable - * GL_TEXTURE_2D here. */ - /* GE (glEnableClientState (GL_VERTEX_ARRAY)); */ + GE (glClientActiveTexture (GL_TEXTURE0 + + attribute->texture_unit)); + GE (glEnableClientState (GL_TEXTURE_COORD_ARRAY)); GE (glTexCoordPointer (attribute->n_components, gl_type, attribute->stride, (const GLvoid *)attribute->u.vbo_offset)); + if (attribute->texture_unit > max_texcoord_attrib_unit) + max_texcoord_attrib_unit = attribute->texture_unit; + disable_mask &= ~(1 << attribute->texture_unit); break; case COGL_VERTEX_BUFFER_ATTRIB_FLAG_VERTEX_ARRAY: enable_flags |= COGL_ENABLE_VERTEX_ARRAY; @@ -1505,8 +1508,51 @@ enable_state_for_drawing_attributes_buffer (CoglVertexBuffer *buffer) } } - cogl_enable (enable_flags); + layers = cogl_material_get_layers (ctx->source_material); + for (tmp = (GList *)layers, i = 0; + tmp != NULL && i <= max_texcoord_attrib_unit; + tmp = tmp->next, i++) + { + CoglHandle layer = (CoglHandle)tmp->data; + CoglHandle tex_handle = cogl_material_layer_get_texture (layer); + CoglTexture *texture = + _cogl_texture_pointer_from_handle (tex_handle); + if (cogl_texture_is_sliced (tex_handle) + || _cogl_texture_span_has_waste (texture, 0, 0)) + { + g_warning ("Disabling layer %d of the current source material, " + "because texturing with the vertex buffer API is not " + "currently supported using sliced textures, or textures " + "with waste\n", i); + + /* XXX: maybe we can add a mechanism for users to forcibly use + * textures with waste where it would be their responsability to use + * texture coords in the range [0,1] such that sampling outside isn't + * required. We can then use a texture matrix (or a modification of + * the users own matrix) to map 1 to the edge of the texture data. + * + * Potentially, given the same guarantee as above we could also + * support a single sliced layer too. We would have to redraw the + * vertices once for each layer, each time with a fiddled texture + * matrix. + */ + fallback_mask |= (1 << i); + } + else if (!(disable_mask & (1 << i))) + fallback_mask |= (1 << i); + } + + cogl_material_flush_gl_state (ctx->source_material, + COGL_MATERIAL_FLUSH_FALLBACK_MASK, + fallback_mask, + COGL_MATERIAL_FLUSH_DISABLE_MASK, + disable_mask, + NULL); + + enable_flags |= cogl_material_get_cogl_enable_flags (ctx->source_material); + + cogl_enable (enable_flags); } static void @@ -1550,9 +1596,9 @@ disable_state_for_drawing_buffer (CoglVertexBuffer *buffer) GE (glDisableClientState (GL_NORMAL_ARRAY)); break; case COGL_VERTEX_BUFFER_ATTRIB_FLAG_TEXTURE_COORD_ARRAY: - /* FIXME: set the active texture unit */ - /* NB: Cogl currently manages unit 0 */ - /* GE (glDisableClientState (GL_VERTEX_ARRAY)); */ + GE (glClientActiveTexture (GL_TEXTURE0 + + attribute->texture_unit)); + GE (glDisableClientState (GL_TEXTURE_COORD_ARRAY)); break; case COGL_VERTEX_BUFFER_ATTRIB_FLAG_VERTEX_ARRAY: /* GE (glDisableClientState (GL_VERTEX_ARRAY)); */ diff --git a/clutter/cogl/gl/Makefile.am b/clutter/cogl/gl/Makefile.am index 114dfac24..394629ce9 100644 --- a/clutter/cogl/gl/Makefile.am +++ b/clutter/cogl/gl/Makefile.am @@ -10,7 +10,9 @@ libclutterinclude_HEADERS = \ $(top_builddir)/clutter/cogl/cogl-shader.h \ $(top_builddir)/clutter/cogl/cogl-texture.h \ $(top_builddir)/clutter/cogl/cogl-types.h \ - $(top_builddir)/clutter/cogl/cogl-vertex-buffer.h + $(top_builddir)/clutter/cogl/cogl-vertex-buffer.h \ + $(top_builddir)/clutter/cogl/cogl-material.h \ + $(top_builddir)/clutter/cogl/cogl-matrix.h INCLUDES = \ -I$(top_srcdir) \ diff --git a/clutter/cogl/gl/cogl-context.c b/clutter/cogl/gl/cogl-context.c index 486940711..252f3d294 100644 --- a/clutter/cogl/gl/cogl-context.c +++ b/clutter/cogl/gl/cogl-context.c @@ -31,6 +31,8 @@ #include "cogl-internal.h" #include "cogl-util.h" #include "cogl-context.h" +#include "cogl-texture-private.h" +#include "cogl-material-private.h" #include @@ -39,6 +41,9 @@ static CoglContext *_context = NULL; gboolean cogl_create_context () { + GLubyte default_texture_data[] = { 0xff, 0xff, 0xff, 0x0 }; + gulong enable_flags = 0; + if (_context != NULL) return FALSE; @@ -52,27 +57,40 @@ cogl_create_context () _context->enable_flags = 0; _context->color_alpha = 255; - _context->path_nodes = g_array_new (FALSE, FALSE, sizeof (CoglPathNode)); - _context->last_path = 0; + _context->material_handles = NULL; + _context->material_layer_handles = NULL; + _context->default_material = cogl_material_new (); + _context->source_material = NULL; _context->texture_handles = NULL; - _context->texture_vertices = g_array_new (FALSE, FALSE, + _context->default_gl_texture_2d_tex = COGL_INVALID_HANDLE; + _context->default_gl_texture_rect_tex = COGL_INVALID_HANDLE; + + _context->journal = g_array_new (FALSE, FALSE, sizeof (CoglJournalEntry)); + _context->logged_vertices = g_array_new (FALSE, FALSE, sizeof (GLfloat)); + _context->static_indices = g_array_new (FALSE, FALSE, sizeof (GLushort)); + _context->polygon_vertices = g_array_new (FALSE, FALSE, sizeof (CoglTextureGLVertex)); - _context->texture_indices = g_array_new (FALSE, FALSE, - sizeof (GLushort)); + + _context->current_material = NULL; + _context->current_material_flags = 0; + _context->current_layers = g_array_new (FALSE, FALSE, + sizeof (CoglLayerInfo)); + _context->n_texcoord_arrays_enabled = 0; _context->fbo_handles = NULL; _context->draw_buffer = COGL_WINDOW_BUFFER; - _context->blend_src_factor = CGL_SRC_ALPHA; - _context->blend_dst_factor = CGL_ONE_MINUS_SRC_ALPHA; - _context->shader_handles = NULL; _context->program_handles = NULL; _context->vertex_buffer_handles = NULL; + _context->path_nodes = g_array_new (FALSE, FALSE, sizeof (CoglPathNode)); + _context->last_path = 0; + _context->stencil_material = cogl_material_new (); + _context->pf_glGenRenderbuffersEXT = NULL; _context->pf_glBindRenderbufferEXT = NULL; _context->pf_glRenderbufferStorageEXT = NULL; @@ -118,15 +136,37 @@ cogl_create_context () _context->pf_glDrawRangeElements = NULL; - /* Init OpenGL state */ - GE( glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) ); - GE( glColorMask (TRUE, TRUE, TRUE, FALSE) ); - GE( glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ); - cogl_enable (0); - /* Initialise the clip stack */ _cogl_clip_stack_state_init (); + /* Create default textures used for fall backs */ + _context->default_gl_texture_2d_tex = + cogl_texture_new_from_data (1, /* width */ + 1, /* height */ + -1, /* max waste */ + FALSE, /* auto mipmap */ + COGL_PIXEL_FORMAT_RGBA_8888, /* data format */ + /* internal format */ + COGL_PIXEL_FORMAT_RGBA_8888, + 0, /* auto calc row stride */ + &default_texture_data); + _context->default_gl_texture_rect_tex = + cogl_texture_new_from_data (1, /* width */ + 1, /* height */ + -1, /* max waste */ + FALSE, /* auto mipmap */ + COGL_PIXEL_FORMAT_RGBA_8888, /* data format */ + /* internal format */ + COGL_PIXEL_FORMAT_RGBA_8888, + 0, /* auto calc row stride */ + &default_texture_data); + + cogl_set_source (_context->default_material); + cogl_material_flush_gl_state (_context->source_material, NULL); + enable_flags = + cogl_material_get_cogl_enable_flags (_context->source_material); + cogl_enable (enable_flags); + return TRUE; } @@ -150,10 +190,25 @@ cogl_destroy_context () if (_context->program_handles) g_array_free (_context->program_handles, TRUE); - if (_context->texture_vertices) - g_array_free (_context->texture_vertices, TRUE); - if (_context->texture_indices) - g_array_free (_context->texture_indices, TRUE); + if (_context->default_gl_texture_2d_tex) + cogl_texture_unref (_context->default_gl_texture_2d_tex); + if (_context->default_gl_texture_rect_tex) + cogl_texture_unref (_context->default_gl_texture_rect_tex); + + if (_context->default_material) + cogl_material_unref (_context->default_material); + + if (_context->journal) + g_array_free (_context->journal, TRUE); + if (_context->logged_vertices) + g_array_free (_context->logged_vertices, TRUE); + + if (_context->static_indices) + g_array_free (_context->static_indices, TRUE); + if (_context->polygon_vertices) + g_array_free (_context->polygon_vertices, TRUE); + if (_context->current_layers) + g_array_free (_context->current_layers, TRUE); g_free (_context); } diff --git a/clutter/cogl/gl/cogl-context.h b/clutter/cogl/gl/cogl-context.h index 3ed044421..2f0dd0817 100644 --- a/clutter/cogl/gl/cogl-context.h +++ b/clutter/cogl/gl/cogl-context.h @@ -45,32 +45,37 @@ typedef struct /* Enable cache */ gulong enable_flags; guint8 color_alpha; - COGLenum blend_src_factor; - COGLenum blend_dst_factor; gboolean enable_backface_culling; - /* Primitives */ - floatVec2 path_start; - floatVec2 path_pen; - GArray *path_nodes; - guint last_path; - floatVec2 path_nodes_min; - floatVec2 path_nodes_max; - /* Cache of inverse projection matrix */ GLfloat inverse_projection[16]; /* Textures */ - GArray *texture_handles; - GArray *texture_vertices; - GArray *texture_indices; - /* The gl texture number that the above vertices apply to. This to - detect when a different slice is encountered so that the vertices - can be flushed */ - GLuint texture_current; - GLenum texture_target; - GLenum texture_wrap_mode; + GArray *texture_handles; + CoglHandle default_gl_texture_2d_tex; + CoglHandle default_gl_texture_rect_tex; + + /* Materials */ + GArray *material_handles; + GArray *material_layer_handles; + CoglHandle default_material; + CoglHandle source_material; + + /* Batching geometry... */ + /* We journal the texture rectangles we want to submit to OpenGL so + * we have an oppertunity to optimise the final order so that we + * can batch things together. */ + GArray *journal; + GArray *logged_vertices; + GArray *static_indices; + GArray *polygon_vertices; + + /* Some simple caching, to minimize state changes... */ + CoglHandle current_material; + gulong current_material_flags; + GArray *current_layers; + guint n_texcoord_arrays_enabled; /* Framebuffer objects */ GArray *fbo_handles; @@ -88,6 +93,15 @@ typedef struct /* Vertex buffers */ GArray *vertex_buffer_handles; + /* Primitives */ + floatVec2 path_start; + floatVec2 path_pen; + GArray *path_nodes; + guint last_path; + floatVec2 path_nodes_min; + floatVec2 path_nodes_max; + CoglHandle stencil_material; + /* Relying on glext.h to define these */ COGL_PFNGLGENRENDERBUFFERSEXTPROC pf_glGenRenderbuffersEXT; COGL_PFNGLDELETERENDERBUFFERSEXTPROC pf_glDeleteRenderbuffersEXT; diff --git a/clutter/cogl/gl/cogl-defines.h.in b/clutter/cogl/gl/cogl-defines.h.in index 480aafb0e..eea0f3d14 100644 --- a/clutter/cogl/gl/cogl-defines.h.in +++ b/clutter/cogl/gl/cogl-defines.h.in @@ -309,6 +309,7 @@ typedef GLuint COGLuint; #define CGL_MAX_TEXTURE_STACK_DEPTH GL_MAX_TEXTURE_STACK_DEPTH #define CGL_MAX_VIEWPORT_DIMS GL_MAX_VIEWPORT_DIMS #define CGL_MAX_CLIENT_ATTRIB_STACK_DEPTH GL_MAX_CLIENT_ATTRIB_STACK_DEPTH +#define CGL_MAX_COMBINED_TEXTURE_IMAGE_UNITS GL_MAX_TEXTURE_UNITS #define CGL_ATTRIB_STACK_DEPTH GL_ATTRIB_STACK_DEPTH #define CGL_CLIENT_ATTRIB_STACK_DEPTH GL_CLIENT_ATTRIB_STACK_DEPTH #define CGL_COLOR_CLEAR_VALUE GL_COLOR_CLEAR_VALUE diff --git a/clutter/cogl/gl/cogl-internal.h b/clutter/cogl/gl/cogl-internal.h index 8887788c2..735e005ee 100644 --- a/clutter/cogl/gl/cogl-internal.h +++ b/clutter/cogl/gl/cogl-internal.h @@ -51,13 +51,10 @@ const char *_cogl_error_string(GLenum errorCode); #endif /* COGL_DEBUG */ #define COGL_ENABLE_BLEND (1<<1) -#define COGL_ENABLE_TEXTURE_2D (1<<2) -#define COGL_ENABLE_ALPHA_TEST (1<<3) -#define COGL_ENABLE_TEXTURE_RECT (1<<4) -#define COGL_ENABLE_VERTEX_ARRAY (1<<5) -#define COGL_ENABLE_TEXCOORD_ARRAY (1<<6) -#define COGL_ENABLE_COLOR_ARRAY (1<<7) -#define COGL_ENABLE_BACKFACE_CULLING (1<<8) +#define COGL_ENABLE_ALPHA_TEST (1<<2) +#define COGL_ENABLE_VERTEX_ARRAY (1<<3) +#define COGL_ENABLE_COLOR_ARRAY (1<<4) +#define COGL_ENABLE_BACKFACE_CULLING (1<<5) gint _cogl_get_format_bpp (CoglPixelFormat format); diff --git a/clutter/cogl/gl/cogl-primitives.c b/clutter/cogl/gl/cogl-primitives.c index e44565780..ddb3bcc5e 100644 --- a/clutter/cogl/gl/cogl-primitives.c +++ b/clutter/cogl/gl/cogl-primitives.c @@ -38,31 +38,17 @@ #define _COGL_MAX_BEZ_RECURSE_DEPTH 16 -void -_cogl_rectangle (float x, - float y, - float width, - float height) -{ - _COGL_GET_CONTEXT (ctx, NO_RETVAL); - - cogl_enable (ctx->color_alpha < 255 - ? COGL_ENABLE_BLEND : 0); - - GE( glRectf (x, y, x + width, y + height) ); -} - void _cogl_path_add_node (gboolean new_sub_path, float x, float y) { CoglPathNode new_node; - + _COGL_GET_CONTEXT (ctx, NO_RETVAL); - new_node.x = (x); - new_node.y = (y); + new_node.x = x; + new_node.y = y; new_node.path_size = 0; if (new_sub_path || ctx->path_nodes->len == 0) @@ -89,13 +75,18 @@ _cogl_path_add_node (gboolean new_sub_path, void _cogl_path_stroke_nodes () { - guint path_start = 0; + guint path_start = 0; + gulong enable_flags = COGL_ENABLE_VERTEX_ARRAY; _COGL_GET_CONTEXT (ctx, NO_RETVAL); - - cogl_enable (COGL_ENABLE_VERTEX_ARRAY - | (ctx->color_alpha < 255 - ? COGL_ENABLE_BLEND : 0)); + + enable_flags |= cogl_material_get_cogl_enable_flags (ctx->source_material); + cogl_enable (enable_flags); + + cogl_material_flush_gl_state (ctx->source_material, + COGL_MATERIAL_FLUSH_DISABLE_MASK, + (guint32)~0, /* disable all texture layers */ + NULL); while (path_start < ctx->path_nodes->len) { @@ -106,7 +97,7 @@ _cogl_path_stroke_nodes () (guchar *) path + G_STRUCT_OFFSET (CoglPathNode, x)) ); GE( glDrawArrays (GL_LINE_STRIP, 0, path->path_size) ); - + path_start += path->path_size; } } @@ -132,12 +123,22 @@ _cogl_add_path_to_stencil_buffer (floatVec2 nodes_min, CoglPathNode *path, gboolean merge) { - guint path_start = 0; - guint sub_path_num = 0; - float bounds_x; - float bounds_y; - float bounds_w; - float bounds_h; + guint path_start = 0; + guint sub_path_num = 0; + float bounds_x; + float bounds_y; + float bounds_w; + float bounds_h; + gulong enable_flags = COGL_ENABLE_VERTEX_ARRAY; + + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + /* Just setup a simple material that doesn't use texturing... */ + cogl_material_flush_gl_state (ctx->stencil_material, NULL); + + enable_flags |= + cogl_material_get_cogl_enable_flags (ctx->source_material); + cogl_enable (enable_flags); _cogl_path_get_bounds (nodes_min, nodes_max, &bounds_x, &bounds_y, &bounds_w, &bounds_h); @@ -159,11 +160,9 @@ _cogl_add_path_to_stencil_buffer (floatVec2 nodes_min, GE( glColorMask (FALSE, FALSE, FALSE, FALSE) ); GE( glDepthMask (FALSE) ); - + while (path_start < path_size) { - cogl_enable (COGL_ENABLE_VERTEX_ARRAY); - GE( glVertexPointer (2, GL_FLOAT, sizeof (CoglPathNode), (guchar *) path + G_STRUCT_OFFSET (CoglPathNode, x)) ); @@ -201,17 +200,17 @@ _cogl_add_path_to_stencil_buffer (floatVec2 nodes_min, GE( glMatrixMode (GL_PROJECTION) ); GE( glPushMatrix () ); GE( glLoadIdentity () ); - GE( glRecti (-1, 1, 1, -1) ); - GE( glRecti (-1, 1, 1, -1) ); + cogl_rectangle (-1.0, -1.0, 2, 2); + cogl_rectangle (-1.0, -1.0, 2, 2); GE( glPopMatrix () ); GE( glMatrixMode (GL_MODELVIEW) ); GE( glPopMatrix () ); } - + GE( glStencilMask (~(GLuint) 0) ); GE( glDepthMask (TRUE) ); GE( glColorMask (TRUE, TRUE, TRUE, TRUE) ); - + GE( glStencilFunc (GL_EQUAL, 0x1, 0x1) ); GE( glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP) ); } @@ -226,6 +225,9 @@ _cogl_path_fill_nodes () _COGL_GET_CONTEXT (ctx, NO_RETVAL); + _cogl_path_get_bounds (ctx->path_nodes_min, ctx->path_nodes_max, + &bounds_x, &bounds_y, &bounds_w, &bounds_h); + _cogl_add_path_to_stencil_buffer (ctx->path_nodes_min, ctx->path_nodes_max, ctx->path_nodes->len, @@ -233,9 +235,6 @@ _cogl_path_fill_nodes () CoglPathNode, 0), ctx->clip.stencil_used); - _cogl_path_get_bounds (ctx->path_nodes_min, ctx->path_nodes_max, - &bounds_x, &bounds_y, &bounds_w, &bounds_h); - cogl_rectangle (bounds_x, bounds_y, bounds_w, bounds_h); /* The stencil buffer now contains garbage so the clip area needs to diff --git a/clutter/cogl/gl/cogl-texture-private.h b/clutter/cogl/gl/cogl-texture-private.h index 117437458..59bcad108 100644 --- a/clutter/cogl/gl/cogl-texture-private.h +++ b/clutter/cogl/gl/cogl-texture-private.h @@ -28,9 +28,9 @@ #include "cogl-bitmap.h" -typedef struct _CoglTexture CoglTexture; -typedef struct _CoglTexSliceSpan CoglTexSliceSpan; -typedef struct _CoglSpanIter CoglSpanIter; +typedef struct _CoglTexture CoglTexture; +typedef struct _CoglTexSliceSpan CoglTexSliceSpan; +typedef struct _CoglSpanIter CoglSpanIter; struct _CoglTexSliceSpan { @@ -59,7 +59,23 @@ struct _CoglTexture gboolean auto_mipmap; }; +/* To improve batching of geometry when submitting vertices to OpenGL we + * log the texture rectangles we want to draw to a journal, so when we + * later flush the journal we aim to batch data, and gl draw calls. */ +typedef struct _CoglJournalEntry +{ + CoglHandle material; + gint n_layers; + guint32 fallback_mask; + GLuint layer0_override_texture; +} CoglJournalEntry; + CoglTexture* _cogl_texture_pointer_from_handle (CoglHandle handle); +gboolean +_cogl_texture_span_has_waste (CoglTexture *tex, + gint x_span_index, + gint y_span_index); + #endif /* __COGL_TEXTURE_H */ diff --git a/clutter/cogl/gl/cogl-texture.c b/clutter/cogl/gl/cogl-texture.c index 71236ebe5..e1c73260f 100644 --- a/clutter/cogl/gl/cogl-texture.c +++ b/clutter/cogl/gl/cogl-texture.c @@ -32,6 +32,7 @@ #include "cogl-util.h" #include "cogl-bitmap.h" #include "cogl-texture-private.h" +#include "cogl-material.h" #include "cogl-context.h" #include "cogl-handle.h" @@ -50,6 +51,19 @@ printf("err: 0x%x\n", err); \ } */ +#ifdef HAVE_COGL_GL +#ifdef glDrawRangeElements +#undef glDrawRangeElements +#endif +#define glDrawRangeElements ctx->pf_glDrawRangeElements +#endif + +static void _cogl_journal_flush (void); + +static void _cogl_texture_free (CoglTexture *tex); + +COGL_HANDLE_DEFINE (Texture, texture, texture_handles); + struct _CoglSpanIter { gint index; @@ -67,11 +81,6 @@ struct _CoglSpanIter gboolean intersects; }; -static void _cogl_texture_free (CoglTexture *tex); - -COGL_HANDLE_DEFINE (Texture, texture, texture_handles); - - static void _cogl_texture_bitmap_free (CoglTexture *tex) { @@ -774,6 +783,10 @@ _cogl_texture_set_wrap_mode_parameter (CoglTexture *tex, { int i; + /* Any queued texture rectangles may be depending on the previous + * wrap mode... */ + _cogl_journal_flush (); + for (i = 0; i < tex->slice_gl_handles->len; i++) { GLuint texnum = g_array_index (tex->slice_gl_handles, GLuint, i); @@ -990,6 +1003,20 @@ _cogl_texture_slices_free (CoglTexture *tex) } } +gboolean +_cogl_texture_span_has_waste (CoglTexture *tex, + gint x_span_index, + gint y_span_index) +{ + CoglTexSliceSpan *x_span; + CoglTexSliceSpan *y_span; + + x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, x_span_index); + y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, y_span_index); + + return (x_span->waste || y_span->waste) ? TRUE : FALSE; +} + static gboolean _cogl_pixel_format_from_gl_internal (GLenum gl_int_format, CoglPixelFormat *out_format) @@ -1392,12 +1419,12 @@ cogl_texture_new_from_file (const gchar *filename, { CoglBitmap *bmp; CoglHandle handle; - + g_return_val_if_fail (error == NULL || *error == NULL, COGL_INVALID_HANDLE); if (!(bmp = cogl_bitmap_new_from_file (filename, error))) return COGL_INVALID_HANDLE; - + handle = cogl_texture_new_from_bitmap (bmp, max_waste, flags, @@ -1920,137 +1947,307 @@ cogl_texture_get_data (CoglHandle handle, return byte_size; } + +/****************************************************************************** + * XXX: Here ends the code that strictly implements "CoglTextures". + * + * The following consists of code for rendering rectangles and polygons. It + * might be neater to move this code somewhere else. I think everything below + * here should be implementable without access to CoglTexture internals, but + * that will at least mean exposing the cogl_span_iter_* funcs. + */ + static void -_cogl_texture_flush_vertices (void) +_cogl_journal_flush_quad_batch (CoglJournalEntry *batch_start, + gint batch_len, + GLfloat *vertex_pointer) { + int needed_indices; + gsize stride; + int i; + gulong enable_flags = 0; + guint32 disable_mask; + _COGL_GET_CONTEXT (ctx, NO_RETVAL); - if (ctx->texture_vertices->len > 0) + /* The indices are always the same sequence regardless of the vertices so we + * only need to change it if there are more vertices than ever before. */ + needed_indices = batch_len * 6; + if (needed_indices > ctx->static_indices->len) { - int needed_indices; - CoglTextureGLVertex *p - = (CoglTextureGLVertex *) ctx->texture_vertices->data; + int old_len = ctx->static_indices->len; + int vert_num = old_len / 6 * 4; + GLushort *q; - /* The indices are always the same sequence regardless of the - vertices so we only need to change it if there are more - vertices than ever before */ - needed_indices = ctx->texture_vertices->len / 4 * 6; - if (needed_indices > ctx->texture_indices->len) + /* Add two triangles for each quad to the list of + indices. That makes six new indices but two of the + vertices in the triangles are shared. */ + g_array_set_size (ctx->static_indices, needed_indices); + q = &g_array_index (ctx->static_indices, GLushort, old_len); + + for (i = old_len; + i < ctx->static_indices->len; + i += 6, vert_num += 4) { - int old_len = ctx->texture_indices->len; - int vert_num = old_len / 6 * 4; - int i; - GLushort *q; + *(q++) = vert_num + 0; + *(q++) = vert_num + 1; + *(q++) = vert_num + 3; - /* Add two triangles for each quad to the list of - indices. That makes six new indices but two of the - vertices in the triangles are shared. */ - g_array_set_size (ctx->texture_indices, needed_indices); - q = &g_array_index (ctx->texture_indices, GLushort, old_len); + *(q++) = vert_num + 1; + *(q++) = vert_num + 2; + *(q++) = vert_num + 3; + } + } - for (i = old_len; - i < ctx->texture_indices->len; - i += 6, vert_num += 4) - { - *(q++) = vert_num + 0; - *(q++) = vert_num + 1; - *(q++) = vert_num + 3; + /* XXX NB: + * Our vertex data is arranged as follows: + * 4 vertices per quad: 2 GLfloats per position, + * 2 GLfloats per tex coord * n_layers + */ + stride = 2 + 2 * batch_start->n_layers; + stride *= sizeof (GLfloat); - *(q++) = vert_num + 1; - *(q++) = vert_num + 2; - *(q++) = vert_num + 3; - } + disable_mask = (1 << batch_start->n_layers) - 1; + disable_mask = ~disable_mask; + + cogl_material_flush_gl_state (ctx->source_material, + COGL_MATERIAL_FLUSH_FALLBACK_MASK, + batch_start->fallback_mask, + COGL_MATERIAL_FLUSH_DISABLE_MASK, + disable_mask, + /* Redundant when dealing with unsliced + * textures but does no harm... */ + COGL_MATERIAL_FLUSH_LAYER0_OVERRIDE, + batch_start->layer0_override_texture, + NULL); + + for (i = 0; i < batch_start->n_layers; i++) + { + GE (glClientActiveTexture (GL_TEXTURE0 + i)); + GE (glEnableClientState (GL_TEXTURE_COORD_ARRAY)); + GE (glTexCoordPointer (2, GL_FLOAT, stride, vertex_pointer + 2 + 2 * i)); + } + for (; i < ctx->n_texcoord_arrays_enabled; i++) + { + GE (glClientActiveTexture (GL_TEXTURE0 + i)); + GE (glDisableClientState (GL_TEXTURE_COORD_ARRAY)); + } + ctx->n_texcoord_arrays_enabled = 0; + + /* FIXME: This api is a bit yukky, ideally it will be removed if we + * re-work the cogl_enable mechanism */ + enable_flags |= cogl_material_get_cogl_enable_flags (ctx->source_material); + + if (ctx->enable_backface_culling) + enable_flags |= COGL_ENABLE_BACKFACE_CULLING; + + enable_flags |= COGL_ENABLE_VERTEX_ARRAY; + cogl_enable (enable_flags); + + GE (glVertexPointer (2, GL_FLOAT, stride, vertex_pointer)); + + GE (ctx->pf_glDrawRangeElements (GL_TRIANGLES, + 0, ctx->static_indices->len - 1, + 6 * batch_len, + GL_UNSIGNED_SHORT, + ctx->static_indices->data)); +} + +static void +_cogl_journal_flush (void) +{ + GLfloat *current_vertex_pointer; + GLfloat *batch_vertex_pointer; + CoglJournalEntry *batch_start; + guint batch_len; + int i; + + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + if (ctx->journal->len == 0) + return; + + /* Current non-variables / constraints: + * + * - We don't have to worry about much GL state changing between journal + * entries since currently the journal never out lasts a single call to + * _cogl_multitexture_multiple_rectangles. So the user doesn't get the + * chance to fiddle with anything. (XXX: later this will be extended at + * which point we can start logging certain state changes) + * + * - Implied from above: all entries will refer to the same material. + * + * - Although _cogl_multitexture_multiple_rectangles can cause the wrap mode + * of textures to be modified, the journal is flushed if a wrap mode is + * changed so we don't currently have to log wrap mode changes. + * + * - XXX - others? + */ + + /* TODO: "compile" the journal to find ways of batching draw calls and vertex + * data. + * + * Simple E.g. given current constraints... + * pass 0 - load all data into a single CoglVertexBuffer + * pass 1 - batch gl draw calls according to entries that use the same + * textures. + * + * We will be able to do cooler stuff here when we extend the life of + * journals beyond _cogl_multitexture_multiple_rectangles. + */ + + batch_vertex_pointer = (GLfloat *)ctx->logged_vertices->data; + batch_start = (CoglJournalEntry *)ctx->journal->data; + batch_len = 1; + + current_vertex_pointer = batch_vertex_pointer; + + for (i = 1; i < ctx->journal->len; i++) + { + CoglJournalEntry *prev_entry = + &g_array_index (ctx->journal, CoglJournalEntry, i - 1); + CoglJournalEntry *current_entry = prev_entry + 1; + gsize stride; + + /* Progress the vertex pointer */ + stride = 2 + current_entry->n_layers * 2; + current_vertex_pointer += stride; + + /* batch rectangles using the same textures */ + if (current_entry->material == prev_entry->material && + current_entry->n_layers == prev_entry->n_layers && + current_entry->fallback_mask == prev_entry->fallback_mask && + current_entry->layer0_override_texture + == prev_entry->layer0_override_texture) + { + batch_len++; + continue; } - GE( glVertexPointer (2, GL_FLOAT, - sizeof (CoglTextureGLVertex), p->v ) ); - GE( glTexCoordPointer (2, GL_FLOAT, - sizeof (CoglTextureGLVertex), p->t ) ); + _cogl_journal_flush_quad_batch (batch_start, + batch_len, + batch_vertex_pointer); - GE( glBindTexture (ctx->texture_target, ctx->texture_current) ); - GE( ctx->pf_glDrawRangeElements (GL_TRIANGLES, - 0, ctx->texture_vertices->len - 1, - needed_indices, - GL_UNSIGNED_SHORT, - ctx->texture_indices->data) ); - - g_array_set_size (ctx->texture_vertices, 0); + batch_start = current_entry; + batch_len = 1; + batch_vertex_pointer = current_vertex_pointer; } + + /* The last batch... */ + _cogl_journal_flush_quad_batch (batch_start, + batch_len, + batch_vertex_pointer); + + + g_array_set_size (ctx->journal, 0); + g_array_set_size (ctx->logged_vertices, 0); } static void -_cogl_texture_add_quad_vertices (GLfloat x_1, - GLfloat y_1, - GLfloat x_2, - GLfloat y_2, - GLfloat tx_1, - GLfloat ty_1, - GLfloat tx_2, - GLfloat ty_2) +_cogl_journal_log_quad (float x_1, + float y_1, + float x_2, + float y_2, + CoglHandle material, + gint n_layers, + guint32 fallback_mask, + GLuint layer0_override_texture, + float *tex_coords, + guint tex_coords_len) { - CoglTextureGLVertex *p; - GLushort first_vert; + int stride; + int next_vert; + GLfloat *v; + int i; + int next_entry; + CoglJournalEntry *entry; _COGL_GET_CONTEXT (ctx, NO_RETVAL); - /* Add the four vertices of the quad to the list of queued - vertices */ - first_vert = ctx->texture_vertices->len; - g_array_set_size (ctx->texture_vertices, first_vert + 4); - p = &g_array_index (ctx->texture_vertices, CoglTextureGLVertex, first_vert); + /* The vertex data is logged into a seperate array in a layout that can be + * directly passed to OpenGL + */ - p->v[0] = x_1; p->v[1] = y_1; - p->t[0] = tx_1; p->t[1] = ty_1; - p++; - p->v[0] = x_1; p->v[1] = y_2; - p->t[0] = tx_1; p->t[1] = ty_2; - p++; - p->v[0] = x_2; p->v[1] = y_2; - p->t[0] = tx_2; p->t[1] = ty_2; - p++; - p->v[0] = x_2; p->v[1] = y_1; - p->t[0] = tx_2; p->t[1] = ty_1; - p++; + /* We pack the vertex data as 2 (x,y) GLfloats folowed by 2 (tx,ty) GLfloats + * for each texture being used, E.g.: + * [X, Y, TX0, TY0, TX1, TY1, X, Y, TX0, TY0, X, Y, ...] + */ + stride = 2 + n_layers * 2; + + next_vert = ctx->logged_vertices->len; + g_array_set_size (ctx->logged_vertices, next_vert + 4 * stride); + v = &g_array_index (ctx->logged_vertices, GLfloat, next_vert); + + /* XXX: All the jumping around to fill in this strided buffer doesn't + * seem ideal. */ + + /* XXX: we could defer expanding the vertex data for GL until we come + * to flushing the journal. */ + + v[0] = x_1; v[1] = y_1; + v += stride; + v[0] = x_1; v[1] = y_2; + v += stride; + v[0] = x_2; v[1] = y_2; + v += stride; + v[0] = x_2; v[1] = y_1; + + for (i = 0; i < n_layers; i++) + { + GLfloat *t = + &g_array_index (ctx->logged_vertices, GLfloat, next_vert + 2 + 2 * i); + + t[0] = tex_coords[0]; t[1] = tex_coords[1]; + t += stride; + t[0] = tex_coords[0]; t[1] = tex_coords[3]; + t += stride; + t[0] = tex_coords[2]; t[1] = tex_coords[3]; + t += stride; + t[0] = tex_coords[2]; t[1] = tex_coords[1]; + } + + next_entry = ctx->journal->len; + g_array_set_size (ctx->journal, next_entry + 1); + entry = &g_array_index (ctx->journal, CoglJournalEntry, next_entry); + + entry->material = material; + entry->n_layers = n_layers; + entry->fallback_mask = fallback_mask; + entry->layer0_override_texture = layer0_override_texture; } static void -_cogl_texture_quad_sw (CoglTexture *tex, - float x_1, - float y_1, - float x_2, - float y_2, - float tx_1, - float ty_1, - float tx_2, - float ty_2) +_cogl_texture_sliced_quad (CoglTexture *tex, + CoglHandle material, + float x_1, + float y_1, + float x_2, + float y_2, + float tx_1, + float ty_1, + float tx_2, + float ty_2) { - CoglSpanIter iter_x , iter_y; - float tw , th; - float tqx , tqy; - float first_tx , first_ty; - float first_qx , first_qy; - float slice_tx1 , slice_ty1; - float slice_tx2 , slice_ty2; - float slice_qx1 , slice_qy1; - float slice_qx2 , slice_qy2; - GLuint gl_handle; + CoglSpanIter iter_x , iter_y; + float tw , th; + float tqx , tqy; + float first_tx , first_ty; + float first_qx , first_qy; + float slice_tx1 , slice_ty1; + float slice_tx2 , slice_ty2; + float slice_qx1 , slice_qy1; + float slice_qx2 , slice_qy2; + GLuint gl_handle; _COGL_GET_CONTEXT (ctx, NO_RETVAL); #if COGL_DEBUG - printf("=== Drawing Tex Quad (Software Tiling Mode) ===\n"); + printf("=== Drawing Tex Quad (Sliced Mode) ===\n"); #endif /* We can't use hardware repeat so we need to set clamp to edge otherwise it might pull in edge pixels from the other side */ - if (ctx->texture_vertices->len > 0 - && ctx->texture_wrap_mode != GL_CLAMP_TO_EDGE) - { - _cogl_texture_flush_vertices (); - } - _cogl_texture_set_wrap_mode_parameter (tex, GL_CLAMP_TO_EDGE); - ctx->texture_wrap_mode = GL_CLAMP_TO_EDGE; /* If the texture coordinates are backwards then swap both the geometry and texture coordinates so that the texture will be @@ -2059,18 +2256,15 @@ _cogl_texture_quad_sw (CoglTexture *tex, if (tx_2 < tx_1) { float temp = x_1; - x_1 = x_2; x_2 = temp; temp = tx_1; tx_1 = tx_2; tx_2 = temp; } - if (ty_2 < ty_1) { float temp = y_1; - y_1 = y_2; y_2 = temp; temp = ty_1; @@ -2079,8 +2273,8 @@ _cogl_texture_quad_sw (CoglTexture *tex, } /* Scale ratio from texture to quad widths */ - tw = (float) (tex->bitmap.width); - th = (float) (tex->bitmap.height); + tw = (float)(tex->bitmap.width); + th = (float)(tex->bitmap.height); tqx = (x_2 - x_1) / (tw * (tx_2 - tx_1)); tqy = (y_2 - y_1) / (th * (ty_2 - ty_1)); @@ -2108,6 +2302,8 @@ _cogl_texture_quad_sw (CoglTexture *tex, !_cogl_span_iter_end (&iter_y) ; _cogl_span_iter_next (&iter_y) ) { + float tex_coords[4]; + /* Discard slices out of quad early */ if (!iter_y.intersects) continue; @@ -2171,186 +2367,358 @@ _cogl_texture_quad_sw (CoglTexture *tex, iter_y.index * iter_x.array->len + iter_x.index); - /* If we're using a different texture from the one already queued - then flush the vertices */ - if (ctx->texture_vertices->len > 0 - && gl_handle != ctx->texture_current) - { - _cogl_texture_flush_vertices (); - } - - ctx->texture_target = tex->gl_target; - ctx->texture_current = gl_handle; - - _cogl_texture_add_quad_vertices ( (slice_qx1), - (slice_qy1), - (slice_qx2), - (slice_qy2), - (slice_tx1), - (slice_ty1), - (slice_tx2), - (slice_ty2)); + tex_coords[0] = slice_tx1; + tex_coords[1] = slice_ty1; + tex_coords[2] = slice_tx2; + tex_coords[3] = slice_ty2; + _cogl_journal_log_quad (slice_qx1, + slice_qy1, + slice_qx2, + slice_qy2, + material, + 1, /* one layer */ + 0, /* don't need to use fallbacks */ + gl_handle, /* replace the layer0 texture */ + tex_coords, + 4); } } } -static void -_cogl_texture_quad_hw (CoglTexture *tex, - float x_1, - float y_1, - float x_2, - float y_2, - float tx_1, - float ty_1, - float tx_2, - float ty_2) +static gboolean +_cogl_multitexture_unsliced_quad (float x_1, + float y_1, + float x_2, + float y_2, + CoglHandle material, + gint n_layers, + guint32 fallback_mask, + const float *user_tex_coords, + gint user_tex_coords_len) { - GLuint gl_handle; - CoglTexSliceSpan *x_span; - CoglTexSliceSpan *y_span; - GLenum wrap_mode; + float *final_tex_coords = alloca (sizeof (float) * 4 * n_layers); + const GList *layers; + GList *tmp; + int i; -#if COGL_DEBUG - printf("=== Drawing Tex Quad (Hardware Tiling Mode) ===\n"); -#endif + _COGL_GET_CONTEXT (ctx, FALSE); - _COGL_GET_CONTEXT (ctx, NO_RETVAL); - - /* If the texture coords are all in the range [0,1] then we want to - clamp the coords to the edge otherwise it can pull in edge pixels - from the wrong side when scaled */ - if (tx_1 >= 0 && tx_1 <= 1.0 && - tx_2 >= 0 && tx_2 <= 1.0 && - ty_1 >= 0 && ty_1 <= 1.0 && - ty_2 >= 0 && ty_2 <= 1.0) + /* + * Validate the texture coordinates for this rectangle. + */ + layers = cogl_material_get_layers (material); + for (tmp = (GList *)layers, i = 0; tmp != NULL; tmp = tmp->next, i++) { - wrap_mode = GL_CLAMP_TO_EDGE; - } - else - wrap_mode = GL_REPEAT; + CoglHandle layer = (CoglHandle)tmp->data; + /* CoglLayerInfo *layer_info; */ + CoglHandle tex_handle; + CoglTexture *tex; + const float *in_tex_coords; + float *out_tex_coords; + CoglTexSliceSpan *x_span; + CoglTexSliceSpan *y_span; - /* Pick and bind opengl texture object */ - gl_handle = g_array_index (tex->slice_gl_handles, GLuint, 0); + /* layer_info = &layers[i]; */ - /* If we're using a different texture from the one already queued - then flush the vertices */ - if (ctx->texture_vertices->len > 0 && - (gl_handle != ctx->texture_current || - ctx->texture_wrap_mode != wrap_mode)) - { - _cogl_texture_flush_vertices (); + /* FIXME - we shouldn't be checking this stuff if layer_info->gl_texture + * already == 0 */ + + tex_handle = cogl_material_layer_get_texture (layer); + tex = _cogl_texture_pointer_from_handle (tex_handle); + + in_tex_coords = &user_tex_coords[i * 4]; + out_tex_coords = &final_tex_coords[i * 4]; + + + /* If the texture has waste or we are using GL_TEXTURE_RECT we + * can't handle texture repeating so we check that the texture + * coords lie in the range [0,1]. + * + * NB: We already know that no texture matrix is being used + * if the texture has waste since we validated that early on. + * TODO: check for a texture matrix in the GL_TEXTURE_RECT + * case. + */ + if ((tex->gl_target == GL_TEXTURE_RECTANGLE_ARB + || _cogl_texture_span_has_waste (tex, 0, 0)) + && (in_tex_coords[0] < 0 || in_tex_coords[0] > 1.0 + || in_tex_coords[1] < 0 || in_tex_coords[1] > 1.0 + || in_tex_coords[2] < 0 || in_tex_coords[2] > 1.0 + || in_tex_coords[3] < 0 || in_tex_coords[3] > 1.0)) + { + if (i == 0) + { + if (n_layers > 1) + { + g_warning ("Skipping layers 1..n of your material since the " + "first layer has waste and you supplied texture " + "coordinates outside the range [0,1]. We don't " + "currently support any multi-texturing using " + "textures with waste when repeating is " + "necissary so we are falling back to sliced " + "textures assuming layer 0 is the most " + "important one keep"); + } + return FALSE; + } + else + { + g_warning ("Skipping layer %d of your material " + "consisting of a texture with waste since " + "you have supplied texture coords outside " + "the range [0,1] (unsupported when " + "multi-texturing)", i); + + /* NB: marking for fallback will replace the layer with + * a default transparent texture */ + fallback_mask |= (1 << i); + } + } + + + /* + * Setup the texture unit... + */ + + /* NB: The user might not have supplied texture coordinates for all + * layers... */ + if (i < (user_tex_coords_len / 4)) + { + GLenum wrap_mode; + + /* If the texture coords are all in the range [0,1] then we want to + clamp the coords to the edge otherwise it can pull in edge pixels + from the wrong side when scaled */ + if (in_tex_coords[0] >= 0 && in_tex_coords[0] <= 1.0 + && in_tex_coords[1] >= 0 && in_tex_coords[1] <= 1.0 + && in_tex_coords[2] >= 0 && in_tex_coords[2] <= 1.0 + && in_tex_coords[3] >= 0 && in_tex_coords[3] <= 1.0) + wrap_mode = GL_CLAMP_TO_EDGE; + else + wrap_mode = GL_REPEAT; + + _cogl_texture_set_wrap_mode_parameter (tex, wrap_mode); + } + else + { + out_tex_coords[0] = 0; /* tx_1 */ + out_tex_coords[1] = 0; /* ty_1 */ + out_tex_coords[2] = 1.0; /* tx_2 */ + out_tex_coords[3] = 1.0; /* ty_2 */ + + _cogl_texture_set_wrap_mode_parameter (tex, GL_CLAMP_TO_EDGE); + } + + /* Don't include the waste in the texture coordinates */ + x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, 0); + y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, 0); + + out_tex_coords[0] = + in_tex_coords[0] * (x_span->size - x_span->waste) / x_span->size; + out_tex_coords[1] = + in_tex_coords[1] * (x_span->size - x_span->waste) / x_span->size; + out_tex_coords[2] = + in_tex_coords[2] * (y_span->size - y_span->waste) / y_span->size; + out_tex_coords[3] = + in_tex_coords[3] * (y_span->size - y_span->waste) / y_span->size; + + /* Denormalize texture coordinates for rectangle textures */ + if (tex->gl_target == GL_TEXTURE_RECTANGLE_ARB) + { + out_tex_coords[0] *= x_span->size; + out_tex_coords[1] *= x_span->size; + out_tex_coords[2] *= y_span->size; + out_tex_coords[3] *= y_span->size; + } } - ctx->texture_target = tex->gl_target; - ctx->texture_current = gl_handle; - ctx->texture_wrap_mode = wrap_mode; + _cogl_journal_log_quad (x_1, + y_1, + x_2, + y_2, + material, + n_layers, + fallback_mask, + 0, /* don't replace the layer0 texture */ + final_tex_coords, + n_layers * 4); - _cogl_texture_set_wrap_mode_parameter (tex, wrap_mode); - - /* Don't include the waste in the texture coordinates */ - x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, 0); - y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, 0); - - /* Don't include the waste in the texture coordinates */ - tx_1 = tx_1 * (x_span->size - x_span->waste) / x_span->size; - tx_2 = tx_2 * (x_span->size - x_span->waste) / x_span->size; - ty_1 = ty_1 * (y_span->size - y_span->waste) / y_span->size; - ty_2 = ty_2 * (y_span->size - y_span->waste) / y_span->size; - - /* Denormalize texture coordinates for rectangle textures */ - if (tex->gl_target == GL_TEXTURE_RECTANGLE_ARB) - { - tx_1 *= x_span->size; - tx_2 *= x_span->size; - ty_1 *= y_span->size; - ty_2 *= y_span->size; - } - - _cogl_texture_add_quad_vertices (x_1, y_1, - x_2, y_2, - tx_1, ty_1, - tx_2, ty_2); + return TRUE; } -void -cogl_texture_multiple_rectangles (CoglHandle handle, - const float *verts, - guint n_rects) +struct _CoglMutiTexturedRect { - CoglTexture *tex; - gulong enable_flags = (COGL_ENABLE_VERTEX_ARRAY | - COGL_ENABLE_TEXCOORD_ARRAY); + float x_1; + float y_1; + float x_2; + float y_2; + const float *tex_coords; + gint tex_coords_len; +}; + +static void +_cogl_rectangles_with_multitexture_coords ( + struct _CoglMutiTexturedRect *rects, + gint n_rects) +{ + CoglHandle material; + const GList *layers; + int n_layers; + const GList *tmp; + guint32 fallback_mask = 0; + gboolean all_use_sliced_quad_fallback = FALSE; + int i; _COGL_GET_CONTEXT (ctx, NO_RETVAL); - /* Check if valid texture */ - if (!cogl_is_texture (handle)) - return; - cogl_clip_ensure (); - tex = _cogl_texture_pointer_from_handle (handle); + material = ctx->source_material; - /* Make sure we got stuff to draw */ - if (tex->slice_gl_handles == NULL) - return; + layers = cogl_material_get_layers (material); + n_layers = g_list_length ((GList *)layers); - if (tex->slice_gl_handles->len == 0) - return; + /* + * Validate all the layers of the current source material... + */ - /* Prepare GL state */ - if (tex->gl_target == CGL_TEXTURE_RECTANGLE_ARB) - enable_flags |= COGL_ENABLE_TEXTURE_RECT; - else - enable_flags |= COGL_ENABLE_TEXTURE_2D; - - if (ctx->color_alpha < 255 || tex->bitmap.format & COGL_A_BIT) - enable_flags |= COGL_ENABLE_BLEND; - - if (ctx->enable_backface_culling) - enable_flags |= COGL_ENABLE_BACKFACE_CULLING; - - cogl_enable (enable_flags); - - g_array_set_size (ctx->texture_vertices, 0); - - while (n_rects-- > 0) + for (tmp = layers, i = 0; tmp != NULL; tmp = tmp->next, i++) { - if (verts[4] != verts[6] && verts[5] != verts[7]) - { - /* If there is only one GL texture and either the texture is - NPOT (no waste) or all of the coordinates are in the - range [0,1] then we can use hardware tiling */ - if (tex->slice_gl_handles->len == 1 - && ((cogl_features_available (COGL_FEATURE_TEXTURE_NPOT) - && tex->gl_target == GL_TEXTURE_2D) - || (verts[4] >= 0 && verts[4] <= 1.0 - && verts[6] >= 0 && verts[6] <= 1.0 - && verts[5] >= 0 && verts[5] <= 1.0 - && verts[7] >= 0 && verts[7] <= 1.0))) - _cogl_texture_quad_hw (tex, verts[0],verts[1], verts[2],verts[3], - verts[4],verts[5], verts[6],verts[7]); - else - _cogl_texture_quad_sw (tex, verts[0],verts[1], verts[2],verts[3], - verts[4],verts[5], verts[6],verts[7]); - } + CoglHandle layer = tmp->data; + CoglHandle tex_handle = cogl_material_layer_get_texture (layer); + CoglTexture *texture = _cogl_texture_pointer_from_handle (tex_handle); + gulong flags; - verts += 8; + if (cogl_material_layer_get_type (layer) + != COGL_MATERIAL_LAYER_TYPE_TEXTURE) + continue; + + /* XXX: + * For now, if the first layer is sliced then all other layers are + * ignored since we currently don't support multi-texturing with + * sliced textures. If the first layer is not sliced then any other + * layers found to be sliced will be skipped. (with a warning) + * + * TODO: Add support for multi-texturing rectangles with sliced + * textures if no texture matrices are in use. + */ + if (cogl_texture_is_sliced (tex_handle)) + { + if (i == 0) + { + fallback_mask = ~1; /* fallback all except the first layer */ + all_use_sliced_quad_fallback = TRUE; + if (tmp->next) + { + g_warning ("Skipping layers 1..n of your material since the " + "first layer is sliced. We don't currently " + "support any multi-texturing with sliced " + "textures but assume layer 0 is the most " + "important to keep"); + } + break; + } + else + { + g_warning ("Skipping layer %d of your material consisting of a " + "sliced texture (unsuported for multi texturing)", + i); + + /* NB: marking for fallback will replace the layer with + * a default transparent texture */ + fallback_mask |= (1 << i); + continue; + } + } + + /* We don't support multi texturing using textures with any waste if the + * user has supplied a custom texture matrix, since we don't know if + * the result will end up trying to texture from the waste area. */ + flags = cogl_material_layer_get_flags (layer); + if (flags & COGL_MATERIAL_LAYER_FLAG_HAS_USER_MATRIX + && _cogl_texture_span_has_waste (texture, 0, 0)) + { + g_warning ("Skipping layer %d of your material consisting of a " + "texture with waste since you have supplied a custom " + "texture matrix and the result may try to sample from " + "the waste area of your texture.", i); + + /* NB: marking for fallback will replace the layer with + * a default transparent texture */ + fallback_mask |= (1 << i); + continue; + } } - _cogl_texture_flush_vertices (); + /* + * Emit geometry for each of the rectangles... + */ + + for (i = 0; i < n_rects; i++) + { + if (all_use_sliced_quad_fallback + || !_cogl_multitexture_unsliced_quad (rects[i].x_1, rects[i].y_1, + rects[i].x_2, rects[i].y_2, + material, + n_layers, + fallback_mask, + rects[i].tex_coords, + rects[i].tex_coords_len)) + { + const GList *layers; + CoglHandle tex_handle; + CoglTexture *texture; + + layers = cogl_material_get_layers (material); + tex_handle = + cogl_material_layer_get_texture ((CoglHandle)layers->data); + texture = _cogl_texture_pointer_from_handle (tex_handle); + _cogl_texture_sliced_quad (texture, + material, + rects[i].x_1, rects[i].y_1, + rects[i].x_2, rects[i].y_2, + rects[i].tex_coords[0], + rects[i].tex_coords[1], + rects[i].tex_coords[2], + rects[i].tex_coords[3]); + } + } + + _cogl_journal_flush (); } void -cogl_texture_rectangle (CoglHandle handle, - float x_1, - float y_1, - float x_2, - float y_2, - float tx_1, - float ty_1, - float tx_2, - float ty_2) +cogl_rectangles_with_texture_coords (const float *verts, + guint n_rects) +{ + struct _CoglMutiTexturedRect rects[n_rects]; + int i; + + for (i = 0; i < n_rects; i++) + { + rects[i].x_1 = verts[i * 8]; + rects[i].y_1 = verts[i * 8 + 1]; + rects[i].x_2 = verts[i * 8 + 2]; + rects[i].y_2 = verts[i * 8 + 3]; + /* FIXME: rect should be defined to have a const float *geom; + * instead, to avoid this copy + * rect[i].geom = &verts[n_rects * 8]; */ + rects[i].tex_coords = &verts[i * 8 + 4]; + rects[i].tex_coords_len = 4; + } + + _cogl_rectangles_with_multitexture_coords (rects, n_rects); +} + +void +cogl_rectangle_with_texture_coords (float x_1, + float y_1, + float x_2, + float y_2, + float tx_1, + float ty_1, + float tx_2, + float ty_2) { float verts[8]; @@ -2363,92 +2731,76 @@ cogl_texture_rectangle (CoglHandle handle, verts[6] = tx_2; verts[7] = ty_2; - cogl_texture_multiple_rectangles (handle, verts, 1); + cogl_rectangles_with_texture_coords (verts, 1); } void -cogl_texture_polygon (CoglHandle handle, - guint n_vertices, - CoglTextureVertex *vertices, - gboolean use_color) +cogl_rectangle_with_multitexture_coords (float x_1, + float y_1, + float x_2, + float y_2, + const float *user_tex_coords, + gint user_tex_coords_len) { - CoglTexture *tex; - int i, x, y, tex_num; - GLuint gl_handle; - CoglTexSliceSpan *y_span, *x_span; - gulong enable_flags; - CoglTextureGLVertex *p; + struct _CoglMutiTexturedRect rect; + + rect.x_1 = x_1; + rect.y_1 = y_1; + rect.x_2 = x_2; + rect.y_2 = y_2; + rect.tex_coords = user_tex_coords; + rect.tex_coords_len = user_tex_coords_len; + + _cogl_rectangles_with_multitexture_coords (&rect, 1); +} + +static void +_cogl_texture_sliced_polygon (CoglTextureVertex *vertices, + guint n_vertices, + guint stride, + gboolean use_color) +{ + const GList *layers; + CoglHandle layer0; + CoglHandle tex_handle; + CoglTexture *tex; + CoglTexSliceSpan *y_span, *x_span; + int x, y, tex_num, i; + GLuint gl_handle; + GLfloat *v; _COGL_GET_CONTEXT (ctx, NO_RETVAL); - /* Check if valid texture */ - if (!cogl_is_texture (handle)) - return; + /* We can assume in this case that we have at least one layer in the + * material that corresponds to a sliced cogl texture */ + layers = cogl_material_get_layers (ctx->source_material); + layer0 = (CoglHandle)layers->data; + tex_handle = cogl_material_layer_get_texture (layer0); + tex = _cogl_texture_pointer_from_handle (tex_handle); - cogl_clip_ensure (); - - tex = _cogl_texture_pointer_from_handle (handle); - - /* The polygon will have artifacts where the slices join if the wrap - mode is GL_LINEAR because the filtering will pull in pixels from - the transparent border. To make it clear that the function - shouldn't be used in these circumstances we just bail out and - draw nothing */ - if (tex->slice_gl_handles->len != 1 - && (tex->min_filter != GL_NEAREST || tex->mag_filter != GL_NEAREST)) + v = (GLfloat *)ctx->logged_vertices->data; + for (i = 0; i < n_vertices; i++) { - static gboolean shown_warning = FALSE; + GLfloat *c; - if (!shown_warning) - { - g_warning ("cogl_texture_polygon does not work for sliced textures " - "when the minification and magnification filters are not " - "CGL_NEAREST"); - shown_warning = TRUE; - } - return; + v[0] = vertices[i].x; + v[1] = vertices[i].y; + v[2] = vertices[i].z; + + /* NB: [X,Y,Z,TX,TY,R,G,B,A,...] */ + c = v + 5; + c[0] = cogl_color_get_red_byte (&vertices[i].color); + c[1] = cogl_color_get_green_byte (&vertices[i].color); + c[2] = cogl_color_get_blue_byte (&vertices[i].color); + c[3] = cogl_color_get_alpha_byte (&vertices[i].color); + + v += stride; } - /* Make sure there is enough space in the global texture vertex - array. This is used so we can render the polygon with a single - call to OpenGL but still support any number of vertices */ - g_array_set_size (ctx->texture_vertices, n_vertices); - p = (CoglTextureGLVertex *) ctx->texture_vertices->data; - - /* Prepare GL state */ - enable_flags = (COGL_ENABLE_VERTEX_ARRAY - | COGL_ENABLE_TEXCOORD_ARRAY - | COGL_ENABLE_BLEND); - - if (tex->gl_target == CGL_TEXTURE_RECTANGLE_ARB) - enable_flags |= COGL_ENABLE_TEXTURE_RECT; - else - enable_flags |= COGL_ENABLE_TEXTURE_2D; - - if (ctx->enable_backface_culling) - enable_flags |= COGL_ENABLE_BACKFACE_CULLING; - - if (use_color) - { - enable_flags |= COGL_ENABLE_COLOR_ARRAY; - GE( glColorPointer (4, GL_UNSIGNED_BYTE, - sizeof (CoglTextureGLVertex), p->c) ); - } - - GE( glVertexPointer (3, GL_FLOAT, sizeof (CoglTextureGLVertex), p->v ) ); - GE( glTexCoordPointer (2, GL_FLOAT, sizeof (CoglTextureGLVertex), p->t ) ); - - cogl_enable (enable_flags); - - /* Temporarily change the wrapping mode on all of the slices to use - a transparent border */ - _cogl_texture_set_wrap_mode_parameter (tex, GL_CLAMP_TO_BORDER); - - tex_num = 0; - /* Render all of the slices with the full geometry but use a transparent border color so that any part of the texture not covered by the slice will be ignored */ + tex_num = 0; for (y = 0; y < tex->slice_y_spans->len; y++) { y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, y); @@ -2459,15 +2811,13 @@ cogl_texture_polygon (CoglHandle handle, gl_handle = g_array_index (tex->slice_gl_handles, GLuint, tex_num++); - p = (CoglTextureGLVertex *) ctx->texture_vertices->data; - /* Convert the vertices into an array of GLfloats ready to pass to OpenGL */ - for (i = 0; i < n_vertices; i++, p++) + v = (GLfloat *)ctx->logged_vertices->data; + for (i = 0; i < n_vertices; i++) { - float tx, ty; - -#define CFX_F + GLfloat *t; + float tx, ty; tx = ((vertices[i].tx - ((float)(x_span->start) @@ -2485,22 +2835,258 @@ cogl_texture_polygon (CoglHandle handle, ty *= y_span->size; } - p->v[0] = CFX_F(vertices[i].x); - p->v[1] = CFX_F(vertices[i].y); - p->v[2] = CFX_F(vertices[i].z); - p->t[0] = CFX_F(tx); - p->t[1] = CFX_F(ty); - p->c[0] = cogl_color_get_red_byte(&vertices[i].color); - p->c[1] = cogl_color_get_green_byte(&vertices[i].color); - p->c[2] = cogl_color_get_blue_byte(&vertices[i].color); - p->c[3] = cogl_color_get_alpha_byte(&vertices[i].color); + /* NB: [X,Y,Z,TX,TY,R,G,B,A,...] */ + t = v + 3; + t[0] = tx; + t[1] = ty; -#undef CFX_F + v += stride; } - GE( glBindTexture (tex->gl_target, gl_handle) ); + cogl_material_flush_gl_state (ctx->source_material, + COGL_MATERIAL_FLUSH_DISABLE_MASK, + (guint32)~1, /* disable all except the + first layer */ + COGL_MATERIAL_FLUSH_LAYER0_OVERRIDE, + gl_handle, + NULL); GE( glDrawArrays (GL_TRIANGLE_FAN, 0, n_vertices) ); } } } + + +static void +_cogl_multitexture_unsliced_polygon (CoglTextureVertex *vertices, + guint n_vertices, + guint n_layers, + guint stride, + gboolean use_color, + guint32 fallback_mask) +{ + CoglHandle material; + const GList *layers; + int i; + GList *tmp; + CoglTexSliceSpan *y_span, *x_span; + GLfloat *v; + + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + + material = ctx->source_material; + layers = cogl_material_get_layers (material); + + /* Convert the vertices into an array of GLfloats ready to pass to + OpenGL */ + for (v = (GLfloat *)ctx->logged_vertices->data, i = 0; + i < n_vertices; + v += stride, i++) + { + GLfloat *c; + int j; + + /* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */ + v[0] = vertices[i].x; + v[1] = vertices[i].y; + v[2] = vertices[i].z; + + for (tmp = (GList *)layers, j = 0; tmp != NULL; tmp = tmp->next, j++) + { + CoglHandle layer = (CoglHandle)tmp->data; + CoglHandle tex_handle; + CoglTexture *tex; + GLfloat *t; + float tx, ty; + + tex_handle = cogl_material_layer_get_texture (layer); + tex = _cogl_texture_pointer_from_handle (tex_handle); + + y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, 0); + x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, 0); + + tx = ((vertices[i].tx + - ((float)(x_span->start) + / tex->bitmap.width)) + * tex->bitmap.width / x_span->size); + ty = ((vertices[i].ty + - ((float)(y_span->start) + / tex->bitmap.height)) + * tex->bitmap.height / y_span->size); + + /* Scale the coordinates up for rectangle textures */ + if (tex->gl_target == CGL_TEXTURE_RECTANGLE_ARB) + { + tx *= x_span->size; + ty *= y_span->size; + } + + /* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */ + t = v + 3 + 2 * j; + t[0] = tx; + t[1] = ty; + } + + /* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */ + c = v + 3 + 2 * n_layers; + c[0] = cogl_color_get_red_float (&vertices[i].color); + c[1] = cogl_color_get_green_float (&vertices[i].color); + c[2] = cogl_color_get_blue_float (&vertices[i].color); + c[3] = cogl_color_get_alpha_float (&vertices[i].color); + } + + cogl_material_flush_gl_state (ctx->source_material, + COGL_MATERIAL_FLUSH_FALLBACK_MASK, + fallback_mask, + NULL); + + GE (glDrawArrays (GL_TRIANGLE_FAN, 0, n_vertices)); +} + +void +cogl_polygon (CoglTextureVertex *vertices, + guint n_vertices, + gboolean use_color) +{ + CoglHandle material; + const GList *layers; + int n_layers; + GList *tmp; + gboolean use_sliced_polygon_fallback = FALSE; + guint32 fallback_mask = 0; + int i; + gulong enable_flags; + guint stride; + gsize stride_bytes; + GLfloat *v; + + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + cogl_clip_ensure (); + + material = ctx->source_material; + layers = cogl_material_get_layers (ctx->source_material); + n_layers = g_list_length ((GList *)layers); + + for (tmp = (GList *)layers, i = 0; tmp != NULL; tmp = tmp->next, i++) + { + CoglHandle layer = (CoglHandle)tmp->data; + CoglHandle tex_handle = cogl_material_layer_get_texture (layer); + CoglTexture *tex = _cogl_texture_pointer_from_handle (tex_handle); + + if (i == 0 && cogl_texture_is_sliced (tex_handle)) + { +#if defined (HAVE_COGL_GLES) || defined (HAVE_COGL_GLES2) + { + static gboolean shown_gles_slicing_warning = FALSE; + if (!shown_gles_slicing_warning) + g_warning ("cogl_polygon does not work for sliced textures " + "on GL ES"); + shown_gles_slicing_warning = TRUE; + return; + } +#endif + if (n_layers > 1) + { + static gboolean shown_slicing_warning = FALSE; + if (!shown_slicing_warning) + { + g_warning ("Disabling layers 1..n since multi-texturing with " + "cogl_polygon isn't supported when using sliced " + "textures\n"); + shown_slicing_warning = TRUE; + } + } + use_sliced_polygon_fallback = TRUE; + n_layers = 1; + + if (tex->min_filter != GL_NEAREST || tex->mag_filter != GL_NEAREST) + { + static gboolean shown_filter_warning = FALSE; + if (!shown_filter_warning) + { + g_warning ("cogl_texture_polygon does not work for sliced textures " + "when the minification and magnification filters are not " + "CGL_NEAREST"); + shown_filter_warning = TRUE; + } + return; + } + + /* Temporarily change the wrapping mode on all of the slices to use + * a transparent border + * XXX: it's doesn't look like we save/restore this, like the comment + * implies? */ + _cogl_texture_set_wrap_mode_parameter (tex, GL_CLAMP_TO_BORDER); + + break; + } + + if (cogl_texture_is_sliced (tex_handle)) + { + g_warning ("Disabling layer %d of the current source material, " + "because texturing with the vertex buffer API is not " + "currently supported using sliced textures, or textures " + "with waste\n", i); + + fallback_mask |= (1 << i); + continue; + } + } + + /* Our data is arranged like: + * [X, Y, Z, TX0, TY0, TX1, TY1..., R, G, B, A,...] */ + stride = 3 + (2 * n_layers) + (use_color ? 4 : 0); + stride_bytes = stride * sizeof (GLfloat); + + /* Make sure there is enough space in the global vertex + array. This is used so we can render the polygon with a single + call to OpenGL but still support any number of vertices */ + g_array_set_size (ctx->logged_vertices, n_vertices * stride); + v = (GLfloat *)ctx->logged_vertices->data; + + /* Prepare GL state */ + enable_flags = COGL_ENABLE_VERTEX_ARRAY; + enable_flags |= cogl_material_get_cogl_enable_flags (ctx->source_material); + + if (ctx->enable_backface_culling) + enable_flags |= COGL_ENABLE_BACKFACE_CULLING; + + if (use_color) + { + enable_flags |= COGL_ENABLE_COLOR_ARRAY; + GE( glColorPointer (4, GL_FLOAT, + stride_bytes, + /* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */ + v + 3 + 2 * n_layers) ); + } + + cogl_enable (enable_flags); + + GE (glVertexPointer (3, GL_FLOAT, stride_bytes, v)); + + for (i = 0; i < n_layers; i++) + { + GE (glClientActiveTexture (GL_TEXTURE0 + i)); + GE (glEnableClientState (GL_TEXTURE_COORD_ARRAY)); + GE (glTexCoordPointer (2, GL_FLOAT, + stride_bytes, + /* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */ + v + 3 + 2 * i)); + } + + if (use_sliced_polygon_fallback) + _cogl_texture_sliced_polygon (vertices, + n_vertices, + stride, + use_color); + else + _cogl_multitexture_unsliced_polygon (vertices, + n_vertices, + n_layers, + stride, + use_color, + fallback_mask); +} + diff --git a/clutter/cogl/gl/cogl.c b/clutter/cogl/gl/cogl.c index 78b2a908c..e82bd9fea 100644 --- a/clutter/cogl/gl/cogl.c +++ b/clutter/cogl/gl/cogl.c @@ -171,6 +171,10 @@ cogl_check_extension (const gchar *name, const gchar *ext) void cogl_paint_init (const CoglColor *color) { +#if COGL_DEBUG + fprintf(stderr, "\n ============== Paint Start ================ \n"); +#endif + GE( glClearColor (cogl_color_get_red_float (color), cogl_color_get_green_float (color), cogl_color_get_blue_float (color), @@ -180,16 +184,16 @@ cogl_paint_init (const CoglColor *color) glDisable (GL_LIGHTING); glDisable (GL_FOG); - /* + /* * Disable the depth test for now as has some strange side effects, - * mainly on x/y axis rotation with multiple layers at same depth - * (eg rotating text on a bg has very strange effect). Seems no clean - * 100% effective way to fix without other odd issues.. So for now + * mainly on x/y axis rotation with multiple layers at same depth + * (eg rotating text on a bg has very strange effect). Seems no clean + * 100% effective way to fix without other odd issues.. So for now * move to application to handle and add cogl_enable_depth_test() * as for custom actors (i.e groups) to enable if need be. * - * glEnable (GL_DEPTH_TEST); - * glEnable (GL_ALPHA_TEST) + * glEnable (GL_DEPTH_TEST); + * glEnable (GL_ALPHA_TEST) * glDepthFunc (GL_LEQUAL); * glAlphaFunc (GL_GREATER, 0.1); */ @@ -199,33 +203,31 @@ cogl_paint_init (const CoglColor *color) void cogl_push_matrix (void) { - glPushMatrix(); + GE( glPushMatrix() ); } void cogl_pop_matrix (void) { - glPopMatrix(); + GE( glPopMatrix() ); } void cogl_scale (float x, float y) { - glScalef ((float)(x), - (float)(y), - 1.0); + GE( glScalef (x, y, 1.0) ); } void cogl_translate (float x, float y, float z) { - glTranslatef (x, y, z); + GE( glTranslatef (x, y, z) ); } void cogl_rotate (float angle, float x, float y, float z) { - glRotatef (angle, x, y, z); + GE( glRotatef (angle, x, y, z) ); } static inline gboolean @@ -251,7 +253,7 @@ cogl_toggle_flag (CoglContext *ctx, GE( glDisable (gl_flag) ); ctx->enable_flags &= ~flag; } - + return FALSE; } @@ -278,7 +280,7 @@ cogl_toggle_client_flag (CoglContext *ctx, GE( glDisableClientState (gl_flag) ); ctx->enable_flags &= ~flag; } - + return FALSE; } @@ -289,33 +291,19 @@ cogl_enable (gulong flags) * hope of lessening number GL traffic. */ _COGL_GET_CONTEXT (ctx, NO_RETVAL); - + cogl_toggle_flag (ctx, flags, COGL_ENABLE_BLEND, GL_BLEND); - - cogl_toggle_flag (ctx, flags, - COGL_ENABLE_TEXTURE_2D, - GL_TEXTURE_2D); cogl_toggle_flag (ctx, flags, COGL_ENABLE_BACKFACE_CULLING, GL_CULL_FACE); -#ifdef GL_TEXTURE_RECTANGLE_ARB - cogl_toggle_flag (ctx, flags, - COGL_ENABLE_TEXTURE_RECT, - GL_TEXTURE_RECTANGLE_ARB); -#endif - cogl_toggle_client_flag (ctx, flags, COGL_ENABLE_VERTEX_ARRAY, GL_VERTEX_ARRAY); - - cogl_toggle_client_flag (ctx, flags, - COGL_ENABLE_TEXCOORD_ARRAY, - GL_TEXTURE_COORD_ARRAY); - + cogl_toggle_client_flag (ctx, flags, COGL_ENABLE_COLOR_ARRAY, GL_COLOR_ARRAY); @@ -325,25 +313,8 @@ gulong cogl_get_enable () { _COGL_GET_CONTEXT (ctx, 0); - - return ctx->enable_flags; -} -void -cogl_blend_func (COGLenum src_factor, COGLenum dst_factor) -{ - /* This function caches the blending setup in the - * hope of lessening GL traffic. - */ - _COGL_GET_CONTEXT (ctx, NO_RETVAL); - - if (ctx->blend_src_factor != src_factor || - ctx->blend_dst_factor != dst_factor) - { - glBlendFunc (src_factor, dst_factor); - ctx->blend_src_factor = src_factor; - ctx->blend_dst_factor = dst_factor; - } + return ctx->enable_flags; } void @@ -351,14 +322,14 @@ cogl_enable_depth_test (gboolean setting) { if (setting) { - glEnable (GL_DEPTH_TEST); + glEnable (GL_DEPTH_TEST); glEnable (GL_ALPHA_TEST); glDepthFunc (GL_LEQUAL); glAlphaFunc (GL_GREATER, 0.1); } else { - glDisable (GL_DEPTH_TEST); + glDisable (GL_DEPTH_TEST); glDisable (GL_ALPHA_TEST); } } @@ -375,21 +346,19 @@ void cogl_set_source_color (const CoglColor *color) { _COGL_GET_CONTEXT (ctx, NO_RETVAL); - - glColor4ub (cogl_color_get_red_byte (color), - cogl_color_get_green_byte (color), - cogl_color_get_blue_byte (color), - cogl_color_get_alpha_byte (color)); - - /* Store alpha for proper blending enables */ - ctx->color_alpha = cogl_color_get_alpha_byte (color); + + /* In case cogl_set_source_texture was previously used... */ + cogl_material_remove_layer (ctx->default_material, 0); + + cogl_material_set_color (ctx->default_material, color); + cogl_set_source (ctx->default_material); } static void -apply_matrix (const GLfloat *matrix, GLfloat *vertex) +apply_matrix (const float *matrix, float *vertex) { int x, y; - GLfloat vertex_out[4] = { 0 }; + float vertex_out[4] = { 0 }; for (y = 0; y < 4; y++) for (x = 0; x < 4; x++) @@ -399,7 +368,9 @@ apply_matrix (const GLfloat *matrix, GLfloat *vertex) } static void -project_vertex (GLfloat *modelview, GLfloat *project, GLfloat *vertex) +project_vertex (float *modelview, + float *project, + float *vertex) { int i; @@ -414,8 +385,8 @@ project_vertex (GLfloat *modelview, GLfloat *project, GLfloat *vertex) static void set_clip_plane (GLint plane_num, - const GLfloat *vertex_a, - const GLfloat *vertex_b) + const float *vertex_a, + const float *vertex_b) { GLdouble plane[4]; GLfloat angle; @@ -423,15 +394,15 @@ set_clip_plane (GLint plane_num, /* Calculate the angle between the axes and the line crossing the two points */ - angle = atan2f ((vertex_b[1] - vertex_a[1]), - (vertex_b[0] - vertex_a[0])) * 180.0f / G_PI; + angle = atan2f (vertex_b[1] - vertex_a[1], + vertex_b[0] - vertex_a[0]) * (180.0/G_PI); GE( glPushMatrix () ); /* Load the identity matrix and multiply by the reverse of the projection matrix so we can specify the plane in screen coordinates */ GE( glLoadIdentity () ); - GE( glMultMatrixf (ctx->inverse_projection) ); + GE( glMultMatrixf ((GLfloat *) ctx->inverse_projection) ); /* Rotate about point a */ GE( glTranslatef (vertex_a[0], vertex_a[1], vertex_a[2]) ); /* Rotate the plane by the calculated angle so that it will connect @@ -439,9 +410,9 @@ set_clip_plane (GLint plane_num, GE( glRotatef (angle, 0.0f, 0.0f, 1.0f) ); GE( glTranslatef (-vertex_a[0], -vertex_a[1], -vertex_a[2]) ); - plane[0] = 0.0f; - plane[1] = -1.0f; - plane[2] = 0.0f; + plane[0] = 0; + plane[1] = -1.0; + plane[2] = 0; plane[3] = vertex_a[1]; GE( glClipPlane (plane_num, plane) ); @@ -456,18 +427,11 @@ _cogl_set_clip_planes (float x_offset, { GLfloat modelview[16], projection[16]; - GLfloat vertex_tl[4] = { (x_offset), - (y_offset), - 0.0f, 1.0f }; - GLfloat vertex_tr[4] = { (x_offset + width), - (y_offset), - 0.0f, 1.0f }; - GLfloat vertex_bl[4] = { (x_offset), - (y_offset + height), - 0.0f, 1.0f }; - GLfloat vertex_br[4] = { (x_offset + width), - (y_offset + height), - 0.0f, 1.0f }; + float vertex_tl[4] = { x_offset, y_offset, 0, 1.0 }; + float vertex_tr[4] = { x_offset + width, y_offset, 0, 1.0 }; + float vertex_bl[4] = { x_offset, y_offset + height, 0, 1.0 }; + float vertex_br[4] = { x_offset + width, y_offset + height, + 0, 1.0 }; GE( glGetFloatv (GL_MODELVIEW_MATRIX, modelview) ); GE( glGetFloatv (GL_PROJECTION_MATRIX, projection) ); @@ -485,7 +449,7 @@ _cogl_set_clip_planes (float x_offset, if ((vertex_tl[0] < vertex_tr[0] ? 1 : 0) != (vertex_bl[1] < vertex_tl[1] ? 1 : 0)) { - GLfloat temp[4]; + float temp[4]; memcpy (temp, vertex_tl, sizeof (temp)); memcpy (vertex_tl, vertex_tr, sizeof (temp)); memcpy (vertex_tr, temp, sizeof (temp)); @@ -512,7 +476,7 @@ _cogl_add_stencil_clip (float x_offset, if (first) { GE( glEnable (GL_STENCIL_TEST) ); - + /* Initially disallow everything */ GE( glClearStencil (0) ); GE( glClear (GL_STENCIL_BUFFER_BIT) ); @@ -545,7 +509,7 @@ _cogl_add_stencil_clip (float x_offset, GE( glMatrixMode (GL_PROJECTION) ); GE( glPushMatrix () ); GE( glLoadIdentity () ); - GE( glRecti (-1, 1, 1, -1) ); + GE( glRectf (-1, 1, 1, -1) ); GE( glPopMatrix () ); GE( glMatrixMode (GL_MODELVIEW) ); GE( glPopMatrix () ); @@ -559,14 +523,8 @@ _cogl_add_stencil_clip (float x_offset, void _cogl_set_matrix (const float *matrix) { - float float_matrix[16]; - int i; - - for (i = 0; i < 16; i++) - float_matrix[i] = (matrix[i]); - GE( glLoadIdentity () ); - GE( glMultMatrixf (float_matrix) ); + GE( glMultMatrixf (matrix) ); } void @@ -593,13 +551,6 @@ _cogl_disable_clip_planes (void) GE( glDisable (GL_CLIP_PLANE0) ); } -void -cogl_alpha_func (COGLenum func, - float ref) -{ - GE( glAlphaFunc (func, (ref)) ); -} - void cogl_perspective (float fovy, float aspect, @@ -637,26 +588,27 @@ cogl_perspective (float fovy, d = (-(2 * zFar) * zNear) / (zFar - zNear); #define M(row,col) m[col*4+row] - M(0,0) = (x); - M(1,1) = (y); - M(2,2) = (c); - M(2,3) = (d); - M(3,2) = -1.0F; + M(0,0) = x; + M(1,1) = y; + M(2,2) = c; + M(2,3) = d; + M(3,2) = -1.0; GE( glMultMatrixf (m) ); GE( glMatrixMode (GL_MODELVIEW) ); /* Calculate and store the inverse of the matrix */ - memset (ctx->inverse_projection, 0, sizeof (GLfloat) * 16); + memset (ctx->inverse_projection, 0, sizeof (float) * 16); #define m ctx->inverse_projection - M(0, 0) = 1.0f / (x); - M(1, 1) = 1.0f / (y); - M(2, 3) = -1.0f; - M(3, 2) = 1.0f / (d); - M(3, 3) = (c) / (d); + M(0, 0) = (1.0 / x); + M(1, 1) = (1.0 / y); + M(2, 3) = -1.0; + M(3, 2) = (1.0 / d); + M(3, 3) = (c / d); #undef m + #undef M } @@ -668,7 +620,7 @@ cogl_frustum (float left, float z_near, float z_far) { - GLfloat c, d; + float c, d; _COGL_GET_CONTEXT (ctx, NO_RETVAL); @@ -685,24 +637,18 @@ cogl_frustum (float left, GE( glMatrixMode (GL_MODELVIEW) ); /* Calculate and store the inverse of the matrix */ - memset (ctx->inverse_projection, 0, sizeof (GLfloat) * 16); + memset (ctx->inverse_projection, 0, sizeof (float) * 16); - c = - (z_far + z_near) - / (z_far - z_near); - d = - (2 * (z_far * z_near)) - / (z_far - z_near); + c = - (z_far + z_near) / (z_far - z_near); + d = - (2 * (z_far * z_near)) / (z_far - z_near); #define M(row,col) ctx->inverse_projection[col*4+row] - M(0,0) = (right - left) - / (2 * z_near); - M(0,3) = (right + left) - / (2 * z_near); - M(1,1) = (top - bottom) - / (2 * z_near); - M(1,3) = (top + bottom) - / (2 * z_near); - M(2,3) = -1.0f; - M(3,2) = 1.0f / d; + M(0,0) = (right - left) / (2 * z_near); + M(0,3) = (right + left) / (2 * z_near); + M(1,1) = (top - bottom) / (2 * z_near); + M(1,3) = (top + bottom) / (2 * z_near); + M(2,3) = -1.0; + M(3,2) = 1.0 / d; M(3,3) = c / d; #undef M } @@ -715,18 +661,22 @@ cogl_viewport (guint width, } void -cogl_setup_viewport (guint width, - guint height, +cogl_setup_viewport (guint width, + guint height, float fovy, float aspect, float z_near, float z_far) { - GLfloat z_camera; - GLfloat projection_matrix[16]; + float z_camera; + float projection_matrix[16]; GE( glViewport (0, 0, width, height) ); + /* For Ortho projection. + * glOrthof (0, width << 16, 0, height << 16, -1 << 16, 1 << 16); + */ + cogl_perspective (fovy, aspect, z_near, z_far); /* @@ -774,9 +724,7 @@ cogl_setup_viewport (guint width, GE( glLoadIdentity () ); GE( glTranslatef (-0.5f, -0.5f, -z_camera) ); - GE( glScalef ( 1.0f / width, - -1.0f / height, - 1.0f / width) ); + GE( glScalef (1.0f / width, -1.0f / height, 1.0f / width) ); GE( glTranslatef (0.0f, -1.0 * height, 0.0f) ); } @@ -815,7 +763,7 @@ _cogl_features_init () GLint num_stencil_bits = 0; _COGL_GET_CONTEXT (ctx, NO_RETVAL); - + flags = COGL_FEATURE_TEXTURE_READ_PIXELS; gl_extensions = (const gchar*) glGetString (GL_EXTENSIONS); @@ -827,7 +775,7 @@ _cogl_features_init () #endif flags |= COGL_FEATURE_TEXTURE_NPOT; } - + #ifdef GL_YCBCR_MESA if (cogl_check_extension ("GL_MESA_ycbcr_texture", gl_extensions)) { @@ -842,47 +790,47 @@ _cogl_features_init () ctx->pf_glCreateProgramObjectARB = (COGL_PFNGLCREATEPROGRAMOBJECTARBPROC) cogl_get_proc_address ("glCreateProgramObjectARB"); - + ctx->pf_glCreateShaderObjectARB = (COGL_PFNGLCREATESHADEROBJECTARBPROC) cogl_get_proc_address ("glCreateShaderObjectARB"); - + ctx->pf_glShaderSourceARB = (COGL_PFNGLSHADERSOURCEARBPROC) cogl_get_proc_address ("glShaderSourceARB"); - + ctx->pf_glCompileShaderARB = (COGL_PFNGLCOMPILESHADERARBPROC) cogl_get_proc_address ("glCompileShaderARB"); - + ctx->pf_glAttachObjectARB = (COGL_PFNGLATTACHOBJECTARBPROC) cogl_get_proc_address ("glAttachObjectARB"); - + ctx->pf_glLinkProgramARB = (COGL_PFNGLLINKPROGRAMARBPROC) cogl_get_proc_address ("glLinkProgramARB"); - + ctx->pf_glUseProgramObjectARB = (COGL_PFNGLUSEPROGRAMOBJECTARBPROC) cogl_get_proc_address ("glUseProgramObjectARB"); - + ctx->pf_glGetUniformLocationARB = (COGL_PFNGLGETUNIFORMLOCATIONARBPROC) cogl_get_proc_address ("glGetUniformLocationARB"); - + ctx->pf_glDeleteObjectARB = (COGL_PFNGLDELETEOBJECTARBPROC) cogl_get_proc_address ("glDeleteObjectARB"); - + ctx->pf_glGetInfoLogARB = (COGL_PFNGLGETINFOLOGARBPROC) cogl_get_proc_address ("glGetInfoLogARB"); - + ctx->pf_glGetObjectParameterivARB = (COGL_PFNGLGETOBJECTPARAMETERIVARBPROC) cogl_get_proc_address ("glGetObjectParameterivARB"); - + ctx->pf_glUniform1fARB = (COGL_PFNGLUNIFORM1FARBPROC) cogl_get_proc_address ("glUniform1fARB"); @@ -898,7 +846,7 @@ _cogl_features_init () ctx->pf_glDisableVertexAttribArrayARB = (COGL_PFNGLDISABLEVERTEXATTRIBARRAYARBPROC) cogl_get_proc_address ("glDisableVertexAttribArrayARB"); - + ctx->pf_glUniform2fARB = (COGL_PFNGLUNIFORM2FARBPROC) cogl_get_proc_address ("glUniform2fARB"); @@ -962,7 +910,7 @@ _cogl_features_init () ctx->pf_glUniformMatrix2fvARB = (COGL_PFNGLUNIFORMMATRIX2FVARBPROC) cogl_get_proc_address ("glUniformMatrix2fvARB"); - + ctx->pf_glUniformMatrix3fvARB = (COGL_PFNGLUNIFORMMATRIX3FVARBPROC) cogl_get_proc_address ("glUniformMatrix3fvARB"); @@ -1006,50 +954,50 @@ _cogl_features_init () ctx->pf_glDisableVertexAttribArrayARB) flags |= COGL_FEATURE_SHADERS_GLSL; } - + if (cogl_check_extension ("GL_EXT_framebuffer_object", gl_extensions) || cogl_check_extension ("GL_ARB_framebuffer_object", gl_extensions)) - { + { ctx->pf_glGenRenderbuffersEXT = (COGL_PFNGLGENRENDERBUFFERSEXTPROC) cogl_get_proc_address ("glGenRenderbuffersEXT"); - + ctx->pf_glDeleteRenderbuffersEXT = (COGL_PFNGLDELETERENDERBUFFERSEXTPROC) cogl_get_proc_address ("glDeleteRenderbuffersEXT"); - + ctx->pf_glBindRenderbufferEXT = (COGL_PFNGLBINDRENDERBUFFEREXTPROC) cogl_get_proc_address ("glBindRenderbufferEXT"); - + ctx->pf_glRenderbufferStorageEXT = (COGL_PFNGLRENDERBUFFERSTORAGEEXTPROC) cogl_get_proc_address ("glRenderbufferStorageEXT"); - + ctx->pf_glGenFramebuffersEXT = (COGL_PFNGLGENFRAMEBUFFERSEXTPROC) cogl_get_proc_address ("glGenFramebuffersEXT"); - + ctx->pf_glBindFramebufferEXT = (COGL_PFNGLBINDFRAMEBUFFEREXTPROC) cogl_get_proc_address ("glBindFramebufferEXT"); - + ctx->pf_glFramebufferTexture2DEXT = (COGL_PFNGLFRAMEBUFFERTEXTURE2DEXTPROC) cogl_get_proc_address ("glFramebufferTexture2DEXT"); - + ctx->pf_glFramebufferRenderbufferEXT = (COGL_PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC) cogl_get_proc_address ("glFramebufferRenderbufferEXT"); - + ctx->pf_glCheckFramebufferStatusEXT = (COGL_PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC) cogl_get_proc_address ("glCheckFramebufferStatusEXT"); - + ctx->pf_glDeleteFramebuffersEXT = (COGL_PFNGLDELETEFRAMEBUFFERSEXTPROC) cogl_get_proc_address ("glDeleteFramebuffersEXT"); - + if (ctx->pf_glGenRenderbuffersEXT && ctx->pf_glBindRenderbufferEXT && ctx->pf_glRenderbufferStorageEXT && @@ -1061,23 +1009,23 @@ _cogl_features_init () ctx->pf_glDeleteFramebuffersEXT) flags |= COGL_FEATURE_OFFSCREEN; } - + if (cogl_check_extension ("GL_EXT_framebuffer_blit", gl_extensions)) { ctx->pf_glBlitFramebufferEXT = (COGL_PFNGLBLITFRAMEBUFFEREXTPROC) cogl_get_proc_address ("glBlitFramebufferEXT"); - + if (ctx->pf_glBlitFramebufferEXT) flags |= COGL_FEATURE_OFFSCREEN_BLIT; } - + if (cogl_check_extension ("GL_EXT_framebuffer_multisample", gl_extensions)) { ctx->pf_glRenderbufferStorageMultisampleEXT = (COGL_PFNGLRENDERBUFFERSTORAGEMULTISAMPLEEXTPROC) cogl_get_proc_address ("glRenderbufferStorageMultisampleEXT"); - + if (ctx->pf_glRenderbufferStorageMultisampleEXT) flags |= COGL_FEATURE_OFFSCREEN_MULTISAMPLE; } @@ -1140,10 +1088,10 @@ CoglFeatureFlags cogl_get_features () { _COGL_GET_CONTEXT (ctx, 0); - + if (!ctx->features_cached) _cogl_features_init (); - + return ctx->feature_flags; } @@ -1151,10 +1099,10 @@ gboolean cogl_features_available (CoglFeatureFlags features) { _COGL_GET_CONTEXT (ctx, 0); - + if (!ctx->features_cached) _cogl_features_init (); - + return (ctx->feature_flags & features) == features; } @@ -1205,8 +1153,8 @@ cogl_get_bitmasks (gint *red, gint *green, gint *blue, gint *alpha) void cogl_fog_set (const CoglColor *fog_color, float density, - float start, - float stop) + float z_near, + float z_far) { GLfloat fogColor[4]; @@ -1222,8 +1170,8 @@ cogl_fog_set (const CoglColor *fog_color, glFogi (GL_FOG_MODE, GL_LINEAR); glHint (GL_FOG_HINT, GL_NICEST); - glFogf (GL_FOG_DENSITY, (density)); - glFogf (GL_FOG_START, (start)); - glFogf (GL_FOG_END, (stop)); + glFogf (GL_FOG_DENSITY, (GLfloat) density); + glFogf (GL_FOG_START, (GLfloat) z_near); + glFogf (GL_FOG_END, (GLfloat) z_far); } diff --git a/clutter/cogl/gles/Makefile.am b/clutter/cogl/gles/Makefile.am index 2b565265f..a33fbbe5d 100644 --- a/clutter/cogl/gles/Makefile.am +++ b/clutter/cogl/gles/Makefile.am @@ -10,7 +10,9 @@ libclutterinclude_HEADERS = \ $(top_builddir)/clutter/cogl/cogl-shader.h \ $(top_builddir)/clutter/cogl/cogl-texture.h \ $(top_builddir)/clutter/cogl/cogl-types.h \ - $(top_builddir)/clutter/cogl/cogl-vertex-buffer.h + $(top_builddir)/clutter/cogl/cogl-vertex-buffer.h \ + $(top_builddir)/clutter/cogl/cogl-material.h \ + $(top_builddir)/clutter/cogl/cogl-matrix.h INCLUDES = \ -I$(top_srcdir) \ diff --git a/clutter/cogl/gles/cogl-context.c b/clutter/cogl/gles/cogl-context.c index c3dc8c592..9d874d78f 100644 --- a/clutter/cogl/gles/cogl-context.c +++ b/clutter/cogl/gles/cogl-context.c @@ -28,65 +28,108 @@ #endif #include "cogl.h" - -#include - #include "cogl-internal.h" #include "cogl-util.h" #include "cogl-context.h" +#include "cogl-texture-private.h" +#include "cogl-material-private.h" #include "cogl-gles2-wrapper.h" +#include + static CoglContext *_context = NULL; gboolean cogl_create_context () { + GLubyte default_texture_data[] = { 0xff, 0xff, 0xff, 0x0 }; + gulong enable_flags = 0; + if (_context != NULL) return FALSE; - + /* Allocate context memory */ _context = (CoglContext*) g_malloc (sizeof (CoglContext)); - + /* Init default values */ _context->feature_flags = 0; _context->features_cached = FALSE; - + _context->enable_flags = 0; _context->color_alpha = 255; - - _context->path_nodes = g_array_new (FALSE, FALSE, sizeof (CoglPathNode)); - _context->last_path = 0; + + _context->material_handles = NULL; + _context->material_layer_handles = NULL; + _context->default_material = cogl_material_new (); + _context->source_material = NULL; _context->texture_handles = NULL; - _context->texture_vertices = g_array_new (FALSE, FALSE, + _context->default_gl_texture_2d_tex = COGL_INVALID_HANDLE; + _context->default_gl_texture_rect_tex = COGL_INVALID_HANDLE; + _context->texture_download_material = COGL_INVALID_HANDLE; + + _context->journal = g_array_new (FALSE, FALSE, sizeof (CoglJournalEntry)); + _context->logged_vertices = g_array_new (FALSE, FALSE, sizeof (GLfloat)); + _context->static_indices = g_array_new (FALSE, FALSE, sizeof (GLushort)); + _context->polygon_vertices = g_array_new (FALSE, FALSE, sizeof (CoglTextureGLVertex)); - _context->texture_indices = g_array_new (FALSE, FALSE, - sizeof (GLushort)); + + _context->current_material = NULL; + _context->current_material_flags = 0; + _context->current_layers = g_array_new (FALSE, FALSE, + sizeof (CoglLayerInfo)); + _context->n_texcoord_arrays_enabled = 0; _context->fbo_handles = NULL; - _context->program_handles = NULL; - _context->shader_handles = NULL; _context->draw_buffer = COGL_WINDOW_BUFFER; + _context->shader_handles = NULL; + + _context->program_handles = NULL; + _context->vertex_buffer_handles = NULL; - - _context->blend_src_factor = CGL_SRC_ALPHA; - _context->blend_dst_factor = CGL_ONE_MINUS_SRC_ALPHA; + + _context->path_nodes = g_array_new (FALSE, FALSE, sizeof (CoglPathNode)); + _context->last_path = 0; + _context->stencil_material = cogl_material_new (); /* Init the GLES2 wrapper */ #ifdef HAVE_COGL_GLES2 cogl_gles2_wrapper_init (&_context->gles2); #endif - - /* Init OpenGL state */ - GE( cogl_wrap_glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) ); - GE( glColorMask (TRUE, TRUE, TRUE, FALSE) ); - GE( glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ); - cogl_enable (0); + /* Initialise the clip stack */ _cogl_clip_stack_state_init (); - + + /* Create default textures used for fall backs */ + _context->default_gl_texture_2d_tex = + cogl_texture_new_from_data (1, /* width */ + 1, /* height */ + -1, /* max waste */ + FALSE, /* auto mipmap */ + COGL_PIXEL_FORMAT_RGBA_8888, /* data format */ + /* internal format */ + COGL_PIXEL_FORMAT_RGBA_8888, + 0, /* auto calc row stride */ + &default_texture_data); + _context->default_gl_texture_rect_tex = + cogl_texture_new_from_data (1, /* width */ + 1, /* height */ + -1, /* max waste */ + FALSE, /* auto mipmap */ + COGL_PIXEL_FORMAT_RGBA_8888, /* data format */ + /* internal format */ + COGL_PIXEL_FORMAT_RGBA_8888, + 0, /* auto calc row stride */ + &default_texture_data); + + cogl_set_source (_context->default_material); + cogl_material_flush_gl_state (_context->source_material, NULL); + enable_flags = + cogl_material_get_cogl_enable_flags (_context->source_material); + cogl_enable (enable_flags); + return TRUE; } @@ -101,15 +144,6 @@ cogl_destroy_context () if (_context->path_nodes) g_array_free (_context->path_nodes, TRUE); -#ifdef HAVE_COGL_GLES2 - cogl_gles2_wrapper_deinit (&_context->gles2); -#endif - - if (_context->texture_vertices) - g_array_free (_context->texture_vertices, TRUE); - if (_context->texture_indices) - g_array_free (_context->texture_indices, TRUE); - if (_context->texture_handles) g_array_free (_context->texture_handles, TRUE); if (_context->fbo_handles) @@ -118,16 +152,36 @@ cogl_destroy_context () g_array_free (_context->shader_handles, TRUE); if (_context->program_handles) g_array_free (_context->program_handles, TRUE); - + + if (_context->default_gl_texture_2d_tex) + cogl_texture_unref (_context->default_gl_texture_2d_tex); + if (_context->default_gl_texture_rect_tex) + cogl_texture_unref (_context->default_gl_texture_rect_tex); + + if (_context->default_material) + cogl_material_unref (_context->default_material); + + if (_context->journal) + g_array_free (_context->journal, TRUE); + if (_context->logged_vertices) + g_array_free (_context->logged_vertices, TRUE); + + if (_context->static_indices) + g_array_free (_context->static_indices, TRUE); + if (_context->polygon_vertices) + g_array_free (_context->polygon_vertices, TRUE); + if (_context->current_layers) + g_array_free (_context->current_layers, TRUE); + g_free (_context); } CoglContext * _cogl_context_get_default () { - /* Create if doesn't exists yet */ + /* Create if doesn't exist yet */ if (_context == NULL) cogl_create_context (); - + return _context; } diff --git a/clutter/cogl/gles/cogl-context.h b/clutter/cogl/gles/cogl-context.h index 812ce4657..041587c56 100644 --- a/clutter/cogl/gles/cogl-context.h +++ b/clutter/cogl/gles/cogl-context.h @@ -41,52 +41,69 @@ typedef struct typedef struct { /* Features cache */ - CoglFeatureFlags feature_flags; - gboolean features_cached; - - /* Enable cache */ - gulong enable_flags; - guint8 color_alpha; - COGLenum blend_src_factor; - COGLenum blend_dst_factor; + CoglFeatureFlags feature_flags; + gboolean features_cached; + + /* Enable cache */ + gulong enable_flags; + guint8 color_alpha; + + gboolean enable_backface_culling; - gboolean enable_backface_culling; - - /* Primitives */ - floatVec2 path_start; - floatVec2 path_pen; - GArray *path_nodes; - guint last_path; - floatVec2 path_nodes_min; - floatVec2 path_nodes_max; - /* Cache of inverse projection matrix */ float inverse_projection[16]; + /* Materials */ + GArray *material_handles; + GArray *material_layer_handles; + CoglHandle default_material; + CoglHandle source_material; + /* Textures */ - GArray *texture_handles; - GArray *texture_vertices; - GArray *texture_indices; - /* The gl texture number that the above vertices apply to. This to - detect when a different slice is encountered so that the vertices - can be flushed */ - GLuint texture_current; - GLenum texture_target; - GLenum texture_format; + GArray *texture_handles; + CoglHandle default_gl_texture_2d_tex; + CoglHandle default_gl_texture_rect_tex; + CoglHandle texture_download_material; + + /* Batching geometry... */ + /* We journal the texture rectangles we want to submit to OpenGL so + * we have an oppertunity to optimise the final order so that we + * can batch things together. */ + GArray *journal; + GArray *logged_vertices; + GArray *static_indices; + GArray *polygon_vertices; + + /* Some simple caching, to minimize state changes... */ + CoglHandle current_material; + gulong current_material_flags; + GArray *current_layers; + guint n_texcoord_arrays_enabled; /* Framebuffer objects */ - GArray *fbo_handles; - CoglBufferTarget draw_buffer; + GArray *fbo_handles; + CoglBufferTarget draw_buffer; /* Shaders */ - GArray *program_handles; - GArray *shader_handles; + GArray *shader_handles; - /* Vertex buffers */ - GArray *vertex_buffer_handles; + /* Programs */ + GArray *program_handles; /* Clip stack */ - CoglClipStackState clip; + CoglClipStackState clip; + + /* Vertex buffers */ + GArray *vertex_buffer_handles; + + /* Primitives */ + floatVec2 path_start; + floatVec2 path_pen; + GArray *path_nodes; + guint last_path; + floatVec2 path_nodes_min; + floatVec2 path_nodes_max; + CoglHandle stencil_material; #ifdef HAVE_COGL_GLES2 CoglGles2Wrapper gles2; @@ -95,7 +112,7 @@ typedef struct supported */ GLint viewport_store[4]; #endif - + } CoglContext; CoglContext * @@ -106,6 +123,6 @@ _cogl_context_get_default (); CoglContext *ctxvar = _cogl_context_get_default (); \ if (ctxvar == NULL) return retval; -#define NO_RETVAL +#define NO_RETVAL #endif /* __COGL_CONTEXT_H */ diff --git a/clutter/cogl/gles/cogl-defines.h.in b/clutter/cogl/gles/cogl-defines.h.in index b3cd355d6..3d69a080e 100644 --- a/clutter/cogl/gles/cogl-defines.h.in +++ b/clutter/cogl/gles/cogl-defines.h.in @@ -162,7 +162,11 @@ G_BEGIN_DECLS #define CGL_STENCIL_PASS_DEPTH_PASS GL_STENCIL_PASS_DEPTH_PASS #define CGL_STENCIL_REF GL_STENCIL_REF #define CGL_STENCIL_WRITEMASK GL_STENCIL_WRITEMASK +#ifdef HAVE_COGL_GLES2 +#define CGL_MATRIX_MODE 0x0BA0 /* bad style but works for now */ +#else #define CGL_MATRIX_MODE GL_MATRIX_MODE +#endif #define CGL_VIEWPORT GL_VIEWPORT #define CGL_MODELVIEW_STACK_DEPTH GL_MODELVIEW_STACK_DEPTH #define CGL_PROJECTION_STACK_DEPTH GL_PROJECTION_STACK_DEPTH @@ -190,7 +194,11 @@ G_BEGIN_DECLS #define CGL_MAX_VIEWPORT_DIMS GL_MAX_VIEWPORT_DIMS #define CGL_MAX_ELEMENTS_VERTICES GL_MAX_ELEMENTS_VERTICES #define CGL_MAX_ELEMENTS_INDICES GL_MAX_ELEMENTS_INDICES -#define CGL_MAX_TEXTURE_UNITS GL_MAX_TEXTURE_UNITS +#ifdef HAVE_COGL_GLES2 +#define CGL_MAX_COMBINED_TEXTURE_IMAGE_UNITS GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS +#else +#define CGL_MAX_COMBINED_TEXTURE_IMAGE_UNITS GL_MAX_TEXTURE_UNITS +#endif #define CGL_SUBPIXEL_BITS GL_SUBPIXEL_BITS #define CGL_RED_BITS GL_RED_BITS #define CGL_GREEN_BITS GL_GREEN_BITS @@ -315,7 +323,7 @@ G_BEGIN_DECLS /* PixelType */ /* GL_UNSIGNED_BYTE */ -#define CGL_UNSIGNED_SHORT_4_4_4_4 GL_UNSIGNED_SHORT_4_4_4_4 +#define CGL_UNSIGNED_SHORT_4_4_4_4 GL_UNSIGNED_SHORT_4_4_4_4 #define CGL_UNSIGNED_SHORT_5_5_5_1 GL_UNSIGNED_SHORT_5_5_5_1 #define CGL_UNSIGNED_SHORT_5_6_5 CGL_UNSIGNED_SHORT_5_6_5 diff --git a/clutter/cogl/gles/cogl-fbo.c b/clutter/cogl/gles/cogl-fbo.c index 3b1099e26..3cf523a00 100644 --- a/clutter/cogl/gles/cogl-fbo.c +++ b/clutter/cogl/gles/cogl-fbo.c @@ -206,29 +206,29 @@ cogl_draw_buffer (CoglBufferTarget target, CoglHandle offscreen) from a non-screen buffer */ GE( glGetIntegerv (GL_VIEWPORT, ctx->viewport_store) ); - GE( cogl_wrap_glMatrixMode (GL_PROJECTION) ); - GE( cogl_wrap_glPushMatrix () ); - GE( cogl_wrap_glLoadIdentity () ); + GE( glMatrixMode (GL_PROJECTION) ); + GE( glPushMatrix () ); + GE( glLoadIdentity () ); - GE( cogl_wrap_glMatrixMode (GL_MODELVIEW) ); - GE( cogl_wrap_glPushMatrix () ); - GE( cogl_wrap_glLoadIdentity () ); + GE( glMatrixMode (GL_MODELVIEW) ); + GE( glPushMatrix () ); + GE( glLoadIdentity () ); } else { /* Override viewport and matrix setup if redirecting from another offscreen buffer */ - GE( cogl_wrap_glMatrixMode (GL_PROJECTION) ); - GE( cogl_wrap_glLoadIdentity () ); + GE( glMatrixMode (GL_PROJECTION) ); + GE( glLoadIdentity () ); - GE( cogl_wrap_glMatrixMode (GL_MODELVIEW) ); - GE( cogl_wrap_glLoadIdentity () ); + GE( glMatrixMode (GL_MODELVIEW) ); + GE( glLoadIdentity () ); } /* Setup new viewport and matrices */ GE( glViewport (0, 0, fbo->width, fbo->height) ); - GE( cogl_wrap_glTranslatef (-1.0, -1.0, 0) ); - GE( cogl_wrap_glScalef (((float)(2) / + GE( glTranslatef (-1.0, -1.0, 0) ); + GE( glScalef (((float)(2) / (float)(fbo->width)), ((float)(2) / (float)(fbo->height)), @@ -265,11 +265,11 @@ cogl_draw_buffer (CoglBufferTarget target, CoglHandle offscreen) GE( glViewport (ctx->viewport_store[0], ctx->viewport_store[1], ctx->viewport_store[2], ctx->viewport_store[3]) ); - GE( cogl_wrap_glMatrixMode (GL_PROJECTION) ); - GE( cogl_wrap_glPopMatrix () ); + GE( glMatrixMode (GL_PROJECTION) ); + GE( glPopMatrix () ); - GE( cogl_wrap_glMatrixMode (GL_MODELVIEW) ); - GE( cogl_wrap_glPopMatrix () ); + GE( glMatrixMode (GL_MODELVIEW) ); + GE( glPopMatrix () ); } /* Bind window framebuffer object */ diff --git a/clutter/cogl/gles/cogl-fixed-fragment-shader.glsl b/clutter/cogl/gles/cogl-fixed-fragment-shader.glsl index 2a5c7b676..128afb42e 100644 --- a/clutter/cogl/gles/cogl-fixed-fragment-shader.glsl +++ b/clutter/cogl/gles/cogl-fixed-fragment-shader.glsl @@ -1,16 +1,20 @@ -/*** cogl_fixed_fragment_shader_start ***/ +/*** cogl_fixed_fragment_shader_variables_start ***/ /* There is no default precision for floats in fragment shaders in GLES 2 so we need to define one */ -precision mediump float; +precision highp float; + +/*** cogl_fixed_fragment_shader_inputs ***/ /* Inputs from the vertex shader */ varying vec4 frag_color; -varying vec2 tex_coord; varying float fog_amount; +/*** cogl_fixed_fragment_shader_texturing_options ***/ + /* Texturing options */ -uniform sampler2D texture_unit; + +/*** cogl_fixed_fragment_shader_fogging_options ***/ /* Fogging options */ uniform vec4 fog_color; @@ -18,28 +22,13 @@ uniform vec4 fog_color; /* Alpha test options */ uniform float alpha_test_ref; +/*** cogl_fixed_fragment_shader_main_declare ***/ + void main (void) { - /*** cogl_fixed_fragment_shader_texture_alpha_only ***/ + /*** cogl_fixed_fragment_shader_main_start ***/ - /* If the texture only has an alpha channel (eg, with the textures - from the pango renderer) then the RGB components will be - black. We want to use the RGB from the current color in that - case */ - gl_FragColor = frag_color; - gl_FragColor.a *= texture2D (texture_unit, tex_coord).a; - - /*** cogl_fixed_fragment_shader_texture ***/ - - /* This pointless extra variable is needed to work around an - apparent bug in the PowerVR drivers. Without it the alpha - blending seems to stop working */ - vec4 frag_color_copy = frag_color; - gl_FragColor = frag_color_copy * texture2D (texture_unit, tex_coord); - - /*** cogl_fixed_fragment_shader_solid_color ***/ - gl_FragColor = frag_color; /*** cogl_fixed_fragment_shader_fog ***/ diff --git a/clutter/cogl/gles/cogl-fixed-vertex-shader.glsl b/clutter/cogl/gles/cogl-fixed-vertex-shader.glsl index 050f7dea4..e5d30a644 100644 --- a/clutter/cogl/gles/cogl-fixed-vertex-shader.glsl +++ b/clutter/cogl/gles/cogl-fixed-vertex-shader.glsl @@ -1,34 +1,41 @@ -/*** cogl_fixed_vertex_shader_start ***/ +/*** cogl_fixed_vertex_shader_per_vertex_attribs ***/ /* Per vertex attributes */ attribute vec4 vertex_attrib; -attribute vec4 tex_coord_attrib; attribute vec4 color_attrib; +/*** cogl_fixed_vertex_shader_transform_matrices ***/ + /* Transformation matrices */ uniform mat4 modelview_matrix; uniform mat4 mvp_matrix; /* combined modelview and projection matrix */ -uniform mat4 texture_matrix; + +/*** cogl_fixed_vertex_shader_output_variables ***/ /* Outputs to the fragment shader */ varying vec4 frag_color; -varying vec2 tex_coord; varying float fog_amount; +/*** cogl_fixed_vertex_shader_fogging_options ***/ + /* Fogging options */ uniform float fog_density; uniform float fog_start; uniform float fog_end; +/*** cogl_fixed_vertex_shader_main_start ***/ + void main (void) { + vec4 transformed_tex_coord; + /* Calculate the transformed position */ gl_Position = mvp_matrix * vertex_attrib; /* Calculate the transformed texture coordinate */ - vec4 transformed_tex_coord = texture_matrix * tex_coord_attrib; - tex_coord = transformed_tex_coord.st / transformed_tex_coord.q; + + /*** cogl_fixed_vertex_shader_frag_color_start ***/ /* Pass the interpolated vertex color on to the fragment shader */ frag_color = color_attrib; diff --git a/clutter/cogl/gles/cogl-gles2-wrapper.c b/clutter/cogl/gles/cogl-gles2-wrapper.c index b65f9e217..387416aaa 100644 --- a/clutter/cogl/gles/cogl-gles2-wrapper.c +++ b/clutter/cogl/gles/cogl-gles2-wrapper.c @@ -38,6 +38,7 @@ #include "cogl-context.h" #include "cogl-shader-private.h" #include "cogl-program.h" +#include "cogl-internal.h" #define _COGL_GET_GLES2_WRAPPER(wvar, retval) \ CoglGles2Wrapper *wvar; \ @@ -66,9 +67,9 @@ while (0) #define COGL_GLES2_WRAPPER_VERTEX_ATTRIB 0 -#define COGL_GLES2_WRAPPER_TEX_COORD_ATTRIB 1 -#define COGL_GLES2_WRAPPER_COLOR_ATTRIB 2 -#define COGL_GLES2_WRAPPER_NORMAL_ATTRIB 3 +#define COGL_GLES2_WRAPPER_COLOR_ATTRIB 1 +#define COGL_GLES2_WRAPPER_NORMAL_ATTRIB 2 + static GLuint cogl_gles2_wrapper_create_shader (GLenum type, const char *source) @@ -109,13 +110,19 @@ cogl_gles2_wrapper_init (CoglGles2Wrapper *wrapper) memset (wrapper, 0, sizeof (CoglGles2Wrapper)); /* Initialize the stacks */ - cogl_wrap_glMatrixMode (GL_TEXTURE); - cogl_wrap_glLoadIdentity (); cogl_wrap_glMatrixMode (GL_PROJECTION); cogl_wrap_glLoadIdentity (); cogl_wrap_glMatrixMode (GL_MODELVIEW); cogl_wrap_glLoadIdentity (); + wrapper->texture_units = + g_array_new (FALSE, FALSE, sizeof (CoglGles2WrapperTextureUnit *)); + + /* The gl*ActiveTexture wrappers will initialise the texture + * stack for the texture unit when it's first activated */ + cogl_wrap_glActiveTexture (GL_TEXTURE0); + cogl_wrap_glClientActiveTexture (GL_TEXTURE0); + /* Initialize the fogging options */ cogl_wrap_glDisable (GL_FOG); cogl_wrap_glFogf (GL_FOG_MODE, GL_LINEAR); @@ -137,11 +144,14 @@ cogl_gles2_settings_equal (const CoglGles2WrapperSettings *a, { if (fragment_tests) { - if (a->texture_2d_enabled != b->texture_2d_enabled) - return FALSE; - - if (a->texture_2d_enabled && a->alpha_only != b->alpha_only) - return FALSE; + int i; + for (i = 0; i < a->n_texture_units; i++) + { + if (a->texture_units[i].enabled != b->texture_units[i].enabled) + return FALSE; + if (a->texture_units[i].alpha_only != b->texture_units[i].alpha_only) + return FALSE; + } if (a->alpha_test_enabled != b->alpha_test_enabled) return FALSE; @@ -165,6 +175,7 @@ cogl_gles2_get_vertex_shader (const CoglGles2WrapperSettings *settings) GLuint shader_obj; CoglGles2WrapperShader *shader; GSList *node; + int i; _COGL_GET_GLES2_WRAPPER (w, NULL); @@ -177,7 +188,47 @@ cogl_gles2_get_vertex_shader (const CoglGles2WrapperSettings *settings) return (CoglGles2WrapperShader *) node->data; /* Otherwise create a new shader */ - shader_source = g_string_new (cogl_fixed_vertex_shader_start); + shader_source = g_string_new (cogl_fixed_vertex_shader_per_vertex_attribs); + + for (i = 0; i < settings->n_texture_units; i++) + { + if (!settings->texture_units[i].enabled) + continue; + g_string_append_printf (shader_source, + "attribute vec4 multi_tex_coord_attrib%d;\n", + i); + } + + g_string_append (shader_source, cogl_fixed_vertex_shader_transform_matrices); + g_string_append_printf (shader_source, + "uniform mat4 texture_matrix[%d];\n", + settings->n_texture_units); + + g_string_append (shader_source, cogl_fixed_vertex_shader_output_variables); + g_string_append_printf (shader_source, + "varying vec2 tex_coord[%d];", + settings->n_texture_units); + + g_string_append (shader_source, cogl_fixed_vertex_shader_fogging_options); + g_string_append (shader_source, cogl_fixed_vertex_shader_main_start); + + for (i = 0; i < settings->n_texture_units; i++) + { + if (!settings->texture_units[i].enabled) + continue; + + g_string_append_printf (shader_source, + "transformed_tex_coord = " + "texture_matrix[%d] " + " * multi_tex_coord_attrib%d;\n", + i, i); + g_string_append_printf (shader_source, + "tex_coord[%d] = transformed_tex_coord.st " + " / transformed_tex_coord.q;\n", + i); + } + + g_string_append (shader_source, cogl_fixed_vertex_shader_frag_color_start); if (settings->fog_enabled) { @@ -207,10 +258,10 @@ cogl_gles2_get_vertex_shader (const CoglGles2WrapperSettings *settings) shader_source->str); g_string_free (shader_source, TRUE); - + if (shader_obj == 0) return NULL; - + shader = g_slice_new (CoglGles2WrapperShader); shader->shader = shader_obj; shader->settings = *settings; @@ -228,6 +279,7 @@ cogl_gles2_get_fragment_shader (const CoglGles2WrapperSettings *settings) GLuint shader_obj; CoglGles2WrapperShader *shader; GSList *node; + int i; _COGL_GET_GLES2_WRAPPER (w, NULL); @@ -240,18 +292,61 @@ cogl_gles2_get_fragment_shader (const CoglGles2WrapperSettings *settings) return (CoglGles2WrapperShader *) node->data; /* Otherwise create a new shader */ - shader_source = g_string_new (cogl_fixed_fragment_shader_start); - if (settings->texture_2d_enabled) + shader_source = g_string_new (cogl_fixed_fragment_shader_variables_start); + + g_string_append (shader_source, cogl_fixed_fragment_shader_inputs); + g_string_append_printf (shader_source, + "varying vec2 tex_coord[%d];\n", + settings->n_texture_units); + + g_string_append (shader_source, cogl_fixed_fragment_shader_texturing_options); + g_string_append_printf (shader_source, + "uniform sampler2D texture_unit[%d];\n", + settings->n_texture_units); + + g_string_append (shader_source, cogl_fixed_fragment_shader_fogging_options); + + g_string_append (shader_source, cogl_fixed_fragment_shader_main_declare); + + g_string_append (shader_source, cogl_fixed_fragment_shader_main_start); + + /* This pointless extra variable is needed to work around an + apparent bug in the PowerVR drivers. Without it the alpha + blending seems to stop working */ + /* g_string_append (shader_source, "gl_FragColor = frag_color;\n"); + */ + g_string_append (shader_source, + "vec4 frag_color_copy = frag_color;\n"); + g_string_append (shader_source, "gl_FragColor = frag_color;\n"); + + for (i = 0; i < settings->n_texture_units; i++) { - if (settings->alpha_only) - g_string_append (shader_source, - cogl_fixed_fragment_shader_texture_alpha_only); + if (settings->texture_units[i].alpha_only) + { + /* If the texture only has an alpha channel (eg, with the textures + from the pango renderer) then the RGB components will be + black. We want to use the RGB from the current color in that + case */ + g_string_append_printf ( + shader_source, + "gl_FragColor.a *= " + "texture2D (texture_unit[%d], tex_coord[%d]).a;\n", + i, i); + } else - g_string_append (shader_source, - cogl_fixed_fragment_shader_texture); + { + g_string_append_printf ( + shader_source, + "gl_FragColor *= " + "texture2D (texture_unit[%d], tex_coord[%d]);\n", + i, i); + } } - else - g_string_append (shader_source, cogl_fixed_fragment_shader_solid_color); + /* FIXME */ + g_string_append (shader_source, "gl_FragColor.r = 1.0;\n"); + g_string_append (shader_source, "gl_FragColor.g = 1.0;\n"); + if (i == 0) + g_string_append (shader_source, "gl_FragColor = frag_color;\n"); if (settings->fog_enabled) g_string_append (shader_source, cogl_fixed_fragment_shader_fog); @@ -294,10 +389,10 @@ cogl_gles2_get_fragment_shader (const CoglGles2WrapperSettings *settings) shader_source->str); g_string_free (shader_source, TRUE); - + if (shader_obj == 0) return NULL; - + shader = g_slice_new (CoglGles2WrapperShader); shader->shader = shader_obj; shader->settings = *settings; @@ -308,6 +403,69 @@ cogl_gles2_get_fragment_shader (const CoglGles2WrapperSettings *settings) return shader; } +static void +cogl_gles2_wrapper_get_locations (GLuint program, + CoglGles2WrapperSettings *settings, + CoglGles2WrapperUniforms *uniforms, + CoglGles2WrapperAttributes *attribs) +{ + int i; + + uniforms->mvp_matrix_uniform + = glGetUniformLocation (program, "mvp_matrix"); + uniforms->modelview_matrix_uniform + = glGetUniformLocation (program, "modelview_matrix"); + + uniforms->texture_matrix_uniforms = + g_array_new (FALSE, FALSE, sizeof (GLint)); + uniforms->texture_sampler_uniforms = + g_array_new (FALSE, FALSE, sizeof (GLint)); + attribs->multi_texture_coords = + g_array_new (FALSE, FALSE, sizeof (GLint)); + for (i = 0; i < settings->n_texture_units; i++) + { + char *matrix_var_name = g_strdup_printf ("texture_matrix[%d]", i); + char *sampler_var_name = g_strdup_printf ("texture_unit[%d]", i); + char *tex_coord_var_name = + g_strdup_printf ("multi_tex_coord_attrib%d", i); + GLint location; + + location = glGetUniformLocation (program, matrix_var_name); + g_array_append_val (uniforms->texture_matrix_uniforms, location); + location = glGetUniformLocation (program, sampler_var_name); + g_array_append_val (uniforms->texture_sampler_uniforms, location); + location = glGetAttribLocation (program, tex_coord_var_name); + g_array_append_val (attribs->multi_texture_coords, location); + + g_free (tex_coord_var_name); + g_free (sampler_var_name); + g_free (matrix_var_name); + } + + uniforms->fog_density_uniform + = glGetUniformLocation (program, "fog_density"); + uniforms->fog_start_uniform + = glGetUniformLocation (program, "fog_start"); + uniforms->fog_end_uniform + = glGetUniformLocation (program, "fog_end"); + uniforms->fog_color_uniform + = glGetUniformLocation (program, "fog_color"); + + uniforms->alpha_test_ref_uniform + = glGetUniformLocation (program, "alpha_test_ref"); +} + +static void +cogl_gles2_wrapper_bind_attributes (GLuint program) +{ + glBindAttribLocation (program, COGL_GLES2_WRAPPER_VERTEX_ATTRIB, + "vertex_attrib"); + glBindAttribLocation (program, COGL_GLES2_WRAPPER_COLOR_ATTRIB, + "color_attrib"); + glBindAttribLocation (program, COGL_GLES2_WRAPPER_NORMAL_ATTRIB, + "normal_attrib"); +} + static CoglGles2WrapperProgram * cogl_gles2_wrapper_get_program (const CoglGles2WrapperSettings *settings) { @@ -403,61 +561,22 @@ cogl_gles2_wrapper_get_program (const CoglGles2WrapperSettings *settings) } program->settings = *settings; - - cogl_gles2_wrapper_get_uniforms (program->program, &program->uniforms); + + cogl_gles2_wrapper_get_locations (program->program, + &program->settings, + &program->uniforms, + &program->attributes); /* We haven't tried to get a location for any of the custom uniforms yet */ for (i = 0; i < COGL_GLES2_NUM_CUSTOM_UNIFORMS; i++) program->custom_uniforms[i] = COGL_GLES2_UNBOUND_CUSTOM_UNIFORM; - + w->compiled_programs = g_slist_append (w->compiled_programs, program); - + return program; } -void -cogl_gles2_wrapper_bind_attributes (GLuint program) -{ - glBindAttribLocation (program, COGL_GLES2_WRAPPER_VERTEX_ATTRIB, - "vertex_attrib"); - glBindAttribLocation (program, COGL_GLES2_WRAPPER_TEX_COORD_ATTRIB, - "tex_coord_attrib"); - glBindAttribLocation (program, COGL_GLES2_WRAPPER_COLOR_ATTRIB, - "color_attrib"); - glBindAttribLocation (program, COGL_GLES2_WRAPPER_NORMAL_ATTRIB, - "normal_attrib"); -} - -void -cogl_gles2_wrapper_get_uniforms (GLuint program, - CoglGles2WrapperUniforms *uniforms) -{ - uniforms->mvp_matrix_uniform - = glGetUniformLocation (program, "mvp_matrix"); - uniforms->modelview_matrix_uniform - = glGetUniformLocation (program, "modelview_matrix"); - uniforms->texture_matrix_uniform - = glGetUniformLocation (program, "texture_matrix"); - uniforms->bound_texture_uniform - = glGetUniformLocation (program, "texture_unit"); - - uniforms->fog_density_uniform - = glGetUniformLocation (program, "fog_density"); - uniforms->fog_start_uniform - = glGetUniformLocation (program, "fog_start"); - uniforms->fog_end_uniform - = glGetUniformLocation (program, "fog_end"); - uniforms->fog_color_uniform - = glGetUniformLocation (program, "fog_color"); - - uniforms->alpha_test_ref_uniform - = glGetUniformLocation (program, "alpha_test_ref"); - - uniforms->texture_unit_uniform - = glGetUniformLocation (program, "texture_unit"); -} - void cogl_gles2_wrapper_deinit (CoglGles2Wrapper *wrapper) { @@ -493,9 +612,11 @@ cogl_gles2_wrapper_deinit (CoglGles2Wrapper *wrapper) g_free (wrapper->custom_uniforms[i].v.array); } -void +static void cogl_gles2_wrapper_update_matrix (CoglGles2Wrapper *wrapper, GLenum matrix_num) { + CoglGles2WrapperTextureUnit *texture_unit; + switch (matrix_num) { default: @@ -509,7 +630,11 @@ cogl_gles2_wrapper_update_matrix (CoglGles2Wrapper *wrapper, GLenum matrix_num) break; case GL_TEXTURE: - wrapper->dirty_uniforms |= COGL_GLES2_DIRTY_TEXTURE_MATRIX; + wrapper->dirty_uniforms |= COGL_GLES2_DIRTY_TEXTURE_MATRICES; + texture_unit = g_array_index (wrapper->texture_units, + CoglGles2WrapperTextureUnit *, + wrapper->active_texture_unit); + texture_unit->dirty_matrix = 1; break; } } @@ -542,11 +667,19 @@ cogl_wrap_glPushMatrix () break; case GL_TEXTURE: - src = w->texture_stack + w->texture_stack_pos * 16; - w->texture_stack_pos = (w->texture_stack_pos + 1) - & (COGL_GLES2_TEXTURE_STACK_SIZE - 1); - dst = w->texture_stack + w->texture_stack_pos * 16; - break; + { + CoglGles2WrapperTextureUnit *texture_unit; + texture_unit = g_array_index (w->texture_units, + CoglGles2WrapperTextureUnit *, + w->active_texture_unit); + src = texture_unit->texture_stack + + texture_unit->texture_stack_pos * 16; + texture_unit->texture_stack_pos = (texture_unit->texture_stack_pos + 1) + & (COGL_GLES2_TEXTURE_STACK_SIZE - 1); + dst = texture_unit->texture_stack + + texture_unit->texture_stack_pos * 16; + break; + } } /* Copy the old matrix to the new position */ @@ -556,6 +689,7 @@ cogl_wrap_glPushMatrix () void cogl_wrap_glPopMatrix () { + CoglGles2WrapperTextureUnit *texture_unit; _COGL_GET_GLES2_WRAPPER (w, NO_RETVAL); /* Decrement the stack pointer */ @@ -573,7 +707,10 @@ cogl_wrap_glPopMatrix () break; case GL_TEXTURE: - w->texture_stack_pos = (w->texture_stack_pos - 1) + texture_unit = g_array_index (w->texture_units, + CoglGles2WrapperTextureUnit *, + w->active_texture_unit); + texture_unit->texture_stack_pos = (texture_unit->texture_stack_pos - 1) & (COGL_GLES2_TEXTURE_STACK_SIZE - 1); break; } @@ -593,6 +730,8 @@ cogl_wrap_glMatrixMode (GLenum mode) static float * cogl_gles2_get_matrix_stack_top (CoglGles2Wrapper *wrapper) { + CoglGles2WrapperTextureUnit *texture_unit; + switch (wrapper->matrix_mode) { default: @@ -603,7 +742,12 @@ cogl_gles2_get_matrix_stack_top (CoglGles2Wrapper *wrapper) return wrapper->projection_stack + wrapper->projection_stack_pos * 16; case GL_TEXTURE: - return wrapper->texture_stack + wrapper->texture_stack_pos * 16; + + texture_unit = g_array_index (wrapper->texture_units, + CoglGles2WrapperTextureUnit *, + wrapper->active_texture_unit); + return texture_unit->texture_stack + + texture_unit->texture_stack_pos * 16; } } @@ -734,7 +878,7 @@ cogl_wrap_glRotatef (GLfloat angle, GLfloat x, GLfloat y, GLfloat z) float anglef = (angle) * G_PI / 180.0f; float c = cosf (anglef); float s = sinf (anglef); - + matrix[0] = xf * xf * (1.0f - c) + c; matrix[1] = yf * xf * (1.0f - c) + zf * s; matrix[2] = xf * zf * (1.0f - c) - yf * s; @@ -791,8 +935,22 @@ void cogl_wrap_glTexCoordPointer (GLint size, GLenum type, GLsizei stride, const GLvoid *pointer) { - glVertexAttribPointer (COGL_GLES2_WRAPPER_TEX_COORD_ATTRIB, size, type, - GL_FALSE, stride, pointer); + int active_unit; + CoglGles2WrapperTextureUnit *texture_unit; + _COGL_GET_GLES2_WRAPPER (w, NO_RETVAL); + + active_unit = w->active_client_texture_unit; + + texture_unit = g_array_index (w->texture_units, + CoglGles2WrapperTextureUnit *, + active_unit); + texture_unit->texture_coords_size = size; + texture_unit->texture_coords_type = type; + texture_unit->texture_coords_stride = stride; + texture_unit->texture_coords_pointer = pointer; + + w->dirty_attribute_pointers + |= COGL_GLES2_DIRTY_TEX_COORD_VERTEX_ATTRIB; } void @@ -926,7 +1084,7 @@ cogl_wrap_prepare_for_draw (void) cogl_gles2_wrapper_mult_matrix (mvp_matrix, w->projection_stack + w->projection_stack_pos * 16, - modelview_matrix); + modelview_matrix); if (program->uniforms.mvp_matrix_uniform != -1) glUniformMatrix4fv (program->uniforms.mvp_matrix_uniform, 1, @@ -935,11 +1093,28 @@ cogl_wrap_prepare_for_draw (void) glUniformMatrix4fv (program->uniforms.modelview_matrix_uniform, 1, GL_FALSE, modelview_matrix); } - if ((w->dirty_uniforms & COGL_GLES2_DIRTY_TEXTURE_MATRIX) - && program->uniforms.texture_matrix_uniform != -1) - glUniformMatrix4fv (program->uniforms.texture_matrix_uniform, 1, - GL_FALSE, - w->texture_stack + w->texture_stack_pos * 16); + if ((w->dirty_uniforms & COGL_GLES2_DIRTY_TEXTURE_MATRICES)) + { + int i; + + /* TODO - we should probably have a per unit dirty flag too */ + + for (i = 0; i < program->uniforms.texture_matrix_uniforms->len; i++) + { + CoglGles2WrapperTextureUnit *texture_unit; + GLint uniform = + g_array_index (program->uniforms.texture_matrix_uniforms, + GLint, i); + + texture_unit = g_array_index (w->texture_units, + CoglGles2WrapperTextureUnit *, + i); + if (uniform != -1) + glUniformMatrix4fv (uniform, 1, GL_FALSE, + texture_unit->texture_stack + + texture_unit->texture_stack_pos * 16); + } + } if ((w->dirty_uniforms & COGL_GLES2_DIRTY_FOG_DENSITY) && program->uniforms.fog_density_uniform != -1) @@ -956,9 +1131,22 @@ cogl_wrap_prepare_for_draw (void) glUniform1f (program->uniforms.alpha_test_ref_uniform, w->alpha_test_ref); - if ((w->dirty_uniforms & COGL_GLES2_DIRTY_TEXTURE_UNIT) - && program->uniforms.texture_unit_uniform != -1) - glUniform1i (program->uniforms.texture_unit_uniform, 0); + if ((w->dirty_uniforms & COGL_GLES2_DIRTY_TEXTURE_UNITS)) + { + int i; + + /* TODO - we should probably have a per unit dirty flag too */ + + for (i = 0; i < program->uniforms.texture_sampler_uniforms->len; i++) + { + GLint uniform = + g_array_index (program->uniforms.texture_sampler_uniforms, + GLint, i); + + if (uniform != -1) + glUniform1i (uniform, i); + } + } w->dirty_uniforms = 0; } @@ -986,10 +1174,70 @@ cogl_wrap_prepare_for_draw (void) &w->custom_uniforms[i]); } } - + w->dirty_custom_uniforms = 0; } + if (w->dirty_attribute_pointers + & COGL_GLES2_DIRTY_TEX_COORD_VERTEX_ATTRIB) + { + int i; + + /* TODO - coverage test */ + for (i = 0; i < w->settings.n_texture_units; i++) + { + GLint tex_coord_var_index; + CoglGles2WrapperTextureUnit *texture_unit; + + if (!w->settings.texture_units[i].enabled) + continue; + + texture_unit = g_array_index (w->texture_units, + CoglGles2WrapperTextureUnit *, + w->active_texture_unit); + if (!texture_unit->texture_coords_enabled) + continue; + + /* TODO - we should probably have a per unit dirty flag too */ + + /* TODO - coverage test */ + tex_coord_var_index = + g_array_index (program->attributes.multi_texture_coords, + GLint, i); + glVertexAttribPointer (tex_coord_var_index, + texture_unit->texture_coords_size, + texture_unit->texture_coords_type, + GL_FALSE, + texture_unit->texture_coords_stride, + texture_unit->texture_coords_pointer); + } + } + + if (w->dirty_vertex_attrib_enables) + { + int i; + + /* TODO - coverage test */ + + /* TODO - we should probably have a per unit dirty flag too */ + + for (i = 0; i < w->texture_units->len; i++) + { + CoglGles2WrapperTextureUnit *texture_unit = + g_array_index (w->texture_units, + CoglGles2WrapperTextureUnit *, + w->active_texture_unit); + if (texture_unit->texture_coords_enabled) + glEnableVertexAttribArray ( + g_array_index (program->attributes.multi_texture_coords, + GLint, i)); + else + glDisableVertexAttribArray ( + g_array_index (program->attributes.multi_texture_coords, + GLint, i)); + w->dirty_vertex_attrib_enables = 0; + } + } } void @@ -1020,7 +1268,10 @@ cogl_gles2_wrapper_bind_texture (GLenum target, GLuint texture, /* We need to keep track of whether the texture is alpha-only because the emulation of GL_MODULATE needs to work differently in that case */ - _COGL_GLES2_CHANGE_SETTING (w, alpha_only, internal_format == GL_ALPHA); + _COGL_GLES2_CHANGE_SETTING ( + w, texture_units[w->active_texture_unit].alpha_only, + internal_format == GL_ALPHA); + } void @@ -1031,6 +1282,79 @@ cogl_wrap_glTexEnvf (GLenum target, GLenum pname, GLfloat param) nothing needs to be done here. */ } +static void +realize_texture_units (CoglGles2Wrapper *w, int texture_unit_index) +{ + /* We save the active texture unit since we may need to temporarily + * change this to initialise each new texture unit and we want to + * restore the active unit afterwards */ + int initial_active_unit = w->active_texture_unit; + + if (texture_unit_index >= w->settings.n_texture_units) + { + int n_new_texture_units = + texture_unit_index + 1 - w->settings.n_texture_units; + GLint prev_mode; + int i; + + w->settings.texture_units = + g_realloc (w->settings.texture_units, + texture_unit_index + 1 + * sizeof (CoglGles2WrapperTextureUnitSettings)); + + /* We will need to set the matrix mode to GL_TEXTURE to + * initialise any new texture units, so we save the current + * mode for restoring afterwards */ + GE( cogl_wrap_glGetIntegerv (CGL_MATRIX_MODE, &prev_mode)); + + for (i = 0; i < n_new_texture_units; i++) + { + CoglGles2WrapperTextureUnit *new_unit; + CoglGles2WrapperTextureUnitSettings *new_unit_settings; + + new_unit = g_new0 (CoglGles2WrapperTextureUnit, 1); + g_array_append_val (w->texture_units, new_unit); + + w->active_texture_unit = i; + GE( cogl_wrap_glMatrixMode (GL_TEXTURE)); + GE( cogl_wrap_glLoadIdentity ()); + + new_unit_settings = + &w->settings.texture_units[w->settings.n_texture_units + i]; + new_unit_settings->enabled = FALSE; + new_unit_settings->alpha_only = FALSE; + } + + GE( cogl_wrap_glMatrixMode ((GLenum)prev_mode)); + + w->settings.n_texture_units = w->texture_units->len; + } + + w->active_texture_unit = initial_active_unit; +} + +void +cogl_wrap_glClientActiveTexture (GLenum texture) +{ + int texture_unit_index = texture - GL_TEXTURE0; + _COGL_GET_GLES2_WRAPPER (w, NO_RETVAL); + + w->active_client_texture_unit = texture_unit_index; + + realize_texture_units (w, texture_unit_index); +} + +void +cogl_wrap_glActiveTexture (GLenum texture) +{ + int texture_unit_index = texture - GL_TEXTURE0; + _COGL_GET_GLES2_WRAPPER (w, NO_RETVAL); + + w->active_texture_unit = texture_unit_index; + + realize_texture_units (w, texture_unit_index); +} + void cogl_wrap_glEnable (GLenum cap) { @@ -1039,7 +1363,8 @@ cogl_wrap_glEnable (GLenum cap) switch (cap) { case GL_TEXTURE_2D: - _COGL_GLES2_CHANGE_SETTING (w, texture_2d_enabled, TRUE); + _COGL_GLES2_CHANGE_SETTING ( + w, texture_units[w->active_texture_unit].enabled, TRUE); break; case GL_FOG: @@ -1063,7 +1388,8 @@ cogl_wrap_glDisable (GLenum cap) switch (cap) { case GL_TEXTURE_2D: - _COGL_GLES2_CHANGE_SETTING (w, texture_2d_enabled, FALSE); + _COGL_GLES2_CHANGE_SETTING ( + w, texture_units[w->active_texture_unit].enabled, FALSE); break; case GL_FOG: @@ -1082,13 +1408,26 @@ cogl_wrap_glDisable (GLenum cap) void cogl_wrap_glEnableClientState (GLenum array) { + CoglGles2WrapperTextureUnit *texture_unit; + _COGL_GET_GLES2_WRAPPER (w, NO_RETVAL); + switch (array) { case GL_VERTEX_ARRAY: glEnableVertexAttribArray (COGL_GLES2_WRAPPER_VERTEX_ATTRIB); break; case GL_TEXTURE_COORD_ARRAY: - glEnableVertexAttribArray (COGL_GLES2_WRAPPER_TEX_COORD_ATTRIB); + /* TODO - review if this should be in w->settings? */ + + texture_unit = g_array_index (w->texture_units, + CoglGles2WrapperTextureUnit *, + w->active_texture_unit); + if (texture_unit->texture_coords_enabled != 1) + { + texture_unit->texture_coords_enabled = 1; + w->dirty_vertex_attrib_enables + |= COGL_GLES2_DIRTY_TEX_COORD_ATTRIB_ENABLES; + } break; case GL_COLOR_ARRAY: glEnableVertexAttribArray (COGL_GLES2_WRAPPER_COLOR_ATTRIB); @@ -1102,13 +1441,26 @@ cogl_wrap_glEnableClientState (GLenum array) void cogl_wrap_glDisableClientState (GLenum array) { + CoglGles2WrapperTextureUnit *texture_unit; + _COGL_GET_GLES2_WRAPPER (w, NO_RETVAL); + switch (array) { case GL_VERTEX_ARRAY: glDisableVertexAttribArray (COGL_GLES2_WRAPPER_VERTEX_ATTRIB); break; case GL_TEXTURE_COORD_ARRAY: - glDisableVertexAttribArray (COGL_GLES2_WRAPPER_TEX_COORD_ATTRIB); + + texture_unit = g_array_index (w->texture_units, + CoglGles2WrapperTextureUnit *, + w->active_texture_unit); + /* TODO - review if this should be in w->settings? */ + if (texture_unit->texture_coords_enabled != 0) + { + texture_unit->texture_coords_enabled = 0; + w->dirty_vertex_attrib_enables + |= COGL_GLES2_DIRTY_TEX_COORD_ATTRIB_ENABLES; + } break; case GL_COLOR_ARRAY: glDisableVertexAttribArray (COGL_GLES2_WRAPPER_COLOR_ATTRIB); @@ -1156,6 +1508,10 @@ cogl_wrap_glGetIntegerv (GLenum pname, GLint *params) *params = 0; break; + case CGL_MATRIX_MODE: + *params = w->matrix_mode; + break; + default: glGetIntegerv (pname, params); break; @@ -1195,7 +1551,7 @@ cogl_wrap_glFogf (GLenum pname, GLfloat param) case GL_FOG_MODE: _COGL_GLES2_CHANGE_SETTING (w, fog_mode, param); break; - + case GL_FOG_DENSITY: _COGL_GLES2_CHANGE_UNIFORM (w, FOG_DENSITY, fog_density, (param)); diff --git a/clutter/cogl/gles/cogl-gles2-wrapper.h b/clutter/cogl/gles/cogl-gles2-wrapper.h index f126993ef..f45b8e635 100644 --- a/clutter/cogl/gles/cogl-gles2-wrapper.h +++ b/clutter/cogl/gles/cogl-gles2-wrapper.h @@ -32,11 +32,17 @@ G_BEGIN_DECLS #ifdef HAVE_COGL_GLES2 -typedef struct _CoglGles2Wrapper CoglGles2Wrapper; -typedef struct _CoglGles2WrapperUniforms CoglGles2WrapperUniforms; -typedef struct _CoglGles2WrapperSettings CoglGles2WrapperSettings; -typedef struct _CoglGles2WrapperProgram CoglGles2WrapperProgram; -typedef struct _CoglGles2WrapperShader CoglGles2WrapperShader; +typedef struct _CoglGles2Wrapper CoglGles2Wrapper; +typedef struct _CoglGles2WrapperTextureUnit + CoglGles2WrapperTextureUnit; + +typedef struct _CoglGles2WrapperAttributes CoglGles2WrapperAttributes; +typedef struct _CoglGles2WrapperUniforms CoglGles2WrapperUniforms; +typedef struct _CoglGles2WrapperTextureUnitSettings + CoglGles2WrapperTextureUnitSettings; +typedef struct _CoglGles2WrapperSettings CoglGles2WrapperSettings; +typedef struct _CoglGles2WrapperProgram CoglGles2WrapperProgram; +typedef struct _CoglGles2WrapperShader CoglGles2WrapperShader; #define COGL_GLES2_NUM_CUSTOM_UNIFORMS 16 #define COGL_GLES2_UNBOUND_CUSTOM_UNIFORM -2 @@ -46,78 +52,129 @@ typedef struct _CoglGles2WrapperShader CoglGles2WrapperShader; #define COGL_GLES2_PROJECTION_STACK_SIZE 2 #define COGL_GLES2_TEXTURE_STACK_SIZE 2 +/* Dirty flags for shader uniforms */ enum { COGL_GLES2_DIRTY_MVP_MATRIX = 1 << 0, COGL_GLES2_DIRTY_MODELVIEW_MATRIX = 1 << 1, - COGL_GLES2_DIRTY_TEXTURE_MATRIX = 1 << 2, + COGL_GLES2_DIRTY_TEXTURE_MATRICES = 1 << 2, COGL_GLES2_DIRTY_FOG_DENSITY = 1 << 3, COGL_GLES2_DIRTY_FOG_START = 1 << 4, COGL_GLES2_DIRTY_FOG_END = 1 << 5, COGL_GLES2_DIRTY_FOG_COLOR = 1 << 6, COGL_GLES2_DIRTY_ALPHA_TEST_REF = 1 << 7, - COGL_GLES2_DIRTY_TEXTURE_UNIT = 1 << 8, + COGL_GLES2_DIRTY_TEXTURE_UNITS = 1 << 8, COGL_GLES2_DIRTY_ALL = (1 << 9) - 1 }; +/* Dirty flags for shader vertex attribute pointers */ +enum + { + COGL_GLES2_DIRTY_TEX_COORD_VERTEX_ATTRIB = 1 << 0 + }; + +/* Dirty flags for shader vertex attributes enabled status */ +enum + { + COGL_GLES2_DIRTY_TEX_COORD_ATTRIB_ENABLES = 1 << 0 + }; + +struct _CoglGles2WrapperAttributes +{ + GArray *multi_texture_coords; +}; + struct _CoglGles2WrapperUniforms { - GLint mvp_matrix_uniform; - GLint modelview_matrix_uniform; - GLint texture_matrix_uniform; - GLint bound_texture_uniform; + GLint mvp_matrix_uniform; + GLint modelview_matrix_uniform; + GArray *texture_matrix_uniforms; - GLint fog_density_uniform; - GLint fog_start_uniform; - GLint fog_end_uniform; - GLint fog_color_uniform; + GArray *texture_sampler_uniforms; - GLint alpha_test_ref_uniform; + GLint fog_density_uniform; + GLint fog_start_uniform; + GLint fog_end_uniform; + GLint fog_color_uniform; + + GLint alpha_test_ref_uniform; GLint texture_unit_uniform; }; +struct _CoglGles2WrapperTextureUnitSettings +{ + guint enabled:1; + guint alpha_only:1; + /* TODO: blending state */ +}; + +/* NB: We get a copy of this for each fragment/vertex + * program varient we generate so we try to keep it + * fairly lean */ struct _CoglGles2WrapperSettings { - gboolean texture_2d_enabled; - gboolean alpha_only; + CoglGles2WrapperTextureUnitSettings *texture_units; + guint n_texture_units; - gboolean alpha_test_enabled; GLint alpha_test_func; - - gboolean fog_enabled; GLint fog_mode; /* The current in-use user program */ CoglHandle user_program; + + guint alpha_test_enabled:1; + guint fog_enabled:1; +}; + +struct _CoglGles2WrapperTextureUnit +{ + GLfloat texture_stack[COGL_GLES2_TEXTURE_STACK_SIZE * 16]; + GLuint texture_stack_pos; + + GLenum texture_coords_type; + GLint texture_coords_size; + GLsizei texture_coords_stride; + const void *texture_coords_pointer; + + guint texture_coords_enabled:1; + guint dirty_matrix:1; /*!< shader uniform needs updating */ }; struct _CoglGles2Wrapper { - GLuint matrix_mode; - GLfloat modelview_stack[COGL_GLES2_MODELVIEW_STACK_SIZE * 16]; - GLuint modelview_stack_pos; - GLfloat projection_stack[COGL_GLES2_PROJECTION_STACK_SIZE * 16]; - GLuint projection_stack_pos; - GLfloat texture_stack[COGL_GLES2_TEXTURE_STACK_SIZE * 16]; - GLuint texture_stack_pos; + GLuint matrix_mode; + GLfloat modelview_stack[COGL_GLES2_MODELVIEW_STACK_SIZE * 16]; + GLuint modelview_stack_pos; + GLfloat projection_stack[COGL_GLES2_PROJECTION_STACK_SIZE * 16]; + GLuint projection_stack_pos; + GArray *texture_units; + guint active_texture_unit; + guint active_client_texture_unit; /* The combined modelview and projection matrix is only updated at the last minute in glDrawArrays to avoid recalculating it for every change to the modelview matrix */ - GLboolean mvp_uptodate; + GLboolean mvp_uptodate; /* The currently bound program */ CoglGles2WrapperProgram *current_program; - /* The current settings */ + /* The current settings. Effectively these represent anything that + * will require a modified fixed function shader */ CoglGles2WrapperSettings settings; /* Whether the settings have changed since the last draw */ gboolean settings_dirty; /* Uniforms that have changed since the last draw */ int dirty_uniforms, dirty_custom_uniforms; + /* Attribute pointers that have changed since the last draw */ + int dirty_attribute_pointers; + + /* Vertex attribute pointer enables that have changed since the last draw */ + int dirty_vertex_attrib_enables; + /* List of all compiled program combinations */ GSList *compiled_programs; @@ -143,6 +200,10 @@ struct _CoglGles2WrapperProgram /* The settings that were used to generate this combination */ CoglGles2WrapperSettings settings; + /* The attributes for this program that are not bound up-front + * with constant indices */ + CoglGles2WrapperAttributes attributes; + /* The uniforms for this program */ CoglGles2WrapperUniforms uniforms; GLint custom_uniforms[COGL_GLES2_NUM_CUSTOM_UNIFORMS]; @@ -232,6 +293,9 @@ void cogl_wrap_glNormalPointer (GLenum type, GLsizei stride, void cogl_wrap_glTexEnvf (GLenum target, GLenum pname, GLfloat param); +void cogl_wrap_glClientActiveTexture (GLenum texture); +void cogl_wrap_glActiveTexture (GLenum texture); + void cogl_wrap_glEnableClientState (GLenum array); void cogl_wrap_glDisableClientState (GLenum array); @@ -258,12 +322,6 @@ void cogl_gles2_wrapper_bind_texture (GLenum target, GLuint texture, /* This function is only available on GLES 2 */ #define cogl_wrap_glGenerateMipmap glGenerateMipmap -void cogl_gles2_wrapper_bind_attributes (GLuint program); -void cogl_gles2_wrapper_get_uniforms (GLuint program, - CoglGles2WrapperUniforms *uniforms); -void cogl_gles2_wrapper_update_matrix (CoglGles2Wrapper *wrapper, - GLenum matrix_num); - void _cogl_gles2_clear_cache_for_program (CoglHandle program); #else /* HAVE_COGL_GLES2 */ @@ -290,6 +348,7 @@ void _cogl_gles2_clear_cache_for_program (CoglHandle program); #define cogl_wrap_glColorPointer glColorPointer #define cogl_wrap_glNormalPointer glNormalPointer #define cogl_wrap_glTexEnvf glTexEnvf +#define cogl_wrap_glActiveTexture glActiveTexture #define cogl_wrap_glEnableClientState glEnableClientState #define cogl_wrap_glDisableClientState glDisableClientState #define cogl_wrap_glAlphaFunc glAlphaFunc @@ -310,6 +369,11 @@ void _cogl_gles2_clear_cache_for_program (CoglHandle program); glGenerateMipmap doesn't need to do anything */ #define cogl_wrap_glGenerateMipmap(x) ((void) 0) +/* GLES doesn't have glDrawRangeElements, so we simply pretend it does + * but that it makes no use of the start, end constraints: */ +#define glDrawRangeElements(mode, start, end, count, type, indices) \ + glDrawElements (mode, count, type, indices) + #endif /* HAVE_COGL_GLES2 */ G_END_DECLS diff --git a/clutter/cogl/gles/cogl-internal.h b/clutter/cogl/gles/cogl-internal.h index c17c5e24f..786e80f84 100644 --- a/clutter/cogl/gles/cogl-internal.h +++ b/clutter/cogl/gles/cogl-internal.h @@ -75,13 +75,10 @@ const char *_cogl_error_string(GLenum errorCode); #endif /* COGL_DEBUG */ #define COGL_ENABLE_BLEND (1<<1) -#define COGL_ENABLE_TEXTURE_2D (1<<2) -#define COGL_ENABLE_ALPHA_TEST (1<<3) -#define COGL_ENABLE_TEXTURE_RECT (1<<4) -#define COGL_ENABLE_VERTEX_ARRAY (1<<5) -#define COGL_ENABLE_TEXCOORD_ARRAY (1<<6) -#define COGL_ENABLE_COLOR_ARRAY (1<<7) -#define COGL_ENABLE_BACKFACE_CULLING (1<<8) +#define COGL_ENABLE_ALPHA_TEST (1<<2) +#define COGL_ENABLE_VERTEX_ARRAY (1<<3) +#define COGL_ENABLE_COLOR_ARRAY (1<<4) +#define COGL_ENABLE_BACKFACE_CULLING (1<<5) gint _cogl_get_format_bpp (CoglPixelFormat format); diff --git a/clutter/cogl/gles/cogl-primitives.c b/clutter/cogl/gles/cogl-primitives.c index d0cba27c9..612237283 100644 --- a/clutter/cogl/gles/cogl-primitives.c +++ b/clutter/cogl/gles/cogl-primitives.c @@ -38,30 +38,9 @@ #define _COGL_MAX_BEZ_RECURSE_DEPTH 16 -void -_cogl_rectangle (float x, - float y, - float width, - float height) -{ - GLfloat rect_verts[8] = { - (GLfloat) x, (GLfloat) y, - (GLfloat) (x + width), (GLfloat) y, - (GLfloat) x, (GLfloat) (y + height), - (GLfloat) (x + width), (GLfloat) (y + height) - }; - - _COGL_GET_CONTEXT (ctx, NO_RETVAL); - - cogl_enable (COGL_ENABLE_VERTEX_ARRAY - | (ctx->color_alpha < 255 ? COGL_ENABLE_BLEND : 0)); - GE ( cogl_wrap_glVertexPointer (2, GL_FLOAT, 0, rect_verts ) ); - GE ( cogl_wrap_glDrawArrays (GL_TRIANGLE_STRIP, 0, 4) ); -} - void _cogl_path_add_node (gboolean new_sub_path, - float x, + float x, float y) { CoglPathNode new_node; @@ -96,23 +75,28 @@ _cogl_path_add_node (gboolean new_sub_path, void _cogl_path_stroke_nodes () { - guint path_start = 0; + guint path_start = 0; + gulong enable_flags = COGL_ENABLE_VERTEX_ARRAY; _COGL_GET_CONTEXT (ctx, NO_RETVAL); - cogl_enable (COGL_ENABLE_VERTEX_ARRAY - | (ctx->color_alpha < 255 - ? COGL_ENABLE_BLEND : 0)); + enable_flags |= cogl_material_get_cogl_enable_flags (ctx->source_material); + cogl_enable (enable_flags); + + cogl_material_flush_gl_state (ctx->source_material, + COGL_MATERIAL_FLUSH_DISABLE_MASK, + (guint32)~0, /* disable all texture layers */ + NULL); while (path_start < ctx->path_nodes->len) { CoglPathNode *path = &g_array_index (ctx->path_nodes, CoglPathNode, path_start); - GE( cogl_wrap_glVertexPointer (2, GL_FLOAT, sizeof (CoglPathNode), - (guchar *) path - + G_STRUCT_OFFSET (CoglPathNode, x)) ); - GE( cogl_wrap_glDrawArrays (GL_LINE_STRIP, 0, path->path_size) ); + GE( glVertexPointer (2, GL_FLOAT, sizeof (CoglPathNode), + (guchar *) path + + G_STRUCT_OFFSET (CoglPathNode, x)) ); + GE( glDrawArrays (GL_LINE_STRIP, 0, path->path_size) ); path_start += path->path_size; } @@ -145,12 +129,22 @@ _cogl_add_path_to_stencil_buffer (floatVec2 nodes_min, CoglPathNode *path, gboolean merge) { - guint path_start = 0; - guint sub_path_num = 0; - float bounds_x; - float bounds_y; - float bounds_w; - float bounds_h; + guint path_start = 0; + guint sub_path_num = 0; + float bounds_x; + float bounds_y; + float bounds_w; + float bounds_h; + gulong enable_flags = COGL_ENABLE_VERTEX_ARRAY; + + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + /* Just setup a simple material that doesn't use texturing... */ + cogl_material_flush_gl_state (ctx->stencil_material, NULL); + + enable_flags |= + cogl_material_get_cogl_enable_flags (ctx->source_material); + cogl_enable (enable_flags); _cogl_path_get_bounds (nodes_min, nodes_max, &bounds_x, &bounds_y, &bounds_w, &bounds_h); @@ -167,7 +161,7 @@ _cogl_add_path_to_stencil_buffer (floatVec2 nodes_min, GE( glStencilFunc (GL_LEQUAL, 0x1, 0x3) ); } - GE( cogl_wrap_glEnable (GL_STENCIL_TEST) ); + GE( glEnable (GL_STENCIL_TEST) ); GE( glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT) ); GE( glColorMask (FALSE, FALSE, FALSE, FALSE) ); @@ -175,12 +169,10 @@ _cogl_add_path_to_stencil_buffer (floatVec2 nodes_min, while (path_start < path_size) { - cogl_enable (COGL_ENABLE_VERTEX_ARRAY); - - GE( cogl_wrap_glVertexPointer (2, GL_FLOAT, sizeof (CoglPathNode), - (guchar *) path - + G_STRUCT_OFFSET (CoglPathNode, x)) ); - GE( cogl_wrap_glDrawArrays (GL_TRIANGLE_FAN, 0, path->path_size) ); + GE( glVertexPointer (2, GL_FLOAT, sizeof (CoglPathNode), + (guchar *) path + + G_STRUCT_OFFSET (CoglPathNode, x)) ); + GE( glDrawArrays (GL_TRIANGLE_FAN, 0, path->path_size) ); if (sub_path_num > 0) { @@ -209,16 +201,16 @@ _cogl_add_path_to_stencil_buffer (floatVec2 nodes_min, GE( glStencilOp (GL_DECR, GL_DECR, GL_DECR) ); /* Decrement all of the bits twice so that only pixels where the value is 3 will remain */ - GE( cogl_wrap_glPushMatrix () ); - GE( cogl_wrap_glLoadIdentity () ); - GE( cogl_wrap_glMatrixMode (GL_PROJECTION) ); - GE( cogl_wrap_glPushMatrix () ); - GE( cogl_wrap_glLoadIdentity () ); + GE( glPushMatrix () ); + GE( glLoadIdentity () ); + GE( glMatrixMode (GL_PROJECTION) ); + GE( glPushMatrix () ); + GE( glLoadIdentity () ); cogl_rectangle (-1.0, -1.0, 2, 2); cogl_rectangle (-1.0, -1.0, 2, 2); - GE( cogl_wrap_glPopMatrix () ); - GE( cogl_wrap_glMatrixMode (GL_MODELVIEW) ); - GE( cogl_wrap_glPopMatrix () ); + GE( glPopMatrix () ); + GE( glMatrixMode (GL_MODELVIEW) ); + GE( glPopMatrix () ); } GE( glStencilMask (~(GLuint) 0) ); @@ -255,7 +247,7 @@ _cogl_path_fill_nodes_scanlines (CoglPathNode *path, _COGL_GET_CONTEXT (ctx, NO_RETVAL); /* clear scanline intersection lists */ - for (i=0; i < bounds_h; i++) + for (i=0; i < bounds_h; i++) scanlines[i]=NULL; first_x = prev_x = (path->x); @@ -392,8 +384,8 @@ _cogl_path_fill_nodes_scanlines (CoglPathNode *path, /* render triangles */ cogl_enable (COGL_ENABLE_VERTEX_ARRAY | (ctx->color_alpha < 255 ? COGL_ENABLE_BLEND : 0)); - GE ( cogl_wrap_glVertexPointer (2, GL_FLOAT, 0, coords ) ); - GE ( cogl_wrap_glDrawArrays (GL_TRIANGLES, 0, spans * 2 * 3)); + GE ( glVertexPointer (2, GL_FLOAT, 0, coords ) ); + GE ( glDrawArrays (GL_TRIANGLES, 0, spans * 2 * 3)); g_free (coords); } } @@ -444,4 +436,3 @@ _cogl_path_fill_nodes () } } } - diff --git a/clutter/cogl/gles/cogl-texture-private.h b/clutter/cogl/gles/cogl-texture-private.h index 117437458..59bcad108 100644 --- a/clutter/cogl/gles/cogl-texture-private.h +++ b/clutter/cogl/gles/cogl-texture-private.h @@ -28,9 +28,9 @@ #include "cogl-bitmap.h" -typedef struct _CoglTexture CoglTexture; -typedef struct _CoglTexSliceSpan CoglTexSliceSpan; -typedef struct _CoglSpanIter CoglSpanIter; +typedef struct _CoglTexture CoglTexture; +typedef struct _CoglTexSliceSpan CoglTexSliceSpan; +typedef struct _CoglSpanIter CoglSpanIter; struct _CoglTexSliceSpan { @@ -59,7 +59,23 @@ struct _CoglTexture gboolean auto_mipmap; }; +/* To improve batching of geometry when submitting vertices to OpenGL we + * log the texture rectangles we want to draw to a journal, so when we + * later flush the journal we aim to batch data, and gl draw calls. */ +typedef struct _CoglJournalEntry +{ + CoglHandle material; + gint n_layers; + guint32 fallback_mask; + GLuint layer0_override_texture; +} CoglJournalEntry; + CoglTexture* _cogl_texture_pointer_from_handle (CoglHandle handle); +gboolean +_cogl_texture_span_has_waste (CoglTexture *tex, + gint x_span_index, + gint y_span_index); + #endif /* __COGL_TEXTURE_H */ diff --git a/clutter/cogl/gles/cogl-texture.c b/clutter/cogl/gles/cogl-texture.c index 84d51685e..ec51c6382 100644 --- a/clutter/cogl/gles/cogl-texture.c +++ b/clutter/cogl/gles/cogl-texture.c @@ -32,6 +32,7 @@ #include "cogl-util.h" #include "cogl-bitmap.h" #include "cogl-texture-private.h" +#include "cogl-material.h" #include "cogl-context.h" #include "cogl-handle.h" @@ -41,13 +42,6 @@ #include #include -#define glVertexPointer cogl_wrap_glVertexPointer -#define glTexCoordPointer cogl_wrap_glTexCoordPointer -#define glColorPointer cogl_wrap_glColorPointer -#define glDrawArrays cogl_wrap_glDrawArrays -#define glDrawElements cogl_wrap_glDrawElements -#define glTexParameteri cogl_wrap_glTexParameteri - /* #define COGL_DEBUG 1 @@ -59,6 +53,25 @@ printf("err: 0x%x\n", err); \ } */ +#ifdef HAVE_COGL_GL + +#define glDrawRangeElements ctx->pf_glDrawRangeElements + +#else + +/* GLES doesn't have glDrawRangeElements, so we simply pretend it does + * but that it makes no use of the start, end constraints: */ +#define glDrawRangeElements(mode, start, end, count, type, indices) \ + glDrawElements (mode, count, type, indices) + +#endif + +static void _cogl_journal_flush (void); + +static void _cogl_texture_free (CoglTexture *tex); + +COGL_HANDLE_DEFINE (Texture, texture, texture_handles); + struct _CoglSpanIter { gint index; @@ -76,22 +89,12 @@ struct _CoglSpanIter gboolean intersects; }; -static void _cogl_texture_free (CoglTexture *tex); - -COGL_HANDLE_DEFINE (Texture, texture, texture_handles); - -CoglHandle -_cogl_texture_handle_from_pointer (CoglTexture *tex) -{ - return (CoglHandle) tex; -} - static void _cogl_texture_bitmap_free (CoglTexture *tex) { if (tex->bitmap.data != NULL && tex->bitmap_owner) g_free (tex->bitmap.data); - + tex->bitmap.data = NULL; tex->bitmap_owner = FALSE; } @@ -102,7 +105,7 @@ _cogl_texture_bitmap_swap (CoglTexture *tex, { if (tex->bitmap.data != NULL && tex->bitmap_owner) g_free (tex->bitmap.data); - + tex->bitmap = *new_bitmap; tex->bitmap_owner = TRUE; } @@ -114,11 +117,11 @@ _cogl_span_iter_update (CoglSpanIter *iter) iter->span = &g_array_index (iter->array, CoglTexSliceSpan, iter->index); - + /* Offset next position by span size */ iter->next_pos = iter->pos + (float)(iter->span->size - iter->span->waste); - + /* Check if span intersects the area to cover */ if (iter->next_pos <= iter->cover_start || iter->pos >= iter->cover_end) @@ -127,15 +130,15 @@ _cogl_span_iter_update (CoglSpanIter *iter) iter->intersects = FALSE; return; } - + iter->intersects = TRUE; - + /* Clip start position to coverage area */ if (iter->pos < iter->cover_start) iter->intersect_start = iter->cover_start; else iter->intersect_start = iter->pos; - + /* Clip end position to coverage area */ if (iter->next_pos > iter->cover_end) iter->intersect_end = iter->cover_end; @@ -158,7 +161,7 @@ _cogl_span_iter_begin (CoglSpanIter *iter, iter->cover_start = cover_start; iter->cover_end = cover_end; iter->pos = iter->origin; - + /* Update intersection */ _cogl_span_iter_update (iter); } @@ -168,10 +171,10 @@ _cogl_span_iter_next (CoglSpanIter *iter) { /* Move current position */ iter->pos = iter->next_pos; - + /* Pick next slice (wrap when last reached) */ iter->index = (iter->index + 1) % iter->array->len; - + /* Update intersection */ _cogl_span_iter_update (iter); } @@ -189,7 +192,7 @@ prep_for_gl_pixels_upload (gint pixels_rowstride, gint pixels_src_y, gint pixels_bpp) { - + if (!(pixels_rowstride & 0x7)) GE( glPixelStorei (GL_UNPACK_ALIGNMENT, 8) ); else if (!(pixels_rowstride & 0x3)) @@ -252,7 +255,7 @@ _cogl_texture_upload_to_gl (CoglTexture *tex) gint x,y; guchar *waste_buf; CoglBitmap slice_bmp; - + bpp = _cogl_get_format_bpp (tex->bitmap.format); waste_buf = _cogl_texture_allocate_waste_buffer (tex); @@ -261,16 +264,16 @@ _cogl_texture_upload_to_gl (CoglTexture *tex) for (y = 0; y < tex->slice_y_spans->len; ++y) { y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, y); - + /* Iterate horizontal slices */ for (x = 0; x < tex->slice_x_spans->len; ++x) { x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, x); - + /* Pick the gl texture object handle */ gl_handle = g_array_index (tex->slice_gl_handles, GLuint, y * tex->slice_x_spans->len + x); - + /* FIXME: might optimize by not copying to intermediate slice bitmap when source rowstride = bpp * width and the texture image is not sliced */ @@ -288,7 +291,7 @@ _cogl_texture_upload_to_gl (CoglTexture *tex) 0, 0, bpp); - + /* Copy subregion data */ _cogl_bitmap_copy_subregion (&tex->bitmap, &slice_bmp, @@ -297,7 +300,7 @@ _cogl_texture_upload_to_gl (CoglTexture *tex) 0, 0, slice_bmp.width, slice_bmp.height); - + /* Upload new image data */ GE( cogl_gles2_wrapper_bind_texture (tex->gl_target, gl_handle, tex->gl_intformat) ); @@ -380,7 +383,7 @@ _cogl_texture_upload_to_gl (CoglTexture *tex) if (tex->auto_mipmap) cogl_wrap_glGenerateMipmap (tex->gl_target); - + /* Free temp bitmap */ g_free (slice_bmp.data); } @@ -395,119 +398,81 @@ _cogl_texture_upload_to_gl (CoglTexture *tex) static void _cogl_texture_draw_and_read (CoglTexture *tex, CoglBitmap *target_bmp, - CoglColor *back_color, GLint *viewport) { - gint bpp; + gint bpp; float rx1, ry1; float rx2, ry2; float tx1, ty1; float tx2, ty2; - int bw, bh; - CoglBitmap rect_bmp; - CoglHandle handle; - + int bw, bh; + CoglBitmap rect_bmp; + CoglHandle handle; + handle = _cogl_texture_handle_from_pointer (tex); bpp = _cogl_get_format_bpp (COGL_PIXEL_FORMAT_RGBA_8888); - - /* If whole image fits into the viewport and target buffer - has got no special rowstride, we can do it in one pass */ - if (tex->bitmap.width < viewport[2] - viewport[0] && - tex->bitmap.height < viewport[3] - viewport[1] && - tex->bitmap.rowstride == bpp * tex->bitmap.width) + + ry1 = 0; ry2 = 0; + ty1 = 0; ty2 = 0; + + /* Walk Y axis until whole bitmap height consumed */ + for (bh = tex->bitmap.height; bh > 0; bh -= viewport[3]) { - /* Clear buffer with transparent black, draw with white - for direct copy to framebuffer */ - cogl_paint_init (back_color); - - /* Draw the texture image */ - cogl_texture_rectangle (handle, - 0, 0, - (float)(tex->bitmap.width), - (float)(tex->bitmap.height), - 0, 0, 1.0, 1.0); - - /* Read into target bitmap */ - prep_for_gl_pixels_download (tex->bitmap.rowstride); - GE( glReadPixels (viewport[0], viewport[1], - tex->bitmap.width, - tex->bitmap.height, - GL_RGBA, GL_UNSIGNED_BYTE, - target_bmp->data) ); - } - else - { - ry1 = 0; ry2 = 0; - ty1 = 0; ty2 = 0; - -#define CFIX (float) - - /* Walk Y axis until whole bitmap height consumed */ - for (bh = tex->bitmap.height; bh > 0; bh -= viewport[3]) + /* Rectangle Y coords */ + ry1 = ry2; + ry2 += (bh < viewport[3]) ? bh : viewport[3]; + + /* Normalized texture Y coords */ + ty1 = ty2; + ty2 = (ry2 / (float)tex->bitmap.height); + + rx1 = 0; rx2 = 0; + tx1 = 0; tx2 = 0; + + /* Walk X axis until whole bitmap width consumed */ + for (bw = tex->bitmap.width; bw > 0; bw-=viewport[2]) { - /* Rectangle Y coords */ - ry1 = ry2; - ry2 += (bh < viewport[3]) ? bh : viewport[3]; - - /* Normalized texture Y coords */ - ty1 = ty2; - ty2 = (CFIX (ry2) / CFIX (tex->bitmap.height)); - - rx1 = 0; rx2 = 0; - tx1 = 0; tx2 = 0; - - /* Walk X axis until whole bitmap width consumed */ - for (bw = tex->bitmap.width; bw > 0; bw-=viewport[2]) - { - /* Rectangle X coords */ - rx1 = rx2; - rx2 += (bw < viewport[2]) ? bw : viewport[2]; - - /* Normalized texture X coords */ - tx1 = tx2; - tx2 = (CFIX (rx2) / CFIX (tex->bitmap.width)); - - /* Clear buffer with transparent black, draw with white - for direct copy to framebuffer */ - cogl_paint_init (back_color); - - /* Draw a portion of texture */ - cogl_texture_rectangle (handle, - 0, 0, - CFIX (rx2 - rx1), - CFIX (ry2 - ry1), - tx1, ty1, - tx2, ty2); - - /* Read into a temporary bitmap */ - rect_bmp.format = COGL_PIXEL_FORMAT_RGBA_8888; - rect_bmp.width = rx2 - rx1; - rect_bmp.height = ry2 - ry1; - rect_bmp.rowstride = bpp * rect_bmp.width; - rect_bmp.data = (guchar*) g_malloc (rect_bmp.rowstride * - rect_bmp.height); - - prep_for_gl_pixels_download (rect_bmp.rowstride); - GE( glReadPixels (viewport[0], viewport[1], - rect_bmp.width, - rect_bmp.height, - GL_RGBA, GL_UNSIGNED_BYTE, - rect_bmp.data) ); - - /* Copy to target bitmap */ - _cogl_bitmap_copy_subregion (&rect_bmp, - target_bmp, - 0,0, - rx1,ry1, - rect_bmp.width, - rect_bmp.height); - - /* Free temp bitmap */ - g_free (rect_bmp.data); - } + /* Rectangle X coords */ + rx1 = rx2; + rx2 += (bw < viewport[2]) ? bw : viewport[2]; + + /* Normalized texture X coords */ + tx1 = tx2; + tx2 = (rx2 / (float)tex->bitmap.width); + + /* Draw a portion of texture */ + cogl_rectangle_with_texture_coords (0, 0, + rx2 - rx1, + ry2 - ry1, + tx1, ty1, + tx2, ty2); + + /* Read into a temporary bitmap */ + rect_bmp.format = COGL_PIXEL_FORMAT_RGBA_8888; + rect_bmp.width = rx2 - rx1; + rect_bmp.height = ry2 - ry1; + rect_bmp.rowstride = bpp * rect_bmp.width; + rect_bmp.data = (guchar*) g_malloc (rect_bmp.rowstride * + rect_bmp.height); + + prep_for_gl_pixels_download (rect_bmp.rowstride); + GE( glReadPixels (viewport[0], viewport[1], + rect_bmp.width, + rect_bmp.height, + GL_RGBA, GL_UNSIGNED_BYTE, + rect_bmp.data) ); + + /* Copy to target bitmap */ + _cogl_bitmap_copy_subregion (&rect_bmp, + target_bmp, + 0,0, + rx1,ry1, + rect_bmp.width, + rect_bmp.height); + + /* Free temp bitmap */ + g_free (rect_bmp.data); } - -#undef CFIX } } @@ -519,63 +484,81 @@ _cogl_texture_download_from_gl (CoglTexture *tex, { gint bpp; GLint viewport[4]; - CoglColor cwhite; CoglBitmap alpha_bmp; - COGLenum old_src_factor; - COGLenum old_dst_factor; _COGL_GET_CONTEXT (ctx, FALSE); - cogl_color_set_from_4ub (&cwhite, 0xff, 0xff, 0xff, 0xff); bpp = _cogl_get_format_bpp (COGL_PIXEL_FORMAT_RGBA_8888); - + /* Viewport needs to have some size and be inside the window for this */ - GE( cogl_wrap_glGetIntegerv (GL_VIEWPORT, viewport) ); - + GE( glGetIntegerv (GL_VIEWPORT, 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 bottom-left corner to draw the texture upside-down so we match the way glReadPixels works) */ - - GE( cogl_wrap_glMatrixMode (GL_PROJECTION) ); - GE( cogl_wrap_glPushMatrix () ); - GE( cogl_wrap_glLoadIdentity () ); - - GE( cogl_wrap_glOrthof (0, (float)(viewport[2]), + + GE( glMatrixMode (GL_PROJECTION) ); + GE( glPushMatrix () ); + GE( glLoadIdentity () ); + + GE( glOrthof (0, (float)(viewport[2]), 0, (float)(viewport[3]), (float)(0), (float)(100)) ); - - GE( cogl_wrap_glMatrixMode (GL_MODELVIEW) ); - GE( cogl_wrap_glPushMatrix () ); - GE( cogl_wrap_glLoadIdentity () ); - + + GE( glMatrixMode (GL_MODELVIEW) ); + GE( glPushMatrix () ); + GE( glLoadIdentity () ); + /* Draw to all channels */ cogl_draw_buffer (COGL_WINDOW_BUFFER | COGL_MASK_BUFFER, 0); - - /* Store old blending factors */ - old_src_factor = ctx->blend_src_factor; - old_dst_factor = ctx->blend_dst_factor; - + /* Direct copy operation */ - cogl_set_source_color (&cwhite); - cogl_blend_func (CGL_ONE, CGL_ZERO); - _cogl_texture_draw_and_read (tex, target_bmp, - &cwhite, viewport); - + + if (ctx->texture_download_material == COGL_INVALID_HANDLE) + { + ctx->texture_download_material = cogl_material_new (); + cogl_material_set_layer_combine_function ( + ctx->texture_download_material, + 0, /* layer */ + COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGB, + COGL_MATERIAL_LAYER_COMBINE_FUNC_REPLACE); + cogl_material_set_layer_combine_arg_src ( + ctx->texture_download_material, + 0, /* layer */ + 0, /* arg */ + COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGB, + COGL_MATERIAL_LAYER_COMBINE_SRC_TEXTURE); + cogl_material_set_blend_factors (ctx->texture_download_material, + COGL_MATERIAL_BLEND_FACTOR_ONE, + COGL_MATERIAL_BLEND_FACTOR_ZERO); + } + + cogl_material_set_layer (ctx->texture_download_material, 0, tex); + + cogl_material_set_layer_combine_arg_op ( + ctx->texture_download_material, + 0, /* layer */ + 0, /* arg */ + COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGB, + COGL_MATERIAL_LAYER_COMBINE_OP_SRC_COLOR); + cogl_material_flush_gl_state (ctx->texture_download_material, NULL); + _cogl_texture_draw_and_read (tex, 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( cogl_wrap_glGetIntegerv (GL_ALPHA_BITS, &a_bits) ); - GE( cogl_wrap_glGetIntegerv (GL_RED_BITS, &r_bits) ); - GE( cogl_wrap_glGetIntegerv (GL_GREEN_BITS, &g_bits) ); - GE( cogl_wrap_glGetIntegerv (GL_BLUE_BITS, &b_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); @@ -587,7 +570,7 @@ _cogl_texture_download_from_gl (CoglTexture *tex, guchar *srcpixel; guchar *dstpixel; gint x,y; - + /* Create temp bitmap for alpha values */ alpha_bmp.format = COGL_PIXEL_FORMAT_RGBA_8888; alpha_bmp.width = target_bmp->width; @@ -595,16 +578,21 @@ _cogl_texture_download_from_gl (CoglTexture *tex, alpha_bmp.rowstride = bpp * alpha_bmp.width; alpha_bmp.data = (guchar*) g_malloc (alpha_bmp.rowstride * alpha_bmp.height); - + /* Draw alpha values into RGB channels */ - cogl_blend_func (CGL_ZERO, CGL_SRC_ALPHA); - _cogl_texture_draw_and_read (tex, &alpha_bmp, - &cwhite, viewport); - + cogl_material_set_layer_combine_arg_op ( + ctx->texture_download_material, + 0, /* layer */ + 0, /* arg */ + COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGB, + COGL_MATERIAL_LAYER_COMBINE_OP_SRC_ALPHA); + cogl_material_flush_gl_state (ctx->texture_download_material, NULL); + _cogl_texture_draw_and_read (tex, &alpha_bmp, viewport); + /* Copy temp R to target A */ srcdata = alpha_bmp.data; dstdata = target_bmp->data; - + for (y=0; yheight; ++y) { for (x=0; xwidth; ++x) @@ -616,19 +604,18 @@ _cogl_texture_download_from_gl (CoglTexture *tex, srcdata += alpha_bmp.rowstride; dstdata += target_bmp->rowstride; } - + g_free (alpha_bmp.data); } - + /* Restore old state */ - cogl_wrap_glMatrixMode (GL_PROJECTION); - cogl_wrap_glPopMatrix (); - cogl_wrap_glMatrixMode (GL_MODELVIEW); - cogl_wrap_glPopMatrix (); - + glMatrixMode (GL_PROJECTION); + glPopMatrix (); + glMatrixMode (GL_MODELVIEW); + glPopMatrix (); + cogl_draw_buffer (COGL_WINDOW_BUFFER, 0); - cogl_blend_func (old_src_factor, old_dst_factor); - + return TRUE; } @@ -655,7 +642,7 @@ _cogl_texture_upload_subregion_to_gl (CoglTexture *tex, gint local_x = 0, local_y = 0; guchar *waste_buf; CoglBitmap slice_bmp; - + bpp = _cogl_get_format_bpp (source_bmp->format); waste_buf = _cogl_texture_allocate_waste_buffer (tex); @@ -665,9 +652,9 @@ _cogl_texture_upload_subregion_to_gl (CoglTexture *tex, _cogl_span_iter_begin (&y_iter, tex->slice_y_spans, 0, (float)(dst_y), (float)(dst_y + height)); - + !_cogl_span_iter_end (&y_iter); - + _cogl_span_iter_next (&y_iter), source_y += inter_h ) { @@ -686,9 +673,9 @@ _cogl_texture_upload_subregion_to_gl (CoglTexture *tex, _cogl_span_iter_begin (&x_iter, tex->slice_x_spans, 0, (float)(dst_x), (float)(dst_x + width)); - + !_cogl_span_iter_end (&x_iter); - + _cogl_span_iter_next (&x_iter), source_x += inter_w ) { @@ -707,22 +694,22 @@ _cogl_texture_upload_subregion_to_gl (CoglTexture *tex, x_iter.intersect_start); inter_h = (y_iter.intersect_end - y_iter.intersect_start); - + /* Localize intersection top-left corner to slice*/ local_x = (x_iter.intersect_start - x_iter.pos); local_y = (y_iter.intersect_start - y_iter.pos); - + /* Pick slice GL handle */ gl_handle = g_array_index (tex->slice_gl_handles, GLuint, y_iter.index * tex->slice_x_spans->len + x_iter.index); - + /* FIXME: might optimize by not copying to intermediate slice bitmap when source rowstride = bpp * width and the texture image is not sliced */ - + /* Setup temp bitmap for slice subregion */ slice_bmp.format = tex->bitmap.format; slice_bmp.width = inter_w; @@ -730,13 +717,13 @@ _cogl_texture_upload_subregion_to_gl (CoglTexture *tex, slice_bmp.rowstride = bpp * slice_bmp.width; slice_bmp.data = (guchar*) g_malloc (slice_bmp.rowstride * slice_bmp.height); - + /* Setup gl alignment to match rowstride and top-left corner */ prep_for_gl_pixels_upload (slice_bmp.rowstride, 0, /* src x */ 0, /* src y */ bpp); - + /* Copy subregion data */ _cogl_bitmap_copy_subregion (source_bmp, &slice_bmp, @@ -745,11 +732,11 @@ _cogl_texture_upload_subregion_to_gl (CoglTexture *tex, 0, 0, slice_bmp.width, slice_bmp.height); - + /* Upload new image data */ GE( cogl_gles2_wrapper_bind_texture (tex->gl_target, gl_handle, tex->gl_intformat) ); - + GE( glTexSubImage2D (tex->gl_target, 0, local_x, local_y, inter_w, inter_h, @@ -877,12 +864,12 @@ _cogl_rect_slices_for_size (gint size_to_fill, { gint n_spans = 0; CoglTexSliceSpan span; - + /* Init first slice span */ span.start = 0; span.size = max_span_size; span.waste = 0; - + /* Repeat until whole area covered */ while (size_to_fill >= span.size) { @@ -892,7 +879,7 @@ _cogl_rect_slices_for_size (gint size_to_fill, size_to_fill -= span.size; n_spans++; } - + /* Add one last smaller slice span */ if (size_to_fill > 0) { @@ -900,7 +887,7 @@ _cogl_rect_slices_for_size (gint size_to_fill, if (out_spans) g_array_append_val (out_spans, span); n_spans++; } - + return n_spans; } @@ -912,15 +899,15 @@ _cogl_pot_slices_for_size (gint size_to_fill, { gint n_spans = 0; CoglTexSliceSpan span; - + /* Init first slice span */ span.start = 0; span.size = max_span_size; span.waste = 0; - + /* Fix invalid max_waste */ if (max_waste < 0) max_waste = 0; - + while (TRUE) { /* Is the whole area covered? */ @@ -949,7 +936,7 @@ _cogl_pot_slices_for_size (gint size_to_fill, } } } - + /* Can't get here */ return 0; } @@ -964,9 +951,36 @@ _cogl_texture_size_supported (GLenum gl_target, return TRUE; } +static void +_cogl_texture_set_wrap_mode_parameter (CoglTexture *tex, + GLenum wrap_mode) +{ + /* Only set the wrap mode if it's different from the current + value to avoid too many GL calls */ + if (tex->wrap_mode != wrap_mode) + { + int i; + + /* Any queued texture rectangles may be depending on the previous + * wrap mode... */ + _cogl_journal_flush (); + + for (i = 0; i < tex->slice_gl_handles->len; i++) + { + GLuint texnum = g_array_index (tex->slice_gl_handles, GLuint, i); + + GE( glBindTexture (tex->gl_target, texnum) ); + GE( glTexParameteri (tex->gl_target, GL_TEXTURE_WRAP_S, wrap_mode) ); + GE( glTexParameteri (tex->gl_target, GL_TEXTURE_WRAP_T, wrap_mode) ); + } + + tex->wrap_mode = wrap_mode; + } +} + static gboolean _cogl_texture_slices_create (CoglTexture *tex) -{ +{ gint bpp; gint max_width; gint max_height; @@ -977,11 +991,11 @@ _cogl_texture_slices_create (CoglTexture *tex) gint x, y; CoglTexSliceSpan *x_span; CoglTexSliceSpan *y_span; - + gint (*slices_for_size) (gint, gint, gint, GArray*); - + bpp = _cogl_get_format_bpp (tex->bitmap.format); - + /* Initialize size of largest slice according to supported features */ if (cogl_features_available (COGL_FEATURE_TEXTURE_NPOT)) { @@ -997,12 +1011,12 @@ _cogl_texture_slices_create (CoglTexture *tex) tex->gl_target = GL_TEXTURE_2D; slices_for_size = _cogl_pot_slices_for_size; } - + /* Negative number means no slicing forced by the user */ if (tex->max_waste <= -1) { CoglTexSliceSpan span; - + /* Check if size supported else bail out */ if (!_cogl_texture_size_supported (tex->gl_target, tex->gl_format, @@ -1012,25 +1026,25 @@ _cogl_texture_slices_create (CoglTexture *tex) { return FALSE; } - + n_x_slices = 1; n_y_slices = 1; - - /* Init span arrays */ + + /* Init span arrays */ tex->slice_x_spans = g_array_sized_new (FALSE, FALSE, sizeof (CoglTexSliceSpan), 1); - + tex->slice_y_spans = g_array_sized_new (FALSE, FALSE, sizeof (CoglTexSliceSpan), 1); - + /* Add a single span for width and height */ span.start = 0; span.size = max_width; span.waste = max_width - tex->bitmap.width; g_array_append_val (tex->slice_x_spans, span); - + span.size = max_height; span.waste = max_height - tex->bitmap.height; g_array_append_val (tex->slice_y_spans, span); @@ -1049,73 +1063,68 @@ _cogl_texture_slices_create (CoglTexture *tex) max_width /= 2; else max_height /= 2; - + if (max_width == 0 || max_height == 0) return FALSE; } - + /* Determine the slices required to cover the bitmap area */ n_x_slices = slices_for_size (tex->bitmap.width, max_width, tex->max_waste, NULL); - + n_y_slices = slices_for_size (tex->bitmap.height, max_height, tex->max_waste, NULL); - + /* Init span arrays with reserved size */ tex->slice_x_spans = g_array_sized_new (FALSE, FALSE, sizeof (CoglTexSliceSpan), n_x_slices); - + tex->slice_y_spans = g_array_sized_new (FALSE, FALSE, sizeof (CoglTexSliceSpan), n_y_slices); - + /* Fill span arrays with info */ slices_for_size (tex->bitmap.width, max_width, tex->max_waste, tex->slice_x_spans); - + slices_for_size (tex->bitmap.height, max_height, tex->max_waste, tex->slice_y_spans); } - + /* Init and resize GL handle array */ n_slices = n_x_slices * n_y_slices; - + tex->slice_gl_handles = g_array_sized_new (FALSE, FALSE, sizeof (GLuint), n_slices); - + g_array_set_size (tex->slice_gl_handles, n_slices); - - - /* Hardware repeated tiling if supported, else tile in software*/ - if (cogl_features_available (COGL_FEATURE_TEXTURE_NPOT) - && n_slices == 1) - tex->wrap_mode = GL_REPEAT; - else - tex->wrap_mode = GL_CLAMP_TO_EDGE; - + + /* Wrap mode not yet set */ + tex->wrap_mode = GL_FALSE; + /* Generate a "working set" of GL texture objects * (some implementations might supported faster * re-binding between textures inside a set) */ gl_handles = (GLuint*) tex->slice_gl_handles->data; - + GE( glGenTextures (n_slices, gl_handles) ); - - + + /* Init each GL texture object */ for (y = 0; y < n_y_slices; ++y) { y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, y); - + for (x = 0; x < n_x_slices; ++x) { x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, x); - + #if COGL_DEBUG printf ("CREATE SLICE (%d,%d)\n", x,y); printf ("size: (%d x %d)\n", @@ -1135,7 +1144,7 @@ _cogl_texture_slices_create (CoglTexture *tex) tex->wrap_mode) ); GE( glTexParameteri (tex->gl_target, GL_TEXTURE_WRAP_T, tex->wrap_mode) ); - + if (tex->auto_mipmap) GE( glTexParameteri (tex->gl_target, GL_GENERATE_MIPMAP, GL_TRUE) ); @@ -1146,19 +1155,19 @@ _cogl_texture_slices_create (CoglTexture *tex) tex->gl_format, tex->gl_type, 0) ); } } - + return TRUE; } static void _cogl_texture_slices_free (CoglTexture *tex) -{ +{ if (tex->slice_x_spans != NULL) g_array_free (tex->slice_x_spans, TRUE); - + if (tex->slice_y_spans != NULL) g_array_free (tex->slice_y_spans, TRUE); - + if (tex->slice_gl_handles != NULL) { if (tex->is_foreign == FALSE) @@ -1166,11 +1175,25 @@ _cogl_texture_slices_free (CoglTexture *tex) GE( glDeleteTextures (tex->slice_gl_handles->len, (GLuint*) tex->slice_gl_handles->data) ); } - + g_array_free (tex->slice_gl_handles, TRUE); } } +gboolean +_cogl_texture_span_has_waste (CoglTexture *tex, + gint x_span_index, + gint y_span_index) +{ + CoglTexSliceSpan *x_span; + CoglTexSliceSpan *y_span; + + x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, x_span_index); + y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, y_span_index); + + return (x_span->waste || y_span->waste) ? TRUE : FALSE; +} + static gboolean _cogl_pixel_format_from_gl_internal (GLenum gl_int_format, CoglPixelFormat *out_format) @@ -1188,17 +1211,17 @@ _cogl_pixel_format_to_gl (CoglPixelFormat format, GLenum glintformat = 0; GLenum glformat = 0; GLenum gltype = 0; - + /* No premultiplied formats accepted by GL * (FIXME: latest hardware?) */ - + if (format & COGL_PREMULT_BIT) format = (format & COGL_UNPREMULT_MASK); - + /* Everything else accepted * (FIXME: check YUV support) */ required_format = format; - + /* Find GL equivalents */ switch (format) { @@ -1212,7 +1235,7 @@ _cogl_pixel_format_to_gl (CoglPixelFormat format, glformat = GL_LUMINANCE; gltype = GL_UNSIGNED_BYTE; break; - + /* Just one 24-bit ordering supported */ case COGL_PIXEL_FORMAT_RGB_888: case COGL_PIXEL_FORMAT_BGR_888: @@ -1221,7 +1244,7 @@ _cogl_pixel_format_to_gl (CoglPixelFormat format, gltype = GL_UNSIGNED_BYTE; required_format = COGL_PIXEL_FORMAT_RGB_888; break; - + /* Just one 32-bit ordering supported */ case COGL_PIXEL_FORMAT_RGBA_8888: case COGL_PIXEL_FORMAT_BGRA_8888: @@ -1232,7 +1255,7 @@ _cogl_pixel_format_to_gl (CoglPixelFormat format, gltype = GL_UNSIGNED_BYTE; required_format = COGL_PIXEL_FORMAT_RGBA_8888; break; - + /* The following three types of channel ordering * are always defined using system word byte * ordering (even according to GLES spec) */ @@ -1251,19 +1274,19 @@ _cogl_pixel_format_to_gl (CoglPixelFormat format, glformat = GL_RGBA; gltype = GL_UNSIGNED_SHORT_5_5_5_1; break; - + /* FIXME: check extensions for YUV support */ default: break; } - + if (out_glintformat != NULL) *out_glintformat = glintformat; if (out_glformat != NULL) *out_glformat = glformat; if (out_gltype != NULL) *out_gltype = gltype; - + return required_format; } @@ -1274,7 +1297,7 @@ _cogl_texture_bitmap_prepare (CoglTexture *tex, CoglBitmap new_bitmap; CoglPixelFormat new_data_format; gboolean success; - + /* Was there any internal conversion requested? */ if (internal_format == COGL_PIXEL_FORMAT_ANY) internal_format = tex->bitmap.format; @@ -1284,21 +1307,21 @@ _cogl_texture_bitmap_prepare (CoglTexture *tex, &tex->gl_intformat, &tex->gl_format, &tex->gl_type); - + /* Convert to internal format */ if (new_data_format != tex->bitmap.format) { success = _cogl_bitmap_convert_and_premult (&tex->bitmap, &new_bitmap, new_data_format); - + if (!success) return FALSE; - + /* Update texture with new data */ _cogl_texture_bitmap_swap (tex, &new_bitmap); } - + return TRUE; } @@ -1313,24 +1336,24 @@ _cogl_texture_free (CoglTexture *tex) } CoglHandle -cogl_texture_new_with_size (guint width, - guint height, - gint max_waste, - CoglTextureFlags flags, - CoglPixelFormat internal_format) +cogl_texture_new_with_size (guint width, + guint height, + gint max_waste, + CoglTextureFlags flags, + CoglPixelFormat internal_format) { CoglTexture *tex; gint bpp; gint rowstride; - + /* Since no data, we need some internal format */ if (internal_format == COGL_PIXEL_FORMAT_ANY) return COGL_INVALID_HANDLE; - + /* Rowstride from width */ bpp = _cogl_get_format_bpp (internal_format); rowstride = width * bpp; - + /* Init texture with empty bitmap */ tex = (CoglTexture*) g_malloc (sizeof (CoglTexture)); @@ -1339,36 +1362,36 @@ cogl_texture_new_with_size (guint width, tex->is_foreign = FALSE; tex->auto_mipmap = ((flags & COGL_TEXTURE_AUTO_MIPMAP) != 0); - + tex->bitmap.width = width; tex->bitmap.height = height; tex->bitmap.format = internal_format; tex->bitmap.rowstride = rowstride; tex->bitmap.data = NULL; tex->bitmap_owner = FALSE; - + tex->slice_x_spans = NULL; tex->slice_y_spans = NULL; tex->slice_gl_handles = NULL; - + tex->max_waste = max_waste; tex->min_filter = CGL_NEAREST; tex->mag_filter = CGL_NEAREST; - + /* Find closest GL format match */ tex->bitmap.format = _cogl_pixel_format_to_gl (internal_format, &tex->gl_intformat, &tex->gl_format, &tex->gl_type); - + /* Create slices for the given format and size */ if (!_cogl_texture_slices_create (tex)) { _cogl_texture_free (tex); return COGL_INVALID_HANDLE; } - + return _cogl_texture_handle_new (tex); } @@ -1384,20 +1407,20 @@ cogl_texture_new_from_data (guint width, { CoglTexture *tex; gint bpp; - + if (format == COGL_PIXEL_FORMAT_ANY) return COGL_INVALID_HANDLE; - + if (data == NULL) return COGL_INVALID_HANDLE; - + /* Rowstride from width if not given */ bpp = _cogl_get_format_bpp (format); if (rowstride == 0) rowstride = width * bpp; - + /* Create new texture and fill with given data */ tex = (CoglTexture*) g_malloc (sizeof (CoglTexture)); - + tex->ref_count = 1; COGL_HANDLE_DEBUG_NEW (texture, tex); @@ -1410,40 +1433,40 @@ cogl_texture_new_from_data (guint width, tex->bitmap.format = format; tex->bitmap.rowstride = rowstride; tex->bitmap_owner = FALSE; - + tex->slice_x_spans = NULL; tex->slice_y_spans = NULL; tex->slice_gl_handles = NULL; - + tex->max_waste = max_waste; tex->min_filter = CGL_NEAREST; tex->mag_filter = CGL_NEAREST; - + /* FIXME: If upload fails we should set some kind of * error flag but still return texture handle (this * is to keep the behavior equal to _new_from_file; * see below) */ - + if (!_cogl_texture_bitmap_prepare (tex, internal_format)) { _cogl_texture_free (tex); return COGL_INVALID_HANDLE; } - + if (!_cogl_texture_slices_create (tex)) { _cogl_texture_free (tex); return COGL_INVALID_HANDLE; } - + if (!_cogl_texture_upload_to_gl (tex)) { _cogl_texture_free (tex); return COGL_INVALID_HANDLE; } - + _cogl_texture_bitmap_free (tex); - + return _cogl_texture_handle_new (tex); } @@ -1457,25 +1480,25 @@ cogl_texture_new_from_bitmap (CoglBitmap *bmp, /* Create new texture and fill with loaded data */ tex = (CoglTexture*) g_malloc ( sizeof (CoglTexture)); - + tex->ref_count = 1; COGL_HANDLE_DEBUG_NEW (texture, tex); - + tex->is_foreign = FALSE; tex->auto_mipmap = ((flags & COGL_TEXTURE_AUTO_MIPMAP) != 0); tex->bitmap = *bmp; tex->bitmap_owner = TRUE; bmp->data = NULL; - + tex->slice_x_spans = NULL; tex->slice_y_spans = NULL; tex->slice_gl_handles = NULL; - + tex->max_waste = max_waste; tex->min_filter = CGL_NEAREST; tex->mag_filter = CGL_NEAREST; - + /* FIXME: If upload fails we should set some kind of * error flag but still return texture handle if the * user decides to destroy another texture and upload @@ -1483,40 +1506,40 @@ cogl_texture_new_from_bitmap (CoglBitmap *bmp, * in that case). As a rule then, everytime a valid * CoglHandle is returned, it should also be destroyed * with cogl_texture_unref at some point! */ - + if (!_cogl_texture_bitmap_prepare (tex, internal_format)) { _cogl_texture_free (tex); return COGL_INVALID_HANDLE; } - + if (!_cogl_texture_slices_create (tex)) { _cogl_texture_free (tex); return COGL_INVALID_HANDLE; } - + if (!_cogl_texture_upload_to_gl (tex)) { _cogl_texture_free (tex); return COGL_INVALID_HANDLE; } - + _cogl_texture_bitmap_free (tex); - + return _cogl_texture_handle_new (tex); } CoglHandle cogl_texture_new_from_file (const gchar *filename, - gint max_waste, + gint max_waste, CoglTextureFlags flags, - CoglPixelFormat internal_format, - GError **error) + CoglPixelFormat internal_format, + GError **error) { CoglBitmap *bmp; CoglHandle handle; - + g_return_val_if_fail (error == NULL || *error == NULL, COGL_INVALID_HANDLE); if (!(bmp = cogl_bitmap_new_from_file (filename, error))) @@ -1546,7 +1569,7 @@ cogl_texture_new_from_foreign (GLuint gl_handle, robustness and for completeness in case one day GLES gains support for them. */ - + GLenum gl_error = 0; GLboolean gl_istexture; GLint gl_compressed = GL_FALSE; @@ -1560,39 +1583,39 @@ cogl_texture_new_from_foreign (GLuint gl_handle, CoglTexture *tex; CoglTexSliceSpan x_span; CoglTexSliceSpan y_span; - + /* Allow 2-dimensional textures only */ if (gl_target != GL_TEXTURE_2D) return COGL_INVALID_HANDLE; - + /* Make sure it is a valid GL texture object */ gl_istexture = glIsTexture (gl_handle); if (gl_istexture == GL_FALSE) return COGL_INVALID_HANDLE; - + /* Make sure binding succeeds */ gl_error = glGetError (); glBindTexture (gl_target, gl_handle); if (glGetError () != GL_NO_ERROR) return COGL_INVALID_HANDLE; - + /* Obtain texture parameters (only level 0 we are interested in) */ - + #if HAVE_COGL_GL GE( glGetTexLevelParameteriv (gl_target, 0, GL_TEXTURE_COMPRESSED, &gl_compressed) ); - + GE( glGetTexLevelParameteriv (gl_target, 0, GL_TEXTURE_INTERNAL_FORMAT, &gl_int_format) ); - + GE( glGetTexLevelParameteriv (gl_target, 0, GL_TEXTURE_WIDTH, &gl_width) ); - + GE( glGetTexLevelParameteriv (gl_target, 0, GL_TEXTURE_HEIGHT, &gl_height) ); @@ -1604,104 +1627,90 @@ cogl_texture_new_from_foreign (GLuint gl_handle, GE( glGetTexParameteriv (gl_target, GL_TEXTURE_MIN_FILTER, &gl_min_filter) ); - + GE( glGetTexParameteriv (gl_target, GL_TEXTURE_MAG_FILTER, &gl_mag_filter) ); - + GE( glGetTexParameteriv (gl_target, GL_GENERATE_MIPMAP, &gl_gen_mipmap) ); - + /* Validate width and height */ if (gl_width <= 0 || gl_height <= 0) return COGL_INVALID_HANDLE; - + /* Validate pot waste */ if (x_pot_waste < 0 || x_pot_waste >= gl_width || y_pot_waste < 0 || y_pot_waste >= gl_height) return COGL_INVALID_HANDLE; - + /* Compressed texture images not supported */ if (gl_compressed == GL_TRUE) return COGL_INVALID_HANDLE; - + /* Try and match to a cogl format */ if (!_cogl_pixel_format_from_gl_internal (gl_int_format, &format)) { return COGL_INVALID_HANDLE; } - + /* Create new texture */ tex = (CoglTexture*) g_malloc ( sizeof (CoglTexture)); - + tex->ref_count = 1; COGL_HANDLE_DEBUG_NEW (texture, tex); - + /* Setup bitmap info */ tex->is_foreign = TRUE; tex->auto_mipmap = (gl_gen_mipmap == GL_TRUE) ? TRUE : FALSE; - + bpp = _cogl_get_format_bpp (format); tex->bitmap.format = format; tex->bitmap.width = gl_width - x_pot_waste; tex->bitmap.height = gl_height - y_pot_waste; tex->bitmap.rowstride = tex->bitmap.width * bpp; tex->bitmap_owner = FALSE; - + tex->gl_target = gl_target; tex->gl_intformat = gl_int_format; tex->gl_format = gl_int_format; tex->gl_type = GL_UNSIGNED_BYTE; - + tex->min_filter = gl_min_filter; tex->mag_filter = gl_mag_filter; tex->max_waste = 0; - + + /* Wrap mode not yet set */ + tex->wrap_mode = GL_FALSE; + /* Create slice arrays */ tex->slice_x_spans = g_array_sized_new (FALSE, FALSE, sizeof (CoglTexSliceSpan), 1); - + tex->slice_y_spans = g_array_sized_new (FALSE, FALSE, sizeof (CoglTexSliceSpan), 1); - + tex->slice_gl_handles = g_array_sized_new (FALSE, FALSE, sizeof (GLuint), 1); - + /* Store info for a single slice */ x_span.start = 0; x_span.size = gl_width; x_span.waste = x_pot_waste; g_array_append_val (tex->slice_x_spans, x_span); - + y_span.start = 0; y_span.size = gl_height; y_span.waste = y_pot_waste; g_array_append_val (tex->slice_y_spans, y_span); - + g_array_append_val (tex->slice_gl_handles, gl_handle); - - /* Force appropriate wrap parameter */ - if (cogl_features_available (COGL_FEATURE_TEXTURE_NPOT) && - gl_target == GL_TEXTURE_2D) - { - /* Hardware repeated tiling */ - tex->wrap_mode = GL_REPEAT; - GE( glTexParameteri (tex->gl_target, GL_TEXTURE_WRAP_S, GL_REPEAT) ); - GE( glTexParameteri (tex->gl_target, GL_TEXTURE_WRAP_T, GL_REPEAT) ); - } - else - { - /* Any tiling will be done in software */ - tex->wrap_mode = GL_CLAMP_TO_EDGE; - GE( glTexParameteri (tex->gl_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) ); - GE( glTexParameteri (tex->gl_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) ); - } - + return _cogl_texture_handle_new (tex); } @@ -1709,12 +1718,12 @@ guint cogl_texture_get_width (CoglHandle handle) { CoglTexture *tex; - + if (!cogl_is_texture (handle)) return 0; - + tex = _cogl_texture_pointer_from_handle (handle); - + return tex->bitmap.width; } @@ -1722,12 +1731,12 @@ guint cogl_texture_get_height (CoglHandle handle) { CoglTexture *tex; - + if (!cogl_is_texture (handle)) return 0; - + tex = _cogl_texture_pointer_from_handle (handle); - + return tex->bitmap.height; } @@ -1735,12 +1744,12 @@ CoglPixelFormat cogl_texture_get_format (CoglHandle handle) { CoglTexture *tex; - + if (!cogl_is_texture (handle)) return COGL_PIXEL_FORMAT_ANY; - + tex = _cogl_texture_pointer_from_handle (handle); - + return tex->bitmap.format; } @@ -1748,12 +1757,12 @@ guint cogl_texture_get_rowstride (CoglHandle handle) { CoglTexture *tex; - + if (!cogl_is_texture (handle)) return 0; - + tex = _cogl_texture_pointer_from_handle (handle); - + return tex->bitmap.rowstride; } @@ -1761,12 +1770,12 @@ gint cogl_texture_get_max_waste (CoglHandle handle) { CoglTexture *tex; - + if (!cogl_is_texture (handle)) return 0; - + tex = _cogl_texture_pointer_from_handle (handle); - + return tex->max_waste; } @@ -1774,18 +1783,18 @@ gboolean cogl_texture_is_sliced (CoglHandle handle) { CoglTexture *tex; - + if (!cogl_is_texture (handle)) return FALSE; - + tex = _cogl_texture_pointer_from_handle (handle); - + if (tex->slice_gl_handles == NULL) return FALSE; - + if (tex->slice_gl_handles->len <= 1) return FALSE; - + return TRUE; } @@ -1795,24 +1804,24 @@ cogl_texture_get_gl_texture (CoglHandle handle, GLenum *out_gl_target) { CoglTexture *tex; - + if (!cogl_is_texture (handle)) return FALSE; - + tex = _cogl_texture_pointer_from_handle (handle); - + if (tex->slice_gl_handles == NULL) return FALSE; - + if (tex->slice_gl_handles->len < 1) return FALSE; - + if (out_gl_handle != NULL) *out_gl_handle = g_array_index (tex->slice_gl_handles, GLuint, 0); - + if (out_gl_target != NULL) *out_gl_target = tex->gl_target; - + return TRUE; } @@ -1820,12 +1829,12 @@ COGLenum cogl_texture_get_min_filter (CoglHandle handle) { CoglTexture *tex; - + if (!cogl_is_texture (handle)) return 0; - + tex = _cogl_texture_pointer_from_handle (handle); - + return tex->min_filter; } @@ -1833,12 +1842,12 @@ COGLenum cogl_texture_get_mag_filter (CoglHandle handle) { CoglTexture *tex; - + if (!cogl_is_texture (handle)) return 0; - + tex = _cogl_texture_pointer_from_handle (handle); - + return tex->mag_filter; } @@ -1850,20 +1859,20 @@ cogl_texture_set_filters (CoglHandle handle, CoglTexture *tex; GLuint gl_handle; int i; - + if (!cogl_is_texture (handle)) return; - + tex = _cogl_texture_pointer_from_handle (handle); - + /* Store new values */ tex->min_filter = min_filter; tex->mag_filter = mag_filter; - + /* Make sure slices were created */ if (tex->slice_gl_handles == NULL) return; - + /* Apply new filters to every slice */ for (i=0; islice_gl_handles->len; ++i) { @@ -1899,37 +1908,37 @@ cogl_texture_set_region (CoglHandle handle, GLenum closest_gl_format; GLenum closest_gl_type; gboolean success; - + /* Check if valid texture handle */ if (!cogl_is_texture (handle)) return FALSE; - + tex = _cogl_texture_pointer_from_handle (handle); - + /* Check for valid format */ if (format == COGL_PIXEL_FORMAT_ANY) return FALSE; - + /* Shortcut out early if the image is empty */ if (width == 0 || height == 0) return TRUE; - + /* Init source bitmap */ source_bmp.width = width; source_bmp.height = height; source_bmp.format = format; source_bmp.data = (guchar*)data; - + /* Rowstride from width if none specified */ bpp = _cogl_get_format_bpp (format); source_bmp.rowstride = (rowstride == 0) ? width * bpp : rowstride; - + /* Find closest format to internal that's supported by GL */ closest_format = _cogl_pixel_format_to_gl (tex->bitmap.format, NULL, /* don't need */ &closest_gl_format, &closest_gl_type); - + /* If no direct match, convert */ if (closest_format != format) { @@ -1937,13 +1946,13 @@ cogl_texture_set_region (CoglHandle handle, success = _cogl_bitmap_convert_and_premult (&source_bmp, &temp_bmp, closest_format); - + /* Swap bitmaps if succeeded */ if (!success) return FALSE; source_bmp = temp_bmp; source_bmp_owner = TRUE; } - + /* Send data to GL */ _cogl_texture_upload_subregion_to_gl (tex, src_x, src_y, @@ -1952,11 +1961,11 @@ cogl_texture_set_region (CoglHandle handle, &source_bmp, closest_gl_format, closest_gl_type); - + /* Free data if owner */ if (source_bmp_owner) g_free (source_bmp.data); - + return TRUE; } @@ -1979,25 +1988,25 @@ cogl_texture_get_data (CoglHandle handle, guchar *src; guchar *dst; gint y; - + /* Check if valid texture handle */ if (!cogl_is_texture (handle)) return 0; - + tex = _cogl_texture_pointer_from_handle (handle); - + /* Default to internal format if none specified */ if (format == COGL_PIXEL_FORMAT_ANY) format = tex->bitmap.format; - + /* Rowstride from texture width if none specified */ bpp = _cogl_get_format_bpp (format); if (rowstride == 0) rowstride = tex->bitmap.width * bpp; - + /* Return byte size if only that requested */ byte_size = tex->bitmap.height * rowstride; if (data == NULL) return byte_size; - + /* Find closest format that's supported by GL (Can't use _cogl_pixel_format_to_gl since available formats when reading pixels on GLES are severely limited) */ @@ -2005,7 +2014,7 @@ cogl_texture_get_data (CoglHandle handle, closest_gl_format = GL_RGBA; closest_gl_type = GL_UNSIGNED_BYTE; closest_bpp = _cogl_get_format_bpp (closest_format); - + /* Is the requested format supported? */ if (closest_format == format) { @@ -2024,12 +2033,12 @@ cogl_texture_get_data (CoglHandle handle, target_bmp.data = (guchar*) g_malloc (target_bmp.height * target_bmp.rowstride); } - + /* Retrieve data from slices */ _cogl_texture_download_from_gl (tex, &target_bmp, closest_gl_format, closest_gl_type); - + /* Was intermediate used? */ if (closest_format != format) { @@ -2037,11 +2046,11 @@ cogl_texture_get_data (CoglHandle handle, success = _cogl_bitmap_convert_and_premult (&target_bmp, &new_bmp, format); - + /* Free intermediate data and return if failed */ g_free (target_bmp.data); if (!success) return 0; - + /* Copy to user buffer */ for (y = 0; y < new_bmp.height; ++y) { @@ -2049,132 +2058,321 @@ cogl_texture_get_data (CoglHandle handle, dst = data + y * rowstride; memcpy (dst, src, new_bmp.width); } - + /* Free converted data */ g_free (new_bmp.data); } - + return byte_size; } + +/****************************************************************************** + * XXX: Here ends the code that strictly implements "CoglTextures". + * + * The following consists of code for rendering rectangles and polygons. It + * might be neater to move this code somewhere else. I think everything below + * here should be implementable without access to CoglTexture internals, but + * that will at least mean exposing the cogl_span_iter_* funcs. + */ + static void -_cogl_texture_flush_vertices (void) +_cogl_journal_flush_quad_batch (CoglJournalEntry *batch_start, + gint batch_len, + GLfloat *vertex_pointer) { + int needed_indices; + gsize stride; + int i; + gulong enable_flags = 0; + guint32 disable_mask; + _COGL_GET_CONTEXT (ctx, NO_RETVAL); - if (ctx->texture_vertices->len > 0) + /* The indices are always the same sequence regardless of the vertices so we + * only need to change it if there are more vertices than ever before. */ + needed_indices = batch_len * 6; + if (needed_indices > ctx->static_indices->len) { - int needed_indices; - CoglTextureGLVertex *p - = (CoglTextureGLVertex *) ctx->texture_vertices->data; + int old_len = ctx->static_indices->len; + int vert_num = old_len / 6 * 4; + GLushort *q; - /* The indices are always the same sequence regardless of the - vertices so we only need to change it if there are more - vertices than ever before */ - needed_indices = ctx->texture_vertices->len / 4 * 6; - if (needed_indices > ctx->texture_indices->len) + /* Add two triangles for each quad to the list of + indices. That makes six new indices but two of the + vertices in the triangles are shared. */ + g_array_set_size (ctx->static_indices, needed_indices); + q = &g_array_index (ctx->static_indices, GLushort, old_len); + + for (i = old_len; + i < ctx->static_indices->len; + i += 6, vert_num += 4) { - int old_len = ctx->texture_indices->len; - int vert_num = old_len / 6 * 4; - int i; - GLushort *q; + *(q++) = vert_num + 0; + *(q++) = vert_num + 1; + *(q++) = vert_num + 3; - /* Add two triangles for each quad to the list of - indices. That makes six new indices but two of the - vertices in the triangles are shared. */ - g_array_set_size (ctx->texture_indices, needed_indices); - q = &g_array_index (ctx->texture_indices, GLushort, old_len); - - for (i = old_len; - i < ctx->texture_indices->len; - i += 6, vert_num += 4) - { - *(q++) = vert_num + 0; - *(q++) = vert_num + 1; - *(q++) = vert_num + 3; - - *(q++) = vert_num + 1; - *(q++) = vert_num + 2; - *(q++) = vert_num + 3; - } + *(q++) = vert_num + 1; + *(q++) = vert_num + 2; + *(q++) = vert_num + 3; } - - GE( glVertexPointer (2, GL_FLOAT, - sizeof (CoglTextureGLVertex), p->v ) ); - GE( glTexCoordPointer (2, GL_FLOAT, - sizeof (CoglTextureGLVertex), p->t ) ); - - GE( cogl_gles2_wrapper_bind_texture (ctx->texture_target, - ctx->texture_current, - ctx->texture_format) ); - GE( glDrawElements (GL_TRIANGLES, - needed_indices, - GL_UNSIGNED_SHORT, - ctx->texture_indices->data) ); - - g_array_set_size (ctx->texture_vertices, 0); } + + /* XXX NB: + * Our vertex data is arranged as follows: + * 4 vertices per quad: 2 GLfloats per position, + * 2 GLfloats per tex coord * n_layers + */ + stride = 2 + 2 * batch_start->n_layers; + stride *= sizeof (GLfloat); + + disable_mask = (1 << batch_start->n_layers) - 1; + disable_mask = ~disable_mask; + + cogl_material_flush_gl_state (ctx->source_material, + COGL_MATERIAL_FLUSH_FALLBACK_MASK, + batch_start->fallback_mask, + COGL_MATERIAL_FLUSH_DISABLE_MASK, + disable_mask, + /* Redundant when dealing with unsliced + * textures but does no harm... */ + COGL_MATERIAL_FLUSH_LAYER0_OVERRIDE, + batch_start->layer0_override_texture, + NULL); + + for (i = 0; i < batch_start->n_layers; i++) + { + GE (glClientActiveTexture (GL_TEXTURE0 + i)); + GE (glEnableClientState (GL_TEXTURE_COORD_ARRAY)); + GE (glTexCoordPointer (2, GL_FLOAT, stride, vertex_pointer + 2 + 2 * i)); + } + /* XXX: Without this we get a segfault with the PVR SDK. + * We should probably be doing this for cogl/gl too though. */ + for (; i < ctx->n_texcoord_arrays_enabled; i++) + { + GE (glClientActiveTexture (GL_TEXTURE0 + i)); + GE (glDisableClientState (GL_TEXTURE_COORD_ARRAY)); + } + ctx->n_texcoord_arrays_enabled = i + 1; + + /* FIXME: This api is a bit yukky, ideally it will be removed if we + * re-work the cogl_enable mechanism */ + enable_flags |= cogl_material_get_cogl_enable_flags (ctx->source_material); + + if (ctx->enable_backface_culling) + enable_flags |= COGL_ENABLE_BACKFACE_CULLING; + + enable_flags |= COGL_ENABLE_VERTEX_ARRAY; + cogl_enable (enable_flags); + + GE (glVertexPointer (2, GL_FLOAT, stride, vertex_pointer)); + + GE (glDrawRangeElements (GL_TRIANGLES, + 0, ctx->static_indices->len - 1, + 6 * batch_len, + GL_UNSIGNED_SHORT, + ctx->static_indices->data)); } static void -_cogl_texture_add_quad_vertices (GLfloat x1, GLfloat y1, - GLfloat x2, GLfloat y2, - GLfloat tx1, GLfloat ty1, - GLfloat tx2, GLfloat ty2) +_cogl_journal_flush (void) { - CoglTextureGLVertex *p; - GLushort first_vert; + GLfloat *current_vertex_pointer; + GLfloat *batch_vertex_pointer; + CoglJournalEntry *batch_start; + guint batch_len; + int i; _COGL_GET_CONTEXT (ctx, NO_RETVAL); - /* Add the four vertices of the quad to the list of queued - vertices */ - first_vert = ctx->texture_vertices->len; - g_array_set_size (ctx->texture_vertices, first_vert + 4); - p = &g_array_index (ctx->texture_vertices, CoglTextureGLVertex, first_vert); + if (ctx->journal->len == 0) + return; - p->v[0] = x1; p->v[1] = y1; - p->t[0] = tx1; p->t[1] = ty1; - p++; - p->v[0] = x1; p->v[1] = y2; - p->t[0] = tx1; p->t[1] = ty2; - p++; - p->v[0] = x2; p->v[1] = y2; - p->t[0] = tx2; p->t[1] = ty2; - p++; - p->v[0] = x2; p->v[1] = y1; - p->t[0] = tx2; p->t[1] = ty1; - p++; + /* Current non-variables / constraints: + * + * - We don't have to worry about much GL state changing between journal + * entries since currently the journal never out lasts a single call to + * _cogl_multitexture_multiple_rectangles. So the user doesn't get the + * chance to fiddle with anything. (XXX: later this will be extended at + * which point we can start logging certain state changes) + * + * - Implied from above: all entries will refer to the same material. + * + * - Although _cogl_multitexture_multiple_rectangles can cause the wrap mode + * of textures to be modified, the journal is flushed if a wrap mode is + * changed so we don't currently have to log wrap mode changes. + * + * - XXX - others? + */ + + /* TODO: "compile" the journal to find ways of batching draw calls and vertex + * data. + * + * Simple E.g. given current constraints... + * pass 0 - load all data into a single CoglVertexBuffer + * pass 1 - batch gl draw calls according to entries that use the same + * textures. + * + * We will be able to do cooler stuff here when we extend the life of + * journals beyond _cogl_multitexture_multiple_rectangles. + */ + + batch_vertex_pointer = (GLfloat *)ctx->logged_vertices->data; + batch_start = (CoglJournalEntry *)ctx->journal->data; + batch_len = 1; + + current_vertex_pointer = batch_vertex_pointer; + + for (i = 1; i < ctx->journal->len; i++) + { + CoglJournalEntry *prev_entry = + &g_array_index (ctx->journal, CoglJournalEntry, i - 1); + CoglJournalEntry *current_entry = prev_entry + 1; + gsize stride; + + /* Progress the vertex pointer */ + stride = 2 + current_entry->n_layers * 2; + current_vertex_pointer += stride; + +#warning "NB: re-enable batching" +#if 1 + /* batch rectangles using the same textures */ + if (current_entry->material == prev_entry->material && + current_entry->n_layers == prev_entry->n_layers && + current_entry->fallback_mask == prev_entry->fallback_mask && + current_entry->layer0_override_texture + == prev_entry->layer0_override_texture) + { + batch_len++; + continue; + } +#endif + + _cogl_journal_flush_quad_batch (batch_start, + batch_len, + batch_vertex_pointer); + + batch_start = current_entry; + batch_len = 1; + batch_vertex_pointer = current_vertex_pointer; + } + + /* The last batch... */ + _cogl_journal_flush_quad_batch (batch_start, + batch_len, + batch_vertex_pointer); + + + g_array_set_size (ctx->journal, 0); + g_array_set_size (ctx->logged_vertices, 0); } static void -_cogl_texture_quad_sw (CoglTexture *tex, - float x1, - float y1, - float x2, - float y2, - float tx1, - float ty1, - float tx2, - float ty2) +_cogl_journal_log_quad (float x1, + float y1, + float x2, + float y2, + CoglHandle material, + gint n_layers, + guint32 fallback_mask, + GLuint layer0_override_texture, + float *tex_coords, + guint tex_coords_len) { - CoglSpanIter iter_x , iter_y; - float tw , th; - float tqx , tqy; - float first_tx , first_ty; - float first_qx , first_qy; - float slice_tx1 , slice_ty1; - float slice_tx2 , slice_ty2; - float slice_qx1 , slice_qy1; - float slice_qx2 , slice_qy2; - GLuint gl_handle; + int stride; + int next_vert; + GLfloat *v; + int i; + int next_entry; + CoglJournalEntry *entry; + + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + /* The vertex data is logged into a seperate array in a layout that can be + * directly passed to OpenGL + */ + + /* We pack the vertex data as 2 (x,y) GLfloats folowed by 2 (tx,ty) GLfloats + * for each texture being used, E.g.: + * [X, Y, TX0, TY0, TX1, TY1, X, Y, TX0, TY0, X, Y, ...] + */ + stride = 2 + n_layers * 2; + + next_vert = ctx->logged_vertices->len; + g_array_set_size (ctx->logged_vertices, next_vert + 4 * stride); + v = &g_array_index (ctx->logged_vertices, GLfloat, next_vert); + + /* XXX: All the jumping around to fill in this strided buffer doesn't + * seem ideal. */ + + /* XXX: we could defer expanding the vertex data for GL until we come + * to flushing the journal. */ + + v[0] = x1; v[1] = y1; + v += stride; + v[0] = x1; v[1] = y2; + v += stride; + v[0] = x2; v[1] = y2; + v += stride; + v[0] = x2; v[1] = y1; + + for (i = 0; i < n_layers; i++) + { + GLfloat *t = + &g_array_index (ctx->logged_vertices, GLfloat, next_vert + 2 + 2 * i); + + t[0] = tex_coords[0]; t[1] = tex_coords[1]; + t += stride; + t[0] = tex_coords[0]; t[1] = tex_coords[3]; + t += stride; + t[0] = tex_coords[2]; t[1] = tex_coords[3]; + t += stride; + t[0] = tex_coords[2]; t[1] = tex_coords[1]; + } + + next_entry = ctx->journal->len; + g_array_set_size (ctx->journal, next_entry + 1); + entry = &g_array_index (ctx->journal, CoglJournalEntry, next_entry); + + entry->material = material; + entry->n_layers = n_layers; + entry->fallback_mask = fallback_mask; + entry->layer0_override_texture = layer0_override_texture; +} + +static void +_cogl_texture_sliced_quad (CoglTexture *tex, + CoglHandle material, + float x1, + float y1, + float x2, + float y2, + float tx1, + float ty1, + float tx2, + float ty2) +{ + CoglSpanIter iter_x , iter_y; + float tw , th; + float tqx , tqy; + float first_tx , first_ty; + float first_qx , first_qy; + float slice_tx1 , slice_ty1; + float slice_tx2 , slice_ty2; + float slice_qx1 , slice_qy1; + float slice_qx2 , slice_qy2; + GLuint gl_handle; _COGL_GET_CONTEXT (ctx, NO_RETVAL); #if COGL_DEBUG - printf("=== Drawing Tex Quad (Software Tiling Mode) ===\n"); + printf("=== Drawing Tex Quad (Sliced Mode) ===\n"); #endif + /* We can't use hardware repeat so we need to set clamp to edge + otherwise it might pull in edge pixels from the other side */ + _cogl_texture_set_wrap_mode_parameter (tex, GL_CLAMP_TO_EDGE); + /* If the texture coordinates are backwards then swap both the geometry and texture coordinates so that the texture will be flipped but we can still use the same algorithm to iterate the @@ -2201,14 +2399,14 @@ _cogl_texture_quad_sw (CoglTexture *tex, /* Scale ratio from texture to quad widths */ tw = (float)(tex->bitmap.width); th = (float)(tex->bitmap.height); - + tqx = (x2 - x1) / (tw * (tx2 - tx1)); tqy = (y2 - y1) / (th * (ty2 - ty1)); /* Integral texture coordinate for first tile */ first_tx = (float)(floorf (tx1)); first_ty = (float)(floorf (ty1)); - + /* Denormalize texture coordinates */ first_tx = (first_tx * tw); first_ty = (first_ty * th); @@ -2220,28 +2418,28 @@ _cogl_texture_quad_sw (CoglTexture *tex, /* Quad coordinate of the first tile */ first_qx = x1 - (tx1 - first_tx) * tqx; first_qy = y1 - (ty1 - first_ty) * tqy; - - + + /* Iterate until whole quad height covered */ for (_cogl_span_iter_begin (&iter_y, tex->slice_y_spans, first_ty, ty1, ty2) ; !_cogl_span_iter_end (&iter_y) ; _cogl_span_iter_next (&iter_y) ) - { + { + float tex_coords[4]; + /* Discard slices out of quad early */ if (!iter_y.intersects) continue; - + /* Span-quad intersection in quad coordinates */ slice_qy1 = first_qy + (iter_y.intersect_start - first_ty) * tqy; - + slice_qy2 = first_qy + (iter_y.intersect_end - first_ty) * tqy; /* Localize slice texture coordinates */ slice_ty1 = iter_y.intersect_start - iter_y.pos; slice_ty2 = iter_y.intersect_end - iter_y.pos; - /* Normalize texture coordinates to current slice - (rectangle texture targets take denormalized) */ slice_ty1 /= iter_y.span->size; slice_ty2 /= iter_y.span->size; @@ -2253,12 +2451,12 @@ _cogl_texture_quad_sw (CoglTexture *tex, { /* Discard slices out of quad early */ if (!iter_x.intersects) continue; - + /* Span-quad intersection in quad coordinates */ slice_qx1 = first_qx + (iter_x.intersect_start - first_tx) * tqx; - + slice_qx2 = first_qx + (iter_x.intersect_end - first_tx) * tqx; - + /* Localize slice texture coordinates */ slice_tx1 = iter_x.intersect_start - iter_x.pos; slice_tx2 = iter_x.intersect_end - iter_x.pos; @@ -2279,162 +2477,356 @@ _cogl_texture_quad_sw (CoglTexture *tex, printf("tx2: %f\n", (slice_tx2)); printf("ty2: %f\n", (slice_ty2)); #endif - + /* Pick and bind opengl texture object */ gl_handle = g_array_index (tex->slice_gl_handles, GLuint, iter_y.index * iter_x.array->len + iter_x.index); - /* If we're using a different texture from the one already queued - then flush the vertices */ - if (ctx->texture_vertices->len > 0 - && gl_handle != ctx->texture_current) - _cogl_texture_flush_vertices (); - ctx->texture_target = tex->gl_target; - ctx->texture_current = gl_handle; - ctx->texture_format = tex->gl_intformat; - - _cogl_texture_add_quad_vertices ( (slice_qx1), - (slice_qy1), - (slice_qx2), - (slice_qy2), - (slice_tx1), - (slice_ty1), - (slice_tx2), - (slice_ty2)); + tex_coords[0] = slice_tx1; + tex_coords[1] = slice_ty1; + tex_coords[2] = slice_tx2; + tex_coords[3] = slice_ty2; + _cogl_journal_log_quad (slice_qx1, + slice_qy1, + slice_qx2, + slice_qy2, + material, + 1, /* one layer */ + 0, /* don't need to use fallbacks */ + gl_handle, /* replace the layer0 texture */ + tex_coords, + 4); } } } -static void -_cogl_texture_quad_hw (CoglTexture *tex, - float x1, - float y1, - float x2, - float y2, - float tx1, - float ty1, - float tx2, - float ty2) +static gboolean +_cogl_multitexture_unsliced_quad (float x1, + float y1, + float x2, + float y2, + CoglHandle material, + gint n_layers, + guint32 fallback_mask, + const float *user_tex_coords, + gint user_tex_coords_len) { - GLuint gl_handle; - CoglTexSliceSpan *x_span; - CoglTexSliceSpan *y_span; + float *final_tex_coords = alloca (sizeof (float) * 4 * n_layers); + const GList *layers; + GList *tmp; + int i; -#if COGL_DEBUG - printf("=== Drawing Tex Quad (Hardware Tiling Mode) ===\n"); -#endif + _COGL_GET_CONTEXT (ctx, FALSE); - _COGL_GET_CONTEXT (ctx, NO_RETVAL); - - /* Pick and bind opengl texture object */ - gl_handle = g_array_index (tex->slice_gl_handles, GLuint, 0); - - /* If we're using a different texture from the one already queued - then flush the vertices */ - if (ctx->texture_vertices->len > 0 - && gl_handle != ctx->texture_current) - _cogl_texture_flush_vertices (); - ctx->texture_target = tex->gl_target; - ctx->texture_current = gl_handle; - ctx->texture_format = tex->gl_intformat; - - /* Don't include the waste in the texture coordinates */ - x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, 0); - y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, 0); - - /* Don't include the waste in the texture coordinates */ - tx1 = tx1 * (x_span->size - x_span->waste) / x_span->size; - tx2 = tx2 * (x_span->size - x_span->waste) / x_span->size; - ty1 = ty1 * (y_span->size - y_span->waste) / y_span->size; - ty2 = ty2 * (y_span->size - y_span->waste) / y_span->size; - - _cogl_texture_add_quad_vertices ( (x1), - (y1), - (x2), - (y2), - (tx1), - (ty1), - (tx2), - (ty2)); -} - -void -cogl_texture_multiple_rectangles (CoglHandle handle, - const float *verts, - guint n_rects) -{ - CoglTexture *tex; - gulong enable_flags = (COGL_ENABLE_VERTEX_ARRAY - | COGL_ENABLE_TEXCOORD_ARRAY - | COGL_ENABLE_TEXTURE_2D); - - _COGL_GET_CONTEXT (ctx, NO_RETVAL); - - /* Check if valid texture */ - if (!cogl_is_texture (handle)) - return; - - cogl_clip_ensure (); - - tex = _cogl_texture_pointer_from_handle (handle); - - /* Make sure we got stuff to draw */ - if (tex->slice_gl_handles == NULL) - return; - - if (tex->slice_gl_handles->len == 0) - return; - - /* Prepare GL state */ - if (ctx->color_alpha < 255 - || tex->bitmap.format & COGL_A_BIT) - enable_flags |= COGL_ENABLE_BLEND; - - if (ctx->enable_backface_culling) - enable_flags |= COGL_ENABLE_BACKFACE_CULLING; - - cogl_enable (enable_flags); - - g_array_set_size (ctx->texture_vertices, 0); - - while (n_rects-- > 0) + /* + * Validate the texture coordinates for this rectangle. + */ + layers = cogl_material_get_layers (material); + for (tmp = (GList *)layers, i = 0; tmp != NULL; tmp = tmp->next, i++) { - if (verts[4] != verts[6] && verts[5] != verts[7]) + CoglHandle layer = (CoglHandle)tmp->data; + /* CoglLayerInfo *layer_info; */ + CoglHandle tex_handle; + CoglTexture *tex; + const float *in_tex_coords; + float *out_tex_coords; + CoglTexSliceSpan *x_span; + CoglTexSliceSpan *y_span; + + /* layer_info = &layers[i]; */ + + /* FIXME - we shouldn't be checking this stuff if layer_info->gl_texture + * already == 0 */ + + tex_handle = cogl_material_layer_get_texture (layer); + tex = _cogl_texture_pointer_from_handle (tex_handle); + + in_tex_coords = &user_tex_coords[i * 4]; + out_tex_coords = &final_tex_coords[i * 4]; + + + /* If the texture has waste or we are using GL_TEXTURE_RECT we + * can't handle texture repeating so we check that the texture + * coords lie in the range [0,1]. + * + * NB: We already know that no texture matrix is being used + * if the texture has waste since we validated that early on. + * TODO: check for a texture matrix in the GL_TEXTURE_RECT + * case. + */ + if (_cogl_texture_span_has_waste (tex, 0, 0) + && (in_tex_coords[0] < 0 || in_tex_coords[0] > 1.0 + || in_tex_coords[1] < 0 || in_tex_coords[1] > 1.0 + || in_tex_coords[2] < 0 || in_tex_coords[2] > 1.0 + || in_tex_coords[3] < 0 || in_tex_coords[3] > 1.0)) { - /* If there is only one GL texture and either the texture is - NPOT (no waste) or all of the coordinates are in the - range [0,1] then we can use hardware tiling */ - if (tex->slice_gl_handles->len == 1 - && ((cogl_features_available (COGL_FEATURE_TEXTURE_NPOT) - && tex->gl_target == GL_TEXTURE_2D) - || (verts[4] >= 0 && verts[4] <= 1.0 - && verts[6] >= 0 && verts[6] <= 1.0 - && verts[5] >= 0 && verts[5] <= 1.0 - && verts[7] >= 0 && verts[7] <= 1.0))) - _cogl_texture_quad_hw (tex, verts[0],verts[1], verts[2],verts[3], - verts[4],verts[5], verts[6],verts[7]); + if (i == 0) + { + if (n_layers > 1) + { + g_warning ("Skipping layers 1..n of your material since the " + "first layer has waste and you supplied texture " + "coordinates outside the range [0,1]. We don't " + "currently support any multi-texturing using " + "textures with waste when repeating is " + "necissary so we are falling back to sliced " + "textures assuming layer 0 is the most " + "important one keep"); + } + return FALSE; + } else - _cogl_texture_quad_sw (tex, verts[0],verts[1], verts[2],verts[3], - verts[4],verts[5], verts[6],verts[7]); + { + g_warning ("Skipping layer %d of your material " + "consisting of a texture with waste since " + "you have supplied texture coords outside " + "the range [0,1] (unsupported when " + "multi-texturing)", i); + + /* NB: marking for fallback will replace the layer with + * a default transparent texture */ + fallback_mask |= (1 << i); + } } - verts += 8; + + /* + * Setup the texture unit... + */ + + /* NB: The user might not have supplied texture coordinates for all + * layers... */ + if (i < (user_tex_coords_len / 4)) + { + GLenum wrap_mode; + + /* If the texture coords are all in the range [0,1] then we want to + clamp the coords to the edge otherwise it can pull in edge pixels + from the wrong side when scaled */ + if (in_tex_coords[0] >= 0 && in_tex_coords[0] <= 1.0 + && in_tex_coords[1] >= 0 && in_tex_coords[1] <= 1.0 + && in_tex_coords[2] >= 0 && in_tex_coords[2] <= 1.0 + && in_tex_coords[3] >= 0 && in_tex_coords[3] <= 1.0) + wrap_mode = GL_CLAMP_TO_EDGE; + else + wrap_mode = GL_REPEAT; + + _cogl_texture_set_wrap_mode_parameter (tex, wrap_mode); + } + else + { + out_tex_coords[0] = 0; /* tx1 */ + out_tex_coords[1] = 0; /* ty1 */ + out_tex_coords[2] = 1.0; /* tx2 */ + out_tex_coords[3] = 1.0; /* ty2 */ + + _cogl_texture_set_wrap_mode_parameter (tex, GL_CLAMP_TO_EDGE); + } + + /* Don't include the waste in the texture coordinates */ + x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, 0); + y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, 0); + + out_tex_coords[0] = + in_tex_coords[0] * (x_span->size - x_span->waste) / x_span->size; + out_tex_coords[1] = + in_tex_coords[1] * (x_span->size - x_span->waste) / x_span->size; + out_tex_coords[2] = + in_tex_coords[2] * (y_span->size - y_span->waste) / y_span->size; + out_tex_coords[3] = + in_tex_coords[3] * (y_span->size - y_span->waste) / y_span->size; } - _cogl_texture_flush_vertices (); + _cogl_journal_log_quad (x1, + y1, + x2, + y2, + material, + n_layers, + fallback_mask, + 0, /* don't replace the layer0 texture */ + final_tex_coords, + n_layers * 4); + + return TRUE; +} + +struct _CoglMutiTexturedRect +{ + float x1; + float y1; + float x2; + float y2; + const float *tex_coords; + gint tex_coords_len; +}; + +static void +_cogl_rectangles_with_multitexture_coords ( + struct _CoglMutiTexturedRect *rects, + gint n_rects) +{ + CoglHandle material; + const GList *layers; + int n_layers; + const GList *tmp; + guint32 fallback_mask = 0; + gboolean all_use_sliced_quad_fallback = FALSE; + int i; + + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + cogl_clip_ensure (); + + material = ctx->source_material; + + layers = cogl_material_get_layers (material); + n_layers = g_list_length ((GList *)layers); + + /* + * Validate all the layers of the current source material... + */ + + for (tmp = layers, i = 0; tmp != NULL; tmp = tmp->next, i++) + { + CoglHandle layer = tmp->data; + CoglHandle tex_handle = cogl_material_layer_get_texture (layer); + CoglTexture *texture = _cogl_texture_pointer_from_handle (tex_handle); + gulong flags; + + if (cogl_material_layer_get_type (layer) + != COGL_MATERIAL_LAYER_TYPE_TEXTURE) + continue; + + /* XXX: + * For now, if the first layer is sliced then all other layers are + * ignored since we currently don't support multi-texturing with + * sliced textures. If the first layer is not sliced then any other + * layers found to be sliced will be skipped. (with a warning) + * + * TODO: Add support for multi-texturing rectangles with sliced + * textures if no texture matrices are in use. + */ + if (cogl_texture_is_sliced (tex_handle)) + { + if (i == 0) + { + fallback_mask = ~1; /* fallback all except the first layer */ + all_use_sliced_quad_fallback = TRUE; + if (tmp->next) + { + g_warning ("Skipping layers 1..n of your material since the " + "first layer is sliced. We don't currently " + "support any multi-texturing with sliced " + "textures but assume layer 0 is the most " + "important to keep"); + } + break; + } + else + { + g_warning ("Skipping layer %d of your material consisting of a " + "sliced texture (unsuported for multi texturing)", + i); + + /* NB: marking for fallback will replace the layer with + * a default transparent texture */ + fallback_mask |= (1 << i); + continue; + } + } + + /* We don't support multi texturing using textures with any waste if the + * user has supplied a custom texture matrix, since we don't know if + * the result will end up trying to texture from the waste area. */ + flags = cogl_material_layer_get_flags (layer); + if (flags & COGL_MATERIAL_LAYER_FLAG_HAS_USER_MATRIX + && _cogl_texture_span_has_waste (texture, 0, 0)) + { + static gboolean shown_warning = FALSE; + if (!shown_warning) + g_warning ("Skipping layer %d of your material consisting of a " + "texture with waste since you have supplied a custom " + "texture matrix and the result may try to sample from " + "the waste area of your texture.", i); + shown_warning = TRUE; + /* NB: marking for fallback will replace the layer with + * a default transparent texture */ + fallback_mask |= (1 << i); + continue; + } + } + + /* + * Emit geometry for each of the rectangles... + */ + + for (i = 0; i < n_rects; i++) + { + if (all_use_sliced_quad_fallback + || !_cogl_multitexture_unsliced_quad (rects[i].x1, rects[i].y1, + rects[i].x2, rects[i].y2, + material, + n_layers, + fallback_mask, + rects[i].tex_coords, + rects[i].tex_coords_len)) + { + const GList *layers; + CoglHandle tex_handle; + CoglTexture *texture; + + layers = cogl_material_get_layers (material); + tex_handle = + cogl_material_layer_get_texture ((CoglHandle)layers->data); + texture = _cogl_texture_pointer_from_handle (tex_handle); + _cogl_texture_sliced_quad (texture, + material, + rects[i].x1, rects[i].y1, + rects[i].x2, rects[i].y2, + rects[i].tex_coords[0], + rects[i].tex_coords[1], + rects[i].tex_coords[2], + rects[i].tex_coords[3]); + } + } + + _cogl_journal_flush (); } void -cogl_texture_rectangle (CoglHandle handle, - float x1, - float y1, - float x2, - float y2, - float tx1, - float ty1, - float tx2, - float ty2) +cogl_rectangles_with_texture_coords (const float *verts, + guint n_rects) +{ + struct _CoglMutiTexturedRect rects[n_rects]; + int i; + + for (i = 0; i < n_rects; i++) + { + rects[i].x1 = verts[i * 8]; + rects[i].y1 = verts[i * 8 + 1]; + rects[i].x2 = verts[i * 8 + 2]; + rects[i].y2 = verts[i * 8 + 3]; + /* FIXME: rect should be defined to have a const float *geom; + * instead, to avoid this copy + * rect[i].geom = &verts[n_rects * 8]; */ + rects[i].tex_coords = &verts[i * 8 + 4]; + rects[i].tex_coords_len = 4; + } + + _cogl_rectangles_with_multitexture_coords (rects, n_rects); +} + +void +cogl_rectangle_with_texture_coords (float x1, + float y1, + float x2, + float y2, + float tx1, + float ty1, + float tx2, + float ty2) { float verts[8]; @@ -2447,73 +2839,312 @@ cogl_texture_rectangle (CoglHandle handle, verts[6] = tx2; verts[7] = ty2; - cogl_texture_multiple_rectangles (handle, verts, 1); + cogl_rectangles_with_texture_coords (verts, 1); } void -cogl_texture_polygon (CoglHandle handle, - guint n_vertices, - CoglTextureVertex *vertices, - gboolean use_color) +cogl_rectangle_with_multitexture_coords (float x1, + float y1, + float x2, + float y2, + const float *user_tex_coords, + gint user_tex_coords_len) { - CoglTexture *tex; - int i; - GLuint gl_handle; - CoglTexSliceSpan *y_span, *x_span; - gulong enable_flags; - CoglTextureGLVertex *p; + struct _CoglMutiTexturedRect rect; + + rect.x1 = x1; + rect.y1 = y1; + rect.x2 = x2; + rect.y2 = y2; + rect.tex_coords = user_tex_coords; + rect.tex_coords_len = user_tex_coords_len; + + _cogl_rectangles_with_multitexture_coords (&rect, 1); +} + +static void +_cogl_texture_sliced_polygon (CoglTextureVertex *vertices, + guint n_vertices, + guint stride, + gboolean use_color) +{ + const GList *layers; + CoglHandle layer0; + CoglHandle tex_handle; + CoglTexture *tex; + CoglTexSliceSpan *y_span, *x_span; + int x, y, tex_num, i; + GLuint gl_handle; + GLfloat *v; _COGL_GET_CONTEXT (ctx, NO_RETVAL); - /* Check if valid texture */ - if (!cogl_is_texture (handle)) - return; + /* We can assume in this case that we have at least one layer in the + * material that corresponds to a sliced cogl texture */ + layers = cogl_material_get_layers (ctx->source_material); + layer0 = (CoglHandle)layers->data; + tex_handle = cogl_material_layer_get_texture (layer0); + tex = _cogl_texture_pointer_from_handle (tex_handle); + + v = (GLfloat *)ctx->logged_vertices->data; + for (i = 0; i < n_vertices; i++) + { + GLfloat *c; + + v[0] = vertices[i].x; + v[1] = vertices[i].y; + v[2] = vertices[i].z; + + /* NB: [X,Y,Z,TX,TY,R,G,B,A,...] */ + c = v + 5; + c[0] = cogl_color_get_red_byte (&vertices[i].color); + c[1] = cogl_color_get_green_byte (&vertices[i].color); + c[2] = cogl_color_get_blue_byte (&vertices[i].color); + c[3] = cogl_color_get_alpha_byte (&vertices[i].color); + + v += stride; + } + + /* Render all of the slices with the full geometry but use a + transparent border color so that any part of the texture not + covered by the slice will be ignored */ + tex_num = 0; + for (y = 0; y < tex->slice_y_spans->len; y++) + { + y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, y); + + for (x = 0; x < tex->slice_x_spans->len; x++) + { + x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, x); + + gl_handle = g_array_index (tex->slice_gl_handles, GLuint, tex_num++); + + /* Convert the vertices into an array of GLfloats ready to pass to + OpenGL */ + v = (GLfloat *)ctx->logged_vertices->data; + for (i = 0; i < n_vertices; i++) + { + GLfloat *t; + float tx, ty; + + tx = ((vertices[i].tx + - ((float)(x_span->start) + / tex->bitmap.width)) + * tex->bitmap.width / x_span->size); + ty = ((vertices[i].ty + - ((float)(y_span->start) + / tex->bitmap.height)) + * tex->bitmap.height / y_span->size); + + /* NB: [X,Y,Z,TX,TY,R,G,B,A,...] */ + t = v + 3; + t[0] = tx; + t[1] = ty; + + v += stride; + } + + cogl_material_flush_gl_state (ctx->source_material, + COGL_MATERIAL_FLUSH_DISABLE_MASK, + (guint32)~1, /* disable all except the + first layer */ + COGL_MATERIAL_FLUSH_LAYER0_OVERRIDE, + gl_handle, + NULL); + + GE( glDrawArrays (GL_TRIANGLE_FAN, 0, n_vertices) ); + } + } +} + + +static void +_cogl_multitexture_unsliced_polygon (CoglTextureVertex *vertices, + guint n_vertices, + guint n_layers, + guint stride, + gboolean use_color, + guint32 fallback_mask) +{ + CoglHandle material; + const GList *layers; + int i; + GList *tmp; + CoglTexSliceSpan *y_span, *x_span; + GLfloat *v; + + _COGL_GET_CONTEXT (ctx, NO_RETVAL); + + v = (GLfloat *)ctx->logged_vertices->data; + + material = ctx->source_material; + layers = cogl_material_get_layers (material); + + /* Convert the vertices into an array of GLfloats ready to pass to + OpenGL */ + for (v = (GLfloat *)ctx->logged_vertices->data, i = 0; + i < n_vertices; + v += stride, i++) + { + GLfloat *c; + int j; + + /* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */ + v[0] = vertices[i].x; + v[1] = vertices[i].y; + v[2] = vertices[i].z; + + for (tmp = (GList *)layers, j = 0; tmp != NULL; tmp = tmp->next, j++) + { + CoglHandle layer = (CoglHandle)tmp->data; + CoglHandle tex_handle; + CoglTexture *tex; + GLfloat *t; + float tx, ty; + + tex_handle = cogl_material_layer_get_texture (layer); + tex = _cogl_texture_pointer_from_handle (tex_handle); + + y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, 0); + x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, 0); + + tx = ((vertices[i].tx + - ((float)(x_span->start) + / tex->bitmap.width)) + * tex->bitmap.width / x_span->size); + ty = ((vertices[i].ty + - ((float)(y_span->start) + / tex->bitmap.height)) + * tex->bitmap.height / y_span->size); + + /* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */ + t = v + 3 + 2 * j; + t[0] = tx; + t[1] = ty; + } + + /* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */ + c = v + 3 + 2 * n_layers; + c[0] = cogl_color_get_red_float (&vertices[i].color); + c[1] = cogl_color_get_green_float (&vertices[i].color); + c[2] = cogl_color_get_blue_float (&vertices[i].color); + c[3] = cogl_color_get_alpha_float (&vertices[i].color); + } + + cogl_material_flush_gl_state (ctx->source_material, + COGL_MATERIAL_FLUSH_FALLBACK_MASK, + fallback_mask, + NULL); + + GE (glDrawArrays (GL_TRIANGLE_FAN, 0, n_vertices)); +} + +void +cogl_polygon (CoglTextureVertex *vertices, + guint n_vertices, + gboolean use_color) +{ + CoglHandle material; + const GList *layers; + int n_layers; + GList *tmp; + gboolean use_sliced_polygon_fallback = FALSE; + guint32 fallback_mask = 0; + int i; + gulong enable_flags; + guint stride; + gsize stride_bytes; + GLfloat *v; + + _COGL_GET_CONTEXT (ctx, NO_RETVAL); cogl_clip_ensure (); - tex = _cogl_texture_pointer_from_handle (handle); + material = ctx->source_material; + layers = cogl_material_get_layers (ctx->source_material); + n_layers = g_list_length ((GList *)layers); - /* GL ES has no GL_CLAMP_TO_BORDER wrap mode so the method used to - render sliced textures in the GL backend will not work. Therefore - cogl_texture_polygon is only supported if the texture is not - sliced */ - if (tex->slice_gl_handles->len != 1) + for (tmp = (GList *)layers, i = 0; tmp != NULL; tmp = tmp->next, i++) { - static gboolean shown_warning = FALSE; + CoglHandle layer = (CoglHandle)tmp->data; + CoglHandle tex_handle = cogl_material_layer_get_texture (layer); + CoglTexture *tex = _cogl_texture_pointer_from_handle (tex_handle); - if (!shown_warning) - { - g_warning ("cogl_texture_polygon does not work for " - "sliced textures on GL ES"); - shown_warning = TRUE; - } - return; + if (i == 0 && cogl_texture_is_sliced (tex_handle)) + { +#if defined (HAVE_COGL_GLES) || defined (HAVE_COGL_GLES2) + { + static gboolean shown_gles_slicing_warning = FALSE; + if (!shown_gles_slicing_warning) + g_warning ("cogl_polygon does not work for sliced textures " + "on GL ES"); + shown_gles_slicing_warning = TRUE; + return; + } +#endif + if (n_layers > 1) + { + static gboolean shown_slicing_warning = FALSE; + if (!shown_slicing_warning) + { + g_warning ("Disabling layers 1..n since multi-texturing with " + "cogl_polygon isn't supported when using sliced " + "textures\n"); + shown_slicing_warning = TRUE; + } + } + use_sliced_polygon_fallback = TRUE; + n_layers = 1; + + if (tex->min_filter != GL_NEAREST || tex->mag_filter != GL_NEAREST) + { + static gboolean shown_filter_warning = FALSE; + if (!shown_filter_warning) + { + g_warning ("cogl_texture_polygon does not work for sliced textures " + "when the minification and magnification filters are not " + "CGL_NEAREST"); + shown_filter_warning = TRUE; + } + return; + } + +#ifdef HAVE_COGL_GL + /* Temporarily change the wrapping mode on all of the slices to use + * a transparent border + * XXX: it's doesn't look like we save/restore this, like the comment + * implies? */ + _cogl_texture_set_wrap_mode_parameter (tex, GL_CLAMP_TO_BORDER); +#endif + break; + } + + if (cogl_texture_is_sliced (tex_handle)) + { + g_warning ("Disabling layer %d of the current source material, " + "because texturing with the vertex buffer API is not " + "currently supported using sliced textures, or textures " + "with waste\n", i); + + fallback_mask |= (1 << i); + continue; + } } - /* Make sure there is enough space in the global texture vertex + /* Our data is arranged like: + * [X, Y, Z, TX0, TY0, TX1, TY1..., R, G, B, A,...] */ + stride = 3 + (2 * n_layers) + (use_color ? 4 : 0); + stride_bytes = stride * sizeof (GLfloat); + + /* Make sure there is enough space in the global vertex array. This is used so we can render the polygon with a single call to OpenGL but still support any number of vertices */ - g_array_set_size (ctx->texture_vertices, n_vertices); - p = (CoglTextureGLVertex *) ctx->texture_vertices->data; + g_array_set_size (ctx->logged_vertices, n_vertices * stride); + v = (GLfloat *)ctx->logged_vertices->data; /* Prepare GL state */ - enable_flags = (COGL_ENABLE_TEXTURE_2D - | COGL_ENABLE_VERTEX_ARRAY - | COGL_ENABLE_TEXCOORD_ARRAY); - - if ((tex->bitmap.format & COGL_A_BIT)) - enable_flags |= COGL_ENABLE_BLEND; - else if (use_color) - { - for (i = 0; i < n_vertices; i++) - if (cogl_color_get_alpha_byte(&vertices[i].color) < 255) - { - enable_flags |= COGL_ENABLE_BLEND; - break; - } - } - else if (ctx->color_alpha < 255) - enable_flags |= COGL_ENABLE_BLEND; + enable_flags = COGL_ENABLE_VERTEX_ARRAY; + enable_flags |= cogl_material_get_cogl_enable_flags (ctx->source_material); if (ctx->enable_backface_culling) enable_flags |= COGL_ENABLE_BACKFACE_CULLING; @@ -2521,47 +3152,36 @@ cogl_texture_polygon (CoglHandle handle, if (use_color) { enable_flags |= COGL_ENABLE_COLOR_ARRAY; - GE( glColorPointer (4, GL_UNSIGNED_BYTE, - sizeof (CoglTextureGLVertex), p->c) ); - } - - GE( glVertexPointer (3, GL_FLOAT, sizeof (CoglTextureGLVertex), p->v ) ); - GE( glTexCoordPointer (2, GL_FLOAT, sizeof (CoglTextureGLVertex), p->t ) ); - - cogl_enable (enable_flags); - - gl_handle = g_array_index (tex->slice_gl_handles, GLuint, 0); - x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, 0); - y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, 0); - - /* Convert the vertices into an array of GLfloats ready to pass to - OpenGL */ - for (i = 0; i < n_vertices; i++, p++) - { -#define CFX_F - - p->v[0] = CFX_F(vertices[i].x); - p->v[1] = CFX_F(vertices[i].y); - p->v[2] = CFX_F(vertices[i].z); - p->t[0] = CFX_F(vertices[i].tx - * (x_span->size - x_span->waste) / x_span->size); - p->t[1] = CFX_F(vertices[i].ty - * (y_span->size - y_span->waste) / y_span->size); - p->c[0] = cogl_color_get_red_byte(&vertices[i].color); - p->c[1] = cogl_color_get_green_byte(&vertices[i].color); - p->c[2] = cogl_color_get_blue_byte(&vertices[i].color); - p->c[3] = cogl_color_get_alpha_byte(&vertices[i].color); - -#undef CFX_F + GE( glColorPointer (4, GL_FLOAT, + stride_bytes, + /* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */ + v + 3 + 2 * n_layers) ); } - GE( cogl_gles2_wrapper_bind_texture (tex->gl_target, gl_handle, - tex->gl_intformat) ); + cogl_enable (enable_flags); - GE( glDrawArrays (GL_TRIANGLE_FAN, 0, n_vertices) ); + GE (glVertexPointer (3, GL_FLOAT, stride_bytes, v)); - /* Set the last color so that the cache of the alpha value will work - properly */ - if (use_color && n_vertices > 0) - cogl_set_source_color (&vertices[n_vertices - 1].color); + for (i = 0; i < n_layers; i++) + { + GE (glClientActiveTexture (GL_TEXTURE0 + i)); + GE (glTexCoordPointer (2, GL_FLOAT, + stride_bytes, + /* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */ + v + 3 + 2 * i)); + } + + if (use_sliced_polygon_fallback) + _cogl_texture_sliced_polygon (vertices, + n_vertices, + stride, + use_color); + else + _cogl_multitexture_unsliced_polygon (vertices, + n_vertices, + n_layers, + stride, + use_color, + fallback_mask); } + diff --git a/clutter/cogl/gles/cogl.c b/clutter/cogl/gles/cogl.c index 717b1e13f..32f4c71dd 100644 --- a/clutter/cogl/gles/cogl.c +++ b/clutter/cogl/gles/cogl.c @@ -73,14 +73,13 @@ _cogl_error_string(GLenum errorCode) } #endif - CoglFuncPtr cogl_get_proc_address (const gchar* name) { return NULL; } -gboolean +gboolean cogl_check_extension (const gchar *name, const gchar *ext) { return FALSE; @@ -93,45 +92,59 @@ cogl_paint_init (const CoglColor *color) fprintf(stderr, "\n ============== Paint Start ================ \n"); #endif - glClearColor (cogl_color_get_red (color), - cogl_color_get_green (color), - cogl_color_get_blue (color), - 0); + GE( glClearColor (cogl_color_get_red_float (color), + cogl_color_get_green_float (color), + cogl_color_get_blue_float (color), + 0.0) ); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - cogl_wrap_glDisable (GL_LIGHTING); - cogl_wrap_glDisable (GL_FOG); + glDisable (GL_LIGHTING); + glDisable (GL_FOG); + + /* + * Disable the depth test for now as has some strange side effects, + * mainly on x/y axis rotation with multiple layers at same depth + * (eg rotating text on a bg has very strange effect). Seems no clean + * 100% effective way to fix without other odd issues.. So for now + * move to application to handle and add cogl_enable_depth_test() + * as for custom actors (i.e groups) to enable if need be. + * + * glEnable (GL_DEPTH_TEST); + * glEnable (GL_ALPHA_TEST) + * glDepthFunc (GL_LEQUAL); + * glAlphaFunc (GL_GREATER, 0.1); + */ } /* FIXME: inline most of these */ void cogl_push_matrix (void) { - GE( cogl_wrap_glPushMatrix() ); + GE( glPushMatrix() ); } void cogl_pop_matrix (void) { - GE( cogl_wrap_glPopMatrix() ); + GE( glPopMatrix() ); } void cogl_scale (float x, float y) { - GE( cogl_wrap_glScalef (x, y, 1.0) ); + GE( glScalef (x, y, 1.0) ); } void cogl_translate (float x, float y, float z) { - GE( cogl_wrap_glTranslatef (x, y, z) ); + GE( glTranslatef (x, y, z) ); } void cogl_rotate (float angle, float x, float y, float z) { - GE( cogl_wrap_glRotatef (angle, x, y, z) ); + GE( glRotatef (angle, x, y, z) ); } static inline gboolean @@ -147,17 +160,17 @@ cogl_toggle_flag (CoglContext *ctx, { if (!(ctx->enable_flags & flag)) { - GE( cogl_wrap_glEnable (gl_flag) ); + GE( glEnable (gl_flag) ); ctx->enable_flags |= flag; return TRUE; } } else if (ctx->enable_flags & flag) { - GE( cogl_wrap_glDisable (gl_flag) ); + GE( glDisable (gl_flag) ); ctx->enable_flags &= ~flag; } - + return FALSE; } @@ -174,17 +187,17 @@ cogl_toggle_client_flag (CoglContext *ctx, { if (!(ctx->enable_flags & flag)) { - GE( cogl_wrap_glEnableClientState (gl_flag) ); + GE( glEnableClientState (gl_flag) ); ctx->enable_flags |= flag; return TRUE; } } else if (ctx->enable_flags & flag) { - GE( cogl_wrap_glDisableClientState (gl_flag) ); + GE( glDisableClientState (gl_flag) ); ctx->enable_flags &= ~flag; } - + return FALSE; } @@ -195,14 +208,10 @@ cogl_enable (gulong flags) * hope of lessening number GL traffic. */ _COGL_GET_CONTEXT (ctx, NO_RETVAL); - + cogl_toggle_flag (ctx, flags, COGL_ENABLE_BLEND, GL_BLEND); - - cogl_toggle_flag (ctx, flags, - COGL_ENABLE_TEXTURE_2D, - GL_TEXTURE_2D); cogl_toggle_flag (ctx, flags, COGL_ENABLE_BACKFACE_CULLING, @@ -211,10 +220,6 @@ cogl_enable (gulong flags) cogl_toggle_client_flag (ctx, flags, COGL_ENABLE_VERTEX_ARRAY, GL_VERTEX_ARRAY); - - cogl_toggle_client_flag (ctx, flags, - COGL_ENABLE_TEXCOORD_ARRAY, - GL_TEXTURE_COORD_ARRAY); cogl_toggle_client_flag (ctx, flags, COGL_ENABLE_COLOR_ARRAY, @@ -225,25 +230,8 @@ gulong cogl_get_enable () { _COGL_GET_CONTEXT (ctx, 0); - - return ctx->enable_flags; -} -void -cogl_blend_func (COGLenum src_factor, COGLenum dst_factor) -{ - /* This function caches the blending setup in the - * hope of lessening GL traffic. - */ - _COGL_GET_CONTEXT (ctx, NO_RETVAL); - - if (ctx->blend_src_factor != src_factor || - ctx->blend_dst_factor != dst_factor) - { - glBlendFunc (src_factor, dst_factor); - ctx->blend_src_factor = src_factor; - ctx->blend_dst_factor = dst_factor; - } + return ctx->enable_flags; } void @@ -251,15 +239,15 @@ cogl_enable_depth_test (gboolean setting) { if (setting) { - cogl_wrap_glEnable (GL_DEPTH_TEST); - cogl_wrap_glEnable (GL_ALPHA_TEST); + glEnable (GL_DEPTH_TEST); + glEnable (GL_ALPHA_TEST); glDepthFunc (GL_LEQUAL); - cogl_wrap_glAlphaFunc (GL_GREATER, 0.1); + glAlphaFunc (GL_GREATER, 0.1); } else { - cogl_wrap_glDisable (GL_DEPTH_TEST); - cogl_wrap_glDisable (GL_ALPHA_TEST); + glDisable (GL_DEPTH_TEST); + glDisable (GL_ALPHA_TEST); } } @@ -275,35 +263,12 @@ void cogl_set_source_color (const CoglColor *color) { _COGL_GET_CONTEXT (ctx, NO_RETVAL); - -#if 0 /*HAVE_GLES_COLOR4UB*/ - /* NOTE: seems SDK_OGLES-1.1_LINUX_PCEMULATION_2.02.22.0756 has this call - * but is broken - see #857. Therefor disabling. - */ + /* In case cogl_set_source_texture was previously used... */ + cogl_material_remove_layer (ctx->default_material, 0); - /* - * GLES 1.1 does actually have this function, it's in the header file but - * missing in the reference manual (and SDK): - * - * http://www.khronos.org/egl/headers/1_1/gl.h - */ - GE( glColor4ub (color->red, - color->green, - color->blue, - color->alpha) ); - - -#else - /* conversion can cause issues with picking on some gles implementations */ - GE( cogl_wrap_glColor4f (cogl_color_get_red (color), - cogl_color_get_green (color), - cogl_color_get_blue (color), - cogl_color_get_alpha (color)) ); -#endif - - /* Store alpha for proper blending enables */ - ctx->color_alpha = cogl_color_get_alpha_byte (color); + cogl_material_set_color (ctx->default_material, color); + cogl_set_source (ctx->default_material); } static void @@ -314,7 +279,7 @@ apply_matrix (const float *matrix, float *vertex) for (y = 0; y < 4; y++) for (x = 0; x < 4; x++) - vertex_out[y] += (vertex[x] * matrix[y + x * 4]); + vertex_out[y] += vertex[x] * matrix[y + x * 4]; memcpy (vertex, vertex_out, sizeof (vertex_out)); } @@ -332,7 +297,7 @@ project_vertex (float *modelview, apply_matrix (project, vertex); /* Convert from homogenized coordinates */ for (i = 0; i < 4; i++) - vertex[i] = (vertex[i] / vertex[3]); + vertex[i] /= vertex[3]; } static void @@ -349,26 +314,26 @@ set_clip_plane (GLint plane_num, angle = atan2f (vertex_b[1] - vertex_a[1], vertex_b[0] - vertex_a[0]) * (180.0/G_PI); - GE( cogl_wrap_glPushMatrix () ); + GE( glPushMatrix () ); /* Load the identity matrix and multiply by the reverse of the projection matrix so we can specify the plane in screen coordinates */ - GE( cogl_wrap_glLoadIdentity () ); - GE( cogl_wrap_glMultMatrixf ((GLfloat *) ctx->inverse_projection) ); + GE( glLoadIdentity () ); + GE( glMultMatrixf ((GLfloat *) ctx->inverse_projection) ); /* Rotate about point a */ - GE( cogl_wrap_glTranslatef (vertex_a[0], vertex_a[1], vertex_a[2]) ); + GE( glTranslatef (vertex_a[0], vertex_a[1], vertex_a[2]) ); /* Rotate the plane by the calculated angle so that it will connect the two points */ - GE( cogl_wrap_glRotatef (angle, 0.0f, 0.0f, 1.0f) ); - GE( cogl_wrap_glTranslatef (-vertex_a[0], -vertex_a[1], -vertex_a[2]) ); + GE( glRotatef (angle, 0.0f, 0.0f, 1.0f) ); + GE( glTranslatef (-vertex_a[0], -vertex_a[1], -vertex_a[2]) ); plane[0] = 0; plane[1] = -1.0; plane[2] = 0; plane[3] = vertex_a[1]; - GE( cogl_wrap_glClipPlanef (plane_num, plane) ); + GE( glClipPlanef (plane_num, plane) ); - GE( cogl_wrap_glPopMatrix () ); + GE( glPopMatrix () ); } void @@ -383,21 +348,21 @@ _cogl_set_clip_planes (float x_offset, float vertex_tr[4] = { x_offset + width, y_offset, 0, 1.0 }; float vertex_bl[4] = { x_offset, y_offset + height, 0, 1.0 }; float vertex_br[4] = { x_offset + width, y_offset + height, - 0, 1.0 }; + 0, 1.0 }; - GE( cogl_wrap_glGetFloatv (GL_MODELVIEW_MATRIX, modelview) ); - GE( cogl_wrap_glGetFloatv (GL_PROJECTION_MATRIX, projection) ); + GE( glGetFloatv (GL_MODELVIEW_MATRIX, modelview) ); + GE( glGetFloatv (GL_PROJECTION_MATRIX, projection) ); project_vertex (modelview, projection, vertex_tl); project_vertex (modelview, projection, vertex_tr); project_vertex (modelview, projection, vertex_bl); project_vertex (modelview, projection, vertex_br); - /* If the order of the top and bottom lines is different from - the order of the left and right lines then the clip rect must - have been transformed so that the back is visible. We - therefore need to swap one pair of vertices otherwise all of - the planes will be the wrong way around */ + /* If the order of the top and bottom lines is different from the + order of the left and right lines then the clip rect must have + been transformed so that the back is visible. We therefore need + to swap one pair of vertices otherwise all of the planes will be + the wrong way around */ if ((vertex_tl[0] < vertex_tr[0] ? 1 : 0) != (vertex_bl[1] < vertex_tl[1] ? 1 : 0)) { @@ -425,10 +390,12 @@ _cogl_add_stencil_clip (float x_offset, { _COGL_GET_CONTEXT (ctx, NO_RETVAL); + cogl_material_flush_gl_state (ctx->stencil_material); + if (first) { - GE( cogl_wrap_glEnable (GL_STENCIL_TEST) ); - + GE( glEnable (GL_STENCIL_TEST) ); + /* Initially disallow everything */ GE( glClearStencil (0) ); GE( glClear (GL_STENCIL_BUFFER_BIT) ); @@ -451,15 +418,15 @@ _cogl_add_stencil_clip (float x_offset, only pixels where both the original stencil buffer and the rectangle are set will be valid */ GE( glStencilOp (GL_DECR, GL_DECR, GL_DECR) ); - GE( cogl_wrap_glPushMatrix () ); - GE( cogl_wrap_glLoadIdentity () ); - GE( cogl_wrap_glMatrixMode (GL_PROJECTION) ); - GE( cogl_wrap_glPushMatrix () ); - GE( cogl_wrap_glLoadIdentity () ); + GE( glPushMatrix () ); + GE( glLoadIdentity () ); + GE( glMatrixMode (GL_PROJECTION) ); + GE( glPushMatrix () ); + GE( glLoadIdentity () ); cogl_rectangle (-1.0, -1.0, 2, 2); - GE( cogl_wrap_glPopMatrix () ); - GE( cogl_wrap_glMatrixMode (GL_MODELVIEW) ); - GE( cogl_wrap_glPopMatrix () ); + GE( glPopMatrix () ); + GE( glMatrixMode (GL_MODELVIEW) ); + GE( glPopMatrix () ); } /* Restore the stencil mode */ @@ -470,44 +437,34 @@ _cogl_add_stencil_clip (float x_offset, void _cogl_set_matrix (const float *matrix) { - GE( cogl_wrap_glLoadIdentity () ); - GE( cogl_wrap_glMultMatrixf (matrix) ); + GE( glLoadIdentity () ); + GE( glMultMatrixf (matrix) ); } void _cogl_disable_stencil_buffer (void) { - GE( cogl_wrap_glDisable (GL_STENCIL_TEST) ); + GE( glDisable (GL_STENCIL_TEST) ); } void _cogl_enable_clip_planes (void) { - GE( cogl_wrap_glEnable (GL_CLIP_PLANE0) ); - GE( cogl_wrap_glEnable (GL_CLIP_PLANE1) ); - GE( cogl_wrap_glEnable (GL_CLIP_PLANE2) ); - GE( cogl_wrap_glEnable (GL_CLIP_PLANE3) ); + GE( glEnable (GL_CLIP_PLANE0) ); + GE( glEnable (GL_CLIP_PLANE1) ); + GE( glEnable (GL_CLIP_PLANE2) ); + GE( glEnable (GL_CLIP_PLANE3) ); } void _cogl_disable_clip_planes (void) { - GE( cogl_wrap_glDisable (GL_CLIP_PLANE3) ); - GE( cogl_wrap_glDisable (GL_CLIP_PLANE2) ); - GE( cogl_wrap_glDisable (GL_CLIP_PLANE1) ); - GE( cogl_wrap_glDisable (GL_CLIP_PLANE0) ); + GE( glDisable (GL_CLIP_PLANE3) ); + GE( glDisable (GL_CLIP_PLANE2) ); + GE( glDisable (GL_CLIP_PLANE1) ); + GE( glDisable (GL_CLIP_PLANE0) ); } -void -cogl_alpha_func (COGLenum func, - float ref) -{ - GE( cogl_wrap_glAlphaFunc (func, (ref)) ); -} - -/* - * Fixed point implementation of the perspective function - */ void cogl_perspective (float fovy, float aspect, @@ -519,21 +476,21 @@ cogl_perspective (float fovy, float fovy_rad_half = (fovy * G_PI) / 360; GLfloat m[16]; - + _COGL_GET_CONTEXT (ctx, NO_RETVAL); memset (&m[0], 0, sizeof (m)); - GE( cogl_wrap_glMatrixMode (GL_PROJECTION) ); - GE( cogl_wrap_glLoadIdentity () ); + GE( glMatrixMode (GL_PROJECTION) ); + GE( glLoadIdentity () ); - /* + /* * Based on the original algorithm in perspective(): - * + * * 1) xmin = -xmax => xmax + xmin == 0 && xmax - xmin == 2 * xmax * same true for y, hence: a == 0 && b == 0; * - * 2) When working with small numbers, we can are loosing significant + * 2) When working with small numbers, we are loosing significant * precision */ ymax = (zNear * (sinf (fovy_rad_half) / cosf (fovy_rad_half))); @@ -551,9 +508,9 @@ cogl_perspective (float fovy, M(2,3) = d; M(3,2) = -1.0; - GE( cogl_wrap_glMultMatrixf (m) ); + GE( glMultMatrixf (m) ); - GE( cogl_wrap_glMatrixMode (GL_MODELVIEW) ); + GE( glMatrixMode (GL_MODELVIEW) ); /* Calculate and store the inverse of the matrix */ memset (ctx->inverse_projection, 0, sizeof (float) * 16); @@ -581,30 +538,33 @@ cogl_frustum (float left, _COGL_GET_CONTEXT (ctx, NO_RETVAL); - GE( cogl_wrap_glMatrixMode (GL_PROJECTION) ); - GE( cogl_wrap_glLoadIdentity () ); + GE( glMatrixMode (GL_PROJECTION) ); + GE( glLoadIdentity () ); - GE( cogl_wrap_glFrustumf (left, right, - bottom, top, - z_near, z_far) ); + GE( glFrustumf (left, + right, + bottom, + top, + z_near, + z_far) ); - GE( cogl_wrap_glMatrixMode (GL_MODELVIEW) ); + GE( glMatrixMode (GL_MODELVIEW) ); /* Calculate and store the inverse of the matrix */ memset (ctx->inverse_projection, 0, sizeof (float) * 16); - c = -(z_far + z_near / z_far - z_near); - d = -(2 * (z_far * z_near) / z_far - z_near); + c = - (z_far + z_near) / (z_far - z_near); + d = - (2 * (z_far * z_near)) / (z_far - z_near); #define M(row,col) ctx->inverse_projection[col*4+row] - M(0,0) = (right - left / 2 * z_near); - M(0,3) = (right + left / 2 * z_near); - M(1,1) = (top - bottom / 2 * z_near); - M(1,3) = (top + bottom / 2 * z_near); + M(0,0) = (right - left) / (2 * z_near); + M(0,3) = (right + left) / (2 * z_near); + M(1,1) = (top - bottom) / (2 * z_near); + M(1,3) = (top + bottom) / (2 * z_near); M(2,3) = -1.0; - M(3,2) = (1.0 / d); - M(3,3) = (c / d); -#undef M + M(3,2) = 1.0 / d; + M(3,3) = c / d; +#undef M } void @@ -615,26 +575,24 @@ cogl_viewport (guint width, } void -cogl_setup_viewport (guint w, - guint h, +cogl_setup_viewport (guint width, + guint height, float fovy, float aspect, float z_near, float z_far) { - gint width = (gint) w; - gint height = (gint) h; float z_camera; float projection_matrix[16]; - + GE( glViewport (0, 0, width, height) ); /* For Ortho projection. - * cogl_wrap_glOrthof (0, width << 16, 0, height << 16, -1 << 16, 1 << 16); + * glOrthof (0, width << 16, 0, height << 16, -1 << 16, 1 << 16); */ cogl_perspective (fovy, aspect, z_near, z_far); - + /* * camera distance from screen * @@ -644,15 +602,13 @@ cogl_setup_viewport (guint w, cogl_get_projection_matrix (projection_matrix); z_camera = 0.5 * projection_matrix[0]; - GE( cogl_wrap_glLoadIdentity () ); + GE( glLoadIdentity () ); - GE( cogl_wrap_glTranslatef (-0.5f, -0.5f, -z_camera) ); + GE( glTranslatef (-0.5f, -0.5f, -z_camera) ); - GE( cogl_wrap_glScalef ( 1.0 / width, - -1.0 / height, - 1.0 / width) ); + GE( glScalef (1.0f / width, -1.0f / height, 1.0f / width) ); - GE( cogl_wrap_glTranslatef (0, -1.0 * height, 0) ); + GE( glTranslatef (0.0f, -1.0 * height, 0.0f) ); } static void @@ -664,12 +620,12 @@ _cogl_features_init () _COGL_GET_CONTEXT (ctx, NO_RETVAL); - GE( cogl_wrap_glGetIntegerv (GL_STENCIL_BITS, &num_stencil_bits) ); + GE( glGetIntegerv (GL_STENCIL_BITS, &num_stencil_bits) ); /* We need at least three stencil bits to combine clips */ if (num_stencil_bits > 2) flags |= COGL_FEATURE_STENCIL_BUFFER; - GE( cogl_wrap_glGetIntegerv (GL_MAX_CLIP_PLANES, &max_clip_planes) ); + GE( glGetIntegerv (GL_MAX_CLIP_PLANES, &max_clip_planes) ); if (max_clip_planes >= 4) flags |= COGL_FEATURE_FOUR_CLIP_PLANES; @@ -677,6 +633,7 @@ _cogl_features_init () flags |= COGL_FEATURE_SHADERS_GLSL | COGL_FEATURE_OFFSCREEN; #endif + /* Cache features */ ctx->feature_flags = flags; ctx->features_cached = TRUE; } @@ -685,10 +642,10 @@ CoglFeatureFlags cogl_get_features () { _COGL_GET_CONTEXT (ctx, 0); - + if (!ctx->features_cached) _cogl_features_init (); - + return ctx->feature_flags; } @@ -696,23 +653,23 @@ gboolean cogl_features_available (CoglFeatureFlags features) { _COGL_GET_CONTEXT (ctx, 0); - + if (!ctx->features_cached) _cogl_features_init (); - + return (ctx->feature_flags & features) == features; } void cogl_get_modelview_matrix (float m[16]) { - cogl_wrap_glGetFloatv (GL_MODELVIEW_MATRIX, m); + glGetFloatv (GL_MODELVIEW_MATRIX, m); } void cogl_get_projection_matrix (float m[16]) { - cogl_wrap_glGetFloatv (GL_PROJECTION_MATRIX, m); + glGetFloatv (GL_PROJECTION_MATRIX, m); } void @@ -721,7 +678,7 @@ cogl_get_viewport (float v[4]) GLint viewport[4]; int i; - cogl_wrap_glGetIntegerv (GL_VIEWPORT, viewport); + glGetIntegerv (GL_VIEWPORT, viewport); for (i = 0; i < 4; i++) v[i] = (float)(viewport[i]); @@ -730,14 +687,27 @@ cogl_get_viewport (float v[4]) void cogl_get_bitmasks (gint *red, gint *green, gint *blue, gint *alpha) { + GLint value; if (red) - GE( cogl_wrap_glGetIntegerv(GL_RED_BITS, red) ); + { + GE( glGetIntegerv(GL_RED_BITS, &value) ); + *red = value; + } if (green) - GE( cogl_wrap_glGetIntegerv(GL_GREEN_BITS, green) ); + { + GE( glGetIntegerv(GL_GREEN_BITS, &value) ); + *green = value; + } if (blue) - GE( cogl_wrap_glGetIntegerv(GL_BLUE_BITS, blue) ); + { + GE( glGetIntegerv(GL_BLUE_BITS, &value) ); + *blue = value; + } if (alpha) - GE( cogl_wrap_glGetIntegerv(GL_ALPHA_BITS, alpha ) ); + { + GE( glGetIntegerv(GL_ALPHA_BITS, &value ) ); + *alpha = value; + } } void @@ -748,19 +718,20 @@ cogl_fog_set (const CoglColor *fog_color, { GLfloat fogColor[4]; - fogColor[0] = cogl_color_get_red (fog_color); - fogColor[1] = cogl_color_get_green (fog_color); - fogColor[2] = cogl_color_get_blue (fog_color); - fogColor[3] = cogl_color_get_alpha (fog_color); + fogColor[0] = cogl_color_get_red_float (fog_color); + fogColor[1] = cogl_color_get_green_float (fog_color); + fogColor[2] = cogl_color_get_blue_float (fog_color); + fogColor[3] = cogl_color_get_alpha_float (fog_color); - cogl_wrap_glEnable (GL_FOG); + glEnable (GL_FOG); - cogl_wrap_glFogfv (GL_FOG_COLOR, fogColor); + glFogfv (GL_FOG_COLOR, fogColor); - cogl_wrap_glFogf (GL_FOG_MODE, GL_LINEAR); + glFogf (GL_FOG_MODE, GL_LINEAR); glHint (GL_FOG_HINT, GL_NICEST); - cogl_wrap_glFogf (GL_FOG_DENSITY, (GLfloat) density); - cogl_wrap_glFogf (GL_FOG_START, (GLfloat) z_near); - cogl_wrap_glFogf (GL_FOG_END, (GLfloat) z_far); + glFogf (GL_FOG_DENSITY, (GLfloat) density); + glFogf (GL_FOG_START, (GLfloat) z_near); + glFogf (GL_FOG_END, (GLfloat) z_far); } + diff --git a/clutter/pango/cogl-pango-render.c b/clutter/pango/cogl-pango-render.c index 3cafc81e2..0e839c573 100644 --- a/clutter/pango/cogl-pango-render.c +++ b/clutter/pango/cogl-pango-render.c @@ -42,8 +42,10 @@ struct _CoglPangoRenderer { PangoRenderer parent_instance; - /* The color to draw the glyphs with */ - CoglColor color; + /* The material used to texture from the glyph cache with */ + CoglHandle glyph_material; + /* The material used for solid fills. (boxes, rectangles + trapezoids) */ + CoglHandle solid_material; /* Two caches of glyphs as textures, one with mipmapped textures and one without */ @@ -68,8 +70,10 @@ cogl_pango_renderer_glyphs_end (CoglPangoRenderer *priv) if (priv->glyph_rectangles->len > 0) { float *rectangles = (float *) priv->glyph_rectangles->data; - cogl_texture_multiple_rectangles (priv->glyph_texture, rectangles, - priv->glyph_rectangles->len / 8); + cogl_material_set_layer (priv->glyph_material, 0, priv->glyph_texture); + cogl_set_source (priv->glyph_material); + cogl_rectangles_with_texture_coords (rectangles, + priv->glyph_rectangles->len / 8); g_array_set_size (priv->glyph_rectangles, 0); } } @@ -128,6 +132,38 @@ G_DEFINE_TYPE (CoglPangoRenderer, cogl_pango_renderer, PANGO_TYPE_RENDERER); static void cogl_pango_renderer_init (CoglPangoRenderer *priv) { + priv->glyph_material = cogl_material_new (); + + /* 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 solid font color) + * + * Since our glyph cache textures are component alpha textures, and so the + * RGB channels are defined as (0, 0, 0) we don't want to modulate the RGB + * channels, instead we want to simply replace them with our solid font + * color... + * + * XXX: we could really do with a neat string based way for describing + * combine modes, like: "REPLACE(PREVIOUS[RGB])" + * XXX: potentially materials could have a fuzzy default combine mode + * such that they default to this for component alpha textures? This would + * give the same semantics as the old-style GL_MODULATE mode but sounds a + * bit hacky. + */ + cogl_material_set_layer_combine_function ( + priv->glyph_material, + 0, /* layer */ + COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGB, + COGL_MATERIAL_LAYER_COMBINE_FUNC_REPLACE); + cogl_material_set_layer_combine_arg_src ( + priv->glyph_material, + 0, /* layer */ + 0, /* arg */ + COGL_MATERIAL_LAYER_COMBINE_CHANNELS_RGB, + COGL_MATERIAL_LAYER_COMBINE_SRC_PREVIOUS); + + priv->solid_material = cogl_material_new (); + priv->glyph_cache = cogl_pango_glyph_cache_new (FALSE); priv->mipmapped_glyph_cache = cogl_pango_glyph_cache_new (TRUE); priv->use_mipmapping = FALSE; @@ -203,7 +239,8 @@ cogl_pango_render_layout_subpixel (PangoLayout *layout, if (G_UNLIKELY (!priv)) return; - priv->color = *color; + cogl_material_set_color (priv->glyph_material, color); + cogl_material_set_color (priv->solid_material, color); pango_renderer_draw_layout (PANGO_RENDERER (priv), layout, x, y); } @@ -259,7 +296,8 @@ cogl_pango_render_layout_line (PangoLayoutLine *line, if (G_UNLIKELY (!priv)) return; - priv->color = *color; + cogl_material_set_color (priv->glyph_material, color); + cogl_material_set_color (priv->solid_material, color); pango_renderer_draw_layout_line (PANGO_RENDERER (priv), line, x, y); } @@ -351,12 +389,12 @@ cogl_pango_ensure_glyph_cache_for_layout (PangoLayout *layout) PangoContext *context; PangoRenderer *renderer; PangoLayoutIter *iter; - + g_return_if_fail (PANGO_IS_LAYOUT (layout)); - + if ((iter = pango_layout_get_iter (layout)) == NULL) return; - + context = pango_layout_get_context (layout); renderer = PANGO_RENDERER (cogl_pango_get_renderer_from_context (context)); @@ -365,19 +403,19 @@ cogl_pango_ensure_glyph_cache_for_layout (PangoLayout *layout) { PangoLayoutLine *line; GSList *l; - + line = pango_layout_iter_get_line_readonly (iter); - + for (l = line->runs; l; l = l->next) { PangoLayoutRun *run = l->data; PangoGlyphString *glyphs = run->glyphs; int i; - + for (i = 0; i < glyphs->num_glyphs; i++) { PangoGlyphInfo *gi = &glyphs->glyphs[i]; - + cogl_pango_renderer_get_cached_glyph (renderer, run->item->analysis.font, gi->glyph); @@ -385,7 +423,7 @@ cogl_pango_ensure_glyph_cache_for_layout (PangoLayout *layout) } } while (pango_layout_iter_next_line (iter)); - + pango_layout_iter_free (iter); } @@ -398,19 +436,32 @@ cogl_pango_renderer_set_color_for_part (PangoRenderer *renderer, if (pango_color) { - cogl_set_source_color4ub (pango_color->red >> 8, - pango_color->green >> 8, - pango_color->blue >> 8, - cogl_color_get_alpha_byte (&priv->color)); + CoglColor color; + guint8 red = pango_color->red >> 8; + guint8 green = pango_color->green >> 8; + guint8 blue = pango_color->blue >> 8; + guint8 alpha; + + cogl_material_get_color (priv->solid_material, &color); + alpha = cogl_color_get_alpha_byte (&color); + + cogl_material_set_color4ub (priv->solid_material, + red, green, blue, alpha); + cogl_material_set_color4ub (priv->glyph_material, + red, green, blue, alpha); } - else - cogl_set_source_color (&priv->color); } static void -cogl_pango_renderer_draw_box (int x, int y, - int width, int height) +cogl_pango_renderer_draw_box (PangoRenderer *renderer, + int x, + int y, + int width, + int height) { + CoglPangoRenderer *priv = COGL_PANGO_RENDERER (renderer); + + cogl_set_source (priv->solid_material); cogl_path_rectangle ((float)(x), (float)(y - height), (float)(width), @@ -450,6 +501,7 @@ cogl_pango_renderer_draw_rectangle (PangoRenderer *renderer, int width, int height) { + CoglPangoRenderer *priv = COGL_PANGO_RENDERER (renderer); float x1, x2, y1, y2; cogl_pango_renderer_set_color_for_part (renderer, part); @@ -461,6 +513,7 @@ cogl_pango_renderer_draw_rectangle (PangoRenderer *renderer, x + width, y + height, &x2, &y2); + cogl_set_source (priv->solid_material); cogl_rectangle (x1, y1, x2 - x1, y2 - y1); } @@ -474,6 +527,7 @@ cogl_pango_renderer_draw_trapezoid (PangoRenderer *renderer, double x12, double x22) { + CoglPangoRenderer *priv = COGL_PANGO_RENDERER (renderer); float points[8]; points[0] = (x11); @@ -487,6 +541,7 @@ cogl_pango_renderer_draw_trapezoid (PangoRenderer *renderer, cogl_pango_renderer_set_color_for_part (renderer, part); + cogl_set_source (priv->solid_material); cogl_path_polygon (points, 4); cogl_path_fill (); } @@ -524,15 +579,17 @@ cogl_pango_renderer_draw_glyphs (PangoRenderer *renderer, if (font == NULL || (metrics = pango_font_get_metrics (font, NULL)) == NULL) { - cogl_pango_renderer_draw_box ( (x), - (y), + cogl_pango_renderer_draw_box (renderer, + x, + y, PANGO_UNKNOWN_GLYPH_WIDTH, PANGO_UNKNOWN_GLYPH_HEIGHT); } else { - cogl_pango_renderer_draw_box ( (x), - (y), + cogl_pango_renderer_draw_box (renderer, + x, + y, metrics->approximate_char_width / PANGO_SCALE, metrics->ascent / PANGO_SCALE); @@ -553,8 +610,9 @@ cogl_pango_renderer_draw_glyphs (PangoRenderer *renderer, { cogl_pango_renderer_glyphs_end (priv); - cogl_pango_renderer_draw_box ( (x), - (y), + cogl_pango_renderer_draw_box (renderer, + x, + y, PANGO_UNKNOWN_GLYPH_WIDTH, PANGO_UNKNOWN_GLYPH_HEIGHT); } diff --git a/doc/reference/cogl/cogl-docs.sgml b/doc/reference/cogl/cogl-docs.sgml index 56e6bc8bf..ea48e5a1a 100644 --- a/doc/reference/cogl/cogl-docs.sgml +++ b/doc/reference/cogl/cogl-docs.sgml @@ -55,7 +55,9 @@ + + diff --git a/doc/reference/cogl/cogl-sections.txt b/doc/reference/cogl/cogl-sections.txt index cf3329f18..a00db7c77 100644 --- a/doc/reference/cogl/cogl-sections.txt +++ b/doc/reference/cogl/cogl-sections.txt @@ -310,3 +310,52 @@ cogl_vertex_buffer_draw cogl_vertex_buffer_draw_range_elements +
+cogl-matrix +Matrices +CoglMatrix +cogl_matrix_init_identity +cogl_matrix_multiply +cogl_matrix_rotate +cogl_matrix_translate +cogl_matrix_scale +
+ +
+cogl-material +Materials +cogl_material_new +cogl_material_ref +cogl_material_unref +cogl_material_set_diffuse +cogl_material_set_ambient +cogl_material_set_ambient_and_diffuse +cogl_material_set_specular +cogl_material_set_shininess +cogl_material_set_emission +cogl_set_source +CoglMaterialAlphaFunc +cogl_material_set_alpha_test_function +CoglMaterialBlendFactor +cogl_material_set_blend_factors +cogl_material_set_layer +cogl_material_remove_layer +CoglMaterialLayerCombineFunc +cogl_material_set_layer_combine_function +CoglMaterialLayerCombineChannels +CoglMaterialLayerCombineSrc +cogl_material_set_layer_combine_arg_src +CoglMaterialLayerCombineOp +cogl_material_set_layer_combine_arg_op +cogl_material_set_layer_matrix +cogl_material_get_cogl_enable_flags +cogl_material_flush_gl_material_state +cogl_material_flush_gl_alpha_func +cogl_material_flush_gl_blend_func +cogl_material_get_layers +CoglMaterialLayerType +cogl_material_layer_get_type +cogl_material_layer_get_texture +cogl_material_layer_flush_gl_sampler_state +
+ diff --git a/tests/conform/test-backface-culling.c b/tests/conform/test-backface-culling.c index f43b7a38b..f021ce81c 100644 --- a/tests/conform/test-backface-culling.c +++ b/tests/conform/test-backface-culling.c @@ -127,47 +127,45 @@ on_paint (ClutterActor *actor, TestState *state) x2 = x1 + (float)(TEXTURE_SIZE); /* Draw a front-facing texture */ - cogl_texture_rectangle (state->texture, - x1, y1, x2, y2, - 0, 0, 1.0, 1.0); + cogl_set_source_texture (state->texture); + cogl_rectangle (x1, y1, x2, y2); x1 = x2; x2 = x1 + (float)(TEXTURE_SIZE); /* Draw a back-facing texture */ - cogl_texture_rectangle (state->texture, - x2, y1, x1, y2, - 0, 0, 1.0, 1.0); + cogl_set_source_texture (state->texture); + cogl_rectangle (x2, y1, x1, y2); x1 = x2; x2 = x1 + (float)(TEXTURE_SIZE); /* Draw a front-facing texture polygon */ - verts[0].x = x1; verts[0].y = y2; - verts[1].x = x2; verts[1].y = y2; - verts[2].x = x2; verts[2].y = y1; - verts[3].x = x1; verts[3].y = y1; - verts[0].tx = 0; verts[0].ty = 0; + verts[0].x = x1; verts[0].y = y2; + verts[1].x = x2; verts[1].y = y2; + verts[2].x = x2; verts[2].y = y1; + verts[3].x = x1; verts[3].y = y1; + verts[0].tx = 0; verts[0].ty = 0; verts[1].tx = 1.0; verts[1].ty = 0; verts[2].tx = 1.0; verts[2].ty = 1.0; - verts[3].tx = 0; verts[3].ty = 1.0; - cogl_texture_polygon (state->texture, 4, - verts, FALSE); + verts[3].tx = 0; verts[3].ty = 1.0; + cogl_set_source_texture (state->texture); + cogl_polygon (verts, 4, FALSE); x1 = x2; x2 = x1 + (float)(TEXTURE_SIZE); /* Draw a back-facing texture polygon */ - verts[0].x = x1; verts[0].y = y1; - verts[1].x = x2; verts[1].y = y1; - verts[2].x = x2; verts[2].y = y2; - verts[3].x = x1; verts[3].y = y2; - verts[0].tx = 0; verts[0].ty = 0; + verts[0].x = x1; verts[0].y = y1; + verts[1].x = x2; verts[1].y = y1; + verts[2].x = x2; verts[2].y = y2; + verts[3].x = x1; verts[3].y = y2; + verts[0].tx = 0; verts[0].ty = 0; verts[1].tx = 1.0; verts[1].ty = 0; verts[2].tx = 1.0; verts[2].ty = 1.0; - verts[3].tx = 0; verts[3].ty = 1.0; - cogl_texture_polygon (state->texture, 4, - verts, FALSE); + verts[3].tx = 0; verts[3].ty = 1.0; + cogl_set_source_texture (state->texture); + cogl_polygon (verts, 4, FALSE); x1 = x2; x2 = x1 + (float)(TEXTURE_SIZE); diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am index c69042d19..58efa73c5 100644 --- a/tests/data/Makefile.am +++ b/tests/data/Makefile.am @@ -1,5 +1,7 @@ EXTRA_DIST = \ redhand.png \ + redhand_alpha.png \ + light0.png \ test-script.json diff --git a/tests/data/light0.png b/tests/data/light0.png new file mode 100644 index 000000000..66871ddb3 Binary files /dev/null and b/tests/data/light0.png differ diff --git a/tests/data/redhand_alpha.png b/tests/data/redhand_alpha.png new file mode 100644 index 000000000..4270765ed Binary files /dev/null and b/tests/data/redhand_alpha.png differ diff --git a/tests/interactive/Makefile.am b/tests/interactive/Makefile.am index 5fa542886..ad8779a81 100644 --- a/tests/interactive/Makefile.am +++ b/tests/interactive/Makefile.am @@ -28,6 +28,7 @@ UNIT_TESTS = \ test-cogl-tex-getset.c \ test-cogl-offscreen.c \ test-cogl-tex-polygon.c \ + test-cogl-multitexture.c \ test-stage-read-pixels.c \ test-random-text.c \ test-clip.c \ @@ -51,6 +52,12 @@ test-script.json: ln -sf $(top_srcdir)/tests/data/test-script.json redhand.png: ln -sf $(top_srcdir)/tests/data/redhand.png +redhand_alpha.png: + ln -sf $(top_srcdir)/tests/data/redhand_alpha.png +light0.png: + ln -sf $(top_srcdir)/tests/data/light0.png +light1.png: + ln -sf $(top_srcdir)/tests/data/light1.png # For convenience, this provides a way to easily run individual unit tests: .PHONY: wrappers @@ -62,7 +69,7 @@ wrappers: test-interactive$(EXEEXT) # NB: BUILT_SOURCES here a misnomer. We aren't building source, just inserting # a phony rule that will generate symlink scripts for running individual tests -BUILT_SOURCES = wrappers redhand.png test-script.json +BUILT_SOURCES = wrappers redhand.png redhand_alpha.png light0.png light1.png test-script.json INCLUDES = -I$(top_srcdir)/ -I$(top_srcdir)/clutter -I$(top_builddir)/clutter LDADD = $(top_builddir)/clutter/libclutter-@CLUTTER_FLAVOUR@-@CLUTTER_MAJORMINOR@.la diff --git a/tests/interactive/test-clip.c b/tests/interactive/test-clip.c index 119b4ac68..ed3f8d7b0 100644 --- a/tests/interactive/test-clip.c +++ b/tests/interactive/test-clip.c @@ -165,12 +165,12 @@ on_paint (ClutterActor *actor, CallbackData *data) cogl_set_source_color4ub (0xff, 0xff, 0xff, 0xff); - cogl_texture_rectangle (data->hand, - CLUTTER_INT_TO_FIXED (-hand_width / 2), - CLUTTER_INT_TO_FIXED (-hand_height / 2), - CLUTTER_INT_TO_FIXED (hand_width / 2), - CLUTTER_INT_TO_FIXED (hand_height / 2), - 0, 0, CFX_ONE, CFX_ONE); + cogl_set_source_texture (data->hand); + cogl_rectangle_with_texture_coords (CLUTTER_INT_TO_FIXED (-hand_width / 2), + CLUTTER_INT_TO_FIXED (-hand_height / 2), + CLUTTER_INT_TO_FIXED (hand_width / 2), + CLUTTER_INT_TO_FIXED (hand_height / 2), + 0, 0, CFX_ONE, CFX_ONE); cogl_pop_matrix (); } diff --git a/tests/interactive/test-cogl-multitexture.c b/tests/interactive/test-cogl-multitexture.c new file mode 100644 index 000000000..4f0dbfc8b --- /dev/null +++ b/tests/interactive/test-cogl-multitexture.c @@ -0,0 +1,160 @@ +#include +#include +#include + +#include +#include +#include + +#include +#include + +#define TIMELINE_FRAME_COUNT 200 + +typedef struct _TestMultiLayerMaterialState +{ + ClutterActor *group; + CoglHandle material; + CoglHandle alpha_tex; + CoglHandle redhand_tex; + CoglHandle light_tex0; + ClutterFixed *tex_coords; + + CoglMatrix tex_matrix; + CoglMatrix rot_matrix; + +} TestMultiLayerMaterialState; + + +static void +frame_cb (ClutterTimeline *timeline, + gint frame_no, + gpointer data) +{ + TestMultiLayerMaterialState *state = data; + + cogl_matrix_multiply (&state->tex_matrix, + &state->tex_matrix, + &state->rot_matrix); + cogl_material_set_layer_matrix (state->material, 2, &state->tex_matrix); +} + +static void +material_rectangle_paint (ClutterActor *actor, gpointer data) +{ + TestMultiLayerMaterialState *state = data; + + cogl_set_source (state->material); + cogl_rectangle_with_multitexture_coords ( + CLUTTER_INT_TO_FIXED(0), + CLUTTER_INT_TO_FIXED(0), + CLUTTER_INT_TO_FIXED(TIMELINE_FRAME_COUNT), + CLUTTER_INT_TO_FIXED(TIMELINE_FRAME_COUNT), + state->tex_coords, + 12); +} + +G_MODULE_EXPORT int +test_cogl_multitexture_main (int argc, char *argv[]) +{ + ClutterTimeline *timeline; + ClutterAlpha *alpha; + ClutterBehaviour *r_behave; + ClutterActor *stage; + ClutterColor stage_color = { 0x61, 0x56, 0x56, 0xff }; + TestMultiLayerMaterialState *state = g_new0 (TestMultiLayerMaterialState, 1); + ClutterGeometry geom; + ClutterFixed tex_coords[] = + { + /* tx1 ty1 tx2 ty2 */ + 0, 0, CLUTTER_INT_TO_FIXED (1), CLUTTER_INT_TO_FIXED (1), + 0, 0, CLUTTER_INT_TO_FIXED (1), CLUTTER_INT_TO_FIXED (1), + 0, 0, CLUTTER_INT_TO_FIXED (1), CLUTTER_INT_TO_FIXED (1) + }; + + clutter_init (&argc, &argv); + + stage = clutter_stage_get_default (); + clutter_actor_get_geometry (stage, &geom); + + clutter_stage_set_color (CLUTTER_STAGE (stage), + &stage_color); + + /* We create a non-descript actor that we know doesn't have a + * default paint handler, so that we can easily control + * painting in a paint signal handler, without having to + * sub-class anything etc. */ + state->group = clutter_group_new (); + clutter_actor_set_position (state->group, geom.width/2, geom.height/2); + g_signal_connect (state->group, "paint", + G_CALLBACK(material_rectangle_paint), state); + + state->alpha_tex = + cogl_texture_new_from_file ("./redhand_alpha.png", + -1, /* disable slicing */ + TRUE, + COGL_PIXEL_FORMAT_ANY, + NULL); + state->redhand_tex = + cogl_texture_new_from_file ("./redhand.png", + -1, /* disable slicing */ + TRUE, + COGL_PIXEL_FORMAT_ANY, + NULL); + state->light_tex0 = + cogl_texture_new_from_file ("./light0.png", + -1, /* disable slicing */ + TRUE, + COGL_PIXEL_FORMAT_ANY, + NULL); + + state->material = cogl_material_new (); + cogl_material_set_layer (state->material, 0, state->alpha_tex); + cogl_material_set_layer (state->material, 1, state->redhand_tex); + cogl_material_set_layer (state->material, 2, state->light_tex0); + + state->tex_coords = tex_coords; + + cogl_matrix_init_identity (&state->tex_matrix); + cogl_matrix_init_identity (&state->rot_matrix); + + cogl_matrix_translate (&state->rot_matrix, 0.5, 0.5, 0); + cogl_matrix_rotate (&state->rot_matrix, 10.0, 0, 0, 1.0); + cogl_matrix_translate (&state->rot_matrix, -0.5, -0.5, 0); + + clutter_actor_set_anchor_point (state->group, 86, 125); + clutter_container_add_actor (CLUTTER_CONTAINER(stage), + state->group); + + timeline = clutter_timeline_new (TIMELINE_FRAME_COUNT, 26 /* fps */); + g_object_set (timeline, "loop", TRUE, NULL); + + g_signal_connect (timeline, "new-frame", G_CALLBACK (frame_cb), state); + + r_behave = + clutter_behaviour_rotate_new (clutter_alpha_new_full (timeline, + CLUTTER_LINEAR), + CLUTTER_Y_AXIS, + CLUTTER_ROTATE_CW, + 0.0, 360.0); + + /* Apply it to our actor */ + clutter_behaviour_apply (r_behave, state->group); + + /* start the timeline and thus the animations */ + clutter_timeline_start (timeline); + + clutter_actor_show_all (stage); + + clutter_main(); + + cogl_material_unref (state->material); + cogl_texture_unref (state->alpha_tex); + cogl_texture_unref (state->redhand_tex); + cogl_texture_unref (state->light_tex0); + g_free (state); + + g_object_unref (r_behave); + + return 0; +} diff --git a/tests/interactive/test-cogl-offscreen.c b/tests/interactive/test-cogl-offscreen.c index 893862e08..eabb754f7 100644 --- a/tests/interactive/test-cogl-offscreen.c +++ b/tests/interactive/test-cogl-offscreen.c @@ -87,20 +87,20 @@ test_coglbox_paint(ClutterActor *self) CLUTTER_FLOAT_TO_FIXED (1.0f), CLUTTER_FLOAT_TO_FIXED (1.0f) }; + CoglHandle material; priv = TEST_COGLBOX_GET_PRIVATE (self); cogl_set_source_color4ub (0x66, 0x66, 0xdd, 0xff); cogl_rectangle (0, 0, 400, 400); - cogl_set_source_color4ub (0xff, 0xff, 0xff, 0xff); - cogl_texture_rectangle (priv->texhand_id, - 0, 0, - CLUTTER_INT_TO_FIXED (400), - CLUTTER_INT_TO_FIXED (400), - 0, 0, - CLUTTER_INT_TO_FIXED (6), - CLUTTER_INT_TO_FIXED (6)); + cogl_set_source_texture (priv->texhand_id); + cogl_rectangle_with_texture_coords (0, 0, + CLUTTER_INT_TO_FIXED (400), + CLUTTER_INT_TO_FIXED (400), + 0, 0, + CLUTTER_INT_TO_FIXED (6), + CLUTTER_INT_TO_FIXED (6)); cogl_draw_buffer (COGL_OFFSCREEN_BUFFER, priv->offscreen_id); @@ -112,16 +112,18 @@ test_coglbox_paint(ClutterActor *self) cogl_draw_buffer (COGL_WINDOW_BUFFER, 0); - cogl_set_source_color4ub (0xff, 0xff, 0xff, 0x88); - cogl_texture_rectangle (priv->texture_id, - CLUTTER_INT_TO_FIXED (100), - CLUTTER_INT_TO_FIXED (100), - CLUTTER_INT_TO_FIXED (300), - CLUTTER_INT_TO_FIXED (300), - texcoords[0], - texcoords[1], - texcoords[2], - texcoords[3]); + material = cogl_material_new (); + cogl_material_set_color4ub (material, 0xff, 0xff, 0xff, 0x88); + cogl_material_set_layer (material, 0, priv->texture_id); + cogl_set_source (material); + cogl_rectangle_with_texture_coords (CLUTTER_INT_TO_FIXED (100), + CLUTTER_INT_TO_FIXED (100), + CLUTTER_INT_TO_FIXED (300), + CLUTTER_INT_TO_FIXED (300), + texcoords[0], + texcoords[1], + texcoords[2], + texcoords[3]); } static void diff --git a/tests/interactive/test-cogl-tex-convert.c b/tests/interactive/test-cogl-tex-convert.c index 816ea403b..a19bd29ae 100644 --- a/tests/interactive/test-cogl-tex-convert.c +++ b/tests/interactive/test-cogl-tex-convert.c @@ -92,45 +92,43 @@ test_coglbox_paint(ClutterActor *self) cogl_set_source_color4ub (0x66, 0x66, 0xdd, 0xff); cogl_rectangle (0, 0, 400, 400); - cogl_set_source_color4ub (0xff, 0xff, 0xff, 0xff); - cogl_push_matrix (); - cogl_texture_rectangle (priv->cogl_tex_id[0], - 0, 0, - CLUTTER_INT_TO_FIXED (200), - CLUTTER_INT_TO_FIXED (213), - texcoords[0], texcoords[1], - texcoords[2], texcoords[3]); + cogl_set_source_texture (priv->cogl_tex_id[0]); + cogl_rectangle_with_texture_coords (0, 0, + CLUTTER_INT_TO_FIXED (200), + CLUTTER_INT_TO_FIXED (213), + texcoords[0], texcoords[1], + texcoords[2], texcoords[3]); cogl_pop_matrix (); cogl_push_matrix (); cogl_translate (200, 0, 0); - cogl_texture_rectangle (priv->cogl_tex_id[1], - 0, 0, - CLUTTER_INT_TO_FIXED (200), - CLUTTER_INT_TO_FIXED (213), - texcoords[0], texcoords[1], - texcoords[2], texcoords[3]); + cogl_set_source_texture (priv->cogl_tex_id[1]); + cogl_rectangle_with_texture_coords (0, 0, + CLUTTER_INT_TO_FIXED (200), + CLUTTER_INT_TO_FIXED (213), + texcoords[0], texcoords[1], + texcoords[2], texcoords[3]); cogl_pop_matrix (); cogl_push_matrix (); cogl_translate (0, 200, 0); - cogl_texture_rectangle (priv->cogl_tex_id[2], - 0, 0, - CLUTTER_INT_TO_FIXED (200), - CLUTTER_INT_TO_FIXED (213), - texcoords[0], texcoords[1], - texcoords[2], texcoords[3]); + cogl_set_source_texture (priv->cogl_tex_id[2]); + cogl_rectangle_with_texture_coords (0, 0, + CLUTTER_INT_TO_FIXED (200), + CLUTTER_INT_TO_FIXED (213), + texcoords[0], texcoords[1], + texcoords[2], texcoords[3]); cogl_pop_matrix (); cogl_push_matrix (); cogl_translate (200, 200, 0); - cogl_texture_rectangle (priv->cogl_tex_id[3], - 0, 0, - CLUTTER_INT_TO_FIXED (200), - CLUTTER_INT_TO_FIXED (213), - texcoords[0], texcoords[1], - texcoords[2], texcoords[3]); + cogl_set_source_texture (priv->cogl_tex_id[3]); + cogl_rectangle_with_texture_coords (0, 0, + CLUTTER_INT_TO_FIXED (200), + CLUTTER_INT_TO_FIXED (213), + texcoords[0], texcoords[1], + texcoords[2], texcoords[3]); cogl_pop_matrix(); } diff --git a/tests/interactive/test-cogl-tex-foreign.c b/tests/interactive/test-cogl-tex-foreign.c index 09d693b24..88c8f8bb5 100644 --- a/tests/interactive/test-cogl-tex-foreign.c +++ b/tests/interactive/test-cogl-tex-foreign.c @@ -92,17 +92,15 @@ test_coglbox_paint(ClutterActor *self) cogl_set_source_color4ub (0x66, 0x66, 0xdd, 0xff); cogl_rectangle (0,0,400,400); - cogl_set_source_color4ub (0xff, 0xff, 0xff, 0xff); - cogl_push_matrix (); cogl_translate (100,100,0); - cogl_texture_rectangle (priv->cogl_handle, - 0, 0, - CLUTTER_INT_TO_FIXED (200), - CLUTTER_INT_TO_FIXED (200), - texcoords[0], texcoords[1], - texcoords[2], texcoords[3]); + cogl_set_source_texture (priv->cogl_handle); + cogl_rectangle_with_texture_coords (0, 0, + CLUTTER_INT_TO_FIXED (200), + CLUTTER_INT_TO_FIXED (200), + texcoords[0], texcoords[1], + texcoords[2], texcoords[3]); cogl_pop_matrix(); } diff --git a/tests/interactive/test-cogl-tex-getset.c b/tests/interactive/test-cogl-tex-getset.c index 056b5693d..84b3b6141 100644 --- a/tests/interactive/test-cogl-tex-getset.c +++ b/tests/interactive/test-cogl-tex-getset.c @@ -91,17 +91,15 @@ test_coglbox_paint(ClutterActor *self) cogl_set_source_color4ub (0x66, 0x66, 0xdd, 0xff); cogl_rectangle (0, 0, 400, 400); - cogl_set_source_color4ub (0xff, 0xff, 0xff, 0xff); - cogl_push_matrix (); cogl_translate (100, 100, 0); - cogl_texture_rectangle (priv->cogl_tex_id[1], - 0, 0, - CLUTTER_INT_TO_FIXED (200), - CLUTTER_INT_TO_FIXED (213), - texcoords[0], texcoords[1], - texcoords[2], texcoords[3]); + cogl_set_source_texture (priv->cogl_tex_id[1]); + cogl_rectangle_with_texture_coords (0, 0, + CLUTTER_INT_TO_FIXED (200), + CLUTTER_INT_TO_FIXED (213), + texcoords[0], texcoords[1], + texcoords[2], texcoords[3]); cogl_pop_matrix(); } diff --git a/tests/interactive/test-cogl-tex-polygon.c b/tests/interactive/test-cogl-tex-polygon.c index ae64998df..9bf9580d7 100644 --- a/tests/interactive/test-cogl-tex-polygon.c +++ b/tests/interactive/test-cogl-tex-polygon.c @@ -122,7 +122,8 @@ test_coglbox_fade_texture (CoglHandle tex_id, ((i ^ (i >> 1)) & 1) ? 0 : 128); } - cogl_texture_polygon (tex_id, 4, vertices, TRUE); + cogl_set_source_texture (tex_id); + cogl_polygon (vertices, 4, TRUE); cogl_set_source_color4ub (255, 255, 255, 255); } @@ -160,7 +161,8 @@ test_coglbox_triangle_texture (CoglHandle tex_id, vertices[2].tx = tx3; vertices[2].ty = ty3; - cogl_texture_polygon (tex_id, 3, vertices, FALSE); + cogl_set_source_texture (tex_id); + cogl_polygon (vertices, 3, FALSE); } static void @@ -172,8 +174,6 @@ test_coglbox_paint (ClutterActor *self) int tex_width = cogl_texture_get_width (tex_handle); int tex_height = cogl_texture_get_height (tex_handle); - cogl_set_source_color4ub (255, 255, 255, 255); - cogl_texture_set_filters (tex_handle, priv->use_linear_filtering ? CGL_LINEAR : CGL_NEAREST, @@ -186,11 +186,11 @@ test_coglbox_paint (ClutterActor *self) cogl_translate (-tex_width / 2, 0, 0); /* Draw a hand and refect it */ - cogl_texture_rectangle (tex_handle, - 0, 0, - CLUTTER_INT_TO_FIXED (tex_width), - CLUTTER_INT_TO_FIXED (tex_height), - 0, 0, CFX_ONE, CFX_ONE); + cogl_set_source_texture (tex_handle); + cogl_rectangle_with_texture_coords (0, 0, + CLUTTER_INT_TO_FIXED (tex_width), + CLUTTER_INT_TO_FIXED (tex_height), + 0, 0, CFX_ONE, CFX_ONE); test_coglbox_fade_texture (tex_handle, 0, CLUTTER_INT_TO_FIXED (tex_height), CLUTTER_INT_TO_FIXED (tex_width), diff --git a/tests/interactive/test-cogl-tex-tile.c b/tests/interactive/test-cogl-tex-tile.c index 0e69c36aa..5388c5de5 100644 --- a/tests/interactive/test-cogl-tex-tile.c +++ b/tests/interactive/test-cogl-tex-tile.c @@ -116,14 +116,13 @@ test_coglbox_paint(ClutterActor *self) cogl_set_source_color4ub (0x66, 0x66, 0xdd, 0xff); cogl_rectangle (0, 0, 400, 400); - cogl_set_source_color4ub (0xff, 0xff, 0xff, 0xff); cogl_translate (100, 100, 0); - cogl_texture_rectangle (priv->cogl_tex_id, - 0, 0, - CLUTTER_INT_TO_FIXED (200), - CLUTTER_INT_TO_FIXED (213), - texcoords[0], texcoords[1], - texcoords[2], texcoords[3]); + cogl_set_source_texture (priv->cogl_tex_id); + cogl_rectangle_with_texture_coords (0, 0, + CLUTTER_INT_TO_FIXED (200), + CLUTTER_INT_TO_FIXED (213), + texcoords[0], texcoords[1], + texcoords[2], texcoords[3]); cogl_pop_matrix(); }