aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/HostShaderCacheEntry.cs
blob: fe79acb34cc50021e06abc30a9b22981dc46803a (plain) (blame)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using Ryujinx.Common;
using Ryujinx.Graphics.Shader;
using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
{
    /// <summary>
    /// Host shader entry used for binding information.
    /// </summary>
    class HostShaderCacheEntry
    {
        /// <summary>
        /// The header of the cached shader entry.
        /// </summary>
        public HostShaderCacheEntryHeader Header { get; }

        /// <summary>
        /// Cached constant buffers.
        /// </summary>
        public BufferDescriptor[] CBuffers { get; }

        /// <summary>
        /// Cached storage buffers.
        /// </summary>
        public BufferDescriptor[] SBuffers { get; }

        /// <summary>
        /// Cached texture descriptors.
        /// </summary>
        public TextureDescriptor[] Textures { get; }

        /// <summary>
        /// Cached image descriptors.
        /// </summary>
        public TextureDescriptor[] Images { get; }

        /// <summary>
        /// Create a new instance of <see cref="HostShaderCacheEntry"/>.
        /// </summary>
        /// <param name="header">The header of the cached shader entry</param>
        /// <param name="cBuffers">Cached constant buffers</param>
        /// <param name="sBuffers">Cached storage buffers</param>
        /// <param name="textures">Cached texture descriptors</param>
        /// <param name="images">Cached image descriptors</param>
        private HostShaderCacheEntry(
            HostShaderCacheEntryHeader header,
            BufferDescriptor[] cBuffers,
            BufferDescriptor[] sBuffers,
            TextureDescriptor[] textures,
            TextureDescriptor[] images)
        {
            Header = header;
            CBuffers = cBuffers;
            SBuffers = sBuffers;
            Textures = textures;
            Images = images;
        }

        private HostShaderCacheEntry()
        {
            Header = new HostShaderCacheEntryHeader();
            CBuffers = new BufferDescriptor[0];
            SBuffers = new BufferDescriptor[0];
            Textures = new TextureDescriptor[0];
            Images = new TextureDescriptor[0];
        }

        private HostShaderCacheEntry(ShaderProgramInfo programInfo)
        {
            Header = new HostShaderCacheEntryHeader(programInfo.CBuffers.Count,
                                                    programInfo.SBuffers.Count,
                                                    programInfo.Textures.Count,
                                                    programInfo.Images.Count,
                                                    programInfo.UsesInstanceId,
                                                    programInfo.UsesRtLayer,
                                                    programInfo.ClipDistancesWritten,
                                                    programInfo.FragmentOutputMap);
            CBuffers = programInfo.CBuffers.ToArray();
            SBuffers = programInfo.SBuffers.ToArray();
            Textures = programInfo.Textures.ToArray();
            Images = programInfo.Images.ToArray();
        }

        /// <summary>
        /// Convert the host shader entry to a <see cref="ShaderProgramInfo"/>.
        /// </summary>
        /// <returns>A new <see cref="ShaderProgramInfo"/> from this instance</returns>
        internal ShaderProgramInfo ToShaderProgramInfo()
        {
            return new ShaderProgramInfo(
                CBuffers,
                SBuffers,
                Textures,
                Images,
                default,
                Header.UseFlags.HasFlag(UseFlags.InstanceId),
                Header.UseFlags.HasFlag(UseFlags.RtLayer),
                Header.ClipDistancesWritten,
                Header.FragmentOutputMap);
        }

        /// <summary>
        /// Parse a raw cached user shader program into an array of shader cache entry.
        /// </summary>
        /// <param name="data">The raw cached host shader</param>
        /// <param name="programCode">The host shader program</param>
        /// <returns>An array of shader cache entry</returns>
        internal static HostShaderCacheEntry[] Parse(ReadOnlySpan<byte> data, out ReadOnlySpan<byte> programCode)
        {
            HostShaderCacheHeader fileHeader = MemoryMarshal.Read<HostShaderCacheHeader>(data);

            data = data.Slice(Unsafe.SizeOf<HostShaderCacheHeader>());

            ReadOnlySpan<HostShaderCacheEntryHeader> entryHeaders = MemoryMarshal.Cast<byte, HostShaderCacheEntryHeader>(data.Slice(0, fileHeader.Count * Unsafe.SizeOf<HostShaderCacheEntryHeader>()));

            data = data.Slice(fileHeader.Count * Unsafe.SizeOf<HostShaderCacheEntryHeader>());

            HostShaderCacheEntry[] result = new HostShaderCacheEntry[fileHeader.Count];

            for (int i = 0; i < result.Length; i++)
            {
                HostShaderCacheEntryHeader header = entryHeaders[i];

                if (!header.InUse)
                {
                    continue;
                }

                int cBufferDescriptorsSize = header.CBuffersCount * Unsafe.SizeOf<BufferDescriptor>();
                int sBufferDescriptorsSize = header.SBuffersCount * Unsafe.SizeOf<BufferDescriptor>();
                int textureDescriptorsSize = header.TexturesCount * Unsafe.SizeOf<TextureDescriptor>();
                int imageDescriptorsSize   = header.ImagesCount * Unsafe.SizeOf<TextureDescriptor>();

                ReadOnlySpan<BufferDescriptor> cBuffers = MemoryMarshal.Cast<byte, BufferDescriptor>(data.Slice(0, cBufferDescriptorsSize));
                data = data.Slice(cBufferDescriptorsSize);

                ReadOnlySpan<BufferDescriptor> sBuffers = MemoryMarshal.Cast<byte, BufferDescriptor>(data.Slice(0, sBufferDescriptorsSize));
                data = data.Slice(sBufferDescriptorsSize);

                ReadOnlySpan<TextureDescriptor> textureDescriptors = MemoryMarshal.Cast<byte, TextureDescriptor>(data.Slice(0, textureDescriptorsSize));
                data = data.Slice(textureDescriptorsSize);

                ReadOnlySpan<TextureDescriptor> imageDescriptors = MemoryMarshal.Cast<byte, TextureDescriptor>(data.Slice(0, imageDescriptorsSize));
                data = data.Slice(imageDescriptorsSize);

                result[i] = new HostShaderCacheEntry(header, cBuffers.ToArray(), sBuffers.ToArray(), textureDescriptors.ToArray(), imageDescriptors.ToArray());
            }

            programCode = data.Slice(0, fileHeader.CodeSize);

            return result;
        }

        /// <summary>
        /// Create a new host shader cache file.
        /// </summary>
        /// <param name="programCode">The host shader program</param>
        /// <param name="codeHolders">The shaders code holder</param>
        /// <returns>Raw data of a new host shader cache file</returns>
        internal static byte[] Create(ReadOnlySpan<byte> programCode, CachedShaderStage[] codeHolders)
        {
            HostShaderCacheHeader header = new HostShaderCacheHeader((byte)codeHolders.Length, programCode.Length);

            HostShaderCacheEntry[] entries = new HostShaderCacheEntry[codeHolders.Length];

            for (int i = 0; i < codeHolders.Length; i++)
            {
                if (codeHolders[i] == null)
                {
                    entries[i] = new HostShaderCacheEntry();
                }
                else
                {
                    entries[i] = new HostShaderCacheEntry(codeHolders[i].Info);
                }
            }

            using (MemoryStream stream = new MemoryStream())
            {
                BinaryWriter writer = new BinaryWriter(stream);

                writer.WriteStruct(header);

                foreach (HostShaderCacheEntry entry in entries)
                {
                    writer.WriteStruct(entry.Header);
                }

                foreach (HostShaderCacheEntry entry in entries)
                {
                    foreach (BufferDescriptor cBuffer in entry.CBuffers)
                    {
                        writer.WriteStruct(cBuffer);
                    }

                    foreach (BufferDescriptor sBuffer in entry.SBuffers)
                    {
                        writer.WriteStruct(sBuffer);
                    }

                    foreach (TextureDescriptor texture in entry.Textures)
                    {
                        writer.WriteStruct(texture);
                    }

                    foreach (TextureDescriptor image in entry.Images)
                    {
                        writer.WriteStruct(image);
                    }
                }

                writer.Write(programCode);

                return stream.ToArray();
            }
        }
    }
}