nv50,nvc0: reject R8G8B8A8/X8_UNORM for multisample surfaces
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_surface.c
index cc0a65687dc3b2214c1635e3f7b3605e06518ee0..a4fd17e53247b801070f393ebd37dff8e0e4e0ec 100644 (file)
 #include "util/u_inlines.h"
 #include "util/u_pack_color.h"
 #include "util/u_format.h"
+#include "util/u_surface.h"
 
 #include "nvc0_context.h"
 #include "nvc0_resource.h"
 
-#include "nv50_defs.xml.h"
+#include "nv50/nv50_defs.xml.h"
+
+#define NVC0_ENG2D_SUPPORTED_FORMATS 0xff9ccfe1cce3ccc9ULL
 
 /* return TRUE for formats that can be converted among each other by NVC0_2D */
 static INLINE boolean
 nvc0_2d_format_faithful(enum pipe_format format)
 {
-   switch (format) {
-   case PIPE_FORMAT_B8G8R8A8_UNORM:
-   case PIPE_FORMAT_B8G8R8X8_UNORM:
-   case PIPE_FORMAT_B8G8R8A8_SRGB:
-   case PIPE_FORMAT_B8G8R8X8_SRGB:
-   case PIPE_FORMAT_B5G6R5_UNORM:
-   case PIPE_FORMAT_B5G5R5A1_UNORM:
-   case PIPE_FORMAT_B10G10R10A2_UNORM:
-   case PIPE_FORMAT_R8_UNORM:
-   case PIPE_FORMAT_R32G32B32A32_FLOAT:
-   case PIPE_FORMAT_R32G32B32_FLOAT:
-      return TRUE;
-   default:
-      return FALSE;
-   }
+   uint8_t id = nvc0_format_table[format].rt;
+
+   return (id >= 0xc0) && (NVC0_ENG2D_SUPPORTED_FORMATS & (1ULL << (id - 0xc0)));
 }
 
 static INLINE uint8_t
@@ -62,7 +53,7 @@ nvc0_2d_format(enum pipe_format format)
    /* Hardware values for color formats range from 0xc0 to 0xff,
     * but the 2D engine doesn't support all of them.
     */
-   if ((id >= 0xc0) && (0xff0843e080608409ULL & (1ULL << (id - 0xc0))))
+   if (nvc0_2d_format_faithful(format))
       return id;
 
    switch (util_format_get_blocksize(format)) {
@@ -71,7 +62,11 @@ nvc0_2d_format(enum pipe_format format)
    case 2:
       return NV50_SURFACE_FORMAT_R16_UNORM;
    case 4:
-      return NV50_SURFACE_FORMAT_A8R8G8B8_UNORM;
+      return NV50_SURFACE_FORMAT_BGRA8_UNORM;
+   case 8:
+      return NV50_SURFACE_FORMAT_RGBA16_UNORM;
+   case 16:
+      return NV50_SURFACE_FORMAT_RGBA32_FLOAT;
    default:
       return 0;
    }
@@ -79,7 +74,7 @@ nvc0_2d_format(enum pipe_format format)
 
 static int
 nvc0_2d_texture_set(struct nouveau_channel *chan, int dst,
-                    struct nvc0_miptree *mt, unsigned level, unsigned layer)
+                    struct nv50_miptree *mt, unsigned level, unsigned layer)
 {
    struct nouveau_bo *bo = mt->base.bo;
    uint32_t width, height, depth;
@@ -95,16 +90,20 @@ nvc0_2d_texture_set(struct nouveau_channel *chan, int dst,
       return 1;
    }
 
-   width = u_minify(mt->base.base.width0, level);
-   height = u_minify(mt->base.base.height0, level);
+   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);
+
+   /* layer has to be < depth, and depth > tile depth / 2 */
 
-   offset = mt->level[level].offset;
    if (!mt->layout_3d) {
       offset += mt->layer_stride * layer;
+      layer = 0;
       depth = 1;
+   } else
+   if (!dst) {
+      offset += nvc0_mt_zslice_offset(mt, level, layer);
       layer = 0;
-   } else {
-      depth = u_minify(mt->base.base.depth0, level);
    }
 
    if (!(bo->tile_flags & NOUVEAU_BO_TILE_LAYOUT_MASK)) {
@@ -145,13 +144,19 @@ nvc0_2d_texture_set(struct nouveau_channel *chan, int dst,
 
 static int
 nvc0_2d_texture_do_copy(struct nouveau_channel *chan,
-                        struct nvc0_miptree *dst, unsigned dst_level,
+                        struct nv50_miptree *dst, unsigned dst_level,
                         unsigned dx, unsigned dy, unsigned dz,
-                        struct nvc0_miptree *src, unsigned src_level,
+                        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
+   };
+
    int ret;
+   uint32_t ctrl = 0x00;
 
    ret = MARK_RING(chan, 2 * 16 + 32, 4);
    if (ret)
@@ -165,24 +170,28 @@ nvc0_2d_texture_do_copy(struct nouveau_channel *chan,
    if (ret)
       return ret;
 
-   /* 0/1 = CENTER/CORNER, 10/00 = POINT/BILINEAR */
+   /* 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, 0);
+   OUT_RING  (chan, ctrl);
    BEGIN_RING(chan, RING_2D(BLIT_DST_X), 4);
-   OUT_RING  (chan, dx);
-   OUT_RING  (chan, dy);
-   OUT_RING  (chan, w);
-   OUT_RING  (chan, h);
+   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, 0);
-   OUT_RING  (chan, 1);
-   OUT_RING  (chan, 0);
-   OUT_RING  (chan, 1);
+   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);
+   OUT_RING  (chan, sx << src->ms_x);
    OUT_RING  (chan, 0);
-   OUT_RING  (chan, sy);
+   OUT_RING  (chan, sy << src->ms_x);
 
    return 0;
 }
@@ -196,17 +205,58 @@ nvc0_resource_copy_region(struct pipe_context *pipe,
 {
    struct nvc0_screen *screen = nvc0_context(pipe)->screen;
    int ret;
+   boolean m2mf;
    unsigned dst_layer = dstz, src_layer = src_box->z;
 
-   assert((src->format == dst->format) ||
-          (nvc0_2d_format_faithful(src->format) &&
-           nvc0_2d_format_faithful(dst->format)));
+   /* Fallback for buffers. */
+   if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
+      util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
+                                src, src_level, src_box);
+      return;
+   }
+
+   assert(src->nr_samples == dst->nr_samples);
+
+   m2mf = (src->format == dst->format) ||
+      (util_format_get_blocksizebits(src->format) ==
+       util_format_get_blocksizebits(dst->format));
+
+   nv04_resource(dst)->status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
+
+   if (m2mf) {
+      struct nv50_m2mf_rect drect, srect;
+      unsigned i;
+      unsigned nx = util_format_get_nblocksx(src->format, src_box->width);
+      unsigned ny = util_format_get_nblocksy(src->format, src_box->height);
+
+      nv50_m2mf_rect_setup(&drect, dst, dst_level, dstx, dsty, dstz);
+      nv50_m2mf_rect_setup(&srect, src, src_level,
+                           src_box->x, src_box->y, src_box->z);
+
+      for (i = 0; i < src_box->depth; ++i) {
+         nvc0_m2mf_transfer_rect(&screen->base.base, &drect, &srect, nx, ny);
+
+         if (nv50_miptree(dst)->layout_3d)
+            drect.z++;
+         else
+            drect.base += nv50_miptree(dst)->layer_stride;
+
+         if (nv50_miptree(src)->layout_3d)
+            srect.z++;
+         else
+            srect.base += nv50_miptree(src)->layer_stride;
+      }
+      return;
+   }
+
+   assert(nvc0_2d_format_faithful(src->format));
+   assert(nvc0_2d_format_faithful(dst->format));
 
    for (; dst_layer < dstz + src_box->depth; ++dst_layer, ++src_layer) {
       ret = nvc0_2d_texture_do_copy(screen->base.channel,
-                                    nvc0_miptree(dst), dst_level,
+                                    nv50_miptree(dst), dst_level,
                                     dstx, dsty, dst_layer,
-                                    nvc0_miptree(src), src_level,
+                                    nv50_miptree(src), src_level,
                                     src_box->x, src_box->y, src_layer,
                                     src_box->width, src_box->height);
       if (ret)
@@ -224,8 +274,8 @@ nvc0_clear_render_target(struct pipe_context *pipe,
        struct nvc0_context *nv50 = nvc0_context(pipe);
        struct nvc0_screen *screen = nv50->screen;
        struct nouveau_channel *chan = screen->base.channel;
-       struct nvc0_miptree *mt = nvc0_miptree(dst->texture);
-       struct nvc0_surface *sf = nvc0_surface(dst);
+       struct nv50_miptree *mt = nv50_miptree(dst->texture);
+       struct nv50_surface *sf = nv50_surface(dst);
        struct nouveau_bo *bo = mt->base.bo;
 
        BEGIN_RING(chan, RING_3D(CLEAR_COLOR(0)), 4);
@@ -239,25 +289,28 @@ nvc0_clear_render_target(struct pipe_context *pipe,
 
        BEGIN_RING(chan, RING_3D(RT_CONTROL), 1);
        OUT_RING  (chan, 1);
-       BEGIN_RING(chan, RING_3D(RT_ADDRESS_HIGH(0)), 8);
+       BEGIN_RING(chan, RING_3D(RT_ADDRESS_HIGH(0)), 9);
        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, sf->width);
        OUT_RING  (chan, sf->height);
        OUT_RING  (chan, nvc0_format_table[dst->format].rt);
-       OUT_RING  (chan, mt->level[sf->base.u.tex.level].tile_mode);
-       OUT_RING  (chan, 1);
-       OUT_RING  (chan, 0);
+       OUT_RING  (chan, (mt->layout_3d << 16) |
+              mt->level[sf->base.u.tex.level].tile_mode);
+       OUT_RING  (chan, dst->u.tex.first_layer + sf->depth);
+       OUT_RING  (chan, mt->layer_stride >> 2);
+       OUT_RING  (chan, dst->u.tex.first_layer);
 
-       /* 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_RING(chan, RING_3D(CLIP_RECT_HORIZ(0)), 2);
+       OUT_RING  (chan, ((dstx + width) << 16) | dstx);
+       OUT_RING  (chan, ((dsty + height) << 16) | dsty);
+       IMMED_RING(chan, RING_3D(CLIP_RECTS_EN), 1);
 
        BEGIN_RING(chan, RING_3D(CLEAR_BUFFERS), 1);
        OUT_RING  (chan, 0x3c);
 
+       IMMED_RING(chan, RING_3D(CLIP_RECTS_EN), 0);
+
        nv50->dirty |= NVC0_NEW_FRAMEBUFFER;
 }
 
@@ -273,10 +326,11 @@ nvc0_clear_depth_stencil(struct pipe_context *pipe,
        struct nvc0_context *nv50 = nvc0_context(pipe);
        struct nvc0_screen *screen = nv50->screen;
        struct nouveau_channel *chan = screen->base.channel;
-       struct nvc0_miptree *mt = nvc0_miptree(dst->texture);
-       struct nvc0_surface *sf = nvc0_surface(dst);
+       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;
+       int unk = mt->base.base.target == PIPE_TEXTURE_2D;
 
        if (clear_flags & PIPE_CLEAR_DEPTH) {
                BEGIN_RING(chan, RING_3D(CLEAR_DEPTH), 1);
@@ -298,21 +352,26 @@ nvc0_clear_depth_stencil(struct pipe_context *pipe,
        OUT_RELOCl(chan, bo, sf->offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
        OUT_RING  (chan, nvc0_format_table[dst->format].rt);
        OUT_RING  (chan, mt->level[sf->base.u.tex.level].tile_mode);
-       OUT_RING  (chan, 0);
+       OUT_RING  (chan, mt->layer_stride >> 2);
        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);
+       OUT_RING  (chan, (unk << 16) | (dst->u.tex.first_layer + sf->depth));
+       BEGIN_RING(chan, RING_3D(ZETA_BASE_LAYER), 1);
+       OUT_RING  (chan, dst->u.tex.first_layer);
 
-       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(CLIP_RECT_HORIZ(0)), 2);
+       OUT_RING  (chan, ((dstx + width) << 16) | dstx);
+       OUT_RING  (chan, ((dsty + height) << 16) | dsty);
+       IMMED_RING(chan, RING_3D(CLIP_RECTS_EN), 1);
 
        BEGIN_RING(chan, RING_3D(CLEAR_BUFFERS), 1);
        OUT_RING  (chan, mode);
 
+       IMMED_RING(chan, RING_3D(CLIP_RECTS_EN), 0);
+
        nv50->dirty |= NVC0_NEW_FRAMEBUFFER;
 }
 
@@ -369,9 +428,11 @@ nvc0_clear(struct pipe_context *pipe, unsigned buffers,
 void
 nvc0_init_surface_functions(struct nvc0_context *nvc0)
 {
-       nvc0->pipe.resource_copy_region = nvc0_resource_copy_region;
-       nvc0->pipe.clear_render_target = nvc0_clear_render_target;
-       nvc0->pipe.clear_depth_stencil = nvc0_clear_depth_stencil;
+   struct pipe_context *pipe = &nvc0->base.pipe;
+
+   pipe->resource_copy_region = nvc0_resource_copy_region;
+   pipe->clear_render_target = nvc0_clear_render_target;
+   pipe->clear_depth_stencil = nvc0_clear_depth_stencil;
 }