From 2020ba06e10d46eafa8ddd28460ebbdc87df3db5 Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Thu, 27 Dec 2018 20:43:06 -0500
Subject: [PATCH] gpu: Remove PixelFormat G8R8U and G8R8S, as they do not seem
 to exist.

- Fixes UI rendering issues in The Legend of Zelda: Breath of the Wild.
---
 src/video_core/morton.cpp                     |  4 -
 .../renderer_opengl/gl_rasterizer_cache.cpp   | 22 -----
 src/video_core/surface.cpp                    |  7 +-
 src/video_core/surface.h                      | 92 +++++++++----------
 4 files changed, 46 insertions(+), 79 deletions(-)

diff --git a/src/video_core/morton.cpp b/src/video_core/morton.cpp
index 47e76d8fe..b68f4fb13 100644
--- a/src/video_core/morton.cpp
+++ b/src/video_core/morton.cpp
@@ -66,8 +66,6 @@ static constexpr ConversionArray morton_to_linear_fns = {
         MortonCopy<true, PixelFormat::BC6H_UF16>,
         MortonCopy<true, PixelFormat::BC6H_SF16>,
         MortonCopy<true, PixelFormat::ASTC_2D_4X4>,
-        MortonCopy<true, PixelFormat::G8R8U>,
-        MortonCopy<true, PixelFormat::G8R8S>,
         MortonCopy<true, PixelFormat::BGRA8>,
         MortonCopy<true, PixelFormat::RGBA32F>,
         MortonCopy<true, PixelFormat::RG32F>,
@@ -138,8 +136,6 @@ static constexpr ConversionArray linear_to_morton_fns = {
         MortonCopy<false, PixelFormat::BC6H_SF16>,
         // TODO(Subv): Swizzling ASTC formats are not supported
         nullptr,
-        MortonCopy<false, PixelFormat::G8R8U>,
-        MortonCopy<false, PixelFormat::G8R8S>,
         MortonCopy<false, PixelFormat::BGRA8>,
         MortonCopy<false, PixelFormat::RGBA32F>,
         MortonCopy<false, PixelFormat::RG32F>,
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
index 7ea07631a..75b4fe88d 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
@@ -288,8 +288,6 @@ static constexpr std::array<FormatTuple, VideoCore::Surface::MaxPixelFormat> tex
     {GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT, GL_RGB, GL_UNSIGNED_INT_8_8_8_8, ComponentType::Float,
      true},                                                                    // BC6H_SF16
     {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false},        // ASTC_2D_4X4
-    {GL_RG8, GL_RG, GL_UNSIGNED_BYTE, ComponentType::UNorm, false},            // G8R8U
-    {GL_RG8, GL_RG, GL_BYTE, ComponentType::SNorm, false},                     // G8R8S
     {GL_RGBA8, GL_BGRA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false},        // BGRA8
     {GL_RGBA32F, GL_RGBA, GL_FLOAT, ComponentType::Float, false},              // RGBA32F
     {GL_RG32F, GL_RG, GL_FLOAT, ComponentType::Float, false},                  // RG32F
@@ -620,18 +618,6 @@ static void ConvertS8Z24ToZ24S8(std::vector<u8>& data, u32 width, u32 height, bo
     }
 }
 
-static void ConvertG8R8ToR8G8(std::vector<u8>& data, u32 width, u32 height) {
-    constexpr auto bpp{GetBytesPerPixel(PixelFormat::G8R8U)};
-    for (std::size_t y = 0; y < height; ++y) {
-        for (std::size_t x = 0; x < width; ++x) {
-            const std::size_t offset{bpp * (y * width + x)};
-            const u8 temp{data[offset]};
-            data[offset] = data[offset + 1];
-            data[offset + 1] = temp;
-        }
-    }
-}
-
 /**
  * Helper function to perform software conversion (as needed) when loading a buffer from Switch
  * memory. This is for Maxwell pixel formats that cannot be represented as-is in OpenGL or with
@@ -664,12 +650,6 @@ static void ConvertFormatAsNeeded_LoadGLBuffer(std::vector<u8>& data, PixelForma
         // Convert the S8Z24 depth format to Z24S8, as OpenGL does not support S8Z24.
         ConvertS8Z24ToZ24S8(data, width, height, false);
         break;
-
-    case PixelFormat::G8R8U:
-    case PixelFormat::G8R8S:
-        // Convert the G8R8 color format to R8G8, as OpenGL does not support G8R8.
-        ConvertG8R8ToR8G8(data, width, height);
-        break;
     }
 }
 
@@ -681,8 +661,6 @@ static void ConvertFormatAsNeeded_LoadGLBuffer(std::vector<u8>& data, PixelForma
 static void ConvertFormatAsNeeded_FlushGLBuffer(std::vector<u8>& data, PixelFormat pixel_format,
                                                 u32 width, u32 height) {
     switch (pixel_format) {
-    case PixelFormat::G8R8U:
-    case PixelFormat::G8R8S:
     case PixelFormat::ASTC_2D_4X4:
     case PixelFormat::ASTC_2D_8X8:
     case PixelFormat::ASTC_2D_4X4_SRGB:
diff --git a/src/video_core/surface.cpp b/src/video_core/surface.cpp
index a97b1562b..1a344229f 100644
--- a/src/video_core/surface.cpp
+++ b/src/video_core/surface.cpp
@@ -196,11 +196,14 @@ PixelFormat PixelFormatFromTextureFormat(Tegra::Texture::TextureFormat format,
         LOG_CRITICAL(HW_GPU, "Unimplemented component_type={}", static_cast<u32>(component_type));
         UNREACHABLE();
     case Tegra::Texture::TextureFormat::G8R8:
+        // TextureFormat::G8R8 is actually ordered red then green, as such we can use
+        // PixelFormat::RG8U and PixelFormat::RG8S. This was tested with The Legend of Zelda: Breath
+        // of the Wild, which uses this format to render the hearts on the UI.
         switch (component_type) {
         case Tegra::Texture::ComponentType::UNORM:
-            return PixelFormat::G8R8U;
+            return PixelFormat::RG8U;
         case Tegra::Texture::ComponentType::SNORM:
-            return PixelFormat::G8R8S;
+            return PixelFormat::RG8S;
         }
         LOG_CRITICAL(HW_GPU, "Unimplemented component_type={}", static_cast<u32>(component_type));
         UNREACHABLE();
diff --git a/src/video_core/surface.h b/src/video_core/surface.h
index e23cfecbc..c2259c3c2 100644
--- a/src/video_core/surface.h
+++ b/src/video_core/surface.h
@@ -38,57 +38,55 @@ enum class PixelFormat {
     BC6H_UF16 = 20,
     BC6H_SF16 = 21,
     ASTC_2D_4X4 = 22,
-    G8R8U = 23,
-    G8R8S = 24,
-    BGRA8 = 25,
-    RGBA32F = 26,
-    RG32F = 27,
-    R32F = 28,
-    R16F = 29,
-    R16U = 30,
-    R16S = 31,
-    R16UI = 32,
-    R16I = 33,
-    RG16 = 34,
-    RG16F = 35,
-    RG16UI = 36,
-    RG16I = 37,
-    RG16S = 38,
-    RGB32F = 39,
-    RGBA8_SRGB = 40,
-    RG8U = 41,
-    RG8S = 42,
-    RG32UI = 43,
-    R32UI = 44,
-    ASTC_2D_8X8 = 45,
-    ASTC_2D_8X5 = 46,
-    ASTC_2D_5X4 = 47,
-    BGRA8_SRGB = 48,
-    DXT1_SRGB = 49,
-    DXT23_SRGB = 50,
-    DXT45_SRGB = 51,
-    BC7U_SRGB = 52,
-    ASTC_2D_4X4_SRGB = 53,
-    ASTC_2D_8X8_SRGB = 54,
-    ASTC_2D_8X5_SRGB = 55,
-    ASTC_2D_5X4_SRGB = 56,
-    ASTC_2D_5X5 = 57,
-    ASTC_2D_5X5_SRGB = 58,
-    ASTC_2D_10X8 = 59,
-    ASTC_2D_10X8_SRGB = 60,
+    BGRA8 = 23,
+    RGBA32F = 24,
+    RG32F = 25,
+    R32F = 26,
+    R16F = 27,
+    R16U = 28,
+    R16S = 29,
+    R16UI = 30,
+    R16I = 31,
+    RG16 = 32,
+    RG16F = 33,
+    RG16UI = 34,
+    RG16I = 35,
+    RG16S = 36,
+    RGB32F = 37,
+    RGBA8_SRGB = 38,
+    RG8U = 39,
+    RG8S = 40,
+    RG32UI = 41,
+    R32UI = 42,
+    ASTC_2D_8X8 = 43,
+    ASTC_2D_8X5 = 44,
+    ASTC_2D_5X4 = 45,
+    BGRA8_SRGB = 46,
+    DXT1_SRGB = 47,
+    DXT23_SRGB = 48,
+    DXT45_SRGB = 49,
+    BC7U_SRGB = 50,
+    ASTC_2D_4X4_SRGB = 51,
+    ASTC_2D_8X8_SRGB = 52,
+    ASTC_2D_8X5_SRGB = 53,
+    ASTC_2D_5X4_SRGB = 54,
+    ASTC_2D_5X5 = 55,
+    ASTC_2D_5X5_SRGB = 56,
+    ASTC_2D_10X8 = 57,
+    ASTC_2D_10X8_SRGB = 58,
 
     MaxColorFormat,
 
     // Depth formats
-    Z32F = 61,
-    Z16 = 62,
+    Z32F = 59,
+    Z16 = 60,
 
     MaxDepthFormat,
 
     // DepthStencil formats
-    Z24S8 = 63,
-    S8Z24 = 64,
-    Z32FS8 = 65,
+    Z24S8 = 61,
+    S8Z24 = 62,
+    Z32FS8 = 63,
 
     MaxDepthStencilFormat,
 
@@ -149,8 +147,6 @@ constexpr std::array<u32, MaxPixelFormat> compression_factor_table = {{
     4, // BC6H_UF16
     4, // BC6H_SF16
     4, // ASTC_2D_4X4
-    1, // G8R8U
-    1, // G8R8S
     1, // BGRA8
     1, // RGBA32F
     1, // RG32F
@@ -232,8 +228,6 @@ constexpr std::array<u32, MaxPixelFormat> block_width_table = {{
     4,  // BC6H_UF16
     4,  // BC6H_SF16
     4,  // ASTC_2D_4X4
-    1,  // G8R8U
-    1,  // G8R8S
     1,  // BGRA8
     1,  // RGBA32F
     1,  // RG32F
@@ -309,8 +303,6 @@ constexpr std::array<u32, MaxPixelFormat> block_height_table = {{
     4, // BC6H_UF16
     4, // BC6H_SF16
     4, // ASTC_2D_4X4
-    1, // G8R8U
-    1, // G8R8S
     1, // BGRA8
     1, // RGBA32F
     1, // RG32F
@@ -386,8 +378,6 @@ constexpr std::array<u32, MaxPixelFormat> bpp_table = {{
     128, // BC6H_UF16
     128, // BC6H_SF16
     128, // ASTC_2D_4X4
-    16,  // G8R8U
-    16,  // G8R8S
     32,  // BGRA8
     128, // RGBA32F
     64,  // RG32F