2010-10-12 07:48:58 -04:00
|
|
|
/*
|
|
|
|
* Cogl
|
|
|
|
*
|
2014-02-21 20:28:54 -05:00
|
|
|
* A Low Level GPU Graphics and Utilities API
|
2010-10-12 07:48:58 -04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Intel Corporation.
|
|
|
|
*
|
2014-02-21 20:28:54 -05:00
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation
|
|
|
|
* files (the "Software"), to deal in the Software without
|
|
|
|
* restriction, including without limitation the rights to use, copy,
|
|
|
|
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
* of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
2010-10-12 07:48:58 -04:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Robert Bragg <robert@linux.intel.com>
|
|
|
|
* Neil Roberts <neil@linux.intel.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-10-13 17:34:30 -04:00
|
|
|
#include "cogl-util.h"
|
2010-10-12 07:48:58 -04:00
|
|
|
#include "cogl-object-private.h"
|
2010-11-04 18:25:52 -04:00
|
|
|
#include "cogl-context-private.h"
|
2010-10-12 07:48:58 -04:00
|
|
|
#include "cogl-indices.h"
|
|
|
|
#include "cogl-indices-private.h"
|
2011-03-02 18:31:19 -05:00
|
|
|
#include "cogl-index-buffer.h"
|
2010-10-12 07:48:58 -04:00
|
|
|
|
|
|
|
#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 *
|
2011-03-02 18:31:19 -05:00
|
|
|
cogl_indices_new_for_buffer (CoglIndicesType type,
|
|
|
|
CoglIndexBuffer *buffer,
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
size_t offset)
|
2010-10-12 07:48:58 -04:00
|
|
|
{
|
|
|
|
CoglIndices *indices = g_slice_new (CoglIndices);
|
|
|
|
|
2011-03-02 18:31:19 -05:00
|
|
|
indices->buffer = cogl_object_ref (buffer);
|
2010-10-12 07:48:58 -04:00
|
|
|
indices->offset = offset;
|
|
|
|
|
|
|
|
indices->type = type;
|
|
|
|
|
|
|
|
indices->immutable_ref = 0;
|
|
|
|
|
|
|
|
return _cogl_indices_object_new (indices);
|
|
|
|
}
|
|
|
|
|
|
|
|
CoglIndices *
|
2012-02-06 12:08:58 -05:00
|
|
|
cogl_indices_new (CoglContext *context,
|
|
|
|
CoglIndicesType type,
|
2010-10-12 07:48:58 -04:00
|
|
|
const void *indices_data,
|
|
|
|
int n_indices)
|
|
|
|
{
|
2011-03-02 18:31:19 -05:00
|
|
|
size_t buffer_bytes = sizeof_indices_type (type) * n_indices;
|
2012-02-06 12:08:58 -05:00
|
|
|
CoglIndexBuffer *index_buffer = cogl_index_buffer_new (context, buffer_bytes);
|
2011-03-02 18:31:19 -05:00
|
|
|
CoglBuffer *buffer = COGL_BUFFER (index_buffer);
|
2010-10-12 07:48:58 -04:00
|
|
|
CoglIndices *indices;
|
2012-11-08 12:54:10 -05:00
|
|
|
CoglError *ignore_error = NULL;
|
|
|
|
|
|
|
|
_cogl_buffer_set_data (buffer,
|
|
|
|
0,
|
|
|
|
indices_data,
|
|
|
|
buffer_bytes,
|
|
|
|
&ignore_error);
|
|
|
|
if (ignore_error)
|
|
|
|
{
|
|
|
|
cogl_error_free (ignore_error);
|
|
|
|
cogl_object_unref (index_buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-10-12 07:48:58 -04:00
|
|
|
|
2011-03-02 18:31:19 -05:00
|
|
|
indices = cogl_indices_new_for_buffer (type, index_buffer, 0);
|
|
|
|
cogl_object_unref (index_buffer);
|
2010-10-12 07:48:58 -04:00
|
|
|
|
|
|
|
return indices;
|
|
|
|
}
|
|
|
|
|
2011-03-02 18:31:19 -05:00
|
|
|
CoglIndexBuffer *
|
|
|
|
cogl_indices_get_buffer (CoglIndices *indices)
|
2010-10-12 07:48:58 -04:00
|
|
|
{
|
2011-03-02 18:31:19 -05:00
|
|
|
return indices->buffer;
|
2010-10-12 07:48:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CoglIndicesType
|
|
|
|
cogl_indices_get_type (CoglIndices *indices)
|
|
|
|
{
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_VAL_IF_FAIL (cogl_is_indices (indices),
|
|
|
|
COGL_INDICES_TYPE_UNSIGNED_BYTE);
|
2010-10-12 07:48:58 -04:00
|
|
|
return indices->type;
|
|
|
|
}
|
|
|
|
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
size_t
|
2010-10-12 07:48:58 -04:00
|
|
|
cogl_indices_get_offset (CoglIndices *indices)
|
|
|
|
{
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_VAL_IF_FAIL (cogl_is_indices (indices), 0);
|
2010-10-12 07:48:58 -04:00
|
|
|
|
|
|
|
return indices->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
warn_about_midscene_changes (void)
|
|
|
|
{
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
static CoglBool seen = FALSE;
|
2010-10-12 07:48:58 -04:00
|
|
|
if (!seen)
|
|
|
|
{
|
|
|
|
g_warning ("Mid-scene modification of indices has "
|
|
|
|
"undefined results\n");
|
|
|
|
seen = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_indices_set_offset (CoglIndices *indices,
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
size_t offset)
|
2010-10-12 07:48:58 -04:00
|
|
|
{
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_IF_FAIL (cogl_is_indices (indices));
|
2010-10-12 07:48:58 -04:00
|
|
|
|
|
|
|
if (G_UNLIKELY (indices->immutable_ref))
|
|
|
|
warn_about_midscene_changes ();
|
|
|
|
|
|
|
|
indices->offset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cogl_indices_free (CoglIndices *indices)
|
|
|
|
{
|
2011-03-02 18:31:19 -05:00
|
|
|
cogl_object_unref (indices->buffer);
|
2010-10-12 07:48:58 -04:00
|
|
|
g_slice_free (CoglIndices, indices);
|
|
|
|
}
|
|
|
|
|
|
|
|
CoglIndices *
|
|
|
|
_cogl_indices_immutable_ref (CoglIndices *indices)
|
|
|
|
{
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_VAL_IF_FAIL (cogl_is_indices (indices), NULL);
|
2010-10-12 07:48:58 -04:00
|
|
|
|
|
|
|
indices->immutable_ref++;
|
2011-03-02 18:31:19 -05:00
|
|
|
_cogl_buffer_immutable_ref (COGL_BUFFER (indices->buffer));
|
2010-10-12 07:48:58 -04:00
|
|
|
return indices;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_indices_immutable_unref (CoglIndices *indices)
|
|
|
|
{
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_IF_FAIL (cogl_is_indices (indices));
|
|
|
|
_COGL_RETURN_IF_FAIL (indices->immutable_ref > 0);
|
2010-10-12 07:48:58 -04:00
|
|
|
|
|
|
|
indices->immutable_ref--;
|
2011-03-02 18:31:19 -05:00
|
|
|
_cogl_buffer_immutable_unref (COGL_BUFFER (indices->buffer));
|
2010-10-12 07:48:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CoglIndices *
|
2012-02-06 12:08:58 -05:00
|
|
|
cogl_get_rectangle_indices (CoglContext *ctx, int n_rectangles)
|
2010-10-12 07:48:58 -04:00
|
|
|
{
|
|
|
|
int n_indices = n_rectangles * 6;
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
uint8_t *byte_array = g_malloc (256 / 4 * 6 * sizeof (uint8_t));
|
|
|
|
uint8_t *p = byte_array;
|
2010-10-12 07:48:58 -04:00
|
|
|
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
|
2012-02-06 12:08:58 -05:00
|
|
|
= cogl_indices_new (ctx,
|
|
|
|
COGL_INDICES_TYPE_UNSIGNED_BYTE,
|
2010-10-12 07:48:58 -04:00
|
|
|
byte_array,
|
|
|
|
256 / 4 * 6);
|
|
|
|
|
|
|
|
g_free (byte_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx->rectangle_byte_indices;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ctx->rectangle_short_indices_len < n_indices)
|
|
|
|
{
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
uint16_t *short_array;
|
|
|
|
uint16_t *p;
|
2010-10-12 07:48:58 -04:00
|
|
|
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
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
* sizeof (uint16_t));
|
2010-10-12 07:48:58 -04:00
|
|
|
|
|
|
|
/* 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
|
2012-02-06 12:08:58 -05:00
|
|
|
= cogl_indices_new (ctx,
|
|
|
|
COGL_INDICES_TYPE_UNSIGNED_SHORT,
|
2010-10-12 07:48:58 -04:00
|
|
|
short_array,
|
|
|
|
ctx->rectangle_short_indices_len);
|
|
|
|
|
|
|
|
g_free (short_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx->rectangle_short_indices;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|