freedreno: make fd_batch a one-shot thing
[mesa.git] / src / gallium / drivers / freedreno / freedreno_batch.c
index 2b7e19a6333b6117c28686120c099b54bad3f873..18e4ca5fb03bff747ed7770a981247239a26d269 100644 (file)
@@ -55,12 +55,16 @@ batch_init(struct fd_batch *batch)
        }
 
        batch->draw    = fd_ringbuffer_new(ctx->pipe, size);
-       batch->binning = fd_ringbuffer_new(ctx->pipe, size);
-       batch->gmem    = fd_ringbuffer_new(ctx->pipe, size);
+       if (!batch->nondraw) {
+               batch->binning = fd_ringbuffer_new(ctx->pipe, size);
+               batch->gmem    = fd_ringbuffer_new(ctx->pipe, size);
 
-       fd_ringbuffer_set_parent(batch->gmem, NULL);
-       fd_ringbuffer_set_parent(batch->draw, batch->gmem);
-       fd_ringbuffer_set_parent(batch->binning, batch->gmem);
+               fd_ringbuffer_set_parent(batch->gmem, NULL);
+               fd_ringbuffer_set_parent(batch->draw, batch->gmem);
+               fd_ringbuffer_set_parent(batch->binning, batch->gmem);
+       } else {
+               fd_ringbuffer_set_parent(batch->draw, NULL);
+       }
 
        batch->in_fence_fd = -1;
        batch->fence = fd_fence_create(batch);
@@ -68,6 +72,7 @@ batch_init(struct fd_batch *batch)
        batch->cleared = batch->partial_cleared = 0;
        batch->restore = batch->resolve = 0;
        batch->needs_flush = false;
+       batch->flushed = false;
        batch->gmem_reason = 0;
        batch->num_draws = 0;
        batch->stage = FD_STAGE_NULL;
@@ -89,7 +94,7 @@ batch_init(struct fd_batch *batch)
 }
 
 struct fd_batch *
-fd_batch_create(struct fd_context *ctx)
+fd_batch_create(struct fd_context *ctx, bool nondraw)
 {
        struct fd_batch *batch = CALLOC_STRUCT(fd_batch);
 
@@ -100,6 +105,7 @@ fd_batch_create(struct fd_context *ctx)
 
        pipe_reference_init(&batch->reference, 1);
        batch->ctx = ctx;
+       batch->nondraw = nondraw;
 
        batch->resources = _mesa_set_create(NULL, _mesa_hash_pointer,
                        _mesa_key_pointer_equal);
@@ -112,6 +118,8 @@ fd_batch_create(struct fd_context *ctx)
 static void
 batch_fini(struct fd_batch *batch)
 {
+       DBG("%p", batch);
+
        pipe_resource_reference(&batch->query_buf, NULL);
 
        if (batch->in_fence_fd != -1)
@@ -123,8 +131,13 @@ batch_fini(struct fd_batch *batch)
        fd_fence_ref(NULL, &batch->fence, NULL);
 
        fd_ringbuffer_del(batch->draw);
-       fd_ringbuffer_del(batch->binning);
-       fd_ringbuffer_del(batch->gmem);
+       if (!batch->nondraw) {
+               fd_ringbuffer_del(batch->binning);
+               fd_ringbuffer_del(batch->gmem);
+       } else {
+               debug_assert(!batch->binning);
+               debug_assert(!batch->gmem);
+       }
        if (batch->lrz_clear) {
                fd_ringbuffer_del(batch->lrz_clear);
                batch->lrz_clear = NULL;
@@ -249,6 +262,8 @@ batch_flush_func(void *job, int id)
 {
        struct fd_batch *batch = job;
 
+       DBG("%p", batch);
+
        fd_gmem_render_tiles(batch);
        batch_reset_resources(batch);
 }
@@ -265,13 +280,8 @@ batch_flush(struct fd_batch *batch, bool force)
 {
        DBG("%p: needs_flush=%d", batch, batch->needs_flush);
 
-       if (!batch->needs_flush) {
-               if (force) {
-                       fd_gmem_render_noop(batch);
-                       goto out;
-               }
+       if (batch->flushed)
                return;
-       }
 
        batch->needs_flush = false;
 
@@ -283,6 +293,8 @@ batch_flush(struct fd_batch *batch, bool force)
        fd_context_all_dirty(batch->ctx);
        batch_flush_reset_dependencies(batch, true);
 
+       batch->flushed = true;
+
        if (batch->ctx->screen->reorder) {
                struct fd_batch *tmp = NULL;
                fd_batch_reference(&tmp, batch);
@@ -300,14 +312,9 @@ batch_flush(struct fd_batch *batch, bool force)
 
        debug_assert(batch->reference.count > 0);
 
-out:
-       if (batch == batch->ctx->batch) {
-               batch_reset(batch);
-       } else {
-               mtx_lock(&batch->ctx->screen->lock);
-               fd_bc_invalidate_batch(batch, false);
-               mtx_unlock(&batch->ctx->screen->lock);
-       }
+       mtx_lock(&batch->ctx->screen->lock);
+       fd_bc_invalidate_batch(batch, false);
+       mtx_unlock(&batch->ctx->screen->lock);
 }
 
 /* NOTE: could drop the last ref to batch
@@ -321,15 +328,36 @@ out:
 void
 fd_batch_flush(struct fd_batch *batch, bool sync, bool force)
 {
+       struct fd_batch *tmp = NULL;
+       bool newbatch = false;
+
        /* NOTE: we need to hold an extra ref across the body of flush,
         * since the last ref to this batch could be dropped when cleaning
         * up used_resources
         */
-       struct fd_batch *tmp = NULL;
        fd_batch_reference(&tmp, batch);
+
+       if (batch == batch->ctx->batch) {
+               batch->ctx->batch = NULL;
+               newbatch = true;
+       }
+
        batch_flush(tmp, force);
+
+       if (newbatch) {
+               struct fd_context *ctx = batch->ctx;
+               struct fd_batch *new_batch =
+                       fd_batch_from_fb(&ctx->screen->batch_cache, ctx, &batch->framebuffer);
+
+               util_copy_framebuffer_state(&new_batch->framebuffer, &batch->framebuffer);
+
+               fd_batch_reference(&batch, NULL);
+               ctx->batch = new_batch;
+       }
+
        if (sync)
                fd_batch_sync(tmp);
+
        fd_batch_reference(&tmp, NULL);
 }
 
@@ -350,26 +378,33 @@ batch_depends_on(struct fd_batch *batch, struct fd_batch *other)
        return false;
 }
 
-static void
-batch_add_dep(struct fd_batch *batch, struct fd_batch *dep)
+void
+fd_batch_add_dep(struct fd_batch *batch, struct fd_batch *dep)
 {
        if (batch->dependents_mask & (1 << dep->idx))
                return;
 
-       /* if the new depedency already depends on us, we need to flush
-        * to avoid a loop in the dependency graph.
-        */
-       if (batch_depends_on(dep, batch)) {
-               DBG("%p: flush forced on %p!", batch, dep);
-               mtx_unlock(&batch->ctx->screen->lock);
-               fd_batch_flush(dep, false, false);
-               mtx_lock(&batch->ctx->screen->lock);
-       } else {
-               struct fd_batch *other = NULL;
-               fd_batch_reference_locked(&other, dep);
-               batch->dependents_mask |= (1 << dep->idx);
-               DBG("%p: added dependency on %p", batch, dep);
-       }
+       /* a loop should not be possible */
+       debug_assert(!batch_depends_on(dep, batch));
+
+       struct fd_batch *other = NULL;
+       fd_batch_reference_locked(&other, dep);
+       batch->dependents_mask |= (1 << dep->idx);
+       DBG("%p: added dependency on %p", batch, dep);
+}
+
+static void
+flush_write_batch(struct fd_resource *rsc)
+{
+       struct fd_batch *b = NULL;
+       fd_batch_reference(&b, rsc->write_batch);
+
+       mtx_unlock(&b->ctx->screen->lock);
+       fd_batch_flush(b, true, false);
+       mtx_lock(&b->ctx->screen->lock);
+
+       fd_bc_invalidate_batch(b, false);
+       fd_batch_reference_locked(&b, NULL);
 }
 
 void
@@ -382,33 +417,44 @@ fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc, bool wri
 
        DBG("%p: %s %p", batch, write ? "write" : "read", rsc);
 
+       if (write)
+               rsc->valid = true;
+
        /* note, invalidate write batch, to avoid further writes to rsc
         * resulting in a write-after-read hazard.
         */
 
        if (write) {
                /* if we are pending read or write by any other batch: */
-               if (rsc->batch_mask != (1 << batch->idx)) {
+               if (rsc->batch_mask & ~(1 << batch->idx)) {
                        struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
                        struct fd_batch *dep;
+
+                       if (rsc->write_batch && rsc->write_batch != batch)
+                               flush_write_batch(rsc);
+
                        foreach_batch(dep, cache, rsc->batch_mask) {
                                struct fd_batch *b = NULL;
+                               if (dep == batch)
+                                       continue;
                                /* note that batch_add_dep could flush and unref dep, so
                                 * we need to hold a reference to keep it live for the
                                 * fd_bc_invalidate_batch()
                                 */
                                fd_batch_reference(&b, dep);
-                               batch_add_dep(batch, b);
+                               fd_batch_add_dep(batch, b);
                                fd_bc_invalidate_batch(b, false);
                                fd_batch_reference_locked(&b, NULL);
                        }
                }
                fd_batch_reference_locked(&rsc->write_batch, batch);
        } else {
-               if (rsc->write_batch) {
-                       batch_add_dep(batch, rsc->write_batch);
-                       fd_bc_invalidate_batch(rsc->write_batch, false);
-               }
+               /* If reading a resource pending a write, go ahead and flush the
+                * writer.  This avoids situations where we end up having to
+                * flush the current batch in _resource_used()
+                */
+               if (rsc->write_batch && rsc->write_batch != batch)
+                       flush_write_batch(rsc);
        }
 
        if (rsc->batch_mask & (1 << batch->idx))
@@ -423,6 +469,8 @@ fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc, bool wri
 void
 fd_batch_check_size(struct fd_batch *batch)
 {
+       debug_assert(!batch->flushed);
+
        if (fd_device_version(batch->ctx->screen->dev) >= FD_VERSION_UNLIMITED_CMDS)
                return;