anv,i965: Lower away image derefs in the driver
[mesa.git] / src / mesa / state_tracker / st_atom_array.c
index db03b20a4126844b01556fcbfcef83c8ba8b8adb..e4fc19eb5e271cb11fefdfedc8b7a62c2a1ad3a5 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"
 
 /* vertex_formats[gltype - GL_BYTE][integer*2 + normalized][size - 1] */
 static const uint16_t vertex_formats[][4][4] = {
@@ -235,13 +238,18 @@ static const uint16_t vertex_formats[][4][4] = {
  * 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)
+st_pipe_vertex_format(const struct gl_array_attributes *attrib)
 {
+   const GLubyte size = attrib->Size;
+   const GLenum16 format = attrib->Format;
+   const bool normalized = attrib->Normalized;
+   const bool integer = attrib->Integer;
+   GLenum16 type = attrib->Type;
    unsigned index;
 
    assert(size >= 1 && size <= 4);
    assert(format == GL_RGBA || format == GL_BGRA);
+   assert(attrib->_ElementSize == _mesa_bytes_per_vertex_attrib(size, type));
 
    switch (type) {
    case GL_HALF_FLOAT_OES:
@@ -299,70 +307,6 @@ st_pipe_vertex_format(GLenum type, GLuint size, GLenum format,
    return vertex_formats[type - GL_BYTE][index][size-1];
 }
 
-static const struct gl_vertex_array *
-get_client_array(const struct st_vertex_program *vp,
-                 const struct gl_vertex_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_vertex_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_vertex_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 (llabs(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,
                           int src_offset, int format,
                           int instance_divisor, int vbo_index)
@@ -376,13 +320,13 @@ static void init_velement(struct pipe_vertex_element *velement,
 
 static void init_velement_lowered(const struct st_vertex_program *vp,
                                   struct pipe_vertex_element *velements,
-                                  int src_offset, int format,
-                                  int instance_divisor, int vbo_index,
-                                  int nr_components, GLboolean doubles,
-                                  GLuint *attr_idx)
+                                  const struct gl_array_attributes *attrib,
+                                  int src_offset, int instance_divisor,
+                                  int vbo_index, int idx)
 {
-   int idx = *attr_idx;
-   if (doubles) {
+   const GLubyte nr_components = attrib->Size;
+
+   if (attrib->Doubles) {
       int lower_format;
 
       if (nr_components < 2)
@@ -411,15 +355,13 @@ static void init_velement_lowered(const struct st_vertex_program *vp,
             init_velement(&velements[idx], src_offset, PIPE_FORMAT_R32G32_UINT,
                           instance_divisor, vbo_index);
          }
-
-         idx++;
       }
    } else {
+      const unsigned format = st_pipe_vertex_format(attrib);
+
       init_velement(&velements[idx], src_offset,
                     format, instance_divisor, vbo_index);
-      idx++;
    }
-   *attr_idx = idx;
 }
 
 static void
@@ -441,230 +383,132 @@ set_vertex_attribs(struct st_context *st,
    cso_set_vertex_elements(cso, num_velements, velements);
 }
 
-/**
- * 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
- */
-static void
-setup_interleaved_attribs(struct st_context *st,
-                          const struct st_vertex_program *vp,
-                          const struct st_vp_variant *vpv,
-                          const struct gl_vertex_array **arrays)
-{
-   struct pipe_vertex_buffer vbuffer;
-   struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS] = {{0}};
-   GLuint attr;
-   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_vertex_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);
-
-   for (attr = 0; attr < vpv->num_inputs;) {
-      const struct gl_vertex_array *array;
-      unsigned src_offset;
-      unsigned src_format;
-
-      array = get_client_array(vp, arrays, attr);
-      assert(array);
-
-      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(vp, velements, src_offset, src_format,
-                            array->InstanceDivisor, 0,
-                            array->Size, array->Doubles, &attr);
-   }
-
-   /*
-    * Return the vbuffer info and setup user-space attrib info, if needed.
-    */
-   if (vpv->num_inputs == 0) {
-      /* just defensive coding here */
-      vbuffer.buffer.resource = NULL;
-      vbuffer.is_user_buffer = false;
-      vbuffer.buffer_offset = 0;
-      vbuffer.stride = 0;
-   }
-   else if (usingVBO) {
-      /* all interleaved arrays in a VBO */
-      struct st_buffer_object *stobj = st_buffer_object(bufobj);
-
-      if (!stobj || !stobj->buffer) {
-         st->vertex_array_out_of_memory = true;
-         return; /* out-of-memory error probably */
-      }
-
-      vbuffer.buffer.resource = stobj->buffer;
-      vbuffer.is_user_buffer = false;
-      vbuffer.buffer_offset = pointer_to_offset(low_addr);
-      vbuffer.stride = stride;
-   }
-   else {
-      /* all interleaved arrays in user memory */
-      vbuffer.buffer.user = low_addr;
-      vbuffer.is_user_buffer = !!low_addr; /* if NULL, then unbind */
-      vbuffer.buffer_offset = 0;
-      vbuffer.stride = stride;
-   }
-
-   set_vertex_attribs(st, &vbuffer, vpv->num_inputs ? 1 : 0,
-                      velements, vpv->num_inputs);
-}
-
-/**
- * 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
- */
-static void
-setup_non_interleaved_attribs(struct st_context *st,
-                              const struct st_vertex_program *vp,
-                              const struct st_vp_variant *vpv,
-                              const struct gl_vertex_array **arrays)
+void
+st_update_array(struct st_context *st)
 {
    struct gl_context *ctx = st->ctx;
+   /* vertex program validation must be done before this */
+   const struct st_vertex_program *vp = st->vp;
+   /* _NEW_PROGRAM, ST_NEW_VS_STATE */
+   const GLbitfield inputs_read = st->vp_variant->vert_attrib_mask;
+   const struct gl_vertex_array_object *vao = ctx->Array._DrawVAO;
+   const ubyte *input_to_index = vp->input_to_index;
+
    struct pipe_vertex_buffer vbuffer[PIPE_MAX_ATTRIBS];
-   struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS] = {{0}};
+   struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
    unsigned num_vbuffers = 0;
-   GLuint attr;
-
-   for (attr = 0; attr < vpv->num_inputs;) {
-      const GLuint mesaAttr = vp->index_to_input[attr];
-      const struct gl_vertex_array *array;
-      struct gl_buffer_object *bufobj;
-      GLsizei stride;
-      unsigned src_format;
-      unsigned bufidx;
-
-      array = get_client_array(vp, arrays, attr);
-      assert(array);
-
-      bufidx = num_vbuffers++;
-
-      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_vertex_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);
 
+   st->vertex_array_out_of_memory = FALSE;
+   st->draw_needs_minmax_index = false;
+
+   /* _NEW_PROGRAM */
+   /* ST_NEW_VERTEX_ARRAYS alias ctx->DriverFlags.NewArray */
+   /* Process attribute array data. */
+   GLbitfield mask = inputs_read & _mesa_draw_array_bits(ctx);
+   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++;
+
+      if (_mesa_is_bufferobj(binding->BufferObj)) {
+         struct st_buffer_object *stobj = st_buffer_object(binding->BufferObj);
          if (!stobj || !stobj->buffer) {
             st->vertex_array_out_of_memory = true;
             return; /* out-of-memory error probably */
          }
 
+         /* Set the binding */
          vbuffer[bufidx].buffer.resource = stobj->buffer;
          vbuffer[bufidx].is_user_buffer = false;
-         vbuffer[bufidx].buffer_offset = pointer_to_offset(array->Ptr);
-      }
-      else {
-         /* wrap user data */
-         void *ptr;
-
-         if (array->Ptr) {
-            ptr = (void *) array->Ptr;
-         }
-         else {
-            /* no array, use ctx->Current.Attrib[] value */
-            ptr = (void *) ctx->Current.Attrib[mesaAttr];
-            stride = 0;
-         }
-
-         assert(ptr);
-
+         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 = !!ptr; /* if NULL, then unbind */
+         vbuffer[bufidx].is_user_buffer = true;
          vbuffer[bufidx].buffer_offset = 0;
-      }
 
-      /* common-case setup */
-      vbuffer[bufidx].stride = stride; /* in bytes */
-
-      src_format = st_pipe_vertex_format(array->Type,
-                                         array->Size,
-                                         array->Format,
-                                         array->Normalized,
-                                         array->Integer);
-
-      init_velement_lowered(vp, velements, 0, src_format,
-                            array->InstanceDivisor, bufidx,
-                            array->Size, array->Doubles, &attr);
+         if (!binding->InstanceDivisor)
+            st->draw_needs_minmax_index = true;
+      }
+      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 */
+      while (attrmask) {
+         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_lowered(vp, velements, attrib, off,
+                               binding->InstanceDivisor, bufidx,
+                               input_to_index[attr]);
+      }
    }
 
-   set_vertex_attribs(st, vbuffer, num_vbuffers, velements, vpv->num_inputs);
-}
+   const unsigned first_current_vbuffer = num_vbuffers;
+   /* _NEW_PROGRAM | _NEW_CURRENT_ATTRIB */
+   /* Process values that should have better been uniforms in the application */
+   GLbitfield curmask = inputs_read & _mesa_draw_current_bits(ctx);
+   if (curmask) {
+      /* 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;
+
+      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 size = attrib->_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_lowered(vp, velements, attrib, cursor - data, 0,
+                               bufidx, input_to_index[attr]);
+
+         cursor += alignment;
+      }
 
-void st_update_array(struct st_context *st)
-{
-   struct gl_context *ctx = st->ctx;
-   const struct gl_vertex_array **arrays = ctx->Array._DrawArrays;
-   const struct st_vertex_program *vp;
-   const struct st_vp_variant *vpv;
+      vbuffer[bufidx].is_user_buffer = false;
+      vbuffer[bufidx].buffer.resource = NULL;
+      /* vbuffer[bufidx].buffer_offset is set below */
+      vbuffer[bufidx].stride = 0;
 
-   st->vertex_array_out_of_memory = FALSE;
+      /* 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.
+       */
+      u_upload_data(st->can_bind_const_buffer_as_vertex ?
+                    st->pipe->const_uploader :
+                    st->pipe->stream_uploader,
+                    0, cursor - data, max_alignment, data,
+                    &vbuffer[bufidx].buffer_offset,
+                    &vbuffer[bufidx].buffer.resource);
+   }
 
-   /* No drawing has been done yet, so do nothing. */
-   if (!arrays)
-      return;
+   if (!ctx->Const.AllowMappedBuffersDuringExecution) {
+      u_upload_unmap(st->pipe->stream_uploader);
+   }
 
-   /* vertex program validation must be done before this */
-   vp = st->vp;
-   vpv = st->vp_variant;
+   const unsigned num_inputs = st->vp_variant->num_inputs;
+   set_vertex_attribs(st, vbuffer, num_vbuffers, velements, num_inputs);
 
-   if (is_interleaved_arrays(vp, vpv, arrays))
-      setup_interleaved_attribs(st, vp, vpv, arrays);
-   else
-      setup_non_interleaved_attribs(st, vp, vpv, arrays);
+   /* Unreference uploaded zero-stride vertex buffers. */
+   for (unsigned i = first_current_vbuffer; i < num_vbuffers; ++i) {
+      pipe_resource_reference(&vbuffer[i].buffer.resource, NULL);
+   }
 }