i965: Upload all vertices used
[mesa.git] / src / mesa / drivers / dri / i965 / brw_draw_upload.c
index 09d7a5ee14df54aceae92e7efd133e98db62b9c6..41565231b57c12c8a245e09a1ad09e41897cb2e1 100644 (file)
@@ -240,12 +240,13 @@ static GLuint get_index_type(GLenum type)
 }
 
 static void
-copy_array_to_vbo_array( struct brw_context *brw,
-                        struct brw_vertex_element *element,
-                        struct brw_vertex_buffer *buffer,
-                        GLuint dst_stride)
+copy_array_to_vbo_array(struct brw_context *brw,
+                       struct brw_vertex_element *element,
+                       int count,
+                       struct brw_vertex_buffer *buffer,
+                       GLuint dst_stride)
 {
-   GLuint size = element->count * dst_stride;
+   GLuint size = count * dst_stride;
 
    buffer->stride = dst_stride;
    if (dst_stride == element->glarray->StrideB) {
@@ -253,15 +254,16 @@ copy_array_to_vbo_array( struct brw_context *brw,
                        &buffer->bo, &buffer->offset);
    } else {
       const unsigned char *src = element->glarray->Ptr;
-      char *dst = intel_upload_map(&brw->intel, size, dst_stride);
+      char *map = intel_upload_map(&brw->intel, size, dst_stride);
+      char *dst = map;
       int i;
 
-      for (i = 0; i < element->count; i++) {
+      for (i = 0; i < count; i++) {
         memcpy(dst, src, dst_stride);
         src += element->glarray->StrideB;
         dst += dst_stride;
       }
-      intel_upload_unmap(&brw->intel, dst, size, dst_stride,
+      intel_upload_unmap(&brw->intel, map, size, dst_stride,
                         &buffer->bo, &buffer->offset);
    }
 }
@@ -270,7 +272,7 @@ static void brw_prepare_vertices(struct brw_context *brw)
 {
    struct gl_context *ctx = &brw->intel.ctx;
    struct intel_context *intel = intel_context(ctx);
-   GLbitfield vs_inputs = brw->vs.prog_data->inputs_read; 
+   GLbitfield vs_inputs = brw->vs.prog_data->inputs_read;
    const unsigned char *ptr = NULL;
    GLuint interleaved = 0, total_size = 0;
    unsigned int min_index = brw->vb.min_index;
@@ -337,6 +339,7 @@ static void brw_prepare_vertices(struct brw_context *brw)
         }
         if (k == i) {
            struct brw_vertex_buffer *buffer = &brw->vb.buffers[j];
+
            /* Named buffer object: Just reference its contents directly. */
            buffer->bo = intel_bufferobj_source(intel, intel_buffer,
                                                &buffer->offset);
@@ -347,7 +350,6 @@ static void brw_prepare_vertices(struct brw_context *brw)
            input->buffer = j++;
            input->offset = 0;
         }
-        input->count = glarray->_MaxElement;
 
         /* This is a common place to reach if the user mistakenly supplies
          * a pointer in place of a VBO offset.  If we just let it go through,
@@ -363,8 +365,6 @@ static void brw_prepare_vertices(struct brw_context *brw)
          */
         assert(input->offset < brw->vb.buffers[input->buffer].bo->size);
       } else {
-        input->count = glarray->StrideB ? max_index + 1 : 1;
-
         /* Queue the buffer object up to be uploaded in the next pass,
          * when we've decided if we're doing interleaved or not.
          */
@@ -384,26 +384,37 @@ static void brw_prepare_vertices(struct brw_context *brw)
         {
            interleaved = 0;
         }
-        else if (total_size & (type_size -1))
+        else if ((uintptr_t)(glarray->Ptr - ptr) & (type_size -1))
         {
            /* enforce natural alignment (for doubles) */
            interleaved = 0;
         }
 
         upload[nr_uploads++] = input;
+        total_size = ALIGN(total_size, type_size);
         total_size += input->element_size;
       }
    }
 
+   /* If we need to upload all the arrays, then we can trim those arrays to
+    * only the used elements [min_index, max_index] so long as we adjust all
+    * the values used in the 3DPRIMITIVE i.e. by setting the vertex bias.
+    */
+   if (nr_uploads == brw->vb.nr_enabled) {
+      brw->vb.start_vertex_bias = min_index;
+   } else {
+      brw->vb.start_vertex_bias = 0;
+      min_index = 0;
+   }
+
    /* Handle any arrays to be uploaded. */
    if (nr_uploads > 1) {
       if (interleaved && interleaved <= 2*total_size) {
         /* All uploads are interleaved, so upload the arrays together as
          * interleaved.  First, upload the contents and set up upload[0].
          */
-        copy_array_to_vbo_array(brw,
-                                upload[0], &brw->vb.buffers[j],
-                                interleaved);
+        copy_array_to_vbo_array(brw, upload[0], max_index - min_index + 1,
+                                &brw->vb.buffers[j], interleaved);
 
         for (i = 0; i < nr_uploads; i++) {
            /* Then, just point upload[i] at upload[0]'s buffer. */
@@ -417,8 +428,9 @@ static void brw_prepare_vertices(struct brw_context *brw)
       }
       else if (total_size < 2048) {
         /* Upload non-interleaved arrays into a single interleaved array */
-        struct brw_vertex_buffer *buffer = &brw->vb.buffers[j];
-        int count = upload[0]->count, offset;
+        struct brw_vertex_buffer *buffer;
+        int count = max_index - min_index + 1;
+        int offset;
         char *map;
 
         map = intel_upload_map(&brw->intel, total_size * count, total_size);
@@ -426,9 +438,13 @@ static void brw_prepare_vertices(struct brw_context *brw)
            const unsigned char *src = upload[i]->glarray->Ptr;
            int size = upload[i]->element_size;
            int stride = upload[i]->glarray->StrideB;
-           char *dst = map + offset;
+           char *dst;
            int n;
 
+           offset = ALIGN(offset, get_size(upload[i]->glarray->Type));
+           dst = map + offset;
+           src += min_index * size;
+
            for (n = 0; n < count; n++) {
               memcpy(dst, src, size);
               src += stride;
@@ -440,24 +456,24 @@ static void brw_prepare_vertices(struct brw_context *brw)
 
            offset += size;
         }
-        intel_upload_unmap(&brw->intel, map, total_size * count, total_size,
+        assert(offset == total_size);
+        buffer = &brw->vb.buffers[j++];
+        intel_upload_unmap(&brw->intel, map, offset * count, offset,
                            &buffer->bo, &buffer->offset);
         buffer->stride = offset;
-        j++;
 
         nr_uploads = 0;
       }
    }
    /* Upload non-interleaved arrays */
    for (i = 0; i < nr_uploads; i++) {
-      copy_array_to_vbo_array(brw,
-                             upload[i], &brw->vb.buffers[j],
-                             upload[i]->element_size);
+      copy_array_to_vbo_array(brw, upload[i], max_index - min_index + 1,
+                             &brw->vb.buffers[j], upload[i]->element_size);
       upload[i]->buffer = j++;
+      upload[i]->offset = 0;
    }
 
    /* can we simply extend the current vb? */
-   brw->vb.start_vertex_bias = 0;
    if (j == brw->vb.nr_current_buffers) {
       int delta = 0;
       for (i = 0; i < j; i++) {
@@ -470,12 +486,12 @@ static void brw_prepare_vertices(struct brw_context *brw)
         d = brw->vb.buffers[i].offset - brw->vb.current_buffers[i].offset;
         if (delta == 0)
            delta = d / brw->vb.current_buffers[i].stride;
-        else if (delta * brw->vb.current_buffers[i].stride != d)
+        if (delta * brw->vb.current_buffers[i].stride != d)
            break;
       }
 
       if (i == j) {
-        brw->vb.start_vertex_bias = delta;
+        brw->vb.start_vertex_bias += delta;
         while (--j >= 0)
            drm_intel_bo_unreference(brw->vb.buffers[j].bo);
         j = 0;
@@ -549,7 +565,7 @@ static void brw_emit_vertices(struct brw_context *brw)
         if (intel->gen >= 5) {
            OUT_RELOC(buffer->bo, I915_GEM_DOMAIN_VERTEX, 0, buffer->bo->size - 1);
         } else
-           OUT_BATCH(0);
+           OUT_BATCH(buffer->bo->size / buffer->stride);
         OUT_BATCH(0); /* Instance data step rate */
 
         brw->vb.current_buffers[i].handle = buffer->bo->handle;
@@ -559,7 +575,6 @@ static void brw_emit_vertices(struct brw_context *brw)
       brw->vb.nr_current_buffers = i;
       ADVANCE_BATCH();
    }
-   ADVANCE_BATCH();
 
    BEGIN_BATCH(1 + brw->vb.nr_enabled * 2);
    OUT_BATCH((CMD_VERTEX_ELEMENT << 16) | (2*brw->vb.nr_enabled - 1));