Use GL_QUADS for flushing a quad batch

Instead of using GL_TRIANGLES and uploading the indices every time, it
now uses GL_QUADS instead on OpenGL. Under GLES it still uses indices
but it uses the new cogl_vertex_buffer_indices_get_for_quads function
to avoid uploading the vertices every time.

This requires the _cogl_vertex_buffer_indices_pointer_from_handle
function to be exposed privately to the rest of Cogl.

The static_indices array has been removed from the Cogl context.
This commit is contained in:
Neil Roberts 2009-06-01 17:10:22 +01:00
parent 39cb36ba01
commit 9d41a27f61
6 changed files with 25 additions and 58 deletions

View File

@ -30,6 +30,7 @@
#include "cogl-context.h" #include "cogl-context.h"
#include "cogl-texture-private.h" #include "cogl-texture-private.h"
#include "cogl-material-private.h" #include "cogl-material-private.h"
#include "cogl-vertex-buffer-private.h"
#include <string.h> #include <string.h>
#include <gmodule.h> #include <gmodule.h>
@ -39,16 +40,8 @@
#ifdef HAVE_COGL_GL #ifdef HAVE_COGL_GL
#define glDrawRangeElements ctx->pf_glDrawRangeElements
#define glClientActiveTexture ctx->pf_glClientActiveTexture #define glClientActiveTexture ctx->pf_glClientActiveTexture
#else
/* GLES doesn't have glDrawRangeElements, so we simply pretend it does
* but that it makes no use of the start, end constraints: */
#define glDrawRangeElements(mode, start, end, count, type, indices) \
glDrawElements (mode, count, type, indices)
#endif #endif
/* these are defined in the particular backend */ /* these are defined in the particular backend */
@ -63,7 +56,6 @@ _cogl_journal_flush_quad_batch (CoglJournalEntry *batch_start,
gint batch_len, gint batch_len,
GLfloat *vertex_pointer) GLfloat *vertex_pointer)
{ {
int needed_indices;
gsize stride; gsize stride;
int i; int i;
gulong enable_flags = 0; gulong enable_flags = 0;
@ -122,52 +114,31 @@ _cogl_journal_flush_quad_batch (CoglJournalEntry *batch_start,
GE (glVertexPointer (2, GL_FLOAT, stride, vertex_pointer)); GE (glVertexPointer (2, GL_FLOAT, stride, vertex_pointer));
_cogl_current_matrix_state_flush (); _cogl_current_matrix_state_flush ();
#ifdef HAVE_COGL_GL
if (batch_len > 1) GE( glDrawArrays (GL_QUADS, 0, batch_len * 4) );
#else /* HAVE_COGL_GL */
/* GLES doesn't support GL_QUADS so we will use GL_TRIANGLES and
indices */
{ {
/* The indices are always the same sequence regardless of the vertices so int needed_indices = batch_len * 6;
* we only need to change it if there are more vertices than ever before. CoglHandle indices_handle
*/ = cogl_vertex_buffer_indices_get_for_quads (needed_indices);
needed_indices = batch_len * 6; CoglVertexBufferIndices *indices
if (needed_indices > ctx->static_indices->len) = _cogl_vertex_buffer_indices_pointer_from_handle (indices_handle);
{
int old_len = ctx->static_indices->len;
int vert_num = old_len / 6 * 4;
GLushort *q;
/* Add two triangles for each quad to the list of GE (glBindBuffer (GL_ELEMENT_ARRAY_BUFFER,
indices. That makes six new indices but two of the GPOINTER_TO_UINT (indices->vbo_name)));
vertices in the triangles are shared. */ GE (glDrawElements (GL_TRIANGLES,
g_array_set_size (ctx->static_indices, needed_indices);
q = &g_array_index (ctx->static_indices, GLushort, old_len);
for (i = old_len;
i < ctx->static_indices->len;
i += 6, vert_num += 4)
{
*(q++) = vert_num + 0;
*(q++) = vert_num + 1;
*(q++) = vert_num + 3;
*(q++) = vert_num + 1;
*(q++) = vert_num + 2;
*(q++) = vert_num + 3;
}
}
GE (glDrawRangeElements (GL_TRIANGLES,
0, ctx->static_indices->len - 1,
6 * batch_len, 6 * batch_len,
GL_UNSIGNED_SHORT, indices->type,
ctx->static_indices->data)); NULL));
} GE (glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0));
else
{
GE (glDrawArrays (GL_TRIANGLE_FAN,
0, /* first */
4)); /* n vertices */
} }
#endif /* HAVE_COGL_GL */
/* DEBUGGING CODE XXX: /* DEBUGGING CODE XXX:
* This path will cause all rectangles to be drawn with a red, green * This path will cause all rectangles to be drawn with a red, green

View File

@ -161,5 +161,9 @@ typedef struct _CoglVertexBuffer
} CoglVertexBuffer; } CoglVertexBuffer;
CoglVertexBuffer *_cogl_vertex_buffer_pointer_from_handle (CoglHandle handle);
CoglVertexBufferIndices *
_cogl_vertex_buffer_indices_pointer_from_handle (CoglHandle handle);
#endif /* __COGL_VERTEX_BUFFER_H */ #endif /* __COGL_VERTEX_BUFFER_H */

View File

@ -69,7 +69,6 @@ cogl_create_context ()
_context->journal = g_array_new (FALSE, FALSE, sizeof (CoglJournalEntry)); _context->journal = g_array_new (FALSE, FALSE, sizeof (CoglJournalEntry));
_context->logged_vertices = g_array_new (FALSE, FALSE, sizeof (GLfloat)); _context->logged_vertices = g_array_new (FALSE, FALSE, sizeof (GLfloat));
_context->static_indices = g_array_new (FALSE, FALSE, sizeof (GLushort));
_context->polygon_vertices = g_array_new (FALSE, FALSE, _context->polygon_vertices = g_array_new (FALSE, FALSE,
sizeof (CoglTextureGLVertex)); sizeof (CoglTextureGLVertex));
@ -204,8 +203,6 @@ _cogl_destroy_context ()
if (_context->logged_vertices) if (_context->logged_vertices)
g_array_free (_context->logged_vertices, TRUE); g_array_free (_context->logged_vertices, TRUE);
if (_context->static_indices)
g_array_free (_context->static_indices, TRUE);
if (_context->polygon_vertices) if (_context->polygon_vertices)
g_array_free (_context->polygon_vertices, TRUE); g_array_free (_context->polygon_vertices, TRUE);
if (_context->current_layers) if (_context->current_layers)

View File

@ -78,7 +78,6 @@ typedef struct
* can batch things together. */ * can batch things together. */
GArray *journal; GArray *journal;
GArray *logged_vertices; GArray *logged_vertices;
GArray *static_indices;
GArray *polygon_vertices; GArray *polygon_vertices;
/* Some simple caching, to minimize state changes... */ /* Some simple caching, to minimize state changes... */

View File

@ -72,7 +72,6 @@ cogl_create_context ()
_context->journal = g_array_new (FALSE, FALSE, sizeof (CoglJournalEntry)); _context->journal = g_array_new (FALSE, FALSE, sizeof (CoglJournalEntry));
_context->logged_vertices = g_array_new (FALSE, FALSE, sizeof (GLfloat)); _context->logged_vertices = g_array_new (FALSE, FALSE, sizeof (GLfloat));
_context->static_indices = g_array_new (FALSE, FALSE, sizeof (GLushort));
_context->polygon_vertices = g_array_new (FALSE, FALSE, _context->polygon_vertices = g_array_new (FALSE, FALSE,
sizeof (CoglTextureGLVertex)); sizeof (CoglTextureGLVertex));
@ -162,8 +161,6 @@ _cogl_destroy_context ()
if (_context->logged_vertices) if (_context->logged_vertices)
g_array_free (_context->logged_vertices, TRUE); g_array_free (_context->logged_vertices, TRUE);
if (_context->static_indices)
g_array_free (_context->static_indices, TRUE);
if (_context->polygon_vertices) if (_context->polygon_vertices)
g_array_free (_context->polygon_vertices, TRUE); g_array_free (_context->polygon_vertices, TRUE);
if (_context->current_layers) if (_context->current_layers)

View File

@ -80,7 +80,6 @@ typedef struct
* can batch things together. */ * can batch things together. */
GArray *journal; GArray *journal;
GArray *logged_vertices; GArray *logged_vertices;
GArray *static_indices;
GArray *polygon_vertices; GArray *polygon_vertices;
/* Some simple caching, to minimize state changes... */ /* Some simple caching, to minimize state changes... */