Merge branch 'xa_branch'
[mesa.git] / src / mesa / state_tracker / st_draw.c
index 46a4f9cf8c53f4f7a430b121385208b54fb369d9..5040c6fa5ab9ebfb084a9a5979824c7455af12df 100644 (file)
@@ -33,7 +33,7 @@
  *
  * We basically convert the VBO's vertex attribute/array information into
  * Gallium vertex state, bind the vertex buffer objects and call
- * pipe->draw_elements(), pipe->draw_range_elements() or pipe->draw_arrays().
+ * pipe->draw_vbo().
  *
  * Authors:
  *   Keith Whitwell <keith@tungstengraphics.com>
@@ -42,6 +42,7 @@
 
 #include "main/imports.h"
 #include "main/image.h"
+#include "main/bufferobj.h"
 #include "main/macros.h"
 #include "main/mfeatures.h"
 #include "program/prog_uniform.h"
@@ -232,11 +233,26 @@ st_pipe_vertex_format(GLenum type, GLuint size, GLenum format,
 }
 
 
+/**
+ * This is very similar to vbo_all_varyings_in_vbos() but we test
+ * the stride.  See bug 38626.
+ */
+static GLboolean
+all_varyings_in_vbos(const struct gl_client_array *arrays[])
+{
+   GLuint i;
+   
+   for (i = 0; i < VERT_ATTRIB_MAX; i++)
+      if (arrays[i]->StrideB && !_mesa_is_bufferobj(arrays[i]->BufferObj))
+        return GL_FALSE;
+
+   return GL_TRUE;
+}
+
 
 /**
  * Examine the active arrays to determine if we have interleaved
  * vertex arrays all living in one VBO, or all living in user space.
- * \param userSpace  returns whether the arrays are in user space.
  */
 static GLboolean
 is_interleaved_arrays(const struct st_vertex_program *vp,
@@ -246,8 +262,8 @@ is_interleaved_arrays(const struct st_vertex_program *vp,
    GLuint attr;
    const struct gl_buffer_object *firstBufObj = NULL;
    GLint firstStride = -1;
-   const GLubyte *client_addr = NULL;
-   GLboolean user_memory = GL_FALSE;
+   const GLubyte *firstPtr = NULL;
+   GLboolean userSpaceBuffer = GL_FALSE;
 
    for (attr = 0; attr < vpv->num_inputs; attr++) {
       const GLuint mesaAttr = vp->index_to_input[attr];
@@ -255,37 +271,26 @@ is_interleaved_arrays(const struct st_vertex_program *vp,
       const struct gl_buffer_object *bufObj = array->BufferObj;
       const GLsizei stride = array->StrideB; /* in bytes */
 
-      if (firstStride < 0) {
+      if (attr == 0) {
+         /* save info about the first array */
          firstStride = stride;
-         user_memory = !bufObj || !bufObj->Name;
-      }
-      else if (firstStride != stride) {
-         return GL_FALSE;
-      }
-
-      if (!bufObj || !bufObj->Name) {
-         /* Try to detect if the client-space arrays are
-          * "close" to each other.
-          */
-         if (!user_memory) {
-            return GL_FALSE;
-         }
-         if (!client_addr) {
-            client_addr = array->Ptr;
-         }
-         else if (abs(array->Ptr - client_addr) > firstStride) {
-            /* arrays start too far apart */
-            return GL_FALSE;
-         }
-      }
-      else if (!firstBufObj) {
-         if (user_memory) {
-            return GL_FALSE;
-         }
+         firstPtr = array->Ptr;         
          firstBufObj = bufObj;
+         userSpaceBuffer = !bufObj || !bufObj->Name;
       }
-      else if (bufObj != firstBufObj) {
-         return GL_FALSE;
+      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 ((!bufObj || !_mesa_is_bufferobj(bufObj)) != userSpaceBuffer)
+            return GL_FALSE; /* mix of VBO and user-space arrays */
       }
    }
 
@@ -336,7 +341,7 @@ setup_interleaved_attribs(struct gl_context *ctx,
       assert(element_size == array->Size * _mesa_sizeof_type(array->Type));
 
       if (attr == 0) {
-         if (bufobj && bufobj->Name) {
+         if (bufobj && _mesa_is_bufferobj(bufobj)) {
             vbuffer->buffer = NULL;
             pipe_resource_reference(&vbuffer->buffer, stobj->buffer);
             vbuffer->buffer_offset = pointer_to_offset(low_addr);
@@ -402,7 +407,7 @@ setup_non_interleaved_attribs(struct gl_context *ctx,
 
       assert(element_size == array->Size * _mesa_sizeof_type(array->Type));
 
-      if (bufobj && bufobj->Name) {
+      if (bufobj && _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.
@@ -493,7 +498,7 @@ setup_index_buffer(struct gl_context *ctx,
       }
 
       /* get/create the index buffer object */
-      if (bufobj && bufobj->Name) {
+      if (bufobj && _mesa_is_bufferobj(bufobj)) {
          /* elements/indexes are in a real VBO */
          struct st_buffer_object *stobj = st_buffer_object(bufobj);
          pipe_resource_reference(&ibuffer->buffer, stobj->buffer);
@@ -509,6 +514,7 @@ setup_index_buffer(struct gl_context *ctx,
    }
 }
 
+
 /**
  * Prior to drawing, check that any uniforms referenced by the
  * current shader have been set.  If a uniform has not been set,
@@ -555,8 +561,8 @@ translate_prim(const struct gl_context *ctx, unsigned prim)
    assert(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
 
    /* Avoid quadstrips if it's easy to do so:
-    * Note: it's imporant to do the correct trimming if we change the prim type!
-    * We do that wherever this function is called.
+    * Note: it's important to do the correct trimming if we change the
+    * prim type!  We do that wherever this function is called.
     */
    if (prim == GL_QUAD_STRIP &&
        ctx->Light.ShadeModel != GL_FLAT &&
@@ -649,7 +655,8 @@ st_draw_vbo(struct gl_context *ctx,
    struct pipe_draw_info info;
    unsigned i, num_instances = 1;
    GLboolean new_array =
-         st->dirty.st && (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM)) != 0;
+      st->dirty.st &&
+      (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT)) != 0;
 
    /* Mesa core state should have been validated already */
    assert(ctx->NewState == 0x0);
@@ -657,7 +664,7 @@ st_draw_vbo(struct gl_context *ctx,
    if (ib) {
       /* Gallium probably doesn't want this in some cases. */
       if (!index_bounds_valid)
-         if (!vbo_all_varyings_in_vbos(arrays))
+         if (!all_varyings_in_vbos(arrays))
             vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
 
       for (i = 0; i < nr_prims; i++) {
@@ -737,8 +744,8 @@ st_draw_vbo(struct gl_context *ctx,
       }
    }
 
-   info.primitive_restart = st->ctx->Array.PrimitiveRestart;
-   info.restart_index = st->ctx->Array.RestartIndex;
+   info.primitive_restart = ctx->Array.PrimitiveRestart;
+   info.restart_index = ctx->Array.RestartIndex;
 
    /* do actual drawing */
    for (i = 0; i < nr_prims; i++) {