freedreno: introduce fd_batch
[mesa.git] / src / gallium / drivers / freedreno / a3xx / fd3_gmem.c
index 8edfb8b46d192ed1bbde1484f5ce40ed6c67132f..2449a84c7215ad2efce731655c66c3dfa9a357f6 100644 (file)
@@ -45,7 +45,8 @@
 
 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 a3xx_tile_mode tile_mode;
        unsigned i;
@@ -56,26 +57,43 @@ emit_mrt(struct fd_ringbuffer *ring, unsigned nr_bufs,
                tile_mode = LINEAR;
        }
 
-       for (i = 0; i < 4; i++) {
+       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 layer_offset = 0;
+               uint32_t offset = 0;
 
                if ((i < nr_bufs) && bufs[i]) {
                        struct pipe_surface *psurf = bufs[i];
 
                        rsc = fd_resource(psurf->texture);
-                       slice = &rsc->slices[psurf->u.tex.level];
-                       format = fd3_pipe2color(psurf->format);
-                       swap = fd3_pipe2swap(psurf->format);
+                       pformat = 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.b.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
+                               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,37 +104,27 @@ 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_A3XX_RB_MRT_BUF_INFO(i), 2);
                OUT_RING(ring, A3XX_RB_MRT_BUF_INFO_COLOR_FORMAT(format) |
                                A3XX_RB_MRT_BUF_INFO_COLOR_TILE_MODE(tile_mode) |
                                A3XX_RB_MRT_BUF_INFO_COLOR_BUF_PITCH(stride) |
-                               A3XX_RB_MRT_BUF_INFO_COLOR_SWAP(swap));
-               if (bin_w || (i >= nr_bufs)) {
+                               A3XX_RB_MRT_BUF_INFO_COLOR_SWAP(swap) |
+                               COND(srgb, A3XX_RB_MRT_BUF_INFO_COLOR_SRGB));
+               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,
-                                       slice->offset + layer_offset, 0, -1);
+                       OUT_RELOCW(ring, rsc->bo, offset, 0, -1);
                }
 
                OUT_PKT0(ring, REG_A3XX_SP_FS_IMAGE_OUTPUT_REG(i), 1);
-               OUT_RING(ring, A3XX_SP_FS_IMAGE_OUTPUT_REG_MRTFORMAT(format));
-       }
-}
-
-static uint32_t
-depth_base(struct fd_context *ctx)
-{
-       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;
+               OUT_RING(ring, COND((i < nr_bufs) && bufs[i],
+                                                       A3XX_SP_FS_IMAGE_OUTPUT_REG_MRTFORMAT(
+                                                                       fd3_fs_output_format(pformat))));
        }
-       return align(gmem->bin_w * gmem->bin_h * cpp, 0x4000);
 }
 
 static bool
@@ -153,6 +161,7 @@ emit_binning_workaround(struct fd_context *ctx)
        struct fd_gmem_stateobj *gmem = &ctx->gmem;
        struct fd_ringbuffer *ring = ctx->ring;
        struct fd3_emit emit = {
+                       .debug = &ctx->debug,
                        .vtx = &fd3_ctx->solid_vbuf_state,
                        .prog = &ctx->solid_prog,
                        .key = {
@@ -162,7 +171,8 @@ emit_binning_workaround(struct fd_context *ctx)
 
        OUT_PKT0(ring, REG_A3XX_RB_MODE_CONTROL, 2);
        OUT_RING(ring, A3XX_RB_MODE_CONTROL_RENDER_MODE(RB_RESOLVE_PASS) |
-                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE);
+                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE |
+                       A3XX_RB_MODE_CONTROL_MRT(0));
        OUT_RING(ring, A3XX_RB_RENDER_CONTROL_BIN_WIDTH(32) |
                        A3XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE |
                        A3XX_RB_RENDER_CONTROL_ALPHA_TEST_FUNC(FUNC_NEVER));
@@ -184,7 +194,7 @@ emit_binning_workaround(struct fd_context *ctx)
                        A3XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
                        A3XX_GRAS_SC_CONTROL_RASTER_MODE(1));
 
-       fd3_program_emit(ring, &emit);
+       fd3_program_emit(ring, &emit, 0, NULL);
        fd3_emit_vertex_bufs(ring, &emit);
 
        OUT_PKT0(ring, REG_A3XX_HLSQ_CONTROL_0_REG, 4);
@@ -269,7 +279,7 @@ emit_binning_workaround(struct fd_context *ctx)
        OUT_PKT3(ring, CP_DRAW_INDX_2, 5);
        OUT_RING(ring, 0x00000000);   /* viz query info. */
        OUT_RING(ring, DRAW(DI_PT_RECTLIST, DI_SRC_SEL_IMMEDIATE,
-                       INDEX_SIZE_32_BIT, IGNORE_VISIBILITY));
+                                               INDEX_SIZE_32_BIT, IGNORE_VISIBILITY, 0));
        OUT_RING(ring, 2);            /* NumIndices */
        OUT_RING(ring, 2);
        OUT_RING(ring, 1);
@@ -299,31 +309,41 @@ emit_binning_workaround(struct fd_context *ctx)
 
 static void
 emit_gmem2mem_surf(struct fd_context *ctx,
-               enum adreno_rb_copy_control_mode mode,
-               uint32_t base, struct pipe_surface *psurf)
+                                  enum adreno_rb_copy_control_mode mode,
+                                  bool stencil,
+                                  uint32_t base, struct pipe_surface *psurf)
 {
        struct fd_ringbuffer *ring = ctx->ring;
        struct fd_resource *rsc = fd_resource(psurf->texture);
-       struct fd_resource_slice *slice = &rsc->slices[psurf->u.tex.level];
-       uint32_t layer_offset = slice->size0 * psurf->u.tex.first_layer;
+       enum pipe_format format = psurf->format;
+       if (stencil) {
+               rsc = rsc->stencil;
+               format = rsc->base.b.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);
 
        debug_assert(psurf->u.tex.first_layer == psurf->u.tex.last_layer);
 
        OUT_PKT0(ring, REG_A3XX_RB_COPY_CONTROL, 4);
        OUT_RING(ring, A3XX_RB_COPY_CONTROL_MSAA_RESOLVE(MSAA_ONE) |
                        A3XX_RB_COPY_CONTROL_MODE(mode) |
-                       A3XX_RB_COPY_CONTROL_GMEM_BASE(base));
+                       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));
 
-       OUT_RELOCW(ring, rsc->bo, slice->offset + layer_offset, 0, -1);    /* RB_COPY_DEST_BASE */
+       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) |
-                       A3XX_RB_COPY_DEST_INFO_FORMAT(fd3_pipe2color(psurf->format)) |
+                       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) |
-                       A3XX_RB_COPY_DEST_INFO_SWAP(fd3_pipe2swap(psurf->format)));
+                       A3XX_RB_COPY_DEST_INFO_SWAP(fd3_pipe2swap(format)));
 
        fd_draw(ctx, ring, DI_PT_RECTLIST, IGNORE_VISIBILITY,
-                       DI_SRC_SEL_AUTO_INDEX, 2, INDEX_SIZE_IGN, 0, 0, NULL);
+                       DI_SRC_SEL_AUTO_INDEX, 2, 0, INDEX_SIZE_IGN, 0, 0, NULL);
 }
 
 static void
@@ -332,15 +352,15 @@ fd3_emit_tile_gmem2mem(struct fd_context *ctx, struct fd_tile *tile)
        struct fd3_context *fd3_ctx = fd3_context(ctx);
        struct fd_ringbuffer *ring = ctx->ring;
        struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
-       enum pipe_format format = pipe_surface_format(pfb->cbufs[0]);
        struct fd3_emit emit = {
+                       .debug = &ctx->debug,
                        .vtx = &fd3_ctx->solid_vbuf_state,
                        .prog = &ctx->solid_prog,
                        .key = {
-                               .half_precision = fd3_half_precision(format),
+                               .half_precision = true,
                        },
-                       .format = format,
        };
+       int i;
 
        OUT_PKT0(ring, REG_A3XX_RB_DEPTH_CONTROL, 1);
        OUT_RING(ring, A3XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_NEVER));
@@ -382,7 +402,8 @@ fd3_emit_tile_gmem2mem(struct fd_context *ctx, struct fd_tile *tile)
 
        OUT_PKT0(ring, REG_A3XX_RB_MODE_CONTROL, 1);
        OUT_RING(ring, A3XX_RB_MODE_CONTROL_RENDER_MODE(RB_RESOLVE_PASS) |
-                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE);
+                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE |
+                       A3XX_RB_MODE_CONTROL_MRT(0));
 
        OUT_PKT0(ring, REG_A3XX_RB_RENDER_CONTROL, 1);
        OUT_RING(ring, A3XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE |
@@ -413,21 +434,34 @@ fd3_emit_tile_gmem2mem(struct fd_context *ctx, struct fd_tile *tile)
        OUT_RING(ring, 0);            /* VFD_INSTANCEID_OFFSET */
        OUT_RING(ring, 0);            /* VFD_INDEX_OFFSET */
 
-       fd3_program_emit(ring, &emit);
+       fd3_program_emit(ring, &emit, 0, NULL);
        fd3_emit_vertex_bufs(ring, &emit);
 
        if (ctx->resolve & (FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
-               uint32_t base = depth_base(ctx);
-               emit_gmem2mem_surf(ctx, RB_COPY_DEPTH_STENCIL, base, pfb->zsbuf);
+               struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
+               if (!rsc->stencil || ctx->resolve & FD_BUFFER_DEPTH)
+                       emit_gmem2mem_surf(ctx, RB_COPY_DEPTH_STENCIL, false,
+                                                          ctx->gmem.zsbuf_base[0], pfb->zsbuf);
+               if (rsc->stencil && ctx->resolve & FD_BUFFER_STENCIL)
+                       emit_gmem2mem_surf(ctx, RB_COPY_DEPTH_STENCIL, true,
+                                                          ctx->gmem.zsbuf_base[1], pfb->zsbuf);
        }
 
        if (ctx->resolve & FD_BUFFER_COLOR) {
-               emit_gmem2mem_surf(ctx, RB_COPY_RESOLVE, 0, pfb->cbufs[0]);
+               for (i = 0; i < pfb->nr_cbufs; i++) {
+                       if (!pfb->cbufs[i])
+                               continue;
+                       if (!(ctx->resolve & (PIPE_CLEAR_COLOR0 << i)))
+                               continue;
+                       emit_gmem2mem_surf(ctx, RB_COPY_RESOLVE, false,
+                                                          ctx->gmem.cbuf_base[i], pfb->cbufs[i]);
+               }
        }
 
        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);
+                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE |
+                       A3XX_RB_MODE_CONTROL_MRT(MAX2(1, pfb->nr_cbufs) - 1));
 
        OUT_PKT0(ring, REG_A3XX_GRAS_SC_CONTROL, 1);
        OUT_RING(ring, A3XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
@@ -438,17 +472,59 @@ fd3_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_context *ctx, uint32_t bases[],
+               struct pipe_surface **psurf, uint32_t bufs, uint32_t bin_w)
 {
        struct fd_ringbuffer *ring = ctx->ring;
+       struct pipe_surface *zsbufs[2];
+
+       assert(bufs > 0);
 
-       emit_mrt(ring, 1, &psurf, &base, bin_w);
+       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 |
+                                  A3XX_RB_MODE_CONTROL_MRT(bufs - 1));
+
+       emit_mrt(ring, bufs, psurf, bases, bin_w, false);
+
+       if (psurf[0] && (psurf[0]->format == PIPE_FORMAT_Z32_FLOAT ||
+                                        psurf[0]->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)) {
+               /* Depth is stored as unorm in gmem, so we have to write it in using a
+                * special blit shader which writes depth.
+                */
+               OUT_PKT0(ring, REG_A3XX_RB_DEPTH_CONTROL, 1);
+               OUT_RING(ring, (A3XX_RB_DEPTH_CONTROL_FRAG_WRITES_Z |
+                                               A3XX_RB_DEPTH_CONTROL_Z_WRITE_ENABLE |
+                                               A3XX_RB_DEPTH_CONTROL_Z_ENABLE |
+                                               A3XX_RB_DEPTH_CONTROL_EARLY_Z_DISABLE |
+                                               A3XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_ALWAYS)));
+
+               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 * ctx->gmem.bin_w));
+
+               if (psurf[0]->format == PIPE_FORMAT_Z32_FLOAT) {
+                       OUT_PKT0(ring, REG_A3XX_RB_MRT_CONTROL(0), 1);
+                       OUT_RING(ring, 0);
+               } else {
+                       /* 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] = psurf[0];
+                       psurf = zsbufs;
+                       bufs = 2;
+               }
+       } else {
+               OUT_PKT0(ring, REG_A3XX_SP_FS_OUTPUT_REG, 1);
+               OUT_RING(ring, A3XX_SP_FS_OUTPUT_REG_MRT(bufs - 1));
+       }
 
-       fd3_emit_gmem_restore_tex(ring, psurf);
+       fd3_emit_gmem_restore_tex(ring, psurf, bufs);
 
        fd_draw(ctx, ring, DI_PT_RECTLIST, IGNORE_VISIBILITY,
-                       DI_SRC_SEL_AUTO_INDEX, 2, INDEX_SIZE_IGN, 0, 0, NULL);
+                       DI_SRC_SEL_AUTO_INDEX, 2, 0, INDEX_SIZE_IGN, 0, 0, NULL);
 }
 
 static void
@@ -458,14 +534,15 @@ fd3_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
        struct fd_gmem_stateobj *gmem = &ctx->gmem;
        struct fd_ringbuffer *ring = ctx->ring;
        struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
-       enum pipe_format format = pipe_surface_format(pfb->cbufs[0]);
        struct fd3_emit emit = {
+                       .debug = &ctx->debug,
                        .vtx = &fd3_ctx->blit_vbuf_state,
-                       .prog = &ctx->blit_prog,
+                       .sprite_coord_enable = 1,
+                       /* NOTE: They all use the same VP, this is for vtx bufs. */
+                       .prog = &ctx->blit_prog[0],
                        .key = {
-                               .half_precision = fd3_half_precision(format),
+                               .half_precision = fd_half_precision(pfb),
                        },
-                       .format = format,
        };
        float x0, y0, x1, y1;
        unsigned bin_w = tile->bin_w;
@@ -485,6 +562,8 @@ fd3_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
        OUT_RING(ring, fui(x1));
        OUT_RING(ring, fui(y1));
 
+       fd3_emit_cache_flush(ctx, ring);
+
        for (i = 0; i < 4; i++) {
                OUT_PKT0(ring, REG_A3XX_RB_MRT_CONTROL(i), 1);
                OUT_RING(ring, A3XX_RB_MRT_CONTROL_ROP_CODE(ROP_COPY) |
@@ -508,6 +587,10 @@ fd3_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
        OUT_PKT0(ring, REG_A3XX_RB_DEPTH_CONTROL, 1);
        OUT_RING(ring, A3XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_LESS));
 
+       OUT_PKT0(ring, REG_A3XX_RB_DEPTH_INFO, 2);
+       OUT_RING(ring, 0);
+       OUT_RING(ring, 0);
+
        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 */
 
@@ -543,6 +626,10 @@ fd3_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
                        A3XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
                        A3XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
 
+       OUT_PKT0(ring, REG_A3XX_RB_STENCIL_INFO, 2);
+       OUT_RING(ring, 0); /* RB_STENCIL_INFO */
+       OUT_RING(ring, 0); /* RB_STENCIL_PITCH */
+
        OUT_PKT0(ring, REG_A3XX_GRAS_SC_CONTROL, 1);
        OUT_RING(ring, A3XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
                        A3XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
@@ -560,7 +647,6 @@ fd3_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
        OUT_RING(ring, 0);            /* VFD_INSTANCEID_OFFSET */
        OUT_RING(ring, 0);            /* VFD_INDEX_OFFSET */
 
-       fd3_program_emit(ring, &emit);
        fd3_emit_vertex_bufs(ring, &emit);
 
        /* for gmem pitch/base calculations, we need to use the non-
@@ -569,16 +655,43 @@ fd3_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(ctx, tile, FD_BUFFER_COLOR)) {
+               emit.prog = &ctx->blit_prog[pfb->nr_cbufs - 1];
+               emit.fp = NULL;      /* frag shader changed so clear cache */
+               fd3_program_emit(ring, &emit, pfb->nr_cbufs, pfb->cbufs);
+               emit_mem2gmem_surf(ctx, 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(ctx, tile, FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
+               if (pfb->zsbuf->format != PIPE_FORMAT_Z32_FLOAT_S8X24_UINT &&
+                       pfb->zsbuf->format != PIPE_FORMAT_Z32_FLOAT) {
+                       /* 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;
+               } 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 */
+               fd3_program_emit(ring, &emit, 1, &pfb->zsbuf);
+               emit_mem2gmem_surf(ctx, gmem->zsbuf_base, &pfb->zsbuf, 1, bin_w);
+       }
 
        OUT_PKT0(ring, REG_A3XX_GRAS_SC_CONTROL, 1);
        OUT_RING(ring, A3XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
                        A3XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
                        A3XX_GRAS_SC_CONTROL_RASTER_MODE(0));
+
+       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 |
+                                  A3XX_RB_MODE_CONTROL_MRT(MAX2(1, pfb->nr_cbufs) - 1));
 }
 
 static void
@@ -587,7 +700,7 @@ patch_draws(struct fd_context *ctx, 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);
-               *patch->cs = patch->val | DRAW(0, 0, 0, vismode);
+               *patch->cs = patch->val | DRAW(0, 0, 0, vismode, 0);
        }
        util_dynarray_resize(&ctx->draw_patches, 0);
 }
@@ -610,21 +723,22 @@ fd3_emit_sysmem_prep(struct fd_context *ctx)
 {
        struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
        struct fd_ringbuffer *ring = ctx->ring;
-       uint32_t pitch = 0;
+       uint32_t i, pitch = 0;
 
-       if (pfb->cbufs[0]) {
-               struct pipe_surface *psurf = pfb->cbufs[0];
-               unsigned lvl = psurf->u.tex.level;
-               pitch = fd_resource(psurf->texture)->slices[lvl].pitch;
+       for (i = 0; i < pfb->nr_cbufs; i++) {
+               struct pipe_surface *psurf = pfb->cbufs[i];
+               if (!psurf)
+                       continue;
+               pitch = fd_resource(psurf->texture)->slices[psurf->u.tex.level].pitch;
        }
 
-       fd3_emit_restore(ctx);
+       fd3_emit_restore(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));
 
-       emit_mrt(ring, pfb->nr_cbufs, pfb->cbufs, NULL, 0);
+       emit_mrt(ring, pfb->nr_cbufs, pfb->cbufs, NULL, 0, true);
 
        /* setup scissor/offset for current tile: */
        OUT_PKT0(ring, REG_A3XX_RB_WINDOW_OFFSET, 1);
@@ -640,7 +754,8 @@ fd3_emit_sysmem_prep(struct fd_context *ctx)
        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_GMEM_BYPASS |
-                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE);
+                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE |
+                       A3XX_RB_MODE_CONTROL_MRT(MAX2(1, pfb->nr_cbufs) - 1));
 
        patch_draws(ctx, IGNORE_VISIBILITY);
        patch_rbrc(ctx, A3XX_RB_RENDER_CONTROL_BIN_WIDTH(pitch));
@@ -679,6 +794,7 @@ emit_binning_pass(struct fd_context *ctx)
 {
        struct fd_gmem_stateobj *gmem = &ctx->gmem;
        struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
+       struct fd_batch *batch = ctx->batch;
        struct fd_ringbuffer *ring = ctx->ring;
        int i;
 
@@ -727,7 +843,8 @@ emit_binning_pass(struct fd_context *ctx)
 
        OUT_PKT0(ring, REG_A3XX_RB_MODE_CONTROL, 1);
        OUT_RING(ring, A3XX_RB_MODE_CONTROL_RENDER_MODE(RB_TILING_PASS) |
-                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE);
+                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE |
+                       A3XX_RB_MODE_CONTROL_MRT(0));
 
        for (i = 0; i < 4; i++) {
                OUT_PKT0(ring, REG_A3XX_RB_MRT_CONTROL(i), 1);
@@ -741,7 +858,7 @@ emit_binning_pass(struct fd_context *ctx)
                        A3XX_PC_VSTREAM_CONTROL_N(0));
 
        /* emit IB to binning drawcmds: */
-       OUT_IB(ring, ctx->binning_start, ctx->binning_end);
+       ctx->emit_ib(ring, batch->binning);
        fd_reset_wfi(ctx);
 
        fd_wfi(ctx, ring);
@@ -767,7 +884,8 @@ emit_binning_pass(struct fd_context *ctx)
 
        OUT_PKT0(ring, REG_A3XX_RB_MODE_CONTROL, 2);
        OUT_RING(ring, A3XX_RB_MODE_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
-                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE);
+                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE |
+                       A3XX_RB_MODE_CONTROL_MRT(pfb->nr_cbufs - 1));
        OUT_RING(ring, A3XX_RB_RENDER_CONTROL_ENABLE_GMEM |
                        A3XX_RB_RENDER_CONTROL_ALPHA_TEST_FUNC(FUNC_NEVER) |
                        A3XX_RB_RENDER_CONTROL_BIN_WIDTH(gmem->bin_w));
@@ -780,7 +898,7 @@ emit_binning_pass(struct fd_context *ctx)
                OUT_PKT3(ring, CP_DRAW_INDX, 3);
                OUT_RING(ring, 0x00000000);
                OUT_RING(ring, DRAW(1, DI_SRC_SEL_AUTO_INDEX,
-                               INDEX_SIZE_IGN, IGNORE_VISIBILITY));
+                                                       INDEX_SIZE_IGN, IGNORE_VISIBILITY, 0));
                OUT_RING(ring, 0);             /* NumIndices */
                fd_reset_wfi(ctx);
        }
@@ -806,7 +924,7 @@ fd3_emit_tile_init(struct fd_context *ctx)
        struct fd_gmem_stateobj *gmem = &ctx->gmem;
        uint32_t rb_render_control;
 
-       fd3_emit_restore(ctx);
+       fd3_emit_restore(ctx, ring);
 
        /* note: use gmem->bin_w/h, the bin_w/h parameters may be truncated
         * at the right and bottom edge tiles
@@ -818,9 +936,6 @@ fd3_emit_tile_init(struct fd_context *ctx)
        update_vsc_pipe(ctx);
 
        if (use_hw_binning(ctx)) {
-               /* mark the end of the binning cmds: */
-               fd_ringmarker_mark(ctx->binning_end);
-
                /* emit hw binning pass: */
                emit_binning_pass(ctx);
 
@@ -841,21 +956,6 @@ fd3_emit_tile_prep(struct fd_context *ctx, struct fd_tile *tile)
 {
        struct fd_ringbuffer *ring = ctx->ring;
        struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
-       struct fd_gmem_stateobj *gmem = &ctx->gmem;
-       uint32_t reg;
-
-       OUT_PKT0(ring, REG_A3XX_RB_DEPTH_INFO, 2);
-       reg = A3XX_RB_DEPTH_INFO_DEPTH_BASE(depth_base(ctx));
-       if (pfb->zsbuf) {
-               reg |= A3XX_RB_DEPTH_INFO_DEPTH_FORMAT(fd_pipe2depth(pfb->zsbuf->format));
-       }
-       OUT_RING(ring, reg);
-       if (pfb->zsbuf) {
-               uint32_t cpp = util_format_get_blocksize(pfb->zsbuf->format);
-               OUT_RING(ring, A3XX_RB_DEPTH_PITCH(cpp * gmem->bin_w));
-       } else {
-               OUT_RING(ring, 0x00000000);
-       }
 
        if (ctx->needs_rb_fbd) {
                fd_wfi(ctx, ring);
@@ -867,7 +967,8 @@ fd3_emit_tile_prep(struct fd_context *ctx, struct fd_tile *tile)
 
        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);
+                       A3XX_RB_MODE_CONTROL_MARB_CACHE_SPLIT_MODE |
+                       A3XX_RB_MODE_CONTROL_MRT(MAX2(1, pfb->nr_cbufs) - 1));
 }
 
 /* before IB to rendering cmds: */
@@ -884,6 +985,26 @@ fd3_emit_tile_renderprep(struct fd_context *ctx, struct fd_tile *tile)
        uint32_t x2 = tile->xoff + tile->bin_w - 1;
        uint32_t y2 = tile->yoff + tile->bin_h - 1;
 
+       uint32_t reg;
+
+       OUT_PKT0(ring, REG_A3XX_RB_DEPTH_INFO, 2);
+       reg = A3XX_RB_DEPTH_INFO_DEPTH_BASE(gmem->zsbuf_base[0]);
+       if (pfb->zsbuf) {
+               reg |= A3XX_RB_DEPTH_INFO_DEPTH_FORMAT(fd_pipe2depth(pfb->zsbuf->format));
+       }
+       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));
+               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));
+               }
+       } else {
+               OUT_RING(ring, 0x00000000);
+       }
+
        if (use_hw_binning(ctx)) {
                struct fd_vsc_pipe *pipe = &ctx->pipe[tile->p];
 
@@ -898,8 +1019,8 @@ fd3_emit_tile_renderprep(struct fd_context *ctx, struct fd_tile *tile)
 
 
                OUT_PKT3(ring, CP_SET_BIN_DATA, 2);
-               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) */
+               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) */
                                (tile->p * 4), 0, 0);
        } else {
                OUT_PKT0(ring, REG_A3XX_PC_VSTREAM_CONTROL, 1);
@@ -911,7 +1032,7 @@ fd3_emit_tile_renderprep(struct fd_context *ctx, struct fd_tile *tile)
        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_A3XX_RB_WINDOW_OFFSET, 1);