Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / mesa / state_tracker / st_atom_array.c
index dcead27ea3640354aafd3cc0b0381b9da506293f..1faf72b0db5e706dccdb496d47917dc7526bc1be 100644 (file)
 
 #include "cso_cache/cso_context.h"
 #include "util/u_math.h"
+#include "util/u_upload_mgr.h"
 #include "main/bufferobj.h"
 #include "main/glformats.h"
+#include "main/varray.h"
+#include "main/arrayobj.h"
 
-
-static GLuint double_types[4] = {
-   PIPE_FORMAT_R64_FLOAT,
-   PIPE_FORMAT_R64G64_FLOAT,
-   PIPE_FORMAT_R64G64B64_FLOAT,
-   PIPE_FORMAT_R64G64B64A64_FLOAT
-};
-
-static GLuint float_types[4] = {
-   PIPE_FORMAT_R32_FLOAT,
-   PIPE_FORMAT_R32G32_FLOAT,
-   PIPE_FORMAT_R32G32B32_FLOAT,
-   PIPE_FORMAT_R32G32B32A32_FLOAT
-};
-
-static GLuint half_float_types[4] = {
-   PIPE_FORMAT_R16_FLOAT,
-   PIPE_FORMAT_R16G16_FLOAT,
-   PIPE_FORMAT_R16G16B16_FLOAT,
-   PIPE_FORMAT_R16G16B16A16_FLOAT
-};
-
-static GLuint uint_types_norm[4] = {
-   PIPE_FORMAT_R32_UNORM,
-   PIPE_FORMAT_R32G32_UNORM,
-   PIPE_FORMAT_R32G32B32_UNORM,
-   PIPE_FORMAT_R32G32B32A32_UNORM
-};
-
-static GLuint uint_types_scale[4] = {
-   PIPE_FORMAT_R32_USCALED,
-   PIPE_FORMAT_R32G32_USCALED,
-   PIPE_FORMAT_R32G32B32_USCALED,
-   PIPE_FORMAT_R32G32B32A32_USCALED
-};
-
-static GLuint uint_types_int[4] = {
-   PIPE_FORMAT_R32_UINT,
-   PIPE_FORMAT_R32G32_UINT,
-   PIPE_FORMAT_R32G32B32_UINT,
-   PIPE_FORMAT_R32G32B32A32_UINT
-};
-
-static GLuint int_types_norm[4] = {
-   PIPE_FORMAT_R32_SNORM,
-   PIPE_FORMAT_R32G32_SNORM,
-   PIPE_FORMAT_R32G32B32_SNORM,
-   PIPE_FORMAT_R32G32B32A32_SNORM
-};
-
-static GLuint int_types_scale[4] = {
-   PIPE_FORMAT_R32_SSCALED,
-   PIPE_FORMAT_R32G32_SSCALED,
-   PIPE_FORMAT_R32G32B32_SSCALED,
-   PIPE_FORMAT_R32G32B32A32_SSCALED
-};
-
-static GLuint int_types_int[4] = {
-   PIPE_FORMAT_R32_SINT,
-   PIPE_FORMAT_R32G32_SINT,
-   PIPE_FORMAT_R32G32B32_SINT,
-   PIPE_FORMAT_R32G32B32A32_SINT
-};
-
-static GLuint ushort_types_norm[4] = {
-   PIPE_FORMAT_R16_UNORM,
-   PIPE_FORMAT_R16G16_UNORM,
-   PIPE_FORMAT_R16G16B16_UNORM,
-   PIPE_FORMAT_R16G16B16A16_UNORM
-};
-
-static GLuint ushort_types_scale[4] = {
-   PIPE_FORMAT_R16_USCALED,
-   PIPE_FORMAT_R16G16_USCALED,
-   PIPE_FORMAT_R16G16B16_USCALED,
-   PIPE_FORMAT_R16G16B16A16_USCALED
-};
-
-static GLuint ushort_types_int[4] = {
-   PIPE_FORMAT_R16_UINT,
-   PIPE_FORMAT_R16G16_UINT,
-   PIPE_FORMAT_R16G16B16_UINT,
-   PIPE_FORMAT_R16G16B16A16_UINT
-};
-
-static GLuint short_types_norm[4] = {
-   PIPE_FORMAT_R16_SNORM,
-   PIPE_FORMAT_R16G16_SNORM,
-   PIPE_FORMAT_R16G16B16_SNORM,
-   PIPE_FORMAT_R16G16B16A16_SNORM
-};
-
-static GLuint short_types_scale[4] = {
-   PIPE_FORMAT_R16_SSCALED,
-   PIPE_FORMAT_R16G16_SSCALED,
-   PIPE_FORMAT_R16G16B16_SSCALED,
-   PIPE_FORMAT_R16G16B16A16_SSCALED
-};
-
-static GLuint short_types_int[4] = {
-   PIPE_FORMAT_R16_SINT,
-   PIPE_FORMAT_R16G16_SINT,
-   PIPE_FORMAT_R16G16B16_SINT,
-   PIPE_FORMAT_R16G16B16A16_SINT
-};
-
-static GLuint ubyte_types_norm[4] = {
-   PIPE_FORMAT_R8_UNORM,
-   PIPE_FORMAT_R8G8_UNORM,
-   PIPE_FORMAT_R8G8B8_UNORM,
-   PIPE_FORMAT_R8G8B8A8_UNORM
-};
-
-static GLuint ubyte_types_scale[4] = {
-   PIPE_FORMAT_R8_USCALED,
-   PIPE_FORMAT_R8G8_USCALED,
-   PIPE_FORMAT_R8G8B8_USCALED,
-   PIPE_FORMAT_R8G8B8A8_USCALED
-};
-
-static GLuint ubyte_types_int[4] = {
-   PIPE_FORMAT_R8_UINT,
-   PIPE_FORMAT_R8G8_UINT,
-   PIPE_FORMAT_R8G8B8_UINT,
-   PIPE_FORMAT_R8G8B8A8_UINT
-};
-
-static GLuint byte_types_norm[4] = {
-   PIPE_FORMAT_R8_SNORM,
-   PIPE_FORMAT_R8G8_SNORM,
-   PIPE_FORMAT_R8G8B8_SNORM,
-   PIPE_FORMAT_R8G8B8A8_SNORM
-};
-
-static GLuint byte_types_scale[4] = {
-   PIPE_FORMAT_R8_SSCALED,
-   PIPE_FORMAT_R8G8_SSCALED,
-   PIPE_FORMAT_R8G8B8_SSCALED,
-   PIPE_FORMAT_R8G8B8A8_SSCALED
-};
-
-static GLuint byte_types_int[4] = {
-   PIPE_FORMAT_R8_SINT,
-   PIPE_FORMAT_R8G8_SINT,
-   PIPE_FORMAT_R8G8B8_SINT,
-   PIPE_FORMAT_R8G8B8A8_SINT
-};
-
-static GLuint fixed_types[4] = {
-   PIPE_FORMAT_R32_FIXED,
-   PIPE_FORMAT_R32G32_FIXED,
-   PIPE_FORMAT_R32G32B32_FIXED,
-   PIPE_FORMAT_R32G32B32A32_FIXED
-};
-
-
-/**
- * Return a PIPE_FORMAT_x for the given GL datatype and size.
- */
-enum pipe_format
-st_pipe_vertex_format(GLenum type, GLuint size, GLenum format,
-                      GLboolean normalized, GLboolean integer)
-{
-   assert((type >= GL_BYTE && type <= GL_DOUBLE) ||
-          type == GL_FIXED || type == GL_HALF_FLOAT ||
-          type == GL_INT_2_10_10_10_REV ||
-          type == GL_UNSIGNED_INT_2_10_10_10_REV ||
-          type == GL_UNSIGNED_INT_10F_11F_11F_REV);
-   assert(size >= 1);
-   assert(size <= 4);
-   assert(format == GL_RGBA || format == GL_BGRA);
-
-   if (type == GL_INT_2_10_10_10_REV ||
-       type == GL_UNSIGNED_INT_2_10_10_10_REV) {
-      assert(size == 4);
-      assert(!integer);
-
-      if (format == GL_BGRA) {
-         if (type == GL_INT_2_10_10_10_REV) {
-            if (normalized)
-               return PIPE_FORMAT_B10G10R10A2_SNORM;
-            else
-               return PIPE_FORMAT_B10G10R10A2_SSCALED;
-         } else {
-            if (normalized)
-               return PIPE_FORMAT_B10G10R10A2_UNORM;
-            else
-               return PIPE_FORMAT_B10G10R10A2_USCALED;
-         }
-      } else {
-         if (type == GL_INT_2_10_10_10_REV) {
-            if (normalized)
-               return PIPE_FORMAT_R10G10B10A2_SNORM;
-            else
-               return PIPE_FORMAT_R10G10B10A2_SSCALED;
-         } else {
-            if (normalized)
-               return PIPE_FORMAT_R10G10B10A2_UNORM;
-            else
-               return PIPE_FORMAT_R10G10B10A2_USCALED;
-         }
-      }
-   }
-
-   if (type == GL_UNSIGNED_INT_10F_11F_11F_REV) {
-      assert(size == 3);
-      assert(!integer);
-      assert(format == GL_RGBA);
-
-      return PIPE_FORMAT_R11G11B10_FLOAT;
-   }
-
-   if (format == GL_BGRA) {
-      /* this is an odd-ball case */
-      assert(type == GL_UNSIGNED_BYTE);
-      assert(normalized);
-      return PIPE_FORMAT_B8G8R8A8_UNORM;
-   }
-
-   if (integer) {
-      switch (type) {
-      case GL_INT: return int_types_int[size-1];
-      case GL_SHORT: return short_types_int[size-1];
-      case GL_BYTE: return byte_types_int[size-1];
-      case GL_UNSIGNED_INT: return uint_types_int[size-1];
-      case GL_UNSIGNED_SHORT: return ushort_types_int[size-1];
-      case GL_UNSIGNED_BYTE: return ubyte_types_int[size-1];
-      default: assert(0); return 0;
-      }
-   }
-   else if (normalized) {
-      switch (type) {
-      case GL_DOUBLE: return double_types[size-1];
-      case GL_FLOAT: return float_types[size-1];
-      case GL_HALF_FLOAT: return half_float_types[size-1];
-      case GL_INT: return int_types_norm[size-1];
-      case GL_SHORT: return short_types_norm[size-1];
-      case GL_BYTE: return byte_types_norm[size-1];
-      case GL_UNSIGNED_INT: return uint_types_norm[size-1];
-      case GL_UNSIGNED_SHORT: return ushort_types_norm[size-1];
-      case GL_UNSIGNED_BYTE: return ubyte_types_norm[size-1];
-      case GL_FIXED: return fixed_types[size-1];
-      default: assert(0); return 0;
-      }
-   }
-   else {
-      switch (type) {
-      case GL_DOUBLE: return double_types[size-1];
-      case GL_FLOAT: return float_types[size-1];
-      case GL_HALF_FLOAT: return half_float_types[size-1];
-      case GL_INT: return int_types_scale[size-1];
-      case GL_SHORT: return short_types_scale[size-1];
-      case GL_BYTE: return byte_types_scale[size-1];
-      case GL_UNSIGNED_INT: return uint_types_scale[size-1];
-      case GL_UNSIGNED_SHORT: return ushort_types_scale[size-1];
-      case GL_UNSIGNED_BYTE: return ubyte_types_scale[size-1];
-      case GL_FIXED: return fixed_types[size-1];
-      default: assert(0); return 0;
-      }
-   }
-   return PIPE_FORMAT_NONE; /* silence compiler warning */
-}
-
-static const struct gl_client_array *
-get_client_array(const struct st_vertex_program *vp,
-                 const struct gl_client_array **arrays,
-                 int attr)
-{
-   const GLuint mesaAttr = vp->index_to_input[attr];
-   /* st_program uses 0xffffffff to denote a double placeholder attribute */
-   if (mesaAttr == ST_DOUBLE_ATTRIB_PLACEHOLDER)
-      return NULL;
-   return arrays[mesaAttr];
-}
-
-/**
- * Examine the active arrays to determine if we have interleaved
- * vertex arrays all living in one VBO, or all living in user space.
- */
-static GLboolean
-is_interleaved_arrays(const struct st_vertex_program *vp,
-                      const struct st_vp_variant *vpv,
-                      const struct gl_client_array **arrays)
-{
-   GLuint attr;
-   const struct gl_buffer_object *firstBufObj = NULL;
-   GLint firstStride = -1;
-   const GLubyte *firstPtr = NULL;
-   GLboolean userSpaceBuffer = GL_FALSE;
-
-   for (attr = 0; attr < vpv->num_inputs; attr++) {
-      const struct gl_client_array *array;
-      const struct gl_buffer_object *bufObj;
-      GLsizei stride;
-
-      array = get_client_array(vp, arrays, attr);
-      if (!array)
-        continue;
-
-      stride = array->StrideB; /* in bytes */
-      bufObj = array->BufferObj;
-      if (attr == 0) {
-         /* save info about the first array */
-         firstStride = stride;
-         firstPtr = array->Ptr;
-         firstBufObj = bufObj;
-         userSpaceBuffer = !bufObj || !bufObj->Name;
-      }
-      else {
-         /* check if other arrays interleave with the first, in same buffer */
-         if (stride != firstStride)
-            return GL_FALSE; /* strides don't match */
-
-         if (bufObj != firstBufObj)
-            return GL_FALSE; /* arrays in different VBOs */
-
-         if (abs(array->Ptr - firstPtr) > firstStride)
-            return GL_FALSE; /* arrays start too far apart */
-
-         if ((!_mesa_is_bufferobj(bufObj)) != userSpaceBuffer)
-            return GL_FALSE; /* mix of VBO and user-space arrays */
-      }
-   }
-
-   return GL_TRUE;
-}
-
-static void init_velement(struct pipe_vertex_element *velement,
+static void set_velement(struct pipe_vertex_element *velement,
                           int src_offset, int format,
                           int instance_divisor, int vbo_index)
 {
@@ -385,310 +61,303 @@ static void init_velement(struct pipe_vertex_element *velement,
    assert(velement->src_format);
 }
 
-static void init_velement_lowered(struct st_context *st,
-                                  struct pipe_vertex_element *velements,
-                                  int src_offset, int format,
-                                  int instance_divisor, int vbo_index,
-                                  int nr_components, GLboolean doubles,
-                                  GLuint *attr_idx)
+static void init_velement_64bit(const struct st_vertex_program *vp,
+                                struct pipe_vertex_element *velements,
+                                const struct gl_vertex_format *vformat,
+                                int src_offset, int instance_divisor,
+                                int vbo_index, int idx)
 {
-   int idx = *attr_idx;
-   if (doubles) {
-      int lower_format;
+   const GLubyte nr_components = vformat->Size;
+   int lower_format;
 
-      if (nr_components == 1)
-         lower_format = PIPE_FORMAT_R32G32_UINT;
-      else if (nr_components >= 2)
-         lower_format = PIPE_FORMAT_R32G32B32A32_UINT;
+   if (nr_components < 2)
+      lower_format = PIPE_FORMAT_R32G32_UINT;
+   else
+      lower_format = PIPE_FORMAT_R32G32B32A32_UINT;
 
-      init_velement(&velements[idx], src_offset,
-                    lower_format, instance_divisor, vbo_index);
-      idx++;
+   set_velement(&velements[idx], src_offset,
+                lower_format, instance_divisor, vbo_index);
+   idx++;
 
-      if (nr_components > 2) {
+   if (idx < vp->num_inputs &&
+       vp->index_to_input[idx] == ST_DOUBLE_ATTRIB_PLACEHOLDER) {
+      if (nr_components >= 3) {
          if (nr_components == 3)
             lower_format = PIPE_FORMAT_R32G32_UINT;
-         else if (nr_components >= 4)
+         else
             lower_format = PIPE_FORMAT_R32G32B32A32_UINT;
 
-         init_velement(&velements[idx], src_offset + 4 * sizeof(float),
-                       lower_format, instance_divisor, vbo_index);
-         idx++;
+         set_velement(&velements[idx], src_offset + 4 * sizeof(float),
+                      lower_format, instance_divisor, vbo_index);
+      } else {
+         /* The values here are undefined. Fill in some conservative
+          * dummy values.
+          */
+         set_velement(&velements[idx], src_offset, PIPE_FORMAT_R32G32_UINT,
+                      instance_divisor, vbo_index);
       }
-   } else {
-      init_velement(&velements[idx], src_offset,
-                    format, instance_divisor, vbo_index);
-      idx++;
    }
-   *attr_idx = idx;
 }
 
-/**
- * Set up for drawing interleaved arrays that all live in one VBO
- * or all live in user space.
- * \param vbuffer  returns vertex buffer info
- * \param velements  returns vertex element info
+/* Always inline the non-64bit element code, so that the compiler can see
+ * that velements is on the stack.
  */
-static boolean
-setup_interleaved_attribs(struct st_context *st,
-                          const struct st_vertex_program *vp,
-                          const struct st_vp_variant *vpv,
-                          const struct gl_client_array **arrays,
-                          struct pipe_vertex_buffer *vbuffer,
-                          struct pipe_vertex_element velements[],
-                          unsigned *num_velements)
+static void ALWAYS_INLINE
+init_velement(const struct st_vertex_program *vp,
+              struct pipe_vertex_element *velements,
+              const struct gl_vertex_format *vformat,
+              int src_offset, int instance_divisor,
+              int vbo_index, int idx)
 {
-   GLuint attr, attr_idx;
-   const GLubyte *low_addr = NULL;
-   GLboolean usingVBO;      /* all arrays in a VBO? */
-   struct gl_buffer_object *bufobj;
-   GLsizei stride;
-
-   /* Find the lowest address of the arrays we're drawing,
-    * Init bufobj and stride.
-    */
-   if (vpv->num_inputs) {
-      const struct gl_client_array *array;
-
-      array = get_client_array(vp, arrays, 0);
-      assert(array);
-
-      /* Since we're doing interleaved arrays, we know there'll be at most
-       * one buffer object and the stride will be the same for all arrays.
-       * Grab them now.
-       */
-      bufobj = array->BufferObj;
-      stride = array->StrideB;
-
-      low_addr = arrays[vp->index_to_input[0]]->Ptr;
-
-      for (attr = 1; attr < vpv->num_inputs; attr++) {
-         const GLubyte *start;
-         array = get_client_array(vp, arrays, attr);
-         if (!array)
-            continue;
-         start = array->Ptr;
-         low_addr = MIN2(low_addr, start);
-      }
-   }
-   else {
-      /* not sure we'll ever have zero inputs, but play it safe */
-      bufobj = NULL;
-      stride = 0;
-      low_addr = 0;
-   }
-
-   /* are the arrays in user space? */
-   usingVBO = _mesa_is_bufferobj(bufobj);
-
-   attr_idx = 0;
-   for (attr = 0; attr < vpv->num_inputs; attr++) {
-      const struct gl_client_array *array;
-      unsigned src_offset;
-      unsigned src_format;
-
-      array = get_client_array(vp, arrays, attr);
-      if (!array)
-         continue;
-
-      src_offset = (unsigned) (array->Ptr - low_addr);
-      assert(array->_ElementSize ==
-             _mesa_bytes_per_vertex_attrib(array->Size, array->Type));
-
-      src_format = st_pipe_vertex_format(array->Type,
-                                         array->Size,
-                                         array->Format,
-                                         array->Normalized,
-                                         array->Integer);
-
-      init_velement_lowered(st, velements, src_offset, src_format,
-                            array->InstanceDivisor, 0,
-                            array->Size, array->Doubles, &attr_idx);
-   }
-
-   *num_velements = attr_idx;
-
-   /*
-    * Return the vbuffer info and setup user-space attrib info, if needed.
-    */
-   if (vpv->num_inputs == 0) {
-      /* just defensive coding here */
-      vbuffer->buffer = NULL;
-      vbuffer->user_buffer = NULL;
-      vbuffer->buffer_offset = 0;
-      vbuffer->stride = 0;
+   if (!vformat->Doubles) {
+      velements[idx].src_offset = src_offset;
+      velements[idx].src_format = vformat->_PipeFormat;
+      velements[idx].instance_divisor = instance_divisor;
+      velements[idx].vertex_buffer_index = vbo_index;
+      assert(velements[idx].src_format);
+      return;
    }
-   else if (usingVBO) {
-      /* all interleaved arrays in a VBO */
-      struct st_buffer_object *stobj = st_buffer_object(bufobj);
 
-      if (!stobj || !stobj->buffer) {
-         return FALSE; /* out-of-memory error probably */
-      }
-
-      vbuffer->buffer = stobj->buffer;
-      vbuffer->user_buffer = NULL;
-      vbuffer->buffer_offset = pointer_to_offset(low_addr);
-      vbuffer->stride = stride;
-   }
-   else {
-      /* all interleaved arrays in user memory */
-      vbuffer->buffer = NULL;
-      vbuffer->user_buffer = low_addr;
-      vbuffer->buffer_offset = 0;
-      vbuffer->stride = stride;
-   }
-   return TRUE;
+   init_velement_64bit(vp, velements, vformat, src_offset, instance_divisor,
+                       vbo_index, idx);
 }
 
-/**
- * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
- * vertex attribute.
- * \param vbuffer  returns vertex buffer info
- * \param velements  returns vertex element info
+/* ALWAYS_INLINE helps the compiler realize that most of the parameters are
+ * on the stack.
  */
-static boolean
-setup_non_interleaved_attribs(struct st_context *st,
-                              const struct st_vertex_program *vp,
-                              const struct st_vp_variant *vpv,
-                              const struct gl_client_array **arrays,
-                              struct pipe_vertex_buffer vbuffer[],
-                              struct pipe_vertex_element velements[],
-                              unsigned *num_velements)
+void
+#ifndef _MSC_VER /* MSVC doesn't like inlining public functions */
+ALWAYS_INLINE
+#endif
+st_setup_arrays(struct st_context *st,
+                const struct st_vertex_program *vp,
+                const struct st_common_variant *vp_variant,
+                struct cso_velems_state *velements,
+                struct pipe_vertex_buffer *vbuffer, unsigned *num_vbuffers,
+                bool *has_user_vertex_buffers)
 {
    struct gl_context *ctx = st->ctx;
-   GLuint attr, attr_idx = 0;
-
-   for (attr = 0; attr < vpv->num_inputs; attr++) {
-      const GLuint mesaAttr = vp->index_to_input[attr];
-      const struct gl_client_array *array;
-      struct gl_buffer_object *bufobj;
-      GLsizei stride;
-      unsigned src_format;
-
-      array = get_client_array(vp, arrays, attr);
-      if (!array) {
-         vbuffer[attr].buffer = NULL;
-         vbuffer[attr].user_buffer = NULL;
-         vbuffer[attr].buffer_offset = 0;
-         continue;
-      }
-
-      stride = array->StrideB;
-      bufobj = array->BufferObj;
-      assert(array->_ElementSize ==
-             _mesa_bytes_per_vertex_attrib(array->Size, array->Type));
-
-      if (_mesa_is_bufferobj(bufobj)) {
-         /* Attribute data is in a VBO.
-          * Recall that for VBOs, the gl_client_array->Ptr field is
-          * really an offset from the start of the VBO, not a pointer.
-          */
-         struct st_buffer_object *stobj = st_buffer_object(bufobj);
-
-         if (!stobj || !stobj->buffer) {
-            return FALSE; /* out-of-memory error probably */
+   const struct gl_vertex_array_object *vao = ctx->Array._DrawVAO;
+   const GLbitfield inputs_read = vp_variant->vert_attrib_mask;
+   const ubyte *input_to_index = vp->input_to_index;
+
+   /* Process attribute array data. */
+   GLbitfield mask = inputs_read & _mesa_draw_array_bits(ctx);
+   GLbitfield userbuf_attribs = inputs_read & _mesa_draw_user_array_bits(ctx);
+
+   *has_user_vertex_buffers = userbuf_attribs != 0;
+   st->draw_needs_minmax_index =
+      (userbuf_attribs & ~_mesa_draw_nonzero_divisor_bits(ctx)) != 0;
+
+   if (vao->IsDynamic) {
+      while (mask) {
+         const gl_vert_attrib attr = u_bit_scan(&mask);
+         const struct gl_array_attributes *const attrib =
+            _mesa_draw_array_attrib(vao, attr);
+         const struct gl_vertex_buffer_binding *const binding =
+            &vao->BufferBinding[attrib->BufferBindingIndex];
+         const unsigned bufidx = (*num_vbuffers)++;
+
+         /* Set the vertex buffer. */
+         if (binding->BufferObj) {
+            struct st_buffer_object *stobj = st_buffer_object(binding->BufferObj);
+
+            vbuffer[bufidx].buffer.resource = stobj ? stobj->buffer : NULL;
+            vbuffer[bufidx].is_user_buffer = false;
+            vbuffer[bufidx].buffer_offset = binding->Offset +
+                                            attrib->RelativeOffset;
+         } else {
+            vbuffer[bufidx].buffer.user = attrib->Ptr;
+            vbuffer[bufidx].is_user_buffer = true;
+            vbuffer[bufidx].buffer_offset = 0;
          }
+         vbuffer[bufidx].stride = binding->Stride; /* in bytes */
 
-         vbuffer[attr].buffer = stobj->buffer;
-         vbuffer[attr].user_buffer = NULL;
-         vbuffer[attr].buffer_offset = pointer_to_offset(array->Ptr);
+         /* Set the vertex element. */
+         init_velement(vp, velements->velems, &attrib->Format, 0,
+                       binding->InstanceDivisor, bufidx,
+                       input_to_index[attr]);
       }
-      else {
-         /* wrap user data */
-         void *ptr;
+      return;
+   }
 
-         if (array->Ptr) {
-            ptr = (void *) array->Ptr;
-         }
-         else {
-            /* no array, use ctx->Current.Attrib[] value */
-            ptr = (void *) ctx->Current.Attrib[mesaAttr];
-            stride = 0;
-         }
+   while (mask) {
+      /* The attribute index to start pulling a binding */
+      const gl_vert_attrib i = ffs(mask) - 1;
+      const struct gl_vertex_buffer_binding *const binding
+         = _mesa_draw_buffer_binding(vao, i);
+      const unsigned bufidx = (*num_vbuffers)++;
 
-         assert(ptr);
+      if (binding->BufferObj) {
+         /* Set the binding */
+         struct st_buffer_object *stobj = st_buffer_object(binding->BufferObj);
 
-         vbuffer[attr].buffer = NULL;
-         vbuffer[attr].user_buffer = ptr;
-         vbuffer[attr].buffer_offset = 0;
+         vbuffer[bufidx].buffer.resource = stobj ? stobj->buffer : NULL;
+         vbuffer[bufidx].is_user_buffer = false;
+         vbuffer[bufidx].buffer_offset = _mesa_draw_binding_offset(binding);
+      } else {
+         /* Set the binding */
+         const void *ptr = (const void *)_mesa_draw_binding_offset(binding);
+         vbuffer[bufidx].buffer.user = ptr;
+         vbuffer[bufidx].is_user_buffer = true;
+         vbuffer[bufidx].buffer_offset = 0;
       }
-
-      /* common-case setup */
-      vbuffer[attr].stride = stride; /* in bytes */
-
-      src_format = st_pipe_vertex_format(array->Type,
-                                         array->Size,
-                                         array->Format,
-                                         array->Normalized,
-                                         array->Integer);
-
-      init_velement_lowered(st, velements, 0, src_format,
-                            array->InstanceDivisor, attr,
-                            array->Size, array->Doubles, &attr_idx);
-
+      vbuffer[bufidx].stride = binding->Stride; /* in bytes */
+
+      const GLbitfield boundmask = _mesa_draw_bound_attrib_bits(binding);
+      GLbitfield attrmask = mask & boundmask;
+      /* Mark the those attributes as processed */
+      mask &= ~boundmask;
+      /* We can assume that we have array for the binding */
+      assert(attrmask);
+      /* Walk attributes belonging to the binding */
+      do {
+         const gl_vert_attrib attr = u_bit_scan(&attrmask);
+         const struct gl_array_attributes *const attrib
+            = _mesa_draw_array_attrib(vao, attr);
+         const GLuint off = _mesa_draw_attributes_relative_offset(attrib);
+         init_velement(vp, velements->velems, &attrib->Format, off,
+                       binding->InstanceDivisor, bufidx,
+                       input_to_index[attr]);
+      } while (attrmask);
    }
-
-   *num_velements = attr_idx;
-   return TRUE;
 }
 
-static void update_array(struct st_context *st)
+/* ALWAYS_INLINE helps the compiler realize that most of the parameters are
+ * on the stack.
+ *
+ * Return the index of the vertex buffer where current attribs have been
+ * uploaded.
+ */
+static int ALWAYS_INLINE
+st_setup_current(struct st_context *st,
+                 const struct st_vertex_program *vp,
+                 const struct st_common_variant *vp_variant,
+                 struct cso_velems_state *velements,
+                 struct pipe_vertex_buffer *vbuffer, unsigned *num_vbuffers)
 {
    struct gl_context *ctx = st->ctx;
-   const struct gl_client_array **arrays = ctx->Array._DrawArrays;
-   const struct st_vertex_program *vp;
-   const struct st_vp_variant *vpv;
-   struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS];
-   struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
-   unsigned num_vbuffers, num_velements;
-
-   st->vertex_array_out_of_memory = FALSE;
-
-   /* No drawing has been done yet, so do nothing. */
-   if (!arrays)
-      return;
-
-   /* vertex program validation must be done before this */
-   vp = st->vp;
-   vpv = st->vp_variant;
-
-   memset(velements, 0, sizeof(struct pipe_vertex_element) * vpv->num_inputs);
-
-   /*
-    * Setup the vbuffer[] and velements[] arrays.
-    */
-   if (is_interleaved_arrays(vp, vpv, arrays)) {
-      if (!setup_interleaved_attribs(st, vp, vpv, arrays, vbuffer, velements, &num_velements)) {
-         st->vertex_array_out_of_memory = TRUE;
-         return;
-      }
-
-      num_vbuffers = 1;
-      if (num_velements == 0)
-         num_vbuffers = 0;
-   }
-   else {
-      if (!setup_non_interleaved_attribs(st, vp, vpv, arrays, vbuffer,
-                                         velements, &num_velements)) {
-         st->vertex_array_out_of_memory = TRUE;
-         return;
-      }
-
-      num_vbuffers = vpv->num_inputs;
+   const GLbitfield inputs_read = vp_variant->vert_attrib_mask;
+
+   /* Process values that should have better been uniforms in the application */
+   GLbitfield curmask = inputs_read & _mesa_draw_current_bits(ctx);
+   if (curmask) {
+      const ubyte *input_to_index = vp->input_to_index;
+      /* For each attribute, upload the maximum possible size. */
+      GLubyte data[VERT_ATTRIB_MAX * sizeof(GLdouble) * 4];
+      GLubyte *cursor = data;
+      const unsigned bufidx = (*num_vbuffers)++;
+      unsigned max_alignment = 1;
+
+      do {
+         const gl_vert_attrib attr = u_bit_scan(&curmask);
+         const struct gl_array_attributes *const attrib
+            = _mesa_draw_current_attrib(ctx, attr);
+         const unsigned size = attrib->Format._ElementSize;
+         const unsigned alignment = util_next_power_of_two(size);
+         max_alignment = MAX2(max_alignment, alignment);
+         memcpy(cursor, attrib->Ptr, size);
+         if (alignment != size)
+            memset(cursor + size, 0, alignment - size);
+
+         init_velement(vp, velements->velems, &attrib->Format, cursor - data,
+                       0, bufidx, input_to_index[attr]);
+
+         cursor += alignment;
+      } while (curmask);
+
+      vbuffer[bufidx].is_user_buffer = false;
+      vbuffer[bufidx].buffer.resource = NULL;
+      /* vbuffer[bufidx].buffer_offset is set below */
+      vbuffer[bufidx].stride = 0;
+
+      /* Use const_uploader for zero-stride vertex attributes, because
+       * it may use a better memory placement than stream_uploader.
+       * The reason is that zero-stride attributes can be fetched many
+       * times (thousands of times), so a better placement is going to
+       * perform better.
+       */
+      struct u_upload_mgr *uploader = st->can_bind_const_buffer_as_vertex ?
+                                      st->pipe->const_uploader :
+                                      st->pipe->stream_uploader;
+      u_upload_data(uploader,
+                    0, cursor - data, max_alignment, data,
+                    &vbuffer[bufidx].buffer_offset,
+                    &vbuffer[bufidx].buffer.resource);
+      /* Always unmap. The uploader might use explicit flushes. */
+      u_upload_unmap(uploader);
+      return bufidx;
    }
+   return -1;
+}
 
-   cso_set_vertex_buffers(st->cso_context, 0, num_vbuffers, vbuffer);
-   if (st->last_num_vbuffers > num_vbuffers) {
-      /* Unbind remaining buffers, if any. */
-      cso_set_vertex_buffers(st->cso_context, num_vbuffers,
-                             st->last_num_vbuffers - num_vbuffers, NULL);
+void
+st_setup_current_user(struct st_context *st,
+                      const struct st_vertex_program *vp,
+                      const struct st_common_variant *vp_variant,
+                      struct cso_velems_state *velements,
+                      struct pipe_vertex_buffer *vbuffer, unsigned *num_vbuffers)
+{
+   struct gl_context *ctx = st->ctx;
+   const GLbitfield inputs_read = vp_variant->vert_attrib_mask;
+   const ubyte *input_to_index = vp->input_to_index;
+
+   /* Process values that should have better been uniforms in the application */
+   GLbitfield curmask = inputs_read & _mesa_draw_current_bits(ctx);
+   /* For each attribute, make an own user buffer binding. */
+   while (curmask) {
+      const gl_vert_attrib attr = u_bit_scan(&curmask);
+      const struct gl_array_attributes *const attrib
+         = _mesa_draw_current_attrib(ctx, attr);
+      const unsigned bufidx = (*num_vbuffers)++;
+
+      init_velement(vp, velements->velems, &attrib->Format, 0, 0,
+                    bufidx, input_to_index[attr]);
+
+      vbuffer[bufidx].is_user_buffer = true;
+      vbuffer[bufidx].buffer.user = attrib->Ptr;
+      vbuffer[bufidx].buffer_offset = 0;
+      vbuffer[bufidx].stride = 0;
    }
-   st->last_num_vbuffers = num_vbuffers;
-   cso_set_vertex_elements(st->cso_context, num_velements, velements);
 }
 
+void
+st_update_array(struct st_context *st)
+{
+   /* vertex program validation must be done before this */
+   /* _NEW_PROGRAM, ST_NEW_VS_STATE */
+   const struct st_vertex_program *vp = (struct st_vertex_program *)st->vp;
+   const struct st_common_variant *vp_variant = st->vp_variant;
+
+   struct pipe_vertex_buffer vbuffer[PIPE_MAX_ATTRIBS];
+   unsigned num_vbuffers = 0;
+   struct cso_velems_state velements;
+   bool uses_user_vertex_buffers;
+
+   /* ST_NEW_VERTEX_ARRAYS alias ctx->DriverFlags.NewArray */
+   /* Setup arrays */
+   st_setup_arrays(st, vp, vp_variant, &velements, vbuffer, &num_vbuffers,
+                   &uses_user_vertex_buffers);
+
+   /* _NEW_CURRENT_ATTRIB */
+   /* Setup zero-stride attribs. */
+   int current_attrib_buffer =
+      st_setup_current(st, vp, vp_variant, &velements, vbuffer, &num_vbuffers);
+
+   velements.count = vp->num_inputs + vp_variant->key.passthrough_edgeflags;
+
+   /* Set vertex buffers and elements. */
+   struct cso_context *cso = st->cso_context;
+   unsigned unbind_trailing_vbuffers =
+      st->last_num_vbuffers > num_vbuffers ?
+         st->last_num_vbuffers - num_vbuffers : 0;
+   cso_set_vertex_buffers_and_elements(cso, &velements,
+                                       num_vbuffers,
+                                       unbind_trailing_vbuffers,
+                                       vbuffer, uses_user_vertex_buffers);
+   st->last_num_vbuffers = num_vbuffers;
 
-const struct st_tracked_state st_update_array = {
-   update_array                                                /* update */
-};
+   /* Unreference uploaded current attrib buffer. */
+   if (current_attrib_buffer >= 0)
+      pipe_resource_reference(&vbuffer[current_attrib_buffer].buffer.resource, NULL);
+}