r600g: avoid useless shader rebuild at draw call
authorJerome Glisse <jglisse@redhat.com>
Mon, 6 Dec 2010 00:24:03 +0000 (19:24 -0500)
committerJerome Glisse <jglisse@redhat.com>
Mon, 6 Dec 2010 20:50:50 +0000 (15:50 -0500)
Avoid rebuilding constant shader state at each draw call,
factor out spi update that might change at each draw call.
Best would be to update spi only when revealent states
change (likely only flat shading & sprite point).

Signed-off-by: Jerome Glisse <jglisse@redhat.com>
src/gallium/drivers/r600/evergreen_state.c
src/gallium/drivers/r600/r600_pipe.c
src/gallium/drivers/r600/r600_shader.c
src/gallium/drivers/r600/r600_shader.h
src/gallium/drivers/r600/r600_state.c
src/gallium/drivers/r600/r600_state_common.c
src/gallium/drivers/r600/r600_translate.c

index b313d5250129da7d38c2fe5869799e973f64c7f4..feb30f3f25d0d5bbdbdc9ce0a4b566c339e02525 100644 (file)
@@ -1239,6 +1239,7 @@ void evergreen_polygon_offset_update(struct r600_pipe_context *rctx)
                default:
                        return;
                }
+               /* FIXME some of those reg can be computed with cso */
                offset_db_fmt_cntl |= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(depth);
                r600_pipe_state_add_reg(&state,
                                R_028B80_PA_SU_POLY_OFFSET_FRONT_SCALE,
@@ -1259,6 +1260,30 @@ void evergreen_polygon_offset_update(struct r600_pipe_context *rctx)
        }
 }
 
+static void evergreen_spi_update(struct r600_pipe_context *rctx)
+{
+       struct r600_pipe_shader *shader = rctx->ps_shader;
+       struct r600_pipe_state rstate;
+       struct r600_shader *rshader = &shader->shader;
+       unsigned i, tmp;
+
+       rstate.nregs = 0;
+       for (i = 0; i < rshader->ninput; i++) {
+               tmp = S_028644_SEMANTIC(r600_find_vs_semantic_index(&rctx->vs_shader->shader, rshader, i));
+               if (rshader->input[i].name == TGSI_SEMANTIC_COLOR ||
+                               rshader->input[i].name == TGSI_SEMANTIC_BCOLOR ||
+                               rshader->input[i].name == TGSI_SEMANTIC_POSITION) {
+                       tmp |= S_028644_FLAT_SHADE(rctx->flatshade);
+               }
+               if (rshader->input[i].name == TGSI_SEMANTIC_GENERIC &&
+                       rctx->sprite_coord_enable & (1 << rshader->input[i].sid)) {
+                       tmp |= S_028644_PT_SPRITE_TEX(1);
+               }
+               r600_pipe_state_add_reg(&rstate, R_028644_SPI_PS_INPUT_CNTL_0 + i * 4, tmp, 0xFFFFFFFF, NULL);
+       }
+       r600_context_pipe_state_set(&rctx->ctx, &rstate);
+}
+
 void evergreen_vertex_buffer_update(struct r600_pipe_context *rctx)
 {
        struct r600_pipe_state *rstate;
@@ -1417,12 +1442,30 @@ void evergreen_draw(struct pipe_context *ctx, const struct pipe_draw_info *info)
        }
        if (r600_conv_pipe_prim(draw.mode, &prim))
                return;
+       if (unlikely(rctx->ps_shader == NULL)) {
+               R600_ERR("missing vertex shader\n");
+               return;
+       }
+       if (unlikely(rctx->vs_shader == NULL)) {
+               R600_ERR("missing vertex shader\n");
+               return;
+       }
+       /* there should be enough input */
+       if (rctx->vertex_elements->count < rctx->vs_shader->shader.bc.nresource) {
+               R600_ERR("%d resources provided, expecting %d\n",
+                       rctx->vertex_elements->count, rctx->vs_shader->shader.bc.nresource);
+               return;
+       }
 
+#if 0
        /* rebuild vertex shader if input format changed */
        if (r600_pipe_shader_update(&rctx->context, rctx->vs_shader))
                return;
        if (r600_pipe_shader_update(&rctx->context, rctx->ps_shader))
                return;
+#endif
+
+       evergreen_spi_update(rctx);
 
 #if 0
        for (i = 0 ; i < rctx->vertex_elements->count; i++) {
@@ -1506,11 +1549,9 @@ void evergreen_pipe_shader_ps(struct pipe_context *ctx, struct r600_pipe_shader
        boolean have_linear = FALSE, have_centroid = FALSE, have_perspective = FALSE;
        unsigned spi_baryc_cntl;
 
-       /* clear previous register */
        rstate->nregs = 0;
 
        for (i = 0; i < rshader->ninput; i++) {
-               tmp = S_028644_SEMANTIC(r600_find_vs_semantic_index(&rctx->vs_shader->shader, rshader, i));
                /* evergreen NUM_INTERP only contains values interpolated into the LDS,
                   POSITION goes via GPRs from the SC so isn't counted */
                if (rshader->input[i].name == TGSI_SEMANTIC_POSITION)
@@ -1528,16 +1569,6 @@ void evergreen_pipe_shader_ps(struct pipe_context *ctx, struct r600_pipe_shader
                        if (rshader->input[i].centroid)
                                have_centroid = TRUE;
                }
-               if (rshader->input[i].name == TGSI_SEMANTIC_COLOR ||
-                   rshader->input[i].name == TGSI_SEMANTIC_BCOLOR ||
-                   rshader->input[i].name == TGSI_SEMANTIC_POSITION) {
-                       tmp |= S_028644_FLAT_SHADE(rshader->flat_shade);
-               }
-               if (rshader->input[i].name == TGSI_SEMANTIC_GENERIC &&
-                       rctx->sprite_coord_enable & (1 << rshader->input[i].sid)) {
-                       tmp |= S_028644_PT_SPRITE_TEX(1);
-               }
-               r600_pipe_state_add_reg(rstate, R_028644_SPI_PS_INPUT_CNTL_0 + i * 4, tmp, 0xFFFFFFFF, NULL);
        }
        for (i = 0; i < rshader->noutput; i++) {
                if (rshader->output[i].name == TGSI_SEMANTIC_POSITION)
index ea57fba8e470833b020da2af33415f5c32b0b61d..6842571044d2f68ff69bf6469d4cf699cf8bff1d 100644 (file)
@@ -79,6 +79,8 @@ static void r600_destroy_context(struct pipe_context *context)
 
        rctx->context.delete_depth_stencil_alpha_state(&rctx->context, rctx->custom_dsa_flush);
 
+       r600_end_vertex_translate(rctx);
+
        r600_context_fini(&rctx->ctx);
 
        util_blitter_destroy(rctx->blitter);
@@ -90,8 +92,6 @@ static void r600_destroy_context(struct pipe_context *context)
        u_upload_destroy(rctx->upload_vb);
        u_upload_destroy(rctx->upload_ib);
 
-       r600_end_vertex_translate(rctx);
-
        if (rctx->tran.translate_cache)
                translate_cache_destroy(rctx->tran.translate_cache);
 
index e40cd1dbcf13e7eb1268f47d05d328cab4300a58..ab401e0f69b907ea6e1cbcafc6a68e1de7d1b377 100644 (file)
@@ -44,6 +44,9 @@ static void r600_pipe_shader_vs(struct pipe_context *ctx, struct r600_pipe_shade
        rstate->nregs = 0;
 
        /* so far never got proper semantic id from tgsi */
+       /* FIXME better to move this in config things so they get emited
+        * only one time per cs
+        */
        for (i = 0; i < 10; i++) {
                spi_vs_out_id[i] = 0;
        }
@@ -112,31 +115,15 @@ static void r600_pipe_shader_ps(struct pipe_context *ctx, struct r600_pipe_shade
        unsigned i, tmp, exports_ps, num_cout, spi_ps_in_control_0, spi_input_z, spi_ps_in_control_1;
        int pos_index = -1, face_index = -1;
 
-       /* clear previous register */
        rstate->nregs = 0;
 
        for (i = 0; i < rshader->ninput; i++) {
-               tmp = S_028644_SEMANTIC(r600_find_vs_semantic_index(&rctx->vs_shader->shader, rshader, i));
-               if (rshader->input[i].centroid)
-                       tmp |= S_028644_SEL_CENTROID(1);
-               if (rshader->input[i].interpolate == TGSI_INTERPOLATE_LINEAR)
-                       tmp |= S_028644_SEL_LINEAR(1);
-
                if (rshader->input[i].name == TGSI_SEMANTIC_POSITION)
                        pos_index = i;
-               if (rshader->input[i].name == TGSI_SEMANTIC_COLOR ||
-                   rshader->input[i].name == TGSI_SEMANTIC_BCOLOR ||
-                   rshader->input[i].name == TGSI_SEMANTIC_POSITION) {
-                       tmp |= S_028644_FLAT_SHADE(rshader->flat_shade);
-               }
                if (rshader->input[i].name == TGSI_SEMANTIC_FACE)
                        face_index = i;
-               if (rshader->input[i].name == TGSI_SEMANTIC_GENERIC &&
-                       rctx->sprite_coord_enable & (1 << rshader->input[i].sid)) {
-                       tmp |= S_028644_PT_SPRITE_TEX(1);
-               }
-               r600_pipe_state_add_reg(rstate, R_028644_SPI_PS_INPUT_CNTL_0 + i * 4, tmp, 0xFFFFFFFF, NULL);
        }
+
        for (i = 0; i < rshader->noutput; i++) {
                if (rshader->output[i].name == TGSI_SEMANTIC_POSITION)
                        r600_pipe_state_add_reg(rstate,
@@ -238,7 +225,6 @@ static int r600_pipe_shader(struct pipe_context *ctx, struct r600_pipe_shader *s
                r600_bo_unmap(rctx->radeon, shader->bo);
        }
        /* build state */
-       rshader->flat_shade = rctx->flatshade;
        switch (rshader->processor_type) {
        case TGSI_PROCESSOR_VERTEX:
                if (rshader->family >= CHIP_CEDAR) {
@@ -257,7 +243,6 @@ static int r600_pipe_shader(struct pipe_context *ctx, struct r600_pipe_shader *s
        default:
                return -EINVAL;
        }
-       r600_context_pipe_state_set(&rctx->ctx, &shader->rstate);
        return 0;
 }
 
@@ -317,17 +302,6 @@ int r600_pipe_shader_update(struct pipe_context *ctx, struct r600_pipe_shader *s
        struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
        int r;
 
-       if (shader == NULL)
-               return -EINVAL;
-       /* there should be enough input */
-       if (rctx->vertex_elements->count < shader->shader.bc.nresource) {
-               R600_ERR("%d resources provided, expecting %d\n",
-                       rctx->vertex_elements->count, shader->shader.bc.nresource);
-               return -EINVAL;
-       }
-       r = r600_shader_update(ctx, shader);
-       if (r)
-               return r;
        return r600_pipe_shader(ctx, shader);
 }
 
@@ -359,7 +333,7 @@ int r600_pipe_shader_create(struct pipe_context *ctx, struct r600_pipe_shader *s
        }
 //r600_bc_dump(&shader->shader.bc);
 //fprintf(stderr, "______________________________________________________________\n");
-       return 0;
+       return r600_pipe_shader(ctx, shader);
 }
 
 void
index cd108da49153171a564cbce0061c448acbabe9bb..e8742b59a445414f18880e97606f4394a82ae1de 100644 (file)
@@ -38,7 +38,6 @@ struct r600_shader_io {
 struct r600_shader {
        unsigned                processor_type;
        struct r600_bc          bc;
-       boolean                 flat_shade;
        unsigned                ninput;
        unsigned                noutput;
        unsigned                nlds;
index 9b70942eebff20fe50549c27f7369a708f50d43d..2ba15b818aaf460fd7e22d8f362027c10ba41022 100644 (file)
@@ -74,6 +74,7 @@ void r600_polygon_offset_update(struct r600_pipe_context *rctx)
                default:
                        return;
                }
+               /* FIXME some of those reg can be computed with cso */
                offset_db_fmt_cntl |= S_028DF8_POLY_OFFSET_NEG_NUM_DB_BITS(depth);
                r600_pipe_state_add_reg(&state,
                                R_028E00_PA_SU_POLY_OFFSET_FRONT_SCALE,
@@ -94,6 +95,36 @@ void r600_polygon_offset_update(struct r600_pipe_context *rctx)
        }
 }
 
+/* FIXME optimize away spi update when it's not needed */
+static void r600_spi_update(struct r600_pipe_context *rctx)
+{
+       struct r600_pipe_shader *shader = rctx->ps_shader;
+       struct r600_pipe_state rstate;
+       struct r600_shader *rshader = &shader->shader;
+       unsigned i, tmp;
+
+       rstate.nregs = 0;
+       for (i = 0; i < rshader->ninput; i++) {
+               tmp = S_028644_SEMANTIC(r600_find_vs_semantic_index(&rctx->vs_shader->shader, rshader, i));
+               if (rshader->input[i].centroid)
+                       tmp |= S_028644_SEL_CENTROID(1);
+               if (rshader->input[i].interpolate == TGSI_INTERPOLATE_LINEAR)
+                       tmp |= S_028644_SEL_LINEAR(1);
+
+               if (rshader->input[i].name == TGSI_SEMANTIC_COLOR ||
+                   rshader->input[i].name == TGSI_SEMANTIC_BCOLOR ||
+                   rshader->input[i].name == TGSI_SEMANTIC_POSITION) {
+                       tmp |= S_028644_FLAT_SHADE(rctx->flatshade);
+               }
+               if (rshader->input[i].name == TGSI_SEMANTIC_GENERIC &&
+                       rctx->sprite_coord_enable & (1 << rshader->input[i].sid)) {
+                       tmp |= S_028644_PT_SPRITE_TEX(1);
+               }
+               r600_pipe_state_add_reg(&rstate, R_028644_SPI_PS_INPUT_CNTL_0 + i * 4, tmp, 0xFFFFFFFF, NULL);
+       }
+       r600_context_pipe_state_set(&rctx->ctx, &rstate);
+}
+
 void r600_vertex_buffer_update(struct r600_pipe_context *rctx)
 {
        struct r600_pipe_state *rstate;
@@ -202,13 +233,30 @@ static void r600_draw_common(struct r600_drawl *draw)
        }
        if (r600_conv_pipe_prim(draw->mode, &prim))
                return;
+       if (unlikely(rctx->ps_shader == NULL)) {
+               R600_ERR("missing vertex shader\n");
+               return;
+       }
+       if (unlikely(rctx->vs_shader == NULL)) {
+               R600_ERR("missing vertex shader\n");
+               return;
+       }
+       /* there should be enough input */
+       if (rctx->vertex_elements->count < rctx->vs_shader->shader.bc.nresource) {
+               R600_ERR("%d resources provided, expecting %d\n",
+                       rctx->vertex_elements->count, rctx->vs_shader->shader.bc.nresource);
+               return;
+       }
 
-
+#if 0
        /* rebuild vertex shader if input format changed */
        if (r600_pipe_shader_update(&rctx->context, rctx->vs_shader))
                return;
        if (r600_pipe_shader_update(&rctx->context, rctx->ps_shader))
                return;
+#endif
+
+       r600_spi_update(rctx);
 
 #if 0
        for (i = 0 ; i < rctx->vertex_elements->count; i++) {
index 889432732cf3c8b810fc4d6b704708de8c675038..c647e77b3738e2262886a0f15c75d9e9a3ed3e2f 100644 (file)
@@ -269,6 +269,9 @@ void r600_bind_ps_shader(struct pipe_context *ctx, void *state)
 
        /* TODO delete old shader */
        rctx->ps_shader = (struct r600_pipe_shader *)state;
+       if (state) {
+               r600_context_pipe_state_set(&rctx->ctx, &rctx->ps_shader->rstate);
+       }
 }
 
 void r600_bind_vs_shader(struct pipe_context *ctx, void *state)
@@ -277,6 +280,9 @@ void r600_bind_vs_shader(struct pipe_context *ctx, void *state)
 
        /* TODO delete old shader */
        rctx->vs_shader = (struct r600_pipe_shader *)state;
+       if (state) {
+               r600_context_pipe_state_set(&rctx->ctx, &rctx->vs_shader->rstate);
+       }
 }
 
 void r600_delete_ps_shader(struct pipe_context *ctx, void *state)
index d927f53398d37aa1d3ff60324562bce832ce43ab..1c227d3215115319a37b300e8f284c6f2f3ceb95 100644 (file)
@@ -169,6 +169,9 @@ void r600_end_vertex_translate(struct r600_pipe_context *rctx)
 {
        struct pipe_context *pipe = &rctx->context;
 
+       if (rctx->tran.new_velems == NULL) {
+               return;
+       }
        /* Restore vertex elements. */
        if (rctx->vertex_elements == rctx->tran.new_velems) {
                pipe->bind_vertex_elements_state(pipe, NULL);