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>
This commit is contained in:
Emma Anholt 2022-08-04 20:21:20 -07:00 committed by Marge Bot
parent 8dda0a01bb
commit 41280ba289

View file

@ -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;
/* 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)
tile_mode = TILE6_LINEAR;
ubwc_enabled = false;
if (format_list_reinterprets_r8g8_r16(format, fmt_list) ||
format_list_has_swaps(fmt_list)) {
tile_mode = TILE6_LINEAR;
}
}
if (!ubwc_possible(image->vk.format, pCreateInfo->imageType,