~oem-solutions-group/unity-2d/clutter-1.0

« back to all changes in this revision

Viewing changes to clutter/cogl/cogl/cogl-vertex-buffer.h

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-03-21 13:27:56 UTC
  • mto: (2.1.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20100321132756-nf8yd30yxo3zzwcm
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Cogl
 
3
 *
 
4
 * An object oriented GL/GLES Abstraction/Utility Layer
 
5
 *
 
6
 * Copyright (C) 2008,2009 Intel Corporation.
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public
 
10
 * License as published by the Free Software Foundation; either
 
11
 * version 2 of the License, or (at your option) any later version.
 
12
 *
 
13
 * This library is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * Lesser General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 
20
 *
 
21
 *
 
22
 *
 
23
 * Authors:
 
24
 *   Robert Bragg <robert@linux.intel.com>
 
25
 */
 
26
 
 
27
#if !defined(__COGL_H_INSIDE__) && !defined(CLUTTER_COMPILATION)
 
28
#error "Only <cogl/cogl.h> can be included directly."
 
29
#endif
 
30
 
 
31
#ifndef __COGL_VERTEX_BUFFER_H__
 
32
#define __COGL_VERTEX_BUFFER_H__
 
33
 
 
34
#include <glib.h>
 
35
#include <cogl/cogl-types.h>
 
36
 
 
37
G_BEGIN_DECLS
 
38
 
 
39
/**
 
40
 * SECTION:cogl-vertex-buffer
 
41
 * @short_description: An API for submitting extensible arrays of vertex
 
42
 *   attributes to be mapped into the GPU for fast drawing.
 
43
 *
 
44
 * For example to describe a textured triangle, you could create a new cogl
 
45
 * vertex buffer with 3 vertices, and then you might add 2 attributes for each
 
46
 * vertex:
 
47
 * <orderedlist>
 
48
 * <listitem>
 
49
 * a "gl_Position" describing the (x,y,z) position for each vertex.
 
50
 * </listitem>
 
51
 * <listitem>
 
52
 * a "gl_MultiTexCoord0" describing the (tx,ty) texture coordinates for each
 
53
 * vertex.
 
54
 * </listitem>
 
55
 * </orderedlist>
 
56
 *
 
57
 * The Vertex Buffer API is designed to be a fairly raw mechanism for
 
58
 * developers to be able to submit geometry to Cogl in a format that can be
 
59
 * directly consumed by an OpenGL driver and mapped into your GPU for fast
 
60
 * re-use. It is designed to avoid repeated validation of the attributes by the
 
61
 * driver; to minimize transport costs (e.g. considering indirect GLX
 
62
 * use-cases) and to potentially avoid repeated format conversions when
 
63
 * attributes are supplied in a format that is not natively supported by the
 
64
 * GPU.
 
65
 *
 
66
 * Although this API does allow you to modify attributes after they have been
 
67
 * submitted to the GPU you should be aware that modification is not that
 
68
 * cheap, since it implies validating the new data and potentially the
 
69
 * OpenGL driver will need to reformat it for the GPU.
 
70
 *
 
71
 * If at all possible think of tricks that let you re-use static attributes,
 
72
 * and if you do need to repeatedly update attributes (e.g. for some kind of
 
73
 * morphing geometry) then only update and re-submit the specific attributes
 
74
 * that have changed.
 
75
 */
 
76
 
 
77
/**
 
78
 * cogl_vertex_buffer_new:
 
79
 * @n_vertices: The number of vertices that your attributes will correspond to.
 
80
 *
 
81
 * Creates a new vertex buffer that you can use to add attributes.
 
82
 *
 
83
 * Return value: a new #CoglHandle
 
84
 */
 
85
CoglHandle
 
86
cogl_vertex_buffer_new (unsigned int n_vertices);
 
87
 
 
88
/**
 
89
 * cogl_vertex_buffer_get_n_vertices:
 
90
 * @handle: A vertex buffer handle
 
91
 *
 
92
 * Retrieves the number of vertices that @handle represents
 
93
 *
 
94
 * Return value: the number of vertices
 
95
 */
 
96
unsigned int
 
97
cogl_vertex_buffer_get_n_vertices (CoglHandle handle);
 
98
 
 
99
/**
 
100
 * CoglAttributeType:
 
101
 * @COGL_ATTRIBUTE_TYPE_BYTE: Data is the same size of a byte
 
102
 * @COGL_ATTRIBUTE_TYPE_UNSIGNED_BYTE: Data is the same size of an
 
103
 *   unsigned byte
 
104
 * @COGL_ATTRIBUTE_TYPE_SHORT: Data is the same size of a short integer
 
105
 * @COGL_ATTRIBUTE_TYPE_UNSIGNED_SHORT: Data is the same size of
 
106
 *   an unsigned short integer
 
107
 * @COGL_ATTRIBUTE_TYPE_FLOAT: Data is the same size of a float
 
108
 *
 
109
 * Data types for the components of cogl_vertex_buffer_add()
 
110
 *
 
111
 * Since: 1.0
 
112
 */
 
113
typedef enum {
 
114
  COGL_ATTRIBUTE_TYPE_BYTE = GL_BYTE,
 
115
  COGL_ATTRIBUTE_TYPE_UNSIGNED_BYTE = GL_UNSIGNED_BYTE,
 
116
  COGL_ATTRIBUTE_TYPE_SHORT = GL_SHORT,
 
117
  COGL_ATTRIBUTE_TYPE_UNSIGNED_SHORT = GL_UNSIGNED_SHORT,
 
118
  COGL_ATTRIBUTE_TYPE_FLOAT = GL_FLOAT
 
119
} CoglAttributeType;
 
120
 
 
121
/**
 
122
 * cogl_vertex_buffer_add:
 
123
 * @handle: A vertex buffer handle
 
124
 * @attribute_name: The name of your attribute. It should be a valid GLSL
 
125
 *   variable name and standard attribute types must use one of following
 
126
 *   built-in names: (Note: they correspond to the built-in names of GLSL)
 
127
 *   <itemizedlist>
 
128
 *     <listitem>"gl_Color"</listitem>
 
129
 *     <listitem>"gl_Normal"</listitem>
 
130
 *     <listitem>"gl_MultiTexCoord0, gl_MultiTexCoord1, ..."</listitem>
 
131
 *     <listitem>"gl_Vertex"</listitem>
 
132
 *   </itemizedlist>
 
133
 *   To support adding multiple variations of the same attribute the name
 
134
 *   can have a detail component, E.g. "gl_Color::active" or
 
135
 *   "gl_Color::inactive"
 
136
 * @n_components: The number of components per attribute and must be 1, 2,
 
137
 *   3 or 4
 
138
 * @type: a #CoglAttributeType specifying the data type of each component.
 
139
 * @normalized: If %TRUE, this specifies that values stored in an integer
 
140
 *   format should be mapped into the range [-1.0, 1.0] or [0.0, 1.0]
 
141
 *   for unsigned values. If %FALSE they are converted to floats
 
142
 *   directly.
 
143
 * @stride: This specifies the number of bytes from the start of one attribute
 
144
 *   value to the start of the next value (for the same attribute). So, for
 
145
 *   example, with a position interleved with color like this:
 
146
 *   XYRGBAXYRGBAXYRGBA, then if each letter represents a byte, the
 
147
 *   stride for both attributes is 6. The special value 0 means the
 
148
 *   values are stored sequentially in memory.
 
149
 * @pointer: This addresses the first attribute in the vertex array. This
 
150
 *   must remain valid until you either call cogl_vertex_buffer_submit() or
 
151
 *   issue a draw call.
 
152
 *
 
153
 * Adds an attribute to a buffer.
 
154
 *
 
155
 * You either can use one of the built-in names such as "gl_Vertex", or
 
156
 * "gl_MultiTexCoord0" to add standard attributes, like positions, colors
 
157
 * and normals, or you can add custom attributes for use in shaders.
 
158
 *
 
159
 * The number of vertices declared when calling cogl_vertex_buffer_new()
 
160
 * determines how many attribute values will be read from the supplied
 
161
 * @pointer.
 
162
 *
 
163
 * The data for your attribute isn't copied anywhere until you call
 
164
 * cogl_vertex_buffer_submit(), or issue a draw call which automatically
 
165
 * submits pending attribute changes. so the supplied pointer must remain
 
166
 * valid until then. If you are updating an existing attribute (done by
 
167
 * re-adding it) then you still need to re-call cogl_vertex_buffer_submit()
 
168
 * to commit the changes to the GPU. Be carefull to minimize the number
 
169
 * of calls to cogl_vertex_buffer_submit(), though.
 
170
 *
 
171
 * <note>If you are interleving attributes it is assumed that each interleaved
 
172
 * attribute starts no farther than +- stride bytes from the other attributes
 
173
 * it is interleved with. I.e. this is ok:
 
174
 * <programlisting>
 
175
 * |-0-0-0-0-0-0-0-0-0-0|
 
176
 * </programlisting>
 
177
 * This is not ok:
 
178
 * <programlisting>
 
179
 * |- - - - -0-0-0-0-0-0 0 0 0 0|
 
180
 * </programlisting>
 
181
 * (Though you can have multiple groups of interleved attributes)</note>
 
182
 */
 
183
void
 
184
cogl_vertex_buffer_add (CoglHandle         handle,
 
185
                        const char        *attribute_name,
 
186
                        guint8             n_components,
 
187
                        CoglAttributeType  type,
 
188
                        gboolean           normalized,
 
189
                        guint16            stride,
 
190
                        const void        *pointer);
 
191
 
 
192
/**
 
193
 * cogl_vertex_buffer_delete:
 
194
 * @handle: A vertex buffer handle
 
195
 * @attribute_name: The name of a previously added attribute
 
196
 *
 
197
 * Deletes an attribute from a buffer. You will need to call
 
198
 * cogl_vertex_buffer_submit() or issue a draw call to commit this
 
199
 * change to the GPU.
 
200
 */
 
201
void
 
202
cogl_vertex_buffer_delete (CoglHandle   handle,
 
203
                           const char  *attribute_name);
 
204
 
 
205
/**
 
206
 * cogl_vertex_buffer_submit:
 
207
 * @handle: A vertex buffer handle
 
208
 *
 
209
 * Submits all the user added attributes to the GPU; once submitted, the
 
210
 * attributes can be used for drawing.
 
211
 *
 
212
 * You should aim to minimize calls to this function since it implies
 
213
 * validating your data; it potentially incurs a transport cost (especially if
 
214
 * you are using GLX indirect rendering) and potentially a format conversion
 
215
 * cost if the GPU doesn't natively support any of the given attribute formats.
 
216
 */
 
217
void
 
218
cogl_vertex_buffer_submit (CoglHandle handle);
 
219
 
 
220
/**
 
221
 * cogl_vertex_buffer_disable:
 
222
 * @handle: A vertex buffer handle
 
223
 * @attribute_name: The name of the attribute you want to disable
 
224
 *
 
225
 * Disables a previosuly added attribute.
 
226
 *
 
227
 * Since it can be costly to add and remove new attributes to buffers; to make
 
228
 * individual buffers more reuseable it is possible to enable and disable
 
229
 * attributes before using a buffer for drawing.
 
230
 *
 
231
 * You don't need to call cogl_vertex_buffer_submit() after using this
 
232
 * function.
 
233
 */
 
234
void
 
235
cogl_vertex_buffer_disable (CoglHandle  handle,
 
236
                            const char *attribute_name);
 
237
 
 
238
/**
 
239
 * cogl_vertex_buffer_enable:
 
240
 * @handle: A vertex buffer handle
 
241
 * @attribute_name: The name of the attribute you want to enable
 
242
 *
 
243
 * Enables a previosuly disabled attribute.
 
244
 *
 
245
 * Since it can be costly to add and remove new attributes to buffers; to make
 
246
 * individual buffers more reuseable it is possible to enable and disable
 
247
 * attributes before using a buffer for drawing.
 
248
 *
 
249
 * You don't need to call cogl_vertex_buffer_submit() after using this function
 
250
 */
 
251
void
 
252
cogl_vertex_buffer_enable (CoglHandle  handle,
 
253
                           const char *attribute_name);
 
254
 
 
255
/**
 
256
 * CoglVerticesMode:
 
257
 * @COGL_VERTICES_MODE_POINTS: FIXME, equivalent to %GL_POINTS
 
258
 * @COGL_VERTICES_MODE_LINE_STRIP: FIXME, equivalent to %GL_LINE_STRIP
 
259
 * @COGL_VERTICES_MODE_LINE_LOOP: FIXME, equivalent to %GL_LINE_LOOP
 
260
 * @COGL_VERTICES_MODE_LINES: FIXME, equivalent to %GL_LINES
 
261
 * @COGL_VERTICES_MODE_TRIANGLE_STRIP: FIXME, equivalent to %GL_TRIANGLE_STRIP
 
262
 * @COGL_VERTICES_MODE_TRIANGLE_FAN: FIXME, equivalent to %GL_TRIANGLE_FAN
 
263
 * @COGL_VERTICES_MODE_TRIANGLES: FIXME, equivalent to %GL_TRIANGLES
 
264
 *
 
265
 * How vertices passed to cogl_vertex_buffer_draw() and
 
266
 * cogl_vertex_buffer_draw_elements() should be interpreted
 
267
 *
 
268
 * Since: 1.0
 
269
 */
 
270
typedef enum {
 
271
  COGL_VERTICES_MODE_POINTS = GL_POINTS,
 
272
  COGL_VERTICES_MODE_LINE_STRIP = GL_LINE_STRIP,
 
273
  COGL_VERTICES_MODE_LINE_LOOP = GL_LINE_LOOP,
 
274
  COGL_VERTICES_MODE_LINES = GL_LINES,
 
275
  COGL_VERTICES_MODE_TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
 
276
  COGL_VERTICES_MODE_TRIANGLE_FAN = GL_TRIANGLE_FAN,
 
277
  COGL_VERTICES_MODE_TRIANGLES = GL_TRIANGLES
 
278
} CoglVerticesMode;
 
279
 
 
280
/**
 
281
 * cogl_vertex_buffer_draw:
 
282
 * @handle: A vertex buffer handle
 
283
 * @mode: A #CoglVerticesMode specifying how the vertices should be
 
284
 *   interpreted.
 
285
 * @first: Specifies the index of the first vertex you want to draw with
 
286
 * @count: Specifies the number of vertices you want to draw.
 
287
 *
 
288
 * Allows you to draw geometry using all or a subset of the
 
289
 * vertices in a vertex buffer.
 
290
 *
 
291
 * Any un-submitted attribute changes are automatically submitted before
 
292
 * drawing.
 
293
 */
 
294
void
 
295
cogl_vertex_buffer_draw (CoglHandle       handle,
 
296
                         CoglVerticesMode mode,
 
297
                         int              first,
 
298
                         int              count);
 
299
 
 
300
/**
 
301
 * CoglIndicesType:
 
302
 * @COGL_INDICES_TYPE_UNSIGNED_BYTE: Your indices are unsigned bytes
 
303
 * @COGL_INDICES_TYPE_UNSIGNED_SHORT: Your indices are unsigned shorts
 
304
 * @COGL_INDICES_TYPE_UNSIGNED_INT: Your indices are unsigned ints
 
305
 *
 
306
 * You should aim to use the smallest data type that gives you enough
 
307
 * range, since it reduces the size of your index array and can help
 
308
 * reduce the demand on memory bandwidth.
 
309
 *
 
310
 * Note that %COGL_INDICES_TYPE_UNSIGNED_INT is only supported if the
 
311
 * %COGL_FEATURE_UNSIGNED_INT_INDICES feature is available. This
 
312
 * should always be available on OpenGL but on OpenGL ES it will only
 
313
 * be available if the GL_OES_element_index_uint extension is
 
314
 * advertized.
 
315
 */
 
316
typedef enum {
 
317
  COGL_INDICES_TYPE_UNSIGNED_BYTE,
 
318
  COGL_INDICES_TYPE_UNSIGNED_SHORT,
 
319
  COGL_INDICES_TYPE_UNSIGNED_INT,
 
320
} CoglIndicesType;
 
321
 
 
322
/**
 
323
 * cogl_vertex_buffer_indices_new:
 
324
 * @indices_type: a #CoglIndicesType specifying the data type used for
 
325
 *    the indices.
 
326
 * @indices_array: (array length=indices_len): Specifies the address of
 
327
 *   your array of indices
 
328
 * @indices_len: The number of indices in indices_array
 
329
 *
 
330
 * Depending on how much geometry you are submitting it can be worthwhile
 
331
 * optimizing the number of redundant vertices you submit. Using an index
 
332
 * array allows you to reference vertices multiple times, for example
 
333
 * during triangle strips.
 
334
 *
 
335
 * Return value: A CoglHandle for the indices which you can pass to
 
336
 *   cogl_vertex_buffer_draw_elements().
 
337
 */
 
338
CoglHandle
 
339
cogl_vertex_buffer_indices_new (CoglIndicesType  indices_type,
 
340
                                const void      *indices_array,
 
341
                                int              indices_len);
 
342
 
 
343
/**
 
344
 * cogl_vertex_buffer_indices_get_type:
 
345
 * @handle: An indices handle
 
346
 *
 
347
 * Queries back the data type used for the given indices
 
348
 *
 
349
 * Returns: The CoglIndicesType used
 
350
 */
 
351
CoglIndicesType
 
352
cogl_vertex_buffer_indices_get_type (CoglHandle indices);
 
353
 
 
354
/**
 
355
 * cogl_vertex_buffer_draw_elements:
 
356
 * @handle: A vertex buffer handle
 
357
 * @mode: A #CoglVerticesMode specifying how the vertices should be
 
358
 *    interpreted.
 
359
 * @indices: A CoglHandle for a set of indices allocated via
 
360
 *    cogl_vertex_buffer_indices_new ()
 
361
 * @min_index: Specifies the minimum vertex index contained in indices
 
362
 * @max_index: Specifies the maximum vertex index contained in indices
 
363
 * @indices_offset: An offset into named indices. The offset marks the first
 
364
 *    index to use for drawing.
 
365
 * @count: Specifies the number of vertices you want to draw.
 
366
 *
 
367
 * This function lets you use an array of indices to specify the vertices
 
368
 * within your vertex buffer that you want to draw. The indices themselves
 
369
 * are created by calling cogl_vertex_buffer_indices_new ()
 
370
 *
 
371
 * Any un-submitted attribute changes are automatically submitted before
 
372
 * drawing.
 
373
 */
 
374
void
 
375
cogl_vertex_buffer_draw_elements (CoglHandle       handle,
 
376
                                  CoglVerticesMode mode,
 
377
                                  CoglHandle       indices,
 
378
                                  int              min_index,
 
379
                                  int              max_index,
 
380
                                  int              indices_offset,
 
381
                                  int              count);
 
382
 
 
383
#ifndef COGL_DISABLE_DEPRECATED
 
384
 
 
385
/**
 
386
 * cogl_vertex_buffer_ref:
 
387
 * @handle: a @CoglHandle.
 
388
 *
 
389
 * Increment the reference count for a vertex buffer
 
390
 *
 
391
 * Return value: the @handle.
 
392
 *
 
393
 * Deprecated: 1.2: Use cogl_handle_ref() instead
 
394
 */
 
395
CoglHandle
 
396
cogl_vertex_buffer_ref (CoglHandle handle) G_GNUC_DEPRECATED;
 
397
 
 
398
/**
 
399
 * cogl_vertex_buffer_unref:
 
400
 * @handle: a @CoglHandle.
 
401
 *
 
402
 * Decrement the reference count for a vertex buffer
 
403
 *
 
404
 * Deprecated: 1.2: Use cogl_handle_unref() instead
 
405
 */
 
406
void
 
407
cogl_vertex_buffer_unref (CoglHandle handle) G_GNUC_DEPRECATED;
 
408
 
 
409
#endif /* COGL_DISABLE_DEPRECATED */
 
410
 
 
411
/**
 
412
 * cogl_vertex_buffer_indices_get_for_quads:
 
413
 * @n_indices: the number of indices in the vertex buffer.
 
414
 *
 
415
 * Creates a vertex buffer containing the indices needed to draw pairs
 
416
 * of triangles from a list of vertices grouped as quads. There will
 
417
 * be at least @n_indices entries in the buffer (but there may be
 
418
 * more).
 
419
 *
 
420
 * The indices will follow this pattern:
 
421
 *
 
422
 * 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7 ... etc
 
423
 *
 
424
 * For example, if you submit vertices for a quad like this:
 
425
 *
 
426
 * |[
 
427
 *    0        3
 
428
 *     ########
 
429
 *     #      #
 
430
 *     #      #
 
431
 *     ########
 
432
 *    1        2
 
433
 * ]|
 
434
 *
 
435
 * Then you can request 6 indices to render two triangles like this:
 
436
 *
 
437
 * |[
 
438
 *    0           0        3
 
439
 *     ##          ########
 
440
 *     # ##          ##   #
 
441
 *     #   ##          ## #
 
442
 *     ########          ##
 
443
 *    1        2           2
 
444
 * ]|
 
445
 *
 
446
 * Returns: A %CoglHandle containing the indices. The handled is
 
447
 * owned by Cogl and should not be modified or unref'd.
 
448
 */
 
449
CoglHandle
 
450
cogl_vertex_buffer_indices_get_for_quads (unsigned int n_indices);
 
451
 
 
452
/**
 
453
 * cogl_is_vertex_buffer:
 
454
 * @handle: a #CoglHandle for a vertex buffer object
 
455
 *
 
456
 * Checks whether @handle is a Vertex Buffer Object
 
457
 *
 
458
 * Return value: %TRUE if the handle is a VBO, and %FALSE
 
459
 *   otherwise
 
460
 *
 
461
 * Since: 1.0
 
462
 */
 
463
gboolean
 
464
cogl_is_vertex_buffer (CoglHandle handle);
 
465
 
 
466
G_END_DECLS
 
467
 
 
468
#endif /* __COGL_VERTEX_BUFFER_H__ */