ShaderDumper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.IO;
  3. namespace Ryujinx.Graphics.Gal
  4. {
  5. static class ShaderDumper
  6. {
  7. private static string RuntimeDir;
  8. private static int DumpIndex = 1;
  9. public static void Dump(IGalMemory Memory, long Position, GalShaderType Type, string ExtSuffix = "")
  10. {
  11. if (string.IsNullOrWhiteSpace(GraphicsConfig.ShadersDumpPath))
  12. {
  13. return;
  14. }
  15. string FileName = "Shader" + DumpIndex.ToString("d4") + "." + ShaderExtension(Type) + ExtSuffix + ".bin";
  16. string FilePath = Path.Combine(DumpDir(), FileName);
  17. DumpIndex++;
  18. using (FileStream Output = File.Create(FilePath))
  19. using (BinaryWriter Writer = new BinaryWriter(Output))
  20. {
  21. long Offset = 0;
  22. ulong Instruction = 0;
  23. //Dump until a NOP instruction is found
  24. while ((Instruction >> 52 & 0xfff8) != 0x50b0)
  25. {
  26. uint Word0 = (uint)Memory.ReadInt32(Position + Offset + 0);
  27. uint Word1 = (uint)Memory.ReadInt32(Position + Offset + 4);
  28. Instruction = Word0 | (ulong)Word1 << 32;
  29. //Zero instructions (other kind of NOP) stop immediatly,
  30. //this is to avoid two rows of zeroes
  31. if (Instruction == 0)
  32. {
  33. break;
  34. }
  35. Writer.Write(Instruction);
  36. Offset += 8;
  37. }
  38. //Align to meet nvdisasm requeriments
  39. while (Offset % 0x20 != 0)
  40. {
  41. Writer.Write(0);
  42. Offset += 4;
  43. }
  44. }
  45. }
  46. private static string DumpDir()
  47. {
  48. if (string.IsNullOrEmpty(RuntimeDir))
  49. {
  50. int Index = 1;
  51. do
  52. {
  53. RuntimeDir = Path.Combine(GraphicsConfig.ShadersDumpPath, "Dumps" + Index.ToString("d2"));
  54. Index++;
  55. }
  56. while (Directory.Exists(RuntimeDir));
  57. Directory.CreateDirectory(RuntimeDir);
  58. }
  59. return RuntimeDir;
  60. }
  61. private static string ShaderExtension(GalShaderType Type)
  62. {
  63. switch (Type)
  64. {
  65. case GalShaderType.Vertex: return "vert";
  66. case GalShaderType.TessControl: return "tesc";
  67. case GalShaderType.TessEvaluation: return "tese";
  68. case GalShaderType.Geometry: return "geom";
  69. case GalShaderType.Fragment: return "frag";
  70. default: throw new ArgumentException(nameof(Type));
  71. }
  72. }
  73. }
  74. }