Module: Mesa
Branch: main
Commit: 41280ba2895fc6fafd02d7f2ba4490215aa0914a
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=41280ba2895fc6fafd02d7f2ba4490215aa0914a

Author: Emma Anholt <[email protected]>
Date:   Thu Aug  4 20:21:20 2022 -0700

tu: Move the vkCreateImage format list checks to helper functions.

Mixing up the two checks made them both harder to read.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17900>

---

 src/freedreno/vulkan/tu_image.c | 90 ++++++++++++++++++++++++-----------------
 1 file changed, 53 insertions(+), 37 deletions(-)

diff --git a/src/freedreno/vulkan/tu_image.c b/src/freedreno/vulkan/tu_image.c
index 9b59d44fb93..6630c812321 100644
--- a/src/freedreno/vulkan/tu_image.c
+++ b/src/freedreno/vulkan/tu_image.c
@@ -338,6 +338,55 @@ ubwc_possible(VkFormat format, VkImageType type, 
VkImageUsageFlags usage,
    return true;
 }
 
+/* R8G8 have a different block width/height and height alignment from other
+ * formats that would normally be compatible (like R16), and so if we are
+ * trying to, for example, sample R16 as R8G8 we need to demote to linear.
+ */
+static bool
+format_list_reinterprets_r8g8_r16(enum pipe_format format, const 
VkImageFormatListCreateInfo *fmt_list)
+{
+   /* Check if it's actually a 2-cpp color format. */
+   if (!tu_is_r8g8_compatible(format))
+      return false;
+
+   /* If there's no format list, then the app may reinterpret to any compatible
+    * format.
+    */
+   if (!fmt_list)
+      return true;
+
+   bool has_r8g8 = false;
+   bool has_non_r8g8 = false;
+   for (uint32_t i = 0; i < fmt_list->viewFormatCount; i++) {
+      enum pipe_format format =
+         tu_vk_format_to_pipe_format(fmt_list->pViewFormats[i]);
+      if (tu_is_r8g8(format))
+         has_r8g8 = true;
+      else
+         has_non_r8g8 = true;
+   }
+   return has_r8g8 && has_non_r8g8;
+}
+
+static bool
+format_list_has_swaps(const VkImageFormatListCreateInfo *fmt_list)
+{
+   /* If there's no format list, then the app may reinterpret to any compatible
+    * format, and presumably one would have the swap set.
+    */
+   if (!fmt_list)
+      return true;
+
+   for (uint32_t i = 0; i < fmt_list->viewFormatCount; i++) {
+      enum pipe_format format =
+         tu_vk_format_to_pipe_format(fmt_list->pViewFormats[i]);
+
+      if (tu6_format_texture(format, TILE6_LINEAR).swap)
+         return true;
+   }
+   return false;
+}
+
 static VkResult
 tu_image_init(struct tu_device *device, struct tu_image *image,
               const VkImageCreateInfo *pCreateInfo, uint64_t modifier,
@@ -388,46 +437,13 @@ tu_image_init(struct tu_device *device, struct tu_image 
*image,
        !vk_format_is_depth_or_stencil(image->vk.format)) {
       const VkImageFormatListCreateInfo *fmt_list =
          vk_find_struct_const(pCreateInfo->pNext, 
IMAGE_FORMAT_LIST_CREATE_INFO);
-      bool may_be_swapped = true;
-      /* Whether a view of the image with a non-R8G8 but R8G8 compatible format
-       * could be made.
-       */
-      bool has_r8g8_compatible = false;
-      if (fmt_list) {
-         may_be_swapped = false;
-         has_r8g8_compatible = !has_r8g8 && tu_is_r8g8_compatible(format);
-         for (uint32_t i = 0; i < fmt_list->viewFormatCount; i++) {
-            enum pipe_format format =
-               tu_vk_format_to_pipe_format(fmt_list->pViewFormats[i]);
-            bool is_r8g8 = tu_is_r8g8(format);
-            has_r8g8 = has_r8g8 || is_r8g8;
-            has_r8g8_compatible = has_r8g8_compatible ||
-                                  (!is_r8g8 && tu_is_r8g8_compatible(format));
-
-            if (tu6_format_texture(format, TILE6_LINEAR).swap) {
-               may_be_swapped = true;
-               break;
-            }
-         }
-      } else {
-         /* If there is no format list it could be reinterpreted as
-          * any compatible format.
-          */
-         has_r8g8 = tu_is_r8g8_compatible(format);
-         has_r8g8_compatible = has_r8g8;
-      }
 
-      if (may_be_swapped)
-         tile_mode = TILE6_LINEAR;
+      ubwc_enabled = false;
 
-      /* R8G8 have a different block width/height and height alignment from 
other
-       * formats that would normally be compatible (like R16), and so if we are
-       * trying to, for example, sample R16 as R8G8 we need to demote to 
linear.
-       */
-      if (has_r8g8 && has_r8g8_compatible)
+      if (format_list_reinterprets_r8g8_r16(format, fmt_list) ||
+          format_list_has_swaps(fmt_list)) {
          tile_mode = TILE6_LINEAR;
-
-      ubwc_enabled = false;
+      }
    }
 
    if (!ubwc_possible(image->vk.format, pCreateInfo->imageType,

Reply via email to