From 4741a05df95dd8964543f9770d8bfe15d842beaf Mon Sep 17 00:00:00 2001
From: riperiperi <rhy3756547@hotmail.com>
Date: Thu, 1 Jun 2023 08:05:39 +0100
Subject: Vulkan: Include DepthMode in ProgramPipelineState (#5185)

---
 src/Ryujinx.Graphics.GAL/ProgramPipelineState.cs          |  2 ++
 src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs    |  6 +++++-
 .../Shader/ShaderSpecializationState.cs                   | 15 +++++++++++++++
 src/Ryujinx.Graphics.Vulkan/PipelineConverter.cs          |  1 +
 4 files changed, 23 insertions(+), 1 deletion(-)

(limited to 'src')

diff --git a/src/Ryujinx.Graphics.GAL/ProgramPipelineState.cs b/src/Ryujinx.Graphics.GAL/ProgramPipelineState.cs
index 41afb34b..96fd667a 100644
--- a/src/Ryujinx.Graphics.GAL/ProgramPipelineState.cs
+++ b/src/Ryujinx.Graphics.GAL/ProgramPipelineState.cs
@@ -63,6 +63,8 @@ namespace Ryujinx.Graphics.GAL
         public bool PrimitiveRestartEnable;
         public uint PatchControlPoints;
 
+        public DepthMode DepthMode;
+
         public void SetVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
         {
             VertexAttribCount = vertexAttribs.Length;
diff --git a/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs b/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs
index 4feb8baf..5fa4702b 100644
--- a/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs
+++ b/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs
@@ -771,7 +771,11 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
         /// </summary>
         private void UpdateDepthMode()
         {
-            _context.Renderer.Pipeline.SetDepthMode(GetDepthMode());
+            DepthMode mode = GetDepthMode();
+
+            _pipeline.DepthMode = mode;
+
+            _context.Renderer.Pipeline.SetDepthMode(mode);
         }
 
         /// <summary>
diff --git a/src/Ryujinx.Graphics.Gpu/Shader/ShaderSpecializationState.cs b/src/Ryujinx.Graphics.Gpu/Shader/ShaderSpecializationState.cs
index b2c4fccd..9b0c8b9b 100644
--- a/src/Ryujinx.Graphics.Gpu/Shader/ShaderSpecializationState.cs
+++ b/src/Ryujinx.Graphics.Gpu/Shader/ShaderSpecializationState.cs
@@ -736,6 +736,19 @@ namespace Ryujinx.Graphics.Gpu.Shader
             return MatchesTexture(specializationState, descriptor);
         }
 
+        /// <summary>
+        /// Populates pipeline state that doesn't exist in older caches with default values
+        /// based on specialization state.
+        /// </summary>
+        /// <param name="pipelineState">Pipeline state to prepare</param>
+        private void PreparePipelineState(ref ProgramPipelineState pipelineState)
+        {
+            if (!_compute)
+            {
+                pipelineState.DepthMode = GraphicsState.DepthMode ? DepthMode.MinusOneToOne : DepthMode.ZeroToOne;
+            }
+        }
+
         /// <summary>
         /// Reads shader specialization state that has been serialized.
         /// </summary>
@@ -776,6 +789,8 @@ namespace Ryujinx.Graphics.Gpu.Shader
             {
                 ProgramPipelineState pipelineState = default;
                 dataReader.ReadWithMagicAndSize(ref pipelineState, PgpsMagic);
+
+                specState.PreparePipelineState(ref pipelineState);
                 specState.PipelineState = pipelineState;
             }
 
diff --git a/src/Ryujinx.Graphics.Vulkan/PipelineConverter.cs b/src/Ryujinx.Graphics.Vulkan/PipelineConverter.cs
index 79179ce0..a52b4462 100644
--- a/src/Ryujinx.Graphics.Vulkan/PipelineConverter.cs
+++ b/src/Ryujinx.Graphics.Vulkan/PipelineConverter.cs
@@ -165,6 +165,7 @@ namespace Ryujinx.Graphics.Vulkan
             pipeline.DepthTestEnable = state.DepthTest.TestEnable;
             pipeline.DepthWriteEnable = state.DepthTest.WriteEnable;
             pipeline.DepthCompareOp = state.DepthTest.Func.Convert();
+            pipeline.DepthMode = state.DepthMode == DepthMode.MinusOneToOne;
 
             pipeline.FrontFace = state.FrontFace.Convert();
 
-- 
cgit v1.2.3-70-g09d2