Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / state_tracker / st_draw.c
index 31383b488749f2aa40a3e727be022c5d31a421d0..c76bff91819d7da2a978042ced88f04c3fdd4457 100644 (file)
@@ -159,12 +159,21 @@ static GLuint fixed_types[4] = {
  * Return a PIPE_FORMAT_x for the given GL datatype and size.
  */
 GLuint
-st_pipe_vertex_format(GLenum type, GLuint size, GLboolean normalized)
+st_pipe_vertex_format(GLenum type, GLuint size, GLenum format,
+                      GLboolean normalized)
 {
    assert((type >= GL_BYTE && type <= GL_DOUBLE) ||
           type == GL_FIXED);
    assert(size >= 1);
    assert(size <= 4);
+   assert(format == GL_RGBA || format == GL_BGRA);
+
+   if (format == GL_BGRA) {
+      /* this is an odd-ball case */
+      assert(type == GL_UNSIGNED_BYTE);
+      assert(normalized);
+      return PIPE_FORMAT_B8G8R8A8_UNORM;
+   }
 
    if (normalized) {
       switch (type) {
@@ -220,10 +229,12 @@ setup_edgeflags(GLcontext *ctx, GLenum primMode, GLint start, GLint count,
       struct st_buffer_object *stobj = st_buffer_object(array->BufferObj);
       ubyte *map;
 
-      if (!stobj)
+      if (!stobj || stobj->Base.Name == 0) {
+         /* edge flags are not in a VBO */
          return NULL;
+      }
 
-      vec = (unsigned *) calloc(sizeof(unsigned), (count + 31) / 32);
+      vec = (unsigned *) _mesa_calloc(sizeof(unsigned) * ((count + 31) / 32));
       if (!vec)
          return NULL;
 
@@ -317,23 +328,29 @@ get_arrays_bounds(const struct st_vertex_program *vp,
                        const GLubyte **low, const GLubyte **high)
 {
    const GLubyte *low_addr = NULL;
+   const GLubyte *high_addr = NULL;
    GLuint attr;
-   GLint stride;
 
    for (attr = 0; attr < vp->num_inputs; attr++) {
       const GLuint mesaAttr = vp->index_to_input[attr];
+      const GLint stride = arrays[mesaAttr]->StrideB;
       const GLubyte *start = arrays[mesaAttr]->Ptr;
-      stride = arrays[mesaAttr]->StrideB;
+      const unsigned sz = (arrays[mesaAttr]->Size * 
+                           _mesa_sizeof_type(arrays[mesaAttr]->Type));
+      const GLubyte *end = start + (max_index * stride) + sz;
+
       if (attr == 0) {
          low_addr = start;
+         high_addr = end;
       }
       else {
          low_addr = MIN2(low_addr, start);
+         high_addr = MAX2(high_addr, end);
       }
    }
 
    *low = low_addr;
-   *high = low_addr + (max_index + 1) * stride;
+   *high = high_addr;
 }
 
 
@@ -378,10 +395,10 @@ setup_interleaved_attribs(GLcontext *ctx,
          }
          else {
             vbuffer->buffer = NULL;
-            pipe_buffer_reference(pipe->screen, &vbuffer->buffer, stobj->buffer);
-            vbuffer->buffer_offset = (unsigned) arrays[mesaAttr]->Ptr;
+            pipe_buffer_reference(&vbuffer->buffer, stobj->buffer);
+            vbuffer->buffer_offset = pointer_to_offset(low);
          }
-         vbuffer->pitch = stride; /* in bytes */
+         vbuffer->stride = stride; /* in bytes */
          vbuffer->max_index = max_index;
       }
 
@@ -392,6 +409,7 @@ setup_interleaved_attribs(GLcontext *ctx,
       velements[attr].src_format =
          st_pipe_vertex_format(arrays[mesaAttr]->Type,
                                arrays[mesaAttr]->Size,
+                               arrays[mesaAttr]->Format,
                                arrays[mesaAttr]->Normalized);
       assert(velements[attr].src_format);
    }
@@ -433,8 +451,8 @@ setup_non_interleaved_attribs(GLcontext *ctx,
          /*printf("stobj %u = %p\n", attr, (void*) stobj);*/
 
          vbuffer[attr].buffer = NULL;
-         pipe_buffer_reference(pipe->screen, &vbuffer[attr].buffer, stobj->buffer);
-         vbuffer[attr].buffer_offset = (unsigned) arrays[mesaAttr]->Ptr;
+         pipe_buffer_reference(&vbuffer[attr].buffer, stobj->buffer);
+         vbuffer[attr].buffer_offset = pointer_to_offset(arrays[mesaAttr]->Ptr);
          velements[attr].src_offset = 0;
       }
       else {
@@ -472,13 +490,14 @@ setup_non_interleaved_attribs(GLcontext *ctx,
       assert(velements[attr].src_offset <= 2048); /* 11-bit field */
 
       /* common-case setup */
-      vbuffer[attr].pitch = stride; /* in bytes */
+      vbuffer[attr].stride = stride; /* in bytes */
       vbuffer[attr].max_index = max_index;
       velements[attr].vertex_buffer_index = attr;
       velements[attr].nr_components = arrays[mesaAttr]->Size;
       velements[attr].src_format
          = st_pipe_vertex_format(arrays[mesaAttr]->Type,
                                  arrays[mesaAttr]->Size,
+                                 arrays[mesaAttr]->Format,
                                  arrays[mesaAttr]->Normalized);
       assert(velements[attr].src_format);
    }
@@ -520,6 +539,7 @@ st_draw_vbo(GLcontext *ctx,
             const struct _mesa_prim *prims,
             GLuint nr_prims,
             const struct _mesa_index_buffer *ib,
+           GLboolean index_bounds_valid,
             GLuint min_index,
             GLuint max_index)
 {
@@ -532,6 +552,10 @@ st_draw_vbo(GLcontext *ctx,
    unsigned num_vbuffers, num_velements;
    GLboolean userSpace;
 
+   /* Gallium probably doesn't want this in some cases. */
+   if (!index_bounds_valid)
+      vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
+
    /* sanity check for pointer arithmetic below */
    assert(sizeof(arrays[0]->Ptr[0]) == 1);
 
@@ -541,9 +565,13 @@ st_draw_vbo(GLcontext *ctx,
    vp = ctx->st->vp;
    vs = &ctx->st->vp->state;
 
+#if 0
    if (MESA_VERBOSE & VERBOSE_GLSL) {
       check_uniforms(ctx);
    }
+#else
+   (void) check_uniforms;
+#endif
 
    /*
     * Setup the vbuffer[] and velements[] arrays.
@@ -569,7 +597,7 @@ st_draw_vbo(GLcontext *ctx,
    {
       GLuint i;
       for (i = 0; i < num_vbuffers; i++) {
-         printf("buffers[%d].pitch = %u\n", i, vbuffer[i].pitch);
+         printf("buffers[%d].stride = %u\n", i, vbuffer[i].stride);
          printf("buffers[%d].max_index = %u\n", i, vbuffer[i].max_index);
          printf("buffers[%d].buffer_offset = %u\n", i, vbuffer[i].buffer_offset);
          printf("buffers[%d].buffer = %p\n", i, (void*) vbuffer[i].buffer);
@@ -615,8 +643,8 @@ st_draw_vbo(GLcontext *ctx,
       if (bufobj && bufobj->Name) {
          /* elements/indexes are in a real VBO */
          struct st_buffer_object *stobj = st_buffer_object(bufobj);
-         pipe_buffer_reference(pipe->screen, &indexBuf, stobj->buffer);
-         indexOffset = (unsigned) ib->ptr / indexSize;
+         pipe_buffer_reference(&indexBuf, stobj->buffer);
+         indexOffset = pointer_to_offset(ib->ptr) / indexSize;
       }
       else {
          /* element/indicies are in user space memory */
@@ -655,7 +683,7 @@ st_draw_vbo(GLcontext *ctx,
          }
       }
 
-      pipe_buffer_reference(pipe->screen, &indexBuf, NULL);
+      pipe_buffer_reference(&indexBuf, NULL);
    }
    else {
       /* non-indexed */
@@ -671,7 +699,7 @@ st_draw_vbo(GLcontext *ctx,
 
    /* unreference buffers (frees wrapped user-space buffer objects) */
    for (attr = 0; attr < num_vbuffers; attr++) {
-      pipe_buffer_reference(pipe->screen, &vbuffer[attr].buffer, NULL);
+      pipe_buffer_reference(&vbuffer[attr].buffer, NULL);
       assert(!vbuffer[attr].buffer);
    }