Move compiler.h and imports.h/c from src/mesa/main into src/util
[mesa.git] / src / mesa / tnl / t_draw.c
index fdde294257e2a7721b79ef45910e0d5a9e18615e..33d39a7bd20a9ed647b41e3f25e2ba39c29ce9be 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/arrayobj.h"
+#include "main/bufferobj.h"
 #include "main/condrender.h"
 #include "main/context.h"
-#include "main/imports.h"
+#include "util/imports.h"
 #include "main/mtypes.h"
 #include "main/macros.h"
 #include "main/enums.h"
+#include "main/varray.h"
+#include "util/half_float.h"
 
 #include "t_context.h"
+#include "t_rebase.h"
 #include "tnl.h"
 
 
 
-static GLubyte *get_space(GLcontext *ctx, GLuint bytes)
+static GLubyte *get_space(struct gl_context *ctx, GLuint bytes)
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
    GLubyte *space = malloc(bytes);
@@ -48,7 +55,7 @@ static GLubyte *get_space(GLcontext *ctx, GLuint bytes)
 }
 
 
-static void free_space(GLcontext *ctx)
+static void free_space(struct gl_context *ctx)
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
    GLuint i;
@@ -63,14 +70,14 @@ static void free_space(GLcontext *ctx)
  */
 #define CONVERT( TYPE, MACRO ) do {            \
    GLuint i, j;                                        \
-   if (input->Normalized) {                    \
+   if (attrib->Format.Normalized) {            \
       for (i = 0; i < count; i++) {            \
         const TYPE *in = (TYPE *)ptr;          \
         for (j = 0; j < sz; j++) {             \
            *fptr++ = MACRO(*in);               \
            in++;                               \
         }                                      \
-        ptr += input->StrideB;                 \
+         ptr += binding->Stride;               \
       }                                                \
    } else {                                    \
       for (i = 0; i < count; i++) {            \
@@ -79,7 +86,7 @@ static void free_space(GLcontext *ctx)
            *fptr++ = (GLfloat)(*in);           \
            in++;                               \
         }                                      \
-        ptr += input->StrideB;                 \
+         ptr += binding->Stride;               \
       }                                                \
    }                                           \
 } while (0)
@@ -91,25 +98,27 @@ static void free_space(GLcontext *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_buffer_binding *binding,
+                      const struct gl_array_attributes *attrib,
                       const GLubyte *ptr, GLfloat *fptr,
                       GLuint count )
 {
    GLuint i;
-   assert(input->Normalized);
-   assert(input->Size == 4);
+   assert(attrib->Format.Normalized);
+   assert(attrib->Format.Size == 4);
    for (i = 0; i < count; i++) {
       const GLubyte *in = (GLubyte *) ptr;  /* in is in BGRA order */
       *fptr++ = UBYTE_TO_FLOAT(in[2]);  /* red */
       *fptr++ = UBYTE_TO_FLOAT(in[1]);  /* green */
       *fptr++ = UBYTE_TO_FLOAT(in[0]);  /* blue */
       *fptr++ = UBYTE_TO_FLOAT(in[3]);  /* alpha */
-      ptr += input->StrideB;
+      ptr += binding->Stride;
    }
 }
 
 static void
-convert_half_to_float(const struct gl_client_array *input,
+convert_half_to_float(const struct gl_vertex_buffer_binding *binding,
+                      const struct gl_array_attributes *attrib,
                      const GLubyte *ptr, GLfloat *fptr,
                      GLuint count, GLuint sz)
 {
@@ -121,36 +130,76 @@ convert_half_to_float(const struct gl_client_array *input,
       for (j = 0; j < sz; j++) {
         *fptr++ = _mesa_half_to_float(in[j]);
       }
-      ptr += input->StrideB;
+      ptr += binding->Stride;
+   }
+}
+
+/**
+ * \brief Convert fixed-point to floating-point.
+ *
+ * In OpenGL, a fixed-point number is a "signed 2's complement 16.16 scaled
+ * integer" (Table 2.2 of the OpenGL ES 2.0 spec).
+ *
+ * If the buffer has the \c normalized flag set, the formula
+ *     \code normalize(x) := (2*x + 1) / (2^16 - 1) \endcode
+ * is used to map the fixed-point numbers into the range [-1, 1].
+ */
+static void
+convert_fixed_to_float(const struct gl_vertex_buffer_binding *binding,
+                       const struct gl_array_attributes *attrib,
+                       const GLubyte *ptr, GLfloat *fptr,
+                       GLuint count)
+{
+   GLuint i;
+   GLint j;
+   const GLint size = attrib->Format.Size;
+
+   if (attrib->Format.Normalized) {
+      for (i = 0; i < count; ++i) {
+         const GLfixed *in = (GLfixed *) ptr;
+         for (j = 0; j < size; ++j) {
+            *fptr++ = (GLfloat) (2 * in[j] + 1) / (GLfloat) ((1 << 16) - 1);
+         }
+         ptr += binding->Stride;
+      }
+   } else {
+      for (i = 0; i < count; ++i) {
+         const GLfixed *in = (GLfixed *) ptr;
+         for (j = 0; j < size; ++j) {
+            *fptr++ = in[j] / (GLfloat) (1 << 16);
+         }
+         ptr += binding->Stride;
+      }
    }
 }
 
 /* Adjust pointer to point at first requested element, convert to
  * floating point, populate VB->AttribPtr[].
  */
-static void _tnl_import_array( GLcontext *ctx,
-                              GLuint attrib,
+static void _tnl_import_array( struct gl_context *ctx,
+                               GLuint attr,
                               GLuint count,
-                              const struct gl_client_array *input,
+                               const struct gl_vertex_buffer_binding *binding,
+                               const struct gl_array_attributes *attrib,
                               const GLubyte *ptr )
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
    struct vertex_buffer *VB = &tnl->vb;
-   GLuint stride = input->StrideB;
+   GLuint stride = binding->Stride;
 
-   if (input->Type != GL_FLOAT) {
-      const GLuint sz = input->Size;
+   if (attrib->Format.Type != GL_FLOAT) {
+      const GLuint sz = attrib->Format.Size;
       GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat));
       GLfloat *fptr = (GLfloat *)buf;
 
-      switch (input->Type) {
+      switch (attrib->Format.Type) {
       case GL_BYTE: 
         CONVERT(GLbyte, BYTE_TO_FLOAT); 
         break;
       case GL_UNSIGNED_BYTE: 
-         if (input->Format == GL_BGRA) {
+         if (attrib->Format.Format == GL_BGRA) {
             /* See GL_EXT_vertex_array_bgra */
-            convert_bgra_to_float(input, ptr, fptr, count);
+            convert_bgra_to_float(binding, attrib, ptr, fptr, count);
          }
          else {
             CONVERT(GLubyte, UBYTE_TO_FLOAT); 
@@ -172,8 +221,11 @@ static void _tnl_import_array( GLcontext *ctx,
         CONVERT(GLdouble, (GLfloat)); 
         break;
       case GL_HALF_FLOAT:
-        convert_half_to_float(input, ptr, fptr, count, sz);
+        convert_half_to_float(binding, attrib, ptr, fptr, count, sz);
         break;
+      case GL_FIXED:
+         convert_fixed_to_float(binding, attrib, ptr, fptr, count);
+         break;
       default:
         assert(0);
         break;
@@ -183,26 +235,26 @@ static void _tnl_import_array( GLcontext *ctx,
       stride = sz * sizeof(GLfloat);
    }
 
-   VB->AttribPtr[attrib] = &tnl->tmp_inputs[attrib];
-   VB->AttribPtr[attrib]->data = (GLfloat (*)[4])ptr;
-   VB->AttribPtr[attrib]->start = (GLfloat *)ptr;
-   VB->AttribPtr[attrib]->count = count;
-   VB->AttribPtr[attrib]->stride = stride;
-   VB->AttribPtr[attrib]->size = input->Size;
+   VB->AttribPtr[attr] = &tnl->tmp_inputs[attr];
+   VB->AttribPtr[attr]->data = (GLfloat (*)[4])ptr;
+   VB->AttribPtr[attr]->start = (GLfloat *)ptr;
+   VB->AttribPtr[attr]->count = count;
+   VB->AttribPtr[attr]->stride = stride;
+   VB->AttribPtr[attr]->size = attrib->Format.Size;
 
    /* This should die, but so should the whole GLvector4f concept: 
     */
-   VB->AttribPtr[attrib]->flags = (((1<<input->Size)-1) | 
+   VB->AttribPtr[attr]->flags = (((1<<attrib->Format.Size)-1) |
                                   VEC_NOT_WRITEABLE |
                                   (stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE));
    
-   VB->AttribPtr[attrib]->storage = NULL;
+   VB->AttribPtr[attr]->storage = NULL;
 }
 
 #define CLIPVERTS  ((6 + MAX_CLIP_PLANES) * 2)
 
 
-static GLboolean *_tnl_import_edgeflag( GLcontext *ctx,
+static GLboolean *_tnl_import_edgeflag( struct gl_context *ctx,
                                        const GLvector4f *input,
                                        GLuint count)
 {
@@ -213,7 +265,7 @@ static GLboolean *_tnl_import_edgeflag( GLcontext *ctx,
    GLuint i;
 
    for (i = 0; i < count; i++) {
-      *bptr++ = ((GLfloat *)ptr)[0] == 1.0;
+      *bptr++ = ((GLfloat *)ptr)[0] == 1.0F;
       ptr += stride;
    }
 
@@ -221,8 +273,8 @@ static GLboolean *_tnl_import_edgeflag( GLcontext *ctx,
 }
 
 
-static void bind_inputs( GLcontext *ctx, 
-                        const struct gl_client_array *inputs[],
+static void bind_inputs( struct gl_context *ctx, 
+                        const struct tnl_vertex_array *inputs,
                         GLint count,
                         struct gl_buffer_object **bo,
                         GLuint *nr_bo )
@@ -234,25 +286,28 @@ static void bind_inputs( GLcontext *ctx,
    /* Map all the VBOs
     */
    for (i = 0; i < VERT_ATTRIB_MAX; i++) {
+      const struct tnl_vertex_array *array = &inputs[i];
+      const struct gl_vertex_buffer_binding *binding = array->BufferBinding;
+      const struct gl_array_attributes *attrib = array->VertexAttrib;
       const void *ptr;
 
-      if (inputs[i]->BufferObj->Name) { 
-        if (!inputs[i]->BufferObj->Pointer) {
-           bo[*nr_bo] = inputs[i]->BufferObj;
+      if (_mesa_is_bufferobj(binding->BufferObj)) {
+        if (!binding->BufferObj->Mappings[MAP_INTERNAL].Pointer) {
+           bo[*nr_bo] = binding->BufferObj;
            (*nr_bo)++;
-           ctx->Driver.MapBuffer(ctx, 
-                                 GL_ARRAY_BUFFER,
-                                 GL_READ_ONLY_ARB,
-                                 inputs[i]->BufferObj);
+           ctx->Driver.MapBufferRange(ctx, 0, binding->BufferObj->Size,
+                                      GL_MAP_READ_BIT,
+                                       binding->BufferObj,
+                                       MAP_INTERNAL);
            
-           assert(inputs[i]->BufferObj->Pointer);
+            assert(binding->BufferObj->Mappings[MAP_INTERNAL].Pointer);
         }
         
-        ptr = ADD_POINTERS(inputs[i]->BufferObj->Pointer,
-                           inputs[i]->Ptr);
+         ptr = ADD_POINTERS(binding->BufferObj->Mappings[MAP_INTERNAL].Pointer,
+                            binding->Offset + attrib->RelativeOffset);
       }
       else
-        ptr = inputs[i]->Ptr;
+         ptr = attrib->Ptr;
 
       /* Just make sure the array is floating point, otherwise convert to
        * temporary storage.  
@@ -260,7 +315,7 @@ static void bind_inputs( GLcontext *ctx,
        * XXX: remove the GLvector4f type at some stage and just use
        * client arrays.
        */
-      _tnl_import_array(ctx, i, count, inputs[i], ptr);
+      _tnl_import_array(ctx, i, count, binding, attrib, ptr);
    }
 
    /* We process only the vertices between min & max index:
@@ -292,7 +347,7 @@ static void bind_inputs( GLcontext *ctx,
 
 /* Translate indices to GLuints and store in VB->Elts.
  */
-static void bind_indices( GLcontext *ctx,
+static void bind_indices( struct gl_context *ctx,
                          const struct _mesa_index_buffer *ib,
                          struct gl_buffer_object **bo,
                          GLuint *nr_bo)
@@ -300,39 +355,41 @@ static void bind_indices( GLcontext *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) {
+   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)++;
-      ctx->Driver.MapBuffer(ctx, 
-                           GL_ELEMENT_ARRAY_BUFFER,
-                           GL_READ_ONLY_ARB,
-                           ib->obj);
-
-      assert(ib->obj->Pointer);
+      ptr = ctx->Driver.MapBufferRange(ctx, (GLsizeiptr) ib->ptr,
+                                       ib->count << ib->index_size_shift,
+                                      GL_MAP_READ_BIT, ib->obj,
+                                       MAP_INTERNAL);
+      assert(ib->obj->Mappings[MAP_INTERNAL].Pointer);
+   } else {
+      /* user-space elements, or buffer already mapped */
+      ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
    }
 
-   ptr = ADD_POINTERS(ib->obj->Pointer, ib->ptr);
-
-   if (ib->type == GL_UNSIGNED_INT && VB->Primitive[0].basevertex == 0) {
+   if (ib->index_size_shift == 2 && 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_shift == 2) {
         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_shift == 1) {
         const GLushort *in = (GLushort *)ptr;
         for (i = 0; i < ib->count; i++) 
            *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
@@ -345,7 +402,7 @@ static void bind_indices( GLcontext *ctx,
    }
 }
 
-static void bind_prims( GLcontext *ctx,
+static void bind_prims( struct gl_context *ctx,
                        const struct _mesa_prim *prim,
                        GLuint nr_prims )
 {
@@ -356,45 +413,31 @@ static void bind_prims( GLcontext *ctx,
    VB->PrimitiveCount = nr_prims;
 }
 
-static void unmap_vbos( GLcontext *ctx,
+static void unmap_vbos( struct gl_context *ctx,
                        struct gl_buffer_object **bo,
                        GLuint nr_bo )
 {
    GLuint i;
    for (i = 0; i < nr_bo; i++) { 
-      ctx->Driver.UnmapBuffer(ctx, 
-                             0, /* target -- I don't see why this would be needed */
-                             bo[i]);
+      ctx->Driver.UnmapBuffer(ctx, bo[i], MAP_INTERNAL);
    }
 }
 
 
-void _tnl_vbo_draw_prims(GLcontext *ctx,
-                        const struct gl_client_array *arrays[],
+/* This is the main workhorse doing all the rendering work.
+ */
+void _tnl_draw_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,
                         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( GLcontext *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,
+                         GLuint num_instances,
+                         GLuint base_instance,
+                        struct gl_transform_feedback_object *tfb_vertcount,
+                         unsigned stream)
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
    const GLuint TEST_SPLIT = 0;
@@ -402,6 +445,9 @@ void _tnl_draw_prims( GLcontext *ctx,
    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);
 
@@ -413,10 +459,10 @@ void _tnl_draw_prims( GLcontext *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);
    }
@@ -424,9 +470,9 @@ void _tnl_draw_prims( GLcontext *ctx,
    if (min_index) {
       /* We always translate away calls with min_index != 0. 
        */
-      vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, 
-                       min_index, max_index,
-                       _tnl_vbo_draw_prims );
+      t_rebase_prims( ctx, arrays, prim, nr_prims, ib,
+                      min_index, max_index, num_instances, base_instance,
+                      _tnl_draw_prims );
       return;
    }
    else if ((GLint)max_index + max_basevertex > max) {
@@ -442,10 +488,11 @@ void _tnl_draw_prims( GLcontext *ctx,
       /* This will split the buffers one way or another and
        * recursively call back into this function.
        */
-      vbo_split_prims( ctx, arrays, prim, nr_prims, ib, 
-                      0, max_index + prim->basevertex,
-                      _tnl_vbo_draw_prims,
-                      &limits );
+      _tnl_split_prims( ctx, arrays, prim, nr_prims, ib,
+                        0, max_index + prim->basevertex,
+                        num_instances, base_instance,
+                        _tnl_draw_prims,
+                        &limits );
    }
    else {
       /* May need to map a vertex buffer object for every attribute plus
@@ -453,6 +500,9 @@ void _tnl_draw_prims( GLcontext *ctx,
        */
       struct gl_buffer_object *bo[VERT_ATTRIB_MAX + 1];
       GLuint nr_bo = 0;
+      GLuint inst;
+
+      assert(num_instances > 0);
 
       for (i = 0; i < nr_prims;) {
         GLuint this_nr_prims;
@@ -470,18 +520,142 @@ void _tnl_draw_prims( GLcontext *ctx,
         /* Binding inputs may imply mapping some vertex buffer objects.
          * They will need to be unmapped below.
          */
-        bind_prims(ctx, &prim[i], this_nr_prims);
-        bind_inputs(ctx, arrays, max_index + prim[i].basevertex + 1,
-                    bo, &nr_bo);
-        bind_indices(ctx, ib, bo, &nr_bo);
+         for (inst = 0; inst < num_instances; inst++) {
 
-        TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx);
+            bind_prims(ctx, &prim[i], this_nr_prims);
+            bind_inputs(ctx, arrays, max_index + prim[i].basevertex + 1,
+                        bo, &nr_bo);
+            bind_indices(ctx, ib, bo, &nr_bo);
 
-        unmap_vbos(ctx, bo, nr_bo);
-        free_space(ctx);
+            tnl->CurInstance = inst;
+            TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx);
+
+            unmap_vbos(ctx, bo, nr_bo);
+            free_space(ctx);
+         }
 
         i += this_nr_prims;
       }
    }
 }
 
+
+void
+_tnl_init_inputs(struct tnl_inputs *inputs)
+{
+   inputs->current = 0;
+   inputs->vertex_processing_mode = VP_MODE_FF;
+}
+
+
+/**
+ * Update the tnl_inputs's arrays to point to the vao->_VertexArray arrays
+ * according to the 'enable' bitmask.
+ * \param enable  bitfield of VERT_BIT_x flags.
+ */
+static inline void
+update_vao_inputs(struct gl_context *ctx,
+                  struct tnl_inputs *inputs, GLbitfield enable)
+{
+   const struct gl_vertex_array_object *vao = ctx->Array._DrawVAO;
+
+   /* Make sure we process only arrays enabled in the VAO */
+   assert((enable & ~_mesa_get_vao_vp_inputs(vao)) == 0);
+
+   /* Fill in the client arrays from the VAO */
+   const struct gl_vertex_buffer_binding *bindings = &vao->BufferBinding[0];
+   while (enable) {
+      const int attr = u_bit_scan(&enable);
+      struct tnl_vertex_array *input = &inputs->inputs[attr];
+      const struct gl_array_attributes *attrib;
+      attrib = _mesa_draw_array_attrib(vao, attr);
+      input->VertexAttrib = attrib;
+      input->BufferBinding = &bindings[attrib->BufferBindingIndex];
+   }
+}
+
+
+/**
+ * Update the tnl_inputs's arrays to point to the vbo->currval arrays
+ * according to the 'current' bitmask.
+ * \param current  bitfield of VERT_BIT_x flags.
+ */
+static inline void
+update_current_inputs(struct gl_context *ctx,
+                      struct tnl_inputs *inputs, GLbitfield current)
+{
+   gl_vertex_processing_mode mode = ctx->VertexProgram._VPMode;
+
+   /* All previously non current array pointers need update. */
+   GLbitfield mask = current & ~inputs->current;
+   /* On mode change, the slots aliasing with materials need update too */
+   if (mode != inputs->vertex_processing_mode)
+      mask |= current & VERT_BIT_MAT_ALL;
+
+   while (mask) {
+      const int attr = u_bit_scan(&mask);
+      struct tnl_vertex_array *input = &inputs->inputs[attr];
+      input->VertexAttrib = _vbo_current_attrib(ctx, attr);
+      input->BufferBinding = _vbo_current_binding(ctx);
+   }
+
+   inputs->current = current;
+   inputs->vertex_processing_mode = mode;
+}
+
+
+/**
+ * Update the tnl_inputs's arrays to point to the vao->_VertexArray and
+ * vbo->currval arrays according to Array._DrawVAO and
+ * Array._DrawVAOEnableAttribs.
+ */
+void
+_tnl_update_inputs(struct gl_context *ctx, struct tnl_inputs *inputs)
+{
+   const GLbitfield enable = ctx->Array._DrawVAOEnabledAttribs;
+
+   /* Update array input pointers */
+   update_vao_inputs(ctx, inputs, enable);
+
+   /* The rest must be current inputs. */
+   update_current_inputs(ctx, inputs, ~enable & VERT_BIT_ALL);
+}
+
+
+const struct tnl_vertex_array*
+_tnl_bind_inputs( struct gl_context *ctx )
+{
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   _tnl_update_inputs(ctx, &tnl->draw_arrays);
+   return tnl->draw_arrays.inputs;
+}
+
+
+/* This is the main entrypoint into the slimmed-down software tnl
+ * module.  In a regular swtnl driver, this can be plugged straight
+ * into the ctx->Driver.Draw() callback.
+ */
+void
+_tnl_draw(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,
+          GLuint num_instances, GLuint base_instance,
+          struct gl_transform_feedback_object *tfb_vertcount,
+          unsigned stream)
+{
+   /* Update TNLcontext::draw_arrays and return that pointer.
+    */
+   const struct tnl_vertex_array* arrays = _tnl_bind_inputs(ctx);
+
+   _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib,
+                   index_bounds_valid, min_index, max_index,
+                   num_instances, base_instance, tfb_vertcount, stream);
+}
+
+
+void
+_tnl_init_driver_draw_function(struct dd_function_table *functions)
+{
+   functions->Draw = _tnl_draw;
+}