// Copyright 2026, Beyley Cardellio
// SPDX-License-Identifier: BSL-1.0
/*!
 * @file
 * @brief Vulkan helpers for formats.
 *
 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
 * @ingroup aux_vk
 */

#include "vk_format.h"


/*
 * All known UNORM/sRGB format pairs.
 *
 * @ingroup aux_vk
 */
static const struct vk_srgb_format_pair vk_format_srgb_pairs[] = {
    {VK_FORMAT_R8_UNORM, VK_FORMAT_R8_SRGB},
    {VK_FORMAT_R8G8_UNORM, VK_FORMAT_R8G8_SRGB},
    {VK_FORMAT_R8G8B8_UNORM, VK_FORMAT_R8G8B8_SRGB},
    {VK_FORMAT_B8G8R8_UNORM, VK_FORMAT_B8G8R8_SRGB},
    {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB},
    {VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_SRGB},
    {VK_FORMAT_A8B8G8R8_UNORM_PACK32, VK_FORMAT_A8B8G8R8_SRGB_PACK32},
    {VK_FORMAT_BC1_RGB_UNORM_BLOCK, VK_FORMAT_BC1_RGB_SRGB_BLOCK},
    {VK_FORMAT_BC1_RGBA_UNORM_BLOCK, VK_FORMAT_BC1_RGBA_SRGB_BLOCK},
    {VK_FORMAT_BC2_UNORM_BLOCK, VK_FORMAT_BC2_SRGB_BLOCK},
    {VK_FORMAT_BC3_UNORM_BLOCK, VK_FORMAT_BC3_SRGB_BLOCK},
    {VK_FORMAT_BC7_UNORM_BLOCK, VK_FORMAT_BC7_SRGB_BLOCK},
    {VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK},
    {VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK, VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK},
    {VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK},
    {VK_FORMAT_ASTC_4x4_UNORM_BLOCK, VK_FORMAT_ASTC_4x4_SRGB_BLOCK},
    {VK_FORMAT_ASTC_5x4_UNORM_BLOCK, VK_FORMAT_ASTC_5x4_SRGB_BLOCK},
    {VK_FORMAT_ASTC_5x5_UNORM_BLOCK, VK_FORMAT_ASTC_5x5_SRGB_BLOCK},
    {VK_FORMAT_ASTC_6x5_UNORM_BLOCK, VK_FORMAT_ASTC_6x5_SRGB_BLOCK},
    {VK_FORMAT_ASTC_6x6_UNORM_BLOCK, VK_FORMAT_ASTC_6x6_SRGB_BLOCK},
    {VK_FORMAT_ASTC_8x5_UNORM_BLOCK, VK_FORMAT_ASTC_8x5_SRGB_BLOCK},
    {VK_FORMAT_ASTC_8x6_UNORM_BLOCK, VK_FORMAT_ASTC_8x6_SRGB_BLOCK},
    {VK_FORMAT_ASTC_8x8_UNORM_BLOCK, VK_FORMAT_ASTC_8x8_SRGB_BLOCK},
    {VK_FORMAT_ASTC_10x5_UNORM_BLOCK, VK_FORMAT_ASTC_10x5_SRGB_BLOCK},
    {VK_FORMAT_ASTC_10x6_UNORM_BLOCK, VK_FORMAT_ASTC_10x6_SRGB_BLOCK},
    {VK_FORMAT_ASTC_10x8_UNORM_BLOCK, VK_FORMAT_ASTC_10x8_SRGB_BLOCK},
    {VK_FORMAT_ASTC_10x10_UNORM_BLOCK, VK_FORMAT_ASTC_10x10_SRGB_BLOCK},
    {VK_FORMAT_ASTC_12x10_UNORM_BLOCK, VK_FORMAT_ASTC_12x10_SRGB_BLOCK},
    {VK_FORMAT_ASTC_12x12_UNORM_BLOCK, VK_FORMAT_ASTC_12x12_SRGB_BLOCK},
#ifdef VK_IMG_format_pvrtc
    {VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG, VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG},
    {VK_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG, VK_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG},
    {VK_FORMAT_PVRTC2_2BPP_UNORM_BLOCK_IMG, VK_FORMAT_PVRTC2_2BPP_SRGB_BLOCK_IMG},
    {VK_FORMAT_PVRTC2_4BPP_UNORM_BLOCK_IMG, VK_FORMAT_PVRTC2_4BPP_SRGB_BLOCK_IMG},
#endif
#ifdef VK_EXT_texture_compression_astc_3d
    {VK_FORMAT_ASTC_3x3x3_UNORM_BLOCK_EXT, VK_FORMAT_ASTC_3x3x3_SRGB_BLOCK_EXT},
    {VK_FORMAT_ASTC_4x3x3_UNORM_BLOCK_EXT, VK_FORMAT_ASTC_4x3x3_SRGB_BLOCK_EXT},
    {VK_FORMAT_ASTC_4x4x3_UNORM_BLOCK_EXT, VK_FORMAT_ASTC_4x4x3_SRGB_BLOCK_EXT},
    {VK_FORMAT_ASTC_4x4x4_UNORM_BLOCK_EXT, VK_FORMAT_ASTC_4x4x4_SRGB_BLOCK_EXT},
    {VK_FORMAT_ASTC_5x4x4_UNORM_BLOCK_EXT, VK_FORMAT_ASTC_5x4x4_SRGB_BLOCK_EXT},
    {VK_FORMAT_ASTC_5x5x4_UNORM_BLOCK_EXT, VK_FORMAT_ASTC_5x5x4_SRGB_BLOCK_EXT},
    {VK_FORMAT_ASTC_5x5x5_UNORM_BLOCK_EXT, VK_FORMAT_ASTC_5x5x5_SRGB_BLOCK_EXT},
    {VK_FORMAT_ASTC_6x5x5_UNORM_BLOCK_EXT, VK_FORMAT_ASTC_6x5x5_SRGB_BLOCK_EXT},
    {VK_FORMAT_ASTC_6x6x5_UNORM_BLOCK_EXT, VK_FORMAT_ASTC_6x6x5_SRGB_BLOCK_EXT},
    {VK_FORMAT_ASTC_6x6x6_UNORM_BLOCK_EXT, VK_FORMAT_ASTC_6x6x6_SRGB_BLOCK_EXT},
#endif
};

VkFormat
vk_format_convert_unorm_to_srgb(VkFormat unorm)
{
	for (size_t i = 0; i < ARRAY_SIZE(vk_format_srgb_pairs); i++) {
		if (vk_format_srgb_pairs[i].unorm == unorm) {
			return vk_format_srgb_pairs[i].srgb;
		}
	}

	return VK_FORMAT_UNDEFINED;
}

VkFormat
vk_format_convert_srgb_to_unorm(VkFormat srgb)
{
	for (size_t i = 0; i < ARRAY_SIZE(vk_format_srgb_pairs); i++) {
		if (vk_format_srgb_pairs[i].srgb == srgb) {
			return vk_format_srgb_pairs[i].unorm;
		}
	}

	return VK_FORMAT_UNDEFINED;
}
