4
* A Low Level GPU Graphics and Utilities API
6
* Copyright (C) 2010 Intel Corporation.
8
* Permission is hereby granted, free of charge, to any person
9
* obtaining a copy of this software and associated documentation
10
* files (the "Software"), to deal in the Software without
11
* restriction, including without limitation the rights to use, copy,
12
* modify, merge, publish, distribute, sublicense, and/or sell copies
13
* of the Software, and to permit persons to whom the Software is
14
* furnished to do so, subject to the following conditions:
16
* The above copyright notice and this permission notice shall be
17
* included in all copies or substantial portions of the Software.
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
* Robert Bragg <robert@linux.intel.com>
32
* Neil Roberts <neil@linux.intel.com>
39
#include "cogl-util.h"
40
#include "cogl-object-private.h"
41
#include "cogl-context-private.h"
42
#include "cogl-indices.h"
43
#include "cogl-indices-private.h"
44
#include "cogl-index-buffer.h"
45
#include "cogl-gtype-private.h"
49
static void _cogl_indices_free (CoglIndices *indices);
51
COGL_OBJECT_DEFINE (Indices, indices);
52
COGL_GTYPE_DEFINE_CLASS (Indices, indices);
55
sizeof_indices_type (CoglIndicesType type)
59
case COGL_INDICES_TYPE_UNSIGNED_BYTE:
61
case COGL_INDICES_TYPE_UNSIGNED_SHORT:
63
case COGL_INDICES_TYPE_UNSIGNED_INT:
66
g_return_val_if_reached (0);
70
cogl_indices_new_for_buffer (CoglIndicesType type,
71
CoglIndexBuffer *buffer,
74
CoglIndices *indices = g_slice_new (CoglIndices);
76
indices->buffer = cogl_object_ref (buffer);
77
indices->offset = offset;
81
indices->immutable_ref = 0;
83
return _cogl_indices_object_new (indices);
87
cogl_indices_new (CoglContext *context,
89
const void *indices_data,
92
size_t buffer_bytes = sizeof_indices_type (type) * n_indices;
93
CoglIndexBuffer *index_buffer = cogl_index_buffer_new (context, buffer_bytes);
94
CoglBuffer *buffer = COGL_BUFFER (index_buffer);
96
CoglError *ignore_error = NULL;
98
_cogl_buffer_set_data (buffer,
105
cogl_error_free (ignore_error);
106
cogl_object_unref (index_buffer);
110
indices = cogl_indices_new_for_buffer (type, index_buffer, 0);
111
cogl_object_unref (index_buffer);
117
cogl_indices_get_buffer (CoglIndices *indices)
119
return indices->buffer;
123
cogl_indices_get_type (CoglIndices *indices)
125
_COGL_RETURN_VAL_IF_FAIL (cogl_is_indices (indices),
126
COGL_INDICES_TYPE_UNSIGNED_BYTE);
127
return indices->type;
131
cogl_indices_get_offset (CoglIndices *indices)
133
_COGL_RETURN_VAL_IF_FAIL (cogl_is_indices (indices), 0);
135
return indices->offset;
139
warn_about_midscene_changes (void)
141
static CoglBool seen = FALSE;
144
g_warning ("Mid-scene modification of indices has "
145
"undefined results\n");
151
cogl_indices_set_offset (CoglIndices *indices,
154
_COGL_RETURN_IF_FAIL (cogl_is_indices (indices));
156
if (G_UNLIKELY (indices->immutable_ref))
157
warn_about_midscene_changes ();
159
indices->offset = offset;
163
_cogl_indices_free (CoglIndices *indices)
165
cogl_object_unref (indices->buffer);
166
g_slice_free (CoglIndices, indices);
170
_cogl_indices_immutable_ref (CoglIndices *indices)
172
_COGL_RETURN_VAL_IF_FAIL (cogl_is_indices (indices), NULL);
174
indices->immutable_ref++;
175
_cogl_buffer_immutable_ref (COGL_BUFFER (indices->buffer));
180
_cogl_indices_immutable_unref (CoglIndices *indices)
182
_COGL_RETURN_IF_FAIL (cogl_is_indices (indices));
183
_COGL_RETURN_IF_FAIL (indices->immutable_ref > 0);
185
indices->immutable_ref--;
186
_cogl_buffer_immutable_unref (COGL_BUFFER (indices->buffer));
190
cogl_get_rectangle_indices (CoglContext *ctx, int n_rectangles)
192
int n_indices = n_rectangles * 6;
194
/* Check if the largest index required will fit in a byte array... */
195
if (n_indices <= 256 / 4 * 6)
197
/* Generate the byte array if we haven't already */
198
if (ctx->rectangle_byte_indices == NULL)
200
uint8_t *byte_array = g_malloc (256 / 4 * 6 * sizeof (uint8_t));
201
uint8_t *p = byte_array;
204
for (i = 0; i < 256 / 4; i++)
206
*(p++) = vert_num + 0;
207
*(p++) = vert_num + 1;
208
*(p++) = vert_num + 2;
209
*(p++) = vert_num + 0;
210
*(p++) = vert_num + 2;
211
*(p++) = vert_num + 3;
215
ctx->rectangle_byte_indices
216
= cogl_indices_new (ctx,
217
COGL_INDICES_TYPE_UNSIGNED_BYTE,
224
return ctx->rectangle_byte_indices;
228
if (ctx->rectangle_short_indices_len < n_indices)
230
uint16_t *short_array;
234
if (ctx->rectangle_short_indices != NULL)
235
cogl_object_unref (ctx->rectangle_short_indices);
236
/* Pick a power of two >= MAX (512, n_indices) */
237
if (ctx->rectangle_short_indices_len == 0)
238
ctx->rectangle_short_indices_len = 512;
239
while (ctx->rectangle_short_indices_len < n_indices)
240
ctx->rectangle_short_indices_len *= 2;
242
/* Over-allocate to generate a whole number of quads */
243
p = short_array = g_malloc ((ctx->rectangle_short_indices_len
245
* sizeof (uint16_t));
247
/* Fill in the complete quads */
248
for (i = 0; i < ctx->rectangle_short_indices_len; i += 6)
250
*(p++) = vert_num + 0;
251
*(p++) = vert_num + 1;
252
*(p++) = vert_num + 2;
253
*(p++) = vert_num + 0;
254
*(p++) = vert_num + 2;
255
*(p++) = vert_num + 3;
259
ctx->rectangle_short_indices
260
= cogl_indices_new (ctx,
261
COGL_INDICES_TYPE_UNSIGNED_SHORT,
263
ctx->rectangle_short_indices_len);
265
g_free (short_array);
268
return ctx->rectangle_short_indices;