[Mesa-dev] [PATCH 5/5] nouveau: Use format modifiers in buffer allocation

James Jones jajones at nvidia.com
Wed Feb 5 20:52:47 UTC 2020


The nvc0 nouveau backend already claimed to
support format modifiers, but in practice it
ignored them when allocating buffers outside of a
perfunctory check for the linear modifier in the
first element of the format modifier list.

This change deduces the supported modifiers, if
any, for a given miptree creation request,
prioritizes them based on performance and memory
waste properties, compares the requested modifiers
against the prioritized list of supported
modifiers, and overrides the internal layout
calculations based on the layout defined by the
resulting modifier.

Additionally, if modifiers are provided and none
are compatible with the miptree creation request,
the function now fails.  This brings the nouveau
behavior in line with other drivers such as i965
and etnaviv.

Signed-off-by: James Jones <jajones at nvidia.com>
---
 .../drivers/nouveau/nvc0/nvc0_miptree.c       | 111 ++++++++++++++++--
 1 file changed, 103 insertions(+), 8 deletions(-)

diff --git a/src/gallium/drivers/nouveau/nvc0/nvc0_miptree.c b/src/gallium/drivers/nouveau/nvc0/nvc0_miptree.c
index 20e4c4decb1..02c163e3e8a 100644
--- a/src/gallium/drivers/nouveau/nvc0/nvc0_miptree.c
+++ b/src/gallium/drivers/nouveau/nvc0/nvc0_miptree.c
@@ -132,7 +132,7 @@ nvc0_choose_tiled_storage_type(enum pipe_format format,
 }
 
 static uint32_t
-nvc0_mt_choose_storage_type(struct nv50_miptree *mt, bool compressed)
+nvc0_mt_choose_storage_type(const struct nv50_miptree *mt, bool compressed)
 {
    const unsigned ms = util_logbase2(mt->base.base.nr_samples);
 
@@ -196,7 +196,7 @@ nvc0_miptree_init_layout_video(struct nv50_miptree *mt)
 }
 
 static void
-nvc0_miptree_init_layout_tiled(struct nv50_miptree *mt)
+nvc0_miptree_init_layout_tiled(struct nv50_miptree *mt, uint64_t modifier)
 {
    struct pipe_resource *pt = &mt->base.base;
    unsigned w, h, d, l;
@@ -213,6 +213,9 @@ nvc0_miptree_init_layout_tiled(struct nv50_miptree *mt)
    d = mt->layout_3d ? pt->depth0 : 1;
 
    assert(!mt->ms_mode || !pt->last_level);
+   assert(modifier == DRM_FORMAT_MOD_INVALID ||
+          (!pt->last_level && !mt->layout_3d));
+   assert(modifier != DRM_FORMAT_MOD_LINEAR);
 
    for (l = 0; l <= pt->last_level; ++l) {
       struct nv50_miptree_level *lvl = &mt->level[l];
@@ -222,7 +225,10 @@ nvc0_miptree_init_layout_tiled(struct nv50_miptree *mt)
 
       lvl->offset = mt->total_size;
 
-      lvl->tile_mode = nvc0_tex_choose_tile_dims(nbx, nby, d, mt->layout_3d);
+      if (modifier != DRM_FORMAT_MOD_INVALID)
+         lvl->tile_mode = ((uint32_t)modifier & 0xf) << 4;
+      else
+         lvl->tile_mode = nvc0_tex_choose_tile_dims(nbx, nby, d, mt->layout_3d);
 
       tsx = NVC0_TILE_SIZE_X(lvl->tile_mode); /* x is tile row pitch in bytes */
       tsy = NVC0_TILE_SIZE_Y(lvl->tile_mode);
@@ -289,6 +295,79 @@ nvc0_miptree_get_handle(struct pipe_screen *pscreen,
    return true;
 }
 
+static uint64_t
+nvc0_miptree_select_best_modifier(struct pipe_screen *pscreen,
+                                  const struct nv50_miptree *mt,
+                                  const uint64_t *modifiers,
+                                  unsigned int count)
+{
+   uint64_t prio_supported_mods[] = {
+      DRM_FORMAT_MOD_INVALID,
+      DRM_FORMAT_MOD_INVALID,
+      DRM_FORMAT_MOD_INVALID,
+      DRM_FORMAT_MOD_INVALID,
+      DRM_FORMAT_MOD_INVALID,
+      DRM_FORMAT_MOD_INVALID,
+      DRM_FORMAT_MOD_LINEAR,
+   };
+   const uint32_t uc_kind = nvc0_mt_choose_storage_type(mt, false);
+   int top_mod_slot = ARRAY_SIZE(prio_supported_mods);
+   unsigned int i;
+   int p;
+
+   if (uc_kind != 0u) {
+      const struct pipe_resource *pt = &mt->base.base;
+      const unsigned nbx = util_format_get_nblocksx(pt->format, pt->width0);
+      const unsigned nby = util_format_get_nblocksy(pt->format, pt->height0);
+      const uint32_t lbh_preferred =
+         NVC0_TILE_MODE_Y(nvc0_tex_choose_tile_dims(nbx, nby, 1u, false));
+      uint32_t lbh = lbh_preferred;
+      bool dec_lbh = true;
+      const uint8_t s = nouveau_screen(pscreen)->tegra_sector_layout ? 0 : 1;
+
+      for (i = 0; i <= 5u; i++) {
+         assert(lbh <= 5u);
+         prio_supported_mods[i] =
+            DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, s, 0, uc_kind, lbh);
+
+         /*
+          * The preferred block height is the largest block size that doesn't
+          * waste excessive space with unused padding bytes relative to the
+          * height of the image.  Construct the priority array such that
+          * the preferred block height is highest priority, followed by
+          * progressively smaller block sizes down to a block height of one,
+          * followed by progressively larger (more wasteful) block sizes up
+          * to 5.
+          */
+         if (lbh == 0u) {
+            lbh = lbh_preferred + 1u;
+            dec_lbh = false;
+         } else if (dec_lbh) {
+            lbh--;
+         } else {
+            lbh++;
+         }
+      }
+   }
+
+   assert(prio_supported_mods[ARRAY_SIZE(prio_supported_mods) - 1] ==
+          DRM_FORMAT_MOD_LINEAR);
+
+   for (i = 0u; i < count; i++) {
+      for (p = 0; p < ARRAY_SIZE(prio_supported_mods); p++) {
+         if (prio_supported_mods[p] == modifiers[i]) {
+            if (top_mod_slot > p) top_mod_slot = p;
+            break;
+         }
+      }
+   }
+
+   if (top_mod_slot >= ARRAY_SIZE(prio_supported_mods))
+       return DRM_FORMAT_MOD_INVALID;
+
+   return prio_supported_mods[top_mod_slot];
+}
+
 const struct u_resource_vtbl nvc0_miptree_vtbl =
 {
    nvc0_miptree_get_handle,         /* get_handle */
@@ -311,6 +390,7 @@ nvc0_miptree_create(struct pipe_screen *pscreen,
    int ret;
    union nouveau_bo_config bo_config;
    uint32_t bo_flags;
+   uint64_t modifier = DRM_FORMAT_MOD_INVALID;
 
    if (!mt)
       return NULL;
@@ -334,13 +414,27 @@ nvc0_miptree_create(struct pipe_screen *pscreen,
       }
    }
 
-   if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_LINEAR)
-      pt->flags |= NOUVEAU_RESOURCE_FLAG_LINEAR;
-
    if (pt->bind & PIPE_BIND_LINEAR)
       pt->flags |= NOUVEAU_RESOURCE_FLAG_LINEAR;
 
-   bo_config.nvc0.memtype = nvc0_mt_choose_storage_type(mt, compressed);
+   if (count > 0) {
+      modifier = nvc0_miptree_select_best_modifier(pscreen, mt,
+                                                   modifiers, count);
+
+      if (modifier == DRM_FORMAT_MOD_INVALID) {
+         FREE(mt);
+         return NULL;
+      }
+
+      if (modifier == DRM_FORMAT_MOD_LINEAR) {
+         pt->flags |= NOUVEAU_RESOURCE_FLAG_LINEAR;
+         bo_config.nvc0.memtype = 0;
+      } else {
+         bo_config.nvc0.memtype = (modifier >> 12) & 0xff;
+      }
+   } else {
+      bo_config.nvc0.memtype = nvc0_mt_choose_storage_type(mt, compressed);
+   }
 
    if (!nvc0_miptree_init_ms_mode(mt)) {
       FREE(mt);
@@ -348,10 +442,11 @@ nvc0_miptree_create(struct pipe_screen *pscreen,
    }
 
    if (unlikely(pt->flags & NVC0_RESOURCE_FLAG_VIDEO)) {
+      assert(modifier == DRM_FORMAT_MOD_INVALID);
       nvc0_miptree_init_layout_video(mt);
    } else
    if (likely(bo_config.nvc0.memtype)) {
-      nvc0_miptree_init_layout_tiled(mt);
+      nvc0_miptree_init_layout_tiled(mt, modifier);
    } else
    if (!nv50_miptree_init_layout_linear(mt, 128)) {
       FREE(mt);
-- 
2.17.1



More information about the mesa-dev mailing list