[Mesa-dev] [PATCH 17/23] anv/formats: Add an anv_get_format helper

Jason Ekstrand jason at jlekstrand.net
Mon May 16 19:08:22 UTC 2016


This commit removes anv_format_for_vk_format and adds an anv_get_format
helper.  The anv_get_format helper returns the anv_format by-value.  Unlike
anv_format_for_vk_format the format returned by anv_get_format is 100%
accurate and includes any tweaks needed for tiled vs. linear.
anv_get_isl_format is now just a wrapper around anv_get_format that picks
off just the isl_format.
---
 src/intel/vulkan/anv_formats.c        | 80 ++++++++++++++++-------------------
 src/intel/vulkan/anv_image.c          | 24 +++++------
 src/intel/vulkan/anv_meta_copy.c      |  3 +-
 src/intel/vulkan/anv_private.h        | 14 +++---
 src/intel/vulkan/genX_pipeline_util.h |  3 +-
 5 files changed, 58 insertions(+), 66 deletions(-)

diff --git a/src/intel/vulkan/anv_formats.c b/src/intel/vulkan/anv_formats.c
index 80b42a3..4d5d3ce 100644
--- a/src/intel/vulkan/anv_formats.c
+++ b/src/intel/vulkan/anv_formats.c
@@ -234,31 +234,22 @@ static const struct anv_format anv_formats[] = {
 
 #undef fmt
 
-const struct anv_format *
-anv_format_for_vk_format(VkFormat format)
-{
-   return &anv_formats[format];
-}
-
 /**
  * Exactly one bit must be set in \a aspect.
  */
-enum isl_format
-anv_get_isl_format(VkFormat vk_format, VkImageAspectFlags aspect,
-                   VkImageTiling tiling, struct anv_format_swizzle *swizzle)
+struct anv_format
+anv_get_format(VkFormat vk_format, VkImageAspectFlags aspect,
+               VkImageTiling tiling)
 {
-   const enum isl_format isl_format = anv_formats[vk_format].isl_format;
-
-   if (swizzle)
-      *swizzle = anv_formats[vk_format].swizzle;
+   struct anv_format format = anv_formats[vk_format];
 
    const struct isl_format_layout *isl_layout =
-      isl_format_get_layout(isl_format);
+      isl_format_get_layout(format.isl_format);
 
    switch (aspect) {
    case VK_IMAGE_ASPECT_COLOR_BIT:
-      if (isl_format == ISL_FORMAT_UNSUPPORTED) {
-         return ISL_FORMAT_UNSUPPORTED;
+      if (format.isl_format == ISL_FORMAT_UNSUPPORTED) {
+         return format;
       } else if (tiling == VK_IMAGE_TILING_OPTIMAL &&
                  !util_is_power_of_two(isl_layout->bs)) {
          /* Tiled formats *must* be power-of-two because we need up upload
@@ -266,27 +257,29 @@ anv_get_isl_format(VkFormat vk_format, VkImageAspectFlags aspect,
           * this by switching them over to RGBX or RGBA formats under the
           * hood.
           */
-         enum isl_format rgbx = isl_format_rgb_to_rgbx(isl_format);
+         enum isl_format rgbx = isl_format_rgb_to_rgbx(format.isl_format);
          if (rgbx != ISL_FORMAT_UNSUPPORTED)
-            return rgbx;
+            format.isl_format = rgbx;
          else
-            return isl_format_rgb_to_rgba(isl_format);
+            format.isl_format = isl_format_rgb_to_rgba(format.isl_format);
+         return format;
       } else {
-         return isl_format;
+         return format;
       }
 
    case VK_IMAGE_ASPECT_DEPTH_BIT:
    case (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT):
       assert(vk_format_aspects(vk_format) & VK_IMAGE_ASPECT_DEPTH_BIT);
-      return isl_format;
+      return format;
 
    case VK_IMAGE_ASPECT_STENCIL_BIT:
       assert(vk_format_aspects(vk_format) & VK_IMAGE_ASPECT_STENCIL_BIT);
-      return ISL_FORMAT_R8_UINT;
+      format.isl_format = ISL_FORMAT_R8_UINT;
+      return format;
 
    default:
       unreachable("bad VkImageAspect");
-      return ISL_FORMAT_UNSUPPORTED;
+      return format;
    }
 }
 
@@ -294,12 +287,12 @@ anv_get_isl_format(VkFormat vk_format, VkImageAspectFlags aspect,
 
 static VkFormatFeatureFlags
 get_image_format_properties(int gen, enum isl_format base,
-                            enum isl_format actual,
-                            struct anv_format_swizzle swizzle)
+                            struct anv_format format)
 {
-   const struct brw_surface_format_info *info = &surface_formats[actual];
+   const struct brw_surface_format_info *info =
+      &surface_formats[format.isl_format];
 
-   if (actual == ISL_FORMAT_UNSUPPORTED || !info->exists)
+   if (format.isl_format == ISL_FORMAT_UNSUPPORTED || !info->exists)
       return 0;
 
    VkFormatFeatureFlags flags = 0;
@@ -315,12 +308,12 @@ get_image_format_properties(int gen, enum isl_format base,
     * moved, then blending won't work correctly.  The PRM tells us
     * straight-up not to render to such a surface.
     */
-   if (info->render_target <= gen && swizzle.a == 3) {
+   if (info->render_target <= gen && format.swizzle.a == 3) {
       flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
                VK_FORMAT_FEATURE_BLIT_DST_BIT;
    }
 
-   if (info->alpha_blend <= gen && swizzle.a == 3)
+   if (info->alpha_blend <= gen && format.swizzle.a == 3)
       flags |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
 
    /* Load/store is determined based on base format.  This prevents RGB
@@ -378,18 +371,17 @@ anv_physical_device_get_format_properties(struct anv_physical_device *physical_d
       tiled |= VK_FORMAT_FEATURE_BLIT_SRC_BIT |
                VK_FORMAT_FEATURE_BLIT_DST_BIT;
    } else {
-      enum isl_format linear_fmt, tiled_fmt;
-      struct anv_format_swizzle linear_swizzle, tiled_swizzle;
-      linear_fmt = anv_get_isl_format(format, VK_IMAGE_ASPECT_COLOR_BIT,
-                                      VK_IMAGE_TILING_LINEAR, &linear_swizzle);
-      tiled_fmt = anv_get_isl_format(format, VK_IMAGE_ASPECT_COLOR_BIT,
-                                     VK_IMAGE_TILING_OPTIMAL, &tiled_swizzle);
-
-      linear = get_image_format_properties(gen, linear_fmt, linear_fmt,
-                                           linear_swizzle);
-      tiled = get_image_format_properties(gen, linear_fmt, tiled_fmt,
-                                          tiled_swizzle);
-      buffer = get_buffer_format_properties(gen, linear_fmt);
+      struct anv_format linear_fmt, tiled_fmt;
+      linear_fmt = anv_get_format(format, VK_IMAGE_ASPECT_COLOR_BIT,
+                                  VK_IMAGE_TILING_LINEAR);
+      tiled_fmt = anv_get_format(format, VK_IMAGE_ASPECT_COLOR_BIT,
+                                 VK_IMAGE_TILING_OPTIMAL);
+
+      linear = get_image_format_properties(gen, linear_fmt.isl_format,
+                                           linear_fmt);
+      tiled = get_image_format_properties(gen, linear_fmt.isl_format,
+                                          tiled_fmt);
+      buffer = get_buffer_format_properties(gen, linear_fmt.isl_format);
 
       /* XXX: We handle 3-channel formats by switching them out for RGBX or
        * RGBA formats behind-the-scenes.  This works fine for textures
@@ -398,9 +390,9 @@ anv_physical_device_get_format_properties(struct anv_physical_device *physical_d
        * substantially more work and we have enough RGBX formats to handle
        * what most clients will want.
        */
-      if (linear_fmt != ISL_FORMAT_UNSUPPORTED &&
-          !util_is_power_of_two(isl_format_layouts[linear_fmt].bs) &&
-          isl_format_rgb_to_rgbx(linear_fmt) == ISL_FORMAT_UNSUPPORTED) {
+      if (linear_fmt.isl_format != ISL_FORMAT_UNSUPPORTED &&
+          !util_is_power_of_two(isl_format_layouts[linear_fmt.isl_format].bs) &&
+          isl_format_rgb_to_rgbx(linear_fmt.isl_format) == ISL_FORMAT_UNSUPPORTED) {
          tiled &= ~VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT &
                   ~VK_FORMAT_FEATURE_BLIT_DST_BIT;
       }
diff --git a/src/intel/vulkan/anv_image.c b/src/intel/vulkan/anv_image.c
index 792645d..75c02b3 100644
--- a/src/intel/vulkan/anv_image.c
+++ b/src/intel/vulkan/anv_image.c
@@ -131,8 +131,7 @@ make_surface(const struct anv_device *dev,
 
    ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
       .dim = vk_to_isl_surf_dim[vk_info->imageType],
-      .format = anv_get_isl_format(vk_info->format, aspect,
-                                   vk_info->tiling, NULL),
+      .format = anv_get_isl_format(vk_info->format, aspect, vk_info->tiling),
       .width = image->extent.width,
       .height = image->extent.height,
       .depth = image->extent.depth,
@@ -473,29 +472,27 @@ anv_image_view_init(struct anv_image_view *iview,
    iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
    iview->vk_format = pCreateInfo->format;
 
-   struct anv_format_swizzle swizzle;
-   enum isl_format format = anv_get_isl_format(pCreateInfo->format,
-                                               range->aspectMask,
-                                               image->tiling, &swizzle);
+   struct anv_format format =
+      anv_get_format(pCreateInfo->format, range->aspectMask, image->tiling);
 
    iview->base_layer = range->baseArrayLayer;
    iview->base_mip = range->baseMipLevel;
 
    struct isl_view isl_view = {
-      .format = format,
+      .format = format.isl_format,
       .base_level = range->baseMipLevel,
       .levels = anv_get_levelCount(image, range),
       .base_array_layer = range->baseArrayLayer,
       .array_len = anv_get_layerCount(image, range),
       .channel_select = {
          remap_swizzle(pCreateInfo->components.r,
-                       VK_COMPONENT_SWIZZLE_R, swizzle),
+                       VK_COMPONENT_SWIZZLE_R, format.swizzle),
          remap_swizzle(pCreateInfo->components.g,
-                       VK_COMPONENT_SWIZZLE_G, swizzle),
+                       VK_COMPONENT_SWIZZLE_G, format.swizzle),
          remap_swizzle(pCreateInfo->components.b,
-                       VK_COMPONENT_SWIZZLE_B, swizzle),
+                       VK_COMPONENT_SWIZZLE_B, format.swizzle),
          remap_swizzle(pCreateInfo->components.a,
-                       VK_COMPONENT_SWIZZLE_A, swizzle),
+                       VK_COMPONENT_SWIZZLE_A, format.swizzle),
       },
    };
 
@@ -548,7 +545,8 @@ anv_image_view_init(struct anv_image_view *iview,
    if (image->usage & usage_mask & VK_IMAGE_USAGE_STORAGE_BIT) {
       iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
 
-      if (isl_has_matching_typed_storage_image_format(&device->info, format)) {
+      if (isl_has_matching_typed_storage_image_format(&device->info,
+                                                      format.isl_format)) {
          isl_view.usage = cube_usage | ISL_SURF_USAGE_STORAGE_BIT;
          isl_surf_fill_state(&device->isl_dev,
                              iview->storage_surface_state.map,
@@ -631,7 +629,7 @@ void anv_buffer_view_init(struct anv_buffer_view *view,
 
    view->format = anv_get_isl_format(pCreateInfo->format,
                                      VK_IMAGE_ASPECT_COLOR_BIT,
-                                     VK_IMAGE_TILING_LINEAR, NULL);
+                                     VK_IMAGE_TILING_LINEAR);
    view->bo = buffer->bo;
    view->offset = buffer->offset + pCreateInfo->offset;
    view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
diff --git a/src/intel/vulkan/anv_meta_copy.c b/src/intel/vulkan/anv_meta_copy.c
index dbec4f7..c509c8f 100644
--- a/src/intel/vulkan/anv_meta_copy.c
+++ b/src/intel/vulkan/anv_meta_copy.c
@@ -162,8 +162,7 @@ meta_copy_buffer_to_image(struct anv_cmd_buffer *cmd_buffer,
       struct anv_meta_blit2d_surf img_bsurf =
          blit_surf_for_image(image, img_isl_surf);
       enum isl_format buf_format = anv_get_isl_format(image->vk_format, aspect,
-                                                      VK_IMAGE_TILING_LINEAR,
-                                                      NULL);
+                                                      VK_IMAGE_TILING_LINEAR);
       struct anv_meta_blit2d_surf buf_bsurf = {
          .bo = buffer->bo,
          .tiling = ISL_TILING_LINEAR,
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index bcf3475..040d0c7 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -1518,12 +1518,16 @@ struct anv_format {
    struct anv_format_swizzle swizzle;
 };
 
-const struct anv_format *
-anv_format_for_vk_format(VkFormat format);
+struct anv_format
+anv_get_format(VkFormat format, VkImageAspectFlags aspect,
+               VkImageTiling tiling);
 
-enum isl_format
-anv_get_isl_format(VkFormat format, VkImageAspectFlags aspect,
-                   VkImageTiling tiling, struct anv_format_swizzle *swizzle);
+static inline enum isl_format
+anv_get_isl_format(VkFormat vk_format, VkImageAspectFlags aspect,
+                   VkImageTiling tiling)
+{
+   return anv_get_format(vk_format, aspect, tiling).isl_format;
+}
 
 /**
  * Subsurface of an anv_image.
diff --git a/src/intel/vulkan/genX_pipeline_util.h b/src/intel/vulkan/genX_pipeline_util.h
index fe1f812..3d362da 100644
--- a/src/intel/vulkan/genX_pipeline_util.h
+++ b/src/intel/vulkan/genX_pipeline_util.h
@@ -102,8 +102,7 @@ emit_vertex_input(struct anv_pipeline *pipeline,
          &info->pVertexAttributeDescriptions[i];
       enum isl_format format = anv_get_isl_format(desc->format,
                                                   VK_IMAGE_ASPECT_COLOR_BIT,
-                                                  VK_IMAGE_TILING_LINEAR,
-                                                  NULL);
+                                                  VK_IMAGE_TILING_LINEAR);
 
       assert(desc->binding < 32);
 
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list