1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-11-14 19:26:41 +00:00
Ryujinx/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/HostShaderCacheEntry.cs
gdkchan 43ebd7a9bb
New shader cache implementation (#3194)
* New shader cache implementation

* Remove some debug code

* Take transform feedback varying count into account

* Create shader cache directory if it does not exist + fragment output map related fixes

* Remove debug code

* Only check texture descriptors if the constant buffer is bound

* Also check CPU VA on GetSpanMapped

* Remove more unused code and move cache related code

* XML docs + remove more unused methods

* Better codegen for TransformFeedbackDescriptor.AsSpan

* Support migration from old cache format, remove more unused code

Shader cache rebuild now also rewrites the shared toc and data files

* Fix migration error with BRX shaders

* Add a limit to the async translation queue

 Avoid async translation threads not being able to keep up and the queue growing very large

* Re-create specialization state on recompile

This might be required if a new version of the shader translator requires more or less state, or if there is a bug related to the GPU state access

* Make shader cache more error resilient

* Add some missing XML docs and move GpuAccessor docs to the interface/use inheritdoc

* Address early PR feedback

* Fix rebase

* Remove IRenderer.CompileShader and IShader interface, replace with new ShaderSource struct passed to CreateProgram directly

* Handle some missing exceptions

* Make shader cache purge delete both old and new shader caches

* Register textures on new specialization state

* Translate and compile shaders in forward order (eliminates diffs due to different binding numbers)

* Limit in-flight shader compilation to the maximum number of compilation threads

* Replace ParallelDiskCacheLoader state changed event with a callback function

* Better handling for invalid constant buffer 1 data length

* Do not create the old cache directory structure if the old cache does not exist

* Constant buffer use should be per-stage. This change will invalidate existing new caches (file format version was incremented)

* Replace rectangle texture with just coordinate normalization

* Skip incompatible shaders that are missing texture information, instead of crashing

This is required if we, for example, support new texture instruction to the shader translator, and then they allow access to textures that were not accessed before. In this scenario, the old cache entry is no longer usable

* Fix coordinates normalization on cubemap textures

* Check if title ID is null before combining shader cache path

* More robust constant buffer address validation on spec state

* More robust constant buffer address validation on spec state (2)

* Regenerate shader cache with one stream, rather than one per shader.

* Only create shader cache directory during initialization

* Logging improvements

* Proper shader program disposal

* PR feedback, and add a comment on serialized structs

* XML docs for RegisterTexture

Co-authored-by: riperiperi <rhy3756547@hotmail.com>
2022-04-10 10:49:44 -03:00

222 lines
8.7 KiB
C#

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();
}
}
}
}