From 044b84b078c910c9c1d16c5351cd037716db70e3 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Tue, 23 Oct 2018 16:04:08 -0300
Subject: [PATCH] Add depth range support on the GPU (#472)

* Add depth range support on the GPU

* Address PR feedback
---
 Ryujinx.Graphics/Gal/GalPipelineState.cs   |  2 +
 Ryujinx.Graphics/Gal/OpenGL/OGLPipeline.cs | 48 +++++++++++++---------
 Ryujinx.Graphics/NvGpuEngine3d.cs          |  3 ++
 Ryujinx.Graphics/NvGpuEngine3dReg.cs       |  2 +
 4 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/Ryujinx.Graphics/Gal/GalPipelineState.cs b/Ryujinx.Graphics/Gal/GalPipelineState.cs
index 56b0fbde3..17f52dd6c 100644
--- a/Ryujinx.Graphics/Gal/GalPipelineState.cs
+++ b/Ryujinx.Graphics/Gal/GalPipelineState.cs
@@ -43,6 +43,8 @@
         public bool DepthTestEnabled;
         public bool DepthWriteEnabled;
         public GalComparisonOp DepthFunc;
+        public float DepthRangeNear;
+        public float DepthRangeFar;
 
         public bool StencilTestEnabled;
         public bool StencilTwoSideEnabled;
diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLPipeline.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLPipeline.cs
index 00699641f..656d8e8e4 100644
--- a/Ryujinx.Graphics/Gal/OpenGL/OGLPipeline.cs
+++ b/Ryujinx.Graphics/Gal/OpenGL/OGLPipeline.cs
@@ -95,44 +95,46 @@ namespace Ryujinx.Graphics.Gal.OpenGL
                 FrontFace = GalFrontFace.CCW,
 
                 CullFaceEnabled = false,
-                CullFace = GalCullFace.Back,
+                CullFace        = GalCullFace.Back,
 
-                DepthTestEnabled = false,
+                DepthTestEnabled  = false,
                 DepthWriteEnabled = true,
-                DepthFunc = GalComparisonOp.Less,
+                DepthFunc         = GalComparisonOp.Less,
+                DepthRangeNear    = 0,
+                DepthRangeFar     = 1,
 
                 StencilTestEnabled = false,
 
                 StencilBackFuncFunc = GalComparisonOp.Always,
-                StencilBackFuncRef = 0,
+                StencilBackFuncRef  = 0,
                 StencilBackFuncMask = UInt32.MaxValue,
-                StencilBackOpFail = GalStencilOp.Keep,
-                StencilBackOpZFail = GalStencilOp.Keep,
-                StencilBackOpZPass = GalStencilOp.Keep,
-                StencilBackMask = UInt32.MaxValue,
+                StencilBackOpFail   = GalStencilOp.Keep,
+                StencilBackOpZFail  = GalStencilOp.Keep,
+                StencilBackOpZPass  = GalStencilOp.Keep,
+                StencilBackMask     = UInt32.MaxValue,
 
                 StencilFrontFuncFunc = GalComparisonOp.Always,
-                StencilFrontFuncRef = 0,
+                StencilFrontFuncRef  = 0,
                 StencilFrontFuncMask = UInt32.MaxValue,
-                StencilFrontOpFail = GalStencilOp.Keep,
-                StencilFrontOpZFail = GalStencilOp.Keep,
-                StencilFrontOpZPass = GalStencilOp.Keep,
-                StencilFrontMask = UInt32.MaxValue,
+                StencilFrontOpFail   = GalStencilOp.Keep,
+                StencilFrontOpZFail  = GalStencilOp.Keep,
+                StencilFrontOpZPass  = GalStencilOp.Keep,
+                StencilFrontMask     = UInt32.MaxValue,
 
-                BlendEnabled = false,
+                BlendEnabled       = false,
                 BlendSeparateAlpha = false,
 
-                BlendEquationRgb = 0,
-                BlendFuncSrcRgb = GalBlendFactor.One,
-                BlendFuncDstRgb = GalBlendFactor.Zero,
+                BlendEquationRgb   = 0,
+                BlendFuncSrcRgb    = GalBlendFactor.One,
+                BlendFuncDstRgb    = GalBlendFactor.Zero,
                 BlendEquationAlpha = 0,
-                BlendFuncSrcAlpha = GalBlendFactor.One,
-                BlendFuncDstAlpha = GalBlendFactor.Zero,
+                BlendFuncSrcAlpha  = GalBlendFactor.One,
+                BlendFuncDstAlpha  = GalBlendFactor.Zero,
 
                 ColorMask = ColorMaskRgba.Default,
 
                 PrimitiveRestartEnabled = false,
-                PrimitiveRestartIndex = 0
+                PrimitiveRestartIndex   = 0
             };
 
             for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
@@ -195,6 +197,12 @@ namespace Ryujinx.Graphics.Gal.OpenGL
                 }
             }
 
+            if (New.DepthRangeNear != Old.DepthRangeNear ||
+                New.DepthRangeFar  != Old.DepthRangeFar)
+            {
+                GL.DepthRange(New.DepthRangeNear, New.DepthRangeFar);
+            }
+
             if (New.StencilTestEnabled != Old.StencilTestEnabled)
             {
                 Enable(EnableCap.StencilTest, New.StencilTestEnabled);
diff --git a/Ryujinx.Graphics/NvGpuEngine3d.cs b/Ryujinx.Graphics/NvGpuEngine3d.cs
index fd15d0b6a..e8ded8898 100644
--- a/Ryujinx.Graphics/NvGpuEngine3d.cs
+++ b/Ryujinx.Graphics/NvGpuEngine3d.cs
@@ -371,6 +371,9 @@ namespace Ryujinx.Graphics
             {
                 State.DepthFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.DepthTestFunction);
             }
+
+            State.DepthRangeNear = ReadRegisterFloat(NvGpuEngine3dReg.DepthRangeNNear);
+            State.DepthRangeFar  = ReadRegisterFloat(NvGpuEngine3dReg.DepthRangeNFar);
         }
 
         private void SetStencil(GalPipelineState State)
diff --git a/Ryujinx.Graphics/NvGpuEngine3dReg.cs b/Ryujinx.Graphics/NvGpuEngine3dReg.cs
index ef74e4f67..6ea226542 100644
--- a/Ryujinx.Graphics/NvGpuEngine3dReg.cs
+++ b/Ryujinx.Graphics/NvGpuEngine3dReg.cs
@@ -15,6 +15,8 @@ namespace Ryujinx.Graphics
         ViewportNTranslateZ  = 0x285,
         ViewportNHoriz       = 0x300,
         ViewportNVert        = 0x301,
+        DepthRangeNNear      = 0x302,
+        DepthRangeNFar       = 0x303,
         VertexArrayFirst     = 0x35d,
         VertexArrayCount     = 0x35e,
         ClearNColor          = 0x360,