lima/gpir: Optimize conditional break/continue
[mesa.git] / src / gallium / drivers / lima / lima_draw.c
index 0b1e1f763c8d2ca26f637ec95fc227707cce028c..47f6d2bfbf590704d52f439df5d2cb3e3d5c7c02 100644 (file)
 #include "lima_resource.h"
 #include "lima_program.h"
 #include "lima_bo.h"
-#include "lima_submit.h"
+#include "lima_job.h"
 #include "lima_texture.h"
 #include "lima_util.h"
 #include "lima_gpu.h"
 
+#include "pan_minmax_cache.h"
+
 #include <drm-uapi/lima_drm.h>
 
 static bool
@@ -60,30 +62,30 @@ lima_is_scissor_zero(struct lima_context *ctx)
 }
 
 static void
-lima_update_submit_wb(struct lima_context *ctx, unsigned buffers)
+lima_update_job_wb(struct lima_context *ctx, unsigned buffers)
 {
-   struct lima_submit *submit = lima_submit_get(ctx);
+   struct lima_job *job = lima_job_get(ctx);
    struct lima_context_framebuffer *fb = &ctx->framebuffer;
 
-   /* add to submit when the buffer is dirty and resolve is clear (not added before) */
+   /* add to job when the buffer is dirty and resolve is clear (not added before) */
    if (fb->base.nr_cbufs && (buffers & PIPE_CLEAR_COLOR0) &&
-       !(submit->resolve & PIPE_CLEAR_COLOR0)) {
+       !(job->resolve & PIPE_CLEAR_COLOR0)) {
       struct lima_resource *res = lima_resource(fb->base.cbufs[0]->texture);
-      lima_flush_submit_accessing_bo(ctx, res->bo, true);
-      _mesa_hash_table_insert(ctx->write_submits, &res->base, submit);
-      lima_submit_add_bo(submit, LIMA_PIPE_PP, res->bo, LIMA_SUBMIT_BO_WRITE);
+      lima_flush_job_accessing_bo(ctx, res->bo, true);
+      _mesa_hash_table_insert(ctx->write_jobs, &res->base, job);
+      lima_job_add_bo(job, LIMA_PIPE_PP, res->bo, LIMA_SUBMIT_BO_WRITE);
    }
 
-   /* add to submit when the buffer is dirty and resolve is clear (not added before) */
+   /* add to job when the buffer is dirty and resolve is clear (not added before) */
    if (fb->base.zsbuf && (buffers & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) &&
-       !(submit->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
+       !(job->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
       struct lima_resource *res = lima_resource(fb->base.zsbuf->texture);
-      lima_flush_submit_accessing_bo(ctx, res->bo, true);
-      _mesa_hash_table_insert(ctx->write_submits, &res->base, submit);
-      lima_submit_add_bo(submit, LIMA_PIPE_PP, res->bo, LIMA_SUBMIT_BO_WRITE);
+      lima_flush_job_accessing_bo(ctx, res->bo, true);
+      _mesa_hash_table_insert(ctx->write_jobs, &res->base, job);
+      lima_job_add_bo(job, LIMA_PIPE_PP, res->bo, LIMA_SUBMIT_BO_WRITE);
    }
 
-   submit->resolve |= buffers;
+   job->resolve |= buffers;
 }
 
 static void
@@ -102,10 +104,16 @@ lima_clear(struct pipe_context *pctx, unsigned buffers,
            const union pipe_color_union *color, double depth, unsigned stencil)
 {
    struct lima_context *ctx = lima_context(pctx);
+   struct lima_job *job = lima_job_get(ctx);
 
-   lima_flush(ctx);
+   /* flush if this job already contains any draw, otherwise multi clear can be
+    * combined into a single job */
+   if (lima_job_has_draw_pending(job)) {
+      lima_do_job(job);
+      job = lima_job_get(ctx);
+   }
 
-   lima_update_submit_wb(ctx, buffers);
+   lima_update_job_wb(ctx, buffers);
 
    /* no need to reload if cleared */
    if (ctx->framebuffer.base.nr_cbufs && (buffers & PIPE_CLEAR_COLOR0)) {
@@ -113,8 +121,7 @@ lima_clear(struct pipe_context *pctx, unsigned buffers,
       surf->reload = false;
    }
 
-   struct lima_submit *submit = lima_submit_get(ctx);
-   struct lima_submit_clear *clear = &submit->clear;
+   struct lima_job_clear *clear = &job->clear;
    clear->buffers = buffers;
 
    if (buffers & PIPE_CLEAR_COLOR0) {
@@ -139,7 +146,7 @@ lima_clear(struct pipe_context *pctx, unsigned buffers,
 
    ctx->dirty |= LIMA_CONTEXT_DIRTY_CLEAR;
 
-   lima_damage_rect_union(&submit->damage_rect,
+   lima_damage_rect_union(&job->damage_rect,
                           0, ctx->framebuffer.base.width,
                           0, ctx->framebuffer.base.height);
 }
@@ -208,9 +215,9 @@ lima_pipe_format_to_attrib_type(enum pipe_format format)
 static void
 lima_pack_vs_cmd(struct lima_context *ctx, const struct pipe_draw_info *info)
 {
-   struct lima_submit *submit = lima_submit_get(ctx);
+   struct lima_job *job = lima_job_get(ctx);
 
-   VS_CMD_BEGIN(&submit->vs_cmd_array, 24);
+   VS_CMD_BEGIN(&job->vs_cmd_array, 24);
 
    if (!info->index_size) {
       VS_CMD_ARRAYS_SEMAPHORE_BEGIN_1();
@@ -260,8 +267,8 @@ lima_pack_plbu_cmd(struct lima_context *ctx, const struct pipe_draw_info *info)
    if (lima_is_scissor_zero(ctx))
       return;
 
-   struct lima_submit *submit = lima_submit_get(ctx);
-   PLBU_CMD_BEGIN(&submit->plbu_cmd_array, 32);
+   struct lima_job *job = lima_job_get(ctx);
+   PLBU_CMD_BEGIN(&job->plbu_cmd_array, 32);
 
    PLBU_CMD_VIEWPORT_LEFT(fui(ctx->viewport.left));
    PLBU_CMD_VIEWPORT_RIGHT(fui(ctx->viewport.right));
@@ -320,7 +327,7 @@ lima_pack_plbu_cmd(struct lima_context *ctx, const struct pipe_draw_info *info)
    maxy = MIN2(maxy, ctx->viewport.top);
 
    PLBU_CMD_SCISSORS(minx, maxx, miny, maxy);
-   lima_damage_rect_union(&submit->damage_rect, minx, maxx, miny, maxy);
+   lima_damage_rect_union(&job->damage_rect, minx, maxx, miny, maxy);
 
    PLBU_CMD_UNKNOWN1();
 
@@ -743,22 +750,22 @@ lima_pack_render_state(struct lima_context *ctx, const struct pipe_draw_info *in
       render->varyings_address = 0x00000000;
    }
 
-   struct lima_submit *submit = lima_submit_get(ctx);
+   struct lima_job *job = lima_job_get(ctx);
 
    lima_dump_command_stream_print(
-      submit->dump, render, sizeof(*render),
+      job->dump, render, sizeof(*render),
       false, "add render state at va %x\n",
       lima_ctx_buff_va(ctx, lima_ctx_buff_pp_plb_rsw));
 
    lima_dump_rsw_command_stream_print(
-      submit->dump, render, sizeof(*render),
+      job->dump, render, sizeof(*render),
       lima_ctx_buff_va(ctx, lima_ctx_buff_pp_plb_rsw));
 }
 
 static void
 lima_update_gp_attribute_info(struct lima_context *ctx, const struct pipe_draw_info *info)
 {
-   struct lima_submit *submit = lima_submit_get(ctx);
+   struct lima_job *job = lima_job_get(ctx);
    struct lima_vertex_element_state *ve = ctx->vertex_elements;
    struct lima_context_vertex_buffer *vb = &ctx->vertex_buffers;
 
@@ -776,7 +783,7 @@ lima_update_gp_attribute_info(struct lima_context *ctx, const struct pipe_draw_i
       struct pipe_vertex_buffer *pvb = vb->vb + pve->vertex_buffer_index;
       struct lima_resource *res = lima_resource(pvb->buffer.resource);
 
-      lima_submit_add_bo(submit, LIMA_PIPE_GP, res->bo, LIMA_SUBMIT_BO_READ);
+      lima_job_add_bo(job, LIMA_PIPE_GP, res->bo, LIMA_SUBMIT_BO_READ);
 
       unsigned start = info->index_size ? (ctx->min_index + info->index_bias) : info->start;
       attribute[n++] = res->bo->va + pvb->buffer_offset + pve->src_offset
@@ -787,7 +794,7 @@ lima_update_gp_attribute_info(struct lima_context *ctx, const struct pipe_draw_i
    }
 
    lima_dump_command_stream_print(
-      submit->dump, attribute, n * 4, false, "update attribute info at va %x\n",
+      job->dump, attribute, n * 4, false, "update attribute info at va %x\n",
       lima_ctx_buff_va(ctx, lima_ctx_buff_gp_attribute_info));
 }
 
@@ -816,10 +823,23 @@ lima_update_gp_uniform(struct lima_context *ctx)
       memcpy(vs_const_buff + vs->uniform_pending_offset + 32,
              vs->constant, vs->constant_size);
 
-   struct lima_submit *submit = lima_submit_get(ctx);
+   struct lima_job *job = lima_job_get(ctx);
+
+   if (lima_debug & LIMA_DEBUG_GP) {
+      float *vs_const_buff_f = vs_const_buff;
+      printf("gp uniforms:\n");
+      for (int i = 0; i < (size / sizeof(float)); i++) {
+         if ((i % 4) == 0)
+            printf("%4d:", i / 4);
+         printf(" %8.4f", vs_const_buff_f[i]);
+         if ((i % 4) == 3)
+            printf("\n");
+      }
+      printf("\n");
+   }
 
    lima_dump_command_stream_print(
-      submit->dump, vs_const_buff, size, true,
+      job->dump, vs_const_buff, size, true,
       "update gp uniform at va %x\n",
       lima_ctx_buff_va(ctx, lima_ctx_buff_gp_uniform));
 }
@@ -845,21 +865,21 @@ lima_update_pp_uniform(struct lima_context *ctx)
 
    *array = lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform);
 
-   struct lima_submit *submit = lima_submit_get(ctx);
+   struct lima_job *job = lima_job_get(ctx);
 
    lima_dump_command_stream_print(
-      submit->dump, fp16_const_buff, const_buff_size * 2,
+      job->dump, fp16_const_buff, const_buff_size * 2,
       false, "add pp uniform data at va %x\n",
       lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform));
    lima_dump_command_stream_print(
-      submit->dump, array, 4, false, "add pp uniform info at va %x\n",
+      job->dump, array, 4, false, "add pp uniform info at va %x\n",
       lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform_array));
 }
 
 static void
 lima_update_varying(struct lima_context *ctx, const struct pipe_draw_info *info)
 {
-   struct lima_submit *submit = lima_submit_get(ctx);
+   struct lima_job *job = lima_job_get(ctx);
    struct lima_screen *screen = lima_screen(ctx->base.screen);
    struct lima_vs_shader_state *vs = ctx->vs;
    uint32_t gp_output_size;
@@ -911,8 +931,8 @@ lima_update_varying(struct lima_context *ctx, const struct pipe_draw_info *info)
     */
    ctx->gp_output = lima_bo_create(screen, gp_output_size, 0);
    assert(ctx->gp_output);
-   lima_submit_add_bo(submit, LIMA_PIPE_GP, ctx->gp_output, LIMA_SUBMIT_BO_WRITE);
-   lima_submit_add_bo(submit, LIMA_PIPE_PP, ctx->gp_output, LIMA_SUBMIT_BO_READ);
+   lima_job_add_bo(job, LIMA_PIPE_GP, ctx->gp_output, LIMA_SUBMIT_BO_WRITE);
+   lima_job_add_bo(job, LIMA_PIPE_PP, ctx->gp_output, LIMA_SUBMIT_BO_READ);
 
    for (int i = 0; i < vs->num_outputs; i++) {
       struct lima_varying_info *v = vs->varying + i;
@@ -935,7 +955,7 @@ lima_update_varying(struct lima_context *ctx, const struct pipe_draw_info *info)
    }
 
    lima_dump_command_stream_print(
-      submit->dump, varying, n * 4, false, "update varying info at va %x\n",
+      job->dump, varying, n * 4, false, "update varying info at va %x\n",
       lima_ctx_buff_va(ctx, lima_ctx_buff_gp_varying_info));
 }
 
@@ -958,7 +978,7 @@ lima_draw_vbo_update(struct pipe_context *pctx,
    if (fb->base.nr_cbufs)
       buffers |= PIPE_CLEAR_COLOR0;
 
-   lima_update_submit_wb(ctx, buffers);
+   lima_update_job_wb(ctx, buffers);
 
    lima_update_gp_attribute_info(ctx, info);
 
@@ -988,7 +1008,7 @@ lima_draw_vbo_update(struct pipe_context *pctx,
    lima_pack_plbu_cmd(ctx, info);
 
    if (ctx->gp_output) {
-      lima_bo_unreference(ctx->gp_output); /* held by submit */
+      lima_bo_unreference(ctx->gp_output); /* held by job */
       ctx->gp_output = NULL;
    }
 
@@ -1000,16 +1020,16 @@ lima_draw_vbo_indexed(struct pipe_context *pctx,
                       const struct pipe_draw_info *info)
 {
    struct lima_context *ctx = lima_context(pctx);
-   struct lima_submit *submit = lima_submit_get(ctx);
+   struct lima_job *job = lima_job_get(ctx);
    struct pipe_resource *indexbuf = NULL;
+   bool needs_indices = true;
 
    /* Mali Utgard GPU always need min/max index info for index draw,
     * compute it if upper layer does not do for us */
-   if (info->max_index == ~0u)
-      u_vbuf_get_minmax_index(pctx, info, &ctx->min_index, &ctx->max_index);
-   else {
+   if (info->max_index != ~0u) {
       ctx->min_index = info->min_index;
       ctx->max_index = info->max_index;
+      needs_indices = false;
    }
 
    if (info->has_user_indices) {
@@ -1019,10 +1039,19 @@ lima_draw_vbo_indexed(struct pipe_context *pctx,
    else {
       ctx->index_res = lima_resource(info->index.resource);
       ctx->index_offset = 0;
+      needs_indices = !panfrost_minmax_cache_get(ctx->index_res->index_cache, info->start,
+                                                 info->count, &ctx->min_index, &ctx->max_index);
+   }
+
+   if (needs_indices) {
+      u_vbuf_get_minmax_index(pctx, info, &ctx->min_index, &ctx->max_index);
+      if (!info->has_user_indices)
+         panfrost_minmax_cache_add(ctx->index_res->index_cache, info->start, info->count,
+                                   ctx->min_index, ctx->max_index);
    }
 
-   lima_submit_add_bo(submit, LIMA_PIPE_GP, ctx->index_res->bo, LIMA_SUBMIT_BO_READ);
-   lima_submit_add_bo(submit, LIMA_PIPE_PP, ctx->index_res->bo, LIMA_SUBMIT_BO_READ);
+   lima_job_add_bo(job, LIMA_PIPE_GP, ctx->index_res->bo, LIMA_SUBMIT_BO_READ);
+   lima_job_add_bo(job, LIMA_PIPE_PP, ctx->index_res->bo, LIMA_SUBMIT_BO_READ);
    lima_draw_vbo_update(pctx, info);
 
    if (indexbuf)
@@ -1076,18 +1105,18 @@ lima_draw_vbo(struct pipe_context *pctx,
    if (!lima_update_vs_state(ctx) || !lima_update_fs_state(ctx))
       return;
 
-   struct lima_submit *submit = lima_submit_get(ctx);
+   struct lima_job *job = lima_job_get(ctx);
 
    lima_dump_command_stream_print(
-      submit->dump, ctx->vs->bo->map, ctx->vs->shader_size, false,
+      job->dump, ctx->vs->bo->map, ctx->vs->shader_size, false,
       "add vs at va %x\n", ctx->vs->bo->va);
 
    lima_dump_command_stream_print(
-      submit->dump, ctx->fs->bo->map, ctx->fs->shader_size, false,
+      job->dump, ctx->fs->bo->map, ctx->fs->shader_size, false,
       "add fs at va %x\n", ctx->fs->bo->va);
 
-   lima_submit_add_bo(submit, LIMA_PIPE_GP, ctx->vs->bo, LIMA_SUBMIT_BO_READ);
-   lima_submit_add_bo(submit, LIMA_PIPE_PP, ctx->fs->bo, LIMA_SUBMIT_BO_READ);
+   lima_job_add_bo(job, LIMA_PIPE_GP, ctx->vs->bo, LIMA_SUBMIT_BO_READ);
+   lima_job_add_bo(job, LIMA_PIPE_PP, ctx->fs->bo, LIMA_SUBMIT_BO_READ);
 
    if (info->index_size)
       lima_draw_vbo_indexed(pctx, info);