mesa: remove _mesa_index_buffer::index_size in favor of index_size_shift
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_vbo_t.c
index a365b977f294c08afc77647fcf68377e5e73ef71..f516bd3e4ab8519c4c5a77d21e365440d0deea87 100644 (file)
  *
  */
 
+#include "nouveau_driver.h"
 #include "nouveau_bufferobj.h"
 #include "nouveau_util.h"
 
 #include "main/bufferobj.h"
+#include "main/glformats.h"
+#include "main/varray.h"
 #include "main/image.h"
 
 /* Arbitrary pushbuf length we can assume we can get with a single
- * WAIT_RING. */
-#define PUSHBUF_DWORDS 2048
+ * call to WAIT_RING. */
+#define PUSHBUF_DWORDS 65536
 
-/* Functions to set up struct nouveau_array_state from something like
- * a GL array or index buffer. */
+/* Functions to turn GL arrays or index buffers into nouveau_array
+ * structures. */
 
-static void
-vbo_init_array(struct nouveau_array_state *a, int attr, int stride,
-              int fields, int type, struct gl_buffer_object *obj,
-              const void *ptr, GLboolean map)
+static int
+get_array_stride(struct gl_context *ctx, const struct tnl_vertex_array *a)
 {
-       a->attr = attr;
-       a->stride = stride;
-       a->fields = fields;
-       a->type = type;
-
-       if (_mesa_is_bufferobj(obj)) {
-               nouveau_bo_ref(to_nouveau_bufferobj(obj)->bo, &a->bo);
-               a->offset = (intptr_t)ptr;
-
-               if (map) {
-                       nouveau_bo_map(a->bo, NOUVEAU_BO_RD);
-                       a->buf = a->bo->map + a->offset;
-               } else {
-                       a->buf = NULL;
-               }
+       struct nouveau_render_state *render = to_render_state(ctx);
+       const struct gl_vertex_buffer_binding *binding = a->BufferBinding;
 
+       if (render->mode == VBO && !_mesa_is_bufferobj(binding->BufferObj)) {
+               const struct gl_array_attributes *attrib = a->VertexAttrib;
+               /* Pack client buffers. */
+               return align(attrib->Format._ElementSize, 4);
        } else {
-               nouveau_bo_ref(NULL, &a->bo);
-               a->offset = 0;
-
-               if (map)
-                       a->buf = ptr;
-               else
-                       a->buf = NULL;
+               return binding->Stride;
        }
-
-       if (a->buf)
-               get_array_extract(a, &a->extract_u, &a->extract_f);
 }
 
 static void
-vbo_deinit_array(struct nouveau_array_state *a)
+vbo_init_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
+               const struct tnl_vertex_array *arrays)
 {
-       if (a->bo) {
-               if (a->bo->map)
-                       nouveau_bo_unmap(a->bo);
-               nouveau_bo_ref(NULL, &a->bo);
-       }
+       struct nouveau_render_state *render = to_render_state(ctx);
+       GLboolean imm = (render->mode == IMM);
+       int i, attr;
 
-       a->buf = NULL;
-       a->fields = 0;
-}
+       if (ib) {
+               GLenum ib_type;
 
-static void
-vbo_init_arrays(GLcontext *ctx, const struct _mesa_index_buffer *ib,
-               const struct gl_client_array **arrays)
-{
-       struct nouveau_render_state *render = to_render_state(ctx);
-       int i;
+               if (ib->index_size_shift == 2)
+                       ib_type = GL_UNSIGNED_INT;
+               else if (ib->index_size_shift == 1)
+                       ib_type = GL_UNSIGNED_SHORT;
+               else
+                       ib_type = GL_UNSIGNED_BYTE;
 
-       if (ib)
-               vbo_init_array(&render->ib, 0, 0, ib->count, ib->type,
-                              ib->obj, ib->ptr, GL_TRUE);
-
-       for (i = 0; i < render->attr_count; i++) {
-               int attr = render->map[i];
-
-               if (attr >= 0) {
-                       const struct gl_client_array *array = arrays[attr];
-                       int stride;
-
-                       if (render->mode == VBO &&
-                           !_mesa_is_bufferobj(array->BufferObj))
-                               /* Pack client buffers. */
-                               stride = align(_mesa_sizeof_type(array->Type)
-                                              * array->Size, 4);
-                       else
-                               stride = array->StrideB;
-
-                       vbo_init_array(&render->attrs[attr], attr,
-                                      stride, array->Size, array->Type,
-                                      array->BufferObj, array->Ptr,
-                                      render->mode == IMM);
-               }
+               nouveau_init_array(&render->ib, 0, 0, ib->count, ib_type,
+                                  ib->obj, ib->ptr, GL_TRUE, ctx);
+       }
+
+       FOR_EACH_BOUND_ATTR(render, i, attr) {
+               const struct tnl_vertex_array *array = &arrays[attr];
+               const struct gl_vertex_buffer_binding *binding =
+                       array->BufferBinding;
+               const struct gl_array_attributes *attrib = array->VertexAttrib;
+               const GLubyte *p = _mesa_vertex_attrib_address(attrib, binding);
+
+               nouveau_init_array(&render->attrs[attr], attr,
+                                  get_array_stride(ctx, array),
+                                  attrib->Format.Size, attrib->Format.Type,
+                                  imm ? binding->BufferObj : NULL,
+                                  p, imm, ctx);
        }
 }
 
 static void
-vbo_deinit_arrays(GLcontext *ctx, const struct _mesa_index_buffer *ib,
-               const struct gl_client_array **arrays)
+vbo_deinit_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
+                 const struct tnl_vertex_array *arrays)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
-       int i;
+       int i, attr;
 
        if (ib)
-               vbo_deinit_array(&render->ib);
+               nouveau_cleanup_array(&render->ib);
 
-       for (i = 0; i < render->attr_count; i++) {
-               int *attr = &render->map[i];
+       FOR_EACH_BOUND_ATTR(render, i, attr) {
+               struct nouveau_array *a = &render->attrs[attr];
 
-               if (*attr >= 0) {
-                       vbo_deinit_array(&render->attrs[*attr]);
-                       *attr = -1;
-               }
+               if (render->mode == IMM)
+                       nouveau_bo_ref(NULL, &a->bo);
+
+               nouveau_deinit_array(a);
+               render->map[i] = -1;
        }
 
        render->attr_count = 0;
@@ -144,7 +118,7 @@ vbo_deinit_arrays(GLcontext *ctx, const struct _mesa_index_buffer *ib,
 /* Make some rendering decisions from the GL context. */
 
 static void
-vbo_choose_render_mode(GLcontext *ctx, const struct gl_client_array **arrays)
+vbo_choose_render_mode(struct gl_context *ctx, const struct tnl_vertex_array *arrays)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
        int i;
@@ -152,40 +126,39 @@ vbo_choose_render_mode(GLcontext *ctx, const struct gl_client_array **arrays)
        render->mode = VBO;
 
        if (ctx->Light.Enabled) {
-               for (i = 0; i < MAT_ATTRIB_MAX; i++) {
-                       if (arrays[VERT_ATTRIB_GENERIC0 + i]->StrideB) {
+               for (i = 0; i < VERT_ATTRIB_MAT_MAX; i++) {
+                       if (arrays[VERT_ATTRIB_MAT(i)].BufferBinding->Stride) {
                                render->mode = IMM;
                                break;
                        }
                }
        }
-
-       if (render->mode == VBO)
-               render->attr_count = NUM_VERTEX_ATTRS;
-       else
-               render->attr_count = 0;
 }
 
 static void
-vbo_emit_attr(GLcontext *ctx, const struct gl_client_array **arrays, int attr)
+vbo_emit_attr(struct gl_context *ctx, const struct tnl_vertex_array *arrays,
+             int attr)
 {
-       struct nouveau_channel *chan = context_chan(ctx);
+       struct nouveau_pushbuf *push = context_push(ctx);
        struct nouveau_render_state *render = to_render_state(ctx);
-       const struct gl_client_array *array = arrays[attr];
-       struct nouveau_array_state *a = &render->attrs[attr];
+       const struct tnl_vertex_array *array = &arrays[attr];
+       const struct gl_vertex_buffer_binding *binding = array->BufferBinding;
+       const struct gl_array_attributes *attrib = array->VertexAttrib;
+       const GLubyte *p = _mesa_vertex_attrib_address(attrib, binding);
+       struct nouveau_array *a = &render->attrs[attr];
        RENDER_LOCALS(ctx);
 
-       if (!array->StrideB) {
-               if (attr >= VERT_ATTRIB_GENERIC0)
+       if (!binding->Stride) {
+               if (attr >= VERT_ATTRIB_MAT(0))
                        /* nouveau_update_state takes care of materials. */
                        return;
 
                /* Constant attribute. */
-               vbo_init_array(a, attr, array->StrideB, array->Size,
-                              array->Type, array->BufferObj, array->Ptr,
-                              GL_TRUE);
+               nouveau_init_array(a, attr, binding->Stride, attrib->Format.Size,
+                                  attrib->Format.Type, binding->BufferObj, p,
+                                  GL_TRUE, ctx);
                EMIT_IMM(ctx, a, 0);
-               vbo_deinit_array(a);
+               nouveau_deinit_array(a);
 
        } else {
                /* Varying attribute. */
@@ -193,7 +166,9 @@ vbo_emit_attr(GLcontext *ctx, const struct gl_client_array **arrays, int attr)
 
                if (render->mode == VBO) {
                        render->map[info->vbo_index] = attr;
-                       render->vertex_size += array->_ElementSize;
+                       render->vertex_size += attrib->Format._ElementSize;
+                       render->attr_count = MAX2(render->attr_count,
+                                                 info->vbo_index + 1);
                } else {
                        render->map[render->attr_count++] = attr;
                        render->vertex_size += 4 * info->imm_fields;
@@ -201,16 +176,17 @@ vbo_emit_attr(GLcontext *ctx, const struct gl_client_array **arrays, int attr)
        }
 }
 
-#define MAT(a) (VERT_ATTRIB_GENERIC0 + MAT_ATTRIB_##a)
+#define MAT(a) VERT_ATTRIB_MAT(MAT_ATTRIB_##a)
 
 static void
-vbo_choose_attrs(GLcontext *ctx, const struct gl_client_array **arrays)
+vbo_choose_attrs(struct gl_context *ctx, const struct tnl_vertex_array *arrays)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
        int i;
 
        /* Reset the vertex size. */
        render->vertex_size = 0;
+       render->attr_count = 0;
 
        vbo_emit_attr(ctx, arrays, VERT_ATTRIB_COLOR0);
        if (ctx->Fog.ColorSumEnabled && !ctx->Light.Enabled)
@@ -224,9 +200,11 @@ vbo_choose_attrs(GLcontext *ctx, const struct gl_client_array **arrays)
        if (ctx->Fog.Enabled && ctx->Fog.FogCoordinateSource == GL_FOG_COORD)
                vbo_emit_attr(ctx, arrays, VERT_ATTRIB_FOG);
 
-       if (ctx->Light.Enabled) {
+       if (ctx->Light.Enabled ||
+           (ctx->Texture._GenFlags & TEXGEN_NEED_NORMALS))
                vbo_emit_attr(ctx, arrays, VERT_ATTRIB_NORMAL);
 
+       if (ctx->Light.Enabled && render->mode == IMM) {
                vbo_emit_attr(ctx, arrays, MAT(FRONT_AMBIENT));
                vbo_emit_attr(ctx, arrays, MAT(FRONT_DIFFUSE));
                vbo_emit_attr(ctx, arrays, MAT(FRONT_SPECULAR));
@@ -243,48 +221,54 @@ vbo_choose_attrs(GLcontext *ctx, const struct gl_client_array **arrays)
        vbo_emit_attr(ctx, arrays, VERT_ATTRIB_POS);
 }
 
-static unsigned
-get_max_client_stride(GLcontext *ctx)
+static int
+get_max_client_stride(struct gl_context *ctx, const struct tnl_vertex_array *arrays)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
-       int i, s = 0;
+       int i, attr, s = 0;
 
-       for (i = 0; i < render->attr_count; i++) {
-               int attr = render->map[i];
-               struct nouveau_array_state *a = &render->attrs[attr];
+       FOR_EACH_BOUND_ATTR(render, i, attr) {
+               const struct tnl_vertex_array *a = &arrays[attr];
 
-               if (attr >= 0 && !a->bo)
-                       s = MAX2(a->stride, s);
+               if (!_mesa_is_bufferobj(a->BufferBinding->BufferObj))
+                       s = MAX2(s, get_array_stride(ctx, a));
        }
 
        return s;
 }
 
 static void
-TAG(vbo_render_prims)(GLcontext *ctx, const struct gl_client_array **arrays,
+TAG(vbo_render_prims)(struct gl_context *ctx,
+                     const struct tnl_vertex_array *arrays,
                      const struct _mesa_prim *prims, GLuint nr_prims,
                      const struct _mesa_index_buffer *ib,
                      GLboolean index_bounds_valid,
-                     GLuint min_index, GLuint max_index);
+                     GLuint min_index, GLuint max_index,
+                      GLuint num_instances, GLuint base_instance,
+                     struct gl_transform_feedback_object *tfb_vertcount,
+                      unsigned stream);
 
 static GLboolean
-vbo_maybe_split(GLcontext *ctx, const struct gl_client_array **arrays,
+vbo_maybe_split(struct gl_context *ctx, const struct tnl_vertex_array *arrays,
            const struct _mesa_prim *prims, GLuint nr_prims,
            const struct _mesa_index_buffer *ib,
-           GLuint min_index, GLuint max_index)
+           GLuint min_index, GLuint max_index,
+            GLuint num_instances, GLuint base_instance)
 {
        struct nouveau_context *nctx = to_nouveau_context(ctx);
        struct nouveau_render_state *render = to_render_state(ctx);
-       unsigned pushbuf_avail = PUSHBUF_DWORDS - 2 * nctx->bo.count,
+       struct nouveau_bufctx *bufctx = nctx->hw.bufctx;
+       unsigned pushbuf_avail = PUSHBUF_DWORDS - 2 * (bufctx->relocs +
+                                                      render->attr_count),
                vert_avail = get_max_vertices(ctx, NULL, pushbuf_avail),
                idx_avail = get_max_vertices(ctx, ib, pushbuf_avail);
        int stride;
 
        /* Try to keep client buffers smaller than the scratch BOs. */
        if (render->mode == VBO &&
-           (stride = get_max_client_stride(ctx)))
+           (stride = get_max_client_stride(ctx, arrays)))
                    vert_avail = MIN2(vert_avail,
-                                     RENDER_SCRATCH_SIZE / stride);
+                                     NOUVEAU_SCRATCH_SIZE / stride);
 
        if (max_index - min_index > vert_avail ||
            (ib && ib->count > idx_avail)) {
@@ -294,8 +278,9 @@ vbo_maybe_split(GLcontext *ctx, const struct gl_client_array **arrays,
                        .max_vb_size = ~0,
                };
 
-               vbo_split_prims(ctx, arrays, prims, nr_prims, ib, min_index,
-                               max_index, TAG(vbo_render_prims), &limits);
+               _tnl_split_prims(ctx, arrays, prims, nr_prims, ib, min_index,
+                                max_index, num_instances, base_instance,
+                                 TAG(vbo_render_prims), &limits);
                return GL_TRUE;
        }
 
@@ -304,54 +289,111 @@ vbo_maybe_split(GLcontext *ctx, const struct gl_client_array **arrays,
 
 /* VBO rendering path. */
 
+static GLboolean
+check_update_array(struct nouveau_array *a, unsigned offset,
+                  struct nouveau_bo *bo, int *pdelta)
+{
+       int delta = *pdelta;
+       GLboolean dirty;
+
+       if (a->bo == bo) {
+               if (delta < 0)
+                       delta = ((int)offset - (int)a->offset) / a->stride;
+
+               dirty = (delta < 0 ||
+                        offset != (a->offset + delta * a->stride));
+       } else {
+               dirty = GL_TRUE;
+       }
+
+       *pdelta = (dirty ? 0 : delta);
+       return dirty;
+}
+
 static void
-vbo_bind_vertices(GLcontext *ctx, const struct gl_client_array **arrays,
-                 GLint basevertex, GLuint min_index, GLuint max_index)
+vbo_bind_vertices(struct gl_context *ctx, const struct tnl_vertex_array *arrays,
+                 int base, unsigned min_index, unsigned max_index, int *pdelta)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
-       int i;
+       struct nouveau_pushbuf *push = context_push(ctx);
+       struct nouveau_bo *bo[NUM_VERTEX_ATTRS];
+       unsigned offset[NUM_VERTEX_ATTRS];
+       GLboolean dirty = GL_FALSE;
+       int i, j, attr;
+       RENDER_LOCALS(ctx);
 
-       for (i = 0; i < NUM_VERTEX_ATTRS; i++) {
-               int attr = render->map[i];
-
-               if (attr >= 0) {
-                       const struct gl_client_array *array = arrays[attr];
-                       struct nouveau_array_state *a = &render->attrs[attr];
-                       unsigned delta = (basevertex + min_index)
-                               * array->StrideB;
-
-                       if (a->bo) {
-                               a->offset = (intptr_t)array->Ptr + delta;
-                       } else {
-                               int j, n = max_index - min_index + 1;
-                               char *sp = (char *)array->Ptr + delta;
-                               char *dp = get_scratch_vbo(ctx, n * a->stride,
-                                                          &a->bo, &a->offset);
-
-                               for (j = 0; j < n; j++)
-                                       memcpy(dp + j * a->stride,
-                                              sp + j * array->StrideB,
-                                              a->stride);
-                       }
+       *pdelta = -1;
+
+       FOR_EACH_BOUND_ATTR(render, i, attr) {
+               const struct tnl_vertex_array *array = &arrays[attr];
+               const struct gl_vertex_buffer_binding *binding =
+                       array->BufferBinding;
+               const struct gl_array_attributes *attrib = array->VertexAttrib;
+               const GLubyte *p = _mesa_vertex_attrib_address(attrib, binding);
+               struct gl_buffer_object *obj = binding->BufferObj;
+               struct nouveau_array *a = &render->attrs[attr];
+               unsigned delta = (base + min_index) * binding->Stride;
+
+               bo[i] = NULL;
+
+               if (nouveau_bufferobj_hw(obj)) {
+                       /* Array in a buffer obj. */
+                       nouveau_bo_ref(to_nouveau_bufferobj(obj)->bo, &bo[i]);
+                       offset[i] = delta + (intptr_t)p;
+
+               } else {
+                       int n = max_index - min_index + 1;
+                       char *sp = (char *)ADD_POINTERS(
+                               nouveau_bufferobj_sys(obj), p) + delta;
+                       char *dp  = nouveau_get_scratch(ctx, n * a->stride,
+                                                       &bo[i], &offset[i]);
+
+                       /* Array in client memory, move it to a
+                        * scratch buffer obj. */
+                       for (j = 0; j < n; j++)
+                               memcpy(dp + j * a->stride,
+                                      sp + j * binding->Stride,
+                                      a->stride);
                }
+
+               dirty |= check_update_array(a, offset[i], bo[i], pdelta);
+       }
+
+       *pdelta -= min_index;
+
+       if (dirty) {
+               /* Buffers changed, update the attribute binding. */
+               FOR_EACH_BOUND_ATTR(render, i, attr) {
+                       struct nouveau_array *a = &render->attrs[attr];
+
+                       nouveau_bo_ref(NULL, &a->bo);
+                       a->offset = offset[i];
+                       a->bo = bo[i];
+               }
+
+               TAG(render_release_vertices)(ctx);
+               TAG(render_bind_vertices)(ctx);
+       } else {
+               /* Just cleanup. */
+               FOR_EACH_BOUND_ATTR(render, i, attr)
+                       nouveau_bo_ref(NULL, &bo[i]);
        }
 
-       TAG(render_bind_vertices)(ctx);
+       BATCH_VALIDATE();
 }
 
 static void
-vbo_draw_vbo(GLcontext *ctx, const struct gl_client_array **arrays,
+vbo_draw_vbo(struct gl_context *ctx, const struct tnl_vertex_array *arrays,
             const struct _mesa_prim *prims, GLuint nr_prims,
             const struct _mesa_index_buffer *ib, GLuint min_index,
             GLuint max_index)
 {
-       struct nouveau_channel *chan = context_chan(ctx);
-       dispatch_t dispatch;
-       int delta = -min_index, basevertex = 0, i;
+       struct nouveau_context *nctx = to_nouveau_context(ctx);
+       struct nouveau_pushbuf *push = context_push(ctx);
+       dispatch_t dispatch = get_array_dispatch(&to_render_state(ctx)->ib);
+       int i, delta = 0, basevertex = 0;
        RENDER_LOCALS(ctx);
 
-       get_array_dispatch(&to_render_state(ctx)->ib, &dispatch);
-
        TAG(render_set_format)(ctx);
 
        for (i = 0; i < nr_prims; i++) {
@@ -360,48 +402,62 @@ vbo_draw_vbo(GLcontext *ctx, const struct gl_client_array **arrays,
 
                if (i == 0 || basevertex != prims[i].basevertex) {
                        basevertex = prims[i].basevertex;
-                       vbo_bind_vertices(ctx, arrays, basevertex,
-                                         min_index, max_index);
+                       vbo_bind_vertices(ctx, arrays, basevertex, min_index,
+                                         max_index, &delta);
+
+                       nouveau_pushbuf_bufctx(push, nctx->hw.bufctx);
+                       if (nouveau_pushbuf_validate(push)) {
+                               nouveau_pushbuf_bufctx(push, NULL);
+                               return;
+                       }
                }
 
-               if (count > get_max_vertices(ctx, ib, AVAIL_RING(chan)))
-                       WAIT_RING(chan, PUSHBUF_DWORDS);
+               if (count > get_max_vertices(ctx, ib, PUSH_AVAIL(push)))
+                       PUSH_SPACE(push, PUSHBUF_DWORDS);
 
                BATCH_BEGIN(nvgl_primitive(prims[i].mode));
                dispatch(ctx, start, delta, count);
                BATCH_END();
        }
 
-       FIRE_RING(chan);
+       nouveau_pushbuf_bufctx(push, NULL);
+       TAG(render_release_vertices)(ctx);
 }
 
 /* Immediate rendering path. */
 
 static unsigned
-extract_id(struct nouveau_array_state *a, int i, int j)
+extract_id(struct nouveau_array *a, int i, int j)
 {
        return j;
 }
 
 static void
-vbo_draw_imm(GLcontext *ctx, const struct gl_client_array **arrays,
+vbo_draw_imm(struct gl_context *ctx, const struct tnl_vertex_array *arrays,
             const struct _mesa_prim *prims, GLuint nr_prims,
             const struct _mesa_index_buffer *ib, GLuint min_index,
             GLuint max_index)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
-       struct nouveau_channel *chan = context_chan(ctx);
+       struct nouveau_context *nctx = to_nouveau_context(ctx);
+       struct nouveau_pushbuf *push = context_push(ctx);
        extract_u_t extract = ib ? render->ib.extract_u : extract_id;
-       int i, j, k;
+       int i, j, k, attr;
        RENDER_LOCALS(ctx);
 
+       nouveau_pushbuf_bufctx(push, nctx->hw.bufctx);
+       if (nouveau_pushbuf_validate(push)) {
+               nouveau_pushbuf_bufctx(push, NULL);
+               return;
+       }
+
        for (i = 0; i < nr_prims; i++) {
                unsigned start = prims[i].start,
                        end = start + prims[i].count;
 
                if (prims[i].count > get_max_vertices(ctx, ib,
-                                                     AVAIL_RING(chan)))
-                       WAIT_RING(chan, PUSHBUF_DWORDS);
+                                                     PUSH_AVAIL(push)))
+                       PUSH_SPACE(push, PUSHBUF_DWORDS);
 
                BATCH_BEGIN(nvgl_primitive(prims[i].mode));
 
@@ -409,36 +465,40 @@ vbo_draw_imm(GLcontext *ctx, const struct gl_client_array **arrays,
                        j = prims[i].basevertex +
                                extract(&render->ib, 0, start);
 
-                       for (k = 0; k < render->attr_count; k++)
-                               EMIT_IMM(ctx, &render->attrs[render->map[k]],
-                                        j);
+                       FOR_EACH_BOUND_ATTR(render, k, attr)
+                               EMIT_IMM(ctx, &render->attrs[attr], j);
                }
 
                BATCH_END();
        }
 
-       FIRE_RING(chan);
+       nouveau_pushbuf_bufctx(push, NULL);
 }
 
 /* draw_prims entry point when we're doing hw-tnl. */
 
 static void
-TAG(vbo_render_prims)(GLcontext *ctx, const struct gl_client_array **arrays,
+TAG(vbo_render_prims)(struct gl_context *ctx,
+                     const struct tnl_vertex_array *arrays,
                      const struct _mesa_prim *prims, GLuint nr_prims,
                      const struct _mesa_index_buffer *ib,
                      GLboolean index_bounds_valid,
-                     GLuint min_index, GLuint max_index)
+                     GLuint min_index, GLuint max_index,
+                      GLuint num_instances, GLuint base_instance,
+                     struct gl_transform_feedback_object *tfb_vertcount,
+                      unsigned stream)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
 
        if (!index_bounds_valid)
-               vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
+               vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index,
+                                      nr_prims);
 
        vbo_choose_render_mode(ctx, arrays);
        vbo_choose_attrs(ctx, arrays);
 
        if (vbo_maybe_split(ctx, arrays, prims, nr_prims, ib, min_index,
-                           max_index))
+                           max_index, num_instances, base_instance))
                return;
 
        vbo_init_arrays(ctx, ib, arrays);
@@ -452,3 +512,71 @@ TAG(vbo_render_prims)(GLcontext *ctx, const struct gl_client_array **arrays,
 
        vbo_deinit_arrays(ctx, ib, arrays);
 }
+
+/* VBO rendering entry points. */
+
+static void
+TAG(vbo_check_render_prims)(struct gl_context *ctx,
+                           const struct tnl_vertex_array *arrays,
+                           const struct _mesa_prim *prims, 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)
+{
+       struct nouveau_context *nctx = to_nouveau_context(ctx);
+
+       nouveau_validate_framebuffer(ctx);
+
+       if (nctx->fallback == HWTNL)
+               TAG(vbo_render_prims)(ctx, arrays, prims, nr_prims, ib,
+                                     index_bounds_valid, min_index, max_index,
+                                     num_instances, base_instance,
+                                      tfb_vertcount, stream);
+
+       if (nctx->fallback == SWTNL)
+               _tnl_draw_prims(ctx, arrays, prims, nr_prims, ib,
+                               index_bounds_valid, min_index, max_index,
+                                num_instances, base_instance,
+                               tfb_vertcount, stream);
+}
+
+static void
+TAG(vbo_draw)(struct gl_context *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,
+              GLuint num_instances, GLuint base_instance,
+             struct gl_transform_feedback_object *tfb_vertcount,
+             unsigned stream)
+{
+       /* Borrow and update the inputs list from the tnl context */
+       const struct tnl_vertex_array* arrays = _tnl_bind_inputs(ctx);
+
+       TAG(vbo_check_render_prims)(ctx, arrays,
+                                   prims, nr_prims, ib,
+                                   index_bounds_valid, min_index, max_index,
+                                    num_instances, base_instance,
+                                   tfb_vertcount, stream);
+}
+
+void
+TAG(vbo_init)(struct gl_context *ctx)
+{
+       struct nouveau_render_state *render = to_render_state(ctx);
+       int i;
+
+       for (i = 0; i < VERT_ATTRIB_MAX; i++)
+               render->map[i] = -1;
+
+       /* Overwrite our draw function */
+       ctx->Driver.Draw = TAG(vbo_draw);
+}
+
+void
+TAG(vbo_destroy)(struct gl_context *ctx)
+{
+}