mesa: Make MultiDrawElements submit multiple primitives at once.
authorEric Anholt <eric@anholt.net>
Mon, 31 Aug 2009 17:13:22 +0000 (10:13 -0700)
committerEric Anholt <eric@anholt.net>
Wed, 2 Sep 2009 03:35:19 +0000 (20:35 -0700)
Previously, MultiDrawElements just called DrawElements a bunch of times.
By sending several primitives down the pipeline at once, we avoid a bunch
of validation.  On my GL demo, this improves fps by 2.5% (+/- .41%) and
reduces CPU usage by 70.5% (+/- 2.9%) (n=3).

Reviewed by: Ian Romanick <ian.d.romanick@intel.com>

src/mesa/main/api_exec.c
src/mesa/main/api_noop.c
src/mesa/main/api_noop.h
src/mesa/main/dd.h
src/mesa/main/dlist.c
src/mesa/main/varray.c
src/mesa/main/vtxfmt.c
src/mesa/main/vtxfmt_tmp.h
src/mesa/vbo/vbo_exec_array.c
src/mesa/vbo/vbo_save_api.c

index 199550b35d35b278672a118f38d8d21012533f1c..cbf48615bf4312ed9f95e5ad9387a2d2602e5085 100644 (file)
@@ -529,7 +529,6 @@ _mesa_init_exec_table(struct _glapi_table *exec)
    /* 148. GL_EXT_multi_draw_arrays */
 #if _HAVE_FULL_GL
    SET_MultiDrawArraysEXT(exec, _mesa_MultiDrawArraysEXT);
-   SET_MultiDrawElementsEXT(exec, _mesa_MultiDrawElementsEXT);
 #endif
 
    /* 173. GL_INGR_blend_func_separate */
index 66f9c4e6bdb0d65573fc8b98d69823dd411fd6dd..09ba7e50622469983e9edabef033b3de922359b8 100644 (file)
@@ -772,6 +772,20 @@ _mesa_noop_DrawRangeElements(GLenum mode,
        CALL_DrawElements(GET_DISPATCH(), (mode, count, type, indices));
 }
 
+/* GL_EXT_multi_draw_arrays */
+void GLAPIENTRY
+_mesa_noop_MultiDrawElements(GLenum mode, const GLsizei *count, GLenum type,
+                            const GLvoid **indices, GLsizei primcount)
+{
+   GLsizei i;
+
+   for (i = 0; i < primcount; i++) {
+      if (count[i] > 0) {
+        CALL_DrawElements(GET_DISPATCH(), (mode, count[i], type, indices[i]));
+      }
+   }
+}
+
 /*
  * Eval Mesh
  */
@@ -980,6 +994,7 @@ _mesa_noop_vtxfmt_init( GLvertexformat *vfmt )
    vfmt->DrawArrays = _mesa_noop_DrawArrays;
    vfmt->DrawElements = _mesa_noop_DrawElements;
    vfmt->DrawRangeElements = _mesa_noop_DrawRangeElements;
+   vfmt->MultiDrawElementsEXT = _mesa_noop_MultiDrawElements;
    vfmt->EvalMesh1 = _mesa_noop_EvalMesh1;
    vfmt->EvalMesh2 = _mesa_noop_EvalMesh2;
 }
index 8bf4660800108cf6bcf84806955f11a44dc0baf3..a7956d00b3b0e5fedab25861a285159f8f597a73 100644 (file)
@@ -40,6 +40,10 @@ _mesa_noop_EvalMesh2(GLenum mode, GLint i1, GLint i2, GLint j1, GLint j2);
 extern void GLAPIENTRY
 _mesa_noop_Materialfv(GLenum face, GLenum pname, const GLfloat *param);
 
+extern void GLAPIENTRY
+_mesa_noop_MultiDrawElements(GLenum mode, const GLsizei *count, GLenum type,
+                            const GLvoid **indices, GLsizei primcount);
+
 extern void
 _mesa_noop_vtxfmt_init(GLvertexformat *vfmt);
 
index 1d92e510a4d5cb062d7622b27f1385c7b61ba15d..f02e868d4a7997fe3cbaa3f9c8b01a7a68713c8d 100644 (file)
@@ -1150,7 +1150,11 @@ typedef struct {
    void (GLAPIENTRYP DrawRangeElements)( GLenum mode, GLuint start,
                              GLuint end, GLsizei count,
                              GLenum type, const GLvoid *indices );
-   /*@}*/
+   void (GLAPIENTRYP MultiDrawElementsEXT)( GLenum mode, const GLsizei *count,
+                                           GLenum type,
+                                           const GLvoid **indices,
+                                           GLsizei primcount);
+  /*@}*/
 
    /**
     * \name Eval
index 9b68b3e116260745d75eaca13d3bbfdc1c5a6d9a..8cff9ea64a0e5445891f1ca47d500078af1649cd 100644 (file)
@@ -7751,18 +7751,6 @@ exec_MultiDrawArraysEXT(GLenum mode, GLint * first,
    CALL_MultiDrawArraysEXT(ctx->Exec, (mode, first, count, primcount));
 }
 
-/* GL_EXT_multi_draw_arrays */
-static void GLAPIENTRY
-exec_MultiDrawElementsEXT(GLenum mode, const GLsizei * count,
-                          GLenum type, const GLvoid ** indices,
-                          GLsizei primcount)
-{
-   GET_CURRENT_CONTEXT(ctx);
-   FLUSH_VERTICES(ctx, 0);
-   CALL_MultiDrawElementsEXT(ctx->Exec,
-                             (mode, count, type, indices, primcount));
-}
-
 /* GL_IBM_multimode_draw_arrays */
 static void GLAPIENTRY
 exec_MultiModeDrawArraysIBM(const GLenum * mode, const GLint * first,
@@ -8108,7 +8096,6 @@ _mesa_init_dlist_table(struct _glapi_table *table)
 
    /* 148. GL_EXT_multi_draw_arrays */
    SET_MultiDrawArraysEXT(table, exec_MultiDrawArraysEXT);
-   SET_MultiDrawElementsEXT(table, exec_MultiDrawElementsEXT);
 
    /* 149. GL_EXT_fog_coord */
    SET_FogCoordPointerEXT(table, exec_FogCoordPointerEXT);
@@ -8723,6 +8710,7 @@ _mesa_save_vtxfmt_init(GLvertexformat * vfmt)
    vfmt->DrawArrays = 0;
    vfmt->DrawElements = 0;
    vfmt->DrawRangeElements = 0;
+   vfmt->MultiDrawElemementsEXT = 0;
 #endif
 }
 
index be1c03cec2a88ac97e93f4bf628d3c256fe32650..6cd2a2f4f64649d80b2483a81a8f8f15999d6ddc 100644 (file)
@@ -1038,24 +1038,6 @@ _mesa_MultiDrawArraysEXT( GLenum mode, GLint *first,
 }
 
 
-/* GL_EXT_multi_draw_arrays */
-void GLAPIENTRY
-_mesa_MultiDrawElementsEXT( GLenum mode, const GLsizei *count, GLenum type,
-                            const GLvoid **indices, GLsizei primcount )
-{
-   GET_CURRENT_CONTEXT(ctx);
-   GLint i;
-
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
-
-   for (i = 0; i < primcount; i++) {
-      if (count[i] > 0) {
-         CALL_DrawElements(ctx->Exec, (mode, count[i], type, indices[i]));
-      }
-   }
-}
-
-
 /* GL_IBM_multimode_draw_arrays */
 void GLAPIENTRY
 _mesa_MultiModeDrawArraysIBM( const GLenum * mode, const GLint * first,
index 1f807dc3dc3a0903e3964b905a721eee0a077590..8d6f560a80a0973491d65e8872cb8ca43041b930 100644 (file)
@@ -133,6 +133,7 @@ install_vtxfmt( struct _glapi_table *tab, const GLvertexformat *vfmt )
    SET_DrawArrays(tab, vfmt->DrawArrays);
    SET_DrawElements(tab, vfmt->DrawElements);
    SET_DrawRangeElements(tab, vfmt->DrawRangeElements);
+   SET_MultiDrawElementsEXT(tab, vfmt->MultiDrawElementsEXT);
    SET_EvalMesh1(tab, vfmt->EvalMesh1);
    SET_EvalMesh2(tab, vfmt->EvalMesh2);
    ASSERT(tab->EvalMesh2);
index 6f5d01e40f2d9ad8e5e4ebf2f1584645456e401c..1308d0aa46646d22370619a7ae5970a463de18e5 100644 (file)
@@ -335,6 +335,17 @@ static void GLAPIENTRY TAG(DrawElements)( GLenum mode, GLsizei count, GLenum typ
    CALL_DrawElements(GET_DISPATCH(), ( mode, count, type, indices ));
 }
 
+static void GLAPIENTRY TAG(MultiDrawElementsEXT)( GLenum mode,
+                                                 const GLsizei *count,
+                                                 GLenum type,
+                                                 const GLvoid **indices,
+                                                 GLsizei primcount)
+{
+   PRE_LOOPBACK( MultiDrawElementsEXT );
+   CALL_MultiDrawElementsEXT(GET_DISPATCH(), ( mode, count, type, indices,
+                                              primcount ));
+}
+
 static void GLAPIENTRY TAG(DrawRangeElements)( GLenum mode, GLuint start,
                                    GLuint end, GLsizei count,
                                    GLenum type, const GLvoid *indices )
@@ -522,6 +533,7 @@ static GLvertexformat TAG(vtxfmt) = {
    TAG(DrawArrays),
    TAG(DrawElements),
    TAG(DrawRangeElements),
+   TAG(MultiDrawElementsEXT),
    TAG(EvalMesh1),
    TAG(EvalMesh2)
 };
index 4148469ef45d900f11b671c038b48568e9bcdc6b..12911f5750c16e8a87c43ae57cb55337a4e0a631 100644 (file)
@@ -33,6 +33,7 @@
 #include "main/api_noop.h"
 #include "main/varray.h"
 #include "main/bufferobj.h"
+#include "main/macros.h"
 #include "glapi/dispatch.h"
 
 #include "vbo_context.h"
@@ -721,6 +722,152 @@ vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type,
                                   count, type, indices);
 }
 
+/* Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements */
+static void
+vbo_validated_multidrawelements(GLcontext *ctx, GLenum mode,
+                               const GLsizei *count, GLenum type,
+                               const GLvoid **indices, GLsizei primcount)
+{
+   struct vbo_context *vbo = vbo_context(ctx);
+   struct vbo_exec_context *exec = &vbo->exec;
+   struct _mesa_index_buffer ib;
+   struct _mesa_prim *prim;
+   unsigned int index_type_size = 0;
+   uintptr_t min_index_ptr, max_index_ptr;
+   GLboolean fallback = GL_FALSE;
+   int i;
+
+   if (primcount == 0)
+      return;
+
+   FLUSH_CURRENT( ctx, 0 );
+
+   if (ctx->NewState)
+      _mesa_update_state( ctx );
+
+   if (!_mesa_valid_to_render(ctx, "glMultiDrawElements")) {
+      return;
+   }
+
+   if (ctx->NewState)
+      _mesa_update_state( ctx );
+
+   prim = _mesa_calloc(primcount * sizeof(*prim));
+   if (prim == NULL) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMultiDrawElements");
+      return;
+   }
+
+   /* Decide if we can do this all as one set of primitives sharing the
+    * same index buffer, or if we have to reset the index pointer per primitive.
+    */
+   bind_arrays( ctx );
+
+   switch (type) {
+   case GL_UNSIGNED_INT:
+      index_type_size = 4;
+      break;
+   case GL_UNSIGNED_SHORT:
+      index_type_size = 2;
+      break;
+   case GL_UNSIGNED_BYTE:
+      index_type_size = 1;
+      break;
+   default:
+      assert(0);
+   }
+
+   min_index_ptr = (uintptr_t)indices[0];
+   max_index_ptr = 0;
+   for (i = 0; i < primcount; i++) {
+      min_index_ptr = MIN2(min_index_ptr, (uintptr_t)indices[i]);
+      max_index_ptr = MAX2(max_index_ptr, (uintptr_t)indices[i] +
+                          index_type_size * count[i]);
+   }
+
+   /* Check if we can handle this thing as a bunch of index offsets from the
+    * same index pointer.  If we can't, then we have to fall back to doing
+    * a draw_prims per primitive.
+    */
+   if (index_type_size != 1) {
+      for (i = 0; i < primcount; i++) {
+        if ((((uintptr_t)indices[i] - min_index_ptr) % index_type_size) != 0) {
+           fallback = GL_TRUE;
+           break;
+        }
+      }
+   }
+
+   /* If the index buffer isn't in a VBO, then treating the application's
+    * subranges of the index buffer as one large index buffer may lead to
+    * us reading unmapped memory.
+    */
+   if (!_mesa_is_bufferobj(ctx->Array.ElementArrayBufferObj))
+      fallback = GL_TRUE;
+
+   if (!fallback) {
+      ib.count = (max_index_ptr - min_index_ptr) / index_type_size;
+      ib.type = type;
+      ib.obj = ctx->Array.ElementArrayBufferObj;
+      ib.ptr = (void *)min_index_ptr;
+
+      for (i = 0; i < primcount; i++) {
+        prim[i].begin = (i == 0);
+        prim[i].end = (i == primcount - 1);
+        prim[i].weak = 0;
+        prim[i].pad = 0;
+        prim[i].mode = mode;
+        prim[i].start = ((uintptr_t)indices[i] - min_index_ptr) / index_type_size;
+        prim[i].count = count[i];
+        prim[i].indexed = 1;
+      }
+
+      vbo->draw_prims(ctx, exec->array.inputs, prim, primcount, &ib,
+                     GL_FALSE, ~0, ~0);
+   } else {
+      for (i = 0; i < primcount; i++) {
+        ib.count = count[i];
+        ib.type = type;
+        ib.obj = ctx->Array.ElementArrayBufferObj;
+        ib.ptr = indices[i];
+
+
+        prim[0].begin = 1;
+        prim[0].end = 1;
+        prim[0].weak = 0;
+        prim[0].pad = 0;
+        prim[0].mode = mode;
+        prim[0].start = 0;
+        prim[0].count = count[i];
+        prim[0].indexed = 1;
+      }
+
+      vbo->draw_prims(ctx, exec->array.inputs, prim, 1, &ib,
+                     GL_FALSE, ~0, ~0);
+   }
+   _mesa_free(prim);
+}
+
+static void GLAPIENTRY
+vbo_exec_MultiDrawElements(GLenum mode,
+                          const GLsizei *count, GLenum type,
+                          const GLvoid **indices,
+                          GLsizei primcount)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   GLint i;
+
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+
+   for (i = 0; i < primcount; i++) {
+      if (!_mesa_validate_DrawElements( ctx, mode, count[i], type, indices[i] ))
+        return;
+   }
+
+   vbo_validated_multidrawelements(ctx, mode, count, type, indices, primcount);
+}
+
+
 
 /***********************************************************************
  * Initialization
@@ -733,10 +880,12 @@ vbo_exec_array_init( struct vbo_exec_context *exec )
    exec->vtxfmt.DrawArrays = vbo_exec_DrawArrays;
    exec->vtxfmt.DrawElements = vbo_exec_DrawElements;
    exec->vtxfmt.DrawRangeElements = vbo_exec_DrawRangeElements;
+   exec->vtxfmt.MultiDrawElementsEXT = vbo_exec_MultiDrawElements;
 #else
    exec->vtxfmt.DrawArrays = _mesa_noop_DrawArrays;
    exec->vtxfmt.DrawElements = _mesa_noop_DrawElements;
    exec->vtxfmt.DrawRangeElements = _mesa_noop_DrawRangeElements;
+   exec->vtxfmt.MultiDrawElementsEXT = _mesa_noop_MultiDrawElements;
 #endif
 }
 
@@ -772,3 +921,11 @@ _mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
 {
    vbo_exec_DrawRangeElements(mode, start, end, count, type, indices);
 }
+
+/* GL_EXT_multi_draw_arrays */
+void GLAPIENTRY
+_mesa_MultiDrawElementsEXT(GLenum mode, const GLsizei *count, GLenum type,
+                          const GLvoid **indices, GLsizei primcount)
+{
+   vbo_exec_MultiDrawElements(mode, count, type, indices, primcount);
+}
index cdbbc9c1876b5512e65cb525cc6f9cc126e6b588..1771510d8488f591a974ce8745e613b534d31dbd 100644 (file)
@@ -73,6 +73,7 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "main/dlist.h"
 #include "main/enums.h"
 #include "main/macros.h"
+#include "main/api_noop.h"
 #include "main/api_validate.h"
 #include "main/api_arrayelt.h"
 #include "main/vtxfmt.h"
@@ -1038,6 +1039,8 @@ static void _save_vtxfmt_init( GLcontext *ctx )
    vfmt->DrawArrays = _save_DrawArrays;
    vfmt->DrawElements = _save_DrawElements;
    vfmt->DrawRangeElements = _save_DrawRangeElements;
+   /* Loops back into vfmt->DrawElements */
+   vfmt->MultiDrawElementsEXT = _mesa_noop_MultiDrawElements;
 
 }
 
@@ -1228,6 +1231,8 @@ void vbo_save_api_init( struct vbo_save_context *save )
    ctx->ListState.ListVtxfmt.DrawArrays = _save_OBE_DrawArrays;
    ctx->ListState.ListVtxfmt.DrawElements = _save_OBE_DrawElements;
    ctx->ListState.ListVtxfmt.DrawRangeElements = _save_OBE_DrawRangeElements;
+   /* loops back into _save_OBE_DrawElements */
+   ctx->ListState.ListVtxfmt.MultiDrawElementsEXT = _mesa_noop_MultiDrawElements;
    _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
 }