mesa|mapi: replace _mesa_[v]snprintf with [v]snprintf
[mesa.git] / src / mesa / tnl / t_rebase.c
index 06d67620f634a2864a576ff9e4c788bc38ead130..6f600e390681a12282e4f49cdd7d6955ac0b0f76 100644 (file)
@@ -39,7 +39,7 @@
  *
  * If we just upload the new data, however, the indices will be
  * incorrect as we tend to upload each set of vertex data to a new
- * region.  
+ * region.
  *
  * This file provides a helper to adjust the arrays, primitives and
  * indices of a draw call so that it can be re-issued with a min_index
@@ -50,7 +50,7 @@
 #include "main/bufferobj.h"
 #include "main/errors.h"
 #include "main/glheader.h"
-#include "main/imports.h"
+#include "main/macros.h"
 #include "main/mtypes.h"
 #include "vbo/vbo.h"
 
 
 
 #define REBASE(TYPE)                                           \
-static void *rebase_##TYPE( const void *ptr,                   \
-                         GLuint count,                         \
-                         TYPE min_index )                      \
+static void *rebase_##TYPE(const void *ptr,                    \
+                           unsigned count,                     \
+                           TYPE min_index)                     \
 {                                                              \
-   GLuint i;                                                   \
    const TYPE *in = (TYPE *)ptr;                               \
    TYPE *tmp_indices = malloc(count * sizeof(TYPE));           \
-                                                               \
+                                                                \
    if (tmp_indices == NULL) {                                   \
       _mesa_error_no_memory(__func__);                          \
       return NULL;                                              \
    }                                                            \
-                                                               \
-   for (i = 0; i < count; i++)                                 \
+                                                                \
+   for (unsigned i = 0; i < count; i++)                         \
       tmp_indices[i] = in[i] - min_index;                      \
-                                                               \
+                                                                \
    return (void *)tmp_indices;                                 \
 }
 
-
 REBASE(GLuint)
 REBASE(GLushort)
 REBASE(GLubyte)
 
 
-
 /* Adjust primitives, indices and vertex definitions so that min_index
  * becomes zero. There are lots of reasons for wanting to do this, eg:
  *
@@ -92,7 +89,7 @@ REBASE(GLubyte)
  *      min_index will be transformed.
  *
  * Hardware tnl:
- *    - if ib != NULL and min_index != 0, otherwise vertices lower than 
+ *    - if ib != NULL and min_index != 0, otherwise vertices lower than
  *      min_index will be uploaded.  Requires adjusting index values.
  *
  *    - if ib == NULL and min_index != 0, just for convenience so this doesn't
@@ -103,16 +100,16 @@ REBASE(GLubyte)
  *    - can't save time by trying to upload half a vbo - typically it is
  *      all or nothing.
  */
-void t_rebase_prims( struct gl_context *ctx,
-                     const struct tnl_vertex_array *arrays,
-                     const struct _mesa_prim *prim,
-                     GLuint nr_prims,
-                     const struct _mesa_index_buffer *ib,
-                     GLuint min_index,
-                     GLuint max_index,
-                     GLuint num_instances,
-                     GLuint base_instance,
-                     tnl_draw_func draw )
+void t_rebase_prims(struct gl_context *ctx,
+                    const struct tnl_vertex_array *arrays,
+                    const struct _mesa_prim *prim,
+                    GLuint nr_prims,
+                    const struct _mesa_index_buffer *ib,
+                    GLuint min_index,
+                    GLuint max_index,
+                    GLuint num_instances,
+                    GLuint base_instance,
+                    tnl_draw_func draw)
 {
    struct gl_array_attributes tmp_attribs[VERT_ATTRIB_MAX];
    struct tnl_vertex_array tmp_arrays[VERT_ATTRIB_MAX];
@@ -127,13 +124,12 @@ void t_rebase_prims( struct gl_context *ctx,
    if (0)
       printf("%s %d..%d\n", __func__, min_index, max_index);
 
-
    /* XXX this path is disabled for now.
     * There's rendering corruption in some apps when it's enabled.
     */
    if (0 && ib && ctx->Extensions.ARB_draw_elements_base_vertex) {
-      /* If we can just tell the hardware or the TNL to interpret our
-       * indices with a different base, do so.
+      /* If we can just tell the hardware or the TNL to interpret our indices
+       * with a different base, do so.
        */
       tmp_prims = malloc(sizeof(*prim) * nr_prims);
 
@@ -143,57 +139,58 @@ void t_rebase_prims( struct gl_context *ctx,
       }
 
       for (i = 0; i < nr_prims; i++) {
-        tmp_prims[i] = prim[i];
-        tmp_prims[i].basevertex -= min_index;
+         tmp_prims[i] = prim[i];
+         tmp_prims[i].basevertex -= min_index;
       }
 
       prim = tmp_prims;
    } else if (ib) {
       /* Unfortunately need to adjust each index individually.
        */
-      GLboolean map_ib = ib->obj->Name &&
-                         !ib->obj->Mappings[MAP_INTERNAL].Pointer;
-      void *ptr;
-
-      if (map_ib) 
-        ctx->Driver.MapBufferRange(ctx, 0, ib->obj->Size, GL_MAP_READ_BIT,
-                                   ib->obj, MAP_INTERNAL);
-
-
-      ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
-
-      /* Some users might prefer it if we translated elements to
-       * GLuints here.  Others wouldn't...
+      bool map_ib = false;
+      const void *ptr;
+
+      if (ib->obj) {
+         if (!ib->obj->Mappings[MAP_INTERNAL].Pointer) {
+            ctx->Driver.MapBufferRange(ctx, 0, ib->obj->Size, GL_MAP_READ_BIT,
+                                       ib->obj, MAP_INTERNAL);
+            map_ib = true;
+         }
+
+         ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
+      } else
+         ptr = ib->ptr;
+
+      /* Some users might prefer it if we translated elements to GLuints here.
+       * Others wouldn't...
        */
-      switch (ib->index_size) {
-      case 4:
-        tmp_indices = rebase_GLuint( ptr, ib->count, min_index );
-        break;
+      switch (ib->index_size_shift) {
       case 2:
-        tmp_indices = rebase_GLushort( ptr, ib->count, min_index );
-        break;
+         tmp_indices = rebase_GLuint( ptr, ib->count, min_index );
+         break;
       case 1:
-        tmp_indices = rebase_GLubyte( ptr, ib->count, min_index );
-        break;
-      }      
+         tmp_indices = rebase_GLushort( ptr, ib->count, min_index );
+         break;
+      case 0:
+         tmp_indices = rebase_GLubyte( ptr, ib->count, min_index );
+         break;
+      }
 
-      if (map_ib) 
-        ctx->Driver.UnmapBuffer(ctx, ib->obj, MAP_INTERNAL);
+      if (map_ib)
+         ctx->Driver.UnmapBuffer(ctx, ib->obj, MAP_INTERNAL);
 
-      if (tmp_indices == NULL) {
+      if (tmp_indices == NULL)
          return;
-      }
 
-      tmp_ib.obj = ctx->Shared->NullBufferObj;
+      tmp_ib.obj = NULL;
       tmp_ib.ptr = tmp_indices;
       tmp_ib.count = ib->count;
-      tmp_ib.index_size = ib->index_size;
+      tmp_ib.index_size_shift = ib->index_size_shift;
 
       ib = &tmp_ib;
    }
    else {
-      /* Otherwise the primitives need adjustment.
-       */
+      /* Otherwise the primitives need adjustment. */
       tmp_prims = malloc(sizeof(*prim) * nr_prims);
 
       if (tmp_prims == NULL) {
@@ -202,12 +199,11 @@ void t_rebase_prims( struct gl_context *ctx,
       }
 
       for (i = 0; i < nr_prims; i++) {
-        /* If this fails, it could indicate an application error:
-         */
-        assert(prim[i].start >= min_index);
+         /* If this fails, it could indicate an application error: */
+         assert(prim[i].start >= min_index);
 
-        tmp_prims[i] = prim[i];
-        tmp_prims[i].start -= min_index;
+         tmp_prims[i] = prim[i];
+         tmp_prims[i].start -= min_index;
       }
 
       prim = tmp_prims;
@@ -227,28 +223,25 @@ void t_rebase_prims( struct gl_context *ctx,
       tmp_attribs[i] = *(arrays[i].VertexAttrib);
       tmp_arrays[i].BufferBinding = arrays[i].BufferBinding;
       tmp_arrays[i].VertexAttrib = &tmp_attribs[i];
-      if (_mesa_is_bufferobj(arrays[i].BufferBinding->BufferObj))
+      if (arrays[i].BufferBinding->BufferObj)
          tmp_attribs[i].RelativeOffset +=
             min_index * arrays[i].BufferBinding->Stride;
       else
          tmp_attribs[i].Ptr += min_index * arrays[i].BufferBinding->Stride;
    }
-   
-   /* Re-issue the draw call.
-    */
-   draw( ctx,
-         tmp_arrays,
-        prim,
-        nr_prims, 
-        ib, 
-        GL_TRUE,
-        0, 
-        max_index - min_index,
-         num_instances, base_instance,
-        NULL, 0);
+
+   /* Re-issue the draw call. */
+   draw(ctx,
+        tmp_arrays,
+        prim,
+        nr_prims,
+        ib,
+        GL_TRUE,
+        0,
+        max_index - min_index,
+        num_instances, base_instance);
 
    free(tmp_indices);
-   
    free(tmp_prims);
 }