[vbo indices] tweak add_indices api to return an id and add delete_indices api

Originally cogl_vertex_buffer_add_indices let the user pass in their own unique
ID for the indices; now the Id is generated internally and returned to the
caller.
This commit is contained in:
Robert Bragg 2009-05-26 16:27:36 +01:00
parent d51faed660
commit 59bd824404
4 changed files with 67 additions and 35 deletions

View File

@ -305,8 +305,6 @@ typedef enum _CoglIndicesType
/** /**
* cogl_vertex_buffer_add_indices: * cogl_vertex_buffer_add_indices:
* @handle: A vertex buffer handle * @handle: A vertex buffer handle
* @id: Any unique number. It's used to identify the indices when you later
* call cogl_vertex_buffer_draw_elements()
* @min_index: Specifies the minimum vertex index contained in indices * @min_index: Specifies the minimum vertex index contained in indices
* @max_index: Specifies the maximum vertex index contained in indices * @max_index: Specifies the maximum vertex index contained in indices
* @indices_type: a #CoglIndicesType specifying the data type used for * @indices_type: a #CoglIndicesType specifying the data type used for
@ -319,20 +317,36 @@ typedef enum _CoglIndicesType
* array allows you to reference vertices multiple times, for example * array allows you to reference vertices multiple times, for example
* during triangle strips. * during triangle strips.
* *
* You should aim to use the COGL_INDICES_TYPE_UNSIGNED_SHORT when possible * You should aim to use the smallest data type possible and correctly reflect
* and correctly reflect the range of index values in the {min,max}_index * the range of index values in the {min,max}_index arguments. This allows Cogl
* arguments. This allows Cogl to optimize the internal storage used for * to optimize the internal storage used for the indices and reduce the demand
* the indices and reduce the demand for memory bandwidth. * for memory bandwidth.
*
* Returns: An identifier (greater than 0) for the indices which you can
* pass to cogl_vertex_buffer_draw_elements().
*/ */
void int
cogl_vertex_buffer_add_indices (CoglHandle handle, cogl_vertex_buffer_add_indices (CoglHandle handle,
int id,
int min_index, int min_index,
int max_index, int max_index,
CoglIndicesType indices_type, CoglIndicesType indices_type,
const void *indices_array, const void *indices_array,
int indices_len); int indices_len);
/**
* cogl_vertex_buffer_delete_indices:
* @handle: A vertex buffer handle
* @indices_id: The identifier for a an array of indices previously added to
* the given Cogl vertex buffer using
* cogl_vertex_buffer_add_indices().
*
* Frees the resources associated with a previously added array of vertex
* indices.
*/
void
cogl_vertex_buffer_delete_indices (CoglHandle handle,
int indices_id);
/** /**
* cogl_vertex_buffer_draw_elements: * cogl_vertex_buffer_draw_elements:
* @handle: A vertex buffer handle * @handle: A vertex buffer handle

View File

@ -1771,9 +1771,8 @@ get_indices_type_size (GLuint indices_type)
} }
} }
void int
cogl_vertex_buffer_add_indices (CoglHandle handle, cogl_vertex_buffer_add_indices (CoglHandle handle,
int id,
int min_index, int min_index,
int max_index, int max_index,
CoglIndicesType indices_type, CoglIndicesType indices_type,
@ -1781,32 +1780,23 @@ cogl_vertex_buffer_add_indices (CoglHandle handle,
int indices_len) int indices_len)
{ {
CoglVertexBuffer *buffer; CoglVertexBuffer *buffer;
GList *l;
gboolean fallback = gboolean fallback =
(cogl_get_features () & COGL_FEATURE_VBOS) ? FALSE : TRUE; (cogl_get_features () & COGL_FEATURE_VBOS) ? FALSE : TRUE;
size_t indices_bytes; size_t indices_bytes;
CoglVertexBufferIndices *indices; CoglVertexBufferIndices *indices;
_COGL_GET_CONTEXT (ctx, NO_RETVAL); static int next_indices_id = 1;
_COGL_GET_CONTEXT (ctx, 0);
if (!cogl_is_vertex_buffer (handle)) if (!cogl_is_vertex_buffer (handle))
return; return 0;
buffer = _cogl_vertex_buffer_pointer_from_handle (handle); buffer = _cogl_vertex_buffer_pointer_from_handle (handle);
for (l = buffer->indices; l; l = l->next)
{
CoglVertexBufferIndices *current_indices = l->data;
if (current_indices->id == id)
{
free_vertex_buffer_indices (l->data);
buffer->indices = g_list_delete_link (buffer->indices, l);
break;
}
}
indices = g_slice_alloc (sizeof (CoglVertexBufferIndices)); indices = g_slice_alloc (sizeof (CoglVertexBufferIndices));
indices->id = id; indices->id = next_indices_id;
indices->min_index = min_index; indices->min_index = min_index;
indices->max_index = max_index; indices->max_index = max_index;
@ -1818,7 +1808,7 @@ cogl_vertex_buffer_add_indices (CoglHandle handle,
{ {
g_critical ("unknown indices type %d", indices_type); g_critical ("unknown indices type %d", indices_type);
g_slice_free (CoglVertexBufferIndices, indices); g_slice_free (CoglVertexBufferIndices, indices);
return; return 0;
} }
indices_bytes = get_indices_type_size (indices->type) * indices_len; indices_bytes = get_indices_type_size (indices->type) * indices_len;
@ -1840,6 +1830,32 @@ cogl_vertex_buffer_add_indices (CoglHandle handle,
} }
buffer->indices = g_list_prepend (buffer->indices, indices); buffer->indices = g_list_prepend (buffer->indices, indices);
return next_indices_id++;
}
void
cogl_vertex_buffer_delete_indices (CoglHandle handle,
int indices_id)
{
CoglVertexBuffer *buffer;
GList *l;
if (!cogl_is_vertex_buffer (handle))
return;
buffer = _cogl_vertex_buffer_pointer_from_handle (handle);
for (l = buffer->indices; l; l = l->next)
{
CoglVertexBufferIndices *current_indices = l->data;
if (current_indices->id == indices_id)
{
free_vertex_buffer_indices (l->data);
buffer->indices = g_list_delete_link (buffer->indices, l);
return;
}
}
} }
void void

View File

@ -309,6 +309,7 @@ cogl_vertex_buffer_enable
cogl_vertex_buffer_draw cogl_vertex_buffer_draw
CoglIndicesType CoglIndicesType
cogl_vertex_buffer_add_indices cogl_vertex_buffer_add_indices
cogl_vertex_buffer_delete_indices
cogl_vertex_buffer_draw_elements cogl_vertex_buffer_draw_elements
<SUBSECTION Private> <SUBSECTION Private>
CoglVertexBufferAttribFlags CoglVertexBufferAttribFlags

View File

@ -49,6 +49,7 @@ typedef struct _TestState
GLubyte *quad_mesh_colors; GLubyte *quad_mesh_colors;
GLushort *static_indices; GLushort *static_indices;
guint n_static_indices; guint n_static_indices;
int indices_id;
ClutterTimeline *timeline; ClutterTimeline *timeline;
} TestState; } TestState;
@ -140,7 +141,7 @@ on_paint (ClutterActor *actor, TestState *state)
cogl_set_source_color4ub (0xff, 0x00, 0x00, 0xff); cogl_set_source_color4ub (0xff, 0x00, 0x00, 0xff);
cogl_vertex_buffer_draw_elements (state->buffer, cogl_vertex_buffer_draw_elements (state->buffer,
COGL_VERTICES_MODE_TRIANGLE_STRIP, COGL_VERTICES_MODE_TRIANGLE_STRIP,
0, /* indices identifier */ state->indices_id,
0, /* indices offset */ 0, /* indices offset */
state->n_static_indices); state->n_static_indices);
} }
@ -219,14 +220,14 @@ init_static_index_arrays (TestState *state)
#undef MESH_INDEX #undef MESH_INDEX
cogl_vertex_buffer_add_indices (state->buffer, state->indices_id =
0, /* identifier */ cogl_vertex_buffer_add_indices (state->buffer,
0, /* min index */ 0, /* min index */
(MESH_WIDTH + 1) * (MESH_WIDTH + 1) *
(MESH_HEIGHT + 1), /* max index */ (MESH_HEIGHT + 1), /* max index */
COGL_INDICES_TYPE_UNSIGNED_SHORT, COGL_INDICES_TYPE_UNSIGNED_SHORT,
state->static_indices, state->static_indices,
state->n_static_indices); state->n_static_indices);
} }
static float static float