2019-11-10 09:03:38 -05:00
|
|
|
using Ryujinx.Graphics.Shader.Translation;
|
|
|
|
using System;
|
2018-07-15 18:37:27 -04:00
|
|
|
using System.IO;
|
|
|
|
|
2019-11-14 13:26:40 -05:00
|
|
|
namespace Ryujinx.Graphics.Gpu.Shader
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
2019-10-13 02:02:07 -04:00
|
|
|
class ShaderDumper
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
2019-10-13 02:02:07 -04:00
|
|
|
private GpuContext _context;
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
private string _runtimeDir;
|
|
|
|
private string _dumpPath;
|
|
|
|
private int _dumpIndex;
|
|
|
|
|
|
|
|
public int CurrentDumpIndex => _dumpIndex;
|
|
|
|
|
|
|
|
public ShaderDumper(GpuContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
|
|
|
|
_dumpIndex = 1;
|
|
|
|
}
|
|
|
|
|
2019-11-10 09:03:38 -05:00
|
|
|
public void Dump(Span<byte> code, bool compute, out string fullPath, out string codePath)
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
2019-10-13 02:02:07 -04:00
|
|
|
_dumpPath = GraphicsConfig.ShadersDumpPath;
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(_dumpPath))
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
2019-11-10 09:03:38 -05:00
|
|
|
fullPath = null;
|
|
|
|
codePath = null;
|
|
|
|
|
2018-07-15 18:37:27 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
string fileName = "Shader" + _dumpIndex.ToString("d4") + ".bin";
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-11-10 09:03:38 -05:00
|
|
|
fullPath = Path.Combine(FullDir(), fileName);
|
|
|
|
codePath = Path.Combine(CodeDir(), fileName);
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
_dumpIndex++;
|
|
|
|
|
2019-11-10 09:03:38 -05:00
|
|
|
code = Translator.ExtractCode(code, compute, out int headerSize);
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-11-10 09:03:38 -05:00
|
|
|
using (MemoryStream stream = new MemoryStream(code.ToArray()))
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
2019-11-10 09:03:38 -05:00
|
|
|
BinaryReader codeReader = new BinaryReader(stream);
|
2018-10-17 17:02:23 -04:00
|
|
|
|
2019-11-10 09:03:38 -05:00
|
|
|
using (FileStream fullFile = File.Create(fullPath))
|
|
|
|
using (FileStream codeFile = File.Create(codePath))
|
2018-07-19 01:33:27 -04:00
|
|
|
{
|
2019-11-10 09:03:38 -05:00
|
|
|
BinaryWriter fullWriter = new BinaryWriter(fullFile);
|
|
|
|
BinaryWriter codeWriter = new BinaryWriter(codeFile);
|
2018-07-19 01:33:27 -04:00
|
|
|
|
2019-11-10 09:03:38 -05:00
|
|
|
fullWriter.Write(codeReader.ReadBytes(headerSize));
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-11-10 09:03:38 -05:00
|
|
|
byte[] temp = codeReader.ReadBytes(code.Length - headerSize);
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-11-10 09:03:38 -05:00
|
|
|
fullWriter.Write(temp);
|
|
|
|
codeWriter.Write(temp);
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-11-10 09:03:38 -05:00
|
|
|
// Align to meet nvdisasm requirements.
|
|
|
|
while (codeFile.Length % 0x20 != 0)
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
2019-11-10 09:03:38 -05:00
|
|
|
codeWriter.Write(0);
|
2018-07-15 18:37:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
private string FullDir()
|
2018-07-19 01:33:27 -04:00
|
|
|
{
|
|
|
|
return CreateAndReturn(Path.Combine(DumpDir(), "Full"));
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
private string CodeDir()
|
2018-07-19 01:33:27 -04:00
|
|
|
{
|
|
|
|
return CreateAndReturn(Path.Combine(DumpDir(), "Code"));
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
private string DumpDir()
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
if (string.IsNullOrEmpty(_runtimeDir))
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
int index = 1;
|
2018-07-15 18:37:27 -04:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2019-10-13 02:02:07 -04:00
|
|
|
_runtimeDir = Path.Combine(_dumpPath, "Dumps" + index.ToString("d2"));
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
index++;
|
2018-07-15 18:37:27 -04:00
|
|
|
}
|
2019-03-03 20:45:25 -05:00
|
|
|
while (Directory.Exists(_runtimeDir));
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
Directory.CreateDirectory(_runtimeDir);
|
2018-07-15 18:37:27 -04:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return _runtimeDir;
|
2018-07-15 18:37:27 -04:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
private static string CreateAndReturn(string dir)
|
2018-07-19 01:33:27 -04:00
|
|
|
{
|
2019-10-13 02:02:07 -04:00
|
|
|
Directory.CreateDirectory(dir);
|
2018-07-19 01:33:27 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return dir;
|
2018-07-19 01:33:27 -04:00
|
|
|
}
|
2018-07-15 18:37:27 -04:00
|
|
|
}
|
|
|
|
}
|