aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Gpu/Engine/ShaderDumper.cs
blob: fdcf0612187a2e668d008e19b8a015525f37907f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System.IO;

namespace Ryujinx.Graphics.Gpu.Engine
{
    class ShaderDumper
    {
        private const int ShaderHeaderSize = 0x50;

        private GpuContext _context;

        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)
        {
            _dumpPath = GraphicsConfig.ShadersDumpPath;

            if (string.IsNullOrWhiteSpace(_dumpPath))
            {
                return;
            }

            string fileName = "Shader" + _dumpIndex.ToString("d4") + ".bin";

            string fullPath = Path.Combine(FullDir(), fileName);
            string codePath = Path.Combine(CodeDir(), fileName);

            _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);

                for (ulong i = 0; i < headerSize; i += 4)
                {
                    fullWriter.Write(_context.MemoryAccessor.ReadInt32(gpuVa + i));
                }

                ulong offset = 0;

                ulong instruction = 0;

                // Dump until a NOP instruction is found.
                while ((instruction >> 48 & 0xfff8) != 0x50b0)
                {
                    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,
                    // this is to avoid two rows of zeroes.
                    if (instruction == 0)
                    {
                        break;
                    }

                    fullWriter.Write(instruction);
                    codeWriter.Write(instruction);

                    offset += 8;
                }

                // Align to meet nvdisasm requirements.
                while (offset % 0x20 != 0)
                {
                    fullWriter.Write(0);
                    codeWriter.Write(0);

                    offset += 4;
                }
            }
        }

        private string FullDir()
        {
            return CreateAndReturn(Path.Combine(DumpDir(), "Full"));
        }

        private string CodeDir()
        {
            return CreateAndReturn(Path.Combine(DumpDir(), "Code"));
        }

        private string DumpDir()
        {
            if (string.IsNullOrEmpty(_runtimeDir))
            {
                int index = 1;

                do
                {
                    _runtimeDir = Path.Combine(_dumpPath, "Dumps" + index.ToString("d2"));

                    index++;
                }
                while (Directory.Exists(_runtimeDir));

                Directory.CreateDirectory(_runtimeDir);
            }

            return _runtimeDir;
        }

        private static string CreateAndReturn(string dir)
        {
            Directory.CreateDirectory(dir);

            return dir;
        }
    }
}