i965/vec4: Remove swizzle_for_size() in favour of brw_swizzle_for_size().
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_vbo_t.c
index c00bd316ed9a2284fcf529bd14be77eebbdc39dd..c85acec12680a2f2889185c4896170d76f9083c3 100644 (file)
  *
  */
 
+#include "nouveau_driver.h"
 #include "nouveau_bufferobj.h"
 #include "nouveau_util.h"
 
 #include "main/bufferobj.h"
+#include "main/glformats.h"
 #include "main/image.h"
 
 /* Arbitrary pushbuf length we can assume we can get with a single
@@ -59,7 +61,7 @@ vbo_init_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
 
        if (ib)
                nouveau_init_array(&render->ib, 0, 0, ib->count, ib->type,
-                                  ib->obj, ib->ptr, GL_TRUE);
+                                  ib->obj, ib->ptr, GL_TRUE, ctx);
 
        FOR_EACH_BOUND_ATTR(render, i, attr) {
                const struct gl_client_array *array = arrays[attr];
@@ -67,8 +69,8 @@ vbo_init_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
                nouveau_init_array(&render->attrs[attr], attr,
                                   get_array_stride(ctx, array),
                                   array->Size, array->Type,
-                                  array->BufferObj,
-                                  array->Ptr, imm);
+                                  imm ? array->BufferObj : NULL,
+                                  array->Ptr, imm, ctx);
        }
 }
 
@@ -85,12 +87,14 @@ vbo_deinit_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
        FOR_EACH_BOUND_ATTR(render, i, attr) {
                struct nouveau_array *a = &render->attrs[attr];
 
+               if (render->mode == IMM)
+                       nouveau_bo_ref(NULL, &a->bo);
+
                nouveau_deinit_array(a);
                render->map[i] = -1;
        }
 
        render->attr_count = 0;
-       context_bctx(ctx, VERTEX);
 }
 
 /* Make some rendering decisions from the GL context. */
@@ -117,7 +121,7 @@ static void
 vbo_emit_attr(struct gl_context *ctx, const struct gl_client_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 *a = &render->attrs[attr];
@@ -131,7 +135,7 @@ vbo_emit_attr(struct gl_context *ctx, const struct gl_client_array **arrays,
                /* Constant attribute. */
                nouveau_init_array(a, attr, array->StrideB, array->Size,
                                   array->Type, array->BufferObj, array->Ptr,
-                                  GL_TRUE);
+                                  GL_TRUE, ctx);
                EMIT_IMM(ctx, a, 0);
                nouveau_deinit_array(a);
 
@@ -148,7 +152,6 @@ vbo_emit_attr(struct gl_context *ctx, const struct gl_client_array **arrays,
                        render->map[render->attr_count++] = attr;
                        render->vertex_size += 4 * info->imm_fields;
                }
-
        }
 }
 
@@ -180,7 +183,7 @@ vbo_choose_attrs(struct gl_context *ctx, const struct gl_client_array **arrays)
            (ctx->Texture._GenFlags & TEXGEN_NEED_NORMALS))
                vbo_emit_attr(ctx, arrays, VERT_ATTRIB_NORMAL);
 
-       if (ctx->Light.Enabled) {
+       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));
@@ -214,11 +217,13 @@ get_max_client_stride(struct gl_context *ctx, const struct gl_client_array **arr
 }
 
 static void
-TAG(vbo_render_prims)(struct gl_context *ctx, const struct gl_client_array **arrays,
+TAG(vbo_render_prims)(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 min_index, GLuint max_index,
+                     struct gl_transform_feedback_object *tfb_vertcount,
+                     struct gl_buffer_object *indirect);
 
 static GLboolean
 vbo_maybe_split(struct gl_context *ctx, const struct gl_client_array **arrays,
@@ -228,7 +233,8 @@ vbo_maybe_split(struct gl_context *ctx, const struct gl_client_array **arrays,
 {
        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);
@@ -258,38 +264,93 @@ vbo_maybe_split(struct gl_context *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(struct gl_context *ctx, const struct gl_client_array **arrays,
-                 GLint basevertex, GLuint min_index, GLuint max_index)
+                 int base, unsigned min_index, unsigned max_index, int *pdelta)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
-       int i, attr;
+       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);
+
+       *pdelta = -1;
 
        FOR_EACH_BOUND_ATTR(render, i, attr) {
                const struct gl_client_array *array = arrays[attr];
+               struct gl_buffer_object *obj = array->BufferObj;
                struct nouveau_array *a = &render->attrs[attr];
-               unsigned delta = (basevertex + min_index)
-                       * array->StrideB;
+               unsigned delta = (base + min_index) * array->StrideB;
 
-               if (a->bo) {
+               bo[i] = NULL;
+
+               if (nouveau_bufferobj_hw(obj)) {
                        /* Array in a buffer obj. */
-                       a->offset = (intptr_t)array->Ptr + delta;
-               } else {
-                       int j, n = max_index - min_index + 1;
-                       char *sp = (char *)array->Ptr + delta;
-                       char *dp = nouveau_get_scratch(
-                               ctx, n * a->stride, &a->bo, &a->offset);
+                       nouveau_bo_ref(to_nouveau_bufferobj(obj)->bo, &bo[i]);
+                       offset[i] = delta + (intptr_t)array->Ptr;
 
-                       /* Array in client memory, move it to
-                        * a scratch buffer obj. */
+               } else {
+                       int n = max_index - min_index + 1;
+                       char *sp = (char *)ADD_POINTERS(
+                               nouveau_bufferobj_sys(obj), array->Ptr) + 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 * array->StrideB,
                                       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
@@ -298,9 +359,10 @@ vbo_draw_vbo(struct gl_context *ctx, const struct gl_client_array **arrays,
             const struct _mesa_index_buffer *ib, GLuint min_index,
             GLuint max_index)
 {
-       struct nouveau_channel *chan = context_chan(ctx);
+       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 = -min_index, basevertex = 0;
+       int i, delta = 0, basevertex = 0;
        RENDER_LOCALS(ctx);
 
        TAG(render_set_format)(ctx);
@@ -311,17 +373,26 @@ vbo_draw_vbo(struct gl_context *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();
        }
+
+       nouveau_pushbuf_bufctx(push, NULL);
+       TAG(render_release_vertices)(ctx);
 }
 
 /* Immediate rendering path. */
@@ -339,18 +410,25 @@ vbo_draw_imm(struct gl_context *ctx, const struct gl_client_array **arrays,
             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, 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));
 
@@ -364,22 +442,27 @@ vbo_draw_imm(struct gl_context *ctx, const struct gl_client_array **arrays,
 
                BATCH_END();
        }
+
+       nouveau_pushbuf_bufctx(push, NULL);
 }
 
 /* draw_prims entry point when we're doing hw-tnl. */
 
 static void
 TAG(vbo_render_prims)(struct gl_context *ctx,
-                     const struct gl_client_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,
+                     struct gl_transform_feedback_object *tfb_vertcount,
+                     struct gl_buffer_object *indirect)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
+       const struct gl_client_array **arrays = ctx->Array._DrawArrays;
 
        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);
@@ -399,3 +482,47 @@ TAG(vbo_render_prims)(struct gl_context *ctx,
 
        vbo_deinit_arrays(ctx, ib, arrays);
 }
+
+/* VBO rendering entry points. */
+
+static void
+TAG(vbo_check_render_prims)(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,
+                           struct gl_transform_feedback_object *tfb_vertcount,
+                           struct gl_buffer_object *indirect)
+{
+       struct nouveau_context *nctx = to_nouveau_context(ctx);
+
+       nouveau_validate_framebuffer(ctx);
+
+       if (nctx->fallback == HWTNL)
+               TAG(vbo_render_prims)(ctx, prims, nr_prims, ib,
+                                     index_bounds_valid, min_index, max_index,
+                                     tfb_vertcount, indirect);
+
+       if (nctx->fallback == SWTNL)
+               _tnl_draw_prims(ctx, prims, nr_prims, ib,
+                               index_bounds_valid, min_index, max_index,
+                               tfb_vertcount, indirect);
+}
+
+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;
+
+       vbo_set_draw_func(ctx, TAG(vbo_check_render_prims));
+       vbo_use_buffer_objects(ctx);
+}
+
+void
+TAG(vbo_destroy)(struct gl_context *ctx)
+{
+}