From 3f9262195b1ab4828d93f661e1b0dedacb01f937 Mon Sep 17 00:00:00 2001
From: Fernando Sahmkow <fsahmkow27@gmail.com>
Date: Sun, 27 Oct 2019 16:44:09 -0400
Subject: [PATCH] Video_Core: Implement texture format E5B9G9R9_SHAREDEXP.

This commit implements the E5B9G9R9 Texture format into the general
system and OpenGL backend.
---
 src/video_core/morton.cpp                        |  2 ++
 .../renderer_opengl/gl_texture_cache.cpp         |  1 +
 src/video_core/surface.cpp                       |  8 ++++++++
 src/video_core/surface.h                         | 16 +++++++++++-----
 4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/src/video_core/morton.cpp b/src/video_core/morton.cpp
index fe5f08ace..2f2fe6859 100644
--- a/src/video_core/morton.cpp
+++ b/src/video_core/morton.cpp
@@ -112,6 +112,7 @@ static constexpr ConversionArray morton_to_linear_fns = {
     MortonCopy<true, PixelFormat::ASTC_2D_8X6_SRGB>,
     MortonCopy<true, PixelFormat::ASTC_2D_6X5>,
     MortonCopy<true, PixelFormat::ASTC_2D_6X5_SRGB>,
+    MortonCopy<true, PixelFormat::E5B9G9R9F>,
     MortonCopy<true, PixelFormat::Z32F>,
     MortonCopy<true, PixelFormat::Z16>,
     MortonCopy<true, PixelFormat::Z24S8>,
@@ -192,6 +193,7 @@ static constexpr ConversionArray linear_to_morton_fns = {
     nullptr,
     nullptr,
     nullptr,
+    MortonCopy<false, PixelFormat::E5B9G9R9F>,
     MortonCopy<false, PixelFormat::Z32F>,
     MortonCopy<false, PixelFormat::Z16>,
     MortonCopy<false, PixelFormat::Z24S8>,
diff --git a/src/video_core/renderer_opengl/gl_texture_cache.cpp b/src/video_core/renderer_opengl/gl_texture_cache.cpp
index 2f9bfd7e4..55b3e58b2 100644
--- a/src/video_core/renderer_opengl/gl_texture_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_texture_cache.cpp
@@ -131,6 +131,7 @@ constexpr std::array<FormatTuple, VideoCore::Surface::MaxPixelFormat> tex_format
     {GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_8X6_SRGB
     {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false},        // ASTC_2D_6X5
     {GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_6X5_SRGB
+    {GL_RGB9_E5, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, ComponentType::Float, false}, // E5B9G9R9F
 
     // Depth formats
     {GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, ComponentType::Float, false}, // Z32F
diff --git a/src/video_core/surface.cpp b/src/video_core/surface.cpp
index 9a3c05288..621136b6e 100644
--- a/src/video_core/surface.cpp
+++ b/src/video_core/surface.cpp
@@ -315,6 +315,14 @@ PixelFormat PixelFormatFromTextureFormat(Tegra::Texture::TextureFormat format,
             break;
         }
         break;
+    case Tegra::Texture::TextureFormat::E5B9G9R9_SHAREDEXP:
+        switch (component_type) {
+        case Tegra::Texture::ComponentType::FLOAT:
+            return PixelFormat::E5B9G9R9F;
+        default:
+            break;
+        }
+        break;
     case Tegra::Texture::TextureFormat::ZF32:
         return PixelFormat::Z32F;
     case Tegra::Texture::TextureFormat::Z16:
diff --git a/src/video_core/surface.h b/src/video_core/surface.h
index 97668f802..d3bcd38c5 100644
--- a/src/video_core/surface.h
+++ b/src/video_core/surface.h
@@ -86,19 +86,20 @@ enum class PixelFormat {
     ASTC_2D_8X6_SRGB = 68,
     ASTC_2D_6X5 = 69,
     ASTC_2D_6X5_SRGB = 70,
+    E5B9G9R9F = 71,
 
     MaxColorFormat,
 
     // Depth formats
-    Z32F = 71,
-    Z16 = 72,
+    Z32F = 72,
+    Z16 = 73,
 
     MaxDepthFormat,
 
     // DepthStencil formats
-    Z24S8 = 73,
-    S8Z24 = 74,
-    Z32FS8 = 75,
+    Z24S8 = 74,
+    S8Z24 = 75,
+    Z32FS8 = 76,
 
     MaxDepthStencilFormat,
 
@@ -207,6 +208,7 @@ constexpr std::array<u32, MaxPixelFormat> compression_factor_shift_table = {{
     2, // ASTC_2D_8X6_SRGB
     2, // ASTC_2D_6X5
     2, // ASTC_2D_6X5_SRGB
+    0, // E5B9G9R9F
     0, // Z32F
     0, // Z16
     0, // Z24S8
@@ -302,6 +304,7 @@ constexpr std::array<u32, MaxPixelFormat> block_width_table = {{
     8,  // ASTC_2D_8X6_SRGB
     6,  // ASTC_2D_6X5
     6,  // ASTC_2D_6X5_SRGB
+    1,  // E5B9G9R9F
     1,  // Z32F
     1,  // Z16
     1,  // Z24S8
@@ -389,6 +392,7 @@ constexpr std::array<u32, MaxPixelFormat> block_height_table = {{
     6,  // ASTC_2D_8X6_SRGB
     5,  // ASTC_2D_6X5
     5,  // ASTC_2D_6X5_SRGB
+    1,  // E5B9G9R9F
     1,  // Z32F
     1,  // Z16
     1,  // Z24S8
@@ -476,6 +480,7 @@ constexpr std::array<u32, MaxPixelFormat> bpp_table = {{
     128, // ASTC_2D_8X6_SRGB
     128, // ASTC_2D_6X5
     128, // ASTC_2D_6X5_SRGB
+    32,  // E5B9G9R9F
     32,  // Z32F
     16,  // Z16
     32,  // Z24S8
@@ -578,6 +583,7 @@ constexpr std::array<SurfaceCompression, MaxPixelFormat> compression_type_table
     SurfaceCompression::Converted,  // ASTC_2D_8X6_SRGB
     SurfaceCompression::Converted,  // ASTC_2D_6X5
     SurfaceCompression::Converted,  // ASTC_2D_6X5_SRGB
+    SurfaceCompression::None,       // E5B9G9R9F
     SurfaceCompression::None,       // Z32F
     SurfaceCompression::None,       // Z16
     SurfaceCompression::None,       // Z24S8