From 8a78a703f2e8e374286efaae575d87fb38242427 Mon Sep 17 00:00:00 2001
From: HorrorTroll <sonicvipduc@gmail.com>
Date: Mon, 17 Sep 2018 07:24:56 +0700
Subject: [PATCH] Implement B5G6R5Unorm, BGR5A1Unorm, RGBA32Uint, R16Unorm and
 Z16 texture format. Fix BC6H_UF16 from Unorm to Sfloat (#417)

* Implement B5G6R5Unorm & BGR5A1Unorm

* Implement RGBA32Uint

* Implement R16Unorm & Z16 texture format

* Fix BC6H_UF16 from Unorm to Sfloat
---
 Ryujinx.Graphics/Gal/GalTextureFormat.cs      |  1 +
 .../Gal/OpenGL/OGLEnumConverter.cs            | 20 ++++----
 Ryujinx.Graphics/Texture/ImageUtils.cs        | 49 ++++++++++---------
 3 files changed, 38 insertions(+), 32 deletions(-)

diff --git a/Ryujinx.Graphics/Gal/GalTextureFormat.cs b/Ryujinx.Graphics/Gal/GalTextureFormat.cs
index 009d2b826e..e865859530 100644
--- a/Ryujinx.Graphics/Gal/GalTextureFormat.cs
+++ b/Ryujinx.Graphics/Gal/GalTextureFormat.cs
@@ -26,6 +26,7 @@ namespace Ryujinx.Graphics.Gal
         Z24S8        = 0x29,
         ZF32         = 0x2f,
         ZF32_X24S8   = 0x30,
+        Z16          = 0x3a,
         Astc2D4x4    = 0x40,
         Astc2D5x5    = 0x41,
         Astc2D6x6    = 0x42,
diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLEnumConverter.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLEnumConverter.cs
index 69c133a335..876c7b992a 100644
--- a/Ryujinx.Graphics/Gal/OpenGL/OGLEnumConverter.cs
+++ b/Ryujinx.Graphics/Gal/OpenGL/OGLEnumConverter.cs
@@ -183,16 +183,16 @@ namespace Ryujinx.Graphics.Gal.OpenGL
         {
             switch (Format)
             {
-                case GalImageFormat.BC6H_UF16 | GalImageFormat.Unorm: return InternalFormat.CompressedRgbBptcUnsignedFloat;
-                case GalImageFormat.BC6H_SF16 | GalImageFormat.Unorm: return InternalFormat.CompressedRgbBptcSignedFloat;
-                case GalImageFormat.BC7       | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaBptcUnorm;
-                case GalImageFormat.BC1_RGBA  | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaS3tcDxt1Ext;
-                case GalImageFormat.BC2       | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaS3tcDxt3Ext;
-                case GalImageFormat.BC3       | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaS3tcDxt5Ext;
-                case GalImageFormat.BC4       | GalImageFormat.Snorm: return InternalFormat.CompressedSignedRedRgtc1;
-                case GalImageFormat.BC4       | GalImageFormat.Unorm: return InternalFormat.CompressedRedRgtc1;
-                case GalImageFormat.BC5       | GalImageFormat.Snorm: return InternalFormat.CompressedSignedRgRgtc2;
-                case GalImageFormat.BC5       | GalImageFormat.Unorm: return InternalFormat.CompressedRgRgtc2;
+                case GalImageFormat.BC6H_UF16 | GalImageFormat.Sfloat: return InternalFormat.CompressedRgbBptcUnsignedFloat;
+                case GalImageFormat.BC6H_SF16 | GalImageFormat.Unorm:  return InternalFormat.CompressedRgbBptcSignedFloat;
+                case GalImageFormat.BC7       | GalImageFormat.Unorm:  return InternalFormat.CompressedRgbaBptcUnorm;
+                case GalImageFormat.BC1_RGBA  | GalImageFormat.Unorm:  return InternalFormat.CompressedRgbaS3tcDxt1Ext;
+                case GalImageFormat.BC2       | GalImageFormat.Unorm:  return InternalFormat.CompressedRgbaS3tcDxt3Ext;
+                case GalImageFormat.BC3       | GalImageFormat.Unorm:  return InternalFormat.CompressedRgbaS3tcDxt5Ext;
+                case GalImageFormat.BC4       | GalImageFormat.Snorm:  return InternalFormat.CompressedSignedRedRgtc1;
+                case GalImageFormat.BC4       | GalImageFormat.Unorm:  return InternalFormat.CompressedRedRgtc1;
+                case GalImageFormat.BC5       | GalImageFormat.Snorm:  return InternalFormat.CompressedSignedRgRgtc2;
+                case GalImageFormat.BC5       | GalImageFormat.Unorm:  return InternalFormat.CompressedRgRgtc2;
             }
 
             throw new NotImplementedException($"{Format & GalImageFormat.FormatMask} {Format & GalImageFormat.TypeMask}");
diff --git a/Ryujinx.Graphics/Texture/ImageUtils.cs b/Ryujinx.Graphics/Texture/ImageUtils.cs
index c09eaf8693..f6db08944e 100644
--- a/Ryujinx.Graphics/Texture/ImageUtils.cs
+++ b/Ryujinx.Graphics/Texture/ImageUtils.cs
@@ -56,30 +56,31 @@ namespace Ryujinx.Graphics.Texture
                 { GalTextureFormat.Z24S8,        GalImageFormat.D24_S8               | Unorm                        },
                 { GalTextureFormat.ZF32,         GalImageFormat.D32                                        | Sfloat },
                 { GalTextureFormat.ZF32_X24S8,   GalImageFormat.D32_S8               | Unorm                        },
+                { GalTextureFormat.Z16,          GalImageFormat.D16                  | Unorm                        },
 
                 //Compressed formats
-                { GalTextureFormat.BC6H_SF16,   GalImageFormat.BC6H_SF16  | Unorm         },
-                { GalTextureFormat.BC6H_UF16,   GalImageFormat.BC6H_UF16  | Unorm         },
-                { GalTextureFormat.BC7U,        GalImageFormat.BC7        | Unorm         },
-                { GalTextureFormat.BC1,         GalImageFormat.BC1_RGBA   | Unorm         },
-                { GalTextureFormat.BC2,         GalImageFormat.BC2        | Unorm         },
-                { GalTextureFormat.BC3,         GalImageFormat.BC3        | Unorm         },
-                { GalTextureFormat.BC4,         GalImageFormat.BC4        | Unorm | Snorm },
-                { GalTextureFormat.BC5,         GalImageFormat.BC5        | Unorm | Snorm },
-                { GalTextureFormat.Astc2D4x4,   GalImageFormat.ASTC_4x4   | Unorm         },
-                { GalTextureFormat.Astc2D5x5,   GalImageFormat.ASTC_5x5   | Unorm         },
-                { GalTextureFormat.Astc2D6x6,   GalImageFormat.ASTC_6x6   | Unorm         },
-                { GalTextureFormat.Astc2D8x8,   GalImageFormat.ASTC_8x8   | Unorm         },
-                { GalTextureFormat.Astc2D10x10, GalImageFormat.ASTC_10x10 | Unorm         },
-                { GalTextureFormat.Astc2D12x12, GalImageFormat.ASTC_12x12 | Unorm         },
-                { GalTextureFormat.Astc2D5x4,   GalImageFormat.ASTC_5x4   | Unorm         },
-                { GalTextureFormat.Astc2D6x5,   GalImageFormat.ASTC_6x5   | Unorm         },
-                { GalTextureFormat.Astc2D8x6,   GalImageFormat.ASTC_8x6   | Unorm         },
-                { GalTextureFormat.Astc2D10x8,  GalImageFormat.ASTC_10x8  | Unorm         },
-                { GalTextureFormat.Astc2D12x10, GalImageFormat.ASTC_12x10 | Unorm         },
-                { GalTextureFormat.Astc2D8x5,   GalImageFormat.ASTC_8x5   | Unorm         },
-                { GalTextureFormat.Astc2D10x5,  GalImageFormat.ASTC_10x5  | Unorm         },
-                { GalTextureFormat.Astc2D10x6,  GalImageFormat.ASTC_10x6  | Unorm         }
+                { GalTextureFormat.BC6H_SF16,   GalImageFormat.BC6H_SF16  | Unorm                  },
+                { GalTextureFormat.BC6H_UF16,   GalImageFormat.BC6H_UF16                  | Sfloat },
+                { GalTextureFormat.BC7U,        GalImageFormat.BC7        | Unorm                  },
+                { GalTextureFormat.BC1,         GalImageFormat.BC1_RGBA   | Unorm                  },
+                { GalTextureFormat.BC2,         GalImageFormat.BC2        | Unorm                  },
+                { GalTextureFormat.BC3,         GalImageFormat.BC3        | Unorm                  },
+                { GalTextureFormat.BC4,         GalImageFormat.BC4        | Unorm | Snorm          },
+                { GalTextureFormat.BC5,         GalImageFormat.BC5        | Unorm | Snorm          },
+                { GalTextureFormat.Astc2D4x4,   GalImageFormat.ASTC_4x4   | Unorm                  },
+                { GalTextureFormat.Astc2D5x5,   GalImageFormat.ASTC_5x5   | Unorm                  },
+                { GalTextureFormat.Astc2D6x6,   GalImageFormat.ASTC_6x6   | Unorm                  },
+                { GalTextureFormat.Astc2D8x8,   GalImageFormat.ASTC_8x8   | Unorm                  },
+                { GalTextureFormat.Astc2D10x10, GalImageFormat.ASTC_10x10 | Unorm                  },
+                { GalTextureFormat.Astc2D12x12, GalImageFormat.ASTC_12x12 | Unorm                  },
+                { GalTextureFormat.Astc2D5x4,   GalImageFormat.ASTC_5x4   | Unorm                  },
+                { GalTextureFormat.Astc2D6x5,   GalImageFormat.ASTC_6x5   | Unorm                  },
+                { GalTextureFormat.Astc2D8x6,   GalImageFormat.ASTC_8x6   | Unorm                  },
+                { GalTextureFormat.Astc2D10x8,  GalImageFormat.ASTC_10x8  | Unorm                  },
+                { GalTextureFormat.Astc2D12x10, GalImageFormat.ASTC_12x10 | Unorm                  },
+                { GalTextureFormat.Astc2D8x5,   GalImageFormat.ASTC_8x5   | Unorm                  },
+                { GalTextureFormat.Astc2D10x5,  GalImageFormat.ASTC_10x5  | Unorm                  },
+                { GalTextureFormat.Astc2D10x6,  GalImageFormat.ASTC_10x6  | Unorm                  }
             };
 
         private static readonly Dictionary<GalImageFormat, ImageDescriptor> s_ImageTable =
@@ -167,6 +168,7 @@ namespace Ryujinx.Graphics.Texture
             switch (Format)
             {
                 case GalSurfaceFormat.RGBA32Float:    return GalImageFormat.R32G32B32A32   | Sfloat;
+                case GalSurfaceFormat.RGBA32Uint:     return GalImageFormat.R32G32B32A32   | Uint;
                 case GalSurfaceFormat.RGBA16Float:    return GalImageFormat.R16G16B16A16   | Sfloat;
                 case GalSurfaceFormat.RG32Float:      return GalImageFormat.R32G32         | Sfloat;
                 case GalSurfaceFormat.RG32Sint:       return GalImageFormat.R32G32         | Sint;
@@ -184,7 +186,10 @@ namespace Ryujinx.Graphics.Texture
                 case GalSurfaceFormat.RG8Unorm:       return GalImageFormat.R8G8           | Unorm;
                 case GalSurfaceFormat.RG8Snorm:       return GalImageFormat.R8             | Snorm;
                 case GalSurfaceFormat.R16Float:       return GalImageFormat.R16            | Sfloat;
+                case GalSurfaceFormat.R16Unorm:       return GalImageFormat.R16            | Unorm;
                 case GalSurfaceFormat.R8Unorm:        return GalImageFormat.R8             | Unorm;
+                case GalSurfaceFormat.B5G6R5Unorm:    return GalImageFormat.B5G6R5         | Unorm;
+                case GalSurfaceFormat.BGR5A1Unorm:    return GalImageFormat.A1R5G5B5       | Unorm;
             }
 
             throw new NotImplementedException(Format.ToString());