2018-07-15 18:37:27 -04:00
|
|
|
using System.IO;
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine
|
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 const int ShaderHeaderSize = 0x50;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dump(ulong gpuVa, bool compute)
|
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
|
|
|
{
|
|
|
|
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-03-03 20:45:25 -05:00
|
|
|
string fullPath = Path.Combine(FullDir(), fileName);
|
|
|
|
string codePath = Path.Combine(CodeDir(), fileName);
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
_dumpIndex++;
|
|
|
|
|
|
|
|
ulong headerSize = compute ? 0UL : ShaderHeaderSize;
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
using (FileStream fullFile = File.Create(fullPath))
|
|
|
|
using (FileStream codeFile = File.Create(codePath))
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
BinaryWriter fullWriter = new BinaryWriter(fullFile);
|
|
|
|
BinaryWriter codeWriter = new BinaryWriter(codeFile);
|
2018-10-17 17:02:23 -04:00
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
for (ulong i = 0; i < headerSize; i += 4)
|
2018-07-19 01:33:27 -04:00
|
|
|
{
|
2019-10-13 02:02:07 -04:00
|
|
|
fullWriter.Write(_context.MemoryAccessor.ReadInt32(gpuVa + i));
|
2018-07-19 01:33:27 -04:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
ulong offset = 0;
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
ulong instruction = 0;
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
// Dump until a NOP instruction is found.
|
2019-03-03 20:45:25 -05:00
|
|
|
while ((instruction >> 48 & 0xfff8) != 0x50b0)
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
2019-10-13 02:02:07 -04:00
|
|
|
uint word0 = (uint)_context.MemoryAccessor.ReadInt32(gpuVa + headerSize + offset + 0);
|
|
|
|
uint word1 = (uint)_context.MemoryAccessor.ReadInt32(gpuVa + headerSize + offset + 4);
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
instruction = word0 | (ulong)word1 << 32;
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Zero instructions (other kind of NOP) stop immediately,
|
2019-10-13 02:02:07 -04:00
|
|
|
// this is to avoid two rows of zeroes.
|
2019-03-03 20:45:25 -05:00
|
|
|
if (instruction == 0)
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
fullWriter.Write(instruction);
|
|
|
|
codeWriter.Write(instruction);
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
offset += 8;
|
2018-07-15 18:37:27 -04:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
// Align to meet nvdisasm requirements.
|
2019-03-03 20:45:25 -05:00
|
|
|
while (offset % 0x20 != 0)
|
2018-07-15 18:37:27 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
fullWriter.Write(0);
|
|
|
|
codeWriter.Write(0);
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
offset += 4;
|
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
|
|
|
}
|
|
|
|
}
|