2018-07-16 00:37:27 +02:00
|
|
|
using System.IO;
|
|
|
|
|
2019-11-14 19:26:40 +01:00
|
|
|
namespace Ryujinx.Graphics.Gpu.Shader
|
2018-07-16 00:37:27 +02:00
|
|
|
{
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Shader dumper, writes binary shader code to disk.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
class ShaderDumper
|
2018-07-16 00:37:27 +02:00
|
|
|
{
|
2019-10-13 08:02:07 +02:00
|
|
|
private string _runtimeDir;
|
|
|
|
private string _dumpPath;
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Current index of the shader dump binary file.
|
|
|
|
/// This is incremented after each save, in order to give unique names to the files.
|
|
|
|
/// </summary>
|
|
|
|
public int CurrentDumpIndex { get; private set; }
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the shader dumper.
|
|
|
|
/// </summary>
|
2019-12-31 05:46:57 +01:00
|
|
|
public ShaderDumper()
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
CurrentDumpIndex = 1;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Dumps shader code to disk.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="code">Code to be dumped</param>
|
|
|
|
/// <param name="compute">True for compute shader code, false for graphics shader code</param>
|
2021-08-11 22:27:00 +02:00
|
|
|
/// <returns>Paths where the shader code was dumped</returns>
|
|
|
|
public ShaderDumpPaths Dump(byte[] code, bool compute)
|
2018-07-16 00:37:27 +02:00
|
|
|
{
|
2019-10-13 08:02:07 +02:00
|
|
|
_dumpPath = GraphicsConfig.ShadersDumpPath;
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(_dumpPath))
|
2018-07-16 00:37:27 +02:00
|
|
|
{
|
2021-08-11 22:27:00 +02:00
|
|
|
return default;
|
2018-07-16 00:37:27 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
string fileName = "Shader" + CurrentDumpIndex.ToString("d4") + ".bin";
|
2018-07-16 00:37:27 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
string fullPath = Path.Combine(FullDir(), fileName);
|
|
|
|
string codePath = Path.Combine(CodeDir(), fileName);
|
2018-07-16 00:37:27 +02:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
CurrentDumpIndex++;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
using MemoryStream stream = new MemoryStream(code);
|
|
|
|
BinaryReader codeReader = new BinaryReader(stream);
|
2018-07-16 00:37:27 +02:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
using FileStream fullFile = File.Create(fullPath);
|
|
|
|
using FileStream codeFile = File.Create(codePath);
|
|
|
|
BinaryWriter fullWriter = new BinaryWriter(fullFile);
|
|
|
|
BinaryWriter codeWriter = new BinaryWriter(codeFile);
|
2018-10-17 23:02:23 +02:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
int headerSize = compute ? 0 : 0x50;
|
2018-07-19 07:33:27 +02:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
fullWriter.Write(codeReader.ReadBytes(headerSize));
|
2018-07-16 00:37:27 +02:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
byte[] temp = codeReader.ReadBytes(code.Length - headerSize);
|
2018-07-16 00:37:27 +02:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
fullWriter.Write(temp);
|
|
|
|
codeWriter.Write(temp);
|
2018-07-16 00:37:27 +02:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
// Align to meet nvdisasm requirements.
|
|
|
|
while (codeFile.Length % 0x20 != 0)
|
|
|
|
{
|
|
|
|
codeWriter.Write(0);
|
2018-07-16 00:37:27 +02:00
|
|
|
}
|
2021-08-11 22:27:00 +02:00
|
|
|
|
|
|
|
return new ShaderDumpPaths(fullPath, codePath);
|
2018-07-16 00:37:27 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the output directory for shader code with header.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Directory path</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
private string FullDir()
|
2018-07-19 07:33:27 +02:00
|
|
|
{
|
|
|
|
return CreateAndReturn(Path.Combine(DumpDir(), "Full"));
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the output directory for shader code without header.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Directory path</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
private string CodeDir()
|
2018-07-19 07:33:27 +02:00
|
|
|
{
|
|
|
|
return CreateAndReturn(Path.Combine(DumpDir(), "Code"));
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the full output directory for the current shader dump.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Directory path</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
private string DumpDir()
|
2018-07-16 00:37:27 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
if (string.IsNullOrEmpty(_runtimeDir))
|
2018-07-16 00:37:27 +02:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
int index = 1;
|
2018-07-16 00:37:27 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2019-10-13 08:02:07 +02:00
|
|
|
_runtimeDir = Path.Combine(_dumpPath, "Dumps" + index.ToString("d2"));
|
2018-07-16 00:37:27 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
index++;
|
2018-07-16 00:37:27 +02:00
|
|
|
}
|
2019-03-04 02:45:25 +01:00
|
|
|
while (Directory.Exists(_runtimeDir));
|
2018-07-16 00:37:27 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
Directory.CreateDirectory(_runtimeDir);
|
2018-07-16 00:37:27 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
return _runtimeDir;
|
2018-07-16 00:37:27 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new specified directory if needed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dir">The directory to create</param>
|
|
|
|
/// <returns>The same directory passed to the method</returns>
|
2019-03-04 02:45:25 +01:00
|
|
|
private static string CreateAndReturn(string dir)
|
2018-07-19 07:33:27 +02:00
|
|
|
{
|
2019-10-13 08:02:07 +02:00
|
|
|
Directory.CreateDirectory(dir);
|
2018-07-19 07:33:27 +02:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
return dir;
|
2018-07-19 07:33:27 +02:00
|
|
|
}
|
2018-07-16 00:37:27 +02:00
|
|
|
}
|
|
|
|
}
|