nv50: account for pesky prefetch in size calculation of linear textures
[mesa.git] / src / gallium / drivers / nv50 / nv50_miptree.c
index bc81604508b3a92cc62b3d2daabef0695fdd8e5a..ed0f8c2b2f1f4ad396f22c553223f43bf5c7ed3b 100644 (file)
 #include "nv50_context.h"
 #include "nv50_resource.h"
 
-static INLINE uint32_t
+uint32_t
+nv50_tex_choose_tile_dims_helper(unsigned nx, unsigned ny, unsigned nz)
+{
+   uint32_t tile_mode = 0x000;
+
+   if (ny > 64) tile_mode = 0x040; /* height 128 tiles */
+   else
+   if (ny > 32) tile_mode = 0x030; /* height 64 tiles */
+   else
+   if (ny > 16) tile_mode = 0x020; /* height 32 tiles */
+   else
+   if (ny >  8) tile_mode = 0x010; /* height 16 tiles */
+
+   if (nz == 1)
+      return tile_mode;
+   else
+      if (tile_mode > 0x020)
+         tile_mode = 0x020;
+
+   if (nz > 16 && tile_mode < 0x020)
+      return tile_mode | 0x500; /* depth 32 tiles */
+   if (nz > 8) return tile_mode | 0x400; /* depth 16 tiles */
+   if (nz > 4) return tile_mode | 0x300; /* depth 8 tiles */
+   if (nz > 2) return tile_mode | 0x200; /* depth 4 tiles */
+
+   return tile_mode | 0x100;
+}
+
+static uint32_t
 nv50_tex_choose_tile_dims(unsigned nx, unsigned ny, unsigned nz)
 {
-   return nvc0_tex_choose_tile_dims(nx, ny * 2, nz) >> 4;
+   return nv50_tex_choose_tile_dims_helper(nx, ny * 2, nz);
 }
 
 static uint32_t
 nv50_mt_choose_storage_type(struct nv50_miptree *mt, boolean compressed)
 {
-   const unsigned ms = util_logbase2(mt->base.base.nr_samples);
+   const unsigned ms = mt->ms_x + mt->ms_y;
 
    uint32_t tile_flags;
 
-   if (mt->base.base.bind & PIPE_BIND_CURSOR)
-      return NOUVEAU_BO_TILE_SCANOUT;
+   if (unlikely(mt->base.base.flags & NOUVEAU_RESOURCE_FLAG_LINEAR))
+      return 0;
+   if (unlikely(mt->base.base.bind & PIPE_BIND_CURSOR))
+      return 0;
 
    switch (mt->base.base.format) {
    case PIPE_FORMAT_Z16_UNORM:
-      tile_flags = 0x6c00 + (ms << 8);
+      tile_flags = 0x6c + ms;
       break;
-   case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
-      tile_flags = 0x1800 + (ms << 8);
+   case PIPE_FORMAT_S8_UINT_Z24_UNORM:
+      tile_flags = 0x18 + ms;
       break;
    case PIPE_FORMAT_Z24X8_UNORM:
-   case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
-      tile_flags = 0x22800 + (ms << 8);
+   case PIPE_FORMAT_Z24_UNORM_S8_UINT:
+      tile_flags = 0x128 + ms;
       break;
    case PIPE_FORMAT_Z32_FLOAT:
-      tile_flags = 0x4000 + (ms << 8);
+      tile_flags = 0x40 + ms;
       break;
-   case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED:
-      tile_flags = 0x6000 + (ms << 8);
+   case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
+      tile_flags = 0x60 + ms;
       break;
    default:
       switch (util_format_get_blocksizebits(mt->base.base.format)) {
       case 128:
          assert(ms < 3);
-         tile_flags = 0x7400;
+         tile_flags = 0x74;
          break;
       case 64:
          switch (ms) {
-         case 2: tile_flags = 0x17c00; break;
-         case 3: tile_flags = 0x17d00; break;
+         case 2: tile_flags = 0xfc; break;
+         case 3: tile_flags = 0xfd; break;
          default:
-            tile_flags = 0x7000;
+            tile_flags = 0x70;
             break;
          }
          break;
       case 32:
          if (mt->base.base.bind & PIPE_BIND_SCANOUT) {
             assert(ms == 0);
-            tile_flags = 0x7a00;
+            tile_flags = 0x7a;
          } else {
             switch (ms) {
-            case 2: tile_flags = 0x17800; break;
-            case 3: tile_flags = 0x17900; break;
+            case 2: tile_flags = 0xf8; break;
+            case 3: tile_flags = 0xf9; break;
             default:
-               tile_flags = 0x7000;
+               tile_flags = 0x70;
                break;
             }
          }
          break;
       case 16:
       case 8:
-         tile_flags = 0x7000;
+         tile_flags = 0x70;
          break;
       default:
          return 0;
@@ -101,11 +131,8 @@ nv50_mt_choose_storage_type(struct nv50_miptree *mt, boolean compressed)
          tile_flags = 0;
    }
 
-   if (mt->base.base.bind & (PIPE_BIND_SCANOUT | PIPE_BIND_CURSOR))
-      tile_flags |= NOUVEAU_BO_TILE_SCANOUT;
-
    if (!compressed)
-      tile_flags &= ~0x30000;
+      tile_flags &= ~0x180;
 
    return tile_flags;
 }
@@ -115,7 +142,14 @@ nv50_miptree_destroy(struct pipe_screen *pscreen, struct pipe_resource *pt)
 {
    struct nv50_miptree *mt = nv50_miptree(pt);
 
-   nouveau_screen_bo_release(pscreen, mt->base.bo);
+   nouveau_bo_ref(NULL, &mt->base.bo);
+
+   nouveau_fence_ref(NULL, &mt->base.fence);
+   nouveau_fence_ref(NULL, &mt->base.fence_wr);
+
+   NOUVEAU_DRV_STAT(nouveau_screen(pscreen), tex_obj_current_count, -1);
+   NOUVEAU_DRV_STAT(nouveau_screen(pscreen), tex_obj_current_bytes,
+                    -(uint64_t)mt->total_size);
 
    FREE(mt);
 }
@@ -131,8 +165,7 @@ nv50_miptree_get_handle(struct pipe_screen *pscreen,
    if (!mt || !mt->base.bo)
       return FALSE;
 
-   stride = util_format_get_stride(mt->base.base.format,
-                                   mt->base.base.width0);
+   stride = mt->level[0].pitch;
 
    return nouveau_screen_bo_get_handle(pscreen,
                                        mt->base.bo,
@@ -144,8 +177,6 @@ const struct u_resource_vtbl nv50_miptree_vtbl =
 {
    nv50_miptree_get_handle,         /* get_handle */
    nv50_miptree_destroy,            /* resource_destroy */
-   nv50_miptree_transfer_new,       /* get_transfer */
-   nv50_miptree_transfer_del,       /* transfer_destroy */
    nv50_miptree_transfer_map,       /* transfer_map */
    u_default_transfer_flush_region, /* transfer_flush_region */
    nv50_miptree_transfer_unmap,     /* transfer_unmap */
@@ -185,6 +216,8 @@ boolean
 nv50_miptree_init_layout_linear(struct nv50_miptree *mt)
 {
    struct pipe_resource *pt = &mt->base.base;
+   const unsigned blocksize = util_format_get_blocksize(pt->format);
+   unsigned h = pt->height0;
 
    if (util_format_is_depth_or_stencil(pt->format))
       return FALSE;
@@ -194,9 +227,13 @@ nv50_miptree_init_layout_linear(struct nv50_miptree *mt)
    if (mt->ms_x | mt->ms_y)
       return FALSE;
 
-   mt->level[0].pitch = align(pt->width0, 64);
+   mt->level[0].pitch = align(pt->width0 * blocksize, 64);
+
+   /* Account for very generous prefetch (allocate size as if tiled). */
+   h = MAX2(h, 8);
+   h = util_next_power_of_two(h);
 
-   mt->total_size = mt->level[0].pitch * pt->height0;
+   mt->total_size = mt->level[0].pitch * h;
 
    return TRUE;
 }
@@ -256,7 +293,8 @@ nv50_miptree_create(struct pipe_screen *pscreen,
    struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
    struct pipe_resource *pt = &mt->base.base;
    int ret;
-   uint32_t tile_flags;
+   union nouveau_bo_config bo_config;
+   uint32_t bo_flags;
 
    if (!mt)
       return NULL;
@@ -266,30 +304,34 @@ nv50_miptree_create(struct pipe_screen *pscreen,
    pipe_reference_init(&pt->reference, 1);
    pt->screen = pscreen;
 
-   tile_flags = nv50_mt_choose_storage_type(mt, TRUE);
+   bo_config.nv50.memtype = nv50_mt_choose_storage_type(mt, TRUE);
 
    if (!nv50_miptree_init_ms_mode(mt)) {
       FREE(mt);
       return NULL;
    }
 
-   if (tile_flags & NOUVEAU_BO_TILE_LAYOUT_MASK) {
+   if (bo_config.nv50.memtype != 0) {
       nv50_miptree_init_layout_tiled(mt);
    } else
    if (!nv50_miptree_init_layout_linear(mt)) {
       FREE(mt);
       return NULL;
    }
+   bo_config.nv50.tile_mode = mt->level[0].tile_mode;
+
+   bo_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP;
+   if (mt->base.base.bind & (PIPE_BIND_CURSOR | PIPE_BIND_DISPLAY_TARGET))
+      bo_flags |= NOUVEAU_BO_CONTIG;
 
-   ret = nouveau_bo_new_tile(dev, NOUVEAU_BO_VRAM, 4096,
-                             mt->total_size,
-                             mt->level[0].tile_mode, tile_flags,
-                             &mt->base.bo);
+   ret = nouveau_bo_new(dev, bo_flags, 4096, mt->total_size, &bo_config,
+                        &mt->base.bo);
    if (ret) {
       FREE(mt);
       return NULL;
    }
    mt->base.domain = NOUVEAU_BO_VRAM;
+   mt->base.address = mt->base.bo->offset;
 
    return pt;
 }
@@ -319,6 +361,8 @@ nv50_miptree_from_handle(struct pipe_screen *pscreen,
       FREE(mt);
       return NULL;
    }
+   mt->base.domain = NOUVEAU_BO_VRAM;
+   mt->base.address = mt->base.bo->offset;
 
    mt->base.base = *templ;
    mt->base.vtbl = &nv50_miptree_vtbl;
@@ -326,7 +370,7 @@ nv50_miptree_from_handle(struct pipe_screen *pscreen,
    mt->base.base.screen = pscreen;
    mt->level[0].pitch = stride;
    mt->level[0].offset = 0;
-   mt->level[0].tile_mode = mt->base.bo->tile_mode;
+   mt->level[0].tile_mode = mt->base.bo->config.nv50.tile_mode;
 
    /* no need to adjust bo reference count */
    return &mt->base.base;
@@ -371,7 +415,7 @@ nv50_surface_from_miptree(struct nv50_miptree *mt,
    pipe_resource_reference(&ps->texture, &mt->base.base);
 
    ps->format = templ->format;
-   ps->usage = templ->usage;
+   ps->writable = templ->writable;
    ps->u.tex.level = templ->u.tex.level;
    ps->u.tex.first_layer = templ->u.tex.first_layer;
    ps->u.tex.last_layer = templ->u.tex.last_layer;
@@ -420,13 +464,3 @@ nv50_miptree_surface_new(struct pipe_context *pipe,
 
    return &ns->base;
 }
-
-void
-nv50_miptree_surface_del(struct pipe_context *pipe, struct pipe_surface *ps)
-{
-   struct nv50_surface *s = nv50_surface(ps);
-
-   pipe_resource_reference(&ps->texture, NULL);
-
-   FREE(s);
-}