ShaderDumper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.IO;
  2. namespace Ryujinx.Graphics.Gpu.Shader
  3. {
  4. /// <summary>
  5. /// Shader dumper, writes binary shader code to disk.
  6. /// </summary>
  7. class ShaderDumper
  8. {
  9. private string _runtimeDir;
  10. private string _dumpPath;
  11. /// <summary>
  12. /// Current index of the shader dump binary file.
  13. /// This is incremented after each save, in order to give unique names to the files.
  14. /// </summary>
  15. public int CurrentDumpIndex { get; private set; }
  16. /// <summary>
  17. /// Creates a new instance of the shader dumper.
  18. /// </summary>
  19. public ShaderDumper()
  20. {
  21. CurrentDumpIndex = 1;
  22. }
  23. /// <summary>
  24. /// Dumps shader code to disk.
  25. /// </summary>
  26. /// <param name="code">Code to be dumped</param>
  27. /// <param name="compute">True for compute shader code, false for graphics shader code</param>
  28. /// <returns>Paths where the shader code was dumped</returns>
  29. public ShaderDumpPaths Dump(byte[] code, bool compute)
  30. {
  31. _dumpPath = GraphicsConfig.ShadersDumpPath;
  32. if (string.IsNullOrWhiteSpace(_dumpPath))
  33. {
  34. return default;
  35. }
  36. string fileName = "Shader" + CurrentDumpIndex.ToString("d4") + ".bin";
  37. string fullPath = Path.Combine(FullDir(), fileName);
  38. string codePath = Path.Combine(CodeDir(), fileName);
  39. CurrentDumpIndex++;
  40. using MemoryStream stream = new MemoryStream(code);
  41. BinaryReader codeReader = new BinaryReader(stream);
  42. using FileStream fullFile = File.Create(fullPath);
  43. using FileStream codeFile = File.Create(codePath);
  44. BinaryWriter fullWriter = new BinaryWriter(fullFile);
  45. BinaryWriter codeWriter = new BinaryWriter(codeFile);
  46. int headerSize = compute ? 0 : 0x50;
  47. fullWriter.Write(codeReader.ReadBytes(headerSize));
  48. byte[] temp = codeReader.ReadBytes(code.Length - headerSize);
  49. fullWriter.Write(temp);
  50. codeWriter.Write(temp);
  51. // Align to meet nvdisasm requirements.
  52. while (codeFile.Length % 0x20 != 0)
  53. {
  54. codeWriter.Write(0);
  55. }
  56. return new ShaderDumpPaths(fullPath, codePath);
  57. }
  58. /// <summary>
  59. /// Returns the output directory for shader code with header.
  60. /// </summary>
  61. /// <returns>Directory path</returns>
  62. private string FullDir()
  63. {
  64. return CreateAndReturn(Path.Combine(DumpDir(), "Full"));
  65. }
  66. /// <summary>
  67. /// Returns the output directory for shader code without header.
  68. /// </summary>
  69. /// <returns>Directory path</returns>
  70. private string CodeDir()
  71. {
  72. return CreateAndReturn(Path.Combine(DumpDir(), "Code"));
  73. }
  74. /// <summary>
  75. /// Returns the full output directory for the current shader dump.
  76. /// </summary>
  77. /// <returns>Directory path</returns>
  78. private string DumpDir()
  79. {
  80. if (string.IsNullOrEmpty(_runtimeDir))
  81. {
  82. int index = 1;
  83. do
  84. {
  85. _runtimeDir = Path.Combine(_dumpPath, "Dumps" + index.ToString("d2"));
  86. index++;
  87. }
  88. while (Directory.Exists(_runtimeDir));
  89. Directory.CreateDirectory(_runtimeDir);
  90. }
  91. return _runtimeDir;
  92. }
  93. /// <summary>
  94. /// Creates a new specified directory if needed.
  95. /// </summary>
  96. /// <param name="dir">The directory to create</param>
  97. /// <returns>The same directory passed to the method</returns>
  98. private static string CreateAndReturn(string dir)
  99. {
  100. Directory.CreateDirectory(dir);
  101. return dir;
  102. }
  103. }
  104. }