SDL: GPU: Remove unnecessary texture format swizzles in Vulkan (#10693)

From 64f12bea4a92d91b2ed571ca9648f9c72f880339 Mon Sep 17 00:00:00 2001
From: Andrei Alexeyev <[EMAIL REDACTED]>
Date: Wed, 11 Sep 2024 19:01:06 +0300
Subject: [PATCH] GPU: Remove unnecessary texture format swizzles in Vulkan
 (#10693)

---
 src/gpu/vulkan/SDL_gpu_vulkan.c | 117 +++++++-------------------------
 1 file changed, 26 insertions(+), 91 deletions(-)

diff --git a/src/gpu/vulkan/SDL_gpu_vulkan.c b/src/gpu/vulkan/SDL_gpu_vulkan.c
index 9138ff5aa2795..5668bbe4bdfa5 100644
--- a/src/gpu/vulkan/SDL_gpu_vulkan.c
+++ b/src/gpu/vulkan/SDL_gpu_vulkan.c
@@ -196,96 +196,31 @@ static VkFormat SDLToVK_SurfaceFormat[] = {
 };
 SDL_COMPILE_TIME_ASSERT(SDLToVK_SurfaceFormat, SDL_arraysize(SDLToVK_SurfaceFormat) == SDL_GPU_TEXTUREFORMAT_MAX_ENUM_VALUE);
 
-static VkComponentMapping SDLToVK_SurfaceSwizzle[] = {
-    IDENTITY_SWIZZLE,
-    {
-        // A8
-        VK_COMPONENT_SWIZZLE_ZERO,
-        VK_COMPONENT_SWIZZLE_ZERO,
-        VK_COMPONENT_SWIZZLE_ZERO,
-        VK_COMPONENT_SWIZZLE_R,
-    },
-    IDENTITY_SWIZZLE, // R8
-    IDENTITY_SWIZZLE, // R8G8
-    IDENTITY_SWIZZLE, // R8G8B8A8
-    IDENTITY_SWIZZLE, // R16
-    IDENTITY_SWIZZLE, // R16G16
-    IDENTITY_SWIZZLE, // R16G16B16A16
-    {
-        // R10G10B10A2
-        VK_COMPONENT_SWIZZLE_R,
-        VK_COMPONENT_SWIZZLE_G,
-        VK_COMPONENT_SWIZZLE_B,
-        VK_COMPONENT_SWIZZLE_A,
-    },
-    {
-        // B5G6R5
-        VK_COMPONENT_SWIZZLE_B,
-        VK_COMPONENT_SWIZZLE_G,
-        VK_COMPONENT_SWIZZLE_R,
-        VK_COMPONENT_SWIZZLE_ONE,
-    },
-    {
-        // B5G5R5A1
-        VK_COMPONENT_SWIZZLE_B,
-        VK_COMPONENT_SWIZZLE_G,
-        VK_COMPONENT_SWIZZLE_R,
-        VK_COMPONENT_SWIZZLE_A,
-    },
-    IDENTITY_SWIZZLE, // B4G4R4A4
-    IDENTITY_SWIZZLE, // B8G8R8A8
-    IDENTITY_SWIZZLE, // BC1
-    IDENTITY_SWIZZLE, // BC2
-    IDENTITY_SWIZZLE, // BC3
-    IDENTITY_SWIZZLE, // BC4
-    IDENTITY_SWIZZLE, // BC5
-    IDENTITY_SWIZZLE, // BC7
-    IDENTITY_SWIZZLE, // BC6H_FLOAT
-    IDENTITY_SWIZZLE, // BC6H_UFLOAT
-    IDENTITY_SWIZZLE, // R8_SNORM
-    IDENTITY_SWIZZLE, // R8G8_SNORM
-    IDENTITY_SWIZZLE, // R8G8B8A8_SNORM
-    IDENTITY_SWIZZLE, // R16_SNORM
-    IDENTITY_SWIZZLE, // R16G16_SNORM
-    IDENTITY_SWIZZLE, // R16G16B16A16_SNORM
-    IDENTITY_SWIZZLE, // R16_SFLOAT
-    IDENTITY_SWIZZLE, // R16G16_SFLOAT
-    IDENTITY_SWIZZLE, // R16G16B16A16_SFLOAT
-    IDENTITY_SWIZZLE, // R32_SFLOAT
-    IDENTITY_SWIZZLE, // R32G32_SFLOAT
-    IDENTITY_SWIZZLE, // R32G32B32A32_SFLOAT
-    {
-        // R11G11B10_UFLOAT
-        VK_COMPONENT_SWIZZLE_R,
-        VK_COMPONENT_SWIZZLE_G,
-        VK_COMPONENT_SWIZZLE_B,
-        VK_COMPONENT_SWIZZLE_ONE,
-    },
-    IDENTITY_SWIZZLE, // R8_UINT
-    IDENTITY_SWIZZLE, // R8G8_UINT
-    IDENTITY_SWIZZLE, // R8G8B8A8_UINT
-    IDENTITY_SWIZZLE, // R16_UINT
-    IDENTITY_SWIZZLE, // R16G16_UINT
-    IDENTITY_SWIZZLE, // R16G16B16A16_UINT
-    IDENTITY_SWIZZLE, // R8_INT
-    IDENTITY_SWIZZLE, // R8G8_INT
-    IDENTITY_SWIZZLE, // R8G8B8A8_INT
-    IDENTITY_SWIZZLE, // R16_INT
-    IDENTITY_SWIZZLE, // R16G16_INT
-    IDENTITY_SWIZZLE, // R16G16B16A16_INT
-    IDENTITY_SWIZZLE, // R8G8B8A8_SRGB
-    IDENTITY_SWIZZLE, // B8G8R8A8_SRGB
-    IDENTITY_SWIZZLE, // BC1_SRGB
-    IDENTITY_SWIZZLE, // BC2_SRGB
-    IDENTITY_SWIZZLE, // BC3_SRGB
-    IDENTITY_SWIZZLE, // BC7_SRGB
-    IDENTITY_SWIZZLE, // D16_UNORM
-    IDENTITY_SWIZZLE, // D24_UNORM
-    IDENTITY_SWIZZLE, // D32_SFLOAT
-    IDENTITY_SWIZZLE, // D24_UNORM_S8_UINT
-    IDENTITY_SWIZZLE, // D32_SFLOAT_S8_UINT
-};
-SDL_COMPILE_TIME_ASSERT(SDLToVK_SurfaceSwizzle, SDL_arraysize(SDLToVK_SurfaceSwizzle) == SDL_GPU_TEXTUREFORMAT_MAX_ENUM_VALUE);
+static VkComponentMapping SwizzleForSDLFormat(SDL_GPUTextureFormat format)
+{
+    if (format == SDL_GPU_TEXTUREFORMAT_A8_UNORM) {
+        // TODO: use VK_FORMAT_A8_UNORM_KHR from VK_KHR_maintenance5 when available
+        return (VkComponentMapping){
+            VK_COMPONENT_SWIZZLE_ZERO,
+            VK_COMPONENT_SWIZZLE_ZERO,
+            VK_COMPONENT_SWIZZLE_ZERO,
+            VK_COMPONENT_SWIZZLE_R,
+        };
+    }
+
+    if (format == SDL_GPU_TEXTUREFORMAT_B4G4R4A4_UNORM) {
+        // ARGB -> BGRA
+        // TODO: use VK_FORMAT_A4R4G4B4_UNORM_PACK16_EXT from VK_EXT_4444_formats when available
+        return (VkComponentMapping){
+            VK_COMPONENT_SWIZZLE_G,
+            VK_COMPONENT_SWIZZLE_R,
+            VK_COMPONENT_SWIZZLE_A,
+            VK_COMPONENT_SWIZZLE_B,
+        };
+    }
+
+    return (VkComponentMapping)IDENTITY_SWIZZLE;
+}
 
 static VkFormat SwapchainCompositionToFormat[] = {
     VK_FORMAT_B8G8R8A8_UNORM,          // SDR
@@ -6972,7 +6907,7 @@ static SDL_GPUTexture *VULKAN_CreateTexture(
     VulkanTextureHandle *textureHandle;
 
     format = SDLToVK_SurfaceFormat[createinfo->format];
-    swizzle = SDLToVK_SurfaceSwizzle[createinfo->format];
+    swizzle = SwizzleForSDLFormat(createinfo->format);
 
     if (isDepthFormat) {
         imageAspectFlags = VK_IMAGE_ASPECT_DEPTH_BIT;