vbo: move DLIST_DANGLING_REFS from mtypes.h to vbo_save_api.c
[mesa.git] / src / mesa / vbo / vbo_rebase.c
index 1de290ff6026908f24984a1c8ea85c299e8f1a34..9f5dc4678f8d59187c82bb568aaed8591d1fcdbc 100644 (file)
@@ -1,7 +1,6 @@
 
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
  *
  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  *
  * Authors:
- *    Keith Whitwell <keith@tungstengraphics.com>
+ *    Keith Whitwell <keithw@vmware.com>
  */
 
 /* Helper for drivers which find themselves rendering a range of
@@ -46,6 +46,7 @@
  * of zero.
  */
 
+#include <stdio.h>
 #include "main/glheader.h"
 #include "main/imports.h"
 #include "main/mtypes.h"
@@ -58,9 +59,14 @@ static void *rebase_##TYPE( const void *ptr,                 \
                          GLuint count,                         \
                          TYPE min_index )                      \
 {                                                              \
-   const TYPE *in = (TYPE *)ptr;                               \
-   TYPE *tmp_indices = malloc(count * sizeof(TYPE));   \
    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++)                                 \
       tmp_indices[i] = in[i] - min_index;                      \
@@ -73,18 +79,19 @@ REBASE(GLuint)
 REBASE(GLushort)
 REBASE(GLubyte)
 
-GLboolean vbo_all_varyings_in_vbos( const struct gl_client_array *arrays[] )
+GLboolean vbo_all_varyings_in_vbos( const struct gl_vertex_array *arrays[] )
 {
    GLuint i;
    
    for (i = 0; i < VERT_ATTRIB_MAX; i++)
-      if (arrays[i]->BufferObj->Name == 0)
+      if (arrays[i]->StrideB &&
+         arrays[i]->BufferObj->Name == 0)
         return GL_FALSE;
 
    return GL_TRUE;
 }
 
-GLboolean vbo_any_varyings_in_vbos( const struct gl_client_array *arrays[] )
+GLboolean vbo_any_varyings_in_vbos( const struct gl_vertex_array *arrays[] )
 {
    GLuint i;
 
@@ -115,7 +122,7 @@ GLboolean vbo_any_varyings_in_vbos( const struct gl_client_array *arrays[] )
  *      all or nothing.
  */
 void vbo_rebase_prims( struct gl_context *ctx,
-                      const struct gl_client_array *arrays[],
+                      const struct gl_vertex_array *arrays[],
                       const struct _mesa_prim *prim,
                       GLuint nr_prims,
                       const struct _mesa_index_buffer *ib,
@@ -123,18 +130,19 @@ void vbo_rebase_prims( struct gl_context *ctx,
                       GLuint max_index,
                       vbo_draw_func draw )
 {
-   struct gl_client_array tmp_arrays[VERT_ATTRIB_MAX];
-   const struct gl_client_array *tmp_array_pointers[VERT_ATTRIB_MAX];
+   struct gl_vertex_array tmp_arrays[VERT_ATTRIB_MAX];
+   const struct gl_vertex_array *tmp_array_pointers[VERT_ATTRIB_MAX];
 
    struct _mesa_index_buffer tmp_ib;
    struct _mesa_prim *tmp_prims = NULL;
+   const struct gl_vertex_array **saved_arrays = ctx->Array._DrawArrays;
    void *tmp_indices = NULL;
    GLuint i;
 
    assert(min_index != 0);
 
    if (0)
-      printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
+      printf("%s %d..%d\n", __func__, min_index, max_index);
 
 
    /* XXX this path is disabled for now.
@@ -144,7 +152,12 @@ void vbo_rebase_prims( struct gl_context *ctx,
       /* If we can just tell the hardware or the TNL to interpret our
        * indices with a different base, do so.
        */
-      tmp_prims = (struct _mesa_prim *)malloc(sizeof(*prim) * nr_prims);
+      tmp_prims = malloc(sizeof(*prim) * nr_prims);
+
+      if (tmp_prims == NULL) {
+         _mesa_error_no_memory(__func__);
+         return;
+      }
 
       for (i = 0; i < nr_prims; i++) {
         tmp_prims[i] = prim[i];
@@ -155,49 +168,55 @@ void vbo_rebase_prims( struct gl_context *ctx,
    } else if (ib) {
       /* Unfortunately need to adjust each index individually.
        */
-      GLboolean map_ib = ib->obj->Name && !ib->obj->Pointer;
+      GLboolean map_ib = ib->obj->Name &&
+                         !ib->obj->Mappings[MAP_INTERNAL].Pointer;
       void *ptr;
 
       if (map_ib) 
-        ctx->Driver.MapBuffer(ctx, 
-                              GL_ELEMENT_ARRAY_BUFFER,
-                              GL_READ_ONLY_ARB,
-                              ib->obj);
+        ctx->Driver.MapBufferRange(ctx, 0, ib->obj->Size, GL_MAP_READ_BIT,
+                                   ib->obj, MAP_INTERNAL);
 
 
-      ptr = ADD_POINTERS(ib->obj->Pointer, ib->ptr);
+      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...
        */
-      switch (ib->type) {
-      case GL_UNSIGNED_INT: 
+      switch (ib->index_size) {
+      case 4:
         tmp_indices = rebase_GLuint( ptr, ib->count, min_index );
         break;
-      case GL_UNSIGNED_SHORT: 
+      case 2:
         tmp_indices = rebase_GLushort( ptr, ib->count, min_index );
         break;
-      case GL_UNSIGNED_BYTE: 
+      case 1:
         tmp_indices = rebase_GLubyte( ptr, ib->count, min_index );
         break;
       }      
 
       if (map_ib) 
-        ctx->Driver.UnmapBuffer(ctx, 
-                                GL_ELEMENT_ARRAY_BUFFER,
-                                ib->obj);
+        ctx->Driver.UnmapBuffer(ctx, ib->obj, MAP_INTERNAL);
+
+      if (tmp_indices == NULL) {
+         return;
+      }
 
       tmp_ib.obj = ctx->Shared->NullBufferObj;
       tmp_ib.ptr = tmp_indices;
       tmp_ib.count = ib->count;
-      tmp_ib.type = ib->type;
+      tmp_ib.index_size = ib->index_size;
 
       ib = &tmp_ib;
    }
    else {
       /* Otherwise the primitives need adjustment.
        */
-      tmp_prims = (struct _mesa_prim *)malloc(sizeof(*prim) * nr_prims);
+      tmp_prims = malloc(sizeof(*prim) * nr_prims);
+
+      if (tmp_prims == NULL) {
+         _mesa_error_no_memory(__func__);
+         return;
+      }
 
       for (i = 0; i < nr_prims; i++) {
         /* If this fails, it could indicate an application error:
@@ -229,20 +248,24 @@ void vbo_rebase_prims( struct gl_context *ctx,
    
    /* Re-issue the draw call.
     */
+   ctx->Array._DrawArrays = tmp_array_pointers;
+   ctx->NewDriverState |= ctx->DriverFlags.NewArray;
+
    draw( ctx, 
-        tmp_array_pointers, 
-        prim, 
+        prim,
         nr_prims, 
         ib, 
         GL_TRUE,
         0, 
-        max_index - min_index );
+        max_index - min_index,
+        NULL, 0, NULL );
+
+   ctx->Array._DrawArrays = saved_arrays;
+   ctx->NewDriverState |= ctx->DriverFlags.NewArray;
    
-   if (tmp_indices)
-      free(tmp_indices);
+   free(tmp_indices);
    
-   if (tmp_prims)
-      free(tmp_prims);
+   free(tmp_prims);
 }