aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs
diff options
context:
space:
mode:
authorTSR Berry <20988865+TSRBerry@users.noreply.github.com>2023-04-08 01:22:00 +0200
committerMary <thog@protonmail.com>2023-04-27 23:51:14 +0200
commitcee712105850ac3385cd0091a923438167433f9f (patch)
tree4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /src/Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs
parentcd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff)
Move solution and projects to src
Diffstat (limited to 'src/Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs')
-rw-r--r--src/Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs b/src/Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs
new file mode 100644
index 00000000..77e52667
--- /dev/null
+++ b/src/Ryujinx.Graphics.Gpu/Shader/DiskCache/ShaderBinarySerializer.cs
@@ -0,0 +1,66 @@
+using Ryujinx.Common;
+using Ryujinx.Common.Memory;
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.Shader;
+using Ryujinx.Graphics.Shader.Translation;
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
+{
+ static class ShaderBinarySerializer
+ {
+ public static byte[] Pack(ShaderSource[] sources)
+ {
+ using MemoryStream output = MemoryStreamManager.Shared.GetStream();
+
+ output.Write(sources.Length);
+
+ foreach (ShaderSource source in sources)
+ {
+ output.Write((int)source.Stage);
+ output.Write(source.BinaryCode.Length);
+ output.Write(source.BinaryCode);
+ }
+
+ return output.ToArray();
+ }
+
+ 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>();
+
+ 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, 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?.Info != null && currentStage.Info.Stage == stage)
+ {
+ 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