2018-07-15 22:37:27 +00:00
|
|
|
using System.IO;
|
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine
|
2018-07-15 22:37:27 +00:00
|
|
|
{
|
2019-10-13 06:02:07 +00:00
|
|
|
class ShaderDumper
|
2018-07-15 22:37:27 +00:00
|
|
|
{
|
2019-10-13 06:02:07 +00:00
|
|
|
private const int ShaderHeaderSize = 0x50;
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
private GpuContext _context;
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
private string _runtimeDir;
|
|
|
|
private string _dumpPath;
|
|
|
|
private int _dumpIndex;
|
|
|
|
|
|
|
|
public int CurrentDumpIndex => _dumpIndex;
|
|
|
|
|
|
|
|
public ShaderDumper(GpuContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
|
|
|
|
_dumpIndex = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dump(ulong gpuVa, bool compute)
|
2018-07-15 22:37:27 +00:00
|
|
|
{
|
2019-10-13 06:02:07 +00:00
|
|
|
_dumpPath = GraphicsConfig.ShadersDumpPath;
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(_dumpPath))
|
2018-07-15 22:37:27 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
string fileName = "Shader" + _dumpIndex.ToString("d4") + ".bin";
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string fullPath = Path.Combine(FullDir(), fileName);
|
|
|
|
string codePath = Path.Combine(CodeDir(), fileName);
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
_dumpIndex++;
|
|
|
|
|
|
|
|
ulong headerSize = compute ? 0UL : ShaderHeaderSize;
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
using (FileStream fullFile = File.Create(fullPath))
|
|
|
|
using (FileStream codeFile = File.Create(codePath))
|
2018-07-15 22:37:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
BinaryWriter fullWriter = new BinaryWriter(fullFile);
|
|
|
|
BinaryWriter codeWriter = new BinaryWriter(codeFile);
|
2018-10-17 21:02:23 +00:00
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
for (ulong i = 0; i < headerSize; i += 4)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-10-13 06:02:07 +00:00
|
|
|
fullWriter.Write(_context.MemoryAccessor.ReadInt32(gpuVa + i));
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
ulong offset = 0;
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
ulong instruction = 0;
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
// Dump until a NOP instruction is found.
|
2019-03-04 01:45:25 +00:00
|
|
|
while ((instruction >> 48 & 0xfff8) != 0x50b0)
|
2018-07-15 22:37:27 +00:00
|
|
|
{
|
2019-10-13 06:02:07 +00:00
|
|
|
uint word0 = (uint)_context.MemoryAccessor.ReadInt32(gpuVa + headerSize + offset + 0);
|
|
|
|
uint word1 = (uint)_context.MemoryAccessor.ReadInt32(gpuVa + headerSize + offset + 4);
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
instruction = word0 | (ulong)word1 << 32;
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-07-02 02:39:22 +00:00
|
|
|
// Zero instructions (other kind of NOP) stop immediately,
|
2019-10-13 06:02:07 +00:00
|
|
|
// this is to avoid two rows of zeroes.
|
2019-03-04 01:45:25 +00:00
|
|
|
if (instruction == 0)
|
2018-07-15 22:37:27 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
fullWriter.Write(instruction);
|
|
|
|
codeWriter.Write(instruction);
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
offset += 8;
|
2018-07-15 22:37:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
// Align to meet nvdisasm requirements.
|
2019-03-04 01:45:25 +00:00
|
|
|
while (offset % 0x20 != 0)
|
2018-07-15 22:37:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
fullWriter.Write(0);
|
|
|
|
codeWriter.Write(0);
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
offset += 4;
|
2018-07-15 22:37:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
private string FullDir()
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
|
|
|
return CreateAndReturn(Path.Combine(DumpDir(), "Full"));
|
|
|
|
}
|
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
private string CodeDir()
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
|
|
|
return CreateAndReturn(Path.Combine(DumpDir(), "Code"));
|
|
|
|
}
|
|
|
|
|
2019-10-13 06:02:07 +00:00
|
|
|
private string DumpDir()
|
2018-07-15 22:37:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (string.IsNullOrEmpty(_runtimeDir))
|
2018-07-15 22:37:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
int index = 1;
|
2018-07-15 22:37:27 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2019-10-13 06:02:07 +00:00
|
|
|
_runtimeDir = Path.Combine(_dumpPath, "Dumps" + index.ToString("d2"));
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
index++;
|
2018-07-15 22:37:27 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
while (Directory.Exists(_runtimeDir));
|
2018-07-15 22:37:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
Directory.CreateDirectory(_runtimeDir);
|
2018-07-15 22:37:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return _runtimeDir;
|
2018-07-15 22:37:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static string CreateAndReturn(string dir)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-10-13 06:02:07 +00:00
|
|
|
Directory.CreateDirectory(dir);
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return dir;
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
2018-07-15 22:37:27 +00:00
|
|
|
}
|
|
|
|
}
|