aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Gpu/Shader/ShaderDumper.cs
blob: 0e22b07e3ac5d193323f034771bb68e3e8150ac1 (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
127
128
129
130
131
using Ryujinx.Graphics.Shader.Translation;
using System;
using System.IO;

namespace Ryujinx.Graphics.Gpu.Shader
{
    /// <summary>
    /// Shader dumper, writes binary shader code to disk.
    /// </summary>
    class ShaderDumper
    {
        private string _runtimeDir;
        private string _dumpPath;
        private int    _dumpIndex;

        public int CurrentDumpIndex => _dumpIndex;

        public ShaderDumper()
        {
            _dumpIndex = 1;
        }

        /// <summary>
        /// Dumps shader code to disk.
        /// </summary>
        /// <param name="code">Code to be dumped</param>
        /// <param name="compute">True for compute shader code, false for graphics shader code</param>
        /// <param name="fullPath">Output path for the shader code with header included</param>
        /// <param name="codePath">Output path for the shader code without header</param>
        public void Dump(ReadOnlySpan<byte> code, bool compute, out string fullPath, out string codePath)
        {
            _dumpPath = GraphicsConfig.ShadersDumpPath;

            if (string.IsNullOrWhiteSpace(_dumpPath))
            {
                fullPath = null;
                codePath = null;

                return;
            }

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

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

            _dumpIndex++;

            code = Translator.ExtractCode(code, compute, out int headerSize);

            using (MemoryStream stream = new MemoryStream(code.ToArray()))
            {
                BinaryReader codeReader = new BinaryReader(stream);

                using (FileStream fullFile = File.Create(fullPath))
                using (FileStream codeFile = File.Create(codePath))
                {
                    BinaryWriter fullWriter = new BinaryWriter(fullFile);
                    BinaryWriter codeWriter = new BinaryWriter(codeFile);

                    fullWriter.Write(codeReader.ReadBytes(headerSize));

                    byte[] temp = codeReader.ReadBytes(code.Length - headerSize);

                    fullWriter.Write(temp);
                    codeWriter.Write(temp);

                    // Align to meet nvdisasm requirements.
                    while (codeFile.Length % 0x20 != 0)
                    {
                        codeWriter.Write(0);
                    }
                }
            }
        }

        /// <summary>
        /// Returns the output directory for shader code with header.
        /// </summary>
        /// <returns>Directory path</returns>
        private string FullDir()
        {
            return CreateAndReturn(Path.Combine(DumpDir(), "Full"));
        }

        /// <summary>
        /// Returns the output directory for shader code without header.
        /// </summary>
        /// <returns>Directory path</returns>
        private string CodeDir()
        {
            return CreateAndReturn(Path.Combine(DumpDir(), "Code"));
        }

        /// <summary>
        /// Returns the full output directory for the current shader dump.
        /// </summary>
        /// <returns>Directory path</returns>
        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;
        }

        /// <summary>
        /// Creates a new specified directory if needed.
        /// </summary>
        /// <param name="dir">The directory to create</param>
        /// <returns>The same directory passed to the method</returns>
        private static string CreateAndReturn(string dir)
        {
            Directory.CreateDirectory(dir);

            return dir;
        }
    }
}