2009-01-20 16:12:44 -05:00
|
|
|
/*
|
2009-04-27 10:48:12 -04:00
|
|
|
* Cogl
|
2009-01-20 16:12:44 -05:00
|
|
|
*
|
2009-04-27 10:48:12 -04:00
|
|
|
* An object oriented GL/GLES Abstraction/Utility Layer
|
2009-01-20 16:12:44 -05:00
|
|
|
*
|
2009-04-27 10:48:12 -04:00
|
|
|
* Copyright (C) 2008,2009 Intel Corporation.
|
2008-11-10 13:53:14 -05:00
|
|
|
*
|
|
|
|
* 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
|
2010-03-01 07:56:10 -05:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*
|
2009-04-27 10:48:12 -04:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Robert Bragg <robert@linux.intel.com>
|
2008-11-10 13:53:14 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* XXX: For an overview of the functionality implemented here, please
|
2009-01-20 16:12:44 -05:00
|
|
|
* see cogl-vertex-buffer.h, which contains the gtk-doc section overview
|
|
|
|
* for the Vertex Buffers API.
|
2008-11-10 13:53:14 -05:00
|
|
|
*/
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
/*
|
2008-11-10 13:53:14 -05:00
|
|
|
* TODO: We need to do a better job of minimizing when we call glVertexPointer
|
2009-02-06 11:10:28 -05:00
|
|
|
* and pals in enable_state_for_drawing_buffer
|
2009-01-20 16:12:44 -05:00
|
|
|
*
|
2008-11-10 13:53:14 -05:00
|
|
|
* We should have an internal 2-tuple cache of (VBO, offset) for each of them
|
|
|
|
* so we can avoid some GL calls. We could have cogl wrappers for the
|
|
|
|
* gl*Pointer funcs that look like this:
|
|
|
|
*
|
|
|
|
* cogl_vertex_pointer (n_components, gl_type, stride, vbo, offset);
|
|
|
|
* cogl_color_pointer (n_components, gl_type, stride, vbo, offset);
|
|
|
|
*
|
|
|
|
* They would also accept NULL for the VBO handle to support old style vertex
|
|
|
|
* arrays.
|
|
|
|
*
|
|
|
|
* TODO:
|
2009-01-20 16:12:44 -05:00
|
|
|
* Actually hook this up to the cogl shaders infrastructure. The vertex
|
|
|
|
* buffer API has been designed to allow adding of arbitrary attributes for use
|
|
|
|
* with shaders, but this has yet to be actually plumbed together and tested.
|
2008-11-10 13:53:14 -05:00
|
|
|
* The bits we are missing:
|
|
|
|
* - cogl_program_use doesn't currently record within ctx-> which program
|
|
|
|
* is currently in use so a.t.m only Clutter knows the current shader.
|
|
|
|
* - We don't query the current shader program for the generic vertex indices
|
|
|
|
* (using glGetAttribLocation) so that we can call glEnableVertexAttribArray
|
|
|
|
* with those indices.
|
|
|
|
* (currently we just make up consecutive indices)
|
2009-01-20 16:12:44 -05:00
|
|
|
* - some dirty flag mechanims to know when the shader program has changed
|
|
|
|
* so we don't need to re-query it each time we draw a buffer.
|
|
|
|
*
|
2008-11-10 13:53:14 -05:00
|
|
|
* TODO:
|
2009-01-20 16:12:44 -05:00
|
|
|
* There is currently no API for querying back info about a buffer, E.g.:
|
|
|
|
* cogl_vertex_buffer_get_n_vertices (buffer_handle);
|
|
|
|
* cogl_vertex_buffer_get_n_components (buffer_handle, "attrib_name");
|
|
|
|
* cogl_vertex_buffer_get_stride (buffer_handle, "attrib_name");
|
|
|
|
* cogl_vertex_buffer_get_normalized (buffer_handle, "attrib_name");
|
|
|
|
* cogl_vertex_buffer_map (buffer_handle, "attrib_name");
|
|
|
|
* cogl_vertex_buffer_unmap (buffer_handle, "attrib_name");
|
|
|
|
* (Realistically I wouldn't expect anyone to use such an API to examine the
|
|
|
|
* contents of a buffer for modification, since you'd need to handle too many
|
2008-11-10 13:53:14 -05:00
|
|
|
* possibilities, but never the less there might be other value in these.)
|
|
|
|
|
|
|
|
* TODO:
|
|
|
|
* It may be worth exposing the underlying VBOs for some advanced use
|
|
|
|
* cases, e.g.:
|
|
|
|
* handle = cogl_vbo_new (COGL_VBO_FLAG_STATIC);
|
|
|
|
* pointer = cogl_vbo_map (handle, COGL_VBO_FLAG_WRITEONLY);
|
|
|
|
* cogl_vbo_unmap (handle);
|
|
|
|
* cogl_vbo_set_data (handle, size, data);
|
|
|
|
* cogl_vbo_set_sub_data (handle, offset, size, data);
|
|
|
|
* cogl_vbo_set_usage_hint (COGL_VBO_FLAG_DYNAMIC);
|
|
|
|
*
|
|
|
|
* TODO:
|
2009-01-20 16:12:44 -05:00
|
|
|
* Experiment with wider use of the vertex buffers API internally to Cogl.
|
2008-11-10 13:53:14 -05:00
|
|
|
* - There is potential, I think, for this API to become a work-horse API
|
|
|
|
* within COGL for submitting geometry to the GPU, and could unify some of
|
|
|
|
* the GL/GLES code paths.
|
|
|
|
* E.g.:
|
2009-01-20 16:12:44 -05:00
|
|
|
* - Try creating a per-context vertex buffer cache for cogl_texture_rectangle
|
|
|
|
* to sit on top of.
|
|
|
|
* - Try saving the tesselation of paths/polygons into vertex buffers
|
|
|
|
* internally.
|
2008-11-10 13:53:14 -05:00
|
|
|
*
|
|
|
|
* TODO
|
2009-01-20 16:12:44 -05:00
|
|
|
* Expose API that lets developers get back a buffer handle for a particular
|
2008-11-10 13:53:14 -05:00
|
|
|
* polygon so they may add custom attributes to them.
|
2009-01-20 16:12:44 -05:00
|
|
|
* - It should be possible to query/modify attributes efficiently, in place,
|
2008-11-10 13:53:14 -05:00
|
|
|
* avoiding copies. It would not be acceptable to simply require that
|
2009-01-20 16:12:44 -05:00
|
|
|
* developers must query back the n_vertices of a buffer and then the
|
|
|
|
* n_components, type and stride etc of each attribute since there
|
2008-11-10 13:53:14 -05:00
|
|
|
* would be too many combinations to realistically handle.
|
2009-01-20 16:12:44 -05:00
|
|
|
*
|
2008-11-10 13:53:14 -05:00
|
|
|
* - In practice, some cases might be best solved with a higher level
|
|
|
|
* EditableMesh API, (see futher below) but for many cases I think an
|
|
|
|
* API like this might be appropriate:
|
|
|
|
*
|
2009-01-20 16:12:44 -05:00
|
|
|
* cogl_vertex_buffer_foreach_vertex (buffer_handle,
|
|
|
|
* (AttributesBufferIteratorFunc)callback,
|
|
|
|
* "gl_Vertex", "gl_Color", NULL);
|
|
|
|
* static void callback (CoglVertexBufferVertex *vert)
|
2008-11-10 13:53:14 -05:00
|
|
|
* {
|
|
|
|
* GLfloat *pos = vert->attrib[0];
|
|
|
|
* GLubyte *color = vert->attrib[1];
|
|
|
|
* GLfloat *new_attrib = buf[vert->index];
|
2009-01-20 16:12:44 -05:00
|
|
|
*
|
2008-11-10 13:53:14 -05:00
|
|
|
* new_attrib = pos*color;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* TODO
|
2009-01-20 16:12:44 -05:00
|
|
|
* Think about a higher level Mesh API for building/modifying attribute buffers
|
|
|
|
* - E.g. look at Blender for inspiration here. They can build a mesh from
|
|
|
|
* "MVert", "MFace" and "MEdge" primitives.
|
2008-11-10 13:53:14 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2009-05-20 10:15:25 -04:00
|
|
|
#include <glib.h>
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
#include "cogl.h"
|
|
|
|
#include "cogl-internal.h"
|
|
|
|
#include "cogl-util.h"
|
|
|
|
#include "cogl-context.h"
|
|
|
|
#include "cogl-handle.h"
|
2009-01-20 16:12:44 -05:00
|
|
|
#include "cogl-vertex-buffer-private.h"
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
#include "cogl-texture-private.h"
|
2009-05-23 12:42:10 -04:00
|
|
|
#include "cogl-material-private.h"
|
2010-07-09 22:40:47 -04:00
|
|
|
#include "cogl-material-opengl-private.h"
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
#include "cogl-primitives.h"
|
2009-11-26 14:06:35 -05:00
|
|
|
#include "cogl-framebuffer-private.h"
|
2010-02-10 13:18:30 -05:00
|
|
|
#include "cogl-journal-private.h"
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
#define PAD_FOR_ALIGNMENT(VAR, TYPE_SIZE) \
|
|
|
|
(VAR = TYPE_SIZE + ((VAR - 1) & ~(TYPE_SIZE - 1)))
|
|
|
|
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
/*
|
2008-11-10 13:53:14 -05:00
|
|
|
* GL/GLES compatability defines for VBO thingies:
|
|
|
|
*/
|
|
|
|
|
2008-11-13 09:28:16 -05:00
|
|
|
#if defined (HAVE_COGL_GL)
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-11-17 08:52:40 -05:00
|
|
|
#define glGenBuffers ctx->drv.pf_glGenBuffers
|
|
|
|
#define glBindBuffer ctx->drv.pf_glBindBuffer
|
|
|
|
#define glBufferData ctx->drv.pf_glBufferData
|
|
|
|
#define glBufferSubData ctx->drv.pf_glBufferSubData
|
|
|
|
#define glGetBufferSubData ctx->drv.pf_glGetBufferSubData
|
|
|
|
#define glDeleteBuffers ctx->drv.pf_glDeleteBuffers
|
|
|
|
#define glMapBuffer ctx->drv.pf_glMapBuffer
|
|
|
|
#define glUnmapBuffer ctx->drv.pf_glUnmapBuffer
|
2009-07-27 20:34:33 -04:00
|
|
|
#define glClientActiveTexture ctx->drv.pf_glClientActiveTexture
|
2008-11-10 13:53:14 -05:00
|
|
|
#ifndef GL_ARRAY_BUFFER
|
|
|
|
#define GL_ARRAY_BUFFER GL_ARRAY_BUFFER_ARB
|
|
|
|
#endif
|
|
|
|
|
2008-11-13 09:28:16 -05:00
|
|
|
#elif defined (HAVE_COGL_GLES2)
|
|
|
|
|
2009-01-29 08:40:37 -05:00
|
|
|
#include "../gles/cogl-gles2-wrapper.h"
|
2008-11-13 09:28:16 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
#endif
|
|
|
|
|
2010-02-23 09:45:44 -05:00
|
|
|
/* This isn't defined in the GLES headers */
|
|
|
|
#ifndef GL_UNSIGNED_INT
|
|
|
|
#define GL_UNSIGNED_INT 0x1405
|
|
|
|
#endif
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
/*
|
2008-11-10 13:53:14 -05:00
|
|
|
* GL/GLES compatability defines for shader things:
|
|
|
|
*/
|
|
|
|
|
2008-11-13 09:28:16 -05:00
|
|
|
#if defined (HAVE_COGL_GL)
|
|
|
|
|
2009-11-17 08:52:40 -05:00
|
|
|
#define glVertexAttribPointer ctx->drv.pf_glVertexAttribPointer
|
|
|
|
#define glEnableVertexAttribArray ctx->drv.pf_glEnableVertexAttribArray
|
2010-07-06 11:08:06 -04:00
|
|
|
#define glDisableVertexAttribArray ctx->drv.pf_glDisableVertexAttribArray
|
2008-11-10 13:53:14 -05:00
|
|
|
#define MAY_HAVE_PROGRAMABLE_GL
|
|
|
|
|
2008-11-13 09:28:16 -05:00
|
|
|
#elif defined (HAVE_COGL_GLES2)
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* NB: GLES2 had shaders in core since day one so again we don't need
|
|
|
|
* defines in this case: */
|
|
|
|
#define MAY_HAVE_PROGRAMABLE_GL
|
2008-11-13 09:28:16 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_COGL_GL
|
2008-11-21 11:18:58 -05:00
|
|
|
|
2008-11-13 09:28:16 -05:00
|
|
|
/* GLES doesn't have glDrawRangeElements, so we simply pretend it does
|
2008-11-10 13:53:14 -05:00
|
|
|
* but that it makes no use of the start, end constraints: */
|
|
|
|
#define glDrawRangeElements(mode, start, end, count, type, indices) \
|
|
|
|
glDrawElements (mode, count, type, indices)
|
2008-11-21 11:18:58 -05:00
|
|
|
|
|
|
|
#else /* HAVE_COGL_GL */
|
|
|
|
|
|
|
|
#define glDrawRangeElements(mode, start, end, count, type, indices) \
|
2009-07-27 20:34:33 -04:00
|
|
|
ctx->drv.pf_glDrawRangeElements (mode, start, end, count, type, indices)
|
2008-11-21 11:18:58 -05:00
|
|
|
|
|
|
|
#endif /* HAVE_COGL_GL */
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
static void _cogl_vertex_buffer_free (CoglVertexBuffer *buffer);
|
2009-05-28 07:49:29 -04:00
|
|
|
static void _cogl_vertex_buffer_indices_free (CoglVertexBufferIndices *buffer_indices);
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-04-01 12:16:44 -04:00
|
|
|
COGL_HANDLE_DEFINE (VertexBuffer, vertex_buffer);
|
2010-07-09 12:59:16 -04:00
|
|
|
COGL_OBJECT_DEFINE_DEPRECATED_REF_COUNTING (vertex_buffer);
|
2009-05-28 07:49:29 -04:00
|
|
|
COGL_HANDLE_DEFINE (VertexBufferIndices, vertex_buffer_indices);
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
CoglHandle
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
cogl_vertex_buffer_new (unsigned int n_vertices)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBuffer *buffer = g_slice_alloc (sizeof (CoglVertexBuffer));
|
|
|
|
|
|
|
|
buffer->n_vertices = n_vertices;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
buffer->submitted_vbos = NULL;
|
|
|
|
buffer->new_attributes = NULL;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
/* return COGL_INVALID_HANDLE; */
|
2009-01-20 16:12:44 -05:00
|
|
|
return _cogl_vertex_buffer_handle_new (buffer);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
unsigned int
|
2009-02-28 12:39:38 -05:00
|
|
|
cogl_vertex_buffer_get_n_vertices (CoglHandle handle)
|
|
|
|
{
|
|
|
|
CoglVertexBuffer *buffer;
|
|
|
|
|
|
|
|
if (!cogl_is_vertex_buffer (handle))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
buffer = _cogl_vertex_buffer_pointer_from_handle (handle);
|
|
|
|
|
|
|
|
return buffer->n_vertices;
|
|
|
|
}
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* There are a number of standard OpenGL attributes that we deal with
|
|
|
|
* specially. These attributes are all namespaced with a "gl_" prefix
|
|
|
|
* so we should catch any typos instead of silently adding a custom
|
|
|
|
* attribute.
|
|
|
|
*/
|
2009-01-20 16:12:44 -05:00
|
|
|
static CoglVertexBufferAttribFlags
|
2008-11-10 13:53:14 -05:00
|
|
|
validate_gl_attribute (const char *gl_attribute,
|
2009-09-24 12:34:26 -04:00
|
|
|
guint8 n_components,
|
2008-11-10 13:53:14 -05:00
|
|
|
guint8 *texture_unit)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttribFlags type;
|
2008-11-10 13:53:14 -05:00
|
|
|
char *detail_seperator = NULL;
|
|
|
|
int name_len;
|
|
|
|
|
|
|
|
detail_seperator = strstr (gl_attribute, "::");
|
|
|
|
if (detail_seperator)
|
|
|
|
name_len = detail_seperator - gl_attribute;
|
|
|
|
else
|
|
|
|
name_len = strlen (gl_attribute);
|
|
|
|
|
|
|
|
if (strncmp (gl_attribute, "Vertex", name_len) == 0)
|
|
|
|
{
|
2009-09-24 12:34:26 -04:00
|
|
|
if (G_UNLIKELY (n_components == 1))
|
|
|
|
g_critical ("glVertexPointer doesn't allow 1 component vertex "
|
|
|
|
"positions so we currently only support \"gl_Vertex\" "
|
|
|
|
"attributes where n_components == 2, 3 or 4");
|
2009-01-20 16:12:44 -05:00
|
|
|
type = COGL_VERTEX_BUFFER_ATTRIB_FLAG_VERTEX_ARRAY;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
else if (strncmp (gl_attribute, "Color", name_len) == 0)
|
|
|
|
{
|
2009-09-24 12:34:26 -04:00
|
|
|
if (G_UNLIKELY (n_components != 3 && n_components != 4))
|
|
|
|
g_critical ("glColorPointer expects 3 or 4 component colors so we "
|
|
|
|
"currently only support \"gl_Color\" attributes where "
|
|
|
|
"n_components == 3 or 4");
|
2009-01-20 16:12:44 -05:00
|
|
|
type = COGL_VERTEX_BUFFER_ATTRIB_FLAG_COLOR_ARRAY;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
else if (strncmp (gl_attribute,
|
|
|
|
"MultiTexCoord",
|
|
|
|
strlen ("MultiTexCoord")) == 0)
|
|
|
|
{
|
|
|
|
unsigned int unit;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
if (sscanf (gl_attribute, "MultiTexCoord%u", &unit) != 1)
|
|
|
|
{
|
|
|
|
g_warning ("gl_MultiTexCoord attributes should include a\n"
|
|
|
|
"texture unit number, E.g. gl_MultiTexCoord0\n");
|
|
|
|
unit = 0;
|
|
|
|
}
|
|
|
|
/* FIXME: validate any '::' delimiter for this case */
|
|
|
|
*texture_unit = unit;
|
2009-01-20 16:12:44 -05:00
|
|
|
type = COGL_VERTEX_BUFFER_ATTRIB_FLAG_TEXTURE_COORD_ARRAY;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
else if (strncmp (gl_attribute, "Normal", name_len) == 0)
|
|
|
|
{
|
2009-09-24 12:34:26 -04:00
|
|
|
if (G_UNLIKELY (n_components != 3))
|
|
|
|
g_critical ("glNormalPointer expects 3 component normals so we "
|
|
|
|
"currently only support \"gl_Normal\" attributes where "
|
|
|
|
"n_components == 3");
|
2009-01-20 16:12:44 -05:00
|
|
|
type = COGL_VERTEX_BUFFER_ATTRIB_FLAG_NORMAL_ARRAY;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_warning ("Unknown gl_* attribute name gl_%s\n", gl_attribute);
|
2009-01-20 16:12:44 -05:00
|
|
|
type = COGL_VERTEX_BUFFER_ATTRIB_FLAG_INVALID;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This validates that a custom attribute name is a valid GLSL variable name
|
|
|
|
*
|
|
|
|
* NB: attribute names may have a detail component delimited using '::' E.g.
|
2009-01-20 16:12:44 -05:00
|
|
|
* custom_attrib::foo or custom_attrib::bar
|
2008-11-10 13:53:14 -05:00
|
|
|
*
|
|
|
|
* maybe I should hang a compiled regex somewhere to handle this
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
validate_custom_attribute_name (const char *attribute_name)
|
|
|
|
{
|
|
|
|
char *detail_seperator = NULL;
|
|
|
|
int name_len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
detail_seperator = strstr (attribute_name, "::");
|
|
|
|
if (detail_seperator)
|
|
|
|
name_len = detail_seperator - attribute_name;
|
|
|
|
else
|
|
|
|
name_len = strlen (attribute_name);
|
|
|
|
|
2008-11-18 11:24:31 -05:00
|
|
|
if (name_len == 0
|
|
|
|
|| !g_ascii_isalpha (attribute_name[0])
|
|
|
|
|| attribute_name[0] != '_')
|
2008-11-10 13:53:14 -05:00
|
|
|
return FALSE;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
for (i = 1; i < name_len; i++)
|
2008-11-18 11:24:31 -05:00
|
|
|
if (!g_ascii_isalnum (attribute_name[i]) || attribute_name[i] != '_')
|
2008-11-10 13:53:14 -05:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
/* Iterates the CoglVertexBufferVBOs of a buffer and creates a flat list
|
|
|
|
* of all the submitted attributes
|
2008-11-10 13:53:14 -05:00
|
|
|
*
|
2009-01-20 16:12:44 -05:00
|
|
|
* Note: The CoglVertexBufferAttrib structs are deep copied.
|
2008-11-10 13:53:14 -05:00
|
|
|
*/
|
|
|
|
static GList *
|
2009-01-20 16:12:44 -05:00
|
|
|
copy_submitted_attributes_list (CoglVertexBuffer *buffer)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
GList *submitted_attributes = NULL;
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
for (tmp = buffer->submitted_vbos; tmp != NULL; tmp = tmp->next)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *cogl_vbo = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
GList *tmp2;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
for (tmp2 = cogl_vbo->attributes; tmp2 != NULL; tmp2 = tmp2->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp2->data;
|
|
|
|
CoglVertexBufferAttrib *copy =
|
|
|
|
g_slice_alloc (sizeof (CoglVertexBufferAttrib));
|
2008-11-10 13:53:14 -05:00
|
|
|
*copy = *attribute;
|
|
|
|
submitted_attributes = g_list_prepend (submitted_attributes, copy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return submitted_attributes;
|
|
|
|
}
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
static CoglVertexBufferAttribFlags
|
2008-11-10 13:53:14 -05:00
|
|
|
get_attribute_gl_type_flag_from_gl_type (GLenum gl_type)
|
|
|
|
{
|
|
|
|
switch (gl_type)
|
|
|
|
{
|
|
|
|
case GL_BYTE:
|
2009-01-20 16:12:44 -05:00
|
|
|
return COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_BYTE;
|
2008-11-10 13:53:14 -05:00
|
|
|
case GL_UNSIGNED_BYTE:
|
2009-01-20 16:12:44 -05:00
|
|
|
return COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_UNSIGNED_BYTE;
|
2008-11-10 13:53:14 -05:00
|
|
|
case GL_SHORT:
|
2009-01-20 16:12:44 -05:00
|
|
|
return COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_SHORT;
|
2008-11-10 13:53:14 -05:00
|
|
|
case GL_UNSIGNED_SHORT:
|
2009-01-20 16:12:44 -05:00
|
|
|
return COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_UNSIGNED_SHORT;
|
2008-11-10 13:53:14 -05:00
|
|
|
case GL_FLOAT:
|
2009-01-20 16:12:44 -05:00
|
|
|
return COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_FLOAT;
|
2008-11-10 13:53:14 -05:00
|
|
|
#if HAVE_COGL_GL
|
|
|
|
case GL_INT:
|
2009-01-20 16:12:44 -05:00
|
|
|
return COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_INT;
|
2008-11-10 13:53:14 -05:00
|
|
|
case GL_UNSIGNED_INT:
|
2009-01-20 16:12:44 -05:00
|
|
|
return COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_UNSIGNED_INT;
|
2008-11-10 13:53:14 -05:00
|
|
|
case GL_DOUBLE:
|
2009-01-20 16:12:44 -05:00
|
|
|
return COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_DOUBLE;
|
2008-11-10 13:53:14 -05:00
|
|
|
#endif
|
|
|
|
default:
|
2009-01-20 16:12:44 -05:00
|
|
|
g_warning ("Attribute Buffers API: "
|
|
|
|
"Unrecognised OpenGL type enum 0x%08x\n", gl_type);
|
2008-11-10 13:53:14 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gsize
|
2009-01-20 16:12:44 -05:00
|
|
|
get_gl_type_size (CoglVertexBufferAttribFlags flags)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttribFlags gl_type =
|
|
|
|
flags & COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_MASK;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
switch (gl_type)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_BYTE:
|
2008-11-10 13:53:14 -05:00
|
|
|
return sizeof (GLbyte);
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_UNSIGNED_BYTE:
|
2008-11-10 13:53:14 -05:00
|
|
|
return sizeof (GLubyte);
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_SHORT:
|
2008-11-10 13:53:14 -05:00
|
|
|
return sizeof (GLshort);
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_UNSIGNED_SHORT:
|
2008-11-10 13:53:14 -05:00
|
|
|
return sizeof (GLushort);
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_FLOAT:
|
2008-11-10 13:53:14 -05:00
|
|
|
return sizeof (GLfloat);
|
|
|
|
#if HAVE_COGL_GL
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_INT:
|
2008-11-10 13:53:14 -05:00
|
|
|
return sizeof (GLint);
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_UNSIGNED_INT:
|
2008-11-10 13:53:14 -05:00
|
|
|
return sizeof (GLuint);
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_DOUBLE:
|
2008-11-10 13:53:14 -05:00
|
|
|
return sizeof (GLdouble);
|
|
|
|
#endif
|
|
|
|
default:
|
2009-02-06 11:10:28 -05:00
|
|
|
g_warning ("Vertex Buffer API: Unrecognised OpenGL type enum 0x%08x\n", gl_type);
|
2008-11-10 13:53:14 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-24 06:33:29 -04:00
|
|
|
cogl_vertex_buffer_add (CoglHandle handle,
|
|
|
|
const char *attribute_name,
|
|
|
|
guint8 n_components,
|
|
|
|
CoglAttributeType type,
|
|
|
|
gboolean normalized,
|
|
|
|
guint16 stride,
|
|
|
|
const void *pointer)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBuffer *buffer;
|
2008-11-10 13:53:14 -05:00
|
|
|
GQuark name_quark = g_quark_from_string (attribute_name);
|
|
|
|
gboolean modifying_an_attrib = FALSE;
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute;
|
|
|
|
CoglVertexBufferAttribFlags flags = 0;
|
2008-11-10 13:53:14 -05:00
|
|
|
guint8 texture_unit = 0;
|
|
|
|
GList *tmp;
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
if (!cogl_is_vertex_buffer (handle))
|
2008-11-10 13:53:14 -05:00
|
|
|
return;
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
buffer = _cogl_vertex_buffer_pointer_from_handle (handle);
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
/* The submit function works by diffing between submitted_attributes
|
|
|
|
* and new_attributes to minimize the upload bandwidth + cost of
|
|
|
|
* allocating new VBOs, so if there isn't already a list of new_attributes
|
|
|
|
* we create one: */
|
2009-01-20 16:12:44 -05:00
|
|
|
if (!buffer->new_attributes)
|
|
|
|
buffer->new_attributes = copy_submitted_attributes_list (buffer);
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* Note: we first look for an existing attribute that we are modifying
|
|
|
|
* so we may skip needing to validate the name */
|
2009-01-20 16:12:44 -05:00
|
|
|
for (tmp = buffer->new_attributes; tmp != NULL; tmp = tmp->next)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *submitted_attribute = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
if (submitted_attribute->name == name_quark)
|
|
|
|
{
|
|
|
|
modifying_an_attrib = TRUE;
|
|
|
|
|
|
|
|
attribute = submitted_attribute;
|
|
|
|
|
|
|
|
/* since we will skip validate_gl_attribute in this case, we need
|
|
|
|
* to pluck out the attribute type before overwriting the flags: */
|
2009-01-20 16:12:44 -05:00
|
|
|
flags |=
|
|
|
|
attribute->flags & COGL_VERTEX_BUFFER_ATTRIB_FLAG_TYPE_MASK;
|
2008-11-10 13:53:14 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!modifying_an_attrib)
|
|
|
|
{
|
|
|
|
/* Validate the attribute name, is suitable as a variable name */
|
|
|
|
if (strncmp (attribute_name, "gl_", 3) == 0)
|
|
|
|
{
|
|
|
|
flags |= validate_gl_attribute (attribute_name + 3,
|
2009-09-24 12:34:26 -04:00
|
|
|
n_components,
|
|
|
|
&texture_unit);
|
2009-01-20 16:12:44 -05:00
|
|
|
if (flags & COGL_VERTEX_BUFFER_ATTRIB_FLAG_INVALID)
|
2008-11-10 13:53:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_CUSTOM_ARRAY;
|
2008-11-10 13:53:14 -05:00
|
|
|
if (validate_custom_attribute_name (attribute_name))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute = g_slice_alloc (sizeof (CoglVertexBufferAttrib));
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
attribute->name = g_quark_from_string (attribute_name);
|
|
|
|
attribute->n_components = n_components;
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute->stride = buffer->n_vertices > 1 ? stride : 0;
|
2008-11-10 13:53:14 -05:00
|
|
|
attribute->u.pointer = pointer;
|
|
|
|
attribute->texture_unit = texture_unit;
|
|
|
|
|
2009-05-24 06:33:29 -04:00
|
|
|
flags |= get_attribute_gl_type_flag_from_gl_type (type);
|
2009-01-20 16:12:44 -05:00
|
|
|
flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_ENABLED;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
/* Note: We currently just assume, if an attribute is *ever* updated
|
|
|
|
* then it should be taged as frequently changing. */
|
|
|
|
if (modifying_an_attrib)
|
2009-01-20 16:12:44 -05:00
|
|
|
flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_FREQUENT_RESUBMIT;
|
2008-11-10 13:53:14 -05:00
|
|
|
else
|
2009-01-20 16:12:44 -05:00
|
|
|
flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_INFREQUENT_RESUBMIT;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
if (normalized)
|
2009-01-20 16:12:44 -05:00
|
|
|
flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_NORMALIZED;
|
2008-11-10 13:53:14 -05:00
|
|
|
attribute->flags = flags;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* NB: get_gl_type_size must be called after setting the type
|
|
|
|
* flags, above. */
|
|
|
|
if (attribute->stride)
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute->span_bytes = buffer->n_vertices * attribute->stride;
|
2008-11-10 13:53:14 -05:00
|
|
|
else
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute->span_bytes = buffer->n_vertices
|
2008-11-10 13:53:14 -05:00
|
|
|
* attribute->n_components
|
|
|
|
* get_gl_type_size (attribute->flags);
|
|
|
|
|
|
|
|
if (!modifying_an_attrib)
|
2009-01-20 16:12:44 -05:00
|
|
|
buffer->new_attributes =
|
|
|
|
g_list_prepend (buffer->new_attributes, attribute);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vertex_buffer_delete (CoglHandle handle,
|
|
|
|
const char *attribute_name)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBuffer *buffer;
|
2008-11-10 13:53:14 -05:00
|
|
|
GQuark name = g_quark_from_string (attribute_name);
|
|
|
|
GList *tmp;
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
if (!cogl_is_vertex_buffer (handle))
|
2008-11-10 13:53:14 -05:00
|
|
|
return;
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
buffer = _cogl_vertex_buffer_pointer_from_handle (handle);
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
/* The submit function works by diffing between submitted_attributes
|
|
|
|
* and new_attributes to minimize the upload bandwidth + cost of
|
|
|
|
* allocating new VBOs, so if there isn't already a list of new_attributes
|
|
|
|
* we create one: */
|
2009-01-20 16:12:44 -05:00
|
|
|
if (!buffer->new_attributes)
|
|
|
|
buffer->new_attributes = copy_submitted_attributes_list (buffer);
|
|
|
|
|
|
|
|
for (tmp = buffer->new_attributes; tmp != NULL; tmp = tmp->next)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *submitted_attribute = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
if (submitted_attribute->name == name)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
buffer->new_attributes =
|
|
|
|
g_list_delete_link (buffer->new_attributes, tmp);
|
|
|
|
g_slice_free (CoglVertexBufferAttrib, submitted_attribute);
|
2008-11-10 13:53:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_warning ("Failed to find an attribute named %s to delete\n",
|
|
|
|
attribute_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_attribute_enable (CoglHandle handle,
|
|
|
|
const char *attribute_name,
|
|
|
|
gboolean state)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBuffer *buffer;
|
2008-11-10 13:53:14 -05:00
|
|
|
GQuark name_quark = g_quark_from_string (attribute_name);
|
|
|
|
GList *tmp;
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
if (!cogl_is_vertex_buffer (handle))
|
2008-11-10 13:53:14 -05:00
|
|
|
return;
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
buffer = _cogl_vertex_buffer_pointer_from_handle (handle);
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
/* NB: If a buffer is currently being edited, then there can be two seperate
|
|
|
|
* lists of attributes; those that are currently submitted and a new list yet
|
|
|
|
* to be submitted, we need to modify both. */
|
|
|
|
|
|
|
|
for (tmp = buffer->new_attributes; tmp != NULL; tmp = tmp->next)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
if (attribute->name == name_quark)
|
|
|
|
{
|
|
|
|
if (state)
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute->flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_ENABLED;
|
2008-11-10 13:53:14 -05:00
|
|
|
else
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute->flags &= ~COGL_VERTEX_BUFFER_ATTRIB_FLAG_ENABLED;
|
2008-11-10 13:53:14 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-01-20 16:12:44 -05:00
|
|
|
|
|
|
|
for (tmp = buffer->submitted_vbos; tmp != NULL; tmp = tmp->next)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *cogl_vbo = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
GList *tmp2;
|
|
|
|
|
|
|
|
for (tmp2 = cogl_vbo->attributes; tmp2 != NULL; tmp2 = tmp2->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp2->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
if (attribute->name == name_quark)
|
|
|
|
{
|
|
|
|
if (state)
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute->flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_ENABLED;
|
2008-11-10 13:53:14 -05:00
|
|
|
else
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute->flags &= ~COGL_VERTEX_BUFFER_ATTRIB_FLAG_ENABLED;
|
2008-11-10 13:53:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_warning ("Failed to find an attribute named %s to %s\n",
|
|
|
|
attribute_name,
|
|
|
|
state == TRUE ? "enable" : "disable");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vertex_buffer_enable (CoglHandle handle,
|
|
|
|
const char *attribute_name)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
set_attribute_enable (handle, attribute_name, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vertex_buffer_disable (CoglHandle handle,
|
|
|
|
const char *attribute_name)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
set_attribute_enable (handle, attribute_name, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vertex_buffer_attribute_free (CoglVertexBufferAttrib *attribute)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
g_slice_free (CoglVertexBufferAttrib, attribute);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Given an attribute that we know has already been submitted before, this
|
|
|
|
* function looks for the existing VBO that contains it.
|
|
|
|
*
|
|
|
|
* Note: It will free redundant attribute struct once the corresponding
|
|
|
|
* VBO has been found.
|
|
|
|
*/
|
|
|
|
static void
|
2009-01-20 16:12:44 -05:00
|
|
|
filter_already_submitted_attribute (CoglVertexBufferAttrib *attribute,
|
2008-11-10 13:53:14 -05:00
|
|
|
GList **reuse_vbos,
|
|
|
|
GList **submitted_vbos)
|
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
|
|
|
|
/* First check the cogl_vbos we already know are being reused since we
|
|
|
|
* are more likley to get a match here */
|
|
|
|
for (tmp = *reuse_vbos; tmp != NULL; tmp = tmp->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *cogl_vbo = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
GList *tmp2;
|
|
|
|
|
|
|
|
for (tmp2 = cogl_vbo->attributes; tmp2 != NULL; tmp2 = tmp2->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *vbo_attribute = tmp2->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
if (vbo_attribute->name == attribute->name)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
vbo_attribute->flags &=
|
|
|
|
~COGL_VERTEX_BUFFER_ATTRIB_FLAG_UNUSED;
|
2008-11-10 13:53:14 -05:00
|
|
|
/* Note: we don't free the redundant attribute here, since it
|
2009-01-20 16:12:44 -05:00
|
|
|
* will be freed after all filtering in
|
|
|
|
* cogl_vertex_buffer_submit */
|
2008-11-10 13:53:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
for (tmp = *submitted_vbos; tmp != NULL; tmp = tmp->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *cogl_vbo = tmp->data;
|
|
|
|
CoglVertexBufferAttrib *reuse_attribute = NULL;
|
2008-11-10 13:53:14 -05:00
|
|
|
GList *tmp2;
|
|
|
|
|
|
|
|
for (tmp2 = cogl_vbo->attributes; tmp2 != NULL; tmp2 = tmp2->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *vbo_attribute = tmp2->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
if (vbo_attribute->name == attribute->name)
|
|
|
|
{
|
|
|
|
reuse_attribute = vbo_attribute;
|
|
|
|
/* Note: we don't free the redundant attribute here, since it
|
2009-01-20 16:12:44 -05:00
|
|
|
* will be freed after all filtering in
|
|
|
|
* cogl_vertex_buffer_submit */
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
*submitted_vbos = g_list_remove_link (*submitted_vbos, tmp);
|
|
|
|
tmp->next = *reuse_vbos;
|
|
|
|
*reuse_vbos = tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reuse_attribute)
|
|
|
|
continue;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* Mark all but the matched attribute as UNUSED, so that when we
|
|
|
|
* finish filtering all our attributes any attrributes still
|
|
|
|
* marked as UNUSED can be removed from the their cogl_vbo */
|
|
|
|
for (tmp2 = cogl_vbo->attributes; tmp2 != NULL; tmp2 = tmp2->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *vbo_attribute = tmp2->data;
|
|
|
|
if (vbo_attribute != reuse_attribute)
|
|
|
|
vbo_attribute->flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_UNUSED;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_critical ("Failed to find the cogl vbo that corresponds to an\n"
|
|
|
|
"attribute that had apparently already been submitted!");
|
|
|
|
}
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
/* When we first mark a CoglVertexBufferVBO to be reused, we mark the
|
|
|
|
* attributes as unsed, so that when filtering of attributes into VBOs is done
|
2008-11-10 13:53:14 -05:00
|
|
|
* we can then prune the now unsed attributes. */
|
|
|
|
static void
|
2009-01-20 16:12:44 -05:00
|
|
|
remove_unused_attributes (CoglVertexBufferVBO *cogl_vbo)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
GList *next;
|
|
|
|
|
|
|
|
for (tmp = cogl_vbo->attributes; tmp != NULL; tmp = next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
next = tmp->next;
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
if (attribute->flags & COGL_VERTEX_BUFFER_ATTRIB_FLAG_UNUSED)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
cogl_vbo->attributes =
|
|
|
|
g_list_delete_link (cogl_vbo->attributes, tmp);
|
2009-01-20 16:12:44 -05:00
|
|
|
g_slice_free (CoglVertexBufferAttrib, attribute);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Give a newly added, strided, attribute, this function looks for a
|
2009-01-20 16:12:44 -05:00
|
|
|
* CoglVertexBufferVBO that the attribute is interleved with. If it can't
|
|
|
|
* find one then a new CoglVertexBufferVBO is allocated and added to the
|
|
|
|
* list of new_strided_vbos.
|
2008-11-10 13:53:14 -05:00
|
|
|
*/
|
|
|
|
static void
|
2009-01-20 16:12:44 -05:00
|
|
|
filter_strided_attribute (CoglVertexBufferAttrib *attribute,
|
2008-11-10 13:53:14 -05:00
|
|
|
GList **new_vbos)
|
|
|
|
{
|
|
|
|
GList *tmp;
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *new_cogl_vbo;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
for (tmp = *new_vbos; tmp != NULL; tmp = tmp->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *cogl_vbo = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
GList *tmp2;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2009-06-05 10:18:43 -04:00
|
|
|
if (!(cogl_vbo->flags & COGL_VERTEX_BUFFER_VBO_FLAG_STRIDED))
|
2008-11-10 13:53:14 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
for (tmp2 = cogl_vbo->attributes; tmp2 != NULL; tmp2 = tmp2->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *vbo_attribute = tmp2->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
const char *attribute_start = attribute->u.pointer;
|
|
|
|
const char *vbo_attribute_start = vbo_attribute->u.pointer;
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
/* NB: All attributes have buffer->n_vertices values which
|
2008-11-10 13:53:14 -05:00
|
|
|
* simplifies determining which attributes are interleved
|
|
|
|
* since we assume they will start no farther than +- a
|
|
|
|
* stride away from each other:
|
|
|
|
*/
|
|
|
|
if (attribute_start <= (vbo_attribute_start - vbo_attribute->stride)
|
|
|
|
|| attribute_start
|
|
|
|
>= (vbo_attribute_start + vbo_attribute->stride))
|
|
|
|
continue; /* Not interleved */
|
|
|
|
|
|
|
|
cogl_vbo->attributes =
|
|
|
|
g_list_prepend (cogl_vbo->attributes, attribute);
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
if (attribute->flags &
|
|
|
|
COGL_VERTEX_BUFFER_ATTRIB_FLAG_FREQUENT_RESUBMIT)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vbo->flags &=
|
|
|
|
~COGL_VERTEX_BUFFER_VBO_FLAG_INFREQUENT_RESUBMIT;
|
|
|
|
cogl_vbo->flags |=
|
|
|
|
COGL_VERTEX_BUFFER_VBO_FLAG_FREQUENT_RESUBMIT;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-01-20 16:12:44 -05:00
|
|
|
new_cogl_vbo = g_slice_alloc (sizeof (CoglVertexBufferVBO));
|
2010-05-27 03:28:29 -04:00
|
|
|
new_cogl_vbo->vbo_name = NULL;
|
2008-11-10 13:53:14 -05:00
|
|
|
new_cogl_vbo->attributes = NULL;
|
|
|
|
new_cogl_vbo->attributes =
|
|
|
|
g_list_prepend (new_cogl_vbo->attributes, attribute);
|
|
|
|
/* Any one of the interleved attributes will have the same span_bytes */
|
|
|
|
new_cogl_vbo->vbo_bytes = attribute->span_bytes;
|
2009-01-20 16:12:44 -05:00
|
|
|
new_cogl_vbo->flags = COGL_VERTEX_BUFFER_VBO_FLAG_STRIDED;
|
|
|
|
|
|
|
|
if (attribute->flags & COGL_VERTEX_BUFFER_ATTRIB_FLAG_INFREQUENT_RESUBMIT)
|
|
|
|
new_cogl_vbo->flags |= COGL_VERTEX_BUFFER_VBO_FLAG_INFREQUENT_RESUBMIT;
|
2008-11-10 13:53:14 -05:00
|
|
|
else
|
2009-01-20 16:12:44 -05:00
|
|
|
new_cogl_vbo->flags |= COGL_VERTEX_BUFFER_VBO_FLAG_FREQUENT_RESUBMIT;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
*new_vbos = g_list_prepend (*new_vbos, new_cogl_vbo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This iterates through the list of submitted VBOs looking for one that
|
|
|
|
* contains attribute. If found the list *link* is removed and returned */
|
|
|
|
static GList *
|
|
|
|
unlink_submitted_vbo_containing_attribute (GList **submitted_vbos,
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
GList *next = NULL;
|
|
|
|
|
|
|
|
for (tmp = *submitted_vbos; tmp != NULL; tmp = next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *submitted_vbo = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
GList *tmp2;
|
|
|
|
|
|
|
|
next = tmp->next;
|
|
|
|
|
|
|
|
for (tmp2 = submitted_vbo->attributes; tmp2 != NULL; tmp2 = tmp2->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *submitted_attribute = tmp2->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
if (submitted_attribute->name == attribute->name)
|
|
|
|
{
|
|
|
|
*submitted_vbos = g_list_remove_link (*submitted_vbos, tmp);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlinks all the submitted VBOs that conflict with the new cogl_vbo and
|
|
|
|
* returns them as a list. */
|
|
|
|
static GList *
|
2009-01-20 16:12:44 -05:00
|
|
|
get_submitted_vbo_conflicts (GList **submitted_vbos,
|
|
|
|
CoglVertexBufferVBO *cogl_vbo)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
GList *conflicts = NULL;
|
|
|
|
|
|
|
|
for (tmp = cogl_vbo->attributes; tmp != NULL; tmp = tmp->next)
|
|
|
|
{
|
|
|
|
GList *link =
|
|
|
|
unlink_submitted_vbo_containing_attribute (submitted_vbos,
|
|
|
|
tmp->data);
|
|
|
|
if (link)
|
|
|
|
{
|
|
|
|
/* prepend the link to the list of conflicts: */
|
|
|
|
link->next = conflicts;
|
|
|
|
conflicts = link;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return conflicts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Any attributes in cogl_vbo gets removed from conflict_vbo */
|
|
|
|
static void
|
2009-01-20 16:12:44 -05:00
|
|
|
disassociate_conflicting_attributes (CoglVertexBufferVBO *conflict_vbo,
|
|
|
|
CoglVertexBufferVBO *cogl_vbo)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *tmp;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* NB: The attributes list in conflict_vbo will be shrinking so
|
|
|
|
* we iterate those in the inner loop. */
|
|
|
|
|
|
|
|
for (tmp = cogl_vbo->attributes; tmp != NULL; tmp = tmp->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
GList *tmp2;
|
|
|
|
for (tmp2 = conflict_vbo->attributes; tmp2 != NULL; tmp2 = tmp2->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *conflict_attribute = tmp2->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
if (conflict_attribute->name == attribute->name)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vertex_buffer_attribute_free (conflict_attribute);
|
2008-11-10 13:53:14 -05:00
|
|
|
conflict_vbo->attributes =
|
|
|
|
g_list_delete_link (conflict_vbo->attributes, tmp2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vertex_buffer_vbo_free (CoglVertexBufferVBO *cogl_vbo,
|
|
|
|
gboolean delete_gl_vbo)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
for (tmp = cogl_vbo->attributes; tmp != NULL; tmp = tmp->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vertex_buffer_attribute_free (tmp->data);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
g_list_free (cogl_vbo->attributes);
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
if (delete_gl_vbo && cogl_vbo->flags &
|
|
|
|
COGL_VERTEX_BUFFER_VBO_FLAG_SUBMITTED)
|
2009-04-15 14:25:55 -04:00
|
|
|
{
|
|
|
|
if (cogl_get_features () & COGL_FEATURE_VBOS)
|
|
|
|
GE (glDeleteBuffers (1, (GLuint *)&cogl_vbo->vbo_name));
|
|
|
|
else
|
|
|
|
g_free (cogl_vbo->vbo_name);
|
|
|
|
}
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
g_slice_free (CoglVertexBufferVBO, cogl_vbo);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This figures out the lowest attribute client pointer. (This pointer is used
|
|
|
|
* to upload all the interleved attributes).
|
|
|
|
*
|
|
|
|
* In the process it also replaces the client pointer with the attributes
|
|
|
|
* offset, and marks the attribute as submitted.
|
|
|
|
*/
|
|
|
|
static const void *
|
2009-01-20 16:12:44 -05:00
|
|
|
prep_strided_vbo_for_upload (CoglVertexBufferVBO *cogl_vbo)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
const char *lowest_pointer = NULL;
|
|
|
|
|
|
|
|
for (tmp = cogl_vbo->attributes; tmp != NULL; tmp = tmp->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
const char *client_pointer = attribute->u.pointer;
|
|
|
|
|
|
|
|
if (!lowest_pointer || client_pointer < lowest_pointer)
|
|
|
|
lowest_pointer = client_pointer;
|
|
|
|
}
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
for (tmp = cogl_vbo->attributes; tmp != NULL; tmp = tmp->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
const char *client_pointer = attribute->u.pointer;
|
|
|
|
attribute->u.vbo_offset = client_pointer - lowest_pointer;
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute->flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_SUBMITTED;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return lowest_pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2009-01-20 16:12:44 -05:00
|
|
|
upload_multipack_vbo_via_map_buffer (CoglVertexBufferVBO *cogl_vbo)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
#if HAVE_COGL_GL
|
|
|
|
GList *tmp;
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
unsigned int offset = 0;
|
2008-11-10 13:53:14 -05:00
|
|
|
char *buf;
|
2009-04-15 14:25:55 -04:00
|
|
|
gboolean fallback =
|
|
|
|
(cogl_get_features () & COGL_FEATURE_VBOS) ? FALSE : TRUE;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, FALSE);
|
|
|
|
|
2009-04-15 14:25:55 -04:00
|
|
|
if (!fallback)
|
|
|
|
{
|
|
|
|
buf = glMapBuffer (GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
|
|
|
glGetError();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
buf = cogl_vbo->vbo_name;
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
if (!buf)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (tmp = cogl_vbo->attributes; tmp != NULL; tmp = tmp->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
gsize attribute_size = attribute->span_bytes;
|
|
|
|
gsize gl_type_size = get_gl_type_size (attribute->flags);
|
|
|
|
|
|
|
|
PAD_FOR_ALIGNMENT (offset, gl_type_size);
|
|
|
|
|
|
|
|
memcpy (buf + offset, attribute->u.pointer, attribute_size);
|
|
|
|
|
|
|
|
attribute->u.vbo_offset = offset;
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute->flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_SUBMITTED;
|
2008-11-10 13:53:14 -05:00
|
|
|
offset += attribute_size;
|
|
|
|
}
|
2009-04-15 14:25:55 -04:00
|
|
|
|
|
|
|
if (!fallback)
|
|
|
|
glUnmapBuffer (GL_ARRAY_BUFFER);
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-01-20 16:12:44 -05:00
|
|
|
upload_multipack_vbo_via_buffer_sub_data (CoglVertexBufferVBO *cogl_vbo)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *tmp;
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
unsigned int offset = 0;
|
2009-04-15 14:25:55 -04:00
|
|
|
gboolean fallback =
|
|
|
|
(cogl_get_features () & COGL_FEATURE_VBOS) ? FALSE : TRUE;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
for (tmp = cogl_vbo->attributes; tmp != NULL; tmp = tmp->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
gsize attribute_size = attribute->span_bytes;
|
|
|
|
gsize gl_type_size = get_gl_type_size (attribute->flags);
|
|
|
|
|
|
|
|
PAD_FOR_ALIGNMENT (offset, gl_type_size);
|
|
|
|
|
2009-04-15 14:25:55 -04:00
|
|
|
if (!fallback)
|
|
|
|
{
|
|
|
|
GE (glBufferSubData (GL_ARRAY_BUFFER,
|
|
|
|
offset,
|
|
|
|
attribute_size,
|
|
|
|
attribute->u.pointer));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *dest = (char *)cogl_vbo->vbo_name + offset;
|
|
|
|
memcpy (dest, attribute->u.pointer, attribute_size);
|
|
|
|
}
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
attribute->u.vbo_offset = offset;
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute->flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_SUBMITTED;
|
2008-11-10 13:53:14 -05:00
|
|
|
offset += attribute_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-01-20 16:12:44 -05:00
|
|
|
upload_gl_vbo (CoglVertexBufferVBO *cogl_vbo)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GLenum usage;
|
2009-04-15 14:25:55 -04:00
|
|
|
gboolean fallback =
|
|
|
|
(cogl_get_features () & COGL_FEATURE_VBOS) ? FALSE : TRUE;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
if (cogl_vbo->flags & COGL_VERTEX_BUFFER_VBO_FLAG_FREQUENT_RESUBMIT)
|
2008-11-10 13:53:14 -05:00
|
|
|
usage = GL_DYNAMIC_DRAW;
|
|
|
|
else
|
|
|
|
usage = GL_STATIC_DRAW;
|
|
|
|
|
2009-04-15 14:25:55 -04:00
|
|
|
if (!fallback)
|
|
|
|
{
|
2010-05-27 03:28:29 -04:00
|
|
|
g_return_if_fail (cogl_vbo->vbo_name != NULL);
|
2009-04-15 14:25:55 -04:00
|
|
|
|
|
|
|
GE (glBindBuffer (GL_ARRAY_BUFFER,
|
|
|
|
GPOINTER_TO_UINT (cogl_vbo->vbo_name)));
|
|
|
|
}
|
|
|
|
else if (cogl_vbo->vbo_name == NULL)
|
|
|
|
{
|
|
|
|
/* If the driver doesn't support VBOs then we simply allocate
|
|
|
|
* a client side fake vbo buffer. Unlike VBOs we can't allocate
|
|
|
|
* without specifying a size which is why we defer allocation
|
|
|
|
* until here. */
|
|
|
|
cogl_vbo->vbo_name = g_malloc (cogl_vbo->vbo_bytes);
|
|
|
|
}
|
2009-01-20 16:12:44 -05:00
|
|
|
|
|
|
|
if (cogl_vbo->flags & COGL_VERTEX_BUFFER_VBO_FLAG_STRIDED)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
const void *pointer =
|
|
|
|
prep_strided_vbo_for_upload (cogl_vbo);
|
2009-04-15 14:25:55 -04:00
|
|
|
if (!fallback)
|
|
|
|
{
|
|
|
|
GE (glBufferData (GL_ARRAY_BUFFER,
|
|
|
|
cogl_vbo->vbo_bytes,
|
|
|
|
pointer,
|
|
|
|
usage));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
memcpy (cogl_vbo->vbo_name, pointer, cogl_vbo->vbo_bytes);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
2009-01-20 16:12:44 -05:00
|
|
|
else if (cogl_vbo->flags & COGL_VERTEX_BUFFER_VBO_FLAG_MULTIPACK)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
/* First we make it obvious to the driver that we want to update the
|
|
|
|
* whole buffer (without this, the driver is more likley to block
|
|
|
|
* if the GPU is busy using the buffer) */
|
2009-04-15 14:25:55 -04:00
|
|
|
if (!fallback)
|
|
|
|
{
|
|
|
|
GE (glBufferData (GL_ARRAY_BUFFER,
|
|
|
|
cogl_vbo->vbo_bytes,
|
|
|
|
NULL,
|
|
|
|
usage));
|
|
|
|
}
|
2009-01-20 16:12:44 -05:00
|
|
|
|
|
|
|
/* I think it might depend on the specific driver/HW whether its better
|
|
|
|
* to use glMapBuffer here or glBufferSubData here. There is even a good
|
2008-11-10 13:53:14 -05:00
|
|
|
* thread about this topic here:
|
|
|
|
* http://www.mail-archive.com/dri-devel@lists.sourceforge.net/msg35004.html
|
|
|
|
* For now I have gone with glMapBuffer, but the jury is still out.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!upload_multipack_vbo_via_map_buffer (cogl_vbo))
|
|
|
|
upload_multipack_vbo_via_buffer_sub_data (cogl_vbo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = cogl_vbo->attributes->data;
|
2009-04-15 14:25:55 -04:00
|
|
|
if (!fallback)
|
|
|
|
{
|
|
|
|
GE (glBufferData (GL_ARRAY_BUFFER,
|
|
|
|
cogl_vbo->vbo_bytes,
|
|
|
|
attribute->u.pointer,
|
|
|
|
usage));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
memcpy (cogl_vbo->vbo_name, attribute->u.pointer, cogl_vbo->vbo_bytes);
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* We forget this pointer now since the client will be free
|
|
|
|
* to re-use this memory */
|
|
|
|
attribute->u.pointer = NULL;
|
2009-01-20 16:12:44 -05:00
|
|
|
attribute->flags |= COGL_VERTEX_BUFFER_ATTRIB_FLAG_SUBMITTED;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vbo->flags |= COGL_VERTEX_BUFFER_VBO_FLAG_SUBMITTED;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-04-15 14:25:55 -04:00
|
|
|
if (!fallback)
|
|
|
|
GE (glBindBuffer (GL_ARRAY_BUFFER, 0));
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
/* Note: although there ends up being quite a few inner loops involved with
|
|
|
|
* resolving buffers, the number of attributes will be low so I don't expect
|
|
|
|
* them to cause a problem. */
|
2008-11-10 13:53:14 -05:00
|
|
|
static void
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vertex_buffer_vbo_resolve (CoglVertexBuffer *buffer,
|
|
|
|
CoglVertexBufferVBO *new_cogl_vbo,
|
|
|
|
GList **final_vbos)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *conflicts;
|
|
|
|
GList *tmp;
|
|
|
|
GList *next;
|
|
|
|
gboolean found_target_vbo = FALSE;
|
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
conflicts =
|
2009-01-20 16:12:44 -05:00
|
|
|
get_submitted_vbo_conflicts (&buffer->submitted_vbos, new_cogl_vbo);
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
for (tmp = conflicts; tmp != NULL; tmp = next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *conflict_vbo = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
next = tmp->next;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
disassociate_conflicting_attributes (conflict_vbo, new_cogl_vbo);
|
|
|
|
|
|
|
|
if (!conflict_vbo->attributes)
|
|
|
|
{
|
|
|
|
/* See if we can re-use this now empty VBO: */
|
|
|
|
|
|
|
|
if (!found_target_vbo
|
|
|
|
&& conflict_vbo->vbo_bytes == new_cogl_vbo->vbo_bytes)
|
|
|
|
{
|
|
|
|
found_target_vbo = TRUE;
|
|
|
|
new_cogl_vbo->vbo_name = conflict_vbo->vbo_name;
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vertex_buffer_vbo_free (conflict_vbo, FALSE);
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
upload_gl_vbo (new_cogl_vbo);
|
|
|
|
|
|
|
|
*final_vbos = g_list_prepend (*final_vbos, new_cogl_vbo);
|
|
|
|
}
|
|
|
|
else
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vertex_buffer_vbo_free (conflict_vbo, TRUE);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
/* Relink the VBO back into buffer->submitted_vbos since it may
|
2008-11-10 13:53:14 -05:00
|
|
|
* be involved in other conflicts later */
|
2009-01-20 16:12:44 -05:00
|
|
|
tmp->next = buffer->submitted_vbos;
|
2008-11-10 13:53:14 -05:00
|
|
|
tmp->prev = NULL;
|
2009-01-20 16:12:44 -05:00
|
|
|
buffer->submitted_vbos = tmp;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found_target_vbo)
|
|
|
|
{
|
2009-04-15 14:25:55 -04:00
|
|
|
if (cogl_get_features () & COGL_FEATURE_VBOS)
|
|
|
|
GE (glGenBuffers (1, (GLuint *)&new_cogl_vbo->vbo_name));
|
|
|
|
else
|
|
|
|
new_cogl_vbo->vbo_name = NULL;
|
|
|
|
/* this will be allocated at upload time */
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
upload_gl_vbo (new_cogl_vbo);
|
|
|
|
*final_vbos = g_list_prepend (*final_vbos, new_cogl_vbo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-28 12:36:25 -05:00
|
|
|
static void
|
|
|
|
cogl_vertex_buffer_submit_real (CoglVertexBuffer *buffer)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *tmp;
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *new_multipack_vbo;
|
2008-11-10 13:53:14 -05:00
|
|
|
GList *new_multipack_vbo_link;
|
|
|
|
GList *new_vbos = NULL;
|
|
|
|
GList *reuse_vbos = NULL;
|
|
|
|
GList *final_vbos = NULL;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2009-02-28 12:36:25 -05:00
|
|
|
if (!buffer->new_attributes)
|
2008-11-10 13:53:14 -05:00
|
|
|
return;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* The objective now is to copy the attribute data supplied by the client
|
2009-01-20 16:12:44 -05:00
|
|
|
* into buffer objects, but it's important to minimize the number of
|
|
|
|
* redundant data uploads.
|
2008-11-10 13:53:14 -05:00
|
|
|
*
|
2009-01-20 16:12:44 -05:00
|
|
|
* We obviously aim to group together the attributes that are interleved so
|
|
|
|
* that they can be delivered in one go to the driver.
|
|
|
|
* All BOs for interleved data are created as STATIC_DRAW_ARB.
|
2008-11-10 13:53:14 -05:00
|
|
|
*
|
|
|
|
* Non interleved attributes tagged as INFREQUENT_RESUBMIT will be grouped
|
|
|
|
* together back to back in a single BO created as STATIC_DRAW_ARB
|
|
|
|
*
|
|
|
|
* Non interleved attributes tagged as FREQUENT_RESUBMIT will be copied into
|
|
|
|
* individual buffer objects, and the BO itself created DYNAMIC_DRAW_ARB
|
|
|
|
*
|
2009-01-20 16:12:44 -05:00
|
|
|
* If we are modifying a previously submitted CoglVertexBuffer then we are
|
|
|
|
* carefull not to needlesly delete OpenGL buffer objects and replace with
|
|
|
|
* new ones, instead we upload new data to the existing buffers.
|
2008-11-10 13:53:14 -05:00
|
|
|
*/
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* NB: We must forget attribute->pointer after submitting since the user
|
|
|
|
* is free to re-use that memory for other purposes now. */
|
|
|
|
|
|
|
|
/* Pseudo code:
|
2009-01-20 16:12:44 -05:00
|
|
|
*
|
2008-11-10 13:53:14 -05:00
|
|
|
* Broadly speaking we start with a list of unsorted attributes, and filter
|
2009-01-20 16:12:44 -05:00
|
|
|
* those into 'new' and 're-use' CoglVertexBufferVBO (CBO) lists. We then
|
|
|
|
* take the list of new CBO structs and compare with the CBOs that have
|
|
|
|
* already been submitted to the GPU (but ignoring those we already know will
|
|
|
|
* be re-used) to determine what other CBOs can be re-used, due to being
|
|
|
|
* superseded, and what new GL VBOs need to be created.
|
|
|
|
*
|
2008-11-10 13:53:14 -05:00
|
|
|
* We have three kinds of CBOs:
|
|
|
|
* - Unstrided CBOs
|
|
|
|
* These contain a single tightly packed attribute
|
|
|
|
* These are currently the only ones ever marked as FREQUENT_SUBMIT
|
|
|
|
* - Strided CBOs
|
|
|
|
* These typically contain multiple interleved sets of attributes,
|
|
|
|
* though they can contain just one attribute with a stride
|
|
|
|
* - Multi Pack CBOs
|
|
|
|
* These contain multiple attributes tightly packed back to back)
|
|
|
|
*
|
|
|
|
* First create a new-CBOs entry "new-multipack-CBO"
|
|
|
|
* Tag "new-multipack-CBO" as MULTIPACK + INFREQUENT_RESUBMIT
|
|
|
|
* For each unsorted attrib:
|
|
|
|
* if already marked as submitted:
|
|
|
|
* iterate reuse-CBOs:
|
|
|
|
* if we find one that contains this attribute:
|
|
|
|
* free redundant unsorted attrib struct
|
|
|
|
* remove the UNUSED flag from the attrib found in the reuse-CBO
|
|
|
|
* continue to next unsorted attrib
|
|
|
|
* iterate submitted VBOs:
|
|
|
|
* if we find one that contains this attribute:
|
|
|
|
* free redundant unsorted attrib struct
|
|
|
|
* unlink the vbo and move it to the list of reuse-CBOs
|
|
|
|
* mark all attributes except the one just matched as UNUSED
|
|
|
|
* assert (found)
|
|
|
|
* continue to next unsorted attrib
|
|
|
|
* if strided:
|
|
|
|
* iterate the new, strided, CBOs, to see if the attribute is
|
|
|
|
* interleved with one of them, if found:
|
|
|
|
* add to the matched CBO
|
|
|
|
* else if not found:
|
|
|
|
* create a new-CBOs entry tagged STRIDED + INFREQUENT_RESUBMIT
|
|
|
|
* else if unstrided && tagged with FREQUENT_RESUBMIT:
|
|
|
|
* create a new-CBOs entry tagged UNSTRIDED + FREQUENT_RESUBMIT
|
|
|
|
* else
|
|
|
|
* add to the new-multipack-CBO
|
|
|
|
* free list of unsorted-attribs
|
2009-01-20 16:12:44 -05:00
|
|
|
*
|
2008-11-10 13:53:14 -05:00
|
|
|
* Next compare the new list of CBOs with the submitted set and try to
|
|
|
|
* minimize the memory bandwidth required to upload the attributes and the
|
|
|
|
* overhead of creating new GL-BOs.
|
|
|
|
*
|
|
|
|
* We deal with four sets of CBOs:
|
|
|
|
* - The "new" CBOs
|
|
|
|
* (as determined above during filtering)
|
|
|
|
* - The "re-use" CBOs
|
|
|
|
* (as determined above during filtering)
|
|
|
|
* - The "submitted" CBOs
|
|
|
|
* (I.e. ones currently submitted to the GPU)
|
|
|
|
* - The "final" CBOs
|
|
|
|
* (The result of resolving the differences between the above sets)
|
2009-01-20 16:12:44 -05:00
|
|
|
*
|
2008-11-10 13:53:14 -05:00
|
|
|
* The re-use CBOs are dealt with first, and we simply delete any remaining
|
|
|
|
* attributes in these that are still marked as UNUSED, and move them
|
|
|
|
* to the list of final CBOs.
|
|
|
|
*
|
|
|
|
* Next we iterate through the "new" CBOs, searching for conflicts
|
|
|
|
* with the "submitted" CBOs and commit our decision to the "final" CBOs
|
|
|
|
*
|
|
|
|
* When searching for submitted entries we always unlink items from the
|
|
|
|
* submitted list once we make matches (before we make descisions
|
|
|
|
* based on the matches). If the CBO node is superseded it is freed,
|
|
|
|
* if it is modified but may be needed for more descisions later it is
|
|
|
|
* relinked back into the submitted list and if it's identical to a new
|
2009-01-20 16:12:44 -05:00
|
|
|
* CBO it will be linked into the final list.
|
2008-11-10 13:53:14 -05:00
|
|
|
*
|
|
|
|
* At the end the list of submitted CBOs represents the attributes that were
|
2009-01-20 16:12:44 -05:00
|
|
|
* deleted from the buffer.
|
|
|
|
*
|
2008-11-10 13:53:14 -05:00
|
|
|
* Iterate re-use-CBOs:
|
|
|
|
* Iterate attribs for each:
|
|
|
|
* if attrib UNUSED:
|
|
|
|
* remove the attrib from the CBO + free
|
|
|
|
* |Note: we could potentially mark this as a re-useable gap
|
|
|
|
* |if needs be later.
|
|
|
|
* add re-use CBO to the final-CBOs
|
|
|
|
* Iterate new-CBOs:
|
|
|
|
* List submitted CBOs conflicting with the this CBO (Unlinked items)
|
|
|
|
* found-target-BO=FALSE
|
|
|
|
* Iterate conflicting CBOs:
|
|
|
|
* Disassociate conflicting attribs from conflicting CBO struct
|
|
|
|
* If no attribs remain:
|
|
|
|
* If found-target-BO!=TRUE
|
|
|
|
* _AND_ If the total size of the conflicting CBO is compatible:
|
|
|
|
* |Note: We don't currently consider re-using oversized buffers
|
|
|
|
* found-target-BO=TRUE
|
|
|
|
* upload replacement data
|
|
|
|
* free submitted CBO struct
|
|
|
|
* add new CBO struct to final-CBOs
|
|
|
|
* else:
|
|
|
|
* delete conflict GL-BO
|
|
|
|
* delete conflict CBO struct
|
|
|
|
* else:
|
|
|
|
* relink CBO back into submitted-CBOs
|
|
|
|
*
|
|
|
|
* if found-target-BO == FALSE:
|
|
|
|
* create a new GL-BO
|
|
|
|
* upload data
|
|
|
|
* add new CBO struct to final-BOs
|
|
|
|
*
|
|
|
|
* Iterate through the remaining "submitted" CBOs:
|
|
|
|
* delete the submitted GL-BO
|
|
|
|
* free the submitted CBO struct
|
|
|
|
*/
|
2009-01-20 16:12:44 -05:00
|
|
|
|
|
|
|
new_multipack_vbo = g_slice_alloc (sizeof (CoglVertexBufferVBO));
|
2010-05-27 03:28:29 -04:00
|
|
|
new_multipack_vbo->vbo_name = NULL;
|
2009-01-20 16:12:44 -05:00
|
|
|
new_multipack_vbo->flags =
|
|
|
|
COGL_VERTEX_BUFFER_VBO_FLAG_MULTIPACK
|
|
|
|
| COGL_VERTEX_BUFFER_VBO_FLAG_INFREQUENT_RESUBMIT;
|
2008-11-10 13:53:14 -05:00
|
|
|
new_multipack_vbo->vbo_bytes = 0;
|
|
|
|
new_multipack_vbo->attributes = NULL;
|
|
|
|
new_vbos = g_list_prepend (new_vbos, new_multipack_vbo);
|
|
|
|
/* We save the link pointer here, just so we can do a fast removal later if
|
|
|
|
* no attributes get added to this vbo. */
|
|
|
|
new_multipack_vbo_link = new_vbos;
|
|
|
|
|
|
|
|
/* Start with a list of unsorted attributes, and filter those into
|
|
|
|
* potential new Cogl BO structs
|
|
|
|
*/
|
2009-01-20 16:12:44 -05:00
|
|
|
for (tmp = buffer->new_attributes; tmp != NULL; tmp = tmp->next)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp->data;
|
|
|
|
|
|
|
|
if (attribute->flags & COGL_VERTEX_BUFFER_ATTRIB_FLAG_SUBMITTED)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
/* If the attribute is already marked as submitted, then we need
|
|
|
|
* to find the existing VBO that contains it so we dont delete it.
|
|
|
|
*
|
|
|
|
* NB: this also frees the attribute struct since it's implicitly
|
|
|
|
* redundant in this case.
|
|
|
|
*/
|
|
|
|
filter_already_submitted_attribute (attribute,
|
|
|
|
&reuse_vbos,
|
2009-01-20 16:12:44 -05:00
|
|
|
&buffer->submitted_vbos);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
else if (attribute->stride)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
/* look for a CoglVertexBufferVBO that the attribute is
|
|
|
|
* interleved with. If one can't be found then a new
|
|
|
|
* CoglVertexBufferVBO is allocated and added to the list of
|
|
|
|
* new_vbos: */
|
2008-11-10 13:53:14 -05:00
|
|
|
filter_strided_attribute (attribute, &new_vbos);
|
|
|
|
}
|
2009-01-20 16:12:44 -05:00
|
|
|
else if (attribute->flags &
|
|
|
|
COGL_VERTEX_BUFFER_ATTRIB_FLAG_FREQUENT_RESUBMIT)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *cogl_vbo =
|
|
|
|
g_slice_alloc (sizeof (CoglVertexBufferVBO));
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* attributes we expect will be frequently resubmitted are placed
|
|
|
|
* in their own VBO so that updates don't impact other attributes
|
|
|
|
*/
|
|
|
|
|
2010-05-27 03:28:29 -04:00
|
|
|
cogl_vbo->vbo_name = NULL;
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vbo->flags =
|
|
|
|
COGL_VERTEX_BUFFER_VBO_FLAG_UNSTRIDED
|
|
|
|
| COGL_VERTEX_BUFFER_VBO_FLAG_FREQUENT_RESUBMIT;
|
2008-11-10 13:53:14 -05:00
|
|
|
cogl_vbo->attributes = NULL;
|
|
|
|
cogl_vbo->attributes = g_list_prepend (cogl_vbo->attributes,
|
|
|
|
attribute);
|
|
|
|
cogl_vbo->vbo_bytes = attribute->span_bytes;
|
|
|
|
new_vbos = g_list_prepend (new_vbos, cogl_vbo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gsize gl_type_size = get_gl_type_size (attribute->flags);
|
|
|
|
|
|
|
|
/* Infrequently updated attributes just get packed back to back
|
|
|
|
* in a single VBO: */
|
|
|
|
new_multipack_vbo->attributes =
|
|
|
|
g_list_prepend (new_multipack_vbo->attributes,
|
|
|
|
attribute);
|
|
|
|
|
|
|
|
/* Note: we have to ensure that each run of attributes is
|
|
|
|
* naturally aligned according to its data type, which may
|
|
|
|
* require some padding bytes: */
|
|
|
|
|
|
|
|
/* XXX: We also have to be sure that the attributes aren't
|
|
|
|
* reorderd before being uploaded because the alignment padding
|
|
|
|
* is based on the adjacent attribute.
|
|
|
|
*/
|
|
|
|
|
|
|
|
PAD_FOR_ALIGNMENT (new_multipack_vbo->vbo_bytes, gl_type_size);
|
|
|
|
|
|
|
|
new_multipack_vbo->vbo_bytes += attribute->span_bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
/* At this point all buffer->new_attributes have been filtered into
|
|
|
|
* CoglVertexBufferVBOs... */
|
|
|
|
g_list_free (buffer->new_attributes);
|
|
|
|
buffer->new_attributes = NULL;
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* If the multipack vbo wasn't needed: */
|
|
|
|
if (new_multipack_vbo->attributes == NULL)
|
|
|
|
{
|
|
|
|
new_vbos = g_list_delete_link (new_vbos, new_multipack_vbo_link);
|
2009-01-20 16:12:44 -05:00
|
|
|
g_slice_free (CoglVertexBufferVBO, new_multipack_vbo);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (tmp = reuse_vbos; tmp != NULL; tmp = tmp->next)
|
|
|
|
remove_unused_attributes (tmp->data);
|
|
|
|
final_vbos = g_list_concat (final_vbos, reuse_vbos);
|
|
|
|
|
|
|
|
for (tmp = new_vbos; tmp != NULL; tmp = tmp->next)
|
2009-01-20 16:12:44 -05:00
|
|
|
cogl_vertex_buffer_vbo_resolve (buffer, tmp->data, &final_vbos);
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* Anything left corresponds to deleted attributes: */
|
2009-01-20 16:12:44 -05:00
|
|
|
for (tmp = buffer->submitted_vbos; tmp != NULL; tmp = tmp->next)
|
|
|
|
cogl_vertex_buffer_vbo_free (tmp->data, TRUE);
|
|
|
|
g_list_free (buffer->submitted_vbos);
|
2009-06-16 10:34:13 -04:00
|
|
|
g_list_free (new_vbos);
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
buffer->submitted_vbos = final_vbos;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
2009-02-28 12:36:25 -05:00
|
|
|
void
|
|
|
|
cogl_vertex_buffer_submit (CoglHandle handle)
|
|
|
|
{
|
|
|
|
CoglVertexBuffer *buffer;
|
|
|
|
|
|
|
|
if (!cogl_is_vertex_buffer (handle))
|
|
|
|
return;
|
|
|
|
|
|
|
|
buffer = _cogl_vertex_buffer_pointer_from_handle (handle);
|
|
|
|
|
|
|
|
cogl_vertex_buffer_submit_real (buffer);
|
|
|
|
}
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
static GLenum
|
2009-01-20 16:12:44 -05:00
|
|
|
get_gl_type_from_attribute_flags (CoglVertexBufferAttribFlags flags)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttribFlags gl_type =
|
|
|
|
flags & COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_MASK;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
switch (gl_type)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_BYTE:
|
2008-11-10 13:53:14 -05:00
|
|
|
return GL_BYTE;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_UNSIGNED_BYTE:
|
2008-11-10 13:53:14 -05:00
|
|
|
return GL_UNSIGNED_BYTE;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_SHORT:
|
2008-11-10 13:53:14 -05:00
|
|
|
return GL_SHORT;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_UNSIGNED_SHORT:
|
2008-11-10 13:53:14 -05:00
|
|
|
return GL_UNSIGNED_SHORT;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_FLOAT:
|
2008-11-10 13:53:14 -05:00
|
|
|
return GL_FLOAT;
|
|
|
|
#if HAVE_COGL_GL
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_INT:
|
2008-11-10 13:53:14 -05:00
|
|
|
return GL_INT;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_UNSIGNED_INT:
|
2008-11-10 13:53:14 -05:00
|
|
|
return GL_UNSIGNED_INT;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_GL_TYPE_DOUBLE:
|
2008-11-10 13:53:14 -05:00
|
|
|
return GL_DOUBLE;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
g_warning ("Couldn't convert from attribute flags (0x%08x) "
|
|
|
|
"to gl type enum\n", flags);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-26 05:01:43 -04:00
|
|
|
static CoglHandle
|
2009-02-06 11:10:28 -05:00
|
|
|
enable_state_for_drawing_buffer (CoglVertexBuffer *buffer)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
GList *tmp;
|
|
|
|
GLenum gl_type;
|
2009-03-23 08:46:20 -04:00
|
|
|
#ifdef MAY_HAVE_PROGRAMABLE_GL
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
GLuint generic_index = 0;
|
2009-03-23 08:46:20 -04:00
|
|
|
#endif
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
unsigned long enable_flags = 0;
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
const GList *layers;
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
guint32 fallback_layers = 0;
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
int i;
|
2010-05-18 17:42:49 -04:00
|
|
|
gboolean skip_gl_color = FALSE;
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
CoglMaterialFlushOptions options;
|
2010-04-26 05:01:43 -04:00
|
|
|
CoglHandle source;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2010-04-26 05:01:43 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, COGL_INVALID_HANDLE);
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2010-04-08 07:21:04 -04:00
|
|
|
source = ctx->source_material;
|
|
|
|
|
2009-02-28 12:36:25 -05:00
|
|
|
if (buffer->new_attributes)
|
|
|
|
cogl_vertex_buffer_submit_real (buffer);
|
|
|
|
|
2010-04-01 13:35:32 -04:00
|
|
|
options.flags =
|
2010-05-19 19:16:49 -04:00
|
|
|
COGL_MATERIAL_FLUSH_FALLBACK_MASK;
|
2010-04-01 13:35:32 -04:00
|
|
|
memset (&options.wrap_mode_overrides, 0,
|
|
|
|
sizeof (options.wrap_mode_overrides));
|
|
|
|
|
2010-05-24 07:40:11 -04:00
|
|
|
_cogl_bitmask_clear_all (&ctx->temp_bitmask);
|
|
|
|
|
2010-09-09 07:15:59 -04:00
|
|
|
/* NB: _cogl_framebuffer_flush_state may disrupt various state (such
|
|
|
|
* as the material state) when flushing the clip stack, so should
|
|
|
|
* always be done first when preparing to draw. We need to do this
|
|
|
|
* before setting up the array pointers because setting up the clip
|
|
|
|
* stack can cause some drawing which would change the array
|
|
|
|
* pointers. */
|
|
|
|
_cogl_framebuffer_flush_state (_cogl_get_framebuffer (), 0);
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
for (tmp = buffer->submitted_vbos; tmp != NULL; tmp = tmp->next)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *cogl_vbo = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
GList *tmp2;
|
2009-04-15 14:25:55 -04:00
|
|
|
char *base;
|
|
|
|
const GLvoid *pointer;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-04-15 14:25:55 -04:00
|
|
|
if (cogl_get_features () & COGL_FEATURE_VBOS)
|
|
|
|
{
|
|
|
|
GE (glBindBuffer (GL_ARRAY_BUFFER,
|
|
|
|
GPOINTER_TO_UINT (cogl_vbo->vbo_name)));
|
2010-05-27 03:28:29 -04:00
|
|
|
base = NULL;
|
2009-04-15 14:25:55 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
base = cogl_vbo->vbo_name;
|
|
|
|
|
|
|
|
/* When GL VBOs are bing used then the "pointer" we pass to
|
|
|
|
* glColorPointer glVertexAttribPointer etc is actually an offset into
|
|
|
|
* the currently bound VBO.
|
|
|
|
*
|
|
|
|
* If we don't have VBO support though, then we must point into
|
|
|
|
* our fake client side VBO.
|
|
|
|
*/
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
for (tmp2 = cogl_vbo->attributes; tmp2 != NULL; tmp2 = tmp2->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp2->data;
|
|
|
|
CoglVertexBufferAttribFlags type =
|
|
|
|
attribute->flags & COGL_VERTEX_BUFFER_ATTRIB_FLAG_TYPE_MASK;
|
|
|
|
|
|
|
|
if (!(attribute->flags & COGL_VERTEX_BUFFER_ATTRIB_FLAG_ENABLED))
|
2008-11-10 13:53:14 -05:00
|
|
|
continue;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
gl_type = get_gl_type_from_attribute_flags (attribute->flags);
|
|
|
|
switch (type)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_COLOR_ARRAY:
|
2010-04-08 07:21:04 -04:00
|
|
|
enable_flags |= COGL_ENABLE_COLOR_ARRAY;
|
2009-02-06 11:10:28 -05:00
|
|
|
/* GE (glEnableClientState (GL_COLOR_ARRAY)); */
|
2009-04-15 14:25:55 -04:00
|
|
|
pointer = (const GLvoid *)(base + attribute->u.vbo_offset);
|
2008-11-10 13:53:14 -05:00
|
|
|
GE (glColorPointer (attribute->n_components,
|
|
|
|
gl_type,
|
|
|
|
attribute->stride,
|
2009-04-15 14:25:55 -04:00
|
|
|
pointer));
|
2010-04-08 07:21:04 -04:00
|
|
|
|
|
|
|
if (!_cogl_material_get_real_blend_enabled (ctx->source_material))
|
|
|
|
{
|
|
|
|
CoglMaterialBlendEnable blend_enable =
|
|
|
|
COGL_MATERIAL_BLEND_ENABLE_ENABLED;
|
|
|
|
source = cogl_material_copy (ctx->source_material);
|
|
|
|
_cogl_material_set_blend_enabled (source, blend_enable);
|
2010-05-18 17:42:49 -04:00
|
|
|
skip_gl_color = TRUE;
|
2010-04-08 07:21:04 -04:00
|
|
|
}
|
2008-11-10 13:53:14 -05:00
|
|
|
break;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_NORMAL_ARRAY:
|
2008-11-10 13:53:14 -05:00
|
|
|
/* FIXME: go through cogl cache to enable normal array */
|
|
|
|
GE (glEnableClientState (GL_NORMAL_ARRAY));
|
2009-04-15 14:25:55 -04:00
|
|
|
pointer = (const GLvoid *)(base + attribute->u.vbo_offset);
|
2008-11-10 13:53:14 -05:00
|
|
|
GE (glNormalPointer (gl_type,
|
|
|
|
attribute->stride,
|
2009-04-15 14:25:55 -04:00
|
|
|
pointer));
|
2008-11-10 13:53:14 -05:00
|
|
|
break;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_TEXTURE_COORD_ARRAY:
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
GE (glClientActiveTexture (GL_TEXTURE0 +
|
|
|
|
attribute->texture_unit));
|
|
|
|
GE (glEnableClientState (GL_TEXTURE_COORD_ARRAY));
|
2009-04-15 14:25:55 -04:00
|
|
|
pointer = (const GLvoid *)(base + attribute->u.vbo_offset);
|
2008-11-10 13:53:14 -05:00
|
|
|
GE (glTexCoordPointer (attribute->n_components,
|
|
|
|
gl_type,
|
|
|
|
attribute->stride,
|
2009-04-15 14:25:55 -04:00
|
|
|
pointer));
|
2010-05-24 07:40:11 -04:00
|
|
|
_cogl_bitmask_set (&ctx->temp_bitmask,
|
|
|
|
attribute->texture_unit, TRUE);
|
2008-11-10 13:53:14 -05:00
|
|
|
break;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_VERTEX_ARRAY:
|
2008-11-10 13:53:14 -05:00
|
|
|
enable_flags |= COGL_ENABLE_VERTEX_ARRAY;
|
|
|
|
/* GE (glEnableClientState (GL_VERTEX_ARRAY)); */
|
2009-04-15 14:25:55 -04:00
|
|
|
pointer = (const GLvoid *)(base + attribute->u.vbo_offset);
|
2008-11-10 13:53:14 -05:00
|
|
|
GE (glVertexPointer (attribute->n_components,
|
|
|
|
gl_type,
|
|
|
|
attribute->stride,
|
2009-04-15 14:25:55 -04:00
|
|
|
pointer));
|
2008-11-10 13:53:14 -05:00
|
|
|
break;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_CUSTOM_ARRAY:
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
#ifdef MAY_HAVE_PROGRAMABLE_GL
|
|
|
|
GLboolean normalized = GL_FALSE;
|
2009-01-20 16:12:44 -05:00
|
|
|
if (attribute->flags &
|
|
|
|
COGL_VERTEX_BUFFER_ATTRIB_FLAG_NORMALIZED)
|
2008-11-10 13:53:14 -05:00
|
|
|
normalized = GL_TRUE;
|
|
|
|
/* FIXME: go through cogl cache to enable generic array */
|
|
|
|
GE (glEnableVertexAttribArray (generic_index++));
|
2009-04-15 14:25:55 -04:00
|
|
|
pointer = (const GLvoid *)(base + attribute->u.vbo_offset);
|
2009-01-20 16:12:44 -05:00
|
|
|
GE (glVertexAttribPointer (generic_index,
|
2008-11-10 13:53:14 -05:00
|
|
|
attribute->n_components,
|
|
|
|
gl_type,
|
|
|
|
normalized,
|
|
|
|
attribute->stride,
|
2009-04-15 14:25:55 -04:00
|
|
|
pointer));
|
2008-11-10 13:53:14 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_warning ("Unrecognised attribute type 0x%08x", type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2010-04-08 07:21:04 -04:00
|
|
|
layers = cogl_material_get_layers (source);
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
for (tmp = (GList *)layers, i = 0;
|
2010-05-19 19:16:49 -04:00
|
|
|
tmp != NULL;
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
tmp = tmp->next, i++)
|
|
|
|
{
|
|
|
|
CoglHandle layer = (CoglHandle)tmp->data;
|
|
|
|
CoglHandle tex_handle = cogl_material_layer_get_texture (layer);
|
2009-06-19 07:15:12 -04:00
|
|
|
|
|
|
|
/* invalid textures will be handled correctly in
|
|
|
|
* _cogl_material_flush_layers_gl_state */
|
|
|
|
if (tex_handle == COGL_INVALID_HANDLE)
|
|
|
|
continue;
|
|
|
|
|
2010-04-07 06:06:20 -04:00
|
|
|
if (!cogl_material_get_layer_point_sprite_coords_enabled (source, i))
|
|
|
|
{
|
|
|
|
/* By default COGL_MATERIAL_WRAP_MODE_AUTOMATIC becomes
|
|
|
|
GL_CLAMP_TO_EDGE but we want GL_REPEAT to maintain
|
|
|
|
compatibility with older versions of Cogl so we'll
|
|
|
|
override it. We don't want to do this for point sprites
|
|
|
|
because in that case the whole texture is drawn so you
|
|
|
|
would usually want clamp-to-edge. */
|
|
|
|
if (cogl_material_layer_get_wrap_mode_s (layer) ==
|
|
|
|
COGL_MATERIAL_WRAP_MODE_AUTOMATIC)
|
|
|
|
{
|
|
|
|
options.wrap_mode_overrides.values[i].s =
|
|
|
|
COGL_MATERIAL_WRAP_MODE_OVERRIDE_REPEAT;
|
|
|
|
options.flags |= COGL_MATERIAL_FLUSH_WRAP_MODE_OVERRIDES;
|
|
|
|
}
|
|
|
|
if (cogl_material_layer_get_wrap_mode_t (layer) ==
|
|
|
|
COGL_MATERIAL_WRAP_MODE_AUTOMATIC)
|
|
|
|
{
|
|
|
|
options.wrap_mode_overrides.values[i].t =
|
|
|
|
COGL_MATERIAL_WRAP_MODE_OVERRIDE_REPEAT;
|
|
|
|
options.flags |= COGL_MATERIAL_FLUSH_WRAP_MODE_OVERRIDES;
|
|
|
|
}
|
2010-07-02 08:33:06 -04:00
|
|
|
if (cogl_material_layer_get_wrap_mode_p (layer) ==
|
2010-04-07 06:06:20 -04:00
|
|
|
COGL_MATERIAL_WRAP_MODE_AUTOMATIC)
|
|
|
|
{
|
2010-07-12 12:01:32 -04:00
|
|
|
options.wrap_mode_overrides.values[i].p =
|
2010-04-07 06:06:20 -04:00
|
|
|
COGL_MATERIAL_WRAP_MODE_OVERRIDE_REPEAT;
|
|
|
|
options.flags |= COGL_MATERIAL_FLUSH_WRAP_MODE_OVERRIDES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-18 04:22:04 -05:00
|
|
|
/* Give the texture a chance to know that we're rendering
|
|
|
|
non-quad shaped primitives. If the texture is in an atlas it
|
|
|
|
will be migrated */
|
|
|
|
_cogl_texture_ensure_non_quad_rendering (tex_handle);
|
|
|
|
|
2010-06-04 12:06:32 -04:00
|
|
|
/* We need to ensure the mipmaps are ready before deciding
|
|
|
|
* anything else about the texture because the texture storate
|
|
|
|
* could completely change if it needs to be migrated out of the
|
|
|
|
* atlas and will affect how we validate the layer.
|
|
|
|
*/
|
2010-06-09 12:39:59 -04:00
|
|
|
_cogl_material_layer_pre_paint (layer);
|
2010-06-04 12:06:32 -04:00
|
|
|
|
2009-09-16 06:56:17 -04:00
|
|
|
if (!_cogl_texture_can_hardware_repeat (tex_handle))
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
{
|
|
|
|
g_warning ("Disabling layer %d of the current source material, "
|
|
|
|
"because texturing with the vertex buffer API is not "
|
|
|
|
"currently supported using sliced textures, or textures "
|
|
|
|
"with waste\n", i);
|
|
|
|
|
|
|
|
/* XXX: maybe we can add a mechanism for users to forcibly use
|
|
|
|
* textures with waste where it would be their responsability to use
|
|
|
|
* texture coords in the range [0,1] such that sampling outside isn't
|
|
|
|
* required. We can then use a texture matrix (or a modification of
|
|
|
|
* the users own matrix) to map 1 to the edge of the texture data.
|
|
|
|
*
|
|
|
|
* Potentially, given the same guarantee as above we could also
|
|
|
|
* support a single sliced layer too. We would have to redraw the
|
|
|
|
* vertices once for each layer, each time with a fiddled texture
|
|
|
|
* matrix.
|
|
|
|
*/
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
fallback_layers |= (1 << i);
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-24 07:40:11 -04:00
|
|
|
/* Disable any tex coord arrays that we didn't use */
|
|
|
|
_cogl_disable_other_texcoord_arrays (&ctx->temp_bitmask);
|
2009-06-24 13:10:50 -04:00
|
|
|
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
options.fallback_layers = fallback_layers;
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
|
2010-05-18 17:42:49 -04:00
|
|
|
if (G_UNLIKELY (options.flags))
|
|
|
|
{
|
|
|
|
/* If we haven't already created a derived material... */
|
|
|
|
if (source == ctx->source_material)
|
|
|
|
source = cogl_material_copy (ctx->source_material);
|
|
|
|
_cogl_material_apply_overrides (source, &options);
|
|
|
|
|
|
|
|
/* TODO:
|
|
|
|
* overrides = cogl_material_get_data (material,
|
|
|
|
* last_overrides_key);
|
|
|
|
* if (overrides)
|
|
|
|
* {
|
|
|
|
* age = cogl_material_get_age (material);
|
|
|
|
* XXX: actually we also need to check for legacy_state
|
|
|
|
* and blending overrides for use of glColorPointer...
|
|
|
|
* if (overrides->ags != age ||
|
|
|
|
* memcmp (&overrides->options, &options,
|
|
|
|
* sizeof (options) != 0)
|
|
|
|
* {
|
|
|
|
* cogl_handle_unref (overrides->weak_material);
|
|
|
|
* g_slice_free (Overrides, overrides);
|
|
|
|
* overrides = NULL;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* if (!overrides)
|
|
|
|
* {
|
|
|
|
* overrides = g_slice_new (Overrides);
|
|
|
|
* overrides->weak_material =
|
|
|
|
* cogl_material_weak_copy (ctx->source_material);
|
|
|
|
* _cogl_material_apply_overrides (overrides->weak_material,
|
|
|
|
* &options);
|
|
|
|
*
|
|
|
|
* cogl_material_set_data (material, last_overrides_key,
|
|
|
|
* weak_overrides,
|
|
|
|
* free_overrides_cb,
|
|
|
|
* NULL);
|
|
|
|
* }
|
|
|
|
* source = overrides->weak_material;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2010-04-26 05:01:43 -04:00
|
|
|
if (G_UNLIKELY (ctx->legacy_state_set))
|
|
|
|
{
|
2010-04-08 07:21:04 -04:00
|
|
|
/* If we haven't already created a derived material... */
|
|
|
|
if (source == ctx->source_material)
|
|
|
|
source = cogl_material_copy (ctx->source_material);
|
2010-04-26 05:01:43 -04:00
|
|
|
_cogl_material_apply_legacy_state (source);
|
|
|
|
}
|
|
|
|
|
2010-05-18 17:42:49 -04:00
|
|
|
_cogl_material_flush_gl_state (source, skip_gl_color);
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-05-21 09:56:38 -04:00
|
|
|
if (ctx->enable_backface_culling)
|
|
|
|
enable_flags |= COGL_ENABLE_BACKFACE_CULLING;
|
|
|
|
|
2010-03-19 05:16:08 -04:00
|
|
|
_cogl_enable (enable_flags);
|
2009-10-22 14:01:52 -04:00
|
|
|
_cogl_flush_face_winding ();
|
2010-04-26 05:01:43 -04:00
|
|
|
|
|
|
|
return source;
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-04-26 05:01:43 -04:00
|
|
|
disable_state_for_drawing_buffer (CoglVertexBuffer *buffer,
|
|
|
|
CoglHandle source)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
GLenum gl_type;
|
2009-03-23 08:46:20 -04:00
|
|
|
#ifdef MAY_HAVE_PROGRAMABLE_GL
|
2008-11-10 13:53:14 -05:00
|
|
|
GLuint generic_index = 0;
|
2009-03-23 08:46:20 -04:00
|
|
|
#endif
|
2008-11-10 13:53:14 -05:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2010-04-26 05:01:43 -04:00
|
|
|
if (G_UNLIKELY (source != ctx->source_material))
|
|
|
|
cogl_handle_unref (source);
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
/* Disable all the client state that cogl doesn't currently know
|
|
|
|
* about:
|
|
|
|
*/
|
2009-04-15 14:25:55 -04:00
|
|
|
if (cogl_get_features () & COGL_FEATURE_VBOS)
|
|
|
|
GE (glBindBuffer (GL_ARRAY_BUFFER, 0));
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
for (tmp = buffer->submitted_vbos; tmp != NULL; tmp = tmp->next)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferVBO *cogl_vbo = tmp->data;
|
2008-11-10 13:53:14 -05:00
|
|
|
GList *tmp2;
|
|
|
|
|
|
|
|
for (tmp2 = cogl_vbo->attributes; tmp2 != NULL; tmp2 = tmp2->next)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBufferAttrib *attribute = tmp2->data;
|
|
|
|
CoglVertexBufferAttribFlags type =
|
|
|
|
attribute->flags & COGL_VERTEX_BUFFER_ATTRIB_FLAG_TYPE_MASK;
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
if (!(attribute->flags & COGL_VERTEX_BUFFER_ATTRIB_FLAG_ENABLED))
|
2008-11-10 13:53:14 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
gl_type = get_gl_type_from_attribute_flags(attribute->flags);
|
|
|
|
switch (type)
|
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_COLOR_ARRAY:
|
2009-02-06 11:10:28 -05:00
|
|
|
/* GE (glDisableClientState (GL_COLOR_ARRAY)); */
|
2008-11-10 13:53:14 -05:00
|
|
|
break;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_NORMAL_ARRAY:
|
2008-11-10 13:53:14 -05:00
|
|
|
/* FIXME: go through cogl cache to enable normal array */
|
|
|
|
GE (glDisableClientState (GL_NORMAL_ARRAY));
|
|
|
|
break;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_TEXTURE_COORD_ARRAY:
|
2010-05-27 09:54:14 -04:00
|
|
|
/* The enabled state of the texture coord arrays is
|
|
|
|
cached in ctx->enabled_texcoord_arrays so we don't
|
|
|
|
need to do anything here. The array will be disabled
|
|
|
|
by the next drawing primitive if it is not
|
|
|
|
required */
|
2008-11-10 13:53:14 -05:00
|
|
|
break;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_VERTEX_ARRAY:
|
2008-11-10 13:53:14 -05:00
|
|
|
/* GE (glDisableClientState (GL_VERTEX_ARRAY)); */
|
|
|
|
break;
|
2009-01-20 16:12:44 -05:00
|
|
|
case COGL_VERTEX_BUFFER_ATTRIB_FLAG_CUSTOM_ARRAY:
|
2008-11-10 13:53:14 -05:00
|
|
|
#ifdef MAY_HAVE_PROGRAMABLE_GL
|
|
|
|
/* FIXME: go through cogl cache to enable generic array */
|
|
|
|
GE (glDisableVertexAttribArray (generic_index++));
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_warning ("Unrecognised attribute type 0x%08x", type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-24 06:33:29 -04:00
|
|
|
cogl_vertex_buffer_draw (CoglHandle handle,
|
|
|
|
CoglVerticesMode mode,
|
|
|
|
int first,
|
|
|
|
int count)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBuffer *buffer;
|
2010-04-26 05:01:43 -04:00
|
|
|
CoglHandle source;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
|
|
|
if (!cogl_is_vertex_buffer (handle))
|
2008-11-10 13:53:14 -05:00
|
|
|
return;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
_cogl_journal_flush ();
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
buffer = _cogl_vertex_buffer_pointer_from_handle (handle);
|
|
|
|
|
2010-04-26 05:01:43 -04:00
|
|
|
source = enable_state_for_drawing_buffer (buffer);
|
2009-02-24 13:51:25 -05:00
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
GE (glDrawArrays (mode, first, count));
|
2009-01-20 16:12:44 -05:00
|
|
|
|
2010-04-26 05:01:43 -04:00
|
|
|
disable_state_for_drawing_buffer (buffer, source);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
2009-05-23 23:04:38 -04:00
|
|
|
static int
|
|
|
|
get_indices_type_size (GLuint indices_type)
|
|
|
|
{
|
|
|
|
if (indices_type == GL_UNSIGNED_BYTE)
|
|
|
|
return sizeof (GLubyte);
|
2010-02-23 09:45:44 -05:00
|
|
|
else if (indices_type == GL_UNSIGNED_SHORT)
|
2009-05-23 23:04:38 -04:00
|
|
|
return sizeof (GLushort);
|
2010-02-23 09:45:44 -05:00
|
|
|
else if (indices_type == GL_UNSIGNED_INT)
|
|
|
|
return sizeof (GLuint);
|
2009-05-23 23:04:38 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
g_critical ("Unknown indices type %d\n", indices_type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-28 07:49:29 -04:00
|
|
|
CoglHandle
|
|
|
|
cogl_vertex_buffer_indices_new (CoglIndicesType indices_type,
|
2009-05-24 06:33:29 -04:00
|
|
|
const void *indices_array,
|
|
|
|
int indices_len)
|
2009-05-23 23:04:38 -04:00
|
|
|
{
|
|
|
|
gboolean fallback =
|
|
|
|
(cogl_get_features () & COGL_FEATURE_VBOS) ? FALSE : TRUE;
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
gsize indices_bytes;
|
2009-05-23 23:04:38 -04:00
|
|
|
CoglVertexBufferIndices *indices;
|
|
|
|
|
2010-05-27 03:28:29 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NULL);
|
2009-05-23 23:04:38 -04:00
|
|
|
|
|
|
|
indices = g_slice_alloc (sizeof (CoglVertexBufferIndices));
|
|
|
|
|
|
|
|
if (indices_type == COGL_INDICES_TYPE_UNSIGNED_BYTE)
|
|
|
|
indices->type = GL_UNSIGNED_BYTE;
|
|
|
|
else if (indices_type == COGL_INDICES_TYPE_UNSIGNED_SHORT)
|
|
|
|
indices->type = GL_UNSIGNED_SHORT;
|
2010-02-23 09:45:44 -05:00
|
|
|
else if (indices_type == COGL_INDICES_TYPE_UNSIGNED_INT)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (cogl_features_available
|
|
|
|
(COGL_FEATURE_UNSIGNED_INT_INDICES),
|
|
|
|
COGL_INVALID_HANDLE);
|
|
|
|
|
|
|
|
indices->type = GL_UNSIGNED_INT;
|
|
|
|
}
|
2009-05-23 23:04:38 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
g_critical ("unknown indices type %d", indices_type);
|
|
|
|
g_slice_free (CoglVertexBufferIndices, indices);
|
2010-05-27 03:28:29 -04:00
|
|
|
return NULL;
|
2009-05-23 23:04:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
indices_bytes = get_indices_type_size (indices->type) * indices_len;
|
|
|
|
if (fallback)
|
|
|
|
{
|
2010-02-17 17:16:17 -05:00
|
|
|
indices->vbo_name = g_malloc (indices_bytes);
|
2009-05-23 23:04:38 -04:00
|
|
|
memcpy (indices->vbo_name, indices_array, indices_bytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GE (glGenBuffers (1, (GLuint *)&indices->vbo_name));
|
|
|
|
GE (glBindBuffer (GL_ELEMENT_ARRAY_BUFFER,
|
|
|
|
GPOINTER_TO_UINT (indices->vbo_name)));
|
|
|
|
GE (glBufferData (GL_ELEMENT_ARRAY_BUFFER,
|
|
|
|
indices_bytes,
|
|
|
|
indices_array,
|
|
|
|
GL_STATIC_DRAW));
|
|
|
|
GE (glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0));
|
|
|
|
}
|
|
|
|
|
2009-05-28 07:49:29 -04:00
|
|
|
return _cogl_vertex_buffer_indices_handle_new (indices);
|
2009-05-26 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2009-06-17 10:03:33 -04:00
|
|
|
CoglIndicesType
|
|
|
|
cogl_vertex_buffer_indices_get_type (CoglHandle indices_handle)
|
|
|
|
{
|
|
|
|
CoglVertexBufferIndices *indices = NULL;
|
|
|
|
|
|
|
|
if (!cogl_is_vertex_buffer_indices (indices_handle))
|
|
|
|
return COGL_INDICES_TYPE_UNSIGNED_SHORT;
|
|
|
|
|
|
|
|
indices = _cogl_vertex_buffer_indices_pointer_from_handle (indices_handle);
|
|
|
|
|
|
|
|
if (indices->type == GL_UNSIGNED_BYTE)
|
|
|
|
return COGL_INDICES_TYPE_UNSIGNED_BYTE;
|
|
|
|
else if (indices->type == GL_UNSIGNED_SHORT)
|
|
|
|
return COGL_INDICES_TYPE_UNSIGNED_SHORT;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_critical ("unknown indices type %d", indices->type);
|
|
|
|
return COGL_INDICES_TYPE_UNSIGNED_SHORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-26 11:27:36 -04:00
|
|
|
void
|
2009-05-28 07:49:29 -04:00
|
|
|
_cogl_vertex_buffer_indices_free (CoglVertexBufferIndices *indices)
|
2009-05-26 11:27:36 -04:00
|
|
|
{
|
2009-05-28 07:49:29 -04:00
|
|
|
gboolean fallback =
|
|
|
|
(cogl_get_features () & COGL_FEATURE_VBOS) ? FALSE : TRUE;
|
2009-05-26 11:27:36 -04:00
|
|
|
|
2009-05-28 07:49:29 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2009-05-26 11:27:36 -04:00
|
|
|
|
2009-05-28 07:49:29 -04:00
|
|
|
if (fallback)
|
|
|
|
g_free (indices->vbo_name);
|
|
|
|
else
|
|
|
|
GE (glDeleteBuffers (1, (GLuint *)&indices->vbo_name));
|
2009-05-26 11:27:36 -04:00
|
|
|
|
2009-05-28 07:49:29 -04:00
|
|
|
g_slice_free (CoglVertexBufferIndices, indices);
|
2009-05-23 23:04:38 -04:00
|
|
|
}
|
|
|
|
|
2008-11-10 13:53:14 -05:00
|
|
|
void
|
2009-05-24 06:33:29 -04:00
|
|
|
cogl_vertex_buffer_draw_elements (CoglHandle handle,
|
|
|
|
CoglVerticesMode mode,
|
2009-05-28 07:49:29 -04:00
|
|
|
CoglHandle indices_handle,
|
|
|
|
int min_index,
|
|
|
|
int max_index,
|
2009-05-24 06:33:29 -04:00
|
|
|
int indices_offset,
|
|
|
|
int count)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
2009-01-20 16:12:44 -05:00
|
|
|
CoglVertexBuffer *buffer;
|
2009-05-23 23:04:38 -04:00
|
|
|
gboolean fallback =
|
|
|
|
(cogl_get_features () & COGL_FEATURE_VBOS) ? FALSE : TRUE;
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
gsize byte_offset;
|
2009-05-23 23:04:38 -04:00
|
|
|
CoglVertexBufferIndices *indices = NULL;
|
2010-04-26 05:01:43 -04:00
|
|
|
CoglHandle source;
|
2008-11-21 11:18:58 -05:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2009-01-20 16:12:44 -05:00
|
|
|
|
|
|
|
if (!cogl_is_vertex_buffer (handle))
|
2008-11-10 13:53:14 -05:00
|
|
|
return;
|
2009-01-20 16:12:44 -05:00
|
|
|
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
_cogl_journal_flush ();
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
buffer = _cogl_vertex_buffer_pointer_from_handle (handle);
|
|
|
|
|
2009-05-28 07:49:29 -04:00
|
|
|
if (!cogl_is_vertex_buffer_indices (indices_handle))
|
|
|
|
return;
|
|
|
|
|
|
|
|
indices = _cogl_vertex_buffer_indices_pointer_from_handle (indices_handle);
|
|
|
|
|
2010-04-26 05:01:43 -04:00
|
|
|
source = enable_state_for_drawing_buffer (buffer);
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2009-05-23 23:04:38 -04:00
|
|
|
byte_offset = indices_offset * get_indices_type_size (indices->type);
|
|
|
|
if (fallback)
|
|
|
|
byte_offset = (size_t)(((char *)indices->vbo_name) + byte_offset);
|
|
|
|
else
|
|
|
|
GE (glBindBuffer (GL_ELEMENT_ARRAY_BUFFER,
|
|
|
|
GPOINTER_TO_UINT (indices->vbo_name)));
|
2009-02-24 13:51:25 -05:00
|
|
|
|
2009-05-28 07:49:29 -04:00
|
|
|
GE (glDrawRangeElements (mode, min_index, max_index,
|
2009-05-23 23:04:38 -04:00
|
|
|
count, indices->type, (void *)byte_offset));
|
2008-11-10 13:53:14 -05:00
|
|
|
|
2010-04-26 05:01:43 -04:00
|
|
|
disable_state_for_drawing_buffer (buffer, source);
|
2009-05-23 23:04:38 -04:00
|
|
|
|
|
|
|
GE (glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0));
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-01-20 16:12:44 -05:00
|
|
|
_cogl_vertex_buffer_free (CoglVertexBuffer *buffer)
|
2008-11-10 13:53:14 -05:00
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
for (tmp = buffer->submitted_vbos; tmp != NULL; tmp = tmp->next)
|
|
|
|
cogl_vertex_buffer_vbo_free (tmp->data, TRUE);
|
2009-05-23 23:04:38 -04:00
|
|
|
g_list_free (buffer->submitted_vbos);
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
for (tmp = buffer->new_attributes; tmp != NULL; tmp = tmp->next)
|
|
|
|
cogl_vertex_buffer_attribute_free (tmp->data);
|
2009-05-23 23:04:38 -04:00
|
|
|
g_list_free (buffer->new_attributes);
|
|
|
|
|
2009-01-20 16:12:44 -05:00
|
|
|
g_slice_free (CoglVertexBuffer, buffer);
|
2008-11-10 13:53:14 -05:00
|
|
|
}
|
2009-05-23 23:04:38 -04:00
|
|
|
|
2009-05-28 08:47:18 -04:00
|
|
|
CoglHandle
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
cogl_vertex_buffer_indices_get_for_quads (unsigned int n_indices)
|
2009-05-28 08:47:18 -04:00
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, COGL_INVALID_HANDLE);
|
|
|
|
|
|
|
|
/* Check if the indices would fit in a byte array */
|
|
|
|
if (n_indices <= 256 / 4 * 6)
|
|
|
|
{
|
|
|
|
/* Generate the byte array if we haven't already */
|
|
|
|
if (ctx->quad_indices_byte == COGL_INVALID_HANDLE)
|
|
|
|
{
|
|
|
|
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->quad_indices_byte
|
|
|
|
= cogl_vertex_buffer_indices_new (COGL_INDICES_TYPE_UNSIGNED_BYTE,
|
|
|
|
byte_array,
|
|
|
|
256 / 4 * 6);
|
|
|
|
|
|
|
|
g_free (byte_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx->quad_indices_byte;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ctx->quad_indices_short_len < n_indices)
|
|
|
|
{
|
|
|
|
guint16 *short_array;
|
|
|
|
guint16 *p;
|
|
|
|
int i, vert_num = 0;
|
|
|
|
|
|
|
|
if (ctx->quad_indices_short != COGL_INVALID_HANDLE)
|
|
|
|
cogl_handle_unref (ctx->quad_indices_short);
|
|
|
|
/* Pick a power of two >= MAX (512, n_indices) */
|
|
|
|
if (ctx->quad_indices_short_len == 0)
|
|
|
|
ctx->quad_indices_short_len = 512;
|
|
|
|
while (ctx->quad_indices_short_len < n_indices)
|
|
|
|
ctx->quad_indices_short_len *= 2;
|
|
|
|
|
|
|
|
/* Over-allocate to generate a whole number of quads */
|
|
|
|
p = short_array = g_malloc ((ctx->quad_indices_short_len
|
|
|
|
+ 5) / 6 * 6
|
|
|
|
* sizeof (guint16));
|
|
|
|
|
|
|
|
/* Fill in the complete quads */
|
|
|
|
for (i = 0; i < ctx->quad_indices_short_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->quad_indices_short
|
|
|
|
= cogl_vertex_buffer_indices_new (COGL_INDICES_TYPE_UNSIGNED_SHORT,
|
|
|
|
short_array,
|
|
|
|
ctx->quad_indices_short_len);
|
|
|
|
|
|
|
|
g_free (short_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx->quad_indices_short;
|
|
|
|
}
|
|
|
|
}
|