cogl: Adds experimental CoglIndices API

CoglIndices define a range of indices inside a CoglIndexArray. I.e. a
CoglIndexArray is simply a buffer of N bytes and you can then
instantiate multiple CoglIndices collections that define a sub-region of
a CoglIndexArray by specifying a start offset and an index data type.
This commit is contained in:
Robert Bragg 2010-10-12 12:48:58 +01:00
parent 296d14e946
commit 6e620e94b6
7 changed files with 416 additions and 0 deletions

View File

@ -74,6 +74,7 @@ cogl_public_h = \
$(srcdir)/cogl-vertex-buffer.h \
$(srcdir)/cogl-index-array.h \
$(srcdir)/cogl-vertex-array.h \
$(srcdir)/cogl-indices.h \
$(srcdir)/cogl.h \
$(NULL)
@ -211,6 +212,8 @@ cogl_sources_c = \
$(srcdir)/cogl-index-array.c \
$(srcdir)/cogl-vertex-array-private.h \
$(srcdir)/cogl-vertex-array.c \
$(srcdir)/cogl-indices-private.h \
$(srcdir)/cogl-indices.c \
$(srcdir)/cogl-matrix.c \
$(srcdir)/cogl-vector.c \
$(srcdir)/cogl-matrix-private.h \

View File

@ -199,6 +199,14 @@ cogl_create_context (void)
_context->quad_indices_short = COGL_INVALID_HANDLE;
_context->quad_indices_short_len = 0;
_context->quad_buffer_indices_byte = COGL_INVALID_HANDLE;
_context->quad_buffer_indices = COGL_INVALID_HANDLE;
_context->quad_buffer_indices_len = 0;
_context->rectangle_byte_indices = NULL;
_context->rectangle_short_indices = NULL;
_context->rectangle_short_indices_len = 0;
_context->texture_download_material = COGL_INVALID_HANDLE;
/* The default for GL_ALPHA_TEST is to always pass which is equivalent to
@ -282,6 +290,16 @@ _cogl_destroy_context (void)
if (_context->quad_indices_short)
cogl_handle_unref (_context->quad_indices_short);
if (_context->quad_buffer_indices_byte)
cogl_handle_unref (_context->quad_buffer_indices_byte);
if (_context->quad_buffer_indices)
cogl_handle_unref (_context->quad_buffer_indices);
if (_context->rectangle_byte_indices)
cogl_object_unref (_context->rectangle_byte_indices);
if (_context->rectangle_short_indices)
cogl_object_unref (_context->rectangle_short_indices);
if (_context->default_material)
cogl_handle_unref (_context->default_material);

View File

@ -145,10 +145,20 @@ typedef struct
/* Pre-generated VBOs containing indices to generate GL_TRIANGLES
out of a vertex array of quads */
/* XXX: These should be removed when the vertex-buffer.c indices
* code is re-worked to use cogl_get_rectangle_indices. */
CoglHandle quad_indices_byte;
unsigned int quad_indices_short_len;
CoglHandle quad_indices_short;
CoglHandle quad_buffer_indices_byte;
unsigned int quad_buffer_indices_len;
CoglHandle quad_buffer_indices;
CoglIndices *rectangle_byte_indices;
CoglIndices *rectangle_short_indices;
int rectangle_short_indices_len;
gboolean in_begin_gl_block;
CoglMaterial *texture_download_material;

View File

@ -0,0 +1,57 @@
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2010 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* 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/>.
*
*
*
* Authors:
* Robert Bragg <robert@linux.intel.com>
*/
#ifndef __COGL_INDICES_PRIVATE_H
#define __COGL_INDICES_PRIVATE_H
#include "cogl-object-private.h"
#include "cogl-index-array-private.h"
#include "cogl-types.h"
struct _CoglIndices
{
CoglObject _parent;
CoglIndexArray *array;
size_t offset;
CoglIndicesType type;
int immutable_ref;
};
CoglIndexArray *
_cogl_indices_get_array (CoglIndices *indices);
CoglIndices *
_cogl_indices_immutable_ref (CoglIndices *indices);
void
_cogl_indices_immutable_unref (CoglIndices *indices);
#endif /* __COGL_INDICES_PRIVATE_H */

253
cogl/cogl-indices.c Normal file
View File

@ -0,0 +1,253 @@
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2010 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* 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/>.
*
*
*
* Authors:
* Robert Bragg <robert@linux.intel.com>
* Neil Roberts <neil@linux.intel.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cogl-object-private.h"
#include "cogl-context.h"
#include "cogl-indices.h"
#include "cogl-indices-private.h"
#include "cogl-index-array.h"
#include <stdarg.h>
static void _cogl_indices_free (CoglIndices *indices);
COGL_OBJECT_DEFINE (Indices, indices);
static size_t
sizeof_indices_type (CoglIndicesType type)
{
switch (type)
{
case COGL_INDICES_TYPE_UNSIGNED_BYTE:
return 1;
case COGL_INDICES_TYPE_UNSIGNED_SHORT:
return 2;
case COGL_INDICES_TYPE_UNSIGNED_INT:
return 4;
}
g_return_val_if_reached (0);
}
CoglIndices *
cogl_indices_new_for_array (CoglIndicesType type,
CoglIndexArray *array,
gsize offset)
{
CoglIndices *indices = g_slice_new (CoglIndices);
indices->array = cogl_object_ref (array);
indices->offset = offset;
indices->type = type;
indices->immutable_ref = 0;
return _cogl_indices_object_new (indices);
}
CoglIndices *
cogl_indices_new (CoglIndicesType type,
const void *indices_data,
int n_indices)
{
size_t array_bytes = sizeof_indices_type (type) * n_indices;
CoglIndexArray *array = cogl_index_array_new (array_bytes);
CoglBuffer *buffer = COGL_BUFFER (array);
CoglIndices *indices;
cogl_buffer_set_data (buffer,
0,
indices_data,
array_bytes);
indices = cogl_indices_new_for_array (type, array, 0);
cogl_object_unref (array);
return indices;
}
CoglIndexArray *
_cogl_indices_get_array (CoglIndices *indices)
{
return indices->array;
}
CoglIndicesType
cogl_indices_get_type (CoglIndices *indices)
{
g_return_val_if_fail (cogl_is_indices (indices),
COGL_INDICES_TYPE_UNSIGNED_BYTE);
return indices->type;
}
gsize
cogl_indices_get_offset (CoglIndices *indices)
{
g_return_val_if_fail (cogl_is_indices (indices), 0);
return indices->offset;
}
static void
warn_about_midscene_changes (void)
{
static gboolean seen = FALSE;
if (!seen)
{
g_warning ("Mid-scene modification of indices has "
"undefined results\n");
seen = TRUE;
}
}
void
cogl_indices_set_offset (CoglIndices *indices,
gsize offset)
{
g_return_if_fail (cogl_is_indices (indices));
if (G_UNLIKELY (indices->immutable_ref))
warn_about_midscene_changes ();
indices->offset = offset;
}
static void
_cogl_indices_free (CoglIndices *indices)
{
cogl_object_unref (indices->array);
g_slice_free (CoglIndices, indices);
}
CoglIndices *
_cogl_indices_immutable_ref (CoglIndices *indices)
{
g_return_val_if_fail (cogl_is_indices (indices), NULL);
indices->immutable_ref++;
_cogl_buffer_immutable_ref (COGL_BUFFER (indices->array));
return indices;
}
void
_cogl_indices_immutable_unref (CoglIndices *indices)
{
g_return_if_fail (cogl_is_indices (indices));
g_return_if_fail (indices->immutable_ref > 0);
indices->immutable_ref--;
_cogl_buffer_immutable_unref (COGL_BUFFER (indices->array));
}
CoglIndices *
cogl_get_rectangle_indices (int n_rectangles)
{
int n_indices = n_rectangles * 6;
_COGL_GET_CONTEXT (ctx, NULL);
/* Check if the largest index required will fit in a byte array... */
if (n_indices <= 256 / 4 * 6)
{
/* Generate the byte array if we haven't already */
if (ctx->rectangle_byte_indices == NULL)
{
guint8 *byte_array = g_malloc (256 / 4 * 6 * sizeof (guint8));
guint8 *p = byte_array;
int i, vert_num = 0;
for (i = 0; i < 256 / 4; i++)
{
*(p++) = vert_num + 0;
*(p++) = vert_num + 1;
*(p++) = vert_num + 2;
*(p++) = vert_num + 0;
*(p++) = vert_num + 2;
*(p++) = vert_num + 3;
vert_num += 4;
}
ctx->rectangle_byte_indices
= cogl_indices_new (COGL_INDICES_TYPE_UNSIGNED_BYTE,
byte_array,
256 / 4 * 6);
g_free (byte_array);
}
return ctx->rectangle_byte_indices;
}
else
{
if (ctx->rectangle_short_indices_len < n_indices)
{
guint16 *short_array;
guint16 *p;
int i, vert_num = 0;
if (ctx->rectangle_short_indices != NULL)
cogl_object_unref (ctx->rectangle_short_indices);
/* Pick a power of two >= MAX (512, n_indices) */
if (ctx->rectangle_short_indices_len == 0)
ctx->rectangle_short_indices_len = 512;
while (ctx->rectangle_short_indices_len < n_indices)
ctx->rectangle_short_indices_len *= 2;
/* Over-allocate to generate a whole number of quads */
p = short_array = g_malloc ((ctx->rectangle_short_indices_len
+ 5) / 6 * 6
* sizeof (guint16));
/* Fill in the complete quads */
for (i = 0; i < ctx->rectangle_short_indices_len; i += 6)
{
*(p++) = vert_num + 0;
*(p++) = vert_num + 1;
*(p++) = vert_num + 2;
*(p++) = vert_num + 0;
*(p++) = vert_num + 2;
*(p++) = vert_num + 3;
vert_num += 4;
}
ctx->rectangle_short_indices
= cogl_indices_new (COGL_INDICES_TYPE_UNSIGNED_SHORT,
short_array,
ctx->rectangle_short_indices_len);
g_free (short_array);
}
return ctx->rectangle_short_indices;
}
}

74
cogl/cogl-indices.h Normal file
View File

@ -0,0 +1,74 @@
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2010 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* 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/>.
*
*
*
* Authors:
* Robert Bragg <robert@linux.intel.com>
*/
#if !defined(__COGL_H_INSIDE__) && !defined(CLUTTER_COMPILATION)
#error "Only <cogl/cogl.h> can be included directly."
#endif
#ifndef __COGL_INDICES_H__
#define __COGL_INDICES_H__
#include <cogl/cogl-index-array.h>
G_BEGIN_DECLS
/**
* SECTION:cogl-index-range
* @short_description: Fuctions for declaring a range of vertex indices
* stored in a #CoglIndexArray.
*
* FIXME
*/
typedef struct _CoglIndices CoglIndices;
CoglIndices *
cogl_indices_new (CoglIndicesType type,
const void *indices_data,
int n_indices);
CoglIndices *
cogl_indices_new_for_array (CoglIndicesType type,
CoglIndexArray *array,
gsize offset);
CoglIndicesType
cogl_indices_get_type (CoglIndices *indices);
gsize
cogl_indices_get_offset (CoglIndices *indices);
void
cogl_indices_set_offset (CoglIndices *indices,
gsize offset);
CoglIndices *
cogl_get_rectangle_indices (int n_rectangles);
G_END_DECLS
#endif /* __COGL_INDICES_H__ */

View File

@ -56,6 +56,7 @@
#include <cogl/cogl-texture-3d.h>
#include <cogl/cogl-index-array.h>
#include <cogl/cogl-vertex-array.h>
#include <cogl/cogl-indices.h>
#endif
G_BEGIN_DECLS