From 3a96670f2df3dffd13381ed370eb0392350cf002 Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Sun, 15 Jul 2018 01:33:42 -0400
Subject: [PATCH] gl_rasterizer_cache: Implement texture format G8R8.

---
 .../renderer_opengl/gl_rasterizer_cache.cpp   | 31 ++++++++++++++++---
 .../renderer_opengl/gl_rasterizer_cache.h     | 15 ++++++---
 src/video_core/textures/decoders.cpp          |  3 ++
 3 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
index 4efe20c70f..c171c4c5ba 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
@@ -105,6 +105,7 @@ static constexpr std::array<FormatTuple, SurfaceParams::MaxPixelFormat> tex_form
     {GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_RGB, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm,
      true},                                                             // BC7U
     {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_4X4
+    {GL_RG8, GL_RG, GL_UNSIGNED_BYTE, ComponentType::UNorm, false},     // G8R8
 
     // DepthStencil formats
     {GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, ComponentType::UNorm,
@@ -196,8 +197,9 @@ static constexpr std::array<void (*)(u32, u32, u32, u8*, Tegra::GPUVAddr),
         MortonCopy<true, PixelFormat::DXT1>,         MortonCopy<true, PixelFormat::DXT23>,
         MortonCopy<true, PixelFormat::DXT45>,        MortonCopy<true, PixelFormat::DXN1>,
         MortonCopy<true, PixelFormat::BC7U>,         MortonCopy<true, PixelFormat::ASTC_2D_4X4>,
-        MortonCopy<true, PixelFormat::Z24S8>,        MortonCopy<true, PixelFormat::S8Z24>,
-        MortonCopy<true, PixelFormat::Z32F>,         MortonCopy<true, PixelFormat::Z16>,
+        MortonCopy<true, PixelFormat::G8R8>,         MortonCopy<true, PixelFormat::Z24S8>,
+        MortonCopy<true, PixelFormat::S8Z24>,        MortonCopy<true, PixelFormat::Z32F>,
+        MortonCopy<true, PixelFormat::Z16>,
 };
 
 static constexpr std::array<void (*)(u32, u32, u32, u8*, Tegra::GPUVAddr),
@@ -217,7 +219,8 @@ static constexpr std::array<void (*)(u32, u32, u32, u8*, Tegra::GPUVAddr),
         nullptr,
         nullptr,
         nullptr,
-        MortonCopy<false, PixelFormat::ABGR8>,
+        nullptr,
+        MortonCopy<false, PixelFormat::G8R8>,
         MortonCopy<false, PixelFormat::Z24S8>,
         MortonCopy<false, PixelFormat::S8Z24>,
         MortonCopy<false, PixelFormat::Z32F>,
@@ -274,10 +277,10 @@ static void ConvertS8Z24ToZ24S8(std::vector<u8>& data, u32 width, u32 height) {
 
     S8Z24 input_pixel{};
     Z24S8 output_pixel{};
-
+    const auto bpp{CachedSurface::GetGLBytesPerPixel(PixelFormat::S8Z24)};
     for (size_t y = 0; y < height; ++y) {
         for (size_t x = 0; x < width; ++x) {
-            const size_t offset{4 * (y * width + x)};
+            const size_t offset{bpp * (y * width + x)};
             std::memcpy(&input_pixel, &data[offset], sizeof(S8Z24));
             output_pixel.s8.Assign(input_pixel.s8);
             output_pixel.z24.Assign(input_pixel.z24);
@@ -285,6 +288,19 @@ static void ConvertS8Z24ToZ24S8(std::vector<u8>& data, u32 width, u32 height) {
         }
     }
 }
+
+static void ConvertG8R8ToR8G8(std::vector<u8>& data, u32 width, u32 height) {
+    const auto bpp{CachedSurface::GetGLBytesPerPixel(PixelFormat::G8R8)};
+    for (size_t y = 0; y < height; ++y) {
+        for (size_t x = 0; x < width; ++x) {
+            const 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
@@ -305,6 +321,11 @@ 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);
         break;
+
+    case PixelFormat::G8R8:
+        // Convert the G8R8 color format to R8G8, as OpenGL does not support G8R8.
+        ConvertG8R8ToR8G8(data, width, height);
+        break;
     }
 }
 
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.h b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
index 0f5b1ff328..718c45ce1b 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
@@ -37,14 +37,15 @@ struct SurfaceParams {
         DXN1 = 11, // This is also known as BC4
         BC7U = 12,
         ASTC_2D_4X4 = 13,
+        G8R8 = 14,
 
         MaxColorFormat,
 
         // DepthStencil formats
-        Z24S8 = 14,
-        S8Z24 = 15,
-        Z32F = 16,
-        Z16 = 17,
+        Z24S8 = 15,
+        S8Z24 = 16,
+        Z32F = 17,
+        Z16 = 18,
 
         MaxDepthStencilFormat,
 
@@ -96,6 +97,7 @@ struct SurfaceParams {
             4, // DXN1
             4, // BC7U
             4, // ASTC_2D_4X4
+            1, // G8R8
             1, // Z24S8
             1, // S8Z24
             1, // Z32F
@@ -125,6 +127,7 @@ struct SurfaceParams {
             64,  // DXN1
             128, // BC7U
             32,  // ASTC_2D_4X4
+            16,  // G8R8
             32,  // Z24S8
             32,  // S8Z24
             32,  // Z32F
@@ -186,6 +189,8 @@ struct SurfaceParams {
             return PixelFormat::A1B5G5R5;
         case Tegra::Texture::TextureFormat::R8:
             return PixelFormat::R8;
+        case Tegra::Texture::TextureFormat::G8R8:
+            return PixelFormat::G8R8;
         case Tegra::Texture::TextureFormat::R16_G16_B16_A16:
             return PixelFormat::RGBA16F;
         case Tegra::Texture::TextureFormat::BF10GF11RF11:
@@ -223,6 +228,8 @@ struct SurfaceParams {
             return Tegra::Texture::TextureFormat::A1B5G5R5;
         case PixelFormat::R8:
             return Tegra::Texture::TextureFormat::R8;
+        case PixelFormat::G8R8:
+            return Tegra::Texture::TextureFormat::G8R8;
         case PixelFormat::RGBA16F:
             return Tegra::Texture::TextureFormat::R16_G16_B16_A16;
         case PixelFormat::R11FG11FB10F:
diff --git a/src/video_core/textures/decoders.cpp b/src/video_core/textures/decoders.cpp
index a4ba9f66ac..be18aa299e 100644
--- a/src/video_core/textures/decoders.cpp
+++ b/src/video_core/textures/decoders.cpp
@@ -62,6 +62,7 @@ u32 BytesPerPixel(TextureFormat format) {
         return 4;
     case TextureFormat::A1B5G5R5:
     case TextureFormat::B5G6R5:
+    case TextureFormat::G8R8:
         return 2;
     case TextureFormat::R8:
         return 1;
@@ -112,6 +113,7 @@ std::vector<u8> UnswizzleTexture(VAddr address, TextureFormat format, u32 width,
     case TextureFormat::A1B5G5R5:
     case TextureFormat::B5G6R5:
     case TextureFormat::R8:
+    case TextureFormat::G8R8:
     case TextureFormat::R16_G16_B16_A16:
     case TextureFormat::R32_G32_B32_A32:
     case TextureFormat::BF10GF11RF11:
@@ -167,6 +169,7 @@ std::vector<u8> DecodeTexture(const std::vector<u8>& texture_data, TextureFormat
     case TextureFormat::A1B5G5R5:
     case TextureFormat::B5G6R5:
     case TextureFormat::R8:
+    case TextureFormat::G8R8:
     case TextureFormat::BF10GF11RF11:
     case TextureFormat::R32_G32_B32_A32:
         // TODO(Subv): For the time being just forward the same data without any decoding.
-- 
GitLab