mesa: don't ever set NullBufferObj in gl_vertex_array_binding
[mesa.git] / src / mesa / main / draw.c
index cceadf2613aca18a296efd521f56f0f29a06d5e0..5bb254e00d2621191b064096518ecca22afca963 100644 (file)
@@ -67,35 +67,30 @@ check_array_data(struct gl_context *ctx, struct gl_vertex_array_object *vao,
                  GLuint attrib, GLuint j)
 {
    const struct gl_array_attributes *array = &vao->VertexAttrib[attrib];
-   if (array->Enabled) {
+   if (vao->Enabled & VERT_BIT(attrib)) {
       const struct gl_vertex_buffer_binding *binding =
          &vao->BufferBinding[array->BufferBindingIndex];
       struct gl_buffer_object *bo = binding->BufferObj;
       const void *data = array->Ptr;
       if (_mesa_is_bufferobj(bo)) {
-         if (!bo->Mappings[MAP_INTERNAL].Pointer) {
-            /* need to map now */
-            bo->Mappings[MAP_INTERNAL].Pointer =
-               ctx->Driver.MapBufferRange(ctx, 0, bo->Size,
-                                          GL_MAP_READ_BIT, bo, MAP_INTERNAL);
-         }
          data = ADD_POINTERS(_mesa_vertex_attrib_address(array, binding),
                              bo->Mappings[MAP_INTERNAL].Pointer);
       }
-      switch (array->Type) {
+      switch (array->Format.Type) {
       case GL_FLOAT:
          {
             GLfloat *f = (GLfloat *) ((GLubyte *) data + binding->Stride * j);
             GLint k;
-            for (k = 0; k < array->Size; k++) {
+            for (k = 0; k < array->Format.Size; k++) {
                if (IS_INF_OR_NAN(f[k]) || f[k] >= 1.0e20F || f[k] <= -1.0e10F) {
                   printf("Bad array data:\n");
                   printf("  Element[%u].%u = %f\n", j, k, f[k]);
                   printf("  Array %u at %p\n", attrib, (void *) array);
                   printf("  Type 0x%x, Size %d, Stride %d\n",
-                         array->Type, array->Size, binding->Stride);
+                         array->Format.Type, array->Format.Size,
+                         binding->Stride);
                   printf("  Address/offset %p in Buffer Object %u\n",
-                         array->Ptr, bo->Name);
+                         array->Ptr, bo ? bo->Name : 0);
                   f[k] = 1.0F;  /* XXX replace the bad value! */
                }
                /*assert(!IS_INF_OR_NAN(f[k])); */
@@ -109,40 +104,18 @@ check_array_data(struct gl_context *ctx, struct gl_vertex_array_object *vao,
 }
 
 
-/**
- * Unmap the buffer object referenced by given array, if mapped.
- */
-static void
-unmap_array_buffer(struct gl_context *ctx, struct gl_vertex_array_object *vao,
-                   GLuint attrib)
-{
-   const struct gl_array_attributes *array = &vao->VertexAttrib[attrib];
-   if (array->Enabled) {
-      const struct gl_vertex_buffer_binding *binding =
-         &vao->BufferBinding[array->BufferBindingIndex];
-      struct gl_buffer_object *bo = binding->BufferObj;
-      if (_mesa_is_bufferobj(bo) && _mesa_bufferobj_mapped(bo, MAP_INTERNAL)) {
-         ctx->Driver.UnmapBuffer(ctx, bo, MAP_INTERNAL);
-      }
-   }
-}
-
-
-static inline int
-sizeof_ib_type(GLenum type)
+static inline void
+get_index_size(GLenum type, struct _mesa_index_buffer *ib)
 {
-   switch (type) {
-   case GL_UNSIGNED_INT:
-      return sizeof(GLuint);
-   case GL_UNSIGNED_SHORT:
-      return sizeof(GLushort);
-   case GL_UNSIGNED_BYTE:
-      return sizeof(GLubyte);
-   default:
-      assert(!"unsupported index data type");
-      /* In case assert is turned off */
-      return 0;
-   }
+   /* The type is already validated, so use a fast conversion.
+    *
+    * GL_UNSIGNED_BYTE  - GL_UNSIGNED_BYTE = 0
+    * GL_UNSIGNED_SHORT - GL_UNSIGNED_BYTE = 2
+    * GL_UNSIGNED_INT   - GL_UNSIGNED_BYTE = 4
+    *
+    * Divide by 2 to get 0,1,2.
+    */
+   ib->index_size_shift = (type - GL_UNSIGNED_BYTE) >> 1;
 }
 
 /**
@@ -155,17 +128,14 @@ check_draw_elements_data(struct gl_context *ctx, GLsizei count,
                          GLint basevertex)
 {
    struct gl_vertex_array_object *vao = ctx->Array.VAO;
-   const void *elemMap;
    GLint i;
    GLuint k;
 
-   if (_mesa_is_bufferobj(vao->IndexBufferObj)) {
-      elemMap = ctx->Driver.MapBufferRange(ctx, 0,
-                                           vao->IndexBufferObj->Size,
-                                           GL_MAP_READ_BIT,
-                                           vao->IndexBufferObj, MAP_INTERNAL);
-      elements = ADD_POINTERS(elements, elemMap);
-   }
+   _mesa_vao_map(ctx, vao, GL_MAP_READ_BIT);
+
+   if (_mesa_is_bufferobj(vao->IndexBufferObj))
+       elements =
+          ADD_POINTERS(vao->IndexBufferObj->Mappings[MAP_INTERNAL].Pointer, elements);
 
    for (i = 0; i < count; i++) {
       GLuint j;
@@ -191,13 +161,7 @@ check_draw_elements_data(struct gl_context *ctx, GLsizei count,
       }
    }
 
-   if (_mesa_is_bufferobj(vao->IndexBufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, vao->IndexBufferObj, MAP_INTERNAL);
-   }
-
-   for (k = 0; k < VERT_ATTRIB_MAX; k++) {
-      unmap_array_buffer(ctx, vao, k);
-   }
+   _mesa_vao_unmap(ctx, vao);
 }
 
 
@@ -225,7 +189,7 @@ skip_validated_draw(struct gl_context *ctx)
    case API_OPENGLES:
       /* For OpenGL ES, only draw if we have vertex positions
        */
-      if (!ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Enabled)
+      if (!(ctx->Array.VAO->Enabled & VERT_BIT_POS))
          return true;
       break;
 
@@ -252,8 +216,7 @@ skip_validated_draw(struct gl_context *ctx)
          /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
           * array [0]).
           */
-         return (!ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Enabled &&
-                 !ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_GENERIC0].Enabled);
+         return !(ctx->Array.VAO->Enabled & (VERT_BIT_POS|VERT_BIT_GENERIC0));
       }
       break;
 
@@ -272,36 +235,35 @@ static void
 print_draw_arrays(struct gl_context *ctx,
                   GLenum mode, GLint start, GLsizei count)
 {
-   const struct gl_vertex_array_object *vao = ctx->Array.VAO;
+   struct gl_vertex_array_object *vao = ctx->Array.VAO;
 
-   printf("_mesa_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
+   printf("_mesa_DrawArrays(mode 0x%x, start %d, count %d):\n",
           mode, start, count);
 
-   unsigned i;
-   for (i = 0; i < VERT_ATTRIB_MAX; ++i) {
+   _mesa_vao_map_arrays(ctx, vao, GL_MAP_READ_BIT);
+
+   GLbitfield mask = vao->Enabled;
+   while (mask) {
+      const gl_vert_attrib i = u_bit_scan(&mask);
       const struct gl_array_attributes *array = &vao->VertexAttrib[i];
-      if (!array->Enabled)
-         continue;
 
       const struct gl_vertex_buffer_binding *binding =
          &vao->BufferBinding[array->BufferBindingIndex];
       struct gl_buffer_object *bufObj = binding->BufferObj;
 
-      printf("attr %s: size %d stride %d  enabled %d  "
+      printf("attr %s: size %d stride %d  "
              "ptr %p  Bufobj %u\n",
              gl_vert_attrib_name((gl_vert_attrib) i),
-             array->Size, binding->Stride, array->Enabled,
-             array->Ptr, bufObj->Name);
+             array->Format.Size, binding->Stride,
+             array->Ptr, bufObj ? bufObj->Name : 0);
 
       if (_mesa_is_bufferobj(bufObj)) {
-         GLubyte *p = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size,
-                                                 GL_MAP_READ_BIT, bufObj,
-                                                 MAP_INTERNAL);
+         GLubyte *p = bufObj->Mappings[MAP_INTERNAL].Pointer;
          int offset = (int) (GLintptr)
             _mesa_vertex_attrib_address(array, binding);
 
          unsigned multiplier;
-         switch (array->Type) {
+         switch (array->Format.Type) {
          case GL_DOUBLE:
          case GL_INT64_ARB:
          case GL_UNSIGNED_INT64_ARB:
@@ -315,7 +277,7 @@ print_draw_arrays(struct gl_context *ctx,
          int *k = (int *) f;
          int i = 0;
          int n = (count - 1) * (binding->Stride / (4 * multiplier))
-           + array->Size;
+            + array->Format.Size;
          if (n > 32)
             n = 32;
          printf("  Data at offset %d:\n", offset);
@@ -327,9 +289,10 @@ print_draw_arrays(struct gl_context *ctx,
                printf("    float[%d] = 0x%08x %f\n", i, k[i], f[i]);
             i++;
          } while (i < n);
-         ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
       }
    }
+
+   _mesa_vao_unmap_arrays(ctx, vao);
 }
 
 
@@ -386,27 +349,24 @@ _mesa_draw_arrays(struct gl_context *ctx, GLenum mode, GLint start,
                   GLsizei count, GLuint numInstances, GLuint baseInstance,
                   GLuint drawID)
 {
-   struct _mesa_prim prim;
-
    if (skip_validated_draw(ctx))
       return;
 
    /* OpenGL 4.5 says that primitive restart is ignored with non-indexed
     * draws.
     */
-   memset(&prim, 0, sizeof(prim));
-   prim.begin = 1;
-   prim.end = 1;
-   prim.mode = mode;
-   prim.num_instances = numInstances;
-   prim.base_instance = baseInstance;
-   prim.draw_id = drawID;
-   prim.is_indirect = 0;
-   prim.start = start;
-   prim.count = count;
+   struct _mesa_prim prim = {
+      .begin = 1,
+      .end = 1,
+      .mode = mode,
+      .draw_id = drawID,
+      .start = start,
+      .count = count,
+   };
 
    ctx->Driver.Draw(ctx, &prim, 1, NULL,
-                    GL_TRUE, start, start + count - 1, NULL, 0, NULL);
+                    GL_TRUE, start, start + count - 1,
+                    numInstances, baseInstance, NULL, 0);
 
    if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
       _mesa_flush(ctx);
@@ -538,8 +498,8 @@ _mesa_exec_EvalMesh2(GLenum mode, GLint i1, GLint i2, GLint j1, GLint j2)
 /**
  * Called from glDrawArrays when in immediate mode (not display list mode).
  */
-static void GLAPIENTRY
-_mesa_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
+void GLAPIENTRY
+_mesa_DrawArrays(GLenum mode, GLint start, GLsizei count)
 {
    GET_CURRENT_CONTEXT(ctx);
 
@@ -573,9 +533,9 @@ _mesa_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
  * Called from glDrawArraysInstanced when in immediate mode (not
  * display list mode).
  */
-static void GLAPIENTRY
-_mesa_exec_DrawArraysInstanced(GLenum mode, GLint start, GLsizei count,
-                               GLsizei numInstances)
+void GLAPIENTRY
+_mesa_DrawArraysInstanced(GLenum mode, GLint start, GLsizei count,
+                          GLsizei numInstances)
 {
    GET_CURRENT_CONTEXT(ctx);
 
@@ -645,6 +605,28 @@ _mesa_exec_DrawArraysInstancedBaseInstance(GLenum mode, GLint first,
 }
 
 
+#define MAX_ALLOCA_PRIMS (50000 / sizeof(*prim))
+
+/* Use calloc for large allocations and alloca for small allocations. */
+/* We have to use a macro because alloca is local within the function. */
+#define ALLOC_PRIMS(prim, primcount, func) do { \
+   if (unlikely(primcount > MAX_ALLOCA_PRIMS)) { \
+      prim = calloc(primcount, sizeof(*prim)); \
+      if (!prim) { \
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, func); \
+         return; \
+      } \
+   } else { \
+      prim = alloca(primcount * sizeof(*prim)); \
+   } \
+} while (0)
+
+#define FREE_PRIMS(prim, primcount) do { \
+   if (primcount > MAX_ALLOCA_PRIMS) \
+      free(prim); \
+} while (0)
+
+
 /**
  * Called from glMultiDrawArrays when in immediate mode.
  */
@@ -672,24 +654,30 @@ _mesa_exec_MultiDrawArrays(GLenum mode, const GLint *first,
          return;
    }
 
-   for (i = 0; i < primcount; i++) {
-      if (count[i] > 0) {
-         if (0)
-            check_draw_arrays_data(ctx, first[i], count[i]);
-
-         /* The GL_ARB_shader_draw_parameters spec adds the following after the
-          * pseudo-code describing glMultiDrawArrays:
-          *
-          *    "The index of the draw (<i> in the above pseudo-code) may be
-          *     read by a vertex shader as <gl_DrawIDARB>, as described in
-          *     Section 11.1.3.9."
-          */
-         _mesa_draw_arrays(ctx, mode, first[i], count[i], 1, 0, i);
+   if (skip_validated_draw(ctx))
+      return;
 
-         if (0)
-            print_draw_arrays(ctx, mode, first[i], count[i]);
-      }
+   struct _mesa_prim *prim;
+
+   ALLOC_PRIMS(prim, primcount, "glMultiDrawElements");
+
+   for (i = 0; i < primcount; i++) {
+      prim[i].begin = 1;
+      prim[i].end = 1;
+      prim[i].mode = mode;
+      prim[i].draw_id = i;
+      prim[i].start = first[i];
+      prim[i].count = count[i];
+      prim[i].basevertex = 0;
    }
+
+   ctx->Driver.Draw(ctx, prim, primcount, NULL, GL_FALSE, 0, 0, 1, 0,
+                    NULL, 0);
+
+   if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
+      _mesa_flush(ctx);
+
+   FREE_PRIMS(prim, primcount);
 }
 
 
@@ -799,21 +787,16 @@ _mesa_validated_drawrangeelements(struct gl_context *ctx, GLenum mode,
       return;
 
    ib.count = count;
-   ib.index_size = sizeof_ib_type(type);
    ib.obj = ctx->Array.VAO->IndexBufferObj;
    ib.ptr = indices;
+   get_index_size(type, &ib);
 
    prim.begin = 1;
    prim.end = 1;
-   prim.pad = 0;
    prim.mode = mode;
    prim.start = 0;
    prim.count = count;
-   prim.indexed = 1;
-   prim.is_indirect = 0;
    prim.basevertex = basevertex;
-   prim.num_instances = numInstances;
-   prim.base_instance = baseInstance;
    prim.draw_id = 0;
 
    /* Need to give special consideration to rendering a range of
@@ -848,7 +831,8 @@ _mesa_validated_drawrangeelements(struct gl_context *ctx, GLenum mode,
     */
 
    ctx->Driver.Draw(ctx, &prim, 1, &ib,
-                    index_bounds_valid, start, end, NULL, 0, NULL);
+                    index_bounds_valid, start, end,
+                    numInstances, baseInstance, NULL, 0);
 
    if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
       _mesa_flush(ctx);
@@ -859,10 +843,10 @@ _mesa_validated_drawrangeelements(struct gl_context *ctx, GLenum mode,
 /**
  * Called by glDrawRangeElementsBaseVertex() in immediate mode.
  */
-static void GLAPIENTRY
-_mesa_exec_DrawRangeElementsBaseVertex(GLenum mode, GLuint start, GLuint end,
-                                       GLsizei count, GLenum type,
-                                       const GLvoid * indices, GLint basevertex)
+void GLAPIENTRY
+_mesa_DrawRangeElementsBaseVertex(GLenum mode, GLuint start, GLuint end,
+                                  GLsizei count, GLenum type,
+                                  const GLvoid * indices, GLint basevertex)
 {
    static GLuint warnCount = 0;
    GLboolean index_bounds_valid = GL_TRUE;
@@ -958,9 +942,9 @@ _mesa_exec_DrawRangeElementsBaseVertex(GLenum mode, GLuint start, GLuint end,
 /**
  * Called by glDrawRangeElements() in immediate mode.
  */
-static void GLAPIENTRY
-_mesa_exec_DrawRangeElements(GLenum mode, GLuint start, GLuint end,
-                             GLsizei count, GLenum type, const GLvoid * indices)
+void GLAPIENTRY
+_mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end,
+                        GLsizei count, GLenum type, const GLvoid * indices)
 {
    if (MESA_VERBOSE & VERBOSE_DRAW) {
       GET_CURRENT_CONTEXT(ctx);
@@ -970,17 +954,17 @@ _mesa_exec_DrawRangeElements(GLenum mode, GLuint start, GLuint end,
                   _mesa_enum_to_string(type), indices);
    }
 
-   _mesa_exec_DrawRangeElementsBaseVertex(mode, start, end, count, type,
-                                          indices, 0);
+   _mesa_DrawRangeElementsBaseVertex(mode, start, end, count, type,
+                                     indices, 0);
 }
 
 
 /**
  * Called by glDrawElements() in immediate mode.
  */
-static void GLAPIENTRY
-_mesa_exec_DrawElements(GLenum mode, GLsizei count, GLenum type,
-                        const GLvoid * indices)
+void GLAPIENTRY
+_mesa_DrawElements(GLenum mode, GLsizei count, GLenum type,
+                   const GLvoid * indices)
 {
    GET_CURRENT_CONTEXT(ctx);
 
@@ -1009,9 +993,9 @@ _mesa_exec_DrawElements(GLenum mode, GLsizei count, GLenum type,
 /**
  * Called by glDrawElementsBaseVertex() in immediate mode.
  */
-static void GLAPIENTRY
-_mesa_exec_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
-                                  const GLvoid * indices, GLint basevertex)
+void GLAPIENTRY
+_mesa_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
+                             const GLvoid * indices, GLint basevertex)
 {
    GET_CURRENT_CONTEXT(ctx);
 
@@ -1199,8 +1183,6 @@ _mesa_validated_multidrawelements(struct gl_context *ctx, GLenum mode,
                                   GLsizei primcount, const GLint *basevertex)
 {
    struct _mesa_index_buffer ib;
-   struct _mesa_prim *prim;
-   unsigned int index_type_size = sizeof_ib_type(type);
    uintptr_t min_index_ptr, max_index_ptr;
    GLboolean fallback = GL_FALSE;
    int i;
@@ -1208,18 +1190,14 @@ _mesa_validated_multidrawelements(struct gl_context *ctx, GLenum mode,
    if (primcount == 0)
       return;
 
-   prim = calloc(primcount, sizeof(*prim));
-   if (prim == NULL) {
-      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMultiDrawElements");
-      return;
-   }
+   get_index_size(type, &ib);
 
    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]);
+                           (count[i] << ib.index_size_shift));
    }
 
    /* Check if we can handle this thing as a bunch of index offsets from the
@@ -1228,52 +1206,46 @@ _mesa_validated_multidrawelements(struct gl_context *ctx, GLenum mode,
     * Check that the difference between each prim's indexes is a multiple of
     * the index/element size.
     */
-   if (index_type_size != 1) {
+   if (ib.index_size_shift) {
       for (i = 0; i < primcount; i++) {
-         if ((((uintptr_t) indices[i] - min_index_ptr) % index_type_size) !=
-             0) {
+         if ((((uintptr_t) indices[i] - min_index_ptr) &
+              ((1 << ib.index_size_shift) - 1)) != 0) {
             fallback = GL_TRUE;
             break;
          }
       }
    }
 
-   /* Draw primitives individually if one count is zero, so we can easily skip
-    * that primitive.
-    */
-   for (i = 0; i < primcount; i++) {
-      if (count[i] == 0) {
+   if (ctx->Const.MultiDrawWithUserIndices) {
+      /* Check whether prim[i].start would overflow. */
+      if (((max_index_ptr - min_index_ptr) >> ib.index_size_shift) > UINT_MAX)
+         fallback = GL_TRUE;
+   } else {
+      /* 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.VAO->IndexBufferObj))
          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.VAO->IndexBufferObj))
-      fallback = GL_TRUE;
-
    if (!fallback) {
-      ib.count = (max_index_ptr - min_index_ptr) / index_type_size;
-      ib.index_size = sizeof_ib_type(type);
+      struct _mesa_prim *prim;
+
+      ALLOC_PRIMS(prim, primcount, "glMultiDrawElements");
+
+      ib.count = (max_index_ptr - min_index_ptr) >> ib.index_size_shift;
       ib.obj = ctx->Array.VAO->IndexBufferObj;
       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].pad = 0;
+         prim[i].begin = 1;
+         prim[i].end = 1;
          prim[i].mode = mode;
          prim[i].start =
-            ((uintptr_t) indices[i] - min_index_ptr) / index_type_size;
+            ((uintptr_t) indices[i] - min_index_ptr) >> ib.index_size_shift;
          prim[i].count = count[i];
-         prim[i].indexed = 1;
-         prim[i].num_instances = 1;
-         prim[i].base_instance = 0;
          prim[i].draw_id = i;
-         prim[i].is_indirect = 0;
          if (basevertex != NULL)
             prim[i].basevertex = basevertex[i];
          else
@@ -1281,50 +1253,44 @@ _mesa_validated_multidrawelements(struct gl_context *ctx, GLenum mode,
       }
 
       ctx->Driver.Draw(ctx, prim, primcount, &ib,
-                       false, 0, ~0, NULL, 0, NULL);
+                       false, 0, ~0, 1, 0, NULL, 0);
+      FREE_PRIMS(prim, primcount);
    }
    else {
       /* render one prim at a time */
       for (i = 0; i < primcount; i++) {
          if (count[i] == 0)
             continue;
+
          ib.count = count[i];
-         ib.index_size = sizeof_ib_type(type);
          ib.obj = ctx->Array.VAO->IndexBufferObj;
          ib.ptr = indices[i];
 
-         prim[0].begin = 1;
-         prim[0].end = 1;
-         prim[0].pad = 0;
-         prim[0].mode = mode;
-         prim[0].start = 0;
-         prim[0].count = count[i];
-         prim[0].indexed = 1;
-         prim[0].num_instances = 1;
-         prim[0].base_instance = 0;
-         prim[0].draw_id = i;
-         prim[0].is_indirect = 0;
+         struct _mesa_prim prim;
+         prim.begin = 1;
+         prim.end = 1;
+         prim.mode = mode;
+         prim.start = 0;
+         prim.count = count[i];
+         prim.draw_id = i;
          if (basevertex != NULL)
-            prim[0].basevertex = basevertex[i];
+            prim.basevertex = basevertex[i];
          else
-            prim[0].basevertex = 0;
+            prim.basevertex = 0;
 
-         ctx->Driver.Draw(ctx, prim, 1, &ib, false, 0, ~0, NULL, 0, NULL);
+         ctx->Driver.Draw(ctx, &prim, 1, &ib, false, 0, ~0, 1, 0, NULL, 0);
       }
    }
 
-   free(prim);
-
    if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
       _mesa_flush(ctx);
    }
 }
 
 
-static void GLAPIENTRY
-_mesa_exec_MultiDrawElements(GLenum mode,
-                             const GLsizei *count, GLenum type,
-                             const GLvoid * const *indices, GLsizei primcount)
+void GLAPIENTRY
+_mesa_MultiDrawElements(GLenum mode, const GLsizei *count, GLenum type,
+                        const GLvoid * const *indices, GLsizei primcount)
 {
    GET_CURRENT_CONTEXT(ctx);
 
@@ -1344,12 +1310,12 @@ _mesa_exec_MultiDrawElements(GLenum mode,
 }
 
 
-static void GLAPIENTRY
-_mesa_exec_MultiDrawElementsBaseVertex(GLenum mode,
-                                       const GLsizei *count, GLenum type,
-                                       const GLvoid * const *indices,
-                                       GLsizei primcount,
-                                       const GLsizei *basevertex)
+void GLAPIENTRY
+_mesa_MultiDrawElementsBaseVertex(GLenum mode,
+                                  const GLsizei *count, GLenum type,
+                                  const GLvoid * const *indices,
+                                  GLsizei primcount,
+                                  const GLsizei *basevertex)
 {
    GET_CURRENT_CONTEXT(ctx);
 
@@ -1420,15 +1386,13 @@ _mesa_draw_transform_feedback(struct gl_context *ctx, GLenum mode,
    prim.begin = 1;
    prim.end = 1;
    prim.mode = mode;
-   prim.num_instances = numInstances;
-   prim.base_instance = 0;
-   prim.is_indirect = 0;
 
    /* Maybe we should do some primitive splitting for primitive restart
     * (like in DrawArrays), but we have no way to know how many vertices
     * will be rendered. */
 
-   ctx->Driver.Draw(ctx, &prim, 1, NULL, GL_FALSE, 0, ~0, obj, stream, NULL);
+   ctx->Driver.Draw(ctx, &prim, 1, NULL, GL_FALSE, 0, ~0, numInstances, 0,
+                    obj, stream);
 
    if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
       _mesa_flush(ctx);
@@ -1444,8 +1408,8 @@ _mesa_draw_transform_feedback(struct gl_context *ctx, GLenum mode,
  * glVertexPointer, glColorPointer, etc.
  * Part of GL_ARB_transform_feedback2.
  */
-static void GLAPIENTRY
-_mesa_exec_DrawTransformFeedback(GLenum mode, GLuint name)
+void GLAPIENTRY
+_mesa_DrawTransformFeedback(GLenum mode, GLuint name)
 {
    GET_CURRENT_CONTEXT(ctx);
    struct gl_transform_feedback_object *obj =
@@ -1509,54 +1473,19 @@ _mesa_exec_DrawTransformFeedbackStreamInstanced(GLenum mode, GLuint name,
 
 
 static void
-_mesa_validated_drawarraysindirect(struct gl_context *ctx,
-                                   GLenum mode, const GLvoid *indirect)
-{
-   ctx->Driver.DrawIndirect(ctx, mode,
-                            ctx->DrawIndirectBuffer, (GLsizeiptr) indirect,
-                            1 /* draw_count */ , 16 /* stride */ ,
-                            NULL, 0, NULL);
-
-   if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
-      _mesa_flush(ctx);
-}
-
-
-static void
-_mesa_validated_multidrawarraysindirect(struct gl_context *ctx,
-                                        GLenum mode,
-                                        const GLvoid *indirect,
-                                        GLsizei primcount, GLsizei stride)
+_mesa_validated_multidrawarraysindirect(struct gl_context *ctx, GLenum mode,
+                                        GLintptr indirect,
+                                        GLintptr drawcount_offset,
+                                        GLsizei drawcount, GLsizei stride,
+                                        struct gl_buffer_object *drawcount_buffer)
 {
-   GLsizeiptr offset = (GLsizeiptr) indirect;
-
-   if (primcount == 0)
+   /* If drawcount_buffer is set, drawcount is the maximum draw count.*/
+   if (drawcount == 0)
       return;
 
-   ctx->Driver.DrawIndirect(ctx, mode, ctx->DrawIndirectBuffer, offset,
-                            primcount, stride, NULL, 0, NULL);
-
-   if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
-      _mesa_flush(ctx);
-}
-
-
-static void
-_mesa_validated_drawelementsindirect(struct gl_context *ctx,
-                                     GLenum mode, GLenum type,
-                                     const GLvoid *indirect)
-{
-   struct _mesa_index_buffer ib;
-
-   ib.count = 0;                /* unknown */
-   ib.index_size = sizeof_ib_type(type);
-   ib.obj = ctx->Array.VAO->IndexBufferObj;
-   ib.ptr = NULL;
-
-   ctx->Driver.DrawIndirect(ctx, mode,
-                            ctx->DrawIndirectBuffer, (GLsizeiptr) indirect,
-                            1 /* draw_count */ , 20 /* stride */ ,
-                            NULL, 0, &ib);
+   ctx->Driver.DrawIndirect(ctx, mode, ctx->DrawIndirectBuffer, indirect,
+                            drawcount, stride, drawcount_buffer,
+                            drawcount_offset, NULL);
 
    if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
       _mesa_flush(ctx);
@@ -1566,25 +1495,25 @@ _mesa_validated_drawelementsindirect(struct gl_context *ctx,
 static void
 _mesa_validated_multidrawelementsindirect(struct gl_context *ctx,
                                           GLenum mode, GLenum type,
-                                          const GLvoid *indirect,
-                                          GLsizei primcount, GLsizei stride)
+                                          GLintptr indirect,
+                                          GLintptr drawcount_offset,
+                                          GLsizei drawcount, GLsizei stride,
+                                          struct gl_buffer_object *drawcount_buffer)
 {
-   struct _mesa_index_buffer ib;
-   GLsizeiptr offset = (GLsizeiptr) indirect;
-
-   if (primcount == 0)
+   /* If drawcount_buffer is set, drawcount is the maximum draw count.*/
+   if (drawcount == 0)
       return;
 
    /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
-
+   struct _mesa_index_buffer ib;
    ib.count = 0;                /* unknown */
-   ib.index_size = sizeof_ib_type(type);
    ib.obj = ctx->Array.VAO->IndexBufferObj;
    ib.ptr = NULL;
+   get_index_size(type, &ib);
 
-   ctx->Driver.DrawIndirect(ctx, mode,
-                            ctx->DrawIndirectBuffer, offset,
-                            primcount, stride, NULL, 0, &ib);
+   ctx->Driver.DrawIndirect(ctx, mode, ctx->DrawIndirectBuffer, indirect,
+                            drawcount, stride, drawcount_buffer,
+                            drawcount_offset, &ib);
 
    if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
       _mesa_flush(ctx);
@@ -1636,7 +1565,8 @@ _mesa_exec_DrawArraysIndirect(GLenum mode, const GLvoid *indirect)
    if (skip_validated_draw(ctx))
       return;
 
-   _mesa_validated_drawarraysindirect(ctx, mode, indirect);
+   _mesa_validated_multidrawarraysindirect(ctx, mode, (GLintptr)indirect,
+                                           0, 1, 16, NULL);
 }
 
 
@@ -1675,7 +1605,7 @@ _mesa_exec_DrawElementsIndirect(GLenum mode, GLenum type, const GLvoid *indirect
 
          /* Convert offset to pointer */
          void *offset = (void *)
-            ((cmd->firstIndex * _mesa_sizeof_type(type)) & 0xffffffffUL);
+            (uintptr_t)((cmd->firstIndex * _mesa_sizeof_type(type)) & 0xffffffffUL);
 
          _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(mode, cmd->count,
                                                                 type, offset,
@@ -1702,7 +1632,9 @@ _mesa_exec_DrawElementsIndirect(GLenum mode, GLenum type, const GLvoid *indirect
    if (skip_validated_draw(ctx))
       return;
 
-   _mesa_validated_drawelementsindirect(ctx, mode, type, indirect);
+   _mesa_validated_multidrawelementsindirect(ctx, mode, type,
+                                             (GLintptr)indirect, 0,
+                                             1, 20, NULL);
 }
 
 
@@ -1734,7 +1666,7 @@ _mesa_exec_MultiDrawArraysIndirect(GLenum mode, const GLvoid *indirect,
                                            "glMultiDrawArraysIndirect"))
          return;
 
-      const ubyte *ptr = (const ubyte *) indirect;
+      const uint8_t *ptr = (const uint8_t *) indirect;
       for (unsigned i = 0; i < primcount; i++) {
          DrawArraysIndirectCommand *cmd = (DrawArraysIndirectCommand *) ptr;
          _mesa_exec_DrawArraysInstancedBaseInstance(mode, cmd->first,
@@ -1767,8 +1699,8 @@ _mesa_exec_MultiDrawArraysIndirect(GLenum mode, const GLvoid *indirect,
    if (skip_validated_draw(ctx))
       return;
 
-   _mesa_validated_multidrawarraysindirect(ctx, mode, indirect,
-                                           primcount, stride);
+   _mesa_validated_multidrawarraysindirect(ctx, mode, (GLintptr)indirect, 0,
+                                           primcount, stride, NULL);
 }
 
 
@@ -1816,7 +1748,7 @@ _mesa_exec_MultiDrawElementsIndirect(GLenum mode, GLenum type,
                                            "glMultiDrawArraysIndirect"))
          return;
 
-      const ubyte *ptr = (const ubyte *) indirect;
+      const uint8_t *ptr = (const uint8_t *) indirect;
       for (unsigned i = 0; i < primcount; i++) {
          _mesa_exec_DrawElementsIndirect(mode, type, ptr);
 
@@ -1846,62 +1778,9 @@ _mesa_exec_MultiDrawElementsIndirect(GLenum mode, GLenum type,
    if (skip_validated_draw(ctx))
       return;
 
-   _mesa_validated_multidrawelementsindirect(ctx, mode, type, indirect,
-                                             primcount, stride);
-}
-
-
-static void
-_mesa_validated_multidrawarraysindirectcount(struct gl_context *ctx,
-                                             GLenum mode,
-                                             GLintptr indirect,
-                                             GLintptr drawcount_offset,
-                                             GLsizei maxdrawcount,
-                                             GLsizei stride)
-{
-   GLsizeiptr offset = indirect;
-
-   if (maxdrawcount == 0)
-      return;
-
-   ctx->Driver.DrawIndirect(ctx, mode,
-                            ctx->DrawIndirectBuffer, offset,
-                            maxdrawcount, stride,
-                            ctx->ParameterBuffer, drawcount_offset, NULL);
-
-   if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
-      _mesa_flush(ctx);
-}
-
-
-static void
-_mesa_validated_multidrawelementsindirectcount(struct gl_context *ctx,
-                                               GLenum mode, GLenum type,
-                                               GLintptr indirect,
-                                               GLintptr drawcount_offset,
-                                               GLsizei maxdrawcount,
-                                               GLsizei stride)
-{
-   struct _mesa_index_buffer ib;
-   GLsizeiptr offset = (GLsizeiptr) indirect;
-
-   if (maxdrawcount == 0)
-      return;
-
-   /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
-
-   ib.count = 0;                /* unknown */
-   ib.index_size = sizeof_ib_type(type);
-   ib.obj = ctx->Array.VAO->IndexBufferObj;
-   ib.ptr = NULL;
-
-   ctx->Driver.DrawIndirect(ctx, mode,
-                            ctx->DrawIndirectBuffer, offset,
-                            maxdrawcount, stride,
-                            ctx->ParameterBuffer, drawcount_offset, &ib);
-
-   if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
-      _mesa_flush(ctx);
+   _mesa_validated_multidrawelementsindirect(ctx, mode, type,
+                                             (GLintptr)indirect, 0, primcount,
+                                             stride, NULL);
 }
 
 
@@ -1941,9 +1820,9 @@ _mesa_exec_MultiDrawArraysIndirectCount(GLenum mode, GLintptr indirect,
    if (skip_validated_draw(ctx))
       return;
 
-   _mesa_validated_multidrawarraysindirectcount(ctx, mode, indirect,
-                                                drawcount_offset,
-                                                maxdrawcount, stride);
+   _mesa_validated_multidrawarraysindirect(ctx, mode, indirect,
+                                           drawcount_offset, maxdrawcount,
+                                           stride, ctx->ParameterBuffer);
 }
 
 
@@ -1984,9 +1863,9 @@ _mesa_exec_MultiDrawElementsIndirectCount(GLenum mode, GLenum type,
    if (skip_validated_draw(ctx))
       return;
 
-   _mesa_validated_multidrawelementsindirectcount(ctx, mode, type, indirect,
-                                                  drawcount_offset, maxdrawcount,
-                                                  stride);
+   _mesa_validated_multidrawelementsindirect(ctx, mode, type, indirect,
+                                             drawcount_offset, maxdrawcount,
+                                             stride, ctx->ParameterBuffer);
 }
 
 
@@ -1997,15 +1876,15 @@ void
 _mesa_initialize_exec_dispatch(const struct gl_context *ctx,
                                struct _glapi_table *exec)
 {
-   SET_DrawArrays(exec, _mesa_exec_DrawArrays);
-   SET_DrawElements(exec, _mesa_exec_DrawElements);
+   SET_DrawArrays(exec, _mesa_DrawArrays);
+   SET_DrawElements(exec, _mesa_DrawElements);
 
    if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
-      SET_DrawRangeElements(exec, _mesa_exec_DrawRangeElements);
+      SET_DrawRangeElements(exec, _mesa_DrawRangeElements);
    }
 
    SET_MultiDrawArrays(exec, _mesa_exec_MultiDrawArrays);
-   SET_MultiDrawElementsEXT(exec, _mesa_exec_MultiDrawElements);
+   SET_MultiDrawElementsEXT(exec, _mesa_MultiDrawElements);
 
    if (ctx->API == API_OPENGL_COMPAT) {
       SET_Rectf(exec, _mesa_exec_Rectf);
@@ -2015,13 +1894,13 @@ _mesa_initialize_exec_dispatch(const struct gl_context *ctx,
 
    if (ctx->API != API_OPENGLES &&
        ctx->Extensions.ARB_draw_elements_base_vertex) {
-      SET_DrawElementsBaseVertex(exec, _mesa_exec_DrawElementsBaseVertex);
+      SET_DrawElementsBaseVertex(exec, _mesa_DrawElementsBaseVertex);
       SET_MultiDrawElementsBaseVertex(exec,
-                                      _mesa_exec_MultiDrawElementsBaseVertex);
+                                      _mesa_MultiDrawElementsBaseVertex);
 
       if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
          SET_DrawRangeElementsBaseVertex(exec,
-                                         _mesa_exec_DrawRangeElementsBaseVertex);
+                                         _mesa_DrawRangeElementsBaseVertex);
          SET_DrawElementsInstancedBaseVertex(exec,
                                              _mesa_exec_DrawElementsInstancedBaseVertex);
       }
@@ -2042,12 +1921,12 @@ _mesa_initialize_exec_dispatch(const struct gl_context *ctx,
    }
 
    if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
-      SET_DrawArraysInstancedARB(exec, _mesa_exec_DrawArraysInstanced);
+      SET_DrawArraysInstancedARB(exec, _mesa_DrawArraysInstanced);
       SET_DrawElementsInstancedARB(exec, _mesa_exec_DrawElementsInstanced);
    }
 
    if (_mesa_is_desktop_gl(ctx)) {
-      SET_DrawTransformFeedback(exec, _mesa_exec_DrawTransformFeedback);
+      SET_DrawTransformFeedback(exec, _mesa_DrawTransformFeedback);
       SET_DrawTransformFeedbackStream(exec,
                                       _mesa_exec_DrawTransformFeedbackStream);
       SET_DrawTransformFeedbackInstanced(exec,
@@ -2065,87 +1944,6 @@ _mesa_initialize_exec_dispatch(const struct gl_context *ctx,
 
 
 
-/**
- * The following functions are only used for OpenGL ES 1/2 support.
- * And some aren't even supported (yet) in ES 1/2.
- */
-
-
-void GLAPIENTRY
-_mesa_DrawArrays(GLenum mode, GLint first, GLsizei count)
-{
-   _mesa_exec_DrawArrays(mode, first, count);
-}
-
-
-void GLAPIENTRY
-_mesa_DrawArraysInstanced(GLenum mode, GLint first, GLsizei count,
-                          GLsizei primcount)
-{
-   _mesa_exec_DrawArraysInstanced(mode, first, count, primcount);
-}
-
-
-void GLAPIENTRY
-_mesa_DrawElements(GLenum mode, GLsizei count, GLenum type,
-                   const GLvoid *indices)
-{
-   _mesa_exec_DrawElements(mode, count, type, indices);
-}
-
-
-void GLAPIENTRY
-_mesa_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
-                             const GLvoid *indices, GLint basevertex)
-{
-   _mesa_exec_DrawElementsBaseVertex(mode, count, type, indices, basevertex);
-}
-
-
-void GLAPIENTRY
-_mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
-                        GLenum type, const GLvoid * indices)
-{
-   _mesa_exec_DrawRangeElements(mode, start, end, count, type, indices);
-}
-
-
-void GLAPIENTRY
-_mesa_DrawRangeElementsBaseVertex(GLenum mode, GLuint start, GLuint end,
-                                  GLsizei count, GLenum type,
-                                  const GLvoid *indices, GLint basevertex)
-{
-   _mesa_exec_DrawRangeElementsBaseVertex(mode, start, end, count, type,
-                                          indices, basevertex);
-}
-
-
-void GLAPIENTRY
-_mesa_MultiDrawElementsEXT(GLenum mode, const GLsizei *count, GLenum type,
-                           const GLvoid ** indices, GLsizei primcount)
-{
-   _mesa_exec_MultiDrawElements(mode, count, type, indices, primcount);
-}
-
-
-void GLAPIENTRY
-_mesa_MultiDrawElementsBaseVertex(GLenum mode,
-                                  const GLsizei *count, GLenum type,
-                                  const GLvoid **indices, GLsizei primcount,
-                                  const GLint *basevertex)
-{
-   _mesa_exec_MultiDrawElementsBaseVertex(mode, count, type, indices,
-                                          primcount, basevertex);
-}
-
-
-void GLAPIENTRY
-_mesa_DrawTransformFeedback(GLenum mode, GLuint name)
-{
-   _mesa_exec_DrawTransformFeedback(mode, name);
-}
-
-
 /* GL_IBM_multimode_draw_arrays */
 void GLAPIENTRY
 _mesa_MultiModeDrawArraysIBM( const GLenum * mode, const GLint * first,
@@ -2187,76 +1985,3 @@ _mesa_MultiModeDrawElementsIBM( const GLenum * mode, const GLsizei * count,
       }
    }
 }
-
-
-/*
- * Helper function for _mesa_draw_indirect below that additionally takes a zero
- * initialized array of _mesa_prim scratch space memory as the last argument.
- */
-static void
-draw_indirect(struct gl_context *ctx, GLuint mode,
-              struct gl_buffer_object *indirect_data,
-              GLsizeiptr indirect_offset, unsigned draw_count,
-              unsigned stride,
-              struct gl_buffer_object *indirect_draw_count_buffer,
-              GLsizeiptr indirect_draw_count_offset,
-              const struct _mesa_index_buffer *ib,
-              struct _mesa_prim *prim)
-{
-   prim[0].begin = 1;
-   prim[draw_count - 1].end = 1;
-   for (unsigned i = 0; i < draw_count; ++i, indirect_offset += stride) {
-      prim[i].mode = mode;
-      prim[i].indexed = !!ib;
-      prim[i].indirect_offset = indirect_offset;
-      prim[i].is_indirect = 1;
-      prim[i].draw_id = i;
-   }
-
-   /* This should always be true at this time */
-   assert(indirect_data == ctx->DrawIndirectBuffer);
-
-   ctx->Driver.Draw(ctx, prim, draw_count, ib, false, 0u, ~0u,
-                    NULL, 0, indirect_data);
-}
-
-
-/*
- * Function to be put into dd_function_table::DrawIndirect as fallback.
- * Calls into dd_function_table::Draw past adapting call arguments.
- * See dd_function_table::DrawIndirect for call argument documentation.
- */
-void
-_mesa_draw_indirect(struct gl_context *ctx, GLuint mode,
-                    struct gl_buffer_object *indirect_data,
-                    GLsizeiptr indirect_offset, unsigned draw_count,
-                    unsigned stride,
-                    struct gl_buffer_object *indirect_draw_count_buffer,
-                    GLsizeiptr indirect_draw_count_offset,
-                    const struct _mesa_index_buffer *ib)
-{
-   /* Use alloca for the prim space if we are somehow in bounds. */
-   if (draw_count*sizeof(struct _mesa_prim) < 1024) {
-      struct _mesa_prim *space = alloca(draw_count*sizeof(struct _mesa_prim));
-      memset(space, 0, draw_count*sizeof(struct _mesa_prim));
-
-      draw_indirect(ctx, mode, indirect_data, indirect_offset, draw_count,
-                    stride, indirect_draw_count_buffer,
-                    indirect_draw_count_offset, ib, space);
-   } else {
-      struct _mesa_prim *space = calloc(draw_count, sizeof(struct _mesa_prim));
-      if (space == NULL) {
-         _mesa_error(ctx, GL_OUT_OF_MEMORY, "gl%sDraw%sIndirect%s",
-                     (draw_count > 1) ? "Multi" : "",
-                     ib ? "Elements" : "Arrays",
-                     indirect_data ? "CountARB" : "");
-         return;
-      }
-
-      draw_indirect(ctx, mode, indirect_data, indirect_offset, draw_count,
-                    stride, indirect_draw_count_buffer,
-                    indirect_draw_count_offset, ib, space);
-
-      free(space);
-   }
-}