freedreno/gmem: make noscis debug actually do something on a6xx
[mesa.git] / src / gallium / drivers / freedreno / freedreno_gmem.c
index 0af7388fe065dfbf4c3de4c64e05ce91250f9586..4c694df1c63130aa56b9fe0e4534d1c1603c5e31 100644 (file)
@@ -109,6 +109,54 @@ gmem_key_equals(const void *_a, const void *_b)
        return memcmp(a, b, sizeof(*a)) == 0;
 }
 
+static void
+dump_gmem_key(const struct gmem_key *key)
+{
+       printf("{ .minx=%u, .miny=%u, .width=%u, .height=%u",
+                       key->minx, key->miny, key->width, key->height);
+       printf(", .gmem_page_align=%u, .nr_cbufs=%u",
+                       key->gmem_page_align, key->nr_cbufs);
+       printf(", .cbuf_cpp = {");
+       for (unsigned i = 0; i < ARRAY_SIZE(key->cbuf_cpp); i++)
+               printf("%u,", key->cbuf_cpp[i]);
+       printf("}, .zsbuf_cpp = {");
+       for (unsigned i = 0; i < ARRAY_SIZE(key->zsbuf_cpp); i++)
+               printf("%u,", key->zsbuf_cpp[i]);
+       printf("}},\n");
+}
+
+static void
+dump_gmem_state(const struct fd_gmem_stateobj *gmem)
+{
+       unsigned total = 0;
+       printf("GMEM LAYOUT: bin=%ux%u, nbins=%ux%u\n",
+                       gmem->bin_w, gmem->bin_h, gmem->nbins_x, gmem->nbins_y);
+       for (int i = 0; i < ARRAY_SIZE(gmem->cbuf_base); i++) {
+               if (!gmem->cbuf_cpp[i])
+                       continue;
+
+               unsigned size = gmem->cbuf_cpp[i] * gmem->bin_w * gmem->bin_h;
+               printf("  cbuf[%d]: base=0x%06x, size=0x%x, cpp=%u\n", i,
+                               gmem->cbuf_base[i], size, gmem->cbuf_cpp[i]);
+
+               total = gmem->cbuf_base[i] + size;
+       }
+
+       for (int i = 0; i < ARRAY_SIZE(gmem->zsbuf_base); i++) {
+               if (!gmem->zsbuf_cpp[i])
+                       continue;
+
+               unsigned size = gmem->zsbuf_cpp[i] * gmem->bin_w * gmem->bin_h;
+               printf("  zsbuf[%d]: base=0x%06x, size=0x%x, cpp=%u\n", i,
+                               gmem->zsbuf_base[i], size, gmem->zsbuf_cpp[i]);
+
+               total = gmem->zsbuf_base[i] + size;
+       }
+
+       printf("total: 0x%06x (of 0x%06x)\n", total,
+                       gmem->screen->gmemsize_bytes);
+}
+
 static uint32_t bin_width(struct fd_screen *screen)
 {
        if (is_a4xx(screen) || is_a5xx(screen) || is_a6xx(screen))
@@ -118,31 +166,54 @@ static uint32_t bin_width(struct fd_screen *screen)
        return 512;
 }
 
-static uint32_t
-total_size(struct gmem_key *key, uint32_t bin_w, uint32_t bin_h,
+static unsigned
+div_align(unsigned num, unsigned denom, unsigned al)
+{
+       return util_align_npot(DIV_ROUND_UP(num, denom), al);
+}
+
+static bool
+layout_gmem(struct gmem_key *key, uint32_t nbins_x, uint32_t nbins_y,
                struct fd_gmem_stateobj *gmem)
 {
+       struct fd_screen *screen = gmem->screen;
        uint32_t gmem_align = key->gmem_page_align * 0x1000;
        uint32_t total = 0, i;
 
+       if ((nbins_x == 0) || (nbins_y == 0))
+               return false;
+
+       uint32_t bin_w, bin_h;
+       bin_w = div_align(key->width, nbins_x, screen->tile_alignw);
+       bin_h = div_align(key->height, nbins_y, screen->tile_alignh);
+
+       gmem->bin_w = bin_w;
+       gmem->bin_h = bin_h;
+
+       /* due to aligning bin_w/h, we could end up with one too
+        * many bins in either dimension, so recalculate:
+        */
+       gmem->nbins_x = DIV_ROUND_UP(key->width, bin_w);
+       gmem->nbins_y = DIV_ROUND_UP(key->height, bin_h);
+
        for (i = 0; i < MAX_RENDER_TARGETS; i++) {
                if (key->cbuf_cpp[i]) {
-                       gmem->cbuf_base[i] = align(total, gmem_align);
+                       gmem->cbuf_base[i] = util_align_npot(total, gmem_align);
                        total = gmem->cbuf_base[i] + key->cbuf_cpp[i] * bin_w * bin_h;
                }
        }
 
        if (key->zsbuf_cpp[0]) {
-               gmem->zsbuf_base[0] = align(total, gmem_align);
+               gmem->zsbuf_base[0] = util_align_npot(total, gmem_align);
                total = gmem->zsbuf_base[0] + key->zsbuf_cpp[0] * bin_w * bin_h;
        }
 
        if (key->zsbuf_cpp[1]) {
-               gmem->zsbuf_base[1] = align(total, gmem_align);
+               gmem->zsbuf_base[1] = util_align_npot(total, gmem_align);
                total = gmem->zsbuf_base[1] + key->zsbuf_cpp[1] * bin_w * bin_h;
        }
 
-       return total;
+       return total <= screen->gmemsize_bytes;
 }
 
 static struct fd_gmem_stateobj *
@@ -155,28 +226,13 @@ gmem_stateobj_init(struct fd_screen *screen, struct gmem_key *key)
        gmem->key = key;
        list_inithead(&gmem->node);
 
-       const uint32_t gmem_alignw = screen->gmem_alignw;
-       const uint32_t gmem_alignh = screen->gmem_alignh;
        const unsigned npipes = screen->num_vsc_pipes;
-       const uint32_t gmem_size = screen->gmemsize_bytes;
        uint32_t nbins_x = 1, nbins_y = 1;
-       uint32_t bin_w, bin_h;
        uint32_t max_width = bin_width(screen);
        uint32_t i, j, t, xoff, yoff;
        uint32_t tpp_x, tpp_y;
        int tile_n[npipes];
 
-       bin_w = align(key->width, gmem_alignw);
-       bin_h = align(key->height, gmem_alignh);
-
-       /* first, find a bin width that satisfies the maximum width
-        * restrictions:
-        */
-       while (bin_w > max_width) {
-               nbins_x++;
-               bin_w = align(key->width / nbins_x, gmem_alignw);
-       }
-
        if (fd_mesa_debug & FD_DBG_MSGS) {
                debug_printf("binning input: cbuf cpp:");
                for (i = 0; i < key->nr_cbufs; i++)
@@ -185,32 +241,54 @@ gmem_stateobj_init(struct fd_screen *screen, struct gmem_key *key)
                                key->zsbuf_cpp[0], key->width, key->height);
        }
 
+       /* first, find a bin width that satisfies the maximum width
+        * restrictions:
+        */
+       while (div_align(key->width, nbins_x, screen->tile_alignw) > max_width) {
+               nbins_x++;
+       }
+
        /* then find a bin width/height that satisfies the memory
         * constraints:
         */
-       while (total_size(key, bin_w, bin_h, gmem) > gmem_size) {
-               if (bin_w > bin_h) {
+       while (!layout_gmem(key, nbins_x, nbins_y, gmem)) {
+               if (nbins_y > nbins_x) {
                        nbins_x++;
-                       bin_w = align(key->width / nbins_x, gmem_alignw);
                } else {
                        nbins_y++;
-                       bin_h = align(key->height / nbins_y, gmem_alignh);
                }
        }
 
-       DBG("using %d bins of size %dx%d", nbins_x*nbins_y, bin_w, bin_h);
+       /* Lets see if we can tweak the layout a bit and come up with
+        * something better:
+        */
+       if ((((nbins_x - 1) * (nbins_y + 1)) < (nbins_x * nbins_y)) &&
+                       layout_gmem(key, nbins_x - 1, nbins_y + 1, gmem)) {
+               nbins_x--;
+               nbins_y++;
+       } else if ((((nbins_x + 1) * (nbins_y - 1)) < (nbins_x * nbins_y)) &&
+                       layout_gmem(key, nbins_x + 1, nbins_y - 1, gmem)) {
+               nbins_x++;
+               nbins_y--;
+       }
+
+       layout_gmem(key, nbins_x, nbins_y, gmem);
+
+       DBG("using %d bins of size %dx%d", gmem->nbins_x * gmem->nbins_y,
+                       gmem->bin_w, gmem->bin_h);
 
        memcpy(gmem->cbuf_cpp, key->cbuf_cpp, sizeof(key->cbuf_cpp));
        memcpy(gmem->zsbuf_cpp, key->zsbuf_cpp, sizeof(key->zsbuf_cpp));
-       gmem->bin_h = bin_h;
-       gmem->bin_w = bin_w;
-       gmem->nbins_x = nbins_x;
-       gmem->nbins_y = nbins_y;
        gmem->minx = key->minx;
        gmem->miny = key->miny;
        gmem->width = key->width;
        gmem->height = key->height;
 
+       if (BIN_DEBUG) {
+               dump_gmem_state(gmem);
+               dump_gmem_key(key);
+       }
+
        /*
         * Assign tiles and pipes:
         *
@@ -289,7 +367,7 @@ gmem_stateobj_init(struct fd_screen *screen, struct gmem_key *key)
                xoff = key->minx;
 
                /* clip bin height: */
-               bh = MIN2(bin_h, key->miny + key->height - yoff);
+               bh = MIN2(gmem->bin_h, key->miny + key->height - yoff);
 
                for (j = 0; j < nbins_x; j++) {
                        struct fd_tile *tile = &gmem->tile[t];
@@ -302,7 +380,7 @@ gmem_stateobj_init(struct fd_screen *screen, struct gmem_key *key)
                        assert(p < gmem->num_vsc_pipes);
 
                        /* clip bin width: */
-                       bw = MIN2(bin_w, key->minx + key->width - xoff);
+                       bw = MIN2(gmem->bin_w, key->minx + key->width - xoff);
                        tile->n = !is_a20x(screen) ? tile_n[p]++ :
                                ((i % tpp_y + 1) << 3 | (j % tpp_x + 1));
                        tile->p = p;
@@ -343,7 +421,7 @@ __fd_gmem_destroy(struct fd_gmem_stateobj *gmem)
 {
        struct fd_gmem_cache *cache = &gmem->screen->gmem_cache;
 
-       pipe_mutex_assert_locked(gmem->screen->lock);
+       fd_screen_assert_locked(gmem->screen);
 
        _mesa_hash_table_remove_key(cache->ht, gmem->key);
        list_del(&gmem->node);
@@ -353,7 +431,7 @@ __fd_gmem_destroy(struct fd_gmem_stateobj *gmem)
 }
 
 static struct gmem_key *
-key_init(struct fd_batch *batch)
+gmem_key_init(struct fd_batch *batch, bool assume_zs, bool no_scis_opt)
 {
        struct fd_screen *screen = batch->ctx->screen;
        struct pipe_framebuffer_state *pfb = &batch->framebuffer;
@@ -361,7 +439,7 @@ key_init(struct fd_batch *batch)
                FD_GMEM_STENCIL_ENABLED | FD_GMEM_CLEARS_DEPTH_STENCIL));
        struct gmem_key *key = rzalloc(screen->gmem_cache.ht, struct gmem_key);
 
-       if (has_zs) {
+       if (has_zs || assume_zs) {
                struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
                key->zsbuf_cpp[0] = rsc->layout.cpp;
                if (rsc->stencil)
@@ -382,7 +460,10 @@ key_init(struct fd_batch *batch)
                key->cbuf_cpp[i] *= pfb->samples;
        }
 
-       if (fd_mesa_debug & FD_DBG_NOSCIS) {
+       /* NOTE: on a6xx, the max-scissor-rect is handled in fd6_gmem, and
+        * we just rely on CP_COND_EXEC to skip bins with no geometry.
+        */
+       if (no_scis_opt || is_a6xx(screen)) {
                key->minx = 0;
                key->miny = 0;
                key->width = pfb->width;
@@ -390,6 +471,13 @@ key_init(struct fd_batch *batch)
        } else {
                struct pipe_scissor_state *scissor = &batch->max_scissor;
 
+               if (fd_mesa_debug & FD_DBG_NOSCIS) {
+                       scissor->minx = 0;
+                       scissor->miny = 0;
+                       scissor->maxx = pfb->width;
+                       scissor->maxy = pfb->height;
+               }
+
                /* round down to multiple of alignment: */
                key->minx = scissor->minx & ~(screen->gmem_alignw - 1);
                key->miny = scissor->miny & ~(screen->gmem_alignh - 1);
@@ -402,6 +490,8 @@ key_init(struct fd_batch *batch)
                 * but the fast clear path requires an alignment of 32K
                 */
                key->gmem_page_align = 8;
+       } else if (is_a6xx(screen)) {
+               key->gmem_page_align = is_a650(screen) ? 3 : 1;
        } else {
                // TODO re-check this across gens.. maybe it should only
                // be a single page in some cases:
@@ -412,15 +502,15 @@ key_init(struct fd_batch *batch)
 }
 
 static struct fd_gmem_stateobj *
-lookup_gmem_state(struct fd_batch *batch)
+lookup_gmem_state(struct fd_batch *batch, bool assume_zs, bool no_scis_opt)
 {
        struct fd_screen *screen = batch->ctx->screen;
        struct fd_gmem_cache *cache = &screen->gmem_cache;
        struct fd_gmem_stateobj *gmem = NULL;
-       struct gmem_key *key = key_init(batch);
+       struct gmem_key *key = gmem_key_init(batch, assume_zs, no_scis_opt);
        uint32_t hash = gmem_key_hash(key);
 
-       mtx_lock(&screen->lock);
+       fd_screen_lock(screen);
 
        struct hash_entry *entry =
                _mesa_hash_table_search_pre_hashed(cache->ht, hash, key);
@@ -447,7 +537,7 @@ found:
        list_delinit(&gmem->node);
        list_add(&gmem->node, &cache->lru);
 
-       mtx_unlock(&screen->lock);
+       fd_screen_unlock(screen);
 
        return gmem;
 }
@@ -532,6 +622,9 @@ flush_ring(struct fd_batch *batch)
        uint32_t timestamp;
        int out_fence_fd = -1;
 
+       if (unlikely(fd_mesa_debug & FD_DBG_NOHW))
+               return;
+
        fd_submit_flush(batch->submit, batch->in_fence_fd,
                        batch->needs_out_fence_fd ? &out_fence_fd : NULL,
                        &timestamp);
@@ -610,7 +703,7 @@ fd_gmem_render_tiles(struct fd_batch *batch)
                render_sysmem(batch);
                ctx->stats.batch_sysmem++;
        } else {
-               struct fd_gmem_stateobj *gmem = lookup_gmem_state(batch);
+               struct fd_gmem_stateobj *gmem = lookup_gmem_state(batch, false, false);
                batch->gmem_state = gmem;
                fd_log(batch, "%p: rendering %dx%d tiles %ux%u (%s/%s)",
                        batch, pfb->width, pfb->height, gmem->nbins_x, gmem->nbins_y,
@@ -621,9 +714,9 @@ fd_gmem_render_tiles(struct fd_batch *batch)
                render_tiles(batch, gmem);
                batch->gmem_state = NULL;
 
-               mtx_lock(&ctx->screen->lock);
+               fd_screen_lock(ctx->screen);
                fd_gmem_reference(&gmem, NULL);
-               mtx_unlock(&ctx->screen->lock);
+               fd_screen_unlock(ctx->screen);
 
                ctx->stats.batch_gmem++;
        }
@@ -631,6 +724,24 @@ fd_gmem_render_tiles(struct fd_batch *batch)
        flush_ring(batch);
 }
 
+/* Determine a worst-case estimate (ie. assuming we don't eliminate an
+ * unused depth/stencil) number of bins per vsc pipe.
+ */
+unsigned
+fd_gmem_estimate_bins_per_pipe(struct fd_batch *batch)
+{
+       struct pipe_framebuffer_state *pfb = &batch->framebuffer;
+       struct fd_screen *screen = batch->ctx->screen;
+       struct fd_gmem_stateobj *gmem = lookup_gmem_state(batch, !!pfb->zsbuf, true);
+       unsigned nbins = gmem->maxpw * gmem->maxph;
+
+       fd_screen_lock(screen);
+       fd_gmem_reference(&gmem, NULL);
+       fd_screen_unlock(screen);
+
+       return nbins;
+}
+
 /* When deciding whether a tile needs mem2gmem, we need to take into
  * account the scissor rect(s) that were cleared.  To simplify we only
  * consider the last scissor rect for each buffer, since the common