mesa: replace _mesa_index_buffer::type with index_size
[mesa.git] / src / mesa / tnl / t_draw.c
index 86af4b7cfe25232093eca6b5b04abe4891427016..9fca4da1f4d6e2e1d9cc65b9316437b3937f6f9a 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.1
  *
  * Copyright (C) 1999-2007  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>
  */
 
+#include <stdio.h>
+
 #include "main/glheader.h"
+#include "main/bufferobj.h"
 #include "main/condrender.h"
 #include "main/context.h"
 #include "main/imports.h"
 #include "main/mtypes.h"
 #include "main/macros.h"
 #include "main/enums.h"
+#include "util/half_float.h"
 
 #include "t_context.h"
 #include "tnl.h"
@@ -91,7 +95,7 @@ static void free_space(struct gl_context *ctx)
  * \param fptr  output/float array
  */
 static void
-convert_bgra_to_float(const struct gl_client_array *input,
+convert_bgra_to_float(const struct gl_vertex_array *input,
                       const GLubyte *ptr, GLfloat *fptr,
                       GLuint count )
 {
@@ -109,7 +113,7 @@ convert_bgra_to_float(const struct gl_client_array *input,
 }
 
 static void
-convert_half_to_float(const struct gl_client_array *input,
+convert_half_to_float(const struct gl_vertex_array *input,
                      const GLubyte *ptr, GLfloat *fptr,
                      GLuint count, GLuint sz)
 {
@@ -136,11 +140,12 @@ convert_half_to_float(const struct gl_client_array *input,
  * is used to map the fixed-point numbers into the range [-1, 1].
  */
 static void
-convert_fixed_to_float(const struct gl_client_array *input,
+convert_fixed_to_float(const struct gl_vertex_array *input,
                        const GLubyte *ptr, GLfloat *fptr,
                        GLuint count)
 {
-   GLuint i, j;
+   GLuint i;
+   GLint j;
    const GLint size = input->Size;
 
    if (input->Normalized) {
@@ -168,7 +173,7 @@ convert_fixed_to_float(const struct gl_client_array *input,
 static void _tnl_import_array( struct gl_context *ctx,
                               GLuint attrib,
                               GLuint count,
-                              const struct gl_client_array *input,
+                              const struct gl_vertex_array *input,
                               const GLubyte *ptr )
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
@@ -253,7 +258,7 @@ static GLboolean *_tnl_import_edgeflag( struct gl_context *ctx,
    GLuint i;
 
    for (i = 0; i < count; i++) {
-      *bptr++ = ((GLfloat *)ptr)[0] == 1.0;
+      *bptr++ = ((GLfloat *)ptr)[0] == 1.0F;
       ptr += stride;
    }
 
@@ -262,7 +267,7 @@ static GLboolean *_tnl_import_edgeflag( struct gl_context *ctx,
 
 
 static void bind_inputs( struct gl_context *ctx, 
-                        const struct gl_client_array *inputs[],
+                        const struct gl_vertex_array *inputs[],
                         GLint count,
                         struct gl_buffer_object **bo,
                         GLuint *nr_bo )
@@ -277,17 +282,18 @@ static void bind_inputs( struct gl_context *ctx,
       const void *ptr;
 
       if (inputs[i]->BufferObj->Name) { 
-        if (!inputs[i]->BufferObj->Pointer) {
+        if (!inputs[i]->BufferObj->Mappings[MAP_INTERNAL].Pointer) {
            bo[*nr_bo] = inputs[i]->BufferObj;
            (*nr_bo)++;
            ctx->Driver.MapBufferRange(ctx, 0, inputs[i]->BufferObj->Size,
                                       GL_MAP_READ_BIT,
-                                      inputs[i]->BufferObj);
+                                      inputs[i]->BufferObj,
+                                       MAP_INTERNAL);
            
-           assert(inputs[i]->BufferObj->Pointer);
+           assert(inputs[i]->BufferObj->Mappings[MAP_INTERNAL].Pointer);
         }
         
-        ptr = ADD_POINTERS(inputs[i]->BufferObj->Pointer,
+        ptr = ADD_POINTERS(inputs[i]->BufferObj->Mappings[MAP_INTERNAL].Pointer,
                            inputs[i]->Ptr);
       }
       else
@@ -339,53 +345,41 @@ static void bind_indices( struct gl_context *ctx,
    TNLcontext *tnl = TNL_CONTEXT(ctx);
    struct vertex_buffer *VB = &tnl->vb;
    GLuint i;
-   void *ptr;
+   const void *ptr;
 
    if (!ib) {
       VB->Elts = NULL;
       return;
    }
 
-   if (ib->obj->Name && !ib->obj->Pointer) {
-      unsigned map_size;
-
-      switch (ib->type) {
-      case GL_UNSIGNED_BYTE:
-        map_size = ib->count * sizeof(GLubyte);
-        break;
-      case GL_UNSIGNED_SHORT:
-        map_size = ib->count * sizeof(GLushort);
-        break;
-      case GL_UNSIGNED_INT:
-        map_size = ib->count * sizeof(GLuint);
-        break;
-      default:
-        assert(0);
-        map_size = 0;
-      }
-
+   if (_mesa_is_bufferobj(ib->obj) &&
+       !_mesa_bufferobj_mapped(ib->obj, MAP_INTERNAL)) {
+      /* if the buffer object isn't mapped yet, map it now */
       bo[*nr_bo] = ib->obj;
       (*nr_bo)++;
-      ptr = ctx->Driver.MapBufferRange(ctx, (GLsizeiptr) ib->ptr, map_size,
-                                      GL_MAP_READ_BIT, ib->obj);
-      assert(ib->obj->Pointer);
+      ptr = ctx->Driver.MapBufferRange(ctx, (GLsizeiptr) ib->ptr,
+                                       ib->count * ib->index_size,
+                                      GL_MAP_READ_BIT, ib->obj,
+                                       MAP_INTERNAL);
+      assert(ib->obj->Mappings[MAP_INTERNAL].Pointer);
    } else {
-      ptr = ib->ptr;
+      /* user-space elements, or buffer already mapped */
+      ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
    }
 
-   if (ib->type == GL_UNSIGNED_INT && VB->Primitive[0].basevertex == 0) {
+   if (ib->index_size == 4 && VB->Primitive[0].basevertex == 0) {
       VB->Elts = (GLuint *) ptr;
    }
    else {
       GLuint *elts = (GLuint *)get_space(ctx, ib->count * sizeof(GLuint));
       VB->Elts = elts;
 
-      if (ib->type == GL_UNSIGNED_INT) {
+      if (ib->index_size == 4) {
         const GLuint *in = (GLuint *)ptr;
         for (i = 0; i < ib->count; i++)
            *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
       }
-      else if (ib->type == GL_UNSIGNED_SHORT) {
+      else if (ib->index_size == 2) {
         const GLushort *in = (GLushort *)ptr;
         for (i = 0; i < ib->count; i++) 
            *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
@@ -415,44 +409,36 @@ static void unmap_vbos( struct gl_context *ctx,
 {
    GLuint i;
    for (i = 0; i < nr_bo; i++) { 
-      ctx->Driver.UnmapBuffer(ctx, bo[i]);
+      ctx->Driver.UnmapBuffer(ctx, bo[i], MAP_INTERNAL);
    }
 }
 
 
-void _tnl_vbo_draw_prims(struct gl_context *ctx,
-                        const struct gl_client_array *arrays[],
+/* This is the main entrypoint into the slimmed-down software tnl
+ * module.  In a regular swtnl driver, this can be plugged straight
+ * into the vbo->Driver.DrawPrims() callback.
+ */
+void _tnl_draw_prims(struct gl_context *ctx,
                         const struct _mesa_prim *prim,
                         GLuint nr_prims,
                         const struct _mesa_index_buffer *ib,
                         GLboolean index_bounds_valid,
                         GLuint min_index,
-                        GLuint max_index)
-{
-   if (!index_bounds_valid)
-      vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
-
-   _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
-}
-
-/* This is the main entrypoint into the slimmed-down software tnl
- * module.  In a regular swtnl driver, this can be plugged straight
- * into the vbo->Driver.DrawPrims() callback.
- */
-void _tnl_draw_prims( struct gl_context *ctx,
-                     const struct gl_client_array *arrays[],
-                     const struct _mesa_prim *prim,
-                     GLuint nr_prims,
-                     const struct _mesa_index_buffer *ib,
-                     GLuint min_index,
-                     GLuint max_index)
+                        GLuint max_index,
+                        struct gl_transform_feedback_object *tfb_vertcount,
+                         unsigned stream,
+                        struct gl_buffer_object *indirect)
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
+   const struct gl_vertex_array **arrays = ctx->Array._DrawArrays;
    const GLuint TEST_SPLIT = 0;
    const GLint max = TEST_SPLIT ? 8 : tnl->vb.Size - MAX_CLIPPED_VERTICES;
    GLint max_basevertex = prim->basevertex;
    GLuint i;
 
+   if (!index_bounds_valid)
+      vbo_get_minmax_indices(ctx, prim, ib, &min_index, &max_index, nr_prims);
+
    /* Mesa core state should have been validated already */
    assert(ctx->NewState == 0x0);
 
@@ -464,10 +450,10 @@ void _tnl_draw_prims( struct gl_context *ctx,
 
    if (0)
    {
-      printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
+      printf("%s %d..%d\n", __func__, min_index, max_index);
       for (i = 0; i < nr_prims; i++)
         printf("prim %d: %s start %d count %d\n", i, 
-               _mesa_lookup_enum_by_nr(prim[i].mode),
+               _mesa_enum_to_string(prim[i].mode),
                prim[i].start,
                prim[i].count);
    }
@@ -477,7 +463,7 @@ void _tnl_draw_prims( struct gl_context *ctx,
        */
       vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, 
                        min_index, max_index,
-                       _tnl_vbo_draw_prims );
+                       _tnl_draw_prims );
       return;
    }
    else if ((GLint)max_index + max_basevertex > max) {
@@ -495,7 +481,7 @@ void _tnl_draw_prims( struct gl_context *ctx,
        */
       vbo_split_prims( ctx, arrays, prim, nr_prims, ib, 
                       0, max_index + prim->basevertex,
-                      _tnl_vbo_draw_prims,
+                      _tnl_draw_prims,
                       &limits );
    }
    else {