dri/nouveau: Use a macro to iterate over the bound vertex attributes.
authorFrancisco Jerez <currojerez@riseup.net>
Fri, 29 Oct 2010 19:29:15 +0000 (21:29 +0200)
committerFrancisco Jerez <currojerez@riseup.net>
Sun, 31 Oct 2010 00:45:38 +0000 (02:45 +0200)
src/mesa/drivers/dri/nouveau/nouveau_render.h
src/mesa/drivers/dri/nouveau/nouveau_swtnl_t.c
src/mesa/drivers/dri/nouveau/nouveau_vbo_t.c
src/mesa/drivers/dri/nouveau/nv10_render.c
src/mesa/drivers/dri/nouveau/nv20_render.c

index a9e8e90faf76c5bfb3fa77b8f3df5853b2266181..498c7e4acff2127cb8d43dff726e72057695b78d 100644 (file)
@@ -84,4 +84,11 @@ struct nouveau_render_state {
 
 #define to_render_state(ctx) (&to_nouveau_context(ctx)->render)
 
+#define FOR_EACH_ATTR(render, i, attr)                                 \
+       for (i = 0; attr = (render)->map[i], i < NUM_VERTEX_ATTRS; i++)
+
+#define FOR_EACH_BOUND_ATTR(render, i, attr)                           \
+       for (i = 0; attr = (render)->map[i], i < render->attr_count; i++) \
+               if (attr >= 0)
+
 #endif
index db69e562e796d4d4f8e625eea1a27038ff511479..9cf963a9335cf49a1f07faf3dac6a75f5b427d09 100644 (file)
@@ -107,7 +107,7 @@ swtnl_choose_attrs(struct gl_context *ctx)
        TNLcontext *tnl = TNL_CONTEXT(ctx);
        struct tnl_clipspace *vtx = &tnl->clipspace;
        static struct tnl_attr_map map[NUM_VERTEX_ATTRS];
-       int fields, i, n = 0;
+       int fields, attr, i, n = 0;
 
        render->mode = VBO;
        render->attr_count = NUM_VERTEX_ATTRS;
@@ -143,13 +143,8 @@ swtnl_choose_attrs(struct gl_context *ctx)
 
        _tnl_install_attrs(ctx, map, n, NULL, 0);
 
-       for (i = 0; i < vtx->attr_count; i++) {
-               struct tnl_clipspace_attr *ta = &vtx->attr[i];
-               struct nouveau_array_state *a = &render->attrs[ta->attrib];
-
-               a->stride = vtx->vertex_size;
-               a->offset = ta->vertoffset;
-       }
+       FOR_EACH_BOUND_ATTR(render, i, attr)
+               render->attrs[attr].stride = vtx->vertex_size;
 
        TAG(render_set_format)(ctx);
 }
@@ -187,15 +182,11 @@ static void
 swtnl_unbind_vertices(struct gl_context *ctx)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
-       int i;
+       int i, attr;
 
-       for (i = 0; i < render->attr_count; i++) {
-               int *attr = &render->map[i];
-
-               if (*attr >= 0) {
-                       nouveau_bo_ref(NULL, &render->attrs[*attr].bo);
-                       *attr = -1;
-               }
+       FOR_EACH_BOUND_ATTR(render, i, attr) {
+               nouveau_bo_ref(NULL, &render->attrs[attr].bo);
+               render->map[i] = -1;
        }
 
        render->attr_count = 0;
index 4565fcc353b1cb90f1b84b6f03466c3d7db3f010..c93f31819d456acf6023110b3cafe013ceda8f37 100644 (file)
@@ -102,44 +102,39 @@ vbo_init_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
                const struct gl_client_array **arrays)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
-       int i;
+       GLboolean imm = (render->mode == IMM);
+       int i, attr;
 
        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];
+       FOR_EACH_BOUND_ATTR(render, i, attr) {
+               const struct gl_client_array *array = arrays[attr];
 
-                       vbo_init_array(&render->attrs[attr], attr,
-                                      get_array_stride(ctx, array),
-                                      array->Size, array->Type,
-                                      array->BufferObj, array->Ptr,
-                                      render->mode == IMM);
-               }
+               vbo_init_array(&render->attrs[attr], attr,
+                              get_array_stride(ctx, array),
+                              array->Size, array->Type,
+                              array->BufferObj,
+                              array->Ptr, imm);
        }
 }
 
 static void
 vbo_deinit_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
-               const struct gl_client_array **arrays)
+                 const struct gl_client_array **arrays)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
-       int i;
+       int i, attr;
 
        if (ib)
                vbo_deinit_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_state *a = &render->attrs[attr];
 
-               if (*attr >= 0) {
-                       vbo_deinit_array(&render->attrs[*attr]);
-                       *attr = -1;
-               }
+               vbo_deinit_array(a);
+               render->map[i] = -1;
        }
 
        render->attr_count = 0;
@@ -164,11 +159,6 @@ vbo_choose_render_mode(struct gl_context *ctx, const struct gl_client_array **ar
                        }
                }
        }
-
-       if (render->mode == VBO)
-               render->attr_count = NUM_VERTEX_ATTRS;
-       else
-               render->attr_count = 0;
 }
 
 static void
@@ -199,10 +189,13 @@ vbo_emit_attr(struct gl_context *ctx, const struct gl_client_array **arrays, int
                if (render->mode == VBO) {
                        render->map[info->vbo_index] = attr;
                        render->vertex_size += array->_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;
                }
+
        }
 }
 
@@ -216,6 +209,7 @@ vbo_choose_attrs(struct gl_context *ctx, const struct gl_client_array **arrays)
 
        /* 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)
@@ -254,17 +248,13 @@ static int
 get_max_client_stride(struct gl_context *ctx, const struct gl_client_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];
+       FOR_EACH_BOUND_ATTR(render, i, attr) {
+               const struct gl_client_array *a = arrays[attr];
 
-               if (attr >= 0) {
-                       const struct gl_client_array *a = arrays[attr];
-
-                       if (!_mesa_is_bufferobj(a->BufferObj))
-                               s = MAX2(s, get_array_stride(ctx, a));
-               }
+               if (!_mesa_is_bufferobj(a->BufferObj))
+                       s = MAX2(s, get_array_stride(ctx, a));
        }
 
        return s;
@@ -320,33 +310,29 @@ vbo_bind_vertices(struct gl_context *ctx, const struct gl_client_array **arrays,
                  GLint basevertex, GLuint min_index, GLuint max_index)
 {
        struct nouveau_render_state *render = to_render_state(ctx);
-       int i;
+       int i, attr;
 
-       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) {
-                               /* 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);
-
-                               /* 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);
-                       }
+       FOR_EACH_BOUND_ATTR(render, i, attr) {
+               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) {
+                       /* 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);
+
+                       /* 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);
                }
        }
 
@@ -404,7 +390,7 @@ vbo_draw_imm(struct gl_context *ctx, const struct gl_client_array **arrays,
        struct nouveau_render_state *render = to_render_state(ctx);
        struct nouveau_channel *chan = context_chan(ctx);
        extract_u_t extract = ib ? render->ib.extract_u : extract_id;
-       int i, j, k;
+       int i, j, k, attr;
        RENDER_LOCALS(ctx);
 
        for (i = 0; i < nr_prims; i++) {
@@ -421,9 +407,8 @@ vbo_draw_imm(struct gl_context *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();
index a03ace35366d89b5b844e0a9af0cb58e76109ec8..3daec613af5c3a21280f7690105bc78ea14e06f8 100644 (file)
@@ -111,11 +111,9 @@ nv10_render_set_format(struct gl_context *ctx)
        struct nouveau_render_state *render = to_render_state(ctx);
        struct nouveau_channel *chan = context_chan(ctx);
        struct nouveau_grobj *celsius = context_eng3d(ctx);
-       int i, hw_format;
-
-       for (i = 0; i < NUM_VERTEX_ATTRS; i++) {
-               int attr = render->map[i];
+       int i, attr, hw_format;
 
+       FOR_EACH_ATTR(render, i, attr) {
                if (attr >= 0) {
                        struct nouveau_array_state *a = &render->attrs[attr];
 
@@ -142,19 +140,15 @@ nv10_render_bind_vertices(struct gl_context *ctx)
        struct nouveau_bo_context *bctx = context_bctx(ctx, VERTEX);
        struct nouveau_channel *chan = context_chan(ctx);
        struct nouveau_grobj *celsius = context_eng3d(ctx);
-       int i;
+       int i, attr;
 
-       for (i = 0; i < NUM_VERTEX_ATTRS; i++) {
-               int attr = render->map[i];
+       FOR_EACH_BOUND_ATTR(render, i, attr) {
+               struct nouveau_array_state *a = &render->attrs[attr];
 
-               if (attr >= 0) {
-                       struct nouveau_array_state *a = &render->attrs[attr];
-
-                       nouveau_bo_markl(bctx, celsius,
-                                        NV10TCL_VTXBUF_ADDRESS(i),
-                                        a->bo, a->offset,
-                                        NOUVEAU_BO_GART | NOUVEAU_BO_RD);
-               }
+               nouveau_bo_markl(bctx, celsius,
+                                NV10TCL_VTXBUF_ADDRESS(i),
+                                a->bo, a->offset,
+                                NOUVEAU_BO_GART | NOUVEAU_BO_RD);
        }
 
        BEGIN_RING(chan, celsius, NV10TCL_VERTEX_ARRAY_VALIDATE, 1);
index 6b66854462767db35433ac523a6aed7a06f31260..de62dd5137e220b6c06187157d0b88bdd07ae652 100644 (file)
@@ -135,11 +135,9 @@ nv20_render_set_format(struct gl_context *ctx)
        struct nouveau_render_state *render = to_render_state(ctx);
        struct nouveau_channel *chan = context_chan(ctx);
        struct nouveau_grobj *kelvin = context_eng3d(ctx);
-       int i, hw_format;
-
-       for (i = 0; i < NUM_VERTEX_ATTRS; i++) {
-               int attr = render->map[i];
+       int i, attr, hw_format;
 
+       FOR_EACH_ATTR(render, i, attr) {
                if (attr >= 0) {
                        struct nouveau_array_state *a = &render->attrs[attr];
 
@@ -164,21 +162,17 @@ nv20_render_bind_vertices(struct gl_context *ctx)
        struct nouveau_bo_context *bctx = context_bctx(ctx, VERTEX);
        struct nouveau_channel *chan = context_chan(ctx);
        struct nouveau_grobj *kelvin = context_eng3d(ctx);
-       int i;
+       int i, attr;
 
-       for (i = 0; i < NUM_VERTEX_ATTRS; i++) {
-               int attr = render->map[i];
+       FOR_EACH_BOUND_ATTR(render, i, attr) {
+               struct nouveau_array_state *a = &render->attrs[attr];
 
-               if (attr >= 0) {
-                       struct nouveau_array_state *a = &render->attrs[attr];
-
-                       nouveau_bo_mark(bctx, kelvin,
-                                       NV20TCL_VTXBUF_ADDRESS(i),
-                                       a->bo, a->offset, 0,
-                                       0, NV20TCL_VTXBUF_ADDRESS_DMA1,
-                                       NOUVEAU_BO_LOW | NOUVEAU_BO_OR |
-                                       NOUVEAU_BO_GART | NOUVEAU_BO_RD);
-               }
+               nouveau_bo_mark(bctx, kelvin,
+                               NV20TCL_VTXBUF_ADDRESS(i),
+                               a->bo, a->offset, 0,
+                               0, NV20TCL_VTXBUF_ADDRESS_DMA1,
+                               NOUVEAU_BO_LOW | NOUVEAU_BO_OR |
+                               NOUVEAU_BO_GART | NOUVEAU_BO_RD);
        }
 
        BEGIN_RING(chan, kelvin, NV20TCL_VTX_CACHE_INVALIDATE, 1);