From 959891e33f61ab8d0590e736198bf48e48d2105c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ol=C5=A1=C3=A1k?= Date: Mon, 25 Mar 2024 02:27:01 -0400 Subject: [PATCH] util: add new format helpers we'll use them Reviewed-by: Yonggang Luo Part-of: --- src/util/format/u_format.c | 52 ++++++++++++++++++++++++++++++++++++++ src/util/format/u_format.h | 4 +++ 2 files changed, 56 insertions(+) diff --git a/src/util/format/u_format.c b/src/util/format/u_format.c index 30dd65e8428..afcf781d294 100644 --- a/src/util/format/u_format.c +++ b/src/util/format/u_format.c @@ -1468,3 +1468,55 @@ util_format_get_array(const enum util_format_type type, const unsigned bits, return PIPE_FORMAT_NONE; } + +unsigned +util_format_get_last_component(enum pipe_format format) +{ + const struct util_format_description *desc = util_format_description(format); + unsigned num = 0; + + for (unsigned i = 1; i < 4; i++) { + if (desc->swizzle[i] <= PIPE_SWIZZLE_W) + num = i; + } + return num; +} + +int +util_format_get_largest_non_void_channel(enum pipe_format format) +{ + const struct util_format_description *desc = util_format_description(format); + int chan = -1, max_size = 0; + + for (int i = 0; i < 4; i++) { + if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID && + desc->channel[i].size > max_size) { + chan = i; + max_size = desc->channel[i].size; + } + } + + return chan; +} + +unsigned +util_format_get_max_channel_size(enum pipe_format format) +{ + const struct util_format_description *desc = util_format_description(format); + int max_src_chan = util_format_get_largest_non_void_channel(format); + assert(max_src_chan >= 0 || util_format_is_compressed(format)); + + switch (format) { + case PIPE_FORMAT_BPTC_RGB_FLOAT: + case PIPE_FORMAT_BPTC_RGB_UFLOAT: + return 16; + case PIPE_FORMAT_ETC2_R11_UNORM: + case PIPE_FORMAT_ETC2_R11_SNORM: + case PIPE_FORMAT_ETC2_RG11_UNORM: + case PIPE_FORMAT_ETC2_RG11_SNORM: + return 11; + default: + return util_format_is_compressed(format) ? + 8 : desc->channel[max_src_chan].size; + } +} diff --git a/src/util/format/u_format.h b/src/util/format/u_format.h index 00d3e75e5a0..d19b3111b3a 100644 --- a/src/util/format/u_format.h +++ b/src/util/format/u_format.h @@ -1797,6 +1797,10 @@ util_format_get_array(const enum util_format_type type, const unsigned bits, const unsigned nr_components, const bool normalized, const bool pure_integer); +unsigned util_format_get_last_component(enum pipe_format format); +int util_format_get_largest_non_void_channel(enum pipe_format format); +unsigned util_format_get_max_channel_size(enum pipe_format format); + #ifdef __cplusplus } // extern "C" { #endif