Archived
1
0
Fork 0
forked from Mirror/Ryujinx
This repository has been archived on 2024-10-11. You can view files and clone it, but cannot push or open issues or pull requests.
jinx/Ryujinx.Graphics.Gpu/Engine/ShaderDumper.cs

126 lines
3.4 KiB
C#
Raw Normal View History

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