[Piglit] [PATCH] primitive-restart: test primitive-restart with VBO

Jordan Justen jordan.l.justen at intel.com
Wed May 9 16:46:42 PDT 2012


Each of the glArrayElement/glDrawElements tests are now run with
and without enabling VBO.

Signed-off-by: Jordan Justen <jordan.l.justen at intel.com>
---
 tests/general/primitive-restart.c |  256 ++++++++++++++++++++++++-------------
 1 file changed, 170 insertions(+), 86 deletions(-)

diff --git a/tests/general/primitive-restart.c b/tests/general/primitive-restart.c
index 3642ffa..1fe401d 100644
--- a/tests/general/primitive-restart.c
+++ b/tests/general/primitive-restart.c
@@ -45,6 +45,8 @@ static GLboolean Have_NV;
 static GLboolean Have_31;
 static GLboolean TestGL31;
 
+#define VBO_ON GL_TRUE
+#define VBO_OFF GL_FALSE
 
 static GLboolean
 check_rendering(void)
@@ -184,33 +186,71 @@ disable_restart(void)
 }
 
 
-static void draw_all_indices(GLboolean one_by_one,
-                             GLenum mode, GLsizei count,
+static GLuint type_size(GLenum type)
+{
+   switch (type) {
+   case GL_UNSIGNED_BYTE:
+      return sizeof(GLubyte);
+   case GL_UNSIGNED_SHORT:
+      return sizeof(GLushort);
+   case GL_UNSIGNED_INT:
+      return sizeof(GLuint);
+   default:
+      assert(0);
+      return 0;
+   }
+}
+
+
+static GLuint type_array_size(GLenum type, GLuint length)
+{
+   return length * type_size(type);
+}
+
+
+static GLuint read_index_value(const GLvoid *indices, GLenum type, GLuint index)
+{
+   switch (type) {
+   case GL_UNSIGNED_BYTE:
+      return (GLuint)((GLubyte*)indices)[index];
+   case GL_UNSIGNED_SHORT:
+      return (GLuint)((GLushort*)indices)[index];
+   case GL_UNSIGNED_INT:
+      return ((GLuint*)indices)[index];
+   default:
+      assert(0);
+   }
+}
+
+
+static void write_index_value(const GLvoid *indices, GLenum type, GLuint index, GLuint value)
+{
+   switch (type) {
+   case GL_UNSIGNED_BYTE:
+      ((GLubyte*)indices)[index] = (GLubyte) value;
+      break;
+   case GL_UNSIGNED_SHORT:
+      ((GLushort*)indices)[index] = (GLushort) value;
+      break;
+   case GL_UNSIGNED_INT:
+      ((GLuint*)indices)[index] = value;
+      break;
+   default:
+      assert(0);
+   }
+}
+
+
+static void do_ArrayElement(GLenum mode, GLsizei count,
                              GLenum type, const GLvoid *indices)
 {
    GLuint index;
 
-   if (one_by_one) {
-      glBegin(mode);
-      for (index = 0; index < count; index++) {
-         switch (type) {
-         case GL_UNSIGNED_BYTE:
-            glArrayElement(((GLubyte*)indices)[index]);
-            break;
-         case GL_UNSIGNED_SHORT:
-            glArrayElement(((GLushort*)indices)[index]);
-            break;
-         case GL_UNSIGNED_INT:
-            glArrayElement(((GLuint*)indices)[index]);
-            break;
-         default:
-            assert(0);
-         }
-      }
-      glEnd();
-   } else {
-      glDrawElements(mode, count, type, indices);
+   glBegin(mode);
+   for (index = 0; index < count; index++) {
+      glArrayElement(read_index_value(indices, type, index));
    }
+   glEnd();
 }
 
 
@@ -218,17 +258,22 @@ static void draw_all_indices(GLboolean one_by_one,
  * Test glDrawElements() with glPrimitiveRestartIndexNV().
  */
 static GLboolean
-test_draw_by_index(GLboolean one_by_one, GLenum primMode, GLenum indexType)
+test_draw_by_index(GLboolean one_by_one, GLboolean use_vbo,
+                   GLenum primMode, GLenum indexType)
 {
 #define NUM_VERTS 48
 #define NUM_ELEMS (NUM_VERTS * 5 / 4)
    GLfloat verts[NUM_VERTS+2][2];
    GLuint elements[NUM_ELEMS];
+   GLubyte indicies_buf[sizeof(elements)];
    GLfloat x, dx;
    GLuint restart_index;
    GLuint num_elems;
    GLboolean pass;
    const char *typeStr = NULL, *primStr = NULL;
+   GLuint vbo;
+   uintptr_t index_offset = sizeof(verts);
+   GLuint i, j;
 
    switch (indexType) {
    case GL_UNSIGNED_BYTE:
@@ -251,7 +296,6 @@ test_draw_by_index(GLboolean one_by_one, GLenum primMode, GLenum indexType)
    dx = 20.0;
 
    if (primMode == GL_TRIANGLE_STRIP) {
-      GLuint i, j;
       const GLfloat y = 0.5 * piglit_height - 10.0, dy = 20.0;
       for (i = 0; i < NUM_VERTS / 2; i++) {
          verts[i*2+0][0] = x;
@@ -263,16 +307,20 @@ test_draw_by_index(GLboolean one_by_one, GLenum primMode, GLenum indexType)
 
       /* setup elements to draw series of squares w/ tri strip */
       for (i = j = 0; i < NUM_VERTS; i++) {
-         elements[j++] = i;
-         if (i > 0 && i % 4 == 3)
-            elements[j++] = restart_index;
+         elements[j] = i;
+         write_index_value(indicies_buf, indexType, j, i);
+         j++;
+         if (i > 0 && i % 4 == 3) {
+            elements[j] = restart_index;
+            write_index_value(indicies_buf, indexType, j, restart_index);
+            j++;
+         }
       }
 
       num_elems = j;
       primStr = "GL_TRIANGLE_STRIP";
    }
    else {
-      GLuint i, j;
       const GLfloat y = 0.5 * piglit_height;
 
       assert(primMode == GL_LINE_STRIP);
@@ -287,9 +335,14 @@ test_draw_by_index(GLboolean one_by_one, GLenum primMode, GLenum indexType)
 
       /* setup elements to draw series of disjoint lines w/ line strip */
       for (i = j = 0; i < NUM_VERTS / 2; i++) {
-         elements[j++] = i;
-         if (i > 0 && i % 2 == 1)
-            elements[j++] = restart_index;
+         elements[j] = i;
+         write_index_value(indicies_buf, indexType, j, i);
+         j++;
+         if (i > 0 && i % 2 == 1) {
+            elements[j] = restart_index;
+            write_index_value(indicies_buf, indexType, j, restart_index);
+            j++;
+         }
       }
 
       num_elems = j;
@@ -311,48 +364,55 @@ test_draw_by_index(GLboolean one_by_one, GLenum primMode, GLenum indexType)
 
    glColor4fv(green);
 
-   glVertexPointer(2, GL_FLOAT, 2*sizeof(GLfloat), verts);
+   if (use_vbo) {
+      glGenBuffers(1, &vbo);
+      glBindBuffer(GL_ARRAY_BUFFER, vbo);
+      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo);
+      glBufferData(GL_ARRAY_BUFFER,
+                   index_offset + sizeof(elements),
+                   NULL, GL_STATIC_DRAW);
+      glBufferSubData(GL_ARRAY_BUFFER,
+                      0, sizeof(verts),
+                      verts);
+      glBufferSubData(GL_ELEMENT_ARRAY_BUFFER,
+                      index_offset, type_array_size(indexType, num_elems),
+                      indicies_buf);
+      glVertexPointer(2, GL_FLOAT, 0, (void *)0);
+   } else {
+      glVertexPointer(2, GL_FLOAT, 0, (void *)verts);
+   }
+
    glEnableClientState(GL_VERTEX_ARRAY);
 
    assert(glGetError()==0);
    enable_restart(restart_index);
 
    /* Draw */
-   switch (indexType) {
-   case GL_UNSIGNED_BYTE:
-      {
-         GLubyte ub_elements[NUM_ELEMS];
-         int i;
-         for (i = 0; i < num_elems; i++)
-            ub_elements[i] = (GLubyte) elements[i];
-         draw_all_indices(one_by_one, primMode, num_elems, GL_UNSIGNED_BYTE, ub_elements);
-      }
-      break;
-   case GL_UNSIGNED_SHORT:
-      {
-         GLushort us_elements[NUM_ELEMS];
-         int i;
-         for (i = 0; i < num_elems; i++)
-            us_elements[i] = (GLushort) elements[i];
-         draw_all_indices(one_by_one, primMode, num_elems, GL_UNSIGNED_SHORT, us_elements);
-      }
-      break;
-   case GL_UNSIGNED_INT:
-      draw_all_indices(one_by_one, primMode, num_elems, GL_UNSIGNED_INT, elements);
-      break;
-   default:
-      assert(0);
+   if (one_by_one) {
+      do_ArrayElement(primMode, num_elems, indexType, indicies_buf);
+   } else if (use_vbo) {
+      glDrawElements(primMode, num_elems, indexType, (void*) index_offset);
+   } else {
+      glDrawElements(primMode, num_elems, indexType, indicies_buf);
    }
 
    disable_restart();
+
    glDisableClientState(GL_VERTEX_ARRAY);
 
+   if (use_vbo) {
+      glBindBuffer(GL_ARRAY_BUFFER, 0);
+      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+      glDeleteBuffers(1, &vbo);
+   }
+
    pass = check_rendering();
    if (!pass) {
-      fprintf(stderr, "%s: failure drawing with %s(%s, %s)\n",
+      fprintf(stderr, "%s: failure drawing with %s(%s, %s), VBO=%s\n",
               TestName,
               one_by_one ? "glArrayElement" : "glDrawElements",
-              primStr, typeStr);
+              primStr, typeStr,
+              use_vbo ? "on" : "off");
    }
 
    piglit_present_results();
@@ -366,9 +426,9 @@ test_draw_by_index(GLboolean one_by_one, GLenum primMode, GLenum indexType)
  * Test glDrawElements() with glPrimitiveRestartIndexNV().
  */
 static GLboolean
-test_draw_elements(GLenum primMode, GLenum indexType)
+test_draw_elements(GLboolean use_vbo, GLenum primMode, GLenum indexType)
 {
-   return test_draw_by_index(GL_FALSE, primMode, indexType);
+   return test_draw_by_index(GL_FALSE, use_vbo, primMode, indexType);
 }
 
 
@@ -376,9 +436,9 @@ test_draw_elements(GLenum primMode, GLenum indexType)
  * Test glArrayElement() with glPrimitiveRestartIndexNV().
  */
 static GLboolean
-test_array_element(GLenum primMode, GLenum indexType)
+test_array_element(GLboolean use_vbo, GLenum primMode, GLenum indexType)
 {
-   return test_draw_by_index(GL_TRUE, primMode, indexType);
+   return test_draw_by_index(GL_TRUE, use_vbo, primMode, indexType);
 }
 
 
@@ -498,35 +558,59 @@ piglit_display(void)
       TestGL31 = GL_FALSE;
       pass = pass && test_begin_end(GL_TRIANGLE_STRIP);
       pass = pass && test_begin_end(GL_LINE_STRIP);
-      pass = pass && test_draw_elements(GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
-      pass = pass && test_draw_elements(GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
-      pass = pass && test_draw_elements(GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
-      pass = pass && test_draw_elements(GL_LINE_STRIP, GL_UNSIGNED_BYTE);
-      pass = pass && test_draw_elements(GL_LINE_STRIP, GL_UNSIGNED_SHORT);
-      pass = pass && test_draw_elements(GL_LINE_STRIP, GL_UNSIGNED_INT);
-      pass = pass && test_array_element(GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
-      pass = pass && test_array_element(GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
-      pass = pass && test_array_element(GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
-      pass = pass && test_array_element(GL_LINE_STRIP, GL_UNSIGNED_BYTE);
-      pass = pass && test_array_element(GL_LINE_STRIP, GL_UNSIGNED_SHORT);
-      pass = pass && test_array_element(GL_LINE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_draw_elements(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_draw_elements(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_draw_elements(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_draw_elements(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_draw_elements(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_draw_elements(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_array_element(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_array_element(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_array_element(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_array_element(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_array_element(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_array_element(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_draw_elements(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_draw_elements(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_draw_elements(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_draw_elements(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_draw_elements(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_draw_elements(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_array_element(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_array_element(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_array_element(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_array_element(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_array_element(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_array_element(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_INT);
       pass = pass && test_draw_arrays();
    }
 
    if (Have_31) {
       TestGL31 = GL_TRUE;
-      pass = pass && test_draw_elements(GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
-      pass = pass && test_draw_elements(GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
-      pass = pass && test_draw_elements(GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
-      pass = pass && test_draw_elements(GL_LINE_STRIP, GL_UNSIGNED_BYTE);
-      pass = pass && test_draw_elements(GL_LINE_STRIP, GL_UNSIGNED_SHORT);
-      pass = pass && test_draw_elements(GL_LINE_STRIP, GL_UNSIGNED_INT);
-      pass = pass && test_array_element(GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
-      pass = pass && test_array_element(GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
-      pass = pass && test_array_element(GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
-      pass = pass && test_array_element(GL_LINE_STRIP, GL_UNSIGNED_BYTE);
-      pass = pass && test_array_element(GL_LINE_STRIP, GL_UNSIGNED_SHORT);
-      pass = pass && test_array_element(GL_LINE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_draw_elements(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_draw_elements(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_draw_elements(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_draw_elements(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_draw_elements(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_draw_elements(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_array_element(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_array_element(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_array_element(VBO_OFF, GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_array_element(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_array_element(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_array_element(VBO_OFF, GL_LINE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_draw_elements(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_draw_elements(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_draw_elements(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_draw_elements(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_draw_elements(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_draw_elements(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_array_element(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_array_element(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_array_element(VBO_ON,  GL_TRIANGLE_STRIP, GL_UNSIGNED_INT);
+      pass = pass && test_array_element(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_BYTE);
+      pass = pass && test_array_element(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_SHORT);
+      pass = pass && test_array_element(VBO_ON,  GL_LINE_STRIP, GL_UNSIGNED_INT);
       pass = pass && test_draw_arrays();
    }
 
-- 
1.7.10



More information about the Piglit mailing list