CoglMaterial: Implements sparse materials design

This is a complete overhaul of the data structures used to manage
CoglMaterial state.

We have these requirements that were aiming to meet:
(Note: the references to "renderlists" correspond to the effort to
support scenegraph level shuffling of Clutter actor primitives so we can
minimize GPU state changes)

Sparse State:
We wanted a design that allows sparse descriptions of state so it scales
well as we make CoglMaterial responsible for more and more state. It
needs to scale well in terms of memory usage and the cost of operations
we need to apply to materials such as comparing, copying and flushing
their state. I.e. we would rather have these things scale by the number
of real changes a material represents not by how much overall state
CoglMaterial becomes responsible for.

Cheap Copies:
As we add support for renderlists in Clutter we will need to be able to
get an immutable handle for a given material's current state so that we
can retain a record of a primitive with its associated material without
worrying that changes to the original material will invalidate that
record.

No more flush override options:
We want to get rid of the flush overrides mechanism we currently use to
deal with texture fallbacks, wrap mode changes and to handle the use of
highlevel CoglTextures that need to be resolved into lowlevel textures
before flushing the material state.

The flush options structure has been expanding in size and the structure
is logged with every journal entry so it is not an approach that scales
well at all. It also makes flushing material state that much more
complex.

Weak Materials:
Again for renderlists we need a way to create materials derived from
other materials but without the strict requirement that modifications to
the original material wont affect the derived ("weak") material. The
only requirement is that its possible to later check if the original
material has been changed.

A summary of the new design:

A CoglMaterial now basically represents a diff against its parent.
Each material has a single parent and a mask of state that it changes.

Each group of state (such as the blending state) has an "authority"
which is found by walking up from a given material through its ancestors
checking the difference mask until a match for that group is found.

There is only one root node to the graph of all materials, which is the
default material first created when Cogl is being initialized.

All the groups of state are divided into two types, such that
infrequently changed state belongs in a separate "BigState" structure
that is only allocated and attached to a material when necessary.

CoglMaterialLayers are another sparse structure. Like CoglMaterials they
represent a diff against their parent and all the layers are part of
another graph with the "default_layer_0" layer being the root node that
Cogl creates during initialization.

Copying a material is now basically just a case of slice allocating a
CoglMaterial, setting the parent to be the source being copied and
zeroing the mask of changes.

Flush overrides should now be handled by simply relying on the cheapness
of copying a material and making changes to it. (This will be done in a
follow on commit)

Weak material support will be added in a follow on commit.
This commit is contained in:
Robert Bragg 2010-04-08 12:21:04 +01:00
parent 5f9c7d3657
commit 365605cf42
14 changed files with 5009 additions and 1935 deletions

View File

@ -374,6 +374,11 @@ _cogl_atlas_texture_migrate_out_of_atlas (CoglAtlasTexture *atlas_tex)
*/ */
_cogl_journal_flush (); _cogl_journal_flush ();
/* Notify cogl-material.c that the texture's underlying GL texture
* storage is changing so it knows it may need to bind a new texture
* if the CoglTexture is reused with the same texture unit. */
_cogl_material_texture_storage_change_notify (atlas_tex);
cogl_handle_unref (atlas_tex->sub_texture); cogl_handle_unref (atlas_tex->sub_texture);
/* Create a new texture at the right size, not including the /* Create a new texture at the right size, not including the
@ -686,6 +691,11 @@ _cogl_atlas_texture_migrate (unsigned int n_textures,
for (i = 0; i < n_textures; i++) for (i = 0; i < n_textures; i++)
{ {
/* Notify cogl-material.c that the texture's underlying GL texture
* storage is changing so it knows it may need to bind a new texture
* if the CoglTexture is reused with the same texture unit. */
_cogl_material_texture_storage_change_notify (textures[i].texture);
/* Skip the texture that is being added because it doesn't contain /* Skip the texture that is being added because it doesn't contain
any data yet */ any data yet */
if (textures[i].texture != skip_texture) if (textures[i].texture != skip_texture)

View File

@ -67,9 +67,9 @@ cogl_create_context (void)
_cogl_features_init (); _cogl_features_init ();
_cogl_material_init_default_material (); _cogl_material_init_default_material ();
_cogl_material_init_default_layers ();
_context->enable_flags = 0; _context->enable_flags = 0;
_context->color_alpha = 0;
_context->fog_enabled = FALSE; _context->fog_enabled = FALSE;
_context->enable_backface_culling = FALSE; _context->enable_backface_culling = FALSE;
@ -104,12 +104,14 @@ cogl_create_context (void)
_context->logged_vertices = g_array_new (FALSE, FALSE, sizeof (GLfloat)); _context->logged_vertices = g_array_new (FALSE, FALSE, sizeof (GLfloat));
_context->current_material = NULL; _context->current_material = NULL;
_context->current_material_flags = COGL_MATERIAL_FLAGS_INIT; _context->current_material_changes_since_flush = 0;
_context->current_material_fallback_layers = 0;
_context->current_material_disable_layers = 0;
_context->current_material_layer0_override = 0;
_context->current_material_skip_gl_color = FALSE; _context->current_material_skip_gl_color = FALSE;
_context->material0_nodes =
g_array_sized_new (FALSE, FALSE, sizeof (CoglHandle), 20);
_context->material1_nodes =
g_array_sized_new (FALSE, FALSE, sizeof (CoglHandle), 20);
_cogl_bitmask_init (&_context->texcoord_arrays_enabled); _cogl_bitmask_init (&_context->texcoord_arrays_enabled);
_cogl_bitmask_init (&_context->temp_bitmask); _cogl_bitmask_init (&_context->temp_bitmask);
_cogl_bitmask_init (&_context->texcoord_arrays_to_disable); _cogl_bitmask_init (&_context->texcoord_arrays_to_disable);
@ -123,6 +125,8 @@ cogl_create_context (void)
_context->current_use_program_type = COGL_MATERIAL_PROGRAM_TYPE_FIXED; _context->current_use_program_type = COGL_MATERIAL_PROGRAM_TYPE_FIXED;
_context->current_gl_program = 0; _context->current_gl_program = 0;
_context->gl_blend_enable_cache = FALSE;
_context->framebuffer_stack = _cogl_create_framebuffer_stack (); _context->framebuffer_stack = _cogl_create_framebuffer_stack ();
window_buffer = _cogl_onscreen_new (); window_buffer = _cogl_onscreen_new ();
@ -173,8 +177,6 @@ cogl_create_context (void)
cogl_set_source (_context->simple_material); cogl_set_source (_context->simple_material);
_cogl_material_flush_gl_state (_context->source_material, NULL); _cogl_material_flush_gl_state (_context->source_material, NULL);
enable_flags =
_cogl_material_get_cogl_enable_flags (_context->source_material);
_cogl_enable (enable_flags); _cogl_enable (enable_flags);
_cogl_flush_face_winding (); _cogl_flush_face_winding ();
@ -221,6 +223,13 @@ _cogl_destroy_context (void)
if (_context->default_material) if (_context->default_material)
cogl_handle_unref (_context->default_material); cogl_handle_unref (_context->default_material);
if (_context->dummy_layer_dependant)
cogl_handle_unref (_context->dummy_layer_dependant);
if (_context->default_layer_n)
cogl_handle_unref (_context->default_layer_n);
if (_context->default_layer_0)
cogl_handle_unref (_context->default_layer_0);
if (_context->atlas) if (_context->atlas)
_cogl_atlas_free (_context->atlas); _cogl_atlas_free (_context->atlas);
if (_context->atlas_texture) if (_context->atlas_texture)

View File

@ -49,10 +49,12 @@ typedef struct
gboolean features_cached; gboolean features_cached;
CoglHandle default_material; CoglHandle default_material;
CoglHandle default_layer_0;
CoglHandle default_layer_n;
CoglHandle dummy_layer_dependant;
/* Enable cache */ /* Enable cache */
unsigned long enable_flags; unsigned long enable_flags;
guint8 color_alpha;
gboolean fog_enabled; gboolean fog_enabled;
gboolean enable_backface_culling; gboolean enable_backface_culling;
@ -91,11 +93,12 @@ typedef struct
/* Some simple caching, to minimize state changes... */ /* Some simple caching, to minimize state changes... */
CoglHandle current_material; CoglHandle current_material;
unsigned long current_material_flags; unsigned long current_material_changes_since_flush;
gboolean current_material_fallback_layers;
gboolean current_material_disable_layers;
GLuint current_material_layer0_override;
gboolean current_material_skip_gl_color; gboolean current_material_skip_gl_color;
GArray *material0_nodes;
GArray *material1_nodes;
/* Bitmask of texture coordinates arrays that are enabled */ /* Bitmask of texture coordinates arrays that are enabled */
CoglBitmask texcoord_arrays_enabled; CoglBitmask texcoord_arrays_enabled;
/* These are temporary bitmasks that are used when disabling /* These are temporary bitmasks that are used when disabling
@ -104,6 +107,8 @@ typedef struct
CoglBitmask texcoord_arrays_to_disable; CoglBitmask texcoord_arrays_to_disable;
CoglBitmask temp_bitmask; CoglBitmask temp_bitmask;
gboolean gl_blend_enable_cache;
/* PBOs */ /* PBOs */
/* This can be used to check if a pbo is bound */ /* This can be used to check if a pbo is bound */
CoglBuffer *current_pbo; CoglBuffer *current_pbo;
@ -143,7 +148,9 @@ typedef struct
GLint max_texture_image_units; GLint max_texture_image_units;
GLint max_activateable_texture_units; GLint max_activateable_texture_units;
/* Fragment processing programs */
CoglHandle current_program; CoglHandle current_program;
CoglMaterialProgramType current_use_program_type; CoglMaterialProgramType current_use_program_type;
GLuint current_gl_program; GLuint current_gl_program;

View File

@ -66,7 +66,8 @@ static const GDebugKey cogl_behavioural_debug_keys[] = {
{ "disable-atlas", COGL_DEBUG_DISABLE_ATLAS }, { "disable-atlas", COGL_DEBUG_DISABLE_ATLAS },
{ "disable-texturing", COGL_DEBUG_DISABLE_TEXTURING}, { "disable-texturing", COGL_DEBUG_DISABLE_TEXTURING},
{ "disable-arbfp", COGL_DEBUG_DISABLE_ARBFP}, { "disable-arbfp", COGL_DEBUG_DISABLE_ARBFP},
{ "disable-glsl", COGL_DEBUG_DISABLE_GLSL} { "disable-glsl", COGL_DEBUG_DISABLE_GLSL},
{ "disable-blending", COGL_DEBUG_DISABLE_BLENDING}
}; };
static const int n_cogl_behavioural_debug_keys = static const int n_cogl_behavioural_debug_keys =
G_N_ELEMENTS (cogl_behavioural_debug_keys); G_N_ELEMENTS (cogl_behavioural_debug_keys);
@ -123,6 +124,7 @@ _cogl_parse_debug_string (const char *value,
OPT ("disable-texturing:", "disable texturing primitives"); OPT ("disable-texturing:", "disable texturing primitives");
OPT ("disable-arbfp:", "disable use of ARBfp"); OPT ("disable-arbfp:", "disable use of ARBfp");
OPT ("disable-glsl:", "disable use of GLSL"); OPT ("disable-glsl:", "disable use of GLSL");
OPT ("disable-blending:", "disable use of blending");
OPT ("show-source:", "show generated ARBfp/GLSL"); OPT ("show-source:", "show generated ARBfp/GLSL");
OPT ("opengl:", "traces some select OpenGL calls"); OPT ("opengl:", "traces some select OpenGL calls");
OPT ("offscreen:", "debug offscreen support"); OPT ("offscreen:", "debug offscreen support");

View File

@ -50,7 +50,8 @@ typedef enum {
COGL_DEBUG_DISABLE_TEXTURING = 1 << 19, COGL_DEBUG_DISABLE_TEXTURING = 1 << 19,
COGL_DEBUG_DISABLE_ARBFP = 1 << 20, COGL_DEBUG_DISABLE_ARBFP = 1 << 20,
COGL_DEBUG_DISABLE_GLSL = 1 << 21, COGL_DEBUG_DISABLE_GLSL = 1 << 21,
COGL_DEBUG_SHOW_SOURCE = 1 << 22 COGL_DEBUG_SHOW_SOURCE = 1 << 22,
COGL_DEBUG_DISABLE_BLENDING = 1 << 23
} CoglDebugFlags; } CoglDebugFlags;
#ifdef COGL_ENABLE_DEBUG #ifdef COGL_ENABLE_DEBUG

View File

@ -97,11 +97,10 @@ cogl_gl_error_to_string (GLenum error_code);
#endif /* COGL_GL_DEBUG */ #endif /* COGL_GL_DEBUG */
#define COGL_ENABLE_BLEND (1<<1) #define COGL_ENABLE_ALPHA_TEST (1<<1)
#define COGL_ENABLE_ALPHA_TEST (1<<2) #define COGL_ENABLE_VERTEX_ARRAY (1<<2)
#define COGL_ENABLE_VERTEX_ARRAY (1<<3) #define COGL_ENABLE_COLOR_ARRAY (1<<3)
#define COGL_ENABLE_COLOR_ARRAY (1<<4) #define COGL_ENABLE_BACKFACE_CULLING (1<<4)
#define COGL_ENABLE_BACKFACE_CULLING (1<<5)
void void
_cogl_features_init (void); _cogl_features_init (void);

View File

@ -318,10 +318,6 @@ _cogl_journal_flush_material_and_entries (CoglJournalEntry *batch_start,
_cogl_material_flush_gl_state (batch_start->material, _cogl_material_flush_gl_state (batch_start->material,
&batch_start->flush_options); &batch_start->flush_options);
/* 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 (batch_start->material);
if (ctx->enable_backface_culling) if (ctx->enable_backface_culling)
enable_flags |= COGL_ENABLE_BACKFACE_CULLING; enable_flags |= COGL_ENABLE_BACKFACE_CULLING;
@ -356,7 +352,8 @@ compare_entry_materials (CoglJournalEntry *entry0, CoglJournalEntry *entry1)
if (_cogl_material_equal (entry0->material, if (_cogl_material_equal (entry0->material,
&entry0->flush_options, &entry0->flush_options,
entry1->material, entry1->material,
&entry1->flush_options)) &entry1->flush_options,
TRUE))
return TRUE; return TRUE;
else else
return FALSE; return FALSE;

View File

@ -3,7 +3,7 @@
* *
* An object oriented GL/GLES Abstraction/Utility Layer * An object oriented GL/GLES Abstraction/Utility Layer
* *
* Copyright (C) 2008,2009 Intel Corporation. * Copyright (C) 2008,2009,2010 Intel Corporation.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -16,7 +16,8 @@
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>. * License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
* *
* *
* *
@ -62,8 +63,9 @@ typedef struct _CoglTextureUnit
/* Whether or not the corresponding gl_target has been glEnabled */ /* Whether or not the corresponding gl_target has been glEnabled */
gboolean enabled; gboolean enabled;
/* The GL target currently glEnabled or 0 if .enabled == FALSE */ /* The GL target currently glEnabled or the target last enabled
GLenum enabled_gl_target; * if .enabled == FALSE */
GLenum current_gl_target;
/* The raw GL texture object name for which we called glBindTexture when /* The raw GL texture object name for which we called glBindTexture when
* we flushed the last layer. (NB: The CoglTexture associated * we flushed the last layer. (NB: The CoglTexture associated
@ -80,13 +82,13 @@ typedef struct _CoglTextureUnit
/* We have many components in Cogl that need to temporarily bind arbitrary /* We have many components in Cogl that need to temporarily bind arbitrary
* textures e.g. to query texture object parameters and since we don't * textures e.g. to query texture object parameters and since we don't
* want that to result in too much redundant reflushing of layer state * want that to result in too much redundant reflushing of layer state
* when all that's needed is to re-bind the layers gl_texture we use this * when all that's needed is to re-bind the layer's gl_texture we use this
* to track when the .layer_gl_texture state is invalid. * to track when the unit->gl_texture state is out of sync with the GL
* texture object really bound too (GL_TEXTURE0+unit->index).
* *
* XXX: as a further optimization cogl-material.c uses a convention * XXX: as a further optimization cogl-material.c uses a convention
* of always leaving texture unit 1 active when not dealing with the * of always using texture unit 1 for these transient bindings so we
* flushing of layer state, so we can assume this is only ever TRUE * can assume this is only ever TRUE for unit 1.
* for unit 1.
*/ */
gboolean dirty_gl_texture; gboolean dirty_gl_texture;
@ -116,24 +118,16 @@ typedef struct _CoglTextureUnit
* invalidated though these flags can be used to optimize the state * invalidated though these flags can be used to optimize the state
* flush of the next layer * flush of the next layer
*/ */
unsigned long layer_differences; unsigned long layer_changes_since_flush;
/* The options that may have affected how the layer state updated /* Whenever a CoglTexture's internal GL texture storage changes
* this texture unit. */ * cogl-material.c is notified with a call to
gboolean fallback; * _cogl_material_texture_storage_change_notify which inturn sets
gboolean layer0_overridden; * this to TRUE for each texture unit that it is currently bound
* too. When we later come to flush some material state then we will
/* When flushing a layers state, fallback options may mean that a * always check this to potentially force an update of the texture
* different CoglTexture is used than layer->texture. * state even if the material hasn't changed. */
* gboolean texture_storage_changed;
* Once a layers state has been flushed we have to keep track of
* changes to that layer so if we are asked to re-flush the same
* layer later we will know what work is required. This also means
* we need to keep track of changes to the CoglTexture of that layer
* so we need to explicitly keep a reference to the final texture
* chosen.
*/
CoglHandle texture;
} CoglTextureUnit; } CoglTextureUnit;
@ -148,63 +142,50 @@ _cogl_bind_gl_texture_transient (GLenum gl_target,
GLuint gl_texture, GLuint gl_texture,
gboolean is_foreign); gboolean is_foreign);
typedef enum _CoglMaterialEqualFlags #if defined (HAVE_COGL_GL)
/* glsl, arbfp, fixed */
#define COGL_MATERIAL_N_BACKENDS 3
#elif defined (HAVE_COGL_GLES2)
/* glsl, fixed */
#define COGL_MATERIAL_N_BACKENDS 2
#else /* HAVE_COGL_GLES */
/* fixed */
#define COGL_MATERIAL_N_BACKENDS 1
#endif
typedef enum
{ {
/* Return FALSE if any component of either material isn't set to its COGL_MATERIAL_LAYER_STATE_UNIT = 1L<<0,
* default value. (Note: if the materials have corresponding flush COGL_MATERIAL_LAYER_STATE_TEXTURE = 1L<<1,
* options indicating that e.g. the material color won't be flushed then COGL_MATERIAL_LAYER_STATE_FILTERS = 1L<<2,
* this will not assert a default color value.) */ COGL_MATERIAL_LAYER_STATE_WRAP_MODES = 1L<<3,
COGL_MATERIAL_EQUAL_FLAGS_ASSERT_ALL_DEFAULTS = 1L<<0,
} CoglMaterialEqualFlags; COGL_MATERIAL_LAYER_STATE_COMBINE = 1L<<4,
COGL_MATERIAL_LAYER_STATE_COMBINE_CONSTANT = 1L<<5,
COGL_MATERIAL_LAYER_STATE_USER_MATRIX = 1L<<6,
typedef enum _CoglMaterialLayerDifferenceFlags /* COGL_MATERIAL_LAYER_STATE_TEXTURE_INTERN = 1L<<7, */
COGL_MATERIAL_LAYER_STATE_ALL_SPARSE =
COGL_MATERIAL_LAYER_STATE_UNIT |
COGL_MATERIAL_LAYER_STATE_TEXTURE |
COGL_MATERIAL_LAYER_STATE_FILTERS |
COGL_MATERIAL_LAYER_STATE_WRAP_MODES |
COGL_MATERIAL_LAYER_STATE_COMBINE |
COGL_MATERIAL_LAYER_STATE_COMBINE_CONSTANT |
COGL_MATERIAL_LAYER_STATE_USER_MATRIX,
COGL_MATERIAL_LAYER_STATE_NEEDS_BIG_STATE =
COGL_MATERIAL_LAYER_STATE_COMBINE |
COGL_MATERIAL_LAYER_STATE_COMBINE_CONSTANT |
COGL_MATERIAL_LAYER_STATE_USER_MATRIX
} CoglMaterialLayerState;
typedef struct
{ {
COGL_MATERIAL_LAYER_DIFFERENCE_TEXTURE = 1L<<0, /* The texture combine state determines how the color of individual
COGL_MATERIAL_LAYER_DIFFERENCE_COMBINE = 1L<<1, * texture fragments are calculated. */
COGL_MATERIAL_LAYER_DIFFERENCE_COMBINE_CONSTANT = 1L<<2,
COGL_MATERIAL_LAYER_DIFFERENCE_USER_MATRIX = 1L<<3,
COGL_MATERIAL_LAYER_DIFFERENCE_FILTERS = 1L<<4
} CoglMaterialLayerDifferenceFlags;
typedef enum _CoglMaterialLayerChangeFlags
{
COGL_MATERIAL_LAYER_CHANGE_TEXTURE = 1L<<0,
COGL_MATERIAL_LAYER_CHANGE_COMBINE = 1L<<1,
COGL_MATERIAL_LAYER_CHANGE_COMBINE_CONSTANT = 1L<<2,
COGL_MATERIAL_LAYER_CHANGE_USER_MATRIX = 1L<<3,
COGL_MATERIAL_LAYER_CHANGE_FILTERS = 1L<<4,
COGL_MATERIAL_LAYER_CHANGE_TEXTURE_INTERN = 1L<<5,
COGL_MATERIAL_LAYER_CHANGE_UNIT = 1L<<6
} CoglMaterialLayerChangeFlags;
struct _CoglMaterialLayer
{
CoglHandleObject _parent;
/* Parent material */
CoglMaterial *material;
unsigned int index; /*!< lowest index is blended first then others on
top */
int unit_index;
unsigned long differences;
CoglHandle texture; /*!< The texture for this layer, or
COGL_INVALID_HANDLE for an empty layer */
CoglMaterialFilter mag_filter;
CoglMaterialFilter min_filter;
CoglMaterialWrapMode wrap_mode_s;
CoglMaterialWrapMode wrap_mode_t;
CoglMaterialWrapMode wrap_mode_r;
/* Determines how the color of individual texture fragments
* are calculated. */
GLint texture_combine_rgb_func; GLint texture_combine_rgb_func;
GLint texture_combine_rgb_src[3]; GLint texture_combine_rgb_src[3];
GLint texture_combine_rgb_op[3]; GLint texture_combine_rgb_op[3];
@ -213,104 +194,363 @@ struct _CoglMaterialLayer
GLint texture_combine_alpha_src[3]; GLint texture_combine_alpha_src[3];
GLint texture_combine_alpha_op[3]; GLint texture_combine_alpha_op[3];
GLfloat texture_combine_constant[4]; float texture_combine_constant[4];
/* TODO: Support purely GLSL based material layers */
/* The texture matrix dscribes how to transform texture coordinates */
CoglMatrix matrix; CoglMatrix matrix;
} CoglMaterialLayerBigState;
struct _CoglMaterialLayer
{
/* XXX: Please think twice about adding members that *have* be
* initialized during a _cogl_material_layer_copy. We are aiming
* to have copies be as cheap as possible and copies may be
* done by the primitives APIs which means they may happen
* in performance critical code paths.
*
* XXX: If you are extending the state we track please consider if
* the state is expected to vary frequently across many materials or
* if the state can be shared among many derived materials instead.
* This will determine if the state should be added directly to this
* structure which will increase the memory overhead for *all*
* layers or if instead it can go under ->big_state.
*/
/* the parent in terms of class hierarchy */
CoglHandleObject _parent;
/* Some layers have a material owner, which is to say that the layer
* is referenced in that materials->layer_differences list. A layer
* doesn't always have an owner and may simply be an ancestor for
* other layers that keeps track of some shared state. */
CoglMaterial *owner;
/* Layers are sparse structures defined as a diff against
* their parent... */
CoglMaterialLayer *parent;
/* As an optimization for creating leaf node layers (the most
* common) we don't require any list node allocations to link
* to a single descendant. */
CoglMaterialLayer *first_child;
/* Layers are sparse structures defined as a diff against
* their parent and may have multiple children which depend
* on them to define the values of properties which they don't
* change. */
GList *children;
/* The lowest index is blended first then others on top */
int index;
/* Different material backends (GLSL/ARBfp/Fixed Function) may /* Different material backends (GLSL/ARBfp/Fixed Function) may
* want to associate private data with a layer... */ * want to associate private data with a layer...
void *backend_priv; *
* NB: we have per backend pointers because a layer may be
* associated with multiple materials with different backends.
*/
void *backend_priv[COGL_MATERIAL_N_BACKENDS];
/* A mask of which state groups are different in this layer
* in comparison to its parent. */
unsigned long differences;
/* Common differences
*
* As a basic way to reduce memory usage we divide the layer
* state into two groups; the minimal state modified in 90% of
* all layers and the rest, so that the second group can
* be allocated dynamically when required.
*/
/* Each layer is directly associated with a single texture unit */
int unit_index;
/* The texture for this layer, or COGL_INVALID_HANDLE for an empty
* layer */
CoglHandle texture;
gboolean texture_overridden;
/* If ->texture_overridden == TRUE then the texture is instead
* defined by these... */
GLuint slice_gl_texture;
GLenum slice_gl_target;
CoglMaterialFilter mag_filter;
CoglMaterialFilter min_filter;
CoglMaterialWrapMode wrap_mode_s;
CoglMaterialWrapMode wrap_mode_t;
CoglMaterialWrapMode wrap_mode_r;
/* Infrequent differences aren't currently tracked in
* a separate, dynamically allocated structure as they are
* for materials... */
CoglMaterialLayerBigState *big_state;
/* bitfields */
/* Determines if layer->first_child and layer->children are
* initialized pointers. */
unsigned int has_children:1;
/* Determines if layer->big_state is valid */
unsigned int has_big_state:1;
}; };
typedef enum _CoglMaterialFlags /* Used in material->differences masks and for notifying material
* state changes... */
typedef enum _CoglMaterialState
{ {
COGL_MATERIAL_FLAG_DEFAULT_COLOR = 1L<<1, COGL_MATERIAL_STATE_COLOR = 1L<<0,
COGL_MATERIAL_FLAG_DEFAULT_GL_MATERIAL = 1L<<2, COGL_MATERIAL_STATE_BLEND_ENABLE = 1L<<1,
COGL_MATERIAL_FLAG_DEFAULT_ALPHA_FUNC = 1L<<3, COGL_MATERIAL_STATE_LAYERS = 1L<<2,
COGL_MATERIAL_FLAG_ENABLE_BLEND = 1L<<4,
COGL_MATERIAL_FLAG_DEFAULT_BLEND = 1L<<5,
COGL_MATERIAL_FLAG_DEFAULT_USER_SHADER = 1L<<6,
COGL_MATERIAL_FLAG_DEFAULT_LAYERS = 1L<<7
} CoglMaterialFlags;
/* This defines the initialization state for COGL_MATERIAL_STATE_LIGHTING = 1L<<3,
* ctx->current_material_flags which should result in the first COGL_MATERIAL_STATE_ALPHA_FUNC = 1L<<4,
* material flush explicitly initializing everything COGL_MATERIAL_STATE_BLEND = 1L<<5,
*/ COGL_MATERIAL_STATE_USER_SHADER = 1L<<6,
#define COGL_MATERIAL_FLAGS_INIT \
COGL_MATERIAL_FLAG_DEFAULT_USER_SHADER
typedef enum _CoglMaterialChangeFlag COGL_MATERIAL_STATE_REAL_BLEND_ENABLE = 1L<<7,
COGL_MATERIAL_STATE_ALL_SPARSE =
COGL_MATERIAL_STATE_COLOR |
COGL_MATERIAL_STATE_BLEND_ENABLE |
COGL_MATERIAL_STATE_LAYERS |
COGL_MATERIAL_STATE_LIGHTING |
COGL_MATERIAL_STATE_ALPHA_FUNC |
COGL_MATERIAL_STATE_BLEND |
COGL_MATERIAL_STATE_USER_SHADER,
COGL_MATERIAL_STATE_AFFECTS_BLENDING =
COGL_MATERIAL_STATE_COLOR |
COGL_MATERIAL_STATE_BLEND_ENABLE |
COGL_MATERIAL_STATE_LAYERS |
COGL_MATERIAL_STATE_LIGHTING |
COGL_MATERIAL_STATE_BLEND |
COGL_MATERIAL_STATE_USER_SHADER,
COGL_MATERIAL_STATE_NEEDS_BIG_STATE =
COGL_MATERIAL_STATE_LIGHTING |
COGL_MATERIAL_STATE_ALPHA_FUNC |
COGL_MATERIAL_STATE_BLEND |
COGL_MATERIAL_STATE_USER_SHADER
} CoglMaterialState;
typedef enum
{ {
COGL_MATERIAL_CHANGE_COLOR = 1L<<1, COGL_MATERIAL_LIGHTING_STATE_PROPERTY_AMBIENT = 1,
COGL_MATERIAL_CHANGE_GL_MATERIAL = 1L<<2, COGL_MATERIAL_LIGHTING_STATE_PROPERTY_DIFFUSE,
COGL_MATERIAL_CHANGE_ALPHA_FUNC = 1L<<3, COGL_MATERIAL_LIGHTING_STATE_PROPERTY_SPECULAR,
COGL_MATERIAL_CHANGE_ENABLE_BLEND = 1L<<4, COGL_MATERIAL_LIGHTING_STATE_PROPERTY_EMISSION,
COGL_MATERIAL_CHANGE_BLEND = 1L<<5, COGL_MATERIAL_LIGHTING_STATE_PROPERTY_SHININESS
COGL_MATERIAL_CHANGE_USER_SHADER = 1L<<6, } CoglMaterialLightingStateProperty;
COGL_MATERIAL_CHANGE_LAYERS = 1L<<7
} CoglMaterialChangeFlag;
struct _CoglMaterial typedef struct
{ {
CoglHandleObject _parent;
unsigned long journal_ref_count;
int backend;
unsigned long flags;
/* If no lighting is enabled; this is the basic material color */
GLubyte unlit[4];
/* Standard OpenGL lighting model attributes */ /* Standard OpenGL lighting model attributes */
GLfloat ambient[4]; float ambient[4];
GLfloat diffuse[4]; float diffuse[4];
GLfloat specular[4]; float specular[4];
GLfloat emission[4]; float emission[4];
GLfloat shininess; float shininess;
} CoglMaterialLightingState;
typedef struct
{
/* Determines what fragments are discarded based on their alpha */ /* Determines what fragments are discarded based on their alpha */
CoglMaterialAlphaFunc alpha_func; CoglMaterialAlphaFunc alpha_func;
GLfloat alpha_func_reference; GLfloat alpha_func_reference;
} CoglMaterialAlphaFuncState;
typedef enum _CoglMaterialBlendEnable
{
/* XXX: we want to detect users mistakenly using TRUE or FALSE
* so start the enum at 2. */
COGL_MATERIAL_BLEND_ENABLE_ENABLED = 2,
COGL_MATERIAL_BLEND_ENABLE_DISABLED,
COGL_MATERIAL_BLEND_ENABLE_AUTOMATIC
} CoglMaterialBlendEnable;
typedef struct
{
/* Determines how this material is blended with other primitives */ /* Determines how this material is blended with other primitives */
#ifndef HAVE_COGL_GLES #ifndef HAVE_COGL_GLES
GLenum blend_equation_rgb; GLenum blend_equation_rgb;
GLenum blend_equation_alpha; GLenum blend_equation_alpha;
GLint blend_src_factor_alpha; GLint blend_src_factor_alpha;
GLint blend_dst_factor_alpha; GLint blend_dst_factor_alpha;
GLfloat blend_constant[4]; CoglColor blend_constant;
#endif #endif
GLint blend_src_factor_rgb; GLint blend_src_factor_rgb;
GLint blend_dst_factor_rgb; GLint blend_dst_factor_rgb;
} CoglMaterialBlendState;
typedef struct
{
CoglMaterialLightingState lighting_state;
CoglMaterialAlphaFuncState alpha_state;
CoglMaterialBlendState blend_state;
CoglHandle user_program; CoglHandle user_program;
} CoglMaterialBigState;
GList *layers; typedef enum
{
COGL_MATERIAL_FLAG_DIRTY_LAYERS_CACHE = 1L<<0,
COGL_MATERIAL_FLAG_DIRTY_GET_LAYERS_LIST = 1L<<1
} CoglMaterialFlag;
typedef struct
{
CoglMaterial *owner;
CoglMaterialLayer *layer;
} CoglMaterialLayerCacheEntry;
struct _CoglMaterial
{
/* XXX: Please think twice about adding members that *have* be
* initialized during a cogl_material_copy. We are aiming to have
* copies be as cheap as possible and copies may be done by the
* primitives APIs which means they may happen in performance
* critical code paths.
*
* XXX: If you are extending the state we track please consider if
* the state is expected to vary frequently across many materials or
* if the state can be shared among many derived materials instead.
* This will determine if the state should be added directly to this
* structure which will increase the memory overhead for *all*
* materials or if instead it can go under ->big_state.
*/
/* the parent in terms of class hierarchy */
CoglHandleObject _parent;
/* We need to track if a material is referenced in the journal
* because we can't allow modification to these materials without
* flushing the journal first */
unsigned long journal_ref_count;
/* Materials are sparse structures defined as a diff against
* their parent. */
CoglMaterial *parent;
/* As an optimization for creating leaf node materials (the most
* common) we don't require any list node allocations to link
* to a single descendant.
*
* Only valid if ->has_children bitfield is set */
CoglMaterial *first_child;
/* Materials are sparse structures defined as a diff against
* their parent and may have multiple children which depend
* on them to define the values of properties which they don't
* change.
*
* Only valid if ->has_children bitfield is set */
GList *children;
/* A mask of which sparse state groups are different in this
* material in comparison to its parent. */
unsigned long differences;
/* The fragment processing backend identified by the ->backend
* bitfield can associate private data with a material. */
void *backend_priv;
/* This is the primary color of the material.
*
* This is a sparse property, ref COGL_MATERIAL_STATE_COLOR */
CoglColor color;
/* A material may be made up with multiple layers used to combine
* textures together.
*
* This is sparse state, ref COGL_MATERIAL_STATE_LAYERS */
GList *layer_differences;
unsigned int n_layers; unsigned int n_layers;
void *backend_priv; /* As a basic way to reduce memory usage we divide the material
* state into two groups; the minimal state modified in 90% of
* all materials and the rest, so that the second group can
* be allocated dynamically when required... */
CoglMaterialBigState *big_state;
/* Cached state... */
/* A cached, complete list of the layers this material depends
* on sorted by layer->unit_index. */
CoglMaterialLayer **layers_cache;
/* To avoid a separate ->layers_cache allocation for common
* materials with only a few layers... */
CoglMaterialLayer *short_layers_cache[3];
/* The deprecated cogl_material_get_layers() API returns a
* const GList of layers, which we track here... */
GList *deprecated_get_layers_list;
/* XXX: consider adding an authorities cache to speed up sparse
* property value lookups:
* CoglMaterial *authorities_cache[COGL_MATERIAL_N_SPARSE_PROPERTIES];
* and corresponding authorities_cache_dirty:1 bitfield
*/
/* bitfields */
/* Determines if material->big_state is valid */
unsigned int has_big_state:1;
/* By default blending is enabled automatically depending on the
* unlit color, the lighting colors or the texture format. The user
* can override this to explicitly enable or disable blending.
*
* This is a sparse property */
unsigned int blend_enable:3;
/* There are many factors that can determine if we need to enable
* blending, this holds our final decision */
unsigned int real_blend_enable:1;
/* Determines if material->first_child and material->children are
* initialized pointers. */
unsigned int has_children:1;
unsigned int layers_cache_dirty:1;
unsigned int deprecated_get_layers_list_dirty:1;
/* There are multiple fragment processing backends for CoglMaterial,
* glsl, arbfp and fixed. This identifies the backend being used for
* the material and any private state the backend has associated
* with the material. */
unsigned int backend:3;
/* Determines if ->backend_priv has been initialized */
unsigned int backend_priv_set:1;
}; };
typedef struct _CoglMaterialBackend typedef struct _CoglMaterialBackend
{ {
int (*get_max_texture_units) (void); int (*get_max_texture_units) (void);
gboolean (*start) (CoglMaterial *material); gboolean (*start) (CoglMaterial *material,
gboolean (*add_layer) (CoglMaterialLayer *layer); int n_layers,
unsigned long materials_difference);
gboolean (*add_layer) (CoglMaterial *material,
CoglMaterialLayer *layer,
unsigned long layers_difference);
gboolean (*passthrough) (CoglMaterial *material); gboolean (*passthrough) (CoglMaterial *material);
gboolean (*end) (CoglMaterial *material); gboolean (*end) (CoglMaterial *material,
unsigned long materials_difference);
void (*material_change_notify) (CoglMaterial *material, void (*material_pre_change_notify) (CoglMaterial *material,
unsigned long changes, CoglMaterialState change,
GLubyte *new_color); const CoglColor *new_color);
void (*layer_change_notify) (CoglMaterialLayer *layer, void (*layer_pre_change_notify) (CoglMaterialLayer *layer,
unsigned long changes); CoglMaterialLayerState change);
void (*free_priv) (CoglMaterial *material); void (*free_priv) (CoglMaterial *material);
void (*free_layer_priv) (CoglMaterialLayer *layer);
} CoglMaterialBackend; } CoglMaterialBackend;
typedef enum typedef enum
@ -320,6 +560,12 @@ typedef enum
COGL_MATERIAL_PROGRAM_TYPE_FIXED COGL_MATERIAL_PROGRAM_TYPE_FIXED
} CoglMaterialProgramType; } CoglMaterialProgramType;
void
_cogl_material_init_default_material (void);
void
_cogl_material_init_default_layers (void);
/* /*
* SECTION:cogl-material-internals * SECTION:cogl-material-internals
* @short_description: Functions for creating custom primitives that make use * @short_description: Functions for creating custom primitives that make use
@ -331,31 +577,8 @@ typedef enum
* able to fill your geometry according to a given Cogl material. * able to fill your geometry according to a given Cogl material.
*/ */
/* gboolean
* _cogl_material_init_default_material: _cogl_material_get_real_blend_enabled (CoglHandle handle);
*
* This initializes the first material owned by the Cogl context. All
* subsequently instantiated materials created via the cogl_material_new()
* API will initially be a copy of this material.
*/
void
_cogl_material_init_default_material (void);
/*
* 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! */
unsigned long
_cogl_material_get_cogl_enable_flags (CoglHandle handle);
gboolean gboolean
_cogl_material_layer_has_user_matrix (CoglHandle layer_handle); _cogl_material_layer_has_user_matrix (CoglHandle layer_handle);
@ -397,11 +620,35 @@ typedef enum _CoglMaterialFlushFlag
COGL_MATERIAL_FLUSH_WRAP_MODE_OVERRIDES = 1L<<4 COGL_MATERIAL_FLUSH_WRAP_MODE_OVERRIDES = 1L<<4
} CoglMaterialFlushFlag; } CoglMaterialFlushFlag;
/* These constants are used to fill in wrap_mode_overrides */ /* This isn't defined in the GLES headers */
#define COGL_MATERIAL_WRAP_MODE_OVERRIDE_NONE 0 /* no override */ #ifndef GL_CLAMP_TO_BORDER
#define COGL_MATERIAL_WRAP_MODE_OVERRIDE_REPEAT 1 #define GL_CLAMP_TO_BORDER 0x812d
#define COGL_MATERIAL_WRAP_MODE_OVERRIDE_CLAMP_TO_EDGE 2 #endif
#define COGL_MATERIAL_WRAP_MODE_OVERRIDE_CLAMP_TO_BORDER 3
/* GL_ALWAYS is just used here as a value that is known not to clash
* with any valid GL wrap modes.
*
* XXX: keep the values in sync with the CoglMaterialWrapMode enum
* so no conversion is actually needed.
*/
typedef enum _CoglMaterialWrapModeInternal
{
COGL_MATERIAL_WRAP_MODE_INTERNAL_REPEAT = GL_REPEAT,
COGL_MATERIAL_WRAP_MODE_INTERNAL_CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
COGL_MATERIAL_WRAP_MODE_INTERNAL_CLAMP_TO_BORDER = GL_CLAMP_TO_BORDER,
COGL_MATERIAL_WRAP_MODE_INTERNAL_AUTOMATIC = GL_ALWAYS
} CoglMaterialWrapModeInternal;
typedef enum _CoglMaterialWrapModeOverride
{
COGL_MATERIAL_WRAP_MODE_OVERRIDE_NONE = 0,
COGL_MATERIAL_WRAP_MODE_OVERRIDE_REPEAT =
COGL_MATERIAL_WRAP_MODE_INTERNAL_REPEAT,
COGL_MATERIAL_WRAP_MODE_OVERRIDE_CLAMP_TO_EDGE =
COGL_MATERIAL_WRAP_MODE_INTERNAL_CLAMP_TO_EDGE,
COGL_MATERIAL_WRAP_MODE_OVERRIDE_CLAMP_TO_BORDER =
COGL_MATERIAL_WRAP_MODE_INTERNAL_CLAMP_TO_BORDER,
} CoglMaterialWrapModeOverride;
/* There can't be more than 32 layers because we need to fit a bitmask /* There can't be more than 32 layers because we need to fit a bitmask
of the layers into a guint32 */ of the layers into a guint32 */
@ -411,9 +658,9 @@ typedef struct _CoglMaterialWrapModeOverrides
{ {
struct struct
{ {
unsigned long s : 2; CoglMaterialWrapModeOverride s;
unsigned long t : 2; CoglMaterialWrapModeOverride t;
unsigned long r : 2; CoglMaterialWrapModeOverride r;
} values[COGL_MATERIAL_MAX_LAYERS]; } values[COGL_MATERIAL_MAX_LAYERS];
} CoglMaterialWrapModeOverrides; } CoglMaterialWrapModeOverrides;
@ -443,7 +690,8 @@ gboolean
_cogl_material_equal (CoglHandle material0_handle, _cogl_material_equal (CoglHandle material0_handle,
CoglMaterialFlushOptions *material0_flush_options, CoglMaterialFlushOptions *material0_flush_options,
CoglHandle material1_handle, CoglHandle material1_handle,
CoglMaterialFlushOptions *material1_flush_options); CoglMaterialFlushOptions *material1_flush_options,
gboolean skip_gl_color);
CoglHandle CoglHandle
_cogl_material_journal_ref (CoglHandle material_handle); _cogl_material_journal_ref (CoglHandle material_handle);
@ -468,11 +716,21 @@ _cogl_material_set_user_program (CoglHandle handle,
void void
_cogl_delete_gl_texture (GLuint gl_texture); _cogl_delete_gl_texture (GLuint gl_texture);
void
_cogl_material_texture_storage_change_notify (CoglHandle texture);
void void
_cogl_material_apply_legacy_state (CoglHandle handle); _cogl_material_apply_legacy_state (CoglHandle handle);
void void
_cogl_gl_use_program_wrapper (GLuint program); _cogl_gl_use_program_wrapper (GLuint program);
CoglMaterialBlendEnable
_cogl_material_get_blend_enabled (CoglHandle handle);
void
_cogl_material_set_blend_enabled (CoglHandle handle,
CoglMaterialBlendEnable enable);
#endif /* __COGL_MATERIAL_PRIVATE_H */ #endif /* __COGL_MATERIAL_PRIVATE_H */

File diff suppressed because it is too large Load Diff

View File

@ -106,7 +106,11 @@ typedef enum {
* Since: 1.4 * Since: 1.4
*/ */
/* GL_ALWAYS is just used here as a value that is known not to clash /* GL_ALWAYS is just used here as a value that is known not to clash
with any valid GL wrap modes */ * with any valid GL wrap modes
*
* XXX: keep the values in sync with the CoglMaterialWrapModeInternal
* enum so no conversion is actually needed.
*/
typedef enum { typedef enum {
COGL_MATERIAL_WRAP_MODE_REPEAT = GL_REPEAT, COGL_MATERIAL_WRAP_MODE_REPEAT = GL_REPEAT,
COGL_MATERIAL_WRAP_MODE_CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE, COGL_MATERIAL_WRAP_MODE_CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,

View File

@ -145,7 +145,6 @@ _cogl_path_stroke_nodes (void)
* always be done first when preparing to draw. */ * always be done first when preparing to draw. */
_cogl_framebuffer_flush_state (_cogl_get_framebuffer (), 0); _cogl_framebuffer_flush_state (_cogl_get_framebuffer (), 0);
enable_flags |= _cogl_material_get_cogl_enable_flags (ctx->source_material);
_cogl_enable (enable_flags); _cogl_enable (enable_flags);
options.flags = COGL_MATERIAL_FLUSH_DISABLE_MASK; options.flags = COGL_MATERIAL_FLUSH_DISABLE_MASK;
@ -234,8 +233,6 @@ _cogl_add_path_to_stencil_buffer (CoglPath *path,
_cogl_material_flush_gl_state (ctx->source_material, NULL); _cogl_material_flush_gl_state (ctx->source_material, NULL);
enable_flags |=
_cogl_material_get_cogl_enable_flags (ctx->source_material);
_cogl_enable (enable_flags); _cogl_enable (enable_flags);
GE( glEnable (GL_STENCIL_TEST) ); GE( glEnable (GL_STENCIL_TEST) );
@ -394,8 +391,8 @@ _cogl_path_fill_nodes_scanlines (CoglPathNode *path,
_cogl_material_flush_gl_state (ctx->source_material, NULL); _cogl_material_flush_gl_state (ctx->source_material, NULL);
_cogl_enable (COGL_ENABLE_VERTEX_ARRAY _cogl_enable (COGL_ENABLE_VERTEX_ARRAY);
| (ctx->color_alpha < 255 ? COGL_ENABLE_BLEND : 0));
/* clear scanline intersection lists */ /* clear scanline intersection lists */
for (i = 0; i < bounds_h; i++) for (i = 0; i < bounds_h; i++)

View File

@ -1073,6 +1073,8 @@ cogl_polygon (const CoglTextureVertex *vertices,
GLfloat *v; GLfloat *v;
CoglMaterialWrapModeOverrides wrap_mode_overrides; CoglMaterialWrapModeOverrides wrap_mode_overrides;
CoglMaterialWrapModeOverrides *wrap_mode_overrides_p = NULL; CoglMaterialWrapModeOverrides *wrap_mode_overrides_p = NULL;
CoglHandle original_source_material;
gboolean overrode_material = FALSE;
_COGL_GET_CONTEXT (ctx, NO_RETVAL); _COGL_GET_CONTEXT (ctx, NO_RETVAL);
@ -1206,18 +1208,31 @@ cogl_polygon (const CoglTextureVertex *vertices,
/* Prepare GL state */ /* Prepare GL state */
enable_flags = COGL_ENABLE_VERTEX_ARRAY; enable_flags = COGL_ENABLE_VERTEX_ARRAY;
enable_flags |= _cogl_material_get_cogl_enable_flags (ctx->source_material);
if (ctx->enable_backface_culling) if (ctx->enable_backface_culling)
enable_flags |= COGL_ENABLE_BACKFACE_CULLING; enable_flags |= COGL_ENABLE_BACKFACE_CULLING;
if (use_color) if (use_color)
{ {
enable_flags |= COGL_ENABLE_COLOR_ARRAY | COGL_ENABLE_BLEND; CoglHandle override;
enable_flags |= COGL_ENABLE_COLOR_ARRAY;
GE( glColorPointer (4, GL_UNSIGNED_BYTE, GE( glColorPointer (4, GL_UNSIGNED_BYTE,
stride_bytes, stride_bytes,
/* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */ /* NB: [X,Y,Z,TX,TY...,R,G,B,A,...] */
v + 3 + 2 * n_layers) ); v + 3 + 2 * n_layers) );
if (!_cogl_material_get_real_blend_enabled (ctx->source_material))
{
CoglMaterialBlendEnable blend_enabled =
COGL_MATERIAL_BLEND_ENABLE_ENABLED;
original_source_material = ctx->source_material;
override = cogl_material_copy (original_source_material);
_cogl_material_set_blend_enabled (override, blend_enabled);
/* XXX: cogl_push_source () */
overrode_material = TRUE;
ctx->source_material = override;
}
} }
_cogl_enable (enable_flags); _cogl_enable (enable_flags);
@ -1253,6 +1268,16 @@ cogl_polygon (const CoglTextureVertex *vertices,
fallback_layers, fallback_layers,
wrap_mode_overrides_p); wrap_mode_overrides_p);
/* XXX: cogl_pop_source () */
if (overrode_material)
{
cogl_handle_unref (ctx->source_material);
ctx->source_material = original_source_material;
/* XXX: when we have weak materials then any override material
* should get associated with the original material so we don't
* create lots of one-shot materials! */
}
/* Reset the size of the logged vertex array because rendering /* Reset the size of the logged vertex array because rendering
rectangles expects it to start at 0 */ rectangles expects it to start at 0 */
g_array_set_size (ctx->logged_vertices, 0); g_array_set_size (ctx->logged_vertices, 0);

View File

@ -1525,6 +1525,8 @@ enable_state_for_drawing_buffer (CoglVertexBuffer *buffer)
_COGL_GET_CONTEXT (ctx, COGL_INVALID_HANDLE); _COGL_GET_CONTEXT (ctx, COGL_INVALID_HANDLE);
source = ctx->source_material;
if (buffer->new_attributes) if (buffer->new_attributes)
cogl_vertex_buffer_submit_real (buffer); cogl_vertex_buffer_submit_real (buffer);
@ -1572,13 +1574,21 @@ enable_state_for_drawing_buffer (CoglVertexBuffer *buffer)
switch (type) switch (type)
{ {
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_COLOR_ARRAY: case COGL_VERTEX_BUFFER_ATTRIB_FLAG_COLOR_ARRAY:
enable_flags |= COGL_ENABLE_COLOR_ARRAY | COGL_ENABLE_BLEND; enable_flags |= COGL_ENABLE_COLOR_ARRAY;
/* GE (glEnableClientState (GL_COLOR_ARRAY)); */ /* GE (glEnableClientState (GL_COLOR_ARRAY)); */
pointer = (const GLvoid *)(base + attribute->u.vbo_offset); pointer = (const GLvoid *)(base + attribute->u.vbo_offset);
GE (glColorPointer (attribute->n_components, GE (glColorPointer (attribute->n_components,
gl_type, gl_type,
attribute->stride, attribute->stride,
pointer)); pointer));
if (!_cogl_material_get_real_blend_enabled (ctx->source_material))
{
CoglMaterialBlendEnable blend_enable =
COGL_MATERIAL_BLEND_ENABLE_ENABLED;
source = cogl_material_copy (ctx->source_material);
_cogl_material_set_blend_enabled (source, blend_enable);
}
break; break;
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_NORMAL_ARRAY: case COGL_VERTEX_BUFFER_ATTRIB_FLAG_NORMAL_ARRAY:
/* FIXME: go through cogl cache to enable normal array */ /* FIXME: go through cogl cache to enable normal array */
@ -1634,7 +1644,7 @@ enable_state_for_drawing_buffer (CoglVertexBuffer *buffer)
} }
} }
layers = cogl_material_get_layers (ctx->source_material); layers = cogl_material_get_layers (source);
for (tmp = (GList *)layers, i = 0; for (tmp = (GList *)layers, i = 0;
tmp != NULL; tmp != NULL;
tmp = tmp->next, i++) tmp = tmp->next, i++)
@ -1719,14 +1729,13 @@ enable_state_for_drawing_buffer (CoglVertexBuffer *buffer)
if (G_UNLIKELY (ctx->legacy_state_set)) if (G_UNLIKELY (ctx->legacy_state_set))
{ {
/* If we haven't already created a derived material... */
if (source == ctx->source_material)
source = cogl_material_copy (ctx->source_material); source = cogl_material_copy (ctx->source_material);
_cogl_material_apply_legacy_state (source); _cogl_material_apply_legacy_state (source);
} }
else
source = ctx->source_material;
_cogl_material_flush_gl_state (source, &options); _cogl_material_flush_gl_state (source, &options);
enable_flags |= _cogl_material_get_cogl_enable_flags (source);
if (ctx->enable_backface_culling) if (ctx->enable_backface_culling)
enable_flags |= COGL_ENABLE_BACKFACE_CULLING; enable_flags |= COGL_ENABLE_BACKFACE_CULLING;

View File

@ -266,10 +266,6 @@ _cogl_enable (unsigned long flags)
*/ */
_COGL_GET_CONTEXT (ctx, NO_RETVAL); _COGL_GET_CONTEXT (ctx, NO_RETVAL);
toggle_flag (ctx, flags,
COGL_ENABLE_BLEND,
GL_BLEND);
toggle_flag (ctx, flags, toggle_flag (ctx, flags,
COGL_ENABLE_BACKFACE_CULLING, COGL_ENABLE_BACKFACE_CULLING,
GL_CULL_FACE); GL_CULL_FACE);
@ -858,10 +854,6 @@ cogl_begin_gl (void)
options.flags = 0; options.flags = 0;
_cogl_material_flush_gl_state (ctx->source_material, &options); _cogl_material_flush_gl_state (ctx->source_material, &options);
/* 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) if (ctx->enable_backface_culling)
enable_flags |= COGL_ENABLE_BACKFACE_CULLING; enable_flags |= COGL_ENABLE_BACKFACE_CULLING;