From e21190f47f2fdc0b24f1220700640ed854972ccc Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Fri, 13 Jul 2018 23:25:11 -0400
Subject: [PATCH] gl_rasterizer_cache: Implement depth format Z16_UNORM.

---
 src/video_core/renderer_opengl/gl_rasterizer_cache.cpp | 5 ++++-
 src/video_core/renderer_opengl/gl_rasterizer_cache.h   | 8 ++++++++
 src/video_core/textures/decoders.cpp                   | 3 +++
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
index 323ff7408e..361a09626b 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
@@ -112,6 +112,8 @@ static constexpr std::array<FormatTuple, SurfaceParams::MaxPixelFormat> tex_form
     {GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, ComponentType::UNorm,
      false},                                                                            // S8Z24
     {GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, ComponentType::Float, false}, // Z32F
+    {GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, ComponentType::UNorm,
+     false}, // Z16
 }};
 
 static const FormatTuple& GetFormatTuple(PixelFormat pixel_format, ComponentType component_type) {
@@ -195,7 +197,7 @@ static constexpr std::array<void (*)(u32, u32, u32, u8*, Tegra::GPUVAddr),
         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::Z32F>,         MortonCopy<true, PixelFormat::Z16>,
 };
 
 static constexpr std::array<void (*)(u32, u32, u32, u8*, Tegra::GPUVAddr),
@@ -219,6 +221,7 @@ static constexpr std::array<void (*)(u32, u32, u32, u8*, Tegra::GPUVAddr),
         MortonCopy<false, PixelFormat::Z24S8>,
         MortonCopy<false, PixelFormat::S8Z24>,
         MortonCopy<false, PixelFormat::Z32F>,
+        MortonCopy<false, PixelFormat::Z16>,
 };
 
 // Allocate an uninitialized texture of appropriate size and format for the surface
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.h b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
index 1bedae992e..0f5b1ff328 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
@@ -44,6 +44,7 @@ struct SurfaceParams {
         Z24S8 = 14,
         S8Z24 = 15,
         Z32F = 16,
+        Z16 = 17,
 
         MaxDepthStencilFormat,
 
@@ -98,6 +99,7 @@ struct SurfaceParams {
             1, // Z24S8
             1, // S8Z24
             1, // Z32F
+            1, // Z16
         }};
 
         ASSERT(static_cast<size_t>(format) < compression_factor_table.size());
@@ -126,6 +128,7 @@ struct SurfaceParams {
             32,  // Z24S8
             32,  // S8Z24
             32,  // Z32F
+            16,  // Z16
         }};
 
         ASSERT(static_cast<size_t>(format) < bpp_table.size());
@@ -143,6 +146,8 @@ struct SurfaceParams {
             return PixelFormat::Z24S8;
         case Tegra::DepthFormat::Z32_FLOAT:
             return PixelFormat::Z32F;
+        case Tegra::DepthFormat::Z16_UNORM:
+            return PixelFormat::Z16;
         default:
             LOG_CRITICAL(HW_GPU, "Unimplemented format={}", static_cast<u32>(format));
             UNREACHABLE();
@@ -249,6 +254,8 @@ struct SurfaceParams {
             return Tegra::DepthFormat::Z24_S8_UNORM;
         case PixelFormat::Z32F:
             return Tegra::DepthFormat::Z32_FLOAT;
+        case PixelFormat::Z16:
+            return Tegra::DepthFormat::Z16_UNORM;
         default:
             UNREACHABLE();
         }
@@ -295,6 +302,7 @@ struct SurfaceParams {
 
     static ComponentType ComponentTypeFromDepthFormat(Tegra::DepthFormat format) {
         switch (format) {
+        case Tegra::DepthFormat::Z16_UNORM:
         case Tegra::DepthFormat::S8_Z24_UNORM:
         case Tegra::DepthFormat::Z24_S8_UNORM:
             return ComponentType::UNorm;
diff --git a/src/video_core/textures/decoders.cpp b/src/video_core/textures/decoders.cpp
index b3937b2fe1..a4ba9f66ac 100644
--- a/src/video_core/textures/decoders.cpp
+++ b/src/video_core/textures/decoders.cpp
@@ -77,6 +77,8 @@ u32 BytesPerPixel(TextureFormat format) {
 
 static u32 DepthBytesPerPixel(DepthFormat format) {
     switch (format) {
+    case DepthFormat::Z16_UNORM:
+        return 2;
     case DepthFormat::S8_Z24_UNORM:
     case DepthFormat::Z24_S8_UNORM:
     case DepthFormat::Z32_FLOAT:
@@ -133,6 +135,7 @@ std::vector<u8> UnswizzleDepthTexture(VAddr address, DepthFormat format, u32 wid
     std::vector<u8> unswizzled_data(width * height * bytes_per_pixel);
 
     switch (format) {
+    case DepthFormat::Z16_UNORM:
     case DepthFormat::S8_Z24_UNORM:
     case DepthFormat::Z24_S8_UNORM:
     case DepthFormat::Z32_FLOAT: