panfrost: Dual source blend support
[mesa.git] / src / gallium / drivers / panfrost / pan_job.c
index 35d5e0be67e6caa5b71ba69a5bb54483fe21a8b4..f83cbeaf4834f45a657974e809beab9a0b02d466 100644 (file)
@@ -33,6 +33,7 @@
 #include "util/ralloc.h"
 #include "util/format/u_format.h"
 #include "util/u_pack_color.h"
+#include "util/rounding.h"
 #include "pan_util.h"
 #include "pandecode/decode.h"
 #include "panfrost-quirks.h"
@@ -42,8 +43,7 @@
  * better GPU utilization.
  *
  * Each accessed BO has a corresponding entry in the ->accessed_bos hash table.
- * A BO is either being written or read at any time, that's what the type field
- * encodes.
+ * A BO is either being written or read at any time (see if writer != NULL).
  * When the last access is a write, the batch writing the BO might have read
  * dependencies (readers that have not been executed yet and want to read the
  * previous BO content), and when the last access is a read, all readers might
@@ -55,7 +55,6 @@
  * updated to point to the new writer.
  */
 struct panfrost_bo_access {
-        uint32_t type;
         struct util_dynarray readers;
         struct panfrost_batch_fence *writer;
 };
@@ -71,7 +70,7 @@ panfrost_create_batch_fence(struct panfrost_batch *batch)
         pipe_reference_init(&fence->reference, 1);
         fence->ctx = batch->ctx;
         fence->batch = batch;
-        ret = drmSyncobjCreate(pan_screen(batch->ctx->base.screen)->fd, 0,
+        ret = drmSyncobjCreate(pan_device(batch->ctx->base.screen)->fd, 0,
                                &fence->syncobj);
         assert(!ret);
 
@@ -81,7 +80,7 @@ panfrost_create_batch_fence(struct panfrost_batch *batch)
 static void
 panfrost_free_batch_fence(struct panfrost_batch_fence *fence)
 {
-        drmSyncobjDestroy(pan_screen(fence->ctx->base.screen)->fd,
+        drmSyncobjDestroy(pan_device(fence->ctx->base.screen)->fd,
                           fence->syncobj);
         ralloc_free(fence);
 }
@@ -108,18 +107,16 @@ panfrost_create_batch(struct panfrost_context *ctx,
         batch->ctx = ctx;
 
         batch->bos = _mesa_hash_table_create(batch, _mesa_hash_pointer,
-                                             _mesa_key_pointer_equal);
+                        _mesa_key_pointer_equal);
 
         batch->minx = batch->miny = ~0;
         batch->maxx = batch->maxy = 0;
-        batch->transient_offset = 0;
 
-        util_dynarray_init(&batch->headers, batch);
-        util_dynarray_init(&batch->gpu_headers, batch);
-        util_dynarray_init(&batch->dependencies, batch);
         batch->out_sync = panfrost_create_batch_fence(batch);
         util_copy_framebuffer_state(&batch->key, key);
 
+        batch->pool = panfrost_create_pool(batch, pan_device(ctx->base.screen));
+
         return batch;
 }
 
@@ -148,7 +145,7 @@ panfrost_freeze_batch(struct panfrost_batch *batch)
         }
 }
 
-#ifndef NDEBUG
+#ifdef PAN_BATCH_DEBUG
 static bool panfrost_batch_is_frozen(struct panfrost_batch *batch)
 {
         struct panfrost_context *ctx = batch->ctx;
@@ -171,19 +168,21 @@ panfrost_free_batch(struct panfrost_batch *batch)
         if (!batch)
                 return;
 
+#ifdef PAN_BATCH_DEBUG
         assert(panfrost_batch_is_frozen(batch));
+#endif
 
         hash_table_foreach(batch->bos, entry)
                 panfrost_bo_unreference((struct panfrost_bo *)entry->key);
 
+        hash_table_foreach(batch->pool.bos, entry)
+                panfrost_bo_unreference((struct panfrost_bo *)entry->key);
+
         util_dynarray_foreach(&batch->dependencies,
                               struct panfrost_batch_fence *, dep) {
                 panfrost_batch_fence_unreference(*dep);
         }
 
-        util_dynarray_fini(&batch->headers);
-        util_dynarray_fini(&batch->gpu_headers);
-
         /* The out_sync fence lifetime is different from the the batch one
          * since other batches might want to wait on a fence of already
          * submitted/signaled batch. All we need to do here is make sure the
@@ -197,7 +196,7 @@ panfrost_free_batch(struct panfrost_batch *batch)
         ralloc_free(batch);
 }
 
-#ifndef NDEBUG
+#ifdef PAN_BATCH_DEBUG
 static bool
 panfrost_dep_graph_contains_batch(struct panfrost_batch *root,
                                   struct panfrost_batch *batch)
@@ -233,8 +232,10 @@ panfrost_batch_add_dep(struct panfrost_batch *batch,
                         return;
         }
 
+#ifdef PAN_BATCH_DEBUG
         /* Make sure the dependency graph is acyclic. */
         assert(!panfrost_dep_graph_contains_batch(newdep->batch, batch));
+#endif
 
         panfrost_batch_fence_reference(newdep);
         util_dynarray_append(&batch->dependencies,
@@ -308,7 +309,7 @@ panfrost_get_fresh_batch_for_fbo(struct panfrost_context *ctx)
          * Note that it's perfectly fine to re-use a batch with an
          * existing clear, we'll just update it with the new clear request.
          */
-        if (!batch->last_job.gpu)
+        if (!batch->scoreboard.first_job)
                 return batch;
 
         /* Otherwise, we need to freeze the existing one and instantiate a new
@@ -328,7 +329,7 @@ panfrost_batch_fence_is_signaled(struct panfrost_batch_fence *fence)
         if (fence->batch)
                 return false;
 
-        int ret = drmSyncobjWait(pan_screen(fence->ctx->base.screen)->fd,
+        int ret = drmSyncobjWait(pan_device(fence->ctx->base.screen)->fd,
                                  &fence->syncobj, 1, 0, 0, NULL);
 
         /* Cache whether the fence was signaled */
@@ -395,7 +396,7 @@ panfrost_gc_fences(struct panfrost_context *ctx)
         }
 }
 
-#ifndef NDEBUG
+#ifdef PAN_BATCH_DEBUG
 static bool
 panfrost_batch_in_readers(struct panfrost_batch *batch,
                           struct panfrost_bo_access *access)
@@ -412,36 +413,31 @@ panfrost_batch_in_readers(struct panfrost_batch *batch,
 
 static void
 panfrost_batch_update_bo_access(struct panfrost_batch *batch,
-                                struct panfrost_bo *bo, uint32_t access_type,
+                                struct panfrost_bo *bo, bool writes,
                                 bool already_accessed)
 {
         struct panfrost_context *ctx = batch->ctx;
         struct panfrost_bo_access *access;
-        uint32_t old_access_type;
+        bool old_writes = false;
         struct hash_entry *entry;
 
-        assert(access_type == PAN_BO_ACCESS_WRITE ||
-               access_type == PAN_BO_ACCESS_READ);
-
         entry = _mesa_hash_table_search(ctx->accessed_bos, bo);
         access = entry ? entry->data : NULL;
         if (access) {
-                old_access_type = access->type;
+                old_writes = access->writer != NULL;
         } else {
                 access = rzalloc(ctx, struct panfrost_bo_access);
                 util_dynarray_init(&access->readers, access);
                 _mesa_hash_table_insert(ctx->accessed_bos, bo, access);
                 /* We are the first to access this BO, let's initialize
-                 * old_access_type to our own access type in that case.
+                 * old_writes to our own access type in that case.
                  */
-                old_access_type = access_type;
-                access->type = access_type;
+                old_writes = writes;
         }
 
         assert(access);
 
-        if (access_type == PAN_BO_ACCESS_WRITE &&
-            old_access_type == PAN_BO_ACCESS_READ) {
+        if (writes && !old_writes) {
                 /* Previous access was a read and we want to write this BO.
                  * We first need to add explicit deps between our batch and
                  * the previous readers.
@@ -459,9 +455,11 @@ panfrost_batch_update_bo_access(struct panfrost_batch *batch,
                 }
                 panfrost_batch_fence_reference(batch->out_sync);
 
+                if (access->writer)
+                        panfrost_batch_fence_unreference(access->writer);
+
                 /* We now are the new writer. */
                 access->writer = batch->out_sync;
-                access->type = access_type;
 
                 /* Release the previous readers and reset the readers array. */
                 util_dynarray_foreach(&access->readers,
@@ -473,10 +471,8 @@ panfrost_batch_update_bo_access(struct panfrost_batch *batch,
                 }
 
                 util_dynarray_clear(&access->readers);
-        } else if (access_type == PAN_BO_ACCESS_WRITE &&
-                   old_access_type == PAN_BO_ACCESS_WRITE) {
-                /* Previous access was a write and we want to write this BO.
-                 * First check if we were the previous writer, in that case
+        } else if (writes && old_writes) {
+                /* First check if we were the previous writer, in that case
                  * there's nothing to do. Otherwise we need to add a
                  * dependency between the new writer and the old one.
                  */
@@ -488,10 +484,8 @@ panfrost_batch_update_bo_access(struct panfrost_batch *batch,
                         panfrost_batch_fence_reference(batch->out_sync);
                         access->writer = batch->out_sync;
                 }
-        } else if (access_type == PAN_BO_ACCESS_READ &&
-                   old_access_type == PAN_BO_ACCESS_WRITE) {
-                /* Previous access was a write and we want to read this BO.
-                 * First check if we were the previous writer, in that case
+        } else if (!writes && old_writes) {
+                /* First check if we were the previous writer, in that case
                  * we want to keep the access type unchanged, as a write is
                  * more constraining than a read.
                  */
@@ -510,16 +504,18 @@ panfrost_batch_update_bo_access(struct panfrost_batch *batch,
                         util_dynarray_append(&access->readers,
                                              struct panfrost_batch_fence *,
                                              batch->out_sync);
-                        access->type = PAN_BO_ACCESS_READ;
+                        access->writer = NULL;
                 }
         } else {
                 /* We already accessed this BO before, so we should already be
                  * in the reader array.
                  */
+#ifdef PAN_BATCH_DEBUG
                 if (already_accessed) {
                         assert(panfrost_batch_in_readers(batch, access));
                         return;
                 }
+#endif
 
                 /* Previous access was a read and we want to read this BO.
                  * Add ourselves to the readers array and add a dependency on
@@ -579,11 +575,24 @@ panfrost_batch_add_bo(struct panfrost_batch *batch, struct panfrost_bo *bo,
         if (batch == batch->ctx->wallpaper_batch)
                 return;
 
-        /* Only pass R/W flags to the dep tracking logic. */
         assert(flags & PAN_BO_ACCESS_RW);
-        flags = (flags & PAN_BO_ACCESS_WRITE) ?
-                PAN_BO_ACCESS_WRITE : PAN_BO_ACCESS_READ;
-        panfrost_batch_update_bo_access(batch, bo, flags, old_flags != 0);
+        panfrost_batch_update_bo_access(batch, bo, flags & PAN_BO_ACCESS_WRITE,
+                        old_flags != 0);
+}
+
+static void
+panfrost_batch_add_resource_bos(struct panfrost_batch *batch,
+                                struct panfrost_resource *rsrc,
+                                uint32_t flags)
+{
+        panfrost_batch_add_bo(batch, rsrc->bo, flags);
+
+        for (unsigned i = 0; i < MAX_MIP_LEVELS; i++)
+                if (rsrc->slices[i].checksum_bo)
+                        panfrost_batch_add_bo(batch, rsrc->slices[i].checksum_bo, flags);
+
+        if (rsrc->separate_stencil)
+                panfrost_batch_add_bo(batch, rsrc->separate_stencil->bo, flags);
 }
 
 void panfrost_batch_add_fbo_bos(struct panfrost_batch *batch)
@@ -594,12 +603,12 @@ void panfrost_batch_add_fbo_bos(struct panfrost_batch *batch)
 
         for (unsigned i = 0; i < batch->key.nr_cbufs; ++i) {
                 struct panfrost_resource *rsrc = pan_resource(batch->key.cbufs[i]->texture);
-                panfrost_batch_add_bo(batch, rsrc->bo, flags);
+                panfrost_batch_add_resource_bos(batch, rsrc, flags);
         }
 
         if (batch->key.zsbuf) {
                 struct panfrost_resource *rsrc = pan_resource(batch->key.zsbuf->texture);
-                panfrost_batch_add_bo(batch, rsrc->bo, flags);
+                panfrost_batch_add_resource_bos(batch, rsrc, flags);
         }
 }
 
@@ -609,7 +618,7 @@ panfrost_batch_create_bo(struct panfrost_batch *batch, size_t size,
 {
         struct panfrost_bo *bo;
 
-        bo = panfrost_bo_create(pan_screen(batch->ctx->base.screen), size,
+        bo = panfrost_bo_create(pan_device(batch->ctx->base.screen), size,
                                 create_flags);
         panfrost_batch_add_bo(batch, bo, access_flags);
 
@@ -705,17 +714,50 @@ panfrost_batch_get_tiler_heap(struct panfrost_batch *batch)
         return batch->tiler_heap;
 }
 
+mali_ptr
+panfrost_batch_get_tiler_meta(struct panfrost_batch *batch, unsigned vertex_count)
+{
+        if (!vertex_count)
+                return 0;
+
+        if (batch->tiler_meta)
+                return batch->tiler_meta;
+
+        struct panfrost_bo *tiler_heap;
+        tiler_heap = panfrost_batch_get_tiler_heap(batch);
+
+        struct bifrost_tiler_heap_meta tiler_heap_meta = {
+            .heap_size = tiler_heap->size,
+            .tiler_heap_start = tiler_heap->gpu,
+            .tiler_heap_free = tiler_heap->gpu,
+            .tiler_heap_end = tiler_heap->gpu + tiler_heap->size,
+            .unk1 = 0x1,
+            .unk7e007e = 0x7e007e,
+        };
+
+        struct bifrost_tiler_meta tiler_meta = {
+            .hierarchy_mask = 0x28,
+            .flags = 0x0,
+            .width = MALI_POSITIVE(batch->key.width),
+            .height = MALI_POSITIVE(batch->key.height),
+            .tiler_heap_meta = panfrost_pool_upload(&batch->pool, &tiler_heap_meta, sizeof(tiler_heap_meta)),
+        };
+
+        batch->tiler_meta = panfrost_pool_upload(&batch->pool, &tiler_meta, sizeof(tiler_meta));
+        return batch->tiler_meta;
+}
+
 struct panfrost_bo *
 panfrost_batch_get_tiler_dummy(struct panfrost_batch *batch)
 {
-        struct panfrost_screen *screen = pan_screen(batch->ctx->base.screen);
+        struct panfrost_device *dev = pan_device(batch->ctx->base.screen);
 
         uint32_t create_flags = 0;
 
         if (batch->tiler_dummy)
                 return batch->tiler_dummy;
 
-        if (!(screen->quirks & MIDGARD_NO_HIER_TILING))
+        if (!(dev->quirks & MIDGARD_NO_HIER_TILING))
                 create_flags = PAN_BO_INVISIBLE;
 
         batch->tiler_dummy = panfrost_batch_create_bo(batch, 4096,
@@ -744,7 +786,7 @@ panfrost_batch_draw_wallpaper(struct panfrost_batch *batch)
         /* No draw calls, and no clear on the depth/stencil bufs.
          * Drawing the wallpaper would be useless.
          */
-        if (!batch->last_tiler.gpu &&
+        if (!batch->scoreboard.tiler_dep &&
             !(batch->clear & PIPE_CLEAR_DEPTHSTENCIL))
                 return;
 
@@ -843,21 +885,39 @@ panfrost_batch_draw_wallpaper(struct panfrost_batch *batch)
         batch->ctx->wallpaper_batch = NULL;
 }
 
+static void
+panfrost_batch_record_bo(struct hash_entry *entry, unsigned *bo_handles, unsigned idx)
+{
+        struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
+        uint32_t flags = (uintptr_t)entry->data;
+
+        assert(bo->gem_handle > 0);
+        bo_handles[idx] = bo->gem_handle;
+
+        /* Update the BO access flags so that panfrost_bo_wait() knows
+         * about all pending accesses.
+         * We only keep the READ/WRITE info since this is all the BO
+         * wait logic cares about.
+         * We also preserve existing flags as this batch might not
+         * be the first one to access the BO.
+         */
+        bo->gpu_access |= flags & (PAN_BO_ACCESS_RW);
+}
+
 static int
 panfrost_batch_submit_ioctl(struct panfrost_batch *batch,
                             mali_ptr first_job_desc,
-                            uint32_t reqs,
-                            struct mali_job_descriptor_header *header)
+                            uint32_t reqs)
 {
         struct panfrost_context *ctx = batch->ctx;
         struct pipe_context *gallium = (struct pipe_context *) ctx;
-        struct panfrost_screen *screen = pan_screen(gallium->screen);
+        struct panfrost_device *dev = pan_device(gallium->screen);
         struct drm_panfrost_submit submit = {0,};
         uint32_t *bo_handles, *in_syncs = NULL;
         bool is_fragment_shader;
         int ret;
 
-        is_fragment_shader = (reqs & PANFROST_JD_REQ_FS) && batch->first_job.gpu;
+        is_fragment_shader = (reqs & PANFROST_JD_REQ_FS) && batch->scoreboard.first_job;
         if (is_fragment_shader)
                 submit.in_sync_count = 1;
         else
@@ -887,45 +947,36 @@ panfrost_batch_submit_ioctl(struct panfrost_batch *batch,
         submit.jc = first_job_desc;
         submit.requirements = reqs;
 
-        bo_handles = calloc(batch->bos->entries, sizeof(*bo_handles));
+        bo_handles = calloc(batch->pool.bos->entries + batch->bos->entries, sizeof(*bo_handles));
         assert(bo_handles);
 
-        hash_table_foreach(batch->bos, entry) {
-                struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
-                uint32_t flags = (uintptr_t)entry->data;
-
-                assert(bo->gem_handle > 0);
-                bo_handles[submit.bo_handle_count++] = bo->gem_handle;
+        hash_table_foreach(batch->bos, entry)
+                panfrost_batch_record_bo(entry, bo_handles, submit.bo_handle_count++);
 
-                /* Update the BO access flags so that panfrost_bo_wait() knows
-                 * about all pending accesses.
-                 * We only keep the READ/WRITE info since this is all the BO
-                 * wait logic cares about.
-                 * We also preserve existing flags as this batch might not
-                 * be the first one to access the BO.
-                 */
-                bo->gpu_access |= flags & (PAN_BO_ACCESS_RW);
-        }
+        hash_table_foreach(batch->pool.bos, entry)
+                panfrost_batch_record_bo(entry, bo_handles, submit.bo_handle_count++);
 
         submit.bo_handles = (u64) (uintptr_t) bo_handles;
-        ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit);
+        ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit);
         free(bo_handles);
         free(in_syncs);
 
         if (ret) {
-                DBG("Error submitting: %m\n");
+                if (dev->debug & PAN_DBG_MSGS)
+                        fprintf(stderr, "Error submitting: %m\n");
+
                 return errno;
         }
 
         /* Trace the job if we're doing that */
-        if (pan_debug & (PAN_DBG_TRACE | PAN_DBG_SYNC)) {
+        if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC)) {
                 /* Wait so we can get errors reported back */
-                drmSyncobjWait(screen->fd, &batch->out_sync->syncobj, 1,
+                drmSyncobjWait(dev->fd, &batch->out_sync->syncobj, 1,
                                INT64_MAX, 0, NULL);
 
                 /* Trace gets priority over sync */
-                bool minimal = !(pan_debug & PAN_DBG_TRACE);
-                pandecode_jc(submit.jc, FALSE, screen->gpu_id, minimal);
+                bool minimal = !(dev->debug & PAN_DBG_TRACE);
+                pandecode_jc(submit.jc, dev->quirks & IS_BIFROST, dev->gpu_id, minimal);
         }
 
         return 0;
@@ -934,20 +985,17 @@ panfrost_batch_submit_ioctl(struct panfrost_batch *batch,
 static int
 panfrost_batch_submit_jobs(struct panfrost_batch *batch)
 {
-        bool has_draws = batch->first_job.gpu;
-        struct mali_job_descriptor_header *header;
+        bool has_draws = batch->scoreboard.first_job;
         int ret = 0;
 
         if (has_draws) {
-                header = (struct mali_job_descriptor_header *)batch->first_job.cpu;
-                ret = panfrost_batch_submit_ioctl(batch, batch->first_job.gpu, 0, header);
+                ret = panfrost_batch_submit_ioctl(batch, batch->scoreboard.first_job, 0);
                 assert(!ret);
         }
 
-        if (batch->first_tiler.gpu || batch->clear) {
-                mali_ptr fragjob = panfrost_fragment_job(batch, has_draws, &header);
-
-                ret = panfrost_batch_submit_ioctl(batch, fragjob, PANFROST_JD_REQ_FS, header);
+        if (batch->scoreboard.tiler_dep || batch->clear) {
+                mali_ptr fragjob = panfrost_fragment_job(batch, has_draws);
+                ret = panfrost_batch_submit_ioctl(batch, fragjob, PANFROST_JD_REQ_FS);
                 assert(!ret);
         }
 
@@ -958,6 +1006,7 @@ static void
 panfrost_batch_submit(struct panfrost_batch *batch)
 {
         assert(batch);
+        struct panfrost_device *dev = pan_device(batch->ctx->base.screen);
 
         /* Submit the dependencies first. */
         util_dynarray_foreach(&batch->dependencies,
@@ -969,7 +1018,7 @@ panfrost_batch_submit(struct panfrost_batch *batch)
         int ret;
 
         /* Nothing to do! */
-        if (!batch->last_job.gpu && !batch->clear) {
+        if (!batch->scoreboard.first_job && !batch->clear) {
                 /* Mark the fence as signaled so the fence logic does not try
                  * to wait on it.
                  */
@@ -982,23 +1031,26 @@ panfrost_batch_submit(struct panfrost_batch *batch)
         /* Now that all draws are in, we can finally prepare the
          * FBD for the batch */
 
-        if (batch->framebuffer.gpu && batch->first_job.gpu) {
+        if (batch->framebuffer.gpu && batch->scoreboard.first_job) {
                 struct panfrost_context *ctx = batch->ctx;
                 struct pipe_context *gallium = (struct pipe_context *) ctx;
-                struct panfrost_screen *screen = pan_screen(gallium->screen);
+                struct panfrost_device *dev = pan_device(gallium->screen);
 
-                if (screen->quirks & MIDGARD_SFBD)
+                if (dev->quirks & MIDGARD_SFBD)
                         panfrost_attach_sfbd(batch, ~0);
                 else
                         panfrost_attach_mfbd(batch, ~0);
         }
 
-        panfrost_scoreboard_link_batch(batch);
+        mali_ptr polygon_list = panfrost_batch_get_polygon_list(batch,
+                MALI_TILER_MINIMUM_HEADER_SIZE);
+
+        panfrost_scoreboard_initialize_tiler(&batch->pool, &batch->scoreboard, polygon_list);
 
         ret = panfrost_batch_submit_jobs(batch);
 
-        if (ret)
-                DBG("panfrost_batch_submit failed: %d\n", ret);
+        if (ret && dev->debug & PAN_DBG_MSGS)
+                fprintf(stderr, "panfrost_batch_submit failed: %d\n", ret);
 
         /* We must reset the damage info of our render targets here even
          * though a damage reset normally happens when the DRI layer swaps
@@ -1058,7 +1110,7 @@ panfrost_flush_all_batches(struct panfrost_context *ctx, bool wait)
         if (!wait)
                 return;
 
-        drmSyncobjWait(pan_screen(ctx->base.screen)->fd,
+        drmSyncobjWait(pan_device(ctx->base.screen)->fd,
                        util_dynarray_begin(&syncobjs),
                        util_dynarray_num_elements(&syncobjs, uint32_t),
                        INT64_MAX, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL, NULL);
@@ -1094,28 +1146,25 @@ panfrost_pending_batches_access_bo(struct panfrost_context *ctx,
         return false;
 }
 
+/* We always flush writers. We might also need to flush readers */
+
 void
 panfrost_flush_batches_accessing_bo(struct panfrost_context *ctx,
                                     struct panfrost_bo *bo,
-                                    uint32_t access_type)
+                                    bool flush_readers)
 {
         struct panfrost_bo_access *access;
         struct hash_entry *hentry;
 
-        /* It doesn't make any to flush only the readers. */
-        assert(access_type == PAN_BO_ACCESS_WRITE ||
-               access_type == PAN_BO_ACCESS_RW);
-
         hentry = _mesa_hash_table_search(ctx->accessed_bos, bo);
         access = hentry ? hentry->data : NULL;
         if (!access)
                 return;
 
-        if (access_type & PAN_BO_ACCESS_WRITE && access->writer &&
-            access->writer->batch)
+        if (access->writer && access->writer->batch)
                 panfrost_batch_submit(access->writer->batch);
 
-        if (!(access_type & PAN_BO_ACCESS_READ))
+        if (!flush_readers)
                 return;
 
         util_dynarray_foreach(&access->readers, struct panfrost_batch_fence *,
@@ -1137,6 +1186,22 @@ panfrost_batch_set_requirements(struct panfrost_batch *batch)
                 batch->requirements |= PAN_REQ_DEPTH_WRITE;
 }
 
+void
+panfrost_batch_adjust_stack_size(struct panfrost_batch *batch)
+{
+        struct panfrost_context *ctx = batch->ctx;
+
+        for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i) {
+                struct panfrost_shader_state *ss;
+
+                ss = panfrost_get_shader_state(ctx, i);
+                if (!ss)
+                        continue;
+
+                batch->stack_size = MAX2(batch->stack_size, ss->stack_size);
+        }
+}
+
 /* Helper to smear a 32-bit color across 128-bit components */
 
 static void
@@ -1176,43 +1241,37 @@ pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_
                                   ((uint32_t) float_to_ubyte(color->f[0]) <<  0));
         } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
                 /* First, we convert the components to R5, G6, B5 separately */
-                unsigned r5 = CLAMP(color->f[0], 0.0, 1.0) * 31.0;
-                unsigned g6 = CLAMP(color->f[1], 0.0, 1.0) * 63.0;
-                unsigned b5 = CLAMP(color->f[2], 0.0, 1.0) * 31.0;
+                unsigned r5 = _mesa_roundevenf(SATURATE(color->f[0]) * 31.0);
+                unsigned g6 = _mesa_roundevenf(SATURATE(color->f[1]) * 63.0);
+                unsigned b5 = _mesa_roundevenf(SATURATE(color->f[2]) * 31.0);
 
                 /* Then we pack into a sparse u32. TODO: Why these shifts? */
                 pan_pack_color_32(packed, (b5 << 25) | (g6 << 14) | (r5 << 5));
         } else if (format == PIPE_FORMAT_B4G4R4A4_UNORM) {
-                /* We scale the components against 0xF0 (=240.0), rather than 0xFF */
-                unsigned r4 = CLAMP(color->f[0], 0.0, 1.0) * 240.0;
-                unsigned g4 = CLAMP(color->f[1], 0.0, 1.0) * 240.0;
-                unsigned b4 = CLAMP(color->f[2], 0.0, 1.0) * 240.0;
-                unsigned a4 = CLAMP(clear_alpha, 0.0, 1.0) * 240.0;
+                /* Convert to 4-bits */
+                unsigned r4 = _mesa_roundevenf(SATURATE(color->f[0]) * 15.0);
+                unsigned g4 = _mesa_roundevenf(SATURATE(color->f[1]) * 15.0);
+                unsigned b4 = _mesa_roundevenf(SATURATE(color->f[2]) * 15.0);
+                unsigned a4 = _mesa_roundevenf(SATURATE(clear_alpha) * 15.0);
 
                 /* Pack on *byte* intervals */
-                pan_pack_color_32(packed, (a4 << 24) | (b4 << 16) | (g4 << 8) | r4);
+                pan_pack_color_32(packed, (a4 << 28) | (b4 << 20) | (g4 << 12) | (r4 << 4));
         } else if (format == PIPE_FORMAT_B5G5R5A1_UNORM) {
                 /* Scale as expected but shift oddly */
-                unsigned r5 = round(CLAMP(color->f[0], 0.0, 1.0)) * 31.0;
-                unsigned g5 = round(CLAMP(color->f[1], 0.0, 1.0)) * 31.0;
-                unsigned b5 = round(CLAMP(color->f[2], 0.0, 1.0)) * 31.0;
-                unsigned a1 = round(CLAMP(clear_alpha, 0.0, 1.0)) * 1.0;
+                unsigned r5 = _mesa_roundevenf(SATURATE(color->f[0]) * 31.0);
+                unsigned g5 = _mesa_roundevenf(SATURATE(color->f[1]) * 31.0);
+                unsigned b5 = _mesa_roundevenf(SATURATE(color->f[2]) * 31.0);
+                unsigned a1 = _mesa_roundevenf(SATURATE(clear_alpha) * 1.0);
 
                 pan_pack_color_32(packed, (a1 << 31) | (b5 << 25) | (g5 << 15) | (r5 << 5));
         } else {
-                /* Try Gallium's generic default path. Doesn't work for all
-                 * formats but it's a good guess. */
-
-                union util_color out;
-
-                if (util_format_is_pure_integer(format)) {
-                        memcpy(out.ui, color->ui, 16);
-                } else {
-                        util_pack_color(color->f, format, &out);
-                }
+                /* Otherwise, it's generic subject to replication */
 
+                union util_color out = { 0 };
                 unsigned size = util_format_get_blocksize(format);
 
+                util_pack_color(color->f, format, &out);
+
                 if (size == 1) {
                         unsigned b = out.ui[0];
                         unsigned s = b | (b << 8);
@@ -1262,7 +1321,7 @@ panfrost_batch_clear(struct panfrost_batch *batch,
 
         /* Clearing affects the entire framebuffer (by definition -- this is
          * the Gallium clear callback, which clears the whole framebuffer. If
-         * the scissor test were enabled from the GL side, the state tracker
+         * the scissor test were enabled from the GL side, the gallium frontend
          * would emit a quad instead and we wouldn't go down this code path) */
 
         panfrost_batch_union_scissor(batch, 0, 0,
@@ -1307,7 +1366,7 @@ panfrost_batch_intersection_scissor(struct panfrost_batch *batch,
         batch->maxy = MIN2(batch->maxy, maxy);
 }
 
-/* Are we currently rendering to the screen (rather than an FBO)? */
+/* Are we currently rendering to the dev (rather than an FBO)? */
 
 bool
 panfrost_batch_is_scanout(struct panfrost_batch *batch)