From 39958c9e9d80ecb2a3f4c058b61242731d2e59a0 Mon Sep 17 00:00:00 2001 From: Faith Ekstrand Date: Mon, 30 Jan 2023 20:12:01 -0600 Subject: [PATCH] vulkan: Add a helper for swizzling color values Part-of: --- src/vulkan/util/vk_format.c | 29 +++++++++++++++++++++++++++++ src/vulkan/util/vk_format.h | 4 ++++ 2 files changed, 33 insertions(+) diff --git a/src/vulkan/util/vk_format.c b/src/vulkan/util/vk_format.c index 19486f8..d016e70 100644 --- a/src/vulkan/util/vk_format.c +++ b/src/vulkan/util/vk_format.c @@ -624,3 +624,32 @@ vk_format_get_ycbcr_info(VkFormat format) return info; } + +static uint32_t +swizzled_color_component(const VkClearColorValue *color, + VkComponentSwizzle swizzle, + uint32_t comp, bool is_int) +{ + switch (swizzle) { + case VK_COMPONENT_SWIZZLE_IDENTITY: return color->uint32[comp]; + case VK_COMPONENT_SWIZZLE_ZERO: return 0; + case VK_COMPONENT_SWIZZLE_ONE: return is_int ? 1 : 0x3f800000; + case VK_COMPONENT_SWIZZLE_R: return color->uint32[0]; + case VK_COMPONENT_SWIZZLE_G: return color->uint32[1]; + case VK_COMPONENT_SWIZZLE_B: return color->uint32[2]; + case VK_COMPONENT_SWIZZLE_A: return color->uint32[3]; + default: unreachable("Invalid component swizzle"); + } +} + +VkClearColorValue +vk_swizzle_color_value(VkClearColorValue color, + VkComponentMapping swizzle, bool is_int) +{ + return (VkClearColorValue) { .uint32 = { + swizzled_color_component(&color, swizzle.r, 0, is_int), + swizzled_color_component(&color, swizzle.g, 1, is_int), + swizzled_color_component(&color, swizzle.b, 2, is_int), + swizzled_color_component(&color, swizzle.a, 3, is_int), + }}; +} diff --git a/src/vulkan/util/vk_format.h b/src/vulkan/util/vk_format.h index 570d281..03297bc 100644 --- a/src/vulkan/util/vk_format.h +++ b/src/vulkan/util/vk_format.h @@ -238,6 +238,10 @@ vk_format_get_plane_count(VkFormat format) return ycbcr_info ? ycbcr_info->n_planes : 1; } +VkClearColorValue +vk_swizzle_color_value(VkClearColorValue color, + VkComponentMapping swizzle, bool is_int); + #ifdef __cplusplus } #endif -- 2.7.4