/ src / Ryujinx.Graphics.Gpu / Shader / DiskCache / ShaderBinarySerializer.cs
ShaderBinarySerializer.cs
 1  using Ryujinx.Common;
 2  using Ryujinx.Common.Memory;
 3  using Ryujinx.Graphics.GAL;
 4  using Ryujinx.Graphics.Shader;
 5  using Ryujinx.Graphics.Shader.Translation;
 6  using System.Collections.Generic;
 7  using System.IO;
 8  
 9  namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
10  {
11      static class ShaderBinarySerializer
12      {
13          public static byte[] Pack(ShaderSource[] sources)
14          {
15              using MemoryStream output = MemoryStreamManager.Shared.GetStream();
16  
17              output.Write(sources.Length);
18  
19              foreach (ShaderSource source in sources)
20              {
21                  output.Write((int)source.Stage);
22                  output.Write(source.BinaryCode.Length);
23                  output.Write(source.BinaryCode);
24              }
25  
26              return output.ToArray();
27          }
28  
29          public static ShaderSource[] Unpack(CachedShaderStage[] stages, byte[] code)
30          {
31              using MemoryStream input = new(code);
32              using BinaryReader reader = new(input);
33  
34              List<ShaderSource> output = new();
35  
36              int count = reader.ReadInt32();
37  
38              for (int i = 0; i < count; i++)
39              {
40                  ShaderStage stage = (ShaderStage)reader.ReadInt32();
41                  int binaryCodeLength = reader.ReadInt32();
42                  byte[] binaryCode = reader.ReadBytes(binaryCodeLength);
43  
44                  output.Add(new ShaderSource(binaryCode, stage, TargetLanguage.Spirv));
45              }
46  
47              return output.ToArray();
48          }
49      }
50  }