radv: Only save the descriptor set if we have one.
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_vbo_t.c
index c85acec12680a2f2889185c4896170d76f9083c3..f037bd2015d1e298d9658c107146a52e98c58a7d 100644 (file)
@@ -30,6 +30,7 @@
 
 #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
  * structures. */
 
 static int
-get_array_stride(struct gl_context *ctx, const struct gl_client_array *a)
+get_array_stride(struct gl_context *ctx, const struct tnl_vertex_array *a)
 {
        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(a->BufferObj))
+       if (render->mode == VBO && !_mesa_is_bufferobj(binding->BufferObj)) {
+               const struct gl_array_attributes *attrib = a->VertexAttrib;
                /* Pack client buffers. */
-               return align(_mesa_sizeof_type(a->Type) * a->Size, 4);
-       else
-               return a->StrideB;
+               return align(attrib->Format._ElementSize, 4);
+       } else {
+               return binding->Stride;
+       }
 }
 
 static void
 vbo_init_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
-               const struct gl_client_array **arrays)
+               const struct tnl_vertex_array *arrays)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
        GLboolean imm = (render->mode == IMM);
        int i, attr;
 
-       if (ib)
-               nouveau_init_array(&render->ib, 0, 0, ib->count, ib->type,
+       if (ib) {
+               GLenum ib_type;
+
+               if (ib->index_size == 4)
+                       ib_type = GL_UNSIGNED_INT;
+               else if (ib->index_size == 2)
+                       ib_type = GL_UNSIGNED_SHORT;
+               else
+                       ib_type = GL_UNSIGNED_BYTE;
+
+               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 gl_client_array *array = arrays[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),
-                                  array->Size, array->Type,
-                                  imm ? array->BufferObj : NULL,
-                                  array->Ptr, imm, ctx);
+                                  attrib->Format.Size, attrib->Format.Type,
+                                  imm ? binding->BufferObj : NULL,
+                                  p, imm, ctx);
        }
 }
 
 static void
 vbo_deinit_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
-                 const struct gl_client_array **arrays)
+                 const struct tnl_vertex_array *arrays)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
        int i, attr;
@@ -100,7 +118,7 @@ vbo_deinit_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
 /* Make some rendering decisions from the GL context. */
 
 static void
-vbo_choose_render_mode(struct gl_context *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;
@@ -108,8 +126,8 @@ vbo_choose_render_mode(struct gl_context *ctx, const struct gl_client_array **ar
        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;
                        }
@@ -118,23 +136,26 @@ vbo_choose_render_mode(struct gl_context *ctx, const struct gl_client_array **ar
 }
 
 static void
-vbo_emit_attr(struct gl_context *ctx, const struct gl_client_array **arrays,
+vbo_emit_attr(struct gl_context *ctx, const struct tnl_vertex_array *arrays,
              int attr)
 {
        struct nouveau_pushbuf *push = context_push(ctx);
        struct nouveau_render_state *render = to_render_state(ctx);
-       const struct gl_client_array *array = arrays[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. */
-               nouveau_init_array(a, attr, array->StrideB, array->Size,
-                                  array->Type, array->BufferObj, array->Ptr,
+               nouveau_init_array(a, attr, binding->Stride, attrib->Format.Size,
+                                  attrib->Format.Type, binding->BufferObj, p,
                                   GL_TRUE, ctx);
                EMIT_IMM(ctx, a, 0);
                nouveau_deinit_array(a);
@@ -145,7 +166,7 @@ vbo_emit_attr(struct gl_context *ctx, const struct gl_client_array **arrays,
 
                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 {
@@ -155,10 +176,10 @@ vbo_emit_attr(struct gl_context *ctx, const struct gl_client_array **arrays,
        }
 }
 
-#define MAT(a) (VERT_ATTRIB_GENERIC0 + MAT_ATTRIB_##a)
+#define MAT(a) VERT_ATTRIB_MAT(MAT_ATTRIB_##a)
 
 static void
-vbo_choose_attrs(struct gl_context *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;
@@ -201,15 +222,15 @@ vbo_choose_attrs(struct gl_context *ctx, const struct gl_client_array **arrays)
 }
 
 static int
-get_max_client_stride(struct gl_context *ctx, const struct gl_client_array **arrays)
+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, attr, s = 0;
 
        FOR_EACH_BOUND_ATTR(render, i, attr) {
-               const struct gl_client_array *a = arrays[attr];
+               const struct tnl_vertex_array *a = &arrays[attr];
 
-               if (!_mesa_is_bufferobj(a->BufferObj))
+               if (!_mesa_is_bufferobj(a->BufferBinding->BufferObj))
                        s = MAX2(s, get_array_stride(ctx, a));
        }
 
@@ -218,15 +239,17 @@ 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 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,
                      struct gl_transform_feedback_object *tfb_vertcount,
+                      unsigned stream,
                      struct gl_buffer_object *indirect);
 
 static GLboolean
-vbo_maybe_split(struct gl_context *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)
@@ -254,8 +277,8 @@ vbo_maybe_split(struct gl_context *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, TAG(vbo_render_prims), &limits);
                return GL_TRUE;
        }
 
@@ -286,7 +309,7 @@ check_update_array(struct nouveau_array *a, unsigned offset,
 }
 
 static void
-vbo_bind_vertices(struct gl_context *ctx, const struct gl_client_array **arrays,
+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);
@@ -300,22 +323,26 @@ vbo_bind_vertices(struct gl_context *ctx, const struct gl_client_array **arrays,
        *pdelta = -1;
 
        FOR_EACH_BOUND_ATTR(render, i, attr) {
-               const struct gl_client_array *array = arrays[attr];
-               struct gl_buffer_object *obj = array->BufferObj;
+               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) * array->StrideB;
+               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)array->Ptr;
+                       offset[i] = delta + (intptr_t)p;
 
                } else {
                        int n = max_index - min_index + 1;
                        char *sp = (char *)ADD_POINTERS(
-                               nouveau_bufferobj_sys(obj), array->Ptr) + delta;
+                               nouveau_bufferobj_sys(obj), p) + delta;
                        char *dp  = nouveau_get_scratch(ctx, n * a->stride,
                                                        &bo[i], &offset[i]);
 
@@ -323,7 +350,7 @@ vbo_bind_vertices(struct gl_context *ctx, const struct gl_client_array **arrays,
                         * scratch buffer obj. */
                        for (j = 0; j < n; j++)
                                memcpy(dp + j * a->stride,
-                                      sp + j * array->StrideB,
+                                      sp + j * binding->Stride,
                                       a->stride);
                }
 
@@ -354,7 +381,7 @@ vbo_bind_vertices(struct gl_context *ctx, const struct gl_client_array **arrays,
 }
 
 static void
-vbo_draw_vbo(struct gl_context *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)
@@ -404,7 +431,7 @@ extract_id(struct nouveau_array *a, int i, int j)
 }
 
 static void
-vbo_draw_imm(struct gl_context *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)
@@ -450,15 +477,16 @@ vbo_draw_imm(struct gl_context *ctx, const struct gl_client_array **arrays,
 
 static void
 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,
                      struct gl_transform_feedback_object *tfb_vertcount,
+                      unsigned stream,
                      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_indices(ctx, prims, ib, &min_index, &max_index,
@@ -487,11 +515,13 @@ TAG(vbo_render_prims)(struct gl_context *ctx,
 
 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,
                            struct gl_transform_feedback_object *tfb_vertcount,
+                            unsigned stream,
                            struct gl_buffer_object *indirect)
 {
        struct nouveau_context *nctx = to_nouveau_context(ctx);
@@ -499,14 +529,33 @@ TAG(vbo_check_render_prims)(struct gl_context *ctx,
        nouveau_validate_framebuffer(ctx);
 
        if (nctx->fallback == HWTNL)
-               TAG(vbo_render_prims)(ctx, prims, nr_prims, ib,
+               TAG(vbo_render_prims)(ctx, arrays, prims, nr_prims, ib,
                                      index_bounds_valid, min_index, max_index,
-                                     tfb_vertcount, indirect);
+                                     tfb_vertcount, stream, indirect);
 
        if (nctx->fallback == SWTNL)
-               _tnl_draw_prims(ctx, prims, nr_prims, ib,
+               _tnl_draw_prims(ctx, arrays, prims, nr_prims, ib,
                                index_bounds_valid, min_index, max_index,
-                               tfb_vertcount, indirect);
+                               tfb_vertcount, stream, indirect);
+}
+
+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,
+             struct gl_transform_feedback_object *tfb_vertcount,
+             unsigned stream,
+             struct gl_buffer_object *indirect)
+{
+       /* 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,
+                                   tfb_vertcount, stream, indirect);
 }
 
 void
@@ -518,7 +567,8 @@ TAG(vbo_init)(struct gl_context *ctx)
        for (i = 0; i < VERT_ATTRIB_MAX; i++)
                render->map[i] = -1;
 
-       vbo_set_draw_func(ctx, TAG(vbo_check_render_prims));
+       /* Overwrite our draw function */
+       ctx->Driver.Draw = TAG(vbo_draw);
        vbo_use_buffer_objects(ctx);
 }