freedreno: also mark draw-indirect buffer as read
[mesa.git] / src / gallium / drivers / freedreno / freedreno_draw.c
index f7b132e0ef4cb6e85169e008ead4910212c3cd89..60a1180f24d5b17dc02a4348c0b628788d7f057e 100644 (file)
@@ -54,6 +54,7 @@ resource_written(struct fd_batch *batch, struct pipe_resource *prsc)
 {
        if (!prsc)
                return;
+       fd_resource(prsc)->valid = true;
        fd_batch_resource_used(batch, fd_resource(prsc), true);
 }
 
@@ -64,7 +65,7 @@ fd_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
        struct fd_batch *batch = ctx->batch;
        struct pipe_framebuffer_state *pfb = &batch->framebuffer;
        struct pipe_scissor_state *scissor = fd_context_get_scissor(ctx);
-       unsigned i, prims, buffers = 0;
+       unsigned i, prims, buffers = 0, restore_buffers = 0;
 
        if (!info->count_from_stream_output && !info->indirect &&
            !info->primitive_restart &&
@@ -85,18 +86,26 @@ fd_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
        if (!fd_supported_prim(ctx, info->mode)) {
                if (ctx->streamout.num_targets > 0)
                        debug_error("stream-out with emulated prims");
-               util_primconvert_save_index_buffer(ctx->primconvert, &ctx->indexbuf);
                util_primconvert_save_rasterizer_state(ctx->primconvert, ctx->rasterizer);
                util_primconvert_draw_vbo(ctx->primconvert, info);
                return;
        }
 
        /* Upload a user index buffer. */
-       struct pipe_index_buffer ibuffer_saved = {};
-       if (info->indexed && ctx->indexbuf.user_buffer &&
-           !util_save_and_upload_index_buffer(pctx, info, &ctx->indexbuf,
-                                              &ibuffer_saved)) {
-               return;
+       struct pipe_resource *indexbuf = NULL;
+       unsigned index_offset = 0;
+       struct pipe_draw_info new_info;
+       if (info->index_size) {
+               if (info->has_user_indices) {
+                       if (!util_upload_index_buffer(pctx, info, &indexbuf, &index_offset))
+                               return;
+                       new_info = *info;
+                       new_info.index.resource = indexbuf;
+                       new_info.has_user_indices = false;
+                       info = &new_info;
+               } else {
+                       indexbuf = info->index.resource;
+               }
        }
 
        if (ctx->in_blit) {
@@ -119,12 +128,16 @@ fd_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
        mtx_lock(&ctx->screen->lock);
 
        if (fd_depth_enabled(ctx)) {
+               if (fd_resource(pfb->zsbuf->texture)->valid)
+                       restore_buffers |= FD_BUFFER_DEPTH;
                buffers |= FD_BUFFER_DEPTH;
                resource_written(batch, pfb->zsbuf->texture);
                batch->gmem_reason |= FD_GMEM_DEPTH_ENABLED;
        }
 
        if (fd_stencil_enabled(ctx)) {
+               if (fd_resource(pfb->zsbuf->texture)->valid)
+                       restore_buffers |= FD_BUFFER_DEPTH;
                buffers |= FD_BUFFER_STENCIL;
                resource_written(batch, pfb->zsbuf->texture);
                batch->gmem_reason |= FD_GMEM_STENCIL_ENABLED;
@@ -142,6 +155,10 @@ fd_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                surf = pfb->cbufs[i]->texture;
 
                resource_written(batch, surf);
+
+               if (fd_resource(surf)->valid)
+                       restore_buffers |= PIPE_CLEAR_COLOR0 << i;
+
                buffers |= PIPE_CLEAR_COLOR0 << i;
 
                if (surf->nr_samples > 1)
@@ -155,7 +172,16 @@ fd_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
         * read vs written, so just assume the worst
         */
        foreach_bit(i, ctx->shaderbuf[PIPE_SHADER_FRAGMENT].enabled_mask)
-               resource_read(batch, ctx->shaderbuf[PIPE_SHADER_FRAGMENT].sb[i].buffer);
+               resource_written(batch, ctx->shaderbuf[PIPE_SHADER_FRAGMENT].sb[i].buffer);
+
+       foreach_bit(i, ctx->shaderimg[PIPE_SHADER_FRAGMENT].enabled_mask) {
+               struct pipe_image_view *img =
+                       &ctx->shaderimg[PIPE_SHADER_FRAGMENT].si[i];
+               if (img->access & PIPE_IMAGE_ACCESS_WRITE)
+                       resource_written(batch, img->resource);
+               else
+                       resource_read(batch, img->resource);
+       }
 
        foreach_bit(i, ctx->constbuf[PIPE_SHADER_VERTEX].enabled_mask)
                resource_read(batch, ctx->constbuf[PIPE_SHADER_VERTEX].cb[i].buffer);
@@ -169,7 +195,11 @@ fd_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
        }
 
        /* Mark index buffer as being read */
-       resource_read(batch, ctx->indexbuf.buffer);
+       resource_read(batch, indexbuf);
+
+       /* Mark indirect draw buffer as being read */
+       if (info->indirect)
+               resource_read(batch, info->indirect->buffer);
 
        /* Mark textures as being read */
        foreach_bit(i, ctx->tex[PIPE_SHADER_VERTEX].valid_textures)
@@ -206,7 +236,7 @@ fd_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
        ctx->stats.prims_generated += prims;
 
        /* any buffers that haven't been cleared yet, we need to restore: */
-       batch->restore |= buffers & (FD_BUFFER_ALL & ~batch->cleared);
+       batch->restore |= restore_buffers & (FD_BUFFER_ALL & ~batch->cleared);
        /* and any buffers used, need to be resolved: */
        batch->resolve |= buffers;
 
@@ -215,7 +245,7 @@ fd_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                util_format_short_name(pipe_surface_format(pfb->cbufs[0])),
                util_format_short_name(pipe_surface_format(pfb->zsbuf)));
 
-       if (ctx->draw_vbo(ctx, info))
+       if (ctx->draw_vbo(ctx, info, index_offset))
                batch->needs_flush = true;
 
        for (i = 0; i < ctx->streamout.num_targets; i++)
@@ -226,8 +256,8 @@ fd_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
 
        fd_batch_check_size(batch);
 
-       if (info->indexed && ibuffer_saved.user_buffer)
-           pctx->set_index_buffer(pctx, &ibuffer_saved);
+       if (info == &new_info)
+               pipe_resource_reference(&indexbuf, NULL);
 }
 
 /* Generic clear implementation (partially) using u_blitter: */
@@ -286,7 +316,7 @@ fd_blitter_clear(struct pipe_context *pctx, unsigned buffers,
                .max_index = 1,
                .instance_count = 1,
        };
-       ctx->draw_vbo(ctx, &info);
+       ctx->draw_vbo(ctx, &info, 0);
 
        util_blitter_restore_constant_buffer_state(blitter);
        util_blitter_restore_vertex_states(blitter);
@@ -376,17 +406,22 @@ fd_clear(struct pipe_context *pctx, unsigned buffers,
        /* if per-gen backend doesn't implement ctx->clear() generic
         * blitter clear:
         */
-       if (!ctx->clear) {
-               fd_blitter_clear(pctx, buffers, color, depth, stencil);
-               return;
-       }
+       bool fallback = true;
 
-       fd_batch_set_stage(batch, FD_STAGE_CLEAR);
+       if (ctx->clear) {
+               fd_batch_set_stage(batch, FD_STAGE_CLEAR);
 
-       ctx->clear(ctx, buffers, color, depth, stencil);
+               if (ctx->clear(ctx, buffers, color, depth, stencil)) {
+                       if (fd_mesa_debug & FD_DBG_DCLEAR)
+                               fd_context_all_dirty(ctx);
 
-       if (fd_mesa_debug & FD_DBG_DCLEAR)
-               fd_context_all_dirty(ctx);
+                       fallback = false;
+               }
+       }
+
+       if (fallback) {
+               fd_blitter_clear(pctx, buffers, color, depth, stencil);
+       }
 }
 
 static void
@@ -415,13 +450,7 @@ fd_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)
        struct fd_batch *batch, *save_batch = NULL;
        unsigned i;
 
-       /* TODO maybe we don't want to allocate and flush a batch each time?
-        * We could use a special bogus (ie. won't match any fb state) key
-        * in the batch-case for compute shaders, and rely on the rest of
-        * the dependency tracking mechanism to tell us when the compute
-        * batch needs to be flushed?
-        */
-       batch = fd_bc_alloc_batch(&ctx->screen->batch_cache, ctx);
+       batch = fd_batch_create(ctx);
        fd_batch_reference(&save_batch, ctx->batch);
        fd_batch_reference(&ctx->batch, batch);
 
@@ -433,6 +462,15 @@ fd_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)
        foreach_bit(i, ctx->shaderbuf[PIPE_SHADER_COMPUTE].enabled_mask)
                resource_read(batch, ctx->shaderbuf[PIPE_SHADER_COMPUTE].sb[i].buffer);
 
+       foreach_bit(i, ctx->shaderimg[PIPE_SHADER_COMPUTE].enabled_mask) {
+               struct pipe_image_view *img =
+                       &ctx->shaderimg[PIPE_SHADER_COMPUTE].si[i];
+               if (img->access & PIPE_IMAGE_ACCESS_WRITE)
+                       resource_written(batch, img->resource);
+               else
+                       resource_read(batch, img->resource);
+       }
+
        /* UBO's are read */
        foreach_bit(i, ctx->constbuf[PIPE_SHADER_COMPUTE].enabled_mask)
                resource_read(batch, ctx->constbuf[PIPE_SHADER_COMPUTE].cb[i].buffer);
@@ -441,6 +479,9 @@ fd_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)
        foreach_bit(i, ctx->tex[PIPE_SHADER_COMPUTE].valid_textures)
                resource_read(batch, ctx->tex[PIPE_SHADER_COMPUTE].textures[i]->texture);
 
+       if (info->indirect)
+               resource_read(batch, info->indirect);
+
        mtx_unlock(&ctx->screen->lock);
 
        ctx->launch_grid(ctx, info);