From 69ced3a6e8636cfe7ea2f7333dd7cb4512def241 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Sat, 19 Nov 2022 06:24:23 -0300
Subject: Fix shader cache on Vulkan when geometry shaders are inserted (#3868)

---
 .../Shader/DiskCache/ShaderBinarySerializer.cs     | 36 ++++++++++++++++------
 1 file changed, 26 insertions(+), 10 deletions(-)

(limited to 'Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs')

diff --git a/Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs b/Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs
index 11e54220..0e5ce292 100644
--- a/Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs
+++ b/Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs
@@ -1,5 +1,7 @@
 using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.Shader;
 using Ryujinx.Graphics.Shader.Translation;
+using System;
 using System.Collections.Generic;
 using System.IO;
 
@@ -12,8 +14,11 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
             using MemoryStream output = new MemoryStream();
             using BinaryWriter writer = new BinaryWriter(output);
 
+            writer.Write(sources.Length);
+
             for (int i = 0; i < sources.Length; i++)
             {
+                writer.Write((int)sources[i].Stage);
                 writer.Write(sources[i].BinaryCode.Length);
                 writer.Write(sources[i].BinaryCode);
             }
@@ -21,29 +26,40 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
             return output.ToArray();
         }
 
-        public static ShaderSource[] Unpack(CachedShaderStage[] stages, byte[] code, bool compute)
+        public static ShaderSource[] Unpack(CachedShaderStage[] stages, byte[] code)
         {
             using MemoryStream input = new MemoryStream(code);
             using BinaryReader reader = new BinaryReader(input);
 
             List<ShaderSource> output = new List<ShaderSource>();
 
-            for (int i = compute ? 0 : 1; i < stages.Length; i++)
-            {
-                CachedShaderStage stage = stages[i];
-
-                if (stage == null)
-                {
-                    continue;
-                }
+            int count = reader.ReadInt32();
 
+            for (int i = 0; i < count; i++)
+            {
+                ShaderStage stage = (ShaderStage)reader.ReadInt32();
                 int binaryCodeLength = reader.ReadInt32();
                 byte[] binaryCode = reader.ReadBytes(binaryCodeLength);
 
-                output.Add(new ShaderSource(binaryCode, ShaderCache.GetBindings(stage.Info), stage.Info.Stage, TargetLanguage.Spirv));
+                output.Add(new ShaderSource(binaryCode, GetBindings(stages, stage), stage, TargetLanguage.Spirv));
             }
 
             return output.ToArray();
         }
+
+        private static ShaderBindings GetBindings(CachedShaderStage[] stages, ShaderStage stage)
+        {
+            for (int i = 0; i < stages.Length; i++)
+            {
+                CachedShaderStage currentStage = stages[i];
+
+                if (currentStage != null && currentStage.Info.Stage == stage && currentStage.Info != null)
+                {
+                    return ShaderCache.GetBindings(currentStage.Info);
+                }
+            }
+
+            return new ShaderBindings(Array.Empty<int>(), Array.Empty<int>(), Array.Empty<int>(), Array.Empty<int>());
+        }
     }
 }
\ No newline at end of file
-- 
cgit v1.2.3-70-g09d2