ShaderDumper.cs 3.9 KB

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