From f386b1f002f111210e87e6f72eb1795ad2befc1e Mon Sep 17 00:00:00 2001
From: Robert Bragg <robert@linux.intel.com>
Date: Mon, 22 Jun 2009 00:11:41 +0100
Subject: [PATCH] [journal] Don't resize a singlton VBO; create and destroy a
 VBO each flush

This simplifies the vertex data uploading in the journal, and could improve
performance.  Modifying a VBO mid-scene could reqire synchronizing with the
GPU or some form of shadowing/copying to avoid modifying data that the GPU
is currently processing; the buffer was also being marked as GL_STATIC_DRAW
which could have made things worse.

Now we simply create a GL_STATIC_DRAW VBO for each flush and and delete it
when we are finished.
---
 clutter/cogl/common/cogl-primitives.c | 40 ++++++++-------------------
 1 file changed, 12 insertions(+), 28 deletions(-)

diff --git a/clutter/cogl/common/cogl-primitives.c b/clutter/cogl/common/cogl-primitives.c
index 2bf358778..dcef4547f 100644
--- a/clutter/cogl/common/cogl-primitives.c
+++ b/clutter/cogl/common/cogl-primitives.c
@@ -473,10 +473,6 @@ _cogl_journal_flush_vbo_offsets_and_entries (CoglJournalEntry *batch_start,
                   _cogl_journal_flush_texcoord_vbo_offsets_and_entries,
                   data);
 
-#ifndef HAVE_COGL_GL
-  GE (glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0));
-#endif
-
   /* progress forward through the VBO containing all our vertices */
   state->vbo_offset += (stride * 4 * batch_len);
   if (G_UNLIKELY (cogl_debug_flags & COGL_DEBUG_JOURNAL))
@@ -507,30 +503,15 @@ upload_vertices_to_vbo (GArray *vertices, CoglJournalFlushState *state)
   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
   needed_vbo_len = vertices->len * sizeof (GLfloat);
-  if (ctx->journal_vbo_len < needed_vbo_len)
-    {
-      GE (glDeleteBuffers (1, &ctx->journal_vbo));
 
-      GE (glGenBuffers (1, &ctx->journal_vbo));
-      GE (glBindBuffer (GL_ARRAY_BUFFER, ctx->journal_vbo));
-      GE (glBufferData (GL_ARRAY_BUFFER,
-                        needed_vbo_len,
-                        vertices->data,
-                        GL_STATIC_DRAW));
-      ctx->journal_vbo_len = needed_vbo_len;
-    }
-  else
-    {
-      GE (glBindBuffer (GL_ARRAY_BUFFER, ctx->journal_vbo));
-      GE (glBufferData (GL_ARRAY_BUFFER,
-                        needed_vbo_len,
-                        NULL,
-                        GL_STATIC_DRAW));
-      GE (glBufferSubData (GL_ARRAY_BUFFER,
-                           0,
-                           needed_vbo_len,
-                           vertices->data));
-    }
+  g_assert (ctx->journal_vbo == 0);
+  g_assert (needed_vbo_len);
+  GE (glGenBuffers (1, &ctx->journal_vbo));
+  GE (glBindBuffer (GL_ARRAY_BUFFER, ctx->journal_vbo));
+  GE (glBufferData (GL_ARRAY_BUFFER,
+                    needed_vbo_len,
+                    vertices->data,
+                    GL_STATIC_DRAW));
 
   /* As we flush the journal entries in batches we walk forward through the
    * above VBO starting at offset 0... */
@@ -613,7 +594,10 @@ _cogl_journal_flush (void)
     }
 
   if (!vbo_fallback)
-    GE (glBindBuffer (GL_ARRAY_BUFFER, 0));
+    {
+      GE (glDeleteBuffers (1, &ctx->journal_vbo));
+      ctx->journal_vbo = 0;
+    }
 
   g_array_set_size (ctx->journal, 0);
   g_array_set_size (ctx->logged_vertices, 0);