using Ryujinx.Graphics.Shader.Translation; using System; using System.IO; namespace Ryujinx.Graphics.Gpu.Shader { /// /// Shader dumper, writes binary shader code to disk. /// class ShaderDumper { private string _runtimeDir; private string _dumpPath; private int _dumpIndex; public int CurrentDumpIndex => _dumpIndex; public ShaderDumper() { _dumpIndex = 1; } /// /// Dumps shader code to disk. /// /// Code to be dumped /// True for compute shader code, false for graphics shader code /// Output path for the shader code with header included /// Output path for the shader code without header public void Dump(ReadOnlySpan code, bool compute, out string fullPath, out string codePath) { _dumpPath = GraphicsConfig.ShadersDumpPath; if (string.IsNullOrWhiteSpace(_dumpPath)) { fullPath = null; codePath = null; return; } string fileName = "Shader" + _dumpIndex.ToString("d4") + ".bin"; fullPath = Path.Combine(FullDir(), fileName); codePath = Path.Combine(CodeDir(), fileName); _dumpIndex++; code = Translator.ExtractCode(code, compute, out int headerSize); using (MemoryStream stream = new MemoryStream(code.ToArray())) { BinaryReader codeReader = new BinaryReader(stream); using (FileStream fullFile = File.Create(fullPath)) using (FileStream codeFile = File.Create(codePath)) { BinaryWriter fullWriter = new BinaryWriter(fullFile); BinaryWriter codeWriter = new BinaryWriter(codeFile); fullWriter.Write(codeReader.ReadBytes(headerSize)); byte[] temp = codeReader.ReadBytes(code.Length - headerSize); fullWriter.Write(temp); codeWriter.Write(temp); // Align to meet nvdisasm requirements. while (codeFile.Length % 0x20 != 0) { codeWriter.Write(0); } } } } /// /// Returns the output directory for shader code with header. /// /// Directory path private string FullDir() { return CreateAndReturn(Path.Combine(DumpDir(), "Full")); } /// /// Returns the output directory for shader code without header. /// /// Directory path private string CodeDir() { return CreateAndReturn(Path.Combine(DumpDir(), "Code")); } /// /// Returns the full output directory for the current shader dump. /// /// Directory path private string DumpDir() { if (string.IsNullOrEmpty(_runtimeDir)) { int index = 1; do { _runtimeDir = Path.Combine(_dumpPath, "Dumps" + index.ToString("d2")); index++; } while (Directory.Exists(_runtimeDir)); Directory.CreateDirectory(_runtimeDir); } return _runtimeDir; } /// /// Creates a new specified directory if needed. /// /// The directory to create /// The same directory passed to the method private static string CreateAndReturn(string dir) { Directory.CreateDirectory(dir); return dir; } } }