ShaderBinarySerializer.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
  6. {
  7. static class ShaderBinarySerializer
  8. {
  9. public static byte[] Pack(ShaderSource[] sources)
  10. {
  11. using MemoryStream output = new MemoryStream();
  12. using BinaryWriter writer = new BinaryWriter(output);
  13. for (int i = 0; i < sources.Length; i++)
  14. {
  15. writer.Write(sources[i].BinaryCode.Length);
  16. writer.Write(sources[i].BinaryCode);
  17. }
  18. return output.ToArray();
  19. }
  20. public static ShaderSource[] Unpack(CachedShaderStage[] stages, byte[] code, bool compute)
  21. {
  22. using MemoryStream input = new MemoryStream(code);
  23. using BinaryReader reader = new BinaryReader(input);
  24. List<ShaderSource> output = new List<ShaderSource>();
  25. for (int i = compute ? 0 : 1; i < stages.Length; i++)
  26. {
  27. CachedShaderStage stage = stages[i];
  28. if (stage == null)
  29. {
  30. continue;
  31. }
  32. int binaryCodeLength = reader.ReadInt32();
  33. byte[] binaryCode = reader.ReadBytes(binaryCodeLength);
  34. output.Add(new ShaderSource(binaryCode, ShaderCache.GetBindings(stage.Info), stage.Info.Stage, TargetLanguage.Spirv));
  35. }
  36. return output.ToArray();
  37. }
  38. }
  39. }