freedreno: Rename vp and fp to vs and fs in fd_program_stateobj
[mesa.git] / src / gallium / drivers / freedreno / a4xx / fd4_gmem.c
index f3d7b1064dd3b1c48bf2c991dbcbecedbde45f1d..663a49c69c88822c9cf2d8d412821186ad3cedb3 100644 (file)
@@ -1,5 +1,3 @@
-/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
-
 /*
  * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
  *
 #include "fd4_draw.h"
 #include "fd4_emit.h"
 #include "fd4_program.h"
-#include "fd4_util.h"
+#include "fd4_format.h"
 #include "fd4_zsa.h"
 
-static const struct ir3_shader_key key = {
-               // XXX should set this based on render target format!  We don't
-               // want half_precision if float32 render target!!!
-               .half_precision = true,
-};
-
 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, uint32_t *bases,
+               uint32_t bin_w, bool decode_srgb)
 {
+       enum a4xx_tile_mode tile_mode;
        unsigned i;
 
-       for (i = 0; i < 8; i++) {
+       if (bin_w) {
+               tile_mode = 2;
+       } else {
+               tile_mode = TILE4_LINEAR;
+       }
+
+       for (i = 0; i < A4XX_MAX_RENDER_TARGETS; i++) {
                enum a4xx_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 layer_offset = 0;
+               uint32_t offset = 0;
 
                if ((i < nr_bufs) && bufs[i]) {
                        struct pipe_surface *psurf = bufs[i];
+                       enum pipe_format pformat = psurf->format;
 
                        rsc = fd_resource(psurf->texture);
-                       slice = &rsc->slices[psurf->u.tex.level];
-                       format = fd4_pipe2color(psurf->format);
-                       swap = fd4_pipe2swap(psurf->format);
+
+                       /* In case we're drawing to Z32F_S8, the "color" actually goes to
+                        * the stencil
+                        */
+                       if (rsc->stencil) {
+                               rsc = rsc->stencil;
+                               pformat = rsc->base.format;
+                               if (bases)
+                                       bases++;
+                       }
+
+                       slice = fd_resource_slice(rsc, psurf->u.tex.level);
+                       format = fd4_pipe2color(pformat);
+                       swap = fd4_pipe2swap(pformat);
+
+                       if (decode_srgb)
+                               srgb = util_format_is_srgb(pformat);
+                       else
+                               pformat = util_format_linear(pformat);
 
                        debug_assert(psurf->u.tex.first_layer == psurf->u.tex.last_layer);
 
-                       layer_offset = slice->size0 * psurf->u.tex.first_layer;
+                       offset = fd_resource_offset(rsc, psurf->u.tex.level,
+                                       psurf->u.tex.first_layer);
 
                        if (bin_w) {
                                stride = bin_w * rsc->cpp;
@@ -86,80 +105,107 @@ emit_mrt(struct fd_ringbuffer *ring, unsigned nr_bufs,
                        } else {
                                stride = slice->pitch * rsc->cpp;
                        }
+               } else if ((i < nr_bufs) && bases) {
+                       base = bases[i];
                }
 
                OUT_PKT0(ring, REG_A4XX_RB_MRT_BUF_INFO(i), 3);
                OUT_RING(ring, A4XX_RB_MRT_BUF_INFO_COLOR_FORMAT(format) |
-                               0x80 | /* XXX not on gmem2mem?? tile-mode? */
+                               A4XX_RB_MRT_BUF_INFO_COLOR_TILE_MODE(tile_mode) |
                                A4XX_RB_MRT_BUF_INFO_COLOR_BUF_PITCH(stride) |
-                               A4XX_RB_MRT_BUF_INFO_COLOR_SWAP(swap));
-               if (bin_w || (i >= nr_bufs)) {
+                               A4XX_RB_MRT_BUF_INFO_COLOR_SWAP(swap) |
+                               COND(srgb, A4XX_RB_MRT_BUF_INFO_COLOR_SRGB));
+               if (bin_w || (i >= nr_bufs) || !bufs[i]) {
                        OUT_RING(ring, base);
+                       OUT_RING(ring, A4XX_RB_MRT_CONTROL3_STRIDE(stride));
                } else {
-                       OUT_RELOCW(ring, rsc->bo,
-                                       slice->offset + layer_offset, 0, -1);
+                       OUT_RELOCW(ring, rsc->bo, offset, 0, 0);
+                       /* RB_MRT[i].CONTROL3.STRIDE not emitted by c2d..
+                        * not sure if we need to skip it for bypass or
+                        * not.
+                        */
+                       OUT_RING(ring, A4XX_RB_MRT_CONTROL3_STRIDE(0));
                }
-               OUT_RING(ring, A4XX_RB_MRT_CONTROL3_STRIDE(stride));
        }
 }
 
-static uint32_t
-depth_base(struct fd_context *ctx)
+static bool
+use_hw_binning(struct fd_batch *batch)
 {
-       struct fd_gmem_stateobj *gmem = &ctx->gmem;
-       struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
-       uint32_t cpp = 4;
-       if (pfb->cbufs[0]) {
-               struct fd_resource *rsc =
-                               fd_resource(pfb->cbufs[0]->texture);
-               cpp = rsc->cpp;
-       }
-       return align(gmem->bin_w * gmem->bin_h * cpp, 0x4000);
+       struct fd_gmem_stateobj *gmem = &batch->ctx->gmem;
+
+       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);
 }
 
 /* transfer from gmem to system memory (ie. normal RAM) */
 
 static void
-emit_gmem2mem_surf(struct fd_context *ctx,
+emit_gmem2mem_surf(struct fd_batch *batch, bool stencil,
                uint32_t base, struct pipe_surface *psurf)
 {
-       struct fd_ringbuffer *ring = ctx->ring;
+       struct fd_ringbuffer *ring = batch->gmem;
        struct fd_resource *rsc = fd_resource(psurf->texture);
-       struct fd_resource_slice *slice = &rsc->slices[psurf->u.tex.level];
+       enum pipe_format pformat = psurf->format;
+       struct fd_resource_slice *slice;
+       uint32_t offset;
+
+       if (!rsc->valid)
+               return;
+
+       if (stencil) {
+               debug_assert(rsc->stencil);
+               rsc = rsc->stencil;
+               pformat = rsc->base.format;
+       }
+
+       slice = &rsc->slices[psurf->u.tex.level];
+       offset = fd_resource_offset(rsc, psurf->u.tex.level,
+                       psurf->u.tex.first_layer);
+
+       debug_assert(psurf->u.tex.first_layer == psurf->u.tex.last_layer);
 
        OUT_PKT0(ring, REG_A4XX_RB_COPY_CONTROL, 4);
        OUT_RING(ring, A4XX_RB_COPY_CONTROL_MSAA_RESOLVE(MSAA_ONE) |
                        A4XX_RB_COPY_CONTROL_MODE(RB_COPY_RESOLVE) |
                        A4XX_RB_COPY_CONTROL_GMEM_BASE(base));
-       OUT_RELOCW(ring, rsc->bo, slice->offset, 0, 0);   /* RB_COPY_DEST_BASE */
+       OUT_RELOCW(ring, rsc->bo, offset, 0, 0);   /* RB_COPY_DEST_BASE */
        OUT_RING(ring, A4XX_RB_COPY_DEST_PITCH_PITCH(slice->pitch * rsc->cpp));
        OUT_RING(ring, A4XX_RB_COPY_DEST_INFO_TILE(TILE4_LINEAR) |
-                       A4XX_RB_COPY_DEST_INFO_FORMAT(fd4_pipe2color(psurf->format)) |
+                       A4XX_RB_COPY_DEST_INFO_FORMAT(fd4_pipe2color(pformat)) |
                        A4XX_RB_COPY_DEST_INFO_COMPONENT_ENABLE(0xf) |
                        A4XX_RB_COPY_DEST_INFO_ENDIAN(ENDIAN_NONE) |
-                       A4XX_RB_COPY_DEST_INFO_SWAP(fd4_pipe2swap(psurf->format)));
+                       A4XX_RB_COPY_DEST_INFO_SWAP(fd4_pipe2swap(pformat)));
 
-       fd4_draw(ctx, ring, DI_PT_RECTLIST, IGNORE_VISIBILITY,
-                       DI_SRC_SEL_AUTO_INDEX, 2, INDEX_SIZE_IGN, 0, 0, NULL);
+       fd4_draw(batch, ring, DI_PT_RECTLIST, IGNORE_VISIBILITY,
+                       DI_SRC_SEL_AUTO_INDEX, 2, 1, INDEX4_SIZE_8_BIT, 0, 0, NULL);
 }
 
 static void
-fd4_emit_tile_gmem2mem(struct fd_context *ctx, struct fd_tile *tile)
+fd4_emit_tile_gmem2mem(struct fd_batch *batch, struct fd_tile *tile)
 {
-       struct fd4_context *fd4_ctx = fd4_context(ctx);
-       struct fd_ringbuffer *ring = ctx->ring;
-       struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
+       struct fd_context *ctx = batch->ctx;
+       struct fd_gmem_stateobj *gmem = &ctx->gmem;
+       struct fd_ringbuffer *ring = batch->gmem;
+       struct pipe_framebuffer_state *pfb = &batch->framebuffer;
        struct fd4_emit emit = {
-                       .vtx = &fd4_ctx->solid_vbuf_state,
+                       .debug = &ctx->debug,
+                       .vtx = &ctx->solid_vbuf_state,
                        .prog = &ctx->solid_prog,
-                       .key = key,
-                       .format = fd4_emit_format(pfb->cbufs[0]),
+                       .key = {
+                               .half_precision = true,
+                       },
        };
 
        OUT_PKT0(ring, REG_A4XX_RB_DEPTH_CONTROL, 1);
        OUT_RING(ring, A4XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_NEVER));
 
-       OUT_PKT0(ring, REG_A4XX_RB_STENCIL_CONTROL, 1);
+       OUT_PKT0(ring, REG_A4XX_RB_STENCIL_CONTROL, 2);
        OUT_RING(ring, A4XX_RB_STENCIL_CONTROL_FUNC(FUNC_NEVER) |
                        A4XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
                        A4XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_KEEP) |
@@ -168,6 +214,7 @@ fd4_emit_tile_gmem2mem(struct fd_context *ctx, struct fd_tile *tile)
                        A4XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
                        A4XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
                        A4XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
+       OUT_RING(ring, 0x00000000); /* RB_STENCIL_CONTROL2 */
 
        OUT_PKT0(ring, REG_A4XX_RB_STENCILREFMASK, 2);
        OUT_RING(ring, 0xff000000 |
@@ -182,7 +229,7 @@ fd4_emit_tile_gmem2mem(struct fd_context *ctx, struct fd_tile *tile)
        OUT_PKT0(ring, REG_A4XX_GRAS_SU_MODE_CONTROL, 1);
        OUT_RING(ring, A4XX_GRAS_SU_MODE_CONTROL_LINEHALFWIDTH(0));
 
-       fd_wfi(ctx, ring);
+       fd_wfi(batch, ring);
 
        OUT_PKT0(ring, REG_A4XX_GRAS_CL_CLIP_CNTL, 1);
        OUT_RING(ring, 0x80000);      /* GRAS_CL_CLIP_CNTL */
@@ -221,16 +268,26 @@ fd4_emit_tile_gmem2mem(struct fd_context *ctx, struct fd_tile *tile)
        OUT_RING(ring, 0);            /* VFD_INDEX_OFFSET */
        OUT_RING(ring, 0);            /* ??? UNKNOWN_2209 */
 
-       fd4_program_emit(ring, &emit);
+       fd4_program_emit(ring, &emit, 0, NULL);
        fd4_emit_vertex_bufs(ring, &emit);
 
-       if (ctx->resolve & (FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
-               uint32_t base = depth_base(ctx);
-               emit_gmem2mem_surf(ctx, base, pfb->zsbuf);
+       if (batch->resolve & (FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
+               struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
+               if (!rsc->stencil || (batch->resolve & FD_BUFFER_DEPTH))
+                       emit_gmem2mem_surf(batch, false, ctx->gmem.zsbuf_base[0], pfb->zsbuf);
+               if (rsc->stencil && (batch->resolve & FD_BUFFER_STENCIL))
+                       emit_gmem2mem_surf(batch, true, ctx->gmem.zsbuf_base[1], pfb->zsbuf);
        }
 
-       if (ctx->resolve & FD_BUFFER_COLOR) {
-               emit_gmem2mem_surf(ctx, 0, pfb->cbufs[0]);
+       if (batch->resolve & FD_BUFFER_COLOR) {
+               unsigned i;
+               for (i = 0; i < pfb->nr_cbufs; i++) {
+                       if (!pfb->cbufs[i])
+                               continue;
+                       if (!(batch->resolve & (PIPE_CLEAR_COLOR0 << i)))
+                               continue;
+                       emit_gmem2mem_surf(batch, false, gmem->cbuf_base[i], pfb->cbufs[i]);
+               }
        }
 
        OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
@@ -243,32 +300,49 @@ fd4_emit_tile_gmem2mem(struct fd_context *ctx, struct fd_tile *tile)
 /* transfer from system memory to gmem */
 
 static void
-emit_mem2gmem_surf(struct fd_context *ctx, uint32_t base,
-               struct pipe_surface *psurf, uint32_t bin_w)
+emit_mem2gmem_surf(struct fd_batch *batch, uint32_t *bases,
+               struct pipe_surface **bufs, uint32_t nr_bufs, uint32_t bin_w)
 {
-       struct fd_ringbuffer *ring = ctx->ring;
-
-       emit_mrt(ring, 1, &psurf, &base, bin_w);
+       struct fd_ringbuffer *ring = batch->gmem;
+       struct pipe_surface *zsbufs[2];
+
+       emit_mrt(ring, nr_bufs, bufs, bases, bin_w, false);
+
+       if (bufs[0] && (bufs[0]->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)) {
+               /* The gmem_restore_tex logic will put the first buffer's stencil
+                * as color. Supply it with the proper information to make that
+                * happen.
+                */
+               zsbufs[0] = zsbufs[1] = bufs[0];
+               bufs = zsbufs;
+               nr_bufs = 2;
+       }
 
-       fd4_emit_gmem_restore_tex(ring, psurf);
+       fd4_emit_gmem_restore_tex(ring, nr_bufs, bufs);
 
-       fd4_draw(ctx, ring, DI_PT_RECTLIST, IGNORE_VISIBILITY,
-                       DI_SRC_SEL_AUTO_INDEX, 2, INDEX_SIZE_IGN, 0, 0, NULL);
+       fd4_draw(batch, ring, DI_PT_RECTLIST, IGNORE_VISIBILITY,
+                       DI_SRC_SEL_AUTO_INDEX, 2, 1, INDEX4_SIZE_8_BIT, 0, 0, NULL);
 }
 
 static void
-fd4_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
+fd4_emit_tile_mem2gmem(struct fd_batch *batch, struct fd_tile *tile)
 {
-       struct fd4_context *fd4_ctx = fd4_context(ctx);
+       struct fd_context *ctx = batch->ctx;
        struct fd_gmem_stateobj *gmem = &ctx->gmem;
-       struct fd_ringbuffer *ring = ctx->ring;
-       struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
+       struct fd_ringbuffer *ring = batch->gmem;
+       struct pipe_framebuffer_state *pfb = &batch->framebuffer;
        struct fd4_emit emit = {
-                       .vtx = &fd4_ctx->blit_vbuf_state,
-                       .prog = &ctx->blit_prog,
-                       .key = key,
-                       .format = fd4_emit_format(pfb->cbufs[0]),
+                       .debug = &ctx->debug,
+                       .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),
+                       },
+                       .no_decode_srgb = true,
        };
+       unsigned char mrt_comp[A4XX_MAX_RENDER_TARGETS] = {0};
        float x0, y0, x1, y1;
        unsigned bin_w = tile->bin_w;
        unsigned bin_h = tile->bin_h;
@@ -281,16 +355,17 @@ fd4_emit_tile_mem2gmem(struct fd_context *ctx, 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(fd4_ctx->blit_texcoord_vbuf)->bo, 0, 0, 0);
+       OUT_RELOCW(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));
 
-       for (i = 0; i < 8; i++) {
+       for (i = 0; i < A4XX_MAX_RENDER_TARGETS; i++) {
+               mrt_comp[i] = ((i < pfb->nr_cbufs) && pfb->cbufs[i]) ? 0xf : 0;
+
                OUT_PKT0(ring, REG_A4XX_RB_MRT_CONTROL(i), 1);
-               OUT_RING(ring, A4XX_RB_MRT_CONTROL_FASTCLEAR |
-                               A4XX_RB_MRT_CONTROL_B11 |
+               OUT_RING(ring, A4XX_RB_MRT_CONTROL_ROP_CODE(ROP_COPY) |
                                A4XX_RB_MRT_CONTROL_COMPONENT_ENABLE(0xf));
 
                OUT_PKT0(ring, REG_A4XX_RB_MRT_BLEND_CONTROL(i), 1);
@@ -302,6 +377,16 @@ fd4_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
                                A4XX_RB_MRT_BLEND_CONTROL_ALPHA_DEST_FACTOR(FACTOR_ZERO));
        }
 
+       OUT_PKT0(ring, REG_A4XX_RB_RENDER_COMPONENTS, 1);
+       OUT_RING(ring, A4XX_RB_RENDER_COMPONENTS_RT0(mrt_comp[0]) |
+                       A4XX_RB_RENDER_COMPONENTS_RT1(mrt_comp[1]) |
+                       A4XX_RB_RENDER_COMPONENTS_RT2(mrt_comp[2]) |
+                       A4XX_RB_RENDER_COMPONENTS_RT3(mrt_comp[3]) |
+                       A4XX_RB_RENDER_COMPONENTS_RT4(mrt_comp[4]) |
+                       A4XX_RB_RENDER_COMPONENTS_RT5(mrt_comp[5]) |
+                       A4XX_RB_RENDER_COMPONENTS_RT6(mrt_comp[6]) |
+                       A4XX_RB_RENDER_COMPONENTS_RT7(mrt_comp[7]));
+
        OUT_PKT0(ring, REG_A4XX_RB_RENDER_CONTROL, 1);
        OUT_RING(ring, 0x8);          /* XXX RB_RENDER_CONTROL */
 
@@ -339,7 +424,7 @@ fd4_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
        OUT_RING(ring, A4XX_RB_MODE_CONTROL_WIDTH(gmem->bin_w) |
                        A4XX_RB_MODE_CONTROL_HEIGHT(gmem->bin_h));
 
-       OUT_PKT0(ring, REG_A4XX_RB_STENCIL_CONTROL, 1);
+       OUT_PKT0(ring, REG_A4XX_RB_STENCIL_CONTROL, 2);
        OUT_RING(ring, A4XX_RB_STENCIL_CONTROL_FUNC(FUNC_ALWAYS) |
                        A4XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
                        A4XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_KEEP) |
@@ -348,6 +433,7 @@ fd4_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
                        A4XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
                        A4XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
                        A4XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
+       OUT_RING(ring, 0x00000000); /* RB_STENCIL_CONTROL2 */
 
        OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
        OUT_RING(ring, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
@@ -357,13 +443,12 @@ fd4_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
 
        OUT_PKT0(ring, REG_A4XX_PC_PRIM_VTX_CNTL, 1);
        OUT_RING(ring, A4XX_PC_PRIM_VTX_CNTL_PROVOKING_VTX_LAST |
-                       A4XX_PC_PRIM_VTX_CNTL_VAROUT);
+                       A4XX_PC_PRIM_VTX_CNTL_VAROUT(1));
 
        OUT_PKT0(ring, REG_A4XX_VFD_INDEX_OFFSET, 2);
        OUT_RING(ring, 0);            /* VFD_INDEX_OFFSET */
        OUT_RING(ring, 0);            /* ??? UNKNOWN_2209 */
 
-       fd4_program_emit(ring, &emit);
        fd4_emit_vertex_bufs(ring, &emit);
 
        /* for gmem pitch/base calculations, we need to use the non-
@@ -372,11 +457,46 @@ fd4_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
        bin_w = gmem->bin_w;
        bin_h = gmem->bin_h;
 
-       if (fd_gmem_needs_restore(ctx, tile, FD_BUFFER_DEPTH | FD_BUFFER_STENCIL))
-               emit_mem2gmem_surf(ctx, depth_base(ctx), pfb->zsbuf, bin_w);
+       if (fd_gmem_needs_restore(batch, tile, FD_BUFFER_COLOR)) {
+               emit.prog = &ctx->blit_prog[pfb->nr_cbufs - 1];
+               emit.fs = NULL;      /* frag shader changed so clear cache */
+               fd4_program_emit(ring, &emit, pfb->nr_cbufs, pfb->cbufs);
+               emit_mem2gmem_surf(batch, gmem->cbuf_base, pfb->cbufs, pfb->nr_cbufs, bin_w);
+       }
 
-       if (fd_gmem_needs_restore(ctx, tile, FD_BUFFER_COLOR))
-               emit_mem2gmem_surf(ctx, 0, pfb->cbufs[0], bin_w);
+       if (fd_gmem_needs_restore(batch, tile, FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
+               switch (pfb->zsbuf->format) {
+               case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
+               case PIPE_FORMAT_Z32_FLOAT:
+                       emit.prog = (pfb->zsbuf->format == PIPE_FORMAT_Z32_FLOAT) ?
+                                       &ctx->blit_z : &ctx->blit_zs;
+                       emit.key.half_precision = false;
+
+                       OUT_PKT0(ring, REG_A4XX_RB_DEPTH_CONTROL, 1);
+                       OUT_RING(ring, A4XX_RB_DEPTH_CONTROL_Z_ENABLE |
+                                       A4XX_RB_DEPTH_CONTROL_Z_WRITE_ENABLE |
+                                       A4XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_ALWAYS) |
+                                       A4XX_RB_DEPTH_CONTROL_EARLY_Z_DISABLE);
+
+                       OUT_PKT0(ring, REG_A4XX_GRAS_ALPHA_CONTROL, 1);
+                       OUT_RING(ring, A4XX_GRAS_ALPHA_CONTROL_ALPHA_TEST_ENABLE);
+
+                       OUT_PKT0(ring, REG_A4XX_GRAS_CL_CLIP_CNTL, 1);
+                       OUT_RING(ring, 0x80000);   /* GRAS_CL_CLIP_CNTL */
+
+                       break;
+               default:
+                       /* Non-float can use a regular color write. It's split over 8-bit
+                        * components, so half precision is always sufficient.
+                        */
+                       emit.prog = &ctx->blit_prog[0];
+                       emit.key.half_precision = true;
+                       break;
+               }
+               emit.fs = NULL;      /* frag shader changed so clear cache */
+               fd4_program_emit(ring, &emit, 1, &pfb->zsbuf);
+               emit_mem2gmem_surf(batch, gmem->zsbuf_base, &pfb->zsbuf, 1, bin_w);
+       }
 
        OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
        OUT_RING(ring, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
@@ -390,33 +510,59 @@ fd4_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
 }
 
 static void
-patch_draws(struct fd_context *ctx, enum pc_di_vis_cull_mode vismode)
+patch_draws(struct fd_batch *batch, enum pc_di_vis_cull_mode vismode)
 {
        unsigned i;
-       for (i = 0; i < fd_patch_num_elements(&ctx->draw_patches); i++) {
-               struct fd_cs_patch *patch = fd_patch_element(&ctx->draw_patches, i);
+       for (i = 0; i < fd_patch_num_elements(&batch->draw_patches); i++) {
+               struct fd_cs_patch *patch = fd_patch_element(&batch->draw_patches, i);
                *patch->cs = patch->val | DRAW4(0, 0, 0, vismode);
        }
-       util_dynarray_resize(&ctx->draw_patches, 0);
+       util_dynarray_clear(&batch->draw_patches);
 }
 
+/* for rendering directly to system memory: */
 static void
-patch_rbrc(struct fd_context *ctx, uint32_t val)
+fd4_emit_sysmem_prep(struct fd_batch *batch)
 {
-       struct fd4_context *fd4_ctx = fd4_context(ctx);
-       unsigned i;
-       for (i = 0; i < fd_patch_num_elements(&fd4_ctx->rbrc_patches); i++) {
-               struct fd_cs_patch *patch = fd_patch_element(&fd4_ctx->rbrc_patches, i);
-               *patch->cs = patch->val | val;
-       }
-       util_dynarray_resize(&fd4_ctx->rbrc_patches, 0);
+       struct pipe_framebuffer_state *pfb = &batch->framebuffer;
+       struct fd_ringbuffer *ring = batch->gmem;
+
+       fd4_emit_restore(batch, ring);
+
+       OUT_PKT0(ring, REG_A4XX_RB_FRAME_BUFFER_DIMENSION, 1);
+       OUT_RING(ring, A4XX_RB_FRAME_BUFFER_DIMENSION_WIDTH(pfb->width) |
+                       A4XX_RB_FRAME_BUFFER_DIMENSION_HEIGHT(pfb->height));
+
+       emit_mrt(ring, pfb->nr_cbufs, pfb->cbufs, NULL, 0, true);
+
+       /* setup scissor/offset for current tile: */
+       OUT_PKT0(ring, REG_A4XX_RB_BIN_OFFSET, 1);
+       OUT_RING(ring, A4XX_RB_BIN_OFFSET_X(0) |
+                       A4XX_RB_BIN_OFFSET_Y(0));
+
+       OUT_PKT0(ring, REG_A4XX_GRAS_SC_SCREEN_SCISSOR_TL, 2);
+       OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_TL_X(0) |
+                       A4XX_GRAS_SC_SCREEN_SCISSOR_TL_Y(0));
+       OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_BR_X(pfb->width - 1) |
+                       A4XX_GRAS_SC_SCREEN_SCISSOR_BR_Y(pfb->height - 1));
+
+       OUT_PKT0(ring, REG_A4XX_RB_MODE_CONTROL, 1);
+       OUT_RING(ring, A4XX_RB_MODE_CONTROL_WIDTH(0) |
+                       A4XX_RB_MODE_CONTROL_HEIGHT(0) |
+                       0x00c00000);  /* XXX */
+
+       OUT_PKT0(ring, REG_A4XX_RB_RENDER_CONTROL, 1);
+       OUT_RING(ring, 0x8);
+
+       patch_draws(batch, IGNORE_VISIBILITY);
 }
 
 static void
-update_vsc_pipe(struct fd_context *ctx)
+update_vsc_pipe(struct fd_batch *batch)
 {
+       struct fd_context *ctx = batch->ctx;
        struct fd4_context *fd4_ctx = fd4_context(ctx);
-       struct fd_ringbuffer *ring = ctx->ring;
+       struct fd_ringbuffer *ring = batch->gmem;
        int i;
 
        OUT_PKT0(ring, REG_A4XX_VSC_SIZE_ADDRESS, 1);
@@ -424,7 +570,7 @@ update_vsc_pipe(struct fd_context *ctx)
 
        OUT_PKT0(ring, REG_A4XX_VSC_PIPE_CONFIG_REG(0), 8);
        for (i = 0; i < 8; i++) {
-               struct fd_vsc_pipe *pipe = &ctx->pipe[i];
+               struct fd_vsc_pipe *pipe = &ctx->vsc_pipe[i];
                OUT_RING(ring, A4XX_VSC_PIPE_CONFIG_REG_X(pipe->x) |
                                A4XX_VSC_PIPE_CONFIG_REG_Y(pipe->y) |
                                A4XX_VSC_PIPE_CONFIG_REG_W(pipe->w) |
@@ -433,104 +579,221 @@ update_vsc_pipe(struct fd_context *ctx)
 
        OUT_PKT0(ring, REG_A4XX_VSC_PIPE_DATA_ADDRESS_REG(0), 8);
        for (i = 0; i < 8; i++) {
-               struct fd_vsc_pipe *pipe = &ctx->pipe[i];
+               struct fd_vsc_pipe *pipe = &ctx->vsc_pipe[i];
                if (!pipe->bo) {
                        pipe->bo = fd_bo_new(ctx->dev, 0x40000,
-                                       DRM_FREEDRENO_GEM_TYPE_KMEM);
+                                       DRM_FREEDRENO_GEM_TYPE_KMEM, "vsc_pipe[%u]", i);
                }
                OUT_RELOCW(ring, pipe->bo, 0, 0, 0);       /* VSC_PIPE_DATA_ADDRESS[i] */
        }
 
        OUT_PKT0(ring, REG_A4XX_VSC_PIPE_DATA_LENGTH_REG(0), 8);
        for (i = 0; i < 8; i++) {
-               struct fd_vsc_pipe *pipe = &ctx->pipe[i];
+               struct fd_vsc_pipe *pipe = &ctx->vsc_pipe[i];
                OUT_RING(ring, fd_bo_size(pipe->bo) - 32); /* VSC_PIPE_DATA_LENGTH[i] */
        }
 }
 
-/* before first tile */
 static void
-fd4_emit_tile_init(struct fd_context *ctx)
+emit_binning_pass(struct fd_batch *batch)
 {
-       struct fd_ringbuffer *ring = ctx->ring;
+       struct fd_context *ctx = batch->ctx;
        struct fd_gmem_stateobj *gmem = &ctx->gmem;
-       uint32_t rb_render_control;
+       struct pipe_framebuffer_state *pfb = &batch->framebuffer;
+       struct fd_ringbuffer *ring = batch->gmem;
+       int i;
+
+       uint32_t x1 = gmem->minx;
+       uint32_t y1 = gmem->miny;
+       uint32_t x2 = gmem->minx + gmem->width - 1;
+       uint32_t y2 = gmem->miny + gmem->height - 1;
+
+       OUT_PKT0(ring, REG_A4XX_PC_BINNING_COMMAND, 1);
+       OUT_RING(ring, A4XX_PC_BINNING_COMMAND_BINNING_ENABLE);
+
+       OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
+       OUT_RING(ring, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_TILING_PASS) |
+                       A4XX_GRAS_SC_CONTROL_MSAA_DISABLE |
+                       A4XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
+                       A4XX_GRAS_SC_CONTROL_RASTER_MODE(0));
+
+       OUT_PKT0(ring, REG_A4XX_RB_FRAME_BUFFER_DIMENSION, 1);
+       OUT_RING(ring, A4XX_RB_FRAME_BUFFER_DIMENSION_WIDTH(pfb->width) |
+                       A4XX_RB_FRAME_BUFFER_DIMENSION_HEIGHT(pfb->height));
 
-       fd4_emit_restore(ctx);
+       /* setup scissor/offset for whole screen: */
+       OUT_PKT0(ring, REG_A4XX_RB_BIN_OFFSET, 1);
+       OUT_RING(ring, A4XX_RB_BIN_OFFSET_X(x1) |
+                       A4XX_RB_BIN_OFFSET_Y(y1));
+
+       OUT_PKT0(ring, REG_A4XX_GRAS_SC_SCREEN_SCISSOR_TL, 2);
+       OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_TL_X(x1) |
+                       A4XX_GRAS_SC_SCREEN_SCISSOR_TL_Y(y1));
+       OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_BR_X(x2) |
+                       A4XX_GRAS_SC_SCREEN_SCISSOR_BR_Y(y2));
+
+       for (i = 0; i < A4XX_MAX_RENDER_TARGETS; i++) {
+               OUT_PKT0(ring, REG_A4XX_RB_MRT_CONTROL(i), 1);
+               OUT_RING(ring, A4XX_RB_MRT_CONTROL_ROP_CODE(ROP_CLEAR) |
+                               A4XX_RB_MRT_CONTROL_COMPONENT_ENABLE(0xf));
+       }
+
+       /* emit IB to binning drawcmds: */
+       fd4_emit_ib(ring, batch->binning);
+
+       fd_reset_wfi(batch);
+       fd_wfi(batch, ring);
+
+       /* and then put stuff back the way it was: */
+
+       OUT_PKT0(ring, REG_A4XX_PC_BINNING_COMMAND, 1);
+       OUT_RING(ring, 0x00000000);
+
+       OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
+       OUT_RING(ring, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
+                       A4XX_GRAS_SC_CONTROL_MSAA_DISABLE |
+                       A4XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
+                       A4XX_GRAS_SC_CONTROL_RASTER_MODE(0));
+
+       fd_event_write(batch, ring, CACHE_FLUSH);
+       fd_wfi(batch, ring);
+}
+
+/* before first tile */
+static void
+fd4_emit_tile_init(struct fd_batch *batch)
+{
+       struct fd_ringbuffer *ring = batch->gmem;
+       struct pipe_framebuffer_state *pfb = &batch->framebuffer;
+       struct fd_gmem_stateobj *gmem = &batch->ctx->gmem;
+
+       fd4_emit_restore(batch, ring);
 
        OUT_PKT0(ring, REG_A4XX_VSC_BIN_SIZE, 1);
        OUT_RING(ring, A4XX_VSC_BIN_SIZE_WIDTH(gmem->bin_w) |
                        A4XX_VSC_BIN_SIZE_HEIGHT(gmem->bin_h));
 
+       update_vsc_pipe(batch);
+
+       fd_wfi(batch, ring);
+       OUT_PKT0(ring, REG_A4XX_RB_FRAME_BUFFER_DIMENSION, 1);
+       OUT_RING(ring, A4XX_RB_FRAME_BUFFER_DIMENSION_WIDTH(pfb->width) |
+                       A4XX_RB_FRAME_BUFFER_DIMENSION_HEIGHT(pfb->height));
+
+       if (use_hw_binning(batch)) {
+               OUT_PKT0(ring, REG_A4XX_RB_MODE_CONTROL, 1);
+               OUT_RING(ring, A4XX_RB_MODE_CONTROL_WIDTH(gmem->bin_w) |
+                               A4XX_RB_MODE_CONTROL_HEIGHT(gmem->bin_h));
+
+               OUT_PKT0(ring, REG_A4XX_RB_RENDER_CONTROL, 1);
+               OUT_RING(ring, A4XX_RB_RENDER_CONTROL_BINNING_PASS |
+                               A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE |
+                               0x8);
+
+               /* emit hw binning pass: */
+               emit_binning_pass(batch);
+
+               patch_draws(batch, USE_VISIBILITY);
+       } else {
+               patch_draws(batch, IGNORE_VISIBILITY);
+       }
+
        OUT_PKT0(ring, REG_A4XX_RB_MODE_CONTROL, 1);
        OUT_RING(ring, A4XX_RB_MODE_CONTROL_WIDTH(gmem->bin_w) |
                        A4XX_RB_MODE_CONTROL_HEIGHT(gmem->bin_h) |
-                       0x00010000);  /* XXX */
-
-       update_vsc_pipe(ctx);
-       patch_draws(ctx, IGNORE_VISIBILITY);
-
-       rb_render_control = 0; // XXX or BINNING_PASS.. but maybe we can emit only from gmem
-       patch_rbrc(ctx, rb_render_control);
+                       A4XX_RB_MODE_CONTROL_ENABLE_GMEM);
 }
 
 /* before mem2gmem */
 static void
-fd4_emit_tile_prep(struct fd_context *ctx, struct fd_tile *tile)
+fd4_emit_tile_prep(struct fd_batch *batch, struct fd_tile *tile)
 {
-       struct fd_ringbuffer *ring = ctx->ring;
-       struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
+       struct fd_context *ctx = batch->ctx;
+       struct fd_ringbuffer *ring = batch->gmem;
+       struct pipe_framebuffer_state *pfb = &batch->framebuffer;
        struct fd_gmem_stateobj *gmem = &ctx->gmem;
-       uint32_t reg;
 
-       OUT_PKT0(ring, REG_A4XX_RB_DEPTH_INFO, 3);
-       reg = A4XX_RB_DEPTH_INFO_DEPTH_BASE(depth_base(ctx));
-       if (pfb->zsbuf) {
-               reg |= A4XX_RB_DEPTH_INFO_DEPTH_FORMAT(fd_pipe2depth(pfb->zsbuf->format));
-       }
-       OUT_RING(ring, reg);
        if (pfb->zsbuf) {
-               OUT_RING(ring, A4XX_RB_DEPTH_PITCH(gmem->bin_w));
-               OUT_RING(ring, A4XX_RB_DEPTH_PITCH2(gmem->bin_w));
+               struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
+               uint32_t cpp = rsc->cpp;
+
+               OUT_PKT0(ring, REG_A4XX_RB_DEPTH_INFO, 3);
+               OUT_RING(ring, A4XX_RB_DEPTH_INFO_DEPTH_BASE(gmem->zsbuf_base[0]) |
+                               A4XX_RB_DEPTH_INFO_DEPTH_FORMAT(fd4_pipe2depth(pfb->zsbuf->format)));
+               OUT_RING(ring, A4XX_RB_DEPTH_PITCH(cpp * gmem->bin_w));
+               OUT_RING(ring, A4XX_RB_DEPTH_PITCH2(cpp * gmem->bin_w));
+
+               OUT_PKT0(ring, REG_A4XX_RB_STENCIL_INFO, 2);
+               if (rsc->stencil) {
+                       OUT_RING(ring, A4XX_RB_STENCIL_INFO_SEPARATE_STENCIL |
+                                       A4XX_RB_STENCIL_INFO_STENCIL_BASE(gmem->zsbuf_base[1]));
+                       OUT_RING(ring, A4XX_RB_STENCIL_PITCH(rsc->stencil->cpp * gmem->bin_w));
+               } else {
+                       OUT_RING(ring, 0x00000000);
+                       OUT_RING(ring, 0x00000000);
+               }
        } else {
+               OUT_PKT0(ring, REG_A4XX_RB_DEPTH_INFO, 3);
+               OUT_RING(ring, 0x00000000);
                OUT_RING(ring, 0x00000000);
                OUT_RING(ring, 0x00000000);
+
+               OUT_PKT0(ring, REG_A4XX_RB_STENCIL_INFO, 2);
+               OUT_RING(ring, 0);            /* RB_STENCIL_INFO */
+               OUT_RING(ring, 0);            /* RB_STENCIL_PITCH */
        }
 
+       OUT_PKT0(ring, REG_A4XX_GRAS_DEPTH_CONTROL, 1);
        if (pfb->zsbuf) {
-               OUT_PKT0(ring, REG_A4XX_GRAS_DEPTH_CONTROL, 1);
                OUT_RING(ring, A4XX_GRAS_DEPTH_CONTROL_FORMAT(
-                               fd_pipe2depth(pfb->zsbuf->format)));
-       }
-
-       if (ctx->needs_rb_fbd) {
-               fd_wfi(ctx, ring);
-               OUT_PKT0(ring, REG_A4XX_RB_FRAME_BUFFER_DIMENSION, 1);
-               OUT_RING(ring, A4XX_RB_FRAME_BUFFER_DIMENSION_WIDTH(pfb->width) |
-                               A4XX_RB_FRAME_BUFFER_DIMENSION_HEIGHT(pfb->height));
-               ctx->needs_rb_fbd = false;
+                               fd4_pipe2depth(pfb->zsbuf->format)));
+       } else {
+               OUT_RING(ring, A4XX_GRAS_DEPTH_CONTROL_FORMAT(DEPTH4_NONE));
        }
 }
 
 /* before IB to rendering cmds: */
 static void
-fd4_emit_tile_renderprep(struct fd_context *ctx, struct fd_tile *tile)
+fd4_emit_tile_renderprep(struct fd_batch *batch, struct fd_tile *tile)
 {
-       struct fd_ringbuffer *ring = ctx->ring;
+       struct fd_context *ctx = batch->ctx;
+       struct fd4_context *fd4_ctx = fd4_context(ctx);
+       struct fd_ringbuffer *ring = batch->gmem;
        struct fd_gmem_stateobj *gmem = &ctx->gmem;
-       struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
+       struct pipe_framebuffer_state *pfb = &batch->framebuffer;
 
        uint32_t x1 = tile->xoff;
        uint32_t y1 = tile->yoff;
        uint32_t x2 = tile->xoff + tile->bin_w - 1;
        uint32_t y2 = tile->yoff + tile->bin_h - 1;
 
+       if (use_hw_binning(batch)) {
+               struct fd_vsc_pipe *pipe = &ctx->vsc_pipe[tile->p];
+
+               assert(pipe->w && pipe->h);
+
+               fd_event_write(batch, ring, HLSQ_FLUSH);
+               fd_wfi(batch, ring);
+
+               OUT_PKT0(ring, REG_A4XX_PC_VSTREAM_CONTROL, 1);
+               OUT_RING(ring, A4XX_PC_VSTREAM_CONTROL_SIZE(pipe->w * pipe->h) |
+                               A4XX_PC_VSTREAM_CONTROL_N(tile->n));
+
+               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, fd4_ctx->vsc_size_mem, /* BIN_SIZE_ADDR <- VSC_SIZE_ADDRESS + (p * 4) */
+                               (tile->p * 4), 0, 0);
+       } else {
+               OUT_PKT0(ring, REG_A4XX_PC_VSTREAM_CONTROL, 1);
+               OUT_RING(ring, 0x00000000);
+       }
+
        OUT_PKT3(ring, CP_SET_BIN, 3);
        OUT_RING(ring, 0x00000000);
        OUT_RING(ring, CP_SET_BIN_1_X1(x1) | CP_SET_BIN_1_Y1(y1));
        OUT_RING(ring, CP_SET_BIN_2_X2(x2) | CP_SET_BIN_2_Y2(y2));
 
-       emit_mrt(ring, pfb->nr_cbufs, pfb->cbufs, NULL, gmem->bin_w);
+       emit_mrt(ring, pfb->nr_cbufs, pfb->cbufs, gmem->cbuf_base, gmem->bin_w, true);
 
        /* setup scissor/offset for current tile: */
        OUT_PKT0(ring, REG_A4XX_RB_BIN_OFFSET, 1);
@@ -542,6 +805,9 @@ fd4_emit_tile_renderprep(struct fd_context *ctx, struct fd_tile *tile)
                        A4XX_GRAS_SC_SCREEN_SCISSOR_TL_Y(y1));
        OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_BR_X(x2) |
                        A4XX_GRAS_SC_SCREEN_SCISSOR_BR_Y(y2));
+
+       OUT_PKT0(ring, REG_A4XX_RB_RENDER_CONTROL, 1);
+       OUT_RING(ring, 0x8);
 }
 
 void
@@ -549,6 +815,7 @@ fd4_gmem_init(struct pipe_context *pctx)
 {
        struct fd_context *ctx = fd_context(pctx);
 
+       ctx->emit_sysmem_prep = fd4_emit_sysmem_prep;
        ctx->emit_tile_init = fd4_emit_tile_init;
        ctx->emit_tile_prep = fd4_emit_tile_prep;
        ctx->emit_tile_mem2gmem = fd4_emit_tile_mem2gmem;