ShaderDumper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Ryujinx.Graphics.Shader.Translation;
  2. using System;
  3. using System.IO;
  4. namespace Ryujinx.Graphics.Gpu.Shader
  5. {
  6. /// <summary>
  7. /// Shader dumper, writes binary shader code to disk.
  8. /// </summary>
  9. class ShaderDumper
  10. {
  11. private string _runtimeDir;
  12. private string _dumpPath;
  13. private int _dumpIndex;
  14. public int CurrentDumpIndex => _dumpIndex;
  15. public ShaderDumper()
  16. {
  17. _dumpIndex = 1;
  18. }
  19. /// <summary>
  20. /// Dumps shader code to disk.
  21. /// </summary>
  22. /// <param name="code">Code to be dumped</param>
  23. /// <param name="compute">True for compute shader code, false for graphics shader code</param>
  24. /// <param name="fullPath">Output path for the shader code with header included</param>
  25. /// <param name="codePath">Output path for the shader code without header</param>
  26. public void Dump(ReadOnlySpan<byte> code, bool compute, out string fullPath, out string codePath)
  27. {
  28. _dumpPath = GraphicsConfig.ShadersDumpPath;
  29. if (string.IsNullOrWhiteSpace(_dumpPath))
  30. {
  31. fullPath = null;
  32. codePath = null;
  33. return;
  34. }
  35. string fileName = "Shader" + _dumpIndex.ToString("d4") + ".bin";
  36. fullPath = Path.Combine(FullDir(), fileName);
  37. codePath = Path.Combine(CodeDir(), fileName);
  38. _dumpIndex++;
  39. code = Translator.ExtractCode(code, compute, out int headerSize);
  40. using (MemoryStream stream = new MemoryStream(code.ToArray()))
  41. {
  42. BinaryReader codeReader = new BinaryReader(stream);
  43. using (FileStream fullFile = File.Create(fullPath))
  44. using (FileStream codeFile = File.Create(codePath))
  45. {
  46. BinaryWriter fullWriter = new BinaryWriter(fullFile);
  47. BinaryWriter codeWriter = new BinaryWriter(codeFile);
  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. }
  58. }
  59. }
  60. /// <summary>
  61. /// Returns the output directory for shader code with header.
  62. /// </summary>
  63. /// <returns>Directory path</returns>
  64. private string FullDir()
  65. {
  66. return CreateAndReturn(Path.Combine(DumpDir(), "Full"));
  67. }
  68. /// <summary>
  69. /// Returns the output directory for shader code without header.
  70. /// </summary>
  71. /// <returns>Directory path</returns>
  72. private string CodeDir()
  73. {
  74. return CreateAndReturn(Path.Combine(DumpDir(), "Code"));
  75. }
  76. /// <summary>
  77. /// Returns the full output directory for the current shader dump.
  78. /// </summary>
  79. /// <returns>Directory path</returns>
  80. private string DumpDir()
  81. {
  82. if (string.IsNullOrEmpty(_runtimeDir))
  83. {
  84. int index = 1;
  85. do
  86. {
  87. _runtimeDir = Path.Combine(_dumpPath, "Dumps" + index.ToString("d2"));
  88. index++;
  89. }
  90. while (Directory.Exists(_runtimeDir));
  91. Directory.CreateDirectory(_runtimeDir);
  92. }
  93. return _runtimeDir;
  94. }
  95. /// <summary>
  96. /// Creates a new specified directory if needed.
  97. /// </summary>
  98. /// <param name="dir">The directory to create</param>
  99. /// <returns>The same directory passed to the method</returns>
  100. private static string CreateAndReturn(string dir)
  101. {
  102. Directory.CreateDirectory(dir);
  103. return dir;
  104. }
  105. }
  106. }