freedreno/layout: layout simplifications and pitch from level 0 pitch
[mesa.git] / src / gallium / drivers / freedreno / a3xx / fd3_gmem.c
index b9af45683f957e8db27ad8a9d19698da86d3c72e..09f5c86b662e8f2da30490a2b4b2861657bed8eb 100644 (file)
@@ -1,5 +1,3 @@
-/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
-
 /*
  * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
  *
@@ -30,7 +28,7 @@
 #include "util/u_string.h"
 #include "util/u_memory.h"
 #include "util/u_inlines.h"
-#include "util/u_format.h"
+#include "util/format/u_format.h"
 
 #include "freedreno_draw.h"
 #include "freedreno_state.h"
 
 static void
 emit_mrt(struct fd_ringbuffer *ring, unsigned nr_bufs,
-                struct pipe_surface **bufs, uint32_t *bases, uint32_t bin_w,
+                struct pipe_surface **bufs, const uint32_t *bases, uint32_t bin_w,
                 bool decode_srgb)
 {
        enum a3xx_tile_mode tile_mode;
        unsigned i;
 
-       if (bin_w) {
-               tile_mode = TILE_32X32;
-       } else {
-               tile_mode = LINEAR;
-       }
-
        for (i = 0; i < A3XX_MAX_RENDER_TARGETS; i++) {
                enum pipe_format pformat = 0;
                enum a3xx_color_fmt format = 0;
                enum a3xx_color_swap swap = WZYX;
                bool srgb = false;
                struct fd_resource *rsc = NULL;
-               struct fd_resource_slice *slice = NULL;
                uint32_t stride = 0;
                uint32_t base = 0;
                uint32_t offset = 0;
 
+               if (bin_w) {
+                       tile_mode = TILE_32X32;
+               } else {
+                       tile_mode = LINEAR;
+               }
+
                if ((i < nr_bufs) && bufs[i]) {
                        struct pipe_surface *psurf = bufs[i];
 
@@ -78,13 +75,11 @@ emit_mrt(struct fd_ringbuffer *ring, unsigned nr_bufs,
                         */
                        if (rsc->stencil) {
                                rsc = rsc->stencil;
-                               pformat = rsc->base.b.format;
+                               pformat = rsc->base.format;
                                if (bases)
                                        bases++;
                        }
-                       slice = fd_resource_slice(rsc, psurf->u.tex.level);
                        format = fd3_pipe2color(pformat);
-                       swap = fd3_pipe2swap(pformat);
                        if (decode_srgb)
                                srgb = util_format_is_srgb(pformat);
                        else
@@ -94,15 +89,17 @@ emit_mrt(struct fd_ringbuffer *ring, unsigned nr_bufs,
 
                        offset = fd_resource_offset(rsc, psurf->u.tex.level,
                                        psurf->u.tex.first_layer);
+                       swap = rsc->layout.tile_mode ? WZYX : fd3_pipe2swap(pformat);
 
                        if (bin_w) {
-                               stride = bin_w * rsc->cpp;
+                               stride = bin_w << fdl_cpp_shift(&rsc->layout);
 
                                if (bases) {
                                        base = bases[i];
                                }
                        } else {
-                               stride = slice->pitch * rsc->cpp;
+                               stride = fd_resource_pitch(rsc, psurf->u.tex.level);
+                               tile_mode = rsc->layout.tile_mode;
                        }
                } else if (i < nr_bufs && bases) {
                        base = bases[i];
@@ -117,7 +114,7 @@ emit_mrt(struct fd_ringbuffer *ring, unsigned nr_bufs,
                if (bin_w || (i >= nr_bufs) || !bufs[i]) {
                        OUT_RING(ring, A3XX_RB_MRT_BUF_BASE_COLOR_BUF_BASE(base));
                } else {
-                       OUT_RELOCW(ring, rsc->bo, offset, 0, -1);
+                       OUT_RELOC(ring, rsc->bo, offset, 0, -1);
                }
 
                OUT_PKT0(ring, REG_A3XX_SP_FS_IMAGE_OUTPUT_REG(i), 1);
@@ -130,7 +127,7 @@ emit_mrt(struct fd_ringbuffer *ring, unsigned nr_bufs,
 static bool
 use_hw_binning(struct fd_batch *batch)
 {
-       struct fd_gmem_stateobj *gmem = &batch->ctx->gmem;
+       const struct fd_gmem_stateobj *gmem = batch->gmem_state;
 
        /* workaround: combining scissor optimization and hw binning
         * seems problematic.  Seems like we end up with a mismatch
@@ -149,6 +146,12 @@ use_hw_binning(struct fd_batch *batch)
        if (gmem->minx || gmem->miny)
                return false;
 
+       if ((gmem->maxpw * gmem->maxph) > 32)
+               return false;
+
+       if ((gmem->maxpw > 15) || (gmem->maxph > 15))
+               return false;
+
        return fd_binning_enabled && ((gmem->nbins_x * gmem->nbins_y) > 2);
 }
 
@@ -158,16 +161,12 @@ static void
 emit_binning_workaround(struct fd_batch *batch)
 {
        struct fd_context *ctx = batch->ctx;
-       struct fd3_context *fd3_ctx = fd3_context(ctx);
-       struct fd_gmem_stateobj *gmem = &ctx->gmem;
+       const struct fd_gmem_stateobj *gmem = batch->gmem_state;
        struct fd_ringbuffer *ring = batch->gmem;
        struct fd3_emit emit = {
                        .debug = &ctx->debug,
-                       .vtx = &fd3_ctx->solid_vbuf_state,
+                       .vtx = &ctx->solid_vbuf_state,
                        .prog = &ctx->solid_prog,
-                       .key = {
-                               .half_precision = true,
-                       },
        };
 
        OUT_PKT0(ring, REG_A3XX_RB_MODE_CONTROL, 2);
@@ -182,7 +181,7 @@ emit_binning_workaround(struct fd_batch *batch)
        OUT_RING(ring, A3XX_RB_COPY_CONTROL_MSAA_RESOLVE(MSAA_ONE) |
                        A3XX_RB_COPY_CONTROL_MODE(0) |
                        A3XX_RB_COPY_CONTROL_GMEM_BASE(0));
-       OUT_RELOCW(ring, fd_resource(fd3_ctx->solid_vbuf)->bo, 0x20, 0, -1);  /* RB_COPY_DEST_BASE */
+       OUT_RELOC(ring, fd_resource(ctx->solid_vbuf)->bo, 0x20, 0, -1);  /* RB_COPY_DEST_BASE */
        OUT_RING(ring, A3XX_RB_COPY_DEST_PITCH_PITCH(128));
        OUT_RING(ring, A3XX_RB_COPY_DEST_INFO_TILE(LINEAR) |
                        A3XX_RB_COPY_DEST_INFO_FORMAT(RB_R8G8B8A8_UNORM) |
@@ -257,7 +256,7 @@ emit_binning_workaround(struct fd_batch *batch)
        OUT_RING(ring, A3XX_GRAS_SC_SCREEN_SCISSOR_BR_X(31) |
                        A3XX_GRAS_SC_SCREEN_SCISSOR_BR_Y(0));
 
-       fd_wfi(ctx, ring);
+       fd_wfi(batch, ring);
        OUT_PKT0(ring, REG_A3XX_GRAS_CL_VPORT_XOFFSET, 6);
        OUT_RING(ring, A3XX_GRAS_CL_VPORT_XOFFSET(0.0));
        OUT_RING(ring, A3XX_GRAS_CL_VPORT_XSCALE(1.0));
@@ -284,7 +283,7 @@ emit_binning_workaround(struct fd_batch *batch)
        OUT_RING(ring, 2);            /* NumIndices */
        OUT_RING(ring, 2);
        OUT_RING(ring, 1);
-       fd_reset_wfi(ctx);
+       fd_reset_wfi(batch);
 
        OUT_PKT0(ring, REG_A3XX_HLSQ_CONTROL_0_REG, 1);
        OUT_RING(ring, A3XX_HLSQ_CONTROL_0_REG_FSTHREADSIZE(TWO_QUADS));
@@ -292,7 +291,7 @@ emit_binning_workaround(struct fd_batch *batch)
        OUT_PKT0(ring, REG_A3XX_VFD_PERFCOUNTER0_SELECT, 1);
        OUT_RING(ring, 0x00000000);
 
-       fd_wfi(ctx, ring);
+       fd_wfi(batch, ring);
        OUT_PKT0(ring, REG_A3XX_VSC_BIN_SIZE, 1);
        OUT_RING(ring, A3XX_VSC_BIN_SIZE_WIDTH(gmem->bin_w) |
                        A3XX_VSC_BIN_SIZE_HEIGHT(gmem->bin_h));
@@ -317,13 +316,18 @@ emit_gmem2mem_surf(struct fd_batch *batch,
        struct fd_ringbuffer *ring = batch->gmem;
        struct fd_resource *rsc = fd_resource(psurf->texture);
        enum pipe_format format = psurf->format;
+
+       if (!rsc->valid)
+               return;
+
        if (stencil) {
                rsc = rsc->stencil;
-               format = rsc->base.b.format;
+               format = rsc->base.format;
        }
-       struct fd_resource_slice *slice = fd_resource_slice(rsc, psurf->u.tex.level);
+
        uint32_t offset = fd_resource_offset(rsc, psurf->u.tex.level,
                        psurf->u.tex.first_layer);
+       uint32_t pitch = fd_resource_pitch(rsc, psurf->u.tex.level);
 
        debug_assert(psurf->u.tex.first_layer == psurf->u.tex.last_layer);
 
@@ -333,11 +337,11 @@ emit_gmem2mem_surf(struct fd_batch *batch,
                        A3XX_RB_COPY_CONTROL_GMEM_BASE(base) |
                        COND(format == PIPE_FORMAT_Z32_FLOAT ||
                                 format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT,
-                                A3XX_RB_COPY_CONTROL_UNK12));
+                                A3XX_RB_COPY_CONTROL_DEPTH32_RESOLVE));
 
-       OUT_RELOCW(ring, rsc->bo, offset, 0, -1);    /* RB_COPY_DEST_BASE */
-       OUT_RING(ring, A3XX_RB_COPY_DEST_PITCH_PITCH(slice->pitch * rsc->cpp));
-       OUT_RING(ring, A3XX_RB_COPY_DEST_INFO_TILE(LINEAR) |
+       OUT_RELOC(ring, rsc->bo, offset, 0, -1);    /* RB_COPY_DEST_BASE */
+       OUT_RING(ring, A3XX_RB_COPY_DEST_PITCH_PITCH(pitch));
+       OUT_RING(ring, A3XX_RB_COPY_DEST_INFO_TILE(rsc->layout.tile_mode) |
                        A3XX_RB_COPY_DEST_INFO_FORMAT(fd3_pipe2color(format)) |
                        A3XX_RB_COPY_DEST_INFO_COMPONENT_ENABLE(0xf) |
                        A3XX_RB_COPY_DEST_INFO_ENDIAN(ENDIAN_NONE) |
@@ -348,19 +352,16 @@ emit_gmem2mem_surf(struct fd_batch *batch,
 }
 
 static void
-fd3_emit_tile_gmem2mem(struct fd_batch *batch, struct fd_tile *tile)
+fd3_emit_tile_gmem2mem(struct fd_batch *batch, const struct fd_tile *tile)
 {
        struct fd_context *ctx = batch->ctx;
-       struct fd3_context *fd3_ctx = fd3_context(ctx);
        struct fd_ringbuffer *ring = batch->gmem;
+       const struct fd_gmem_stateobj *gmem = batch->gmem_state;
        struct pipe_framebuffer_state *pfb = &batch->framebuffer;
        struct fd3_emit emit = {
                        .debug = &ctx->debug,
-                       .vtx = &fd3_ctx->solid_vbuf_state,
+                       .vtx = &ctx->solid_vbuf_state,
                        .prog = &ctx->solid_prog,
-                       .key = {
-                               .half_precision = true,
-                       },
        };
        int i;
 
@@ -393,7 +394,7 @@ fd3_emit_tile_gmem2mem(struct fd_batch *batch, struct fd_tile *tile)
        OUT_PKT0(ring, REG_A3XX_GRAS_CL_CLIP_CNTL, 1);
        OUT_RING(ring, 0x00000000);   /* GRAS_CL_CLIP_CNTL */
 
-       fd_wfi(ctx, ring);
+       fd_wfi(batch, ring);
        OUT_PKT0(ring, REG_A3XX_GRAS_CL_VPORT_XOFFSET, 6);
        OUT_RING(ring, A3XX_GRAS_CL_VPORT_XOFFSET((float)pfb->width/2.0 - 0.5));
        OUT_RING(ring, A3XX_GRAS_CL_VPORT_XSCALE((float)pfb->width/2.0));
@@ -411,7 +412,7 @@ fd3_emit_tile_gmem2mem(struct fd_batch *batch, struct fd_tile *tile)
        OUT_RING(ring, A3XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE |
                        A3XX_RB_RENDER_CONTROL_ENABLE_GMEM |
                        A3XX_RB_RENDER_CONTROL_ALPHA_TEST_FUNC(FUNC_NEVER) |
-                       A3XX_RB_RENDER_CONTROL_BIN_WIDTH(ctx->gmem.bin_w));
+                       A3XX_RB_RENDER_CONTROL_BIN_WIDTH(batch->gmem_state->bin_w));
 
        OUT_PKT0(ring, REG_A3XX_GRAS_SC_CONTROL, 1);
        OUT_RING(ring, A3XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RESOLVE_PASS) |
@@ -443,10 +444,10 @@ fd3_emit_tile_gmem2mem(struct fd_batch *batch, struct fd_tile *tile)
                struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
                if (!rsc->stencil || batch->resolve & FD_BUFFER_DEPTH)
                        emit_gmem2mem_surf(batch, RB_COPY_DEPTH_STENCIL, false,
-                                                          ctx->gmem.zsbuf_base[0], pfb->zsbuf);
+                                                          gmem->zsbuf_base[0], pfb->zsbuf);
                if (rsc->stencil && batch->resolve & FD_BUFFER_STENCIL)
                        emit_gmem2mem_surf(batch, RB_COPY_DEPTH_STENCIL, true,
-                                                          ctx->gmem.zsbuf_base[1], pfb->zsbuf);
+                                                          gmem->zsbuf_base[1], pfb->zsbuf);
        }
 
        if (batch->resolve & FD_BUFFER_COLOR) {
@@ -456,7 +457,7 @@ fd3_emit_tile_gmem2mem(struct fd_batch *batch, struct fd_tile *tile)
                        if (!(batch->resolve & (PIPE_CLEAR_COLOR0 << i)))
                                continue;
                        emit_gmem2mem_surf(batch, RB_COPY_RESOLVE, false,
-                                                          ctx->gmem.cbuf_base[i], pfb->cbufs[i]);
+                                                          gmem->cbuf_base[i], pfb->cbufs[i]);
                }
        }
 
@@ -474,7 +475,7 @@ fd3_emit_tile_gmem2mem(struct fd_batch *batch, struct fd_tile *tile)
 /* transfer from system memory to gmem */
 
 static void
-emit_mem2gmem_surf(struct fd_batch *batch, uint32_t bases[],
+emit_mem2gmem_surf(struct fd_batch *batch, const uint32_t bases[],
                struct pipe_surface **psurf, uint32_t bufs, uint32_t bin_w)
 {
        struct fd_ringbuffer *ring = batch->gmem;
@@ -504,7 +505,7 @@ emit_mem2gmem_surf(struct fd_batch *batch, uint32_t bases[],
                OUT_PKT0(ring, REG_A3XX_RB_DEPTH_INFO, 2);
                OUT_RING(ring, A3XX_RB_DEPTH_INFO_DEPTH_BASE(bases[0]) |
                                 A3XX_RB_DEPTH_INFO_DEPTH_FORMAT(DEPTHX_32));
-               OUT_RING(ring, A3XX_RB_DEPTH_PITCH(4 * batch->ctx->gmem.bin_w));
+               OUT_RING(ring, A3XX_RB_DEPTH_PITCH(4 * batch->gmem_state->bin_w));
 
                if (psurf[0]->format == PIPE_FORMAT_Z32_FLOAT) {
                        OUT_PKT0(ring, REG_A3XX_RB_MRT_CONTROL(0), 1);
@@ -530,22 +531,18 @@ emit_mem2gmem_surf(struct fd_batch *batch, uint32_t bases[],
 }
 
 static void
-fd3_emit_tile_mem2gmem(struct fd_batch *batch, struct fd_tile *tile)
+fd3_emit_tile_mem2gmem(struct fd_batch *batch, const struct fd_tile *tile)
 {
        struct fd_context *ctx = batch->ctx;
-       struct fd3_context *fd3_ctx = fd3_context(ctx);
-       struct fd_gmem_stateobj *gmem = &ctx->gmem;
+       const struct fd_gmem_stateobj *gmem = batch->gmem_state;
        struct fd_ringbuffer *ring = batch->gmem;
        struct pipe_framebuffer_state *pfb = &batch->framebuffer;
        struct fd3_emit emit = {
                        .debug = &ctx->debug,
-                       .vtx = &fd3_ctx->blit_vbuf_state,
+                       .vtx = &ctx->blit_vbuf_state,
                        .sprite_coord_enable = 1,
                        /* NOTE: They all use the same VP, this is for vtx bufs. */
                        .prog = &ctx->blit_prog[0],
-                       .key = {
-                               .half_precision = fd_half_precision(pfb),
-                       },
        };
        float x0, y0, x1, y1;
        unsigned bin_w = tile->bin_w;
@@ -559,13 +556,13 @@ fd3_emit_tile_mem2gmem(struct fd_batch *batch, struct fd_tile *tile)
        y1 = ((float)tile->yoff + bin_h) / ((float)pfb->height);
 
        OUT_PKT3(ring, CP_MEM_WRITE, 5);
-       OUT_RELOCW(ring, fd_resource(fd3_ctx->blit_texcoord_vbuf)->bo, 0, 0, 0);
+       OUT_RELOC(ring, fd_resource(ctx->blit_texcoord_vbuf)->bo, 0, 0, 0);
        OUT_RING(ring, fui(x0));
        OUT_RING(ring, fui(y0));
        OUT_RING(ring, fui(x1));
        OUT_RING(ring, fui(y1));
 
-       fd3_emit_cache_flush(ctx, ring);
+       fd3_emit_cache_flush(batch, ring);
 
        for (i = 0; i < 4; i++) {
                OUT_PKT0(ring, REG_A3XX_RB_MRT_CONTROL(i), 1);
@@ -586,7 +583,7 @@ fd3_emit_tile_mem2gmem(struct fd_batch *batch, struct fd_tile *tile)
        OUT_RING(ring, A3XX_RB_RENDER_CONTROL_ALPHA_TEST_FUNC(FUNC_ALWAYS) |
                        A3XX_RB_RENDER_CONTROL_BIN_WIDTH(gmem->bin_w));
 
-       fd_wfi(ctx, ring);
+       fd_wfi(batch, ring);
        OUT_PKT0(ring, REG_A3XX_RB_DEPTH_CONTROL, 1);
        OUT_RING(ring, A3XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_LESS));
 
@@ -597,7 +594,7 @@ fd3_emit_tile_mem2gmem(struct fd_batch *batch, struct fd_tile *tile)
        OUT_PKT0(ring, REG_A3XX_GRAS_CL_CLIP_CNTL, 1);
        OUT_RING(ring, A3XX_GRAS_CL_CLIP_CNTL_IJ_PERSP_CENTER);   /* GRAS_CL_CLIP_CNTL */
 
-       fd_wfi(ctx, ring);
+       fd_wfi(batch, ring);
        OUT_PKT0(ring, REG_A3XX_GRAS_CL_VPORT_XOFFSET, 6);
        OUT_RING(ring, A3XX_GRAS_CL_VPORT_XOFFSET((float)bin_w/2.0 - 0.5));
        OUT_RING(ring, A3XX_GRAS_CL_VPORT_XSCALE((float)bin_w/2.0));
@@ -660,7 +657,7 @@ fd3_emit_tile_mem2gmem(struct fd_batch *batch, struct fd_tile *tile)
 
        if (fd_gmem_needs_restore(batch, tile, FD_BUFFER_COLOR)) {
                emit.prog = &ctx->blit_prog[pfb->nr_cbufs - 1];
-               emit.fp = NULL;      /* frag shader changed so clear cache */
+               emit.fs = NULL;      /* frag shader changed so clear cache */
                fd3_program_emit(ring, &emit, pfb->nr_cbufs, pfb->cbufs);
                emit_mem2gmem_surf(batch, gmem->cbuf_base, pfb->cbufs, pfb->nr_cbufs, bin_w);
        }
@@ -672,16 +669,14 @@ fd3_emit_tile_mem2gmem(struct fd_batch *batch, struct fd_tile *tile)
                         * components, so half precision is always sufficient.
                         */
                        emit.prog = &ctx->blit_prog[0];
-                       emit.key.half_precision = true;
                } else {
                        /* Float depth needs special blit shader that writes depth */
                        if (pfb->zsbuf->format == PIPE_FORMAT_Z32_FLOAT)
                                emit.prog = &ctx->blit_z;
                        else
                                emit.prog = &ctx->blit_zs;
-                       emit.key.half_precision = false;
                }
-               emit.fp = NULL;      /* frag shader changed so clear cache */
+               emit.fs = NULL;      /* frag shader changed so clear cache */
                fd3_program_emit(ring, &emit, 1, &pfb->zsbuf);
                emit_mem2gmem_surf(batch, gmem->zsbuf_base, &pfb->zsbuf, 1, bin_w);
        }
@@ -705,7 +700,7 @@ patch_draws(struct fd_batch *batch, enum pc_di_vis_cull_mode vismode)
                struct fd_cs_patch *patch = fd_patch_element(&batch->draw_patches, i);
                *patch->cs = patch->val | DRAW(0, 0, 0, vismode, 0);
        }
-       util_dynarray_resize(&batch->draw_patches, 0);
+       util_dynarray_clear(&batch->draw_patches);
 }
 
 static void
@@ -716,7 +711,7 @@ patch_rbrc(struct fd_batch *batch, uint32_t val)
                struct fd_cs_patch *patch = fd_patch_element(&batch->rbrc_patches, i);
                *patch->cs = patch->val | val;
        }
-       util_dynarray_resize(&batch->rbrc_patches, 0);
+       util_dynarray_clear(&batch->rbrc_patches);
 }
 
 /* for rendering directly to system memory: */
@@ -731,10 +726,11 @@ fd3_emit_sysmem_prep(struct fd_batch *batch)
                struct pipe_surface *psurf = pfb->cbufs[i];
                if (!psurf)
                        continue;
-               pitch = fd_resource(psurf->texture)->slices[psurf->u.tex.level].pitch;
+               struct fd_resource *rsc = fd_resource(psurf->texture);
+               pitch = fd_resource_pitch(rsc, psurf->u.tex.level) / rsc->layout.cpp;
        }
 
-       fd3_emit_restore(batch->ctx, ring);
+       fd3_emit_restore(batch, ring);
 
        OUT_PKT0(ring, REG_A3XX_RB_FRAME_BUFFER_DIMENSION, 1);
        OUT_RING(ring, A3XX_RB_FRAME_BUFFER_DIMENSION_WIDTH(pfb->width) |
@@ -767,19 +763,20 @@ static void
 update_vsc_pipe(struct fd_batch *batch)
 {
        struct fd_context *ctx = batch->ctx;
+       const struct fd_gmem_stateobj *gmem = batch->gmem_state;
        struct fd3_context *fd3_ctx = fd3_context(ctx);
        struct fd_ringbuffer *ring = batch->gmem;
        int i;
 
        OUT_PKT0(ring, REG_A3XX_VSC_SIZE_ADDRESS, 1);
-       OUT_RELOCW(ring, fd3_ctx->vsc_size_mem, 0, 0, 0); /* VSC_SIZE_ADDRESS */
+       OUT_RELOC(ring, fd3_ctx->vsc_size_mem, 0, 0, 0); /* VSC_SIZE_ADDRESS */
 
        for (i = 0; i < 8; i++) {
-               struct fd_vsc_pipe *pipe = &ctx->pipe[i];
+               const struct fd_vsc_pipe *pipe = &gmem->vsc_pipe[i];
 
-               if (!pipe->bo) {
-                       pipe->bo = fd_bo_new(ctx->dev, 0x40000,
-                                       DRM_FREEDRENO_GEM_TYPE_KMEM);
+               if (!ctx->vsc_pipe_bo[i]) {
+                       ctx->vsc_pipe_bo[i] = fd_bo_new(ctx->dev, 0x40000,
+                                       DRM_FREEDRENO_GEM_TYPE_KMEM, "vsc_pipe[%u]", i);
                }
 
                OUT_PKT0(ring, REG_A3XX_VSC_PIPE(i), 3);
@@ -787,8 +784,8 @@ update_vsc_pipe(struct fd_batch *batch)
                                A3XX_VSC_PIPE_CONFIG_Y(pipe->y) |
                                A3XX_VSC_PIPE_CONFIG_W(pipe->w) |
                                A3XX_VSC_PIPE_CONFIG_H(pipe->h));
-               OUT_RELOCW(ring, pipe->bo, 0, 0, 0);       /* VSC_PIPE[i].DATA_ADDRESS */
-               OUT_RING(ring, fd_bo_size(pipe->bo) - 32); /* VSC_PIPE[i].DATA_LENGTH */
+               OUT_RELOC(ring, ctx->vsc_pipe_bo[i], 0, 0, 0);       /* VSC_PIPE[i].DATA_ADDRESS */
+               OUT_RING(ring, fd_bo_size(ctx->vsc_pipe_bo[i]) - 32); /* VSC_PIPE[i].DATA_LENGTH */
        }
 }
 
@@ -796,7 +793,7 @@ static void
 emit_binning_pass(struct fd_batch *batch)
 {
        struct fd_context *ctx = batch->ctx;
-       struct fd_gmem_stateobj *gmem = &ctx->gmem;
+       const struct fd_gmem_stateobj *gmem = batch->gmem_state;
        struct pipe_framebuffer_state *pfb = &batch->framebuffer;
        struct fd_ringbuffer *ring = batch->gmem;
        int i;
@@ -808,7 +805,7 @@ emit_binning_pass(struct fd_batch *batch)
 
        if (ctx->screen->gpu_id == 320) {
                emit_binning_workaround(batch);
-               fd_wfi(ctx, ring);
+               fd_wfi(batch, ring);
                OUT_PKT3(ring, CP_INVALIDATE_STATE, 1);
                OUT_RING(ring, 0x00007fff);
        }
@@ -861,10 +858,10 @@ emit_binning_pass(struct fd_batch *batch)
                        A3XX_PC_VSTREAM_CONTROL_N(0));
 
        /* emit IB to binning drawcmds: */
-       ctx->emit_ib(ring, batch->binning);
-       fd_reset_wfi(ctx);
+       fd3_emit_ib(ring, batch->binning);
+       fd_reset_wfi(batch);
 
-       fd_wfi(ctx, ring);
+       fd_wfi(batch, ring);
 
        /* and then put stuff back the way it was: */
 
@@ -893,8 +890,8 @@ emit_binning_pass(struct fd_batch *batch)
                        A3XX_RB_RENDER_CONTROL_ALPHA_TEST_FUNC(FUNC_NEVER) |
                        A3XX_RB_RENDER_CONTROL_BIN_WIDTH(gmem->bin_w));
 
-       fd_event_write(ctx, ring, CACHE_FLUSH);
-       fd_wfi(ctx, ring);
+       fd_event_write(batch, ring, CACHE_FLUSH);
+       fd_wfi(batch, ring);
 
        if (ctx->screen->gpu_id == 320) {
                /* dummy-draw workaround: */
@@ -903,7 +900,7 @@ emit_binning_pass(struct fd_batch *batch)
                OUT_RING(ring, DRAW(1, DI_SRC_SEL_AUTO_INDEX,
                                                        INDEX_SIZE_IGN, IGNORE_VISIBILITY, 0));
                OUT_RING(ring, 0);             /* NumIndices */
-               fd_reset_wfi(ctx);
+               fd_reset_wfi(batch);
        }
 
        OUT_PKT3(ring, CP_NOP, 4);
@@ -912,7 +909,7 @@ emit_binning_pass(struct fd_batch *batch)
        OUT_RING(ring, 0x00000000);
        OUT_RING(ring, 0x00000000);
 
-       fd_wfi(ctx, ring);
+       fd_wfi(batch, ring);
 
        if (ctx->screen->gpu_id == 320) {
                emit_binning_workaround(batch);
@@ -924,10 +921,11 @@ static void
 fd3_emit_tile_init(struct fd_batch *batch)
 {
        struct fd_ringbuffer *ring = batch->gmem;
-       struct fd_gmem_stateobj *gmem = &batch->ctx->gmem;
+       struct pipe_framebuffer_state *pfb = &batch->framebuffer;
+       const struct fd_gmem_stateobj *gmem = batch->gmem_state;
        uint32_t rb_render_control;
 
-       fd3_emit_restore(batch->ctx, ring);
+       fd3_emit_restore(batch, ring);
 
        /* note: use gmem->bin_w/h, the bin_w/h parameters may be truncated
         * at the right and bottom edge tiles
@@ -938,6 +936,11 @@ fd3_emit_tile_init(struct fd_batch *batch)
 
        update_vsc_pipe(batch);
 
+       fd_wfi(batch, ring);
+       OUT_PKT0(ring, REG_A3XX_RB_FRAME_BUFFER_DIMENSION, 1);
+       OUT_RING(ring, A3XX_RB_FRAME_BUFFER_DIMENSION_WIDTH(pfb->width) |
+                       A3XX_RB_FRAME_BUFFER_DIMENSION_HEIGHT(pfb->height));
+
        if (use_hw_binning(batch)) {
                /* emit hw binning pass: */
                emit_binning_pass(batch);
@@ -955,20 +958,11 @@ fd3_emit_tile_init(struct fd_batch *batch)
 
 /* before mem2gmem */
 static void
-fd3_emit_tile_prep(struct fd_batch *batch, struct fd_tile *tile)
+fd3_emit_tile_prep(struct fd_batch *batch, const struct fd_tile *tile)
 {
-       struct fd_context *ctx = batch->ctx;
        struct fd_ringbuffer *ring = batch->gmem;
        struct pipe_framebuffer_state *pfb = &batch->framebuffer;
 
-       if (ctx->needs_rb_fbd) {
-               fd_wfi(ctx, ring);
-               OUT_PKT0(ring, REG_A3XX_RB_FRAME_BUFFER_DIMENSION, 1);
-               OUT_RING(ring, A3XX_RB_FRAME_BUFFER_DIMENSION_WIDTH(pfb->width) |
-                               A3XX_RB_FRAME_BUFFER_DIMENSION_HEIGHT(pfb->height));
-               ctx->needs_rb_fbd = false;
-       }
-
        OUT_PKT0(ring, REG_A3XX_RB_MODE_CONTROL, 1);
        OUT_RING(ring, A3XX_RB_MODE_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
                        A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE |
@@ -977,12 +971,12 @@ fd3_emit_tile_prep(struct fd_batch *batch, struct fd_tile *tile)
 
 /* before IB to rendering cmds: */
 static void
-fd3_emit_tile_renderprep(struct fd_batch *batch, struct fd_tile *tile)
+fd3_emit_tile_renderprep(struct fd_batch *batch, const struct fd_tile *tile)
 {
        struct fd_context *ctx = batch->ctx;
        struct fd3_context *fd3_ctx = fd3_context(ctx);
        struct fd_ringbuffer *ring = batch->gmem;
-       struct fd_gmem_stateobj *gmem = &ctx->gmem;
+       const struct fd_gmem_stateobj *gmem = batch->gmem_state;
        struct pipe_framebuffer_state *pfb = &batch->framebuffer;
 
        uint32_t x1 = tile->xoff;
@@ -1000,23 +994,26 @@ fd3_emit_tile_renderprep(struct fd_batch *batch, struct fd_tile *tile)
        OUT_RING(ring, reg);
        if (pfb->zsbuf) {
                struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
-               OUT_RING(ring, A3XX_RB_DEPTH_PITCH(rsc->cpp * gmem->bin_w));
+               OUT_RING(ring, A3XX_RB_DEPTH_PITCH(gmem->bin_w <<
+                                               fdl_cpp_shift(&rsc->layout)));
                if (rsc->stencil) {
                        OUT_PKT0(ring, REG_A3XX_RB_STENCIL_INFO, 2);
                        OUT_RING(ring, A3XX_RB_STENCIL_INFO_STENCIL_BASE(gmem->zsbuf_base[1]));
-                       OUT_RING(ring, A3XX_RB_STENCIL_PITCH(rsc->stencil->cpp * gmem->bin_w));
+                       OUT_RING(ring, A3XX_RB_STENCIL_PITCH(gmem->bin_w <<
+                                                       fdl_cpp_shift(&rsc->stencil->layout)));
                }
        } else {
                OUT_RING(ring, 0x00000000);
        }
 
        if (use_hw_binning(batch)) {
-               struct fd_vsc_pipe *pipe = &ctx->pipe[tile->p];
+               const struct fd_vsc_pipe *pipe = &gmem->vsc_pipe[tile->p];
+               struct fd_bo *pipe_bo = ctx->vsc_pipe_bo[tile->p];
 
-               assert(pipe->w * pipe->h);
+               assert(pipe->w && pipe->h);
 
-               fd_event_write(ctx, ring, HLSQ_FLUSH);
-               fd_wfi(ctx, ring);
+               fd_event_write(batch, ring, HLSQ_FLUSH);
+               fd_wfi(batch, ring);
 
                OUT_PKT0(ring, REG_A3XX_PC_VSTREAM_CONTROL, 1);
                OUT_RING(ring, A3XX_PC_VSTREAM_CONTROL_SIZE(pipe->w * pipe->h) |
@@ -1024,8 +1021,8 @@ fd3_emit_tile_renderprep(struct fd_batch *batch, struct fd_tile *tile)
 
 
                OUT_PKT3(ring, CP_SET_BIN_DATA, 2);
-               OUT_RELOCW(ring, pipe->bo, 0, 0, 0);    /* BIN_DATA_ADDR <- VSC_PIPE[p].DATA_ADDRESS */
-               OUT_RELOCW(ring, fd3_ctx->vsc_size_mem, /* BIN_SIZE_ADDR <- VSC_SIZE_ADDRESS + (p * 4) */
+               OUT_RELOC(ring, pipe_bo, 0, 0, 0);     /* BIN_DATA_ADDR <- VSC_PIPE[p].DATA_ADDRESS */
+               OUT_RELOC(ring, fd3_ctx->vsc_size_mem, /* BIN_SIZE_ADDR <- VSC_SIZE_ADDRESS + (p * 4) */
                                (tile->p * 4), 0, 0);
        } else {
                OUT_PKT0(ring, REG_A3XX_PC_VSTREAM_CONTROL, 1);