radeonsi: Fix draws using user index buffer.
[mesa.git] / src / gallium / drivers / nv50 / nv50_surface.c
index 910e18ff03c3e47a0891a0c349769dbb85dfb33f..7a0470c429441a7ffdc7b5a2427ca939786687ef 100644 (file)
 #include "util/u_format.h"
 #include "util/u_surface.h"
 
+#include "tgsi/tgsi_ureg.h"
+
+#include "os/os_thread.h"
+
 #include "nv50_context.h"
 #include "nv50_resource.h"
+#include "nv50_blit.h"
 
 #include "nv50_defs.xml.h"
+#include "nv50_texture.xml.h"
 
 #define NV50_ENG2D_SUPPORTED_FORMATS 0xff0843e080608409ULL
 
@@ -70,121 +76,114 @@ nv50_2d_format(enum pipe_format format)
 }
 
 static int
-nv50_2d_texture_set(struct nouveau_channel *chan, int dst,
-                    struct nv50_miptree *mt, unsigned level, unsigned layer)
+nv50_2d_texture_set(struct nouveau_pushbuf *push, int dst,
+                    struct nv50_miptree *mt, unsigned level, unsigned layer,
+                    enum pipe_format pformat)
 {
    struct nouveau_bo *bo = mt->base.bo;
    uint32_t width, height, depth;
    uint32_t format;
    uint32_t mthd = dst ? NV50_2D_DST_FORMAT : NV50_2D_SRC_FORMAT;
-   uint32_t flags = mt->base.domain | (dst ? NOUVEAU_BO_WR : NOUVEAU_BO_RD);
    uint32_t offset = mt->level[level].offset;
 
-   format = nv50_2d_format(mt->base.base.format);
+   format = nv50_2d_format(pformat);
    if (!format) {
       NOUVEAU_ERR("invalid/unsupported surface format: %s\n",
-                  util_format_name(mt->base.base.format));
+                  util_format_name(pformat));
       return 1;
    }
 
    width = u_minify(mt->base.base.width0, level) << mt->ms_x;
    height = u_minify(mt->base.base.height0, level) << mt->ms_y;
+   depth = u_minify(mt->base.base.depth0, level);
 
    offset = mt->level[level].offset;
    if (!mt->layout_3d) {
       offset += mt->layer_stride * layer;
       depth = 1;
       layer = 0;
+   } else
+   if (!dst) {
+      offset += nv50_mt_zslice_offset(mt, level, layer);
+      layer = 0;
+   }
+
+   if (!nouveau_bo_memtype(bo)) {
+      BEGIN_NV04(push, SUBC_2D(mthd), 2);
+      PUSH_DATA (push, format);
+      PUSH_DATA (push, 1);
+      BEGIN_NV04(push, SUBC_2D(mthd + 0x14), 5);
+      PUSH_DATA (push, mt->level[level].pitch);
+      PUSH_DATA (push, width);
+      PUSH_DATA (push, height);
+      PUSH_DATAh(push, bo->offset + offset);
+      PUSH_DATA (push, bo->offset + offset);
    } else {
-      depth = u_minify(mt->base.base.depth0, level);
-   }
-
-   if (!(bo->tile_flags & NOUVEAU_BO_TILE_LAYOUT_MASK)) {
-      BEGIN_RING(chan, RING_2D_(mthd), 2);
-      OUT_RING  (chan, format);
-      OUT_RING  (chan, 1);
-      BEGIN_RING(chan, RING_2D_(mthd + 0x14), 5);
-      OUT_RING  (chan, mt->level[level].pitch);
-      OUT_RING  (chan, width);
-      OUT_RING  (chan, height);
-      OUT_RELOCh(chan, bo, offset, flags);
-      OUT_RELOCl(chan, bo, offset, flags);
-   } else {
-      BEGIN_RING(chan, RING_2D_(mthd), 5);
-      OUT_RING  (chan, format);
-      OUT_RING  (chan, 0);
-      OUT_RING  (chan, mt->level[level].tile_mode << 4);
-      OUT_RING  (chan, depth);
-      OUT_RING  (chan, layer);
-      BEGIN_RING(chan, RING_2D_(mthd + 0x18), 4);
-      OUT_RING  (chan, width);
-      OUT_RING  (chan, height);
-      OUT_RELOCh(chan, bo, offset, flags);
-      OUT_RELOCl(chan, bo, offset, flags);
+      BEGIN_NV04(push, SUBC_2D(mthd), 5);
+      PUSH_DATA (push, format);
+      PUSH_DATA (push, 0);
+      PUSH_DATA (push, mt->level[level].tile_mode);
+      PUSH_DATA (push, depth);
+      PUSH_DATA (push, layer);
+      BEGIN_NV04(push, SUBC_2D(mthd + 0x18), 4);
+      PUSH_DATA (push, width);
+      PUSH_DATA (push, height);
+      PUSH_DATAh(push, bo->offset + offset);
+      PUSH_DATA (push, bo->offset + offset);
    }
 
 #if 0
    if (dst) {
-      BEGIN_RING(chan, RING_2D_(NV50_2D_CLIP_X), 4);
-      OUT_RING  (chan, 0);
-      OUT_RING  (chan, 0);
-      OUT_RING  (chan, width);
-      OUT_RING  (chan, height);
+      BEGIN_NV04(push, SUBC_2D(NV50_2D_CLIP_X), 4);
+      PUSH_DATA (push, 0);
+      PUSH_DATA (push, 0);
+      PUSH_DATA (push, width);
+      PUSH_DATA (push, height);
    }
 #endif
    return 0;
 }
 
 static int
-nv50_2d_texture_do_copy(struct nouveau_channel *chan,
+nv50_2d_texture_do_copy(struct nouveau_pushbuf *push,
                         struct nv50_miptree *dst, unsigned dst_level,
                         unsigned dx, unsigned dy, unsigned dz,
                         struct nv50_miptree *src, unsigned src_level,
                         unsigned sx, unsigned sy, unsigned sz,
                         unsigned w, unsigned h)
 {
-   static const uint32_t duvdxy[5] =
-   {
-      0x40000000, 0x80000000, 0x00000001, 0x00000002, 0x00000004
-   };
-
+   const enum pipe_format dfmt = dst->base.base.format;
+   const enum pipe_format sfmt = src->base.base.format;
    int ret;
-   uint32_t ctrl;
 
-   ret = MARK_RING(chan, 2 * 16 + 32, 4);
-   if (ret)
-      return ret;
+   if (!PUSH_SPACE(push, 2 * 16 + 32))
+      return PIPE_ERROR;
 
-   ret = nv50_2d_texture_set(chan, 1, dst, dst_level, dz);
+   ret = nv50_2d_texture_set(push, 1, dst, dst_level, dz, dfmt);
    if (ret)
       return ret;
 
-   ret = nv50_2d_texture_set(chan, 0, src, src_level, sz);
+   ret = nv50_2d_texture_set(push, 0, src, src_level, sz, sfmt);
    if (ret)
       return ret;
 
-   /* NOTE: 2D engine doesn't work for MS8 */
-   if (src->ms_x)
-      ctrl = 0x11;
-
-   /* 0/1 = CENTER/CORNER, 00/10 = POINT/BILINEAR */
-   BEGIN_RING(chan, RING_2D(BLIT_CONTROL), 1);
-   OUT_RING  (chan, ctrl);
-   BEGIN_RING(chan, RING_2D(BLIT_DST_X), 4);
-   OUT_RING  (chan, dx << dst->ms_x);
-   OUT_RING  (chan, dy << dst->ms_y);
-   OUT_RING  (chan, w << dst->ms_x);
-   OUT_RING  (chan, h << dst->ms_y);
-   BEGIN_RING(chan, RING_2D(BLIT_DU_DX_FRACT), 4);
-   OUT_RING  (chan, duvdxy[2 + ((int)src->ms_x - (int)dst->ms_x)] & 0xf0000000);
-   OUT_RING  (chan, duvdxy[2 + ((int)src->ms_x - (int)dst->ms_x)] & 0x0000000f);
-   OUT_RING  (chan, duvdxy[2 + ((int)src->ms_y - (int)dst->ms_y)] & 0xf0000000);
-   OUT_RING  (chan, duvdxy[2 + ((int)src->ms_y - (int)dst->ms_y)] & 0x0000000f);
-   BEGIN_RING(chan, RING_2D(BLIT_SRC_X_FRACT), 4);
-   OUT_RING  (chan, 0);
-   OUT_RING  (chan, sx << src->ms_x);
-   OUT_RING  (chan, 0);
-   OUT_RING  (chan, sy << src->ms_y);
+   BEGIN_NV04(push, NV50_2D(BLIT_CONTROL), 1);
+   PUSH_DATA (push, NV50_2D_BLIT_CONTROL_FILTER_POINT_SAMPLE);
+   BEGIN_NV04(push, NV50_2D(BLIT_DST_X), 4);
+   PUSH_DATA (push, dx << dst->ms_x);
+   PUSH_DATA (push, dy << dst->ms_y);
+   PUSH_DATA (push, w << dst->ms_x);
+   PUSH_DATA (push, h << dst->ms_y);
+   BEGIN_NV04(push, NV50_2D(BLIT_DU_DX_FRACT), 4);
+   PUSH_DATA (push, 0);
+   PUSH_DATA (push, 1);
+   PUSH_DATA (push, 0);
+   PUSH_DATA (push, 1);
+   BEGIN_NV04(push, NV50_2D(BLIT_SRC_X_FRACT), 4);
+   PUSH_DATA (push, 0);
+   PUSH_DATA (push, sx << src->ms_x);
+   PUSH_DATA (push, 0);
+   PUSH_DATA (push, sy << src->ms_y);
 
    return 0;
 }
@@ -196,7 +195,7 @@ nv50_resource_copy_region(struct pipe_context *pipe,
                           struct pipe_resource *src, unsigned src_level,
                           const struct pipe_box *src_box)
 {
-   struct nv50_screen *screen = nv50_context(pipe)->screen;
+   struct nv50_context *nv50 = nv50_context(pipe);
    int ret;
    boolean m2mf;
    unsigned dst_layer = dstz, src_layer = src_box->z;
@@ -208,7 +207,8 @@ nv50_resource_copy_region(struct pipe_context *pipe,
       return;
    }
 
-   assert(src->nr_samples == dst->nr_samples);
+   /* 0 and 1 are equal, only supporting 0/1, 2, 4 and 8 */
+   assert((src->nr_samples | 1) == (dst->nr_samples | 1));
 
    m2mf = (src->format == dst->format) ||
       (util_format_get_blocksizebits(src->format) ==
@@ -227,7 +227,7 @@ nv50_resource_copy_region(struct pipe_context *pipe,
                            src_box->x, src_box->y, src_box->z);
 
       for (i = 0; i < src_box->depth; ++i) {
-         nv50_m2mf_transfer_rect(&screen->base.base, &drect, &srect, nx, ny);
+         nv50_m2mf_transfer_rect(nv50, &drect, &srect, nx, ny);
 
          if (nv50_miptree(dst)->layout_3d)
             drect.z++;
@@ -246,16 +246,22 @@ nv50_resource_copy_region(struct pipe_context *pipe,
           (nv50_2d_format_faithful(src->format) &&
            nv50_2d_format_faithful(dst->format)));
 
+   BCTX_REFN(nv50->bufctx, 2D, nv04_resource(src), RD);
+   BCTX_REFN(nv50->bufctx, 2D, nv04_resource(dst), WR);
+   nouveau_pushbuf_bufctx(nv50->base.pushbuf, nv50->bufctx);
+   nouveau_pushbuf_validate(nv50->base.pushbuf);
+
    for (; dst_layer < dstz + src_box->depth; ++dst_layer, ++src_layer) {
-      ret = nv50_2d_texture_do_copy(screen->base.channel,
+      ret = nv50_2d_texture_do_copy(nv50->base.pushbuf,
                                     nv50_miptree(dst), dst_level,
                                     dstx, dsty, dst_layer,
                                     nv50_miptree(src), src_level,
                                     src_box->x, src_box->y, src_layer,
                                     src_box->width, src_box->height);
       if (ret)
-         return;
+         break;
    }
+   nouveau_bufctx_reset(nv50->bufctx, NV50_BIND_2D);
 }
 
 static void
@@ -266,51 +272,56 @@ nv50_clear_render_target(struct pipe_context *pipe,
                          unsigned width, unsigned height)
 {
    struct nv50_context *nv50 = nv50_context(pipe);
-   struct nv50_screen *screen = nv50->screen;
-   struct nouveau_channel *chan = screen->base.channel;
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
    struct nv50_miptree *mt = nv50_miptree(dst->texture);
    struct nv50_surface *sf = nv50_surface(dst);
    struct nouveau_bo *bo = mt->base.bo;
+   unsigned z;
 
-   BEGIN_RING(chan, RING_3D(CLEAR_COLOR(0)), 4);
-   OUT_RINGf (chan, color->f[0]);
-   OUT_RINGf (chan, color->f[1]);
-   OUT_RINGf (chan, color->f[2]);
-   OUT_RINGf (chan, color->f[3]);
+   BEGIN_NV04(push, NV50_3D(CLEAR_COLOR(0)), 4);
+   PUSH_DATAf(push, color->f[0]);
+   PUSH_DATAf(push, color->f[1]);
+   PUSH_DATAf(push, color->f[2]);
+   PUSH_DATAf(push, color->f[3]);
 
-   if (MARK_RING(chan, 18, 2))
+   if (nouveau_pushbuf_space(push, 32 + sf->depth, 1, 0))
       return;
 
-   BEGIN_RING(chan, RING_3D(RT_CONTROL), 1);
-   OUT_RING  (chan, 1);
-   BEGIN_RING(chan, RING_3D(RT_ADDRESS_HIGH(0)), 5);
-   OUT_RELOCh(chan, bo, sf->offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
-   OUT_RELOCl(chan, bo, sf->offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
-   OUT_RING  (chan, nv50_format_table[dst->format].rt);
-   OUT_RING  (chan, mt->level[sf->base.u.tex.level].tile_mode << 4);
-   OUT_RING  (chan, 0);
-   BEGIN_RING(chan, RING_3D(RT_HORIZ(0)), 2);
-   if (nouveau_bo_tile_layout(bo))
-      OUT_RING(chan, sf->width);
+   PUSH_REFN(push, bo, mt->base.domain | NOUVEAU_BO_WR);
+
+   BEGIN_NV04(push, NV50_3D(RT_CONTROL), 1);
+   PUSH_DATA (push, 1);
+   BEGIN_NV04(push, NV50_3D(RT_ADDRESS_HIGH(0)), 5);
+   PUSH_DATAh(push, bo->offset + sf->offset);
+   PUSH_DATA (push, bo->offset + sf->offset);
+   PUSH_DATA (push, nv50_format_table[dst->format].rt);
+   PUSH_DATA (push, mt->level[sf->base.u.tex.level].tile_mode);
+   PUSH_DATA (push, 0);
+   BEGIN_NV04(push, NV50_3D(RT_HORIZ(0)), 2);
+   if (nouveau_bo_memtype(bo))
+      PUSH_DATA(push, sf->width);
    else
-      OUT_RING(chan, NV50_3D_RT_HORIZ_LINEAR | mt->level[0].pitch);
-   OUT_RING  (chan, sf->height);
-   BEGIN_RING(chan, RING_3D(RT_ARRAY_MODE), 1);
-   OUT_RING  (chan, 1);
-
-   if (!nouveau_bo_tile_layout(bo)) {
-      BEGIN_RING(chan, RING_3D(ZETA_ENABLE), 1);
-      OUT_RING  (chan, 0);
+      PUSH_DATA(push, NV50_3D_RT_HORIZ_LINEAR | mt->level[0].pitch);
+   PUSH_DATA (push, sf->height);
+   BEGIN_NV04(push, NV50_3D(RT_ARRAY_MODE), 1);
+   PUSH_DATA (push, 1);
+
+   if (!nouveau_bo_memtype(bo)) {
+      BEGIN_NV04(push, NV50_3D(ZETA_ENABLE), 1);
+      PUSH_DATA (push, 0);
    }
 
    /* NOTE: only works with D3D clear flag (5097/0x143c bit 4) */
 
-   BEGIN_RING(chan, RING_3D(VIEWPORT_HORIZ(0)), 2);
-   OUT_RING  (chan, (width << 16) | dstx);
-   OUT_RING  (chan, (height << 16) | dsty);
+   BEGIN_NV04(push, NV50_3D(VIEWPORT_HORIZ(0)), 2);
+   PUSH_DATA (push, (width << 16) | dstx);
+   PUSH_DATA (push, (height << 16) | dsty);
 
-   BEGIN_RING(chan, RING_3D(CLEAR_BUFFERS), 1);
-   OUT_RING  (chan, 0x3c);
+   BEGIN_NI04(push, NV50_3D(CLEAR_BUFFERS), sf->depth);
+   for (z = 0; z < sf->depth; ++z) {
+      PUSH_DATA (push, 0x3c |
+                 (z << NV50_3D_CLEAR_BUFFERS_LAYER__SHIFT));
+   }
 
    nv50->dirty |= NV50_NEW_FRAMEBUFFER;
 }
@@ -325,49 +336,54 @@ nv50_clear_depth_stencil(struct pipe_context *pipe,
                          unsigned width, unsigned height)
 {
    struct nv50_context *nv50 = nv50_context(pipe);
-   struct nv50_screen *screen = nv50->screen;
-   struct nouveau_channel *chan = screen->base.channel;
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
    struct nv50_miptree *mt = nv50_miptree(dst->texture);
    struct nv50_surface *sf = nv50_surface(dst);
    struct nouveau_bo *bo = mt->base.bo;
    uint32_t mode = 0;
+   unsigned z;
 
-   assert(nouveau_bo_tile_layout(bo)); /* ZETA cannot be linear */
+   assert(nouveau_bo_memtype(bo)); /* ZETA cannot be linear */
 
    if (clear_flags & PIPE_CLEAR_DEPTH) {
-      BEGIN_RING(chan, RING_3D(CLEAR_DEPTH), 1);
-      OUT_RINGf (chan, depth);
+      BEGIN_NV04(push, NV50_3D(CLEAR_DEPTH), 1);
+      PUSH_DATAf(push, depth);
       mode |= NV50_3D_CLEAR_BUFFERS_Z;
    }
 
    if (clear_flags & PIPE_CLEAR_STENCIL) {
-      BEGIN_RING(chan, RING_3D(CLEAR_STENCIL), 1);
-      OUT_RING  (chan, stencil & 0xff);
+      BEGIN_NV04(push, NV50_3D(CLEAR_STENCIL), 1);
+      PUSH_DATA (push, stencil & 0xff);
       mode |= NV50_3D_CLEAR_BUFFERS_S;
    }
 
-   if (MARK_RING(chan, 17, 2))
+   if (nouveau_pushbuf_space(push, 32 + sf->depth, 1, 0))
       return;
 
-   BEGIN_RING(chan, RING_3D(ZETA_ADDRESS_HIGH), 5);
-   OUT_RELOCh(chan, bo, sf->offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
-   OUT_RELOCl(chan, bo, sf->offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
-   OUT_RING  (chan, nv50_format_table[dst->format].rt);
-   OUT_RING  (chan, mt->level[sf->base.u.tex.level].tile_mode << 4);
-   OUT_RING  (chan, 0);
-   BEGIN_RING(chan, RING_3D(ZETA_ENABLE), 1);
-   OUT_RING  (chan, 1);
-   BEGIN_RING(chan, RING_3D(ZETA_HORIZ), 3);
-   OUT_RING  (chan, sf->width);
-   OUT_RING  (chan, sf->height);
-   OUT_RING  (chan, (1 << 16) | 1);
-
-   BEGIN_RING(chan, RING_3D(VIEWPORT_HORIZ(0)), 2);
-   OUT_RING  (chan, (width << 16) | dstx);
-   OUT_RING  (chan, (height << 16) | dsty);
-
-   BEGIN_RING(chan, RING_3D(CLEAR_BUFFERS), 1);
-   OUT_RING  (chan, mode);
+   PUSH_REFN(push, bo, mt->base.domain | NOUVEAU_BO_WR);
+
+   BEGIN_NV04(push, NV50_3D(ZETA_ADDRESS_HIGH), 5);
+   PUSH_DATAh(push, bo->offset + sf->offset);
+   PUSH_DATA (push, bo->offset + sf->offset);
+   PUSH_DATA (push, nv50_format_table[dst->format].rt);
+   PUSH_DATA (push, mt->level[sf->base.u.tex.level].tile_mode);
+   PUSH_DATA (push, 0);
+   BEGIN_NV04(push, NV50_3D(ZETA_ENABLE), 1);
+   PUSH_DATA (push, 1);
+   BEGIN_NV04(push, NV50_3D(ZETA_HORIZ), 3);
+   PUSH_DATA (push, sf->width);
+   PUSH_DATA (push, sf->height);
+   PUSH_DATA (push, (1 << 16) | 1);
+
+   BEGIN_NV04(push, NV50_3D(VIEWPORT_HORIZ(0)), 2);
+   PUSH_DATA (push, (width << 16) | dstx);
+   PUSH_DATA (push, (height << 16) | dsty);
+
+   BEGIN_NI04(push, NV50_3D(CLEAR_BUFFERS), sf->depth);
+   for (z = 0; z < sf->depth; ++z) {
+      PUSH_DATA (push, mode |
+                 (z << NV50_3D_CLEAR_BUFFERS_LAYER__SHIFT));
+   }
 
    nv50->dirty |= NV50_NEW_FRAMEBUFFER;
 }
@@ -378,7 +394,7 @@ nv50_clear(struct pipe_context *pipe, unsigned buffers,
            double depth, unsigned stencil)
 {
    struct nv50_context *nv50 = nv50_context(pipe);
-   struct nouveau_channel *chan = nv50->screen->base.channel;
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
    struct pipe_framebuffer_state *fb = &nv50->framebuffer;
    unsigned i;
    uint32_t mode = 0;
@@ -388,41 +404,59 @@ nv50_clear(struct pipe_context *pipe, unsigned buffers,
       return;
 
    if (buffers & PIPE_CLEAR_COLOR && fb->nr_cbufs) {
-      BEGIN_RING(chan, RING_3D(CLEAR_COLOR(0)), 4);
-      OUT_RINGf (chan, color->f[0]);
-      OUT_RINGf (chan, color->f[1]);
-      OUT_RINGf (chan, color->f[2]);
-      OUT_RINGf (chan, color->f[3]);
+      BEGIN_NV04(push, NV50_3D(CLEAR_COLOR(0)), 4);
+      PUSH_DATAf(push, color->f[0]);
+      PUSH_DATAf(push, color->f[1]);
+      PUSH_DATAf(push, color->f[2]);
+      PUSH_DATAf(push, color->f[3]);
       mode =
          NV50_3D_CLEAR_BUFFERS_R | NV50_3D_CLEAR_BUFFERS_G |
          NV50_3D_CLEAR_BUFFERS_B | NV50_3D_CLEAR_BUFFERS_A;
    }
 
    if (buffers & PIPE_CLEAR_DEPTH) {
-      BEGIN_RING(chan, RING_3D(CLEAR_DEPTH), 1);
-      OUT_RING  (chan, fui(depth));
+      BEGIN_NV04(push, NV50_3D(CLEAR_DEPTH), 1);
+      PUSH_DATA (push, fui(depth));
       mode |= NV50_3D_CLEAR_BUFFERS_Z;
    }
 
    if (buffers & PIPE_CLEAR_STENCIL) {
-      BEGIN_RING(chan, RING_3D(CLEAR_STENCIL), 1);
-      OUT_RING  (chan, stencil & 0xff);
+      BEGIN_NV04(push, NV50_3D(CLEAR_STENCIL), 1);
+      PUSH_DATA (push, stencil & 0xff);
       mode |= NV50_3D_CLEAR_BUFFERS_S;
    }
 
-   BEGIN_RING(chan, RING_3D(CLEAR_BUFFERS), 1);
-   OUT_RING  (chan, mode);
+   BEGIN_NV04(push, NV50_3D(CLEAR_BUFFERS), 1);
+   PUSH_DATA (push, mode);
 
    for (i = 1; i < fb->nr_cbufs; i++) {
-      BEGIN_RING(chan, RING_3D(CLEAR_BUFFERS), 1);
-      OUT_RING  (chan, (i << 6) | 0x3c);
+      BEGIN_NV04(push, NV50_3D(CLEAR_BUFFERS), 1);
+      PUSH_DATA (push, (i << 6) | 0x3c);
    }
 }
 
 
+/* =============================== BLIT CODE ===================================
+ */
+
+struct nv50_blitter
+{
+   struct nv50_program *fp[NV50_BLIT_MAX_TEXTURE_TYPES][NV50_BLIT_MODES];
+   struct nv50_program vp;
+
+   struct nv50_tsc_entry sampler[2]; /* nearest, bilinear */
+
+   pipe_mutex mutex;
+};
+
 struct nv50_blitctx
 {
-   struct nv50_screen *screen;
+   struct nv50_context *nv50;
+   struct nv50_program *fp;
+   uint8_t mode;
+   uint16_t color_mask;
+   uint8_t filter;
+   enum pipe_texture_target target;
    struct {
       struct pipe_framebuffer_state fb;
       struct nv50_program *vp;
@@ -430,34 +464,22 @@ struct nv50_blitctx
       struct nv50_program *fp;
       unsigned num_textures[3];
       unsigned num_samplers[3];
-      struct pipe_sampler_view *texture;
-      struct nv50_tsc_entry *sampler;
-      unsigned dirty;
-      unsigned clip_nr;
+      struct pipe_sampler_view *texture[2];
+      struct nv50_tsc_entry *sampler[2];
+      uint32_t dirty;
    } saved;
-   struct nv50_program vp;
-   struct nv50_program fp;
-   struct nv50_tsc_entry sampler[2]; /* nearest, bilinear */
-   uint32_t fp_offset;
-   uint16_t color_mask;
-   uint8_t filter;
 };
 
 static void
-nv50_blitctx_make_vp(struct nv50_blitctx *blit)
+nv50_blitter_make_vp(struct nv50_blitter *blit)
 {
    static const uint32_t code[] =
    {
-      0x10000001, /* mov b32 o[0x00] s[0x00] */ /* HPOS.x */
-      0x0423c788,
-      0x10000205, /* mov b32 o[0x04] s[0x04] */ /* HPOS.y */
-      0x0423c788,
-      0x10000409, /* mov b32 o[0x08] s[0x08] */ /* TEXC.x */
-      0x0423c788,
-      0x1000060d, /* mov b32 o[0x0c] s[0x0c] */ /* TEXC.y */
-      0x0423c788,
-      0x10000811, /* exit mov b32 o[0x10] s[0x10] */ /* TEXC.z */
-      0x0423c789,
+      0x10000001, 0x0423c788, /* mov b32 o[0x00] s[0x00] */ /* HPOS.x */
+      0x10000205, 0x0423c788, /* mov b32 o[0x04] s[0x04] */ /* HPOS.y */
+      0x10000409, 0x0423c788, /* mov b32 o[0x08] s[0x08] */ /* TEXC.x */
+      0x1000060d, 0x0423c788, /* mov b32 o[0x0c] s[0x0c] */ /* TEXC.y */
+      0x10000811, 0x0423c789, /* mov b32 o[0x10] s[0x10] */ /* TEXC.z */
    };
 
    blit->vp.type = PIPE_SHADER_VERTEX;
@@ -472,199 +494,229 @@ nv50_blitctx_make_vp(struct nv50_blitctx *blit)
    blit->vp.out[1].hw = 2;
    blit->vp.out[1].mask = 0x7;
    blit->vp.out[1].sn = TGSI_SEMANTIC_GENERIC;
+   blit->vp.out[1].si = 8;
    blit->vp.vp.attrs[0] = 0x73;
    blit->vp.vp.psiz = 0x40;
    blit->vp.vp.edgeflag = 0x40;
 }
 
-static void
-nv50_blitctx_make_fp(struct nv50_blitctx *blit)
+void *
+nv50_blitter_make_fp(struct pipe_context *pipe,
+                     unsigned mode,
+                     enum pipe_texture_target ptarg)
 {
-   static const uint32_t code[] =
-   {
-      /* 3 coords RGBA in, RGBA out, also for Z32_FLOAT(_S8X24_UINT) */
-      0x80000000, /* interp $r0 v[0x0] */
-      0x80010004, /* interp $r1 v[0x4] */
-      0x80020009, /* interp $r2 flat v[0x8] */
-      0x00040780,
-      0xf6800001, /* texauto live { $r0,1,2,3 } $t0 $s0 { $r0,1,2 } */
-      0x0000c785, /* exit */
-
-      /* 3 coords ZS in, S encoded in R, Z encoded in GBA (8_UNORM) */
-      0x80000000, /* interp $r0 v[0x00] */
-      0x80010004, /* interp $r1 v[0x04] */
-      0x80020009, /* interp $r2 flat v[0x8] */
-      0x00040780,
-      0xf6800001, /* texauto live { $r0,1,#,# } $t0 $s0 { $r0,1,2 } */
-      0x00000784,
-      0xc03f0009, /* mul f32 $r2 $r0 (2^24 - 1) */
-      0x04b7ffff,
-      0xa0000201, /* cvt f32 $r0 s32 $r1 */
-      0x44014780,
-      0xa0000409, /* cvt rni s32 $r2 f32 $r2 */
-      0x8c004780,
-      0xc0010001, /* mul f32 $r0 $r0 1/0xff */
-      0x03b8080b,
-      0xd03f0405, /* and b32 $r1 $r2 0x0000ff */
-      0x0000000f,
-      0xd000040d, /* and b32 $r3 $r2 0xff0000 */
-      0x000ff003,
-      0xd0000409, /* and b32 $r2 $r2 0x00ff00 */
-      0x00000ff3,
-      0xa0000205, /* cvt f32 $r1 s32 $r1 */
-      0x44014780,
-      0xa000060d, /* cvt f32 $r3 s32 $r3 */
-      0x44014780,
-      0xa0000409, /* cvt f32 $r2 s32 $r2 */
-      0x44014780,
-      0xc0010205, /* mul f32 $r1 $r1 1/0x0000ff */
-      0x03b8080b,
-      0xc001060d, /* mul f32 $r3 $r3 1/0x00ff00 */
-      0x0338080b,
-      0xc0010409, /* mul f32 $r2 $r2 1/0xff0000 */
-      0x0378080b,
-      0xf0000001, /* exit never nop */
-      0xe0000001,
-
-      /* 3 coords ZS in, Z encoded in RGB, S encoded in A (U8_UNORM) */
-      0x80000000, /* interp $r0 v[0x00] */
-      0x80010004, /* interp $r1 v[0x04] */
-      0x80020009, /* interp $r2 flat v[0x8] */
-      0x00040780,
-      0xf6800001, /* texauto live { $r0,1,#,# } $t0 $s0 { $r0,1,2 } */
-      0x00000784,
-      0xc03f0009, /* mul f32 $r2 $r0 (2^24 - 1) */
-      0x04b7ffff,
-      0xa0000281, /* cvt f32 $r3 s32 $r1 */
-      0x44014780,
-      0xa0000409, /* cvt rni s32 $r2 f32 $r2 */
-      0x8c004780,
-      0xc001060d, /* mul f32 $r3 $r3 1/0xff */
-      0x03b8080b,
-      0xd03f0401, /* and b32 $r0 $r2 0x0000ff */
-      0x0000000f,
-      0xd0000405, /* and b32 $r1 $r2 0x00ff00 */
-      0x00000ff3,
-      0xd0000409, /* and b32 $r2 $r2 0xff0000 */
-      0x000ff003,
-      0xa0000001, /* cvt f32 $r0 s32 $r0 */
-      0x44014780,
-      0xa0000205, /* cvt f32 $r1 s32 $r1 */
-      0x44014780,
-      0xa0000409, /* cvt f32 $r2 s32 $r2 */
-      0x44014780,
-      0xc0010001, /* mul f32 $r0 $r0 1/0x0000ff */
-      0x03b8080b,
-      0xc0010205, /* mul f32 $r1 $r1 1/0x00ff00 */
-      0x0378080b,
-      0xc0010409, /* mul f32 $r2 $r2 1/0xff0000 */
-      0x0338080b,
-      0xf0000001, /* exit never nop */
-      0xe0000001
-   };
+   struct ureg_program *ureg;
+   struct ureg_src tc;
+   struct ureg_dst out;
+   struct ureg_dst data;
+
+   const unsigned target = nv50_blit_get_tgsi_texture_target(ptarg);
+
+   boolean tex_rgbaz = FALSE;
+   boolean tex_s = FALSE;
+   boolean cvt_un8 = FALSE;
+
+   if (mode != NV50_BLIT_MODE_PASS &&
+       mode != NV50_BLIT_MODE_Z24X8 &&
+       mode != NV50_BLIT_MODE_X8Z24)
+      tex_s = TRUE;
+
+   if (mode != NV50_BLIT_MODE_X24S8 &&
+       mode != NV50_BLIT_MODE_S8X24 &&
+       mode != NV50_BLIT_MODE_XS)
+      tex_rgbaz = TRUE;
+
+   if (mode != NV50_BLIT_MODE_PASS &&
+       mode != NV50_BLIT_MODE_ZS &&
+       mode != NV50_BLIT_MODE_XS)
+      cvt_un8 = TRUE;
+
+   ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT);
+   if (!ureg)
+      return NULL;
+
+   out = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
+   /* NOTE: use GENERIC[8] so we don't use the TEXCOORD slots on nvc0 */
+   tc = ureg_DECL_fs_input(
+      ureg, TGSI_SEMANTIC_GENERIC, 8, TGSI_INTERPOLATE_LINEAR);
+
+   data = ureg_DECL_temporary(ureg);
+
+   if (tex_s) {
+      ureg_TEX(ureg, ureg_writemask(data, TGSI_WRITEMASK_X),
+               target, tc, ureg_DECL_sampler(ureg, 1));
+      ureg_MOV(ureg, ureg_writemask(data, TGSI_WRITEMASK_Y),
+               ureg_scalar(ureg_src(data), TGSI_SWIZZLE_X));
+   }
+   if (tex_rgbaz) {
+      const unsigned mask = (mode == NV50_BLIT_MODE_PASS) ?
+         TGSI_WRITEMASK_XYZW : TGSI_WRITEMASK_X;
+      ureg_TEX(ureg, ureg_writemask(data, mask),
+               target, tc, ureg_DECL_sampler(ureg, 0));
+   }
+
+   if (cvt_un8) {
+      struct ureg_src mask;
+      struct ureg_src scale;
+      struct ureg_dst outz;
+      struct ureg_dst outs;
+      struct ureg_dst zdst3 = ureg_writemask(data, TGSI_WRITEMASK_XYZ);
+      struct ureg_dst zdst = ureg_writemask(data, TGSI_WRITEMASK_X);
+      struct ureg_dst sdst = ureg_writemask(data, TGSI_WRITEMASK_Y);
+      struct ureg_src zsrc3 = ureg_src(data);
+      struct ureg_src zsrc = ureg_scalar(zsrc3, TGSI_SWIZZLE_X);
+      struct ureg_src ssrc = ureg_scalar(zsrc3, TGSI_SWIZZLE_Y);
+      struct ureg_src zshuf;
+
+      mask = ureg_imm3u(ureg, 0x0000ff, 0x00ff00, 0xff0000);
+      scale = ureg_imm4f(ureg,
+                         1.0f / 0x0000ff, 1.0f / 0x00ff00, 1.0f / 0xff0000,
+                         (1 << 24) - 1);
+
+      if (mode == NV50_BLIT_MODE_Z24S8 ||
+          mode == NV50_BLIT_MODE_X24S8 ||
+          mode == NV50_BLIT_MODE_Z24X8) {
+         outz = ureg_writemask(out, TGSI_WRITEMASK_XYZ);
+         outs = ureg_writemask(out, TGSI_WRITEMASK_W);
+         zshuf = ureg_src(data);
+      } else {
+         outz = ureg_writemask(out, TGSI_WRITEMASK_YZW);
+         outs = ureg_writemask(out, TGSI_WRITEMASK_X);
+         zshuf = ureg_swizzle(zsrc3, TGSI_SWIZZLE_W,
+                              TGSI_SWIZZLE_X, TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Z);
+      }
+
+      if (tex_s) {
+         ureg_I2F(ureg, sdst, ssrc);
+         ureg_MUL(ureg, outs, ssrc, ureg_scalar(scale, TGSI_SWIZZLE_X));
+      }
+
+      if (tex_rgbaz) {
+         ureg_MUL(ureg, zdst, zsrc, ureg_scalar(scale, TGSI_SWIZZLE_W));
+         ureg_F2I(ureg, zdst, zsrc);
+         ureg_AND(ureg, zdst3, zsrc, mask);
+         ureg_I2F(ureg, zdst3, zsrc3);
+         ureg_MUL(ureg, zdst3, zsrc3, scale);
+         ureg_MOV(ureg, outz, zshuf);
+      }
+   } else {
+      unsigned mask = TGSI_WRITEMASK_XYZW;
+
+      if (mode != NV50_BLIT_MODE_PASS) {
+         mask &= ~TGSI_WRITEMASK_ZW;
+         if (!tex_s)
+            mask = TGSI_WRITEMASK_X;
+         if (!tex_rgbaz)
+            mask = TGSI_WRITEMASK_Y;
+      }
+      ureg_MOV(ureg, ureg_writemask(out, mask), ureg_src(data));
+   }
+   ureg_END(ureg);
 
-   blit->fp.type = PIPE_SHADER_FRAGMENT;
-   blit->fp.translated = TRUE;
-   blit->fp.code = (uint32_t *)code; /* const_cast */
-   blit->fp.code_size = sizeof(code);
-   blit->fp.max_gpr = 4;
-   blit->fp.max_out = 4;
-   blit->fp.in_nr = 1;
-   blit->fp.in[0].mask = 0x7; /* last component flat */
-   blit->fp.in[0].linear = 1;
-   blit->fp.in[0].sn = TGSI_SEMANTIC_GENERIC;
-   blit->fp.out_nr = 1;
-   blit->fp.out[0].mask = 0xf;
-   blit->fp.out[0].sn = TGSI_SEMANTIC_COLOR;
-   blit->fp.fp.interp = 0x00020403;
-   blit->fp.gp.primid = 0x80;
+   return ureg_create_shader_and_destroy(ureg, pipe);
 }
 
 static void
-nv50_blitctx_make_sampler(struct nv50_blitctx *blit)
+nv50_blitter_make_sampler(struct nv50_blitter *blit)
 {
    /* clamp to edge, min/max lod = 0, nearest filtering */
 
    blit->sampler[0].id = -1;
 
-   blit->sampler[0].tsc[0] = 0x00000092;
-   blit->sampler[0].tsc[1] = 0x00000051;
+   blit->sampler[0].tsc[0] = NV50_TSC_0_SRGB_CONVERSION_ALLOWED |
+      (NV50_TSC_WRAP_CLAMP_TO_EDGE << NV50_TSC_0_WRAPS__SHIFT) |
+      (NV50_TSC_WRAP_CLAMP_TO_EDGE << NV50_TSC_0_WRAPT__SHIFT) |
+      (NV50_TSC_WRAP_CLAMP_TO_EDGE << NV50_TSC_0_WRAPR__SHIFT);
+   blit->sampler[0].tsc[1] =
+      NV50_TSC_1_MAGF_NEAREST | NV50_TSC_1_MINF_NEAREST | NV50_TSC_1_MIPF_NONE;
 
    /* clamp to edge, min/max lod = 0, bilinear filtering */
 
    blit->sampler[1].id = -1;
 
-   blit->sampler[1].tsc[0] = 0x00000092;
-   blit->sampler[1].tsc[1] = 0x00000062;
+   blit->sampler[1].tsc[0] = blit->sampler[0].tsc[0];
+   blit->sampler[1].tsc[1] =
+      NV50_TSC_1_MAGF_LINEAR | NV50_TSC_1_MINF_LINEAR | NV50_TSC_1_MIPF_NONE;
 }
 
-/* Since shaders cannot export stencil, we cannot copy stencil values when
- * rendering to ZETA, so we attach the ZS surface to a colour render target.
- */
-static INLINE enum pipe_format
-nv50_blit_zeta_to_colour_format(enum pipe_format format)
+unsigned
+nv50_blit_select_mode(const struct pipe_blit_info *info)
 {
-   switch (format) {
-   case PIPE_FORMAT_Z16_UNORM:               return PIPE_FORMAT_R16_UNORM;
+   const unsigned mask = info->mask;
+
+   switch (info->dst.resource->format) {
    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
+   case PIPE_FORMAT_Z24X8_UNORM:
+      switch (mask & PIPE_MASK_ZS) {
+      case PIPE_MASK_ZS: return NV50_BLIT_MODE_Z24S8;
+      case PIPE_MASK_Z:  return NV50_BLIT_MODE_Z24X8;
+      default:
+         return NV50_BLIT_MODE_X24S8;
+      }
    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
-   case PIPE_FORMAT_Z24X8_UNORM:             return PIPE_FORMAT_R8G8B8A8_UNORM;
-   case PIPE_FORMAT_Z32_FLOAT:               return PIPE_FORMAT_R32_FLOAT;
-   case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT: return PIPE_FORMAT_R32G32_FLOAT;
+      switch (mask & PIPE_MASK_ZS) {
+      case PIPE_MASK_ZS: return NV50_BLIT_MODE_S8Z24;
+      case PIPE_MASK_Z:  return NV50_BLIT_MODE_X8Z24;
+      default:
+         return NV50_BLIT_MODE_S8X24;
+      }
+   case PIPE_FORMAT_Z32_FLOAT:
+   case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
+      switch (mask & PIPE_MASK_ZS) {
+      case PIPE_MASK_ZS: return NV50_BLIT_MODE_ZS;
+      case PIPE_MASK_Z:  return NV50_BLIT_MODE_PASS;
+      default:
+         return NV50_BLIT_MODE_XS;
+      }
    default:
-      assert(0);
-      return PIPE_FORMAT_NONE;
+      return NV50_BLIT_MODE_PASS;
    }
 }
 
 static void
-nv50_blitctx_get_color_mask_and_fp(struct nv50_blitctx *blit,
-                                   enum pipe_format format, uint8_t mask)
+nv50_blit_select_fp(struct nv50_blitctx *ctx, const struct pipe_blit_info *info)
 {
-   blit->color_mask = 0;
+   struct nv50_blitter *blitter = ctx->nv50->screen->blitter;
 
-   switch (format) {
-   case PIPE_FORMAT_Z24X8_UNORM:
-   case PIPE_FORMAT_Z24_UNORM_S8_UINT:
-      blit->fp_offset = 160;
-      if (mask & PIPE_MASK_Z)
-         blit->color_mask |= 0x0111;
-      if (mask & PIPE_MASK_S)
-         blit->color_mask |= 0x1000;
-      break;
-   case PIPE_FORMAT_S8_UINT_Z24_UNORM:
-      blit->fp_offset = 24;
-      if (mask & PIPE_MASK_Z)
-         blit->color_mask |= 0x1110;
-      if (mask & PIPE_MASK_S)
-         blit->color_mask |= 0x0001;
-      break;
-   default:
-      blit->fp_offset = 0;
-      if (mask & (PIPE_MASK_R | PIPE_MASK_Z)) blit->color_mask |= 0x0001;
-      if (mask & (PIPE_MASK_G | PIPE_MASK_S)) blit->color_mask |= 0x0010;
-      if (mask & PIPE_MASK_B) blit->color_mask |= 0x0100;
-      if (mask & PIPE_MASK_A) blit->color_mask |= 0x1000;
-      break;
+   const enum pipe_texture_target ptarg =
+      nv50_blit_reinterpret_pipe_texture_target(info->src.resource->target);
+
+   const unsigned targ = nv50_blit_texture_type(ptarg);
+   const unsigned mode = ctx->mode;
+
+   if (!blitter->fp[targ][mode]) {
+      pipe_mutex_lock(blitter->mutex);
+      if (!blitter->fp[targ][mode])
+         blitter->fp[targ][mode] =
+            nv50_blitter_make_fp(&ctx->nv50->base.pipe, mode, ptarg);
+      pipe_mutex_unlock(blitter->mutex);
    }
+   ctx->fp = blitter->fp[targ][mode];
 }
 
 static void
-nv50_blit_set_dst(struct nv50_context *nv50,
-                  struct pipe_resource *res, unsigned level, unsigned layer)
+nv50_blit_set_dst(struct nv50_blitctx *ctx,
+                  struct pipe_resource *res, unsigned level, unsigned layer,
+                  enum pipe_format format)
 {
+   struct nv50_context *nv50 = ctx->nv50;
    struct pipe_context *pipe = &nv50->base.pipe;
    struct pipe_surface templ;
 
-   if (util_format_is_depth_or_stencil(res->format))
-      templ.format = nv50_blit_zeta_to_colour_format(res->format);
+   if (util_format_is_depth_or_stencil(format))
+      templ.format = nv50_blit_zeta_to_colour_format(format);
    else
-      templ.format = res->format;
+      templ.format = format;
 
-   templ.usage = PIPE_USAGE_STREAM;
    templ.u.tex.level = level;
    templ.u.tex.first_layer = templ.u.tex.last_layer = layer;
 
+   if (layer == -1) {
+      templ.u.tex.first_layer = 0;
+      templ.u.tex.last_layer =
+         (res->target == PIPE_TEXTURE_3D ? res->depth0 : res->array_size) - 1;
+   }
+
    nv50->framebuffer.cbufs[0] = nv50_miptree_surface_new(pipe, res, &templ);
    nv50->framebuffer.nr_cbufs = 1;
    nv50->framebuffer.zsbuf = NULL;
@@ -672,126 +724,146 @@ nv50_blit_set_dst(struct nv50_context *nv50,
    nv50->framebuffer.height = nv50->framebuffer.cbufs[0]->height;
 }
 
-static INLINE void
-nv50_blit_fixup_tic_entry(struct pipe_sampler_view *view)
-{
-   struct nv50_tic_entry *ent = nv50_tic_entry(view);
-
-   ent->tic[2] &= ~(1 << 31); /* scaled coordinates, ok with 3d textures ? */
-
-   /* magic: */
-
-   ent->tic[3] = 0x20000000; /* affects quality of near vertical edges in MS8 */
-}
-
 static void
-nv50_blit_set_src(struct nv50_context *nv50,
-                  struct pipe_resource *res, unsigned level, unsigned layer)
+nv50_blit_set_src(struct nv50_blitctx *blit,
+                  struct pipe_resource *res, unsigned level, unsigned layer,
+                  enum pipe_format format, const uint8_t filter)
 {
+   struct nv50_context *nv50 = blit->nv50;
    struct pipe_context *pipe = &nv50->base.pipe;
    struct pipe_sampler_view templ;
+   uint32_t flags;
+   enum pipe_texture_target target;
 
-   templ.format = res->format;
-   templ.u.tex.first_layer = templ.u.tex.last_layer = layer;
+   target = nv50_blit_reinterpret_pipe_texture_target(res->target);
+
+   templ.format = format;
    templ.u.tex.first_level = templ.u.tex.last_level = level;
+   templ.u.tex.first_layer = templ.u.tex.last_layer = layer;
    templ.swizzle_r = PIPE_SWIZZLE_RED;
    templ.swizzle_g = PIPE_SWIZZLE_GREEN;
    templ.swizzle_b = PIPE_SWIZZLE_BLUE;
    templ.swizzle_a = PIPE_SWIZZLE_ALPHA;
 
-   nv50->textures[2][0] = nv50_create_sampler_view(pipe, res, &templ);
+   if (layer == -1) {
+      templ.u.tex.first_layer = 0;
+      templ.u.tex.last_layer =
+         (res->target == PIPE_TEXTURE_3D ? res->depth0 : res->array_size) - 1;
+   }
+
+   flags = res->last_level ? 0 : NV50_TEXVIEW_SCALED_COORDS;
+   if (filter && res->nr_samples == 8)
+      flags |= NV50_TEXVIEW_FILTER_MSAA8;
 
-   nv50_blit_fixup_tic_entry(nv50->textures[2][0]);
+   nv50->textures[2][0] = nv50_create_texture_view(
+      pipe, res, &templ, flags, target);
+   nv50->textures[2][1] = NULL;
 
    nv50->num_textures[0] = nv50->num_textures[1] = 0;
    nv50->num_textures[2] = 1;
+
+   templ.format = nv50_zs_to_s_format(format);
+   if (templ.format != res->format) {
+      nv50->textures[2][1] = nv50_create_texture_view(
+         pipe, res, &templ, flags, target);
+      nv50->num_textures[2] = 2;
+   }
 }
 
 static void
 nv50_blitctx_prepare_state(struct nv50_blitctx *blit)
 {
-   struct nouveau_channel *chan = blit->screen->base.channel;
+   struct nouveau_pushbuf *push = blit->nv50->base.pushbuf;
+
+   if (blit->nv50->cond_query) {
+      BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
+      PUSH_DATA (push, NV50_3D_COND_MODE_ALWAYS);
+   }
 
    /* blend state */
-   BEGIN_RING(chan, RING_3D(COLOR_MASK(0)), 1);
-   OUT_RING  (chan, blit->color_mask);
-   BEGIN_RING(chan, RING_3D(BLEND_ENABLE(0)), 1);
-   OUT_RING  (chan, 0);
-   BEGIN_RING(chan, RING_3D(LOGIC_OP_ENABLE), 1);
-   OUT_RING  (chan, 0);
+   BEGIN_NV04(push, NV50_3D(COLOR_MASK(0)), 1);
+   PUSH_DATA (push, blit->color_mask);
+   BEGIN_NV04(push, NV50_3D(BLEND_ENABLE(0)), 1);
+   PUSH_DATA (push, 0);
+   BEGIN_NV04(push, NV50_3D(LOGIC_OP_ENABLE), 1);
+   PUSH_DATA (push, 0);
 
    /* rasterizer state */
 #ifndef NV50_SCISSORS_CLIPPING
-   BEGIN_RING(chan, RING_3D(SCISSOR_ENABLE(0)), 1);
-   OUT_RING  (chan, 1);
+   BEGIN_NV04(push, NV50_3D(SCISSOR_ENABLE(0)), 1);
+   PUSH_DATA (push, 1);
 #endif
-   BEGIN_RING(chan, RING_3D(VERTEX_TWO_SIDE_ENABLE), 1);
-   OUT_RING  (chan, 0);
-   BEGIN_RING(chan, RING_3D(FRAG_COLOR_CLAMP_EN), 1);
-   OUT_RING  (chan, 0);
-   BEGIN_RING(chan, RING_3D(MULTISAMPLE_ENABLE), 1);
-   OUT_RING  (chan, 0);
-   BEGIN_RING(chan, RING_3D(MSAA_MASK(0)), 4);
-   OUT_RING  (chan, 0xffff);
-   OUT_RING  (chan, 0xffff);
-   OUT_RING  (chan, 0xffff);
-   OUT_RING  (chan, 0xffff);
-   BEGIN_RING(chan, RING_3D(POLYGON_MODE_FRONT), 3);
-   OUT_RING  (chan, NV50_3D_POLYGON_MODE_FRONT_FILL);
-   OUT_RING  (chan, NV50_3D_POLYGON_MODE_BACK_FILL);
-   OUT_RING  (chan, 0);
-   BEGIN_RING(chan, RING_3D(CULL_FACE_ENABLE), 1);
-   OUT_RING  (chan, 0);
-   BEGIN_RING(chan, RING_3D(POLYGON_STIPPLE_ENABLE), 1);
-   OUT_RING  (chan, 0);
-   BEGIN_RING(chan, RING_3D(POLYGON_OFFSET_FILL_ENABLE), 1);
-   OUT_RING  (chan, 0);
+   BEGIN_NV04(push, NV50_3D(VERTEX_TWO_SIDE_ENABLE), 1);
+   PUSH_DATA (push, 0);
+   BEGIN_NV04(push, NV50_3D(FRAG_COLOR_CLAMP_EN), 1);
+   PUSH_DATA (push, 0);
+   BEGIN_NV04(push, NV50_3D(MULTISAMPLE_ENABLE), 1);
+   PUSH_DATA (push, 0);
+   BEGIN_NV04(push, NV50_3D(MSAA_MASK(0)), 4);
+   PUSH_DATA (push, 0xffff);
+   PUSH_DATA (push, 0xffff);
+   PUSH_DATA (push, 0xffff);
+   PUSH_DATA (push, 0xffff);
+   BEGIN_NV04(push, NV50_3D(POLYGON_MODE_FRONT), 3);
+   PUSH_DATA (push, NV50_3D_POLYGON_MODE_FRONT_FILL);
+   PUSH_DATA (push, NV50_3D_POLYGON_MODE_BACK_FILL);
+   PUSH_DATA (push, 0);
+   BEGIN_NV04(push, NV50_3D(CULL_FACE_ENABLE), 1);
+   PUSH_DATA (push, 0);
+   BEGIN_NV04(push, NV50_3D(POLYGON_STIPPLE_ENABLE), 1);
+   PUSH_DATA (push, 0);
+   BEGIN_NV04(push, NV50_3D(POLYGON_OFFSET_FILL_ENABLE), 1);
+   PUSH_DATA (push, 0);
 
    /* zsa state */
-   BEGIN_RING(chan, RING_3D(DEPTH_TEST_ENABLE), 1);
-   OUT_RING  (chan, 0);
-   BEGIN_RING(chan, RING_3D(STENCIL_ENABLE), 1);
-   OUT_RING  (chan, 0);
-   BEGIN_RING(chan, RING_3D(ALPHA_TEST_ENABLE), 1);
-   OUT_RING  (chan, 0);
+   BEGIN_NV04(push, NV50_3D(DEPTH_TEST_ENABLE), 1);
+   PUSH_DATA (push, 0);
+   BEGIN_NV04(push, NV50_3D(STENCIL_ENABLE), 1);
+   PUSH_DATA (push, 0);
+   BEGIN_NV04(push, NV50_3D(ALPHA_TEST_ENABLE), 1);
+   PUSH_DATA (push, 0);
 }
 
 static void
-nv50_blitctx_pre_blit(struct nv50_blitctx *blit, struct nv50_context *nv50)
+nv50_blitctx_pre_blit(struct nv50_blitctx *ctx)
 {
+   struct nv50_context *nv50 = ctx->nv50;
+   struct nv50_blitter *blitter = nv50->screen->blitter;
    int s;
 
-   blit->saved.fb.width = nv50->framebuffer.width;
-   blit->saved.fb.height = nv50->framebuffer.height;
-   blit->saved.fb.nr_cbufs = nv50->framebuffer.nr_cbufs;
-   blit->saved.fb.cbufs[0] = nv50->framebuffer.cbufs[0];
-   blit->saved.fb.zsbuf = nv50->framebuffer.zsbuf;
+   ctx->saved.fb.width = nv50->framebuffer.width;
+   ctx->saved.fb.height = nv50->framebuffer.height;
+   ctx->saved.fb.nr_cbufs = nv50->framebuffer.nr_cbufs;
+   ctx->saved.fb.cbufs[0] = nv50->framebuffer.cbufs[0];
+   ctx->saved.fb.zsbuf = nv50->framebuffer.zsbuf;
 
-   blit->saved.vp = nv50->vertprog;
-   blit->saved.gp = nv50->gmtyprog;
-   blit->saved.fp = nv50->fragprog;
+   ctx->saved.vp = nv50->vertprog;
+   ctx->saved.gp = nv50->gmtyprog;
+   ctx->saved.fp = nv50->fragprog;
 
-   nv50->vertprog = &blit->vp;
+   nv50->vertprog = &blitter->vp;
    nv50->gmtyprog = NULL;
-   nv50->fragprog = &blit->fp;
-
-   blit->saved.clip_nr = nv50->clip.nr;
-
-   nv50->clip.nr = 0;
+   nv50->fragprog = ctx->fp;
 
    for (s = 0; s < 3; ++s) {
-      blit->saved.num_textures[s] = nv50->num_textures[s];
-      blit->saved.num_samplers[s] = nv50->num_samplers[s];
+      ctx->saved.num_textures[s] = nv50->num_textures[s];
+      ctx->saved.num_samplers[s] = nv50->num_samplers[s];
    }
-   blit->saved.texture = nv50->textures[2][0];
-   blit->saved.sampler = nv50->samplers[2][0];
+   ctx->saved.texture[0] = nv50->textures[2][0];
+   ctx->saved.texture[1] = nv50->textures[2][1];
+   ctx->saved.sampler[0] = nv50->samplers[2][0];
+   ctx->saved.sampler[1] = nv50->samplers[2][1];
 
-   nv50->samplers[2][0] = &blit->sampler[blit->filter];
+   nv50->samplers[2][0] = &blitter->sampler[ctx->filter];
+   nv50->samplers[2][1] = &blitter->sampler[ctx->filter];
 
    nv50->num_samplers[0] = nv50->num_samplers[1] = 0;
-   nv50->num_samplers[2] = 1;
+   nv50->num_samplers[2] = 2;
+
+   ctx->saved.dirty = nv50->dirty;
 
-   blit->saved.dirty = nv50->dirty;
+   nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_FB);
+   nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_TEXTURES);
 
    nv50->dirty =
       NV50_NEW_FRAMEBUFFER |
@@ -800,8 +872,9 @@ nv50_blitctx_pre_blit(struct nv50_blitctx *blit, struct nv50_context *nv50)
 }
 
 static void
-nv50_blitctx_post_blit(struct nv50_context *nv50, struct nv50_blitctx *blit)
+nv50_blitctx_post_blit(struct nv50_blitctx *blit)
 {
+   struct nv50_context *nv50 = blit->nv50;
    int s;
 
    pipe_surface_reference(&nv50->framebuffer.cbufs[0], NULL);
@@ -816,16 +889,24 @@ nv50_blitctx_post_blit(struct nv50_context *nv50, struct nv50_blitctx *blit)
    nv50->gmtyprog = blit->saved.gp;
    nv50->fragprog = blit->saved.fp;
 
-   nv50->clip.nr = blit->saved.clip_nr;
-
    pipe_sampler_view_reference(&nv50->textures[2][0], NULL);
+   pipe_sampler_view_reference(&nv50->textures[2][1], NULL);
 
    for (s = 0; s < 3; ++s) {
       nv50->num_textures[s] = blit->saved.num_textures[s];
       nv50->num_samplers[s] = blit->saved.num_samplers[s];
    }
-   nv50->textures[2][0] = blit->saved.texture;
-   nv50->samplers[2][0] = blit->saved.sampler;
+   nv50->textures[2][0] = blit->saved.texture[0];
+   nv50->textures[2][1] = blit->saved.texture[1];
+   nv50->samplers[2][0] = blit->saved.sampler[0];
+   nv50->samplers[2][1] = blit->saved.sampler[1];
+
+   if (nv50->cond_query)
+      nv50->base.pipe.render_condition(&nv50->base.pipe, nv50->cond_query,
+                                       nv50->cond_mode);
+
+   nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_FB);
+   nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_TEXTURES);
 
    nv50->dirty = blit->saved.dirty |
       (NV50_NEW_FRAMEBUFFER | NV50_NEW_SCISSOR | NV50_NEW_SAMPLE_MASK |
@@ -834,41 +915,40 @@ nv50_blitctx_post_blit(struct nv50_context *nv50, struct nv50_blitctx *blit)
        NV50_NEW_VERTPROG | NV50_NEW_GMTYPROG | NV50_NEW_FRAGPROG);
 }
 
+
 static void
-nv50_resource_resolve(struct pipe_context *pipe,
-                      const struct pipe_resolve_info *info)
+nv50_blit_3d(struct nv50_context *nv50, const struct pipe_blit_info *info)
 {
-   struct nv50_context *nv50 = nv50_context(pipe);
-   struct nv50_screen *screen = nv50->screen;
-   struct nv50_blitctx *blit = screen->blitctx;
-   struct nouveau_channel *chan = screen->base.channel;
-   struct pipe_resource *src = info->src.res;
-   struct pipe_resource *dst = info->dst.res;
+   struct nv50_blitctx *blit = nv50->blit;
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
+   struct pipe_resource *src = info->src.resource;
+   struct pipe_resource *dst = info->dst.resource;
+   int32_t minx, maxx, miny, maxy;
+   int32_t i;
    float x0, x1, y0, y1, z;
+   float dz;
    float x_range, y_range;
 
-   nv50_blitctx_get_color_mask_and_fp(blit, dst->format, info->mask);
+   blit->mode = nv50_blit_select_mode(info);
+   blit->color_mask = nv50_blit_derive_color_mask(info);
+   blit->filter = nv50_blit_get_filter(info);
 
-   blit->filter = util_format_is_depth_or_stencil(dst->format) ? 0 : 1;
+   nv50_blit_select_fp(blit, info);
+   nv50_blitctx_pre_blit(blit);
 
-   nv50_blitctx_pre_blit(blit, nv50);
-
-   nv50_blit_set_dst(nv50, dst, info->dst.level, info->dst.layer);
-   nv50_blit_set_src(nv50, src, 0,               info->src.layer);
+   nv50_blit_set_dst(blit, dst, info->dst.level,  0, info->dst.format);
+   nv50_blit_set_src(blit, src, info->src.level, -1, info->src.format,
+                     blit->filter);
 
    nv50_blitctx_prepare_state(blit);
 
    nv50_state_validate(nv50, ~0, 36);
 
-   x_range =
-      (float)(info->src.x1 - info->src.x0) /
-      (float)(info->dst.x1 - info->dst.x0);
-   y_range =
-      (float)(info->src.y1 - info->src.y0) /
-      (float)(info->dst.y1 - info->dst.y0);
+   x_range = (float)info->src.box.width / (float)info->dst.box.width;
+   y_range = (float)info->src.box.height / (float)info->dst.box.height;
 
-   x0 = (float)info->src.x0 - x_range * (float)info->dst.x0;
-   y0 = (float)info->src.y0 - y_range * (float)info->dst.y0;
+   x0 = (float)info->src.box.x - x_range * (float)info->dst.box.x;
+   y0 = (float)info->src.box.y - y_range * (float)info->dst.box.y;
 
    x1 = x0 + 16384.0f * x_range;
    y1 = y0 + 16384.0f * y_range;
@@ -878,14 +958,25 @@ nv50_resource_resolve(struct pipe_context *pipe,
    y0 *= (float)(1 << nv50_miptree(src)->ms_y);
    y1 *= (float)(1 << nv50_miptree(src)->ms_y);
 
-   z = (float)info->src.layer;
+   if (src->last_level > 0) {
+      /* If there are mip maps, GPU always assumes normalized coordinates. */
+      const unsigned l = info->src.level;
+      const float fh = u_minify(src->width0 << nv50_miptree(src)->ms_x, l);
+      const float fv = u_minify(src->height0 << nv50_miptree(src)->ms_y, l);
+      x0 /= fh;
+      x1 /= fh;
+      y0 /= fv;
+      y1 /= fv;
+   }
 
-   BEGIN_RING(chan, RING_3D(FP_START_ID), 1);
-   OUT_RING  (chan,
-              blit->fp.code_base + blit->fp_offset);
+   /* XXX: multiply by 6 for cube arrays ? */
+   dz = (float)info->src.box.depth / (float)info->dst.box.depth;
+   z = (float)info->src.box.z;
+   if (nv50_miptree(src)->layout_3d)
+      z += 0.5f * dz;
 
-   BEGIN_RING(chan, RING_3D(VIEWPORT_TRANSFORM_EN), 1);
-   OUT_RING  (chan, 0);
+   BEGIN_NV04(push, NV50_3D(VIEWPORT_TRANSFORM_EN), 1);
+   PUSH_DATA (push, 0);
 
    /* Draw a large triangle in screen coordinates covering the whole
     * render target, with scissors defining the destination region.
@@ -893,61 +984,320 @@ nv50_resource_resolve(struct pipe_context *pipe,
     * arranged in a way to yield the desired offset and scale.
     */
 
-   BEGIN_RING(chan, RING_3D(SCISSOR_HORIZ(0)), 2);
-   OUT_RING  (chan, (info->dst.x1 << 16) | info->dst.x0);
-   OUT_RING  (chan, (info->dst.y1 << 16) | info->dst.y0);
-
-   BEGIN_RING(chan, RING_3D(VERTEX_BEGIN_GL), 1);
-   OUT_RING  (chan, NV50_3D_VERTEX_BEGIN_GL_PRIMITIVE_TRIANGLES);
-   BEGIN_RING(chan, RING_3D(VTX_ATTR_3F_X(1)), 3);
-   OUT_RINGf (chan, x0);
-   OUT_RINGf (chan, y0);
-   OUT_RINGf (chan, z);
-   BEGIN_RING(chan, RING_3D(VTX_ATTR_2F_X(0)), 2);
-   OUT_RINGf (chan, 0.0f);
-   OUT_RINGf (chan, 0.0f);
-   BEGIN_RING(chan, RING_3D(VTX_ATTR_3F_X(1)), 3);
-   OUT_RINGf (chan, x1);
-   OUT_RINGf (chan, y0);
-   OUT_RINGf (chan, z);
-   BEGIN_RING(chan, RING_3D(VTX_ATTR_2F_X(0)), 2);
-   OUT_RINGf (chan, 16384 << nv50_miptree(dst)->ms_x);
-   OUT_RINGf (chan, 0.0f);
-   BEGIN_RING(chan, RING_3D(VTX_ATTR_3F_X(1)), 3);
-   OUT_RINGf (chan, x0);
-   OUT_RINGf (chan, y1);
-   OUT_RINGf (chan, z);
-   BEGIN_RING(chan, RING_3D(VTX_ATTR_2F_X(0)), 2);
-   OUT_RINGf (chan, 0.0f);
-   OUT_RINGf (chan, 16384 << nv50_miptree(dst)->ms_y);
-   BEGIN_RING(chan, RING_3D(VERTEX_END_GL), 1);
-   OUT_RING  (chan, 0);
+   minx = info->dst.box.x;
+   maxx = info->dst.box.x + info->dst.box.width;
+   miny = info->dst.box.y;
+   maxy = info->dst.box.y + info->dst.box.height;
+   if (info->scissor_enable) {
+      minx = MAX2(minx, info->scissor.minx);
+      maxx = MIN2(maxx, info->scissor.maxx);
+      miny = MAX2(miny, info->scissor.miny);
+      maxy = MIN2(maxy, info->scissor.maxy);
+   }
+   BEGIN_NV04(push, NV50_3D(SCISSOR_HORIZ(0)), 2);
+   PUSH_DATA (push, (maxx << 16) | minx);
+   PUSH_DATA (push, (maxy << 16) | miny);
+
+   for (i = 0; i < info->dst.box.depth; ++i, z += dz) {
+      if (info->dst.box.z + i) {
+         BEGIN_NV04(push, NV50_3D(LAYER), 1);
+         PUSH_DATA (push, info->dst.box.z + i);
+      }
+      PUSH_SPACE(push, 32);
+      BEGIN_NV04(push, NV50_3D(VERTEX_BEGIN_GL), 1);
+      PUSH_DATA (push, NV50_3D_VERTEX_BEGIN_GL_PRIMITIVE_TRIANGLES);
+      BEGIN_NV04(push, NV50_3D(VTX_ATTR_3F_X(1)), 3);
+      PUSH_DATAf(push, x0);
+      PUSH_DATAf(push, y0);
+      PUSH_DATAf(push, z);
+      BEGIN_NV04(push, NV50_3D(VTX_ATTR_2F_X(0)), 2);
+      PUSH_DATAf(push, 0.0f);
+      PUSH_DATAf(push, 0.0f);
+      BEGIN_NV04(push, NV50_3D(VTX_ATTR_3F_X(1)), 3);
+      PUSH_DATAf(push, x1);
+      PUSH_DATAf(push, y0);
+      PUSH_DATAf(push, z);
+      BEGIN_NV04(push, NV50_3D(VTX_ATTR_2F_X(0)), 2);
+      PUSH_DATAf(push, 16384 << nv50_miptree(dst)->ms_x);
+      PUSH_DATAf(push, 0.0f);
+      BEGIN_NV04(push, NV50_3D(VTX_ATTR_3F_X(1)), 3);
+      PUSH_DATAf(push, x0);
+      PUSH_DATAf(push, y1);
+      PUSH_DATAf(push, z);
+      BEGIN_NV04(push, NV50_3D(VTX_ATTR_2F_X(0)), 2);
+      PUSH_DATAf(push, 0.0f);
+      PUSH_DATAf(push, 16384 << nv50_miptree(dst)->ms_y);
+      BEGIN_NV04(push, NV50_3D(VERTEX_END_GL), 1);
+      PUSH_DATA (push, 0);
+   }
+   if (info->dst.box.z + info->dst.box.depth - 1) {
+      BEGIN_NV04(push, NV50_3D(LAYER), 1);
+      PUSH_DATA (push, 0);
+   }
 
    /* re-enable normally constant state */
 
-   BEGIN_RING(chan, RING_3D(VIEWPORT_TRANSFORM_EN), 1);
-   OUT_RING  (chan, 1);
+   BEGIN_NV04(push, NV50_3D(VIEWPORT_TRANSFORM_EN), 1);
+   PUSH_DATA (push, 1);
 
-   nv50_blitctx_post_blit(nv50, blit);
+   nv50_blitctx_post_blit(blit);
+}
+
+static void
+nv50_blit_eng2d(struct nv50_context *nv50, const struct pipe_blit_info *info)
+{
+   struct nouveau_pushbuf *push = nv50->base.pushbuf;
+   struct nv50_miptree *dst = nv50_miptree(info->dst.resource);
+   struct nv50_miptree *src = nv50_miptree(info->src.resource);
+   const int32_t srcx_adj = info->src.box.width < 0 ? -1 : 0;
+   const int32_t srcy_adj = info->src.box.height < 0 ? -1 : 0;
+   const int32_t dz = info->dst.box.z;
+   const int32_t sz = info->src.box.z;
+   uint32_t dstw, dsth;
+   int32_t dstx, dsty;
+   int64_t srcx, srcy;
+   int64_t du_dx, dv_dy;
+   int i;
+   uint32_t mode;
+   const uint32_t mask = nv50_blit_eng2d_get_mask(info);
+
+   mode = nv50_blit_get_filter(info) ?
+      NV50_2D_BLIT_CONTROL_FILTER_BILINEAR :
+      NV50_2D_BLIT_CONTROL_FILTER_POINT_SAMPLE;
+   mode |= (src->base.base.nr_samples > dst->base.base.nr_samples) ?
+      NV50_2D_BLIT_CONTROL_ORIGIN_CORNER : NV50_2D_BLIT_CONTROL_ORIGIN_CENTER;
+
+   du_dx = ((int64_t)info->src.box.width << 32) / info->dst.box.width;
+   dv_dy = ((int64_t)info->src.box.height << 32) / info->dst.box.height;
+
+   nv50_2d_texture_set(push, 1, dst, info->dst.level, dz, info->dst.format);
+   nv50_2d_texture_set(push, 0, src, info->src.level, sz, info->src.format);
+
+   if (info->scissor_enable) {
+      BEGIN_NV04(push, NV50_2D(CLIP_X), 5);
+      PUSH_DATA (push, info->scissor.minx << dst->ms_x);
+      PUSH_DATA (push, info->scissor.miny << dst->ms_y);
+      PUSH_DATA (push, (info->scissor.maxx - info->scissor.minx) << dst->ms_x);
+      PUSH_DATA (push, (info->scissor.maxy - info->scissor.miny) << dst->ms_y);
+      PUSH_DATA (push, 1); /* enable */
+   }
+
+   if (mask != 0xffffffff) {
+      BEGIN_NV04(push, NV50_2D(ROP), 1);
+      PUSH_DATA (push, 0xca); /* DPSDxax */
+      BEGIN_NV04(push, NV50_2D(PATTERN_COLOR_FORMAT), 1);
+      PUSH_DATA (push, NV50_2D_PATTERN_COLOR_FORMAT_32BPP);
+      BEGIN_NV04(push, NV50_2D(PATTERN_COLOR(0)), 4);
+      PUSH_DATA (push, 0x00000000);
+      PUSH_DATA (push, mask);
+      PUSH_DATA (push, 0xffffffff);
+      PUSH_DATA (push, 0xffffffff);
+      BEGIN_NV04(push, NV50_2D(OPERATION), 1);
+      PUSH_DATA (push, NV50_2D_OPERATION_ROP);
+   }
+
+   if (src->ms_x > dst->ms_x || src->ms_y > dst->ms_y) {
+      /* ms_x is always >= ms_y */
+      du_dx <<= src->ms_x - dst->ms_x;
+      dv_dy <<= src->ms_y - dst->ms_y;
+   } else {
+      du_dx >>= dst->ms_x - src->ms_x;
+      dv_dy >>= dst->ms_y - src->ms_y;
+   }
+
+   srcx = (int64_t)(info->src.box.x + srcx_adj) << (src->ms_x + 32);
+   srcy = (int64_t)(info->src.box.y + srcy_adj) << (src->ms_y + 32);
+
+   if (src->base.base.nr_samples > dst->base.base.nr_samples) {
+      /* center src coorinates for proper MS resolve filtering */
+      srcx += (int64_t)src->ms_x << 32;
+      srcy += (int64_t)src->ms_y << 32;
+   }
+
+   dstx = info->dst.box.x << dst->ms_x;
+   dsty = info->dst.box.y << dst->ms_y;
+
+   dstw = info->dst.box.width << dst->ms_x;
+   dsth = info->dst.box.height << dst->ms_y;
+
+   if (dstx < 0) {
+      dstw += dstx;
+      srcx -= du_dx * dstx;
+      dstx = 0;
+   }
+   if (dsty < 0) {
+      dsth += dsty;
+      srcy -= dv_dy * dsty;
+      dsty = 0;
+   }
+
+   BEGIN_NV04(push, NV50_2D(BLIT_CONTROL), 1);
+   PUSH_DATA (push, mode);
+   BEGIN_NV04(push, NV50_2D(BLIT_DST_X), 4);
+   PUSH_DATA (push, dstx);
+   PUSH_DATA (push, dsty);
+   PUSH_DATA (push, dstw);
+   PUSH_DATA (push, dsth);
+   BEGIN_NV04(push, NV50_2D(BLIT_DU_DX_FRACT), 4);
+   PUSH_DATA (push, du_dx);
+   PUSH_DATA (push, du_dx >> 32);
+   PUSH_DATA (push, dv_dy);
+   PUSH_DATA (push, dv_dy >> 32);
+
+   BCTX_REFN(nv50->bufctx, 2D, &dst->base, WR);
+   BCTX_REFN(nv50->bufctx, 2D, &src->base, RD);
+   nouveau_pushbuf_bufctx(nv50->base.pushbuf, nv50->bufctx);
+   if (nouveau_pushbuf_validate(nv50->base.pushbuf))
+      return;
+
+   for (i = 0; i < info->dst.box.depth; ++i) {
+      if (i > 0) {
+         /* no scaling in z-direction possible for eng2d blits */
+         if (dst->layout_3d) {
+            BEGIN_NV04(push, NV50_2D(DST_LAYER), 1);
+            PUSH_DATA (push, info->dst.box.z + i);
+         } else {
+            const unsigned z = info->dst.box.z + i;
+            BEGIN_NV04(push, NV50_2D(DST_ADDRESS_HIGH), 2);
+            PUSH_DATAh(push, dst->base.address + z * dst->layer_stride);
+            PUSH_DATA (push, dst->base.address + z * dst->layer_stride);
+         }
+         if (src->layout_3d) {
+            /* not possible because of depth tiling */
+            assert(0);
+         } else {
+            const unsigned z = info->src.box.z + i;
+            BEGIN_NV04(push, NV50_2D(SRC_ADDRESS_HIGH), 2);
+            PUSH_DATAh(push, src->base.address + z * src->layer_stride);
+            PUSH_DATA (push, src->base.address + z * src->layer_stride);
+         }
+         BEGIN_NV04(push, NV50_2D(BLIT_SRC_Y_INT), 1); /* trigger */
+         PUSH_DATA (push, srcy >> 32);
+      } else {
+         BEGIN_NV04(push, NV50_2D(BLIT_SRC_X_FRACT), 4);
+         PUSH_DATA (push, srcx);
+         PUSH_DATA (push, srcx >> 32);
+         PUSH_DATA (push, srcy);
+         PUSH_DATA (push, srcy >> 32);
+      }
+   }
+   nv50_bufctx_fence(nv50->bufctx, FALSE);
+
+   nouveau_bufctx_reset(nv50->bufctx, NV50_BIND_2D);
+
+   if (info->scissor_enable) {
+      BEGIN_NV04(push, NV50_2D(CLIP_ENABLE), 1);
+      PUSH_DATA (push, 0);
+   }
+   if (mask != 0xffffffff) {
+      BEGIN_NV04(push, NV50_2D(OPERATION), 1);
+      PUSH_DATA (push, NV50_2D_OPERATION_SRCCOPY);
+   }
+}
+
+static void
+nv50_blit(struct pipe_context *pipe, const struct pipe_blit_info *info)
+{
+   struct nv50_context *nv50 = nv50_context(pipe);
+   boolean eng3d = FALSE;
+
+   if (util_format_is_depth_or_stencil(info->dst.resource->format)) {
+      if (!(info->mask & PIPE_MASK_ZS))
+         return;
+      if (info->dst.resource->format == PIPE_FORMAT_Z32_FLOAT ||
+          info->dst.resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)
+         eng3d = TRUE;
+      if (info->filter != PIPE_TEX_FILTER_NEAREST)
+         eng3d = TRUE;
+   } else {
+      if (!(info->mask & PIPE_MASK_RGBA))
+         return;
+      if (info->mask != PIPE_MASK_RGBA)
+         eng3d = TRUE;
+   }
+
+   if (nv50_miptree(info->src.resource)->layout_3d) {
+      eng3d = TRUE;
+   } else
+   if (info->src.box.depth != info->dst.box.depth) {
+      eng3d = TRUE;
+      debug_printf("blit: cannot filter array or cube textures in z direction");
+   }
+
+   if (!eng3d && info->dst.format != info->src.format)
+      if (!nv50_2d_format_faithful(info->dst.format) ||
+          !nv50_2d_format_faithful(info->src.format))
+         eng3d = TRUE;
+
+   if (info->src.resource->nr_samples == 8 &&
+       info->dst.resource->nr_samples <= 1)
+      eng3d = TRUE;
+
+   /* FIXME: can't make this work with eng2d anymore */
+   if (info->src.resource->nr_samples > 1 ||
+       info->dst.resource->nr_samples > 1)
+      eng3d = TRUE;
+
+   /* FIXME: find correct src coordinate adjustments */
+   if ((info->src.box.width !=  info->dst.box.width &&
+        info->src.box.width != -info->dst.box.width) ||
+       (info->src.box.height !=  info->dst.box.height &&
+        info->src.box.height != -info->dst.box.height))
+      eng3d = TRUE;
+
+   if (!eng3d)
+      nv50_blit_eng2d(nv50, info);
+   else
+      nv50_blit_3d(nv50, info);
 }
 
 boolean
-nv50_blitctx_create(struct nv50_screen *screen)
+nv50_blitter_create(struct nv50_screen *screen)
 {
-   screen->blitctx = CALLOC_STRUCT(nv50_blitctx);
-   if (!screen->blitctx) {
-      NOUVEAU_ERR("failed to allocate blit context\n");
+   screen->blitter = CALLOC_STRUCT(nv50_blitter);
+   if (!screen->blitter) {
+      NOUVEAU_ERR("failed to allocate blitter struct\n");
       return FALSE;
    }
 
-   screen->blitctx->screen = screen;
+   pipe_mutex_init(screen->blitter->mutex);
+
+   nv50_blitter_make_vp(screen->blitter);
+   nv50_blitter_make_sampler(screen->blitter);
+
+   return TRUE;
+}
+
+void
+nv50_blitter_destroy(struct nv50_screen *screen)
+{
+   struct nv50_blitter *blitter = screen->blitter;
+   unsigned i, m;
+
+   for (i = 0; i < NV50_BLIT_MAX_TEXTURE_TYPES; ++i) {
+      for (m = 0; m < NV50_BLIT_MODES; ++m) {
+         struct nv50_program *prog = blitter->fp[i][m];
+         if (prog) {
+            nv50_program_destroy(NULL, prog);
+            FREE((void *)prog->pipe.tokens);
+            FREE(prog);
+         }
+      }
+   }
 
-   nv50_blitctx_make_vp(screen->blitctx);
-   nv50_blitctx_make_fp(screen->blitctx);
+   FREE(blitter);
+}
 
-   nv50_blitctx_make_sampler(screen->blitctx);
+boolean
+nv50_blitctx_create(struct nv50_context *nv50)
+{
+   nv50->blit = CALLOC_STRUCT(nv50_blitctx);
+   if (!nv50->blit) {
+      NOUVEAU_ERR("failed to allocate blit context\n");
+      return FALSE;
+   }
 
-   screen->blitctx->color_mask = 0x1111;
+   nv50->blit->nv50 = nv50;
 
    return TRUE;
 }
@@ -958,7 +1308,7 @@ nv50_init_surface_functions(struct nv50_context *nv50)
    struct pipe_context *pipe = &nv50->base.pipe;
 
    pipe->resource_copy_region = nv50_resource_copy_region;
-   pipe->resource_resolve = nv50_resource_resolve;
+   pipe->blit = nv50_blit;
    pipe->clear_render_target = nv50_clear_render_target;
    pipe->clear_depth_stencil = nv50_clear_depth_stencil;
 }