2018-07-15 18:37:27 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal
|
|
|
|
|
{
|
|
|
|
|
static class ShaderDumper
|
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
|
private static string _runtimeDir;
|
2018-07-15 18:37:27 -04:00
|
|
|
|
|
2018-10-17 17:02:23 -04:00
|
|
|
|
public static int DumpIndex { get; private set; } = 1;
|
2018-07-15 18:37:27 -04:00
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
|
public static void Dump(IGalMemory memory, long position, GalShaderType type, string extSuffix = "")
|
2018-07-15 18:37:27 -04:00
|
|
|
|
{
|
2018-10-17 17:02:23 -04:00
|
|
|
|
if (!IsDumpEnabled())
|
2018-07-15 18:37:27 -04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
|
string fileName = "Shader" + DumpIndex.ToString("d4") + "." + ShaderExtension(type) + extSuffix + ".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
|
|
|
|
|
|
|
|
|
DumpIndex++;
|
|
|
|
|
|
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
|
|
|
|
|
2018-07-19 01:33:27 -04:00
|
|
|
|
for (long i = 0; i < 0x50; i += 4)
|
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
|
fullWriter.Write(memory.ReadInt32(position + i));
|
2018-07-19 01:33:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
|
long 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-07-01 22:39:22 -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-03-03 20:45:25 -05:00
|
|
|
|
uint word0 = (uint)memory.ReadInt32(position + 0x50 + offset + 0);
|
|
|
|
|
uint word1 = (uint)memory.ReadInt32(position + 0x50 + 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,
|
|
|
|
|
// 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-07-01 22:39:22 -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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-17 17:02:23 -04:00
|
|
|
|
public static bool IsDumpEnabled()
|
|
|
|
|
{
|
|
|
|
|
return !string.IsNullOrWhiteSpace(GraphicsConfig.ShadersDumpPath);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-19 01:33:27 -04:00
|
|
|
|
private static string FullDir()
|
|
|
|
|
{
|
|
|
|
|
return CreateAndReturn(Path.Combine(DumpDir(), "Full"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string CodeDir()
|
|
|
|
|
{
|
|
|
|
|
return CreateAndReturn(Path.Combine(DumpDir(), "Code"));
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-15 18:37:27 -04:00
|
|
|
|
private static string DumpDir()
|
|
|
|
|
{
|
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-03-03 20:45:25 -05:00
|
|
|
|
_runtimeDir = Path.Combine(GraphicsConfig.ShadersDumpPath, "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-03-03 20:45:25 -05:00
|
|
|
|
if (!Directory.Exists(dir))
|
2018-07-19 01:33:27 -04:00
|
|
|
|
{
|
2019-03-03 20:45:25 -05: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
|
|
|
|
}
|
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
|
private static string ShaderExtension(GalShaderType type)
|
2018-07-15 18:37:27 -04:00
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
|
switch (type)
|
2018-07-15 18:37:27 -04:00
|
|
|
|
{
|
|
|
|
|
case GalShaderType.Vertex: return "vert";
|
|
|
|
|
case GalShaderType.TessControl: return "tesc";
|
|
|
|
|
case GalShaderType.TessEvaluation: return "tese";
|
|
|
|
|
case GalShaderType.Geometry: return "geom";
|
|
|
|
|
case GalShaderType.Fragment: return "frag";
|
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
|
default: throw new ArgumentException(nameof(type));
|
2018-07-15 18:37:27 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|