Salieri: shader cache (#1701)
Here come Salieri, my implementation of a disk shader cache! "I'm sure you know why I named it that." "It doesn't really mean anything." This implementation collects shaders at runtime and cache them to be later compiled when starting a game.
This commit is contained in:
parent
7166e82c3c
commit
48f6570557
57 changed files with 3589 additions and 396 deletions
|
@ -0,0 +1,38 @@
|
|||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Graphics API type accepted by the shader cache.
|
||||
/// </summary>
|
||||
enum CacheGraphicsApi : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// OpenGL Core
|
||||
/// </summary>
|
||||
OpenGL,
|
||||
|
||||
/// <summary>
|
||||
/// OpenGL ES
|
||||
/// </summary>
|
||||
OpenGLES,
|
||||
|
||||
/// <summary>
|
||||
/// Vulkan
|
||||
/// </summary>
|
||||
Vulkan,
|
||||
|
||||
/// <summary>
|
||||
/// DirectX
|
||||
/// </summary>
|
||||
DirectX,
|
||||
|
||||
/// <summary>
|
||||
/// Metal
|
||||
/// </summary>
|
||||
Metal,
|
||||
|
||||
/// <summary>
|
||||
/// Guest, used to cache games raw shader programs.
|
||||
/// </summary>
|
||||
Guest
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Hash algorithm accepted by the shader cache.
|
||||
/// </summary>
|
||||
enum CacheHashType : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// xxHash128
|
||||
/// </summary>
|
||||
XxHash128
|
||||
}
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Header of the shader cache manifest.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x10)]
|
||||
struct CacheManifestHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The version of the cache.
|
||||
/// </summary>
|
||||
public ulong Version;
|
||||
|
||||
/// <summary>
|
||||
/// The graphics api used for this cache.
|
||||
/// </summary>
|
||||
public CacheGraphicsApi GraphicsApi;
|
||||
|
||||
/// <summary>
|
||||
/// The hash type used for this cache.
|
||||
/// </summary>
|
||||
public CacheHashType HashType;
|
||||
|
||||
/// <summary>
|
||||
/// CRC-16 checksum over the data in the file.
|
||||
/// </summary>
|
||||
public ushort TableChecksum;
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new cache manifest header.
|
||||
/// </summary>
|
||||
/// <param name="version">The version of the cache</param>
|
||||
/// <param name="graphicsApi">The graphics api used for this cache</param>
|
||||
/// <param name="hashType">The hash type used for this cache</param>
|
||||
public CacheManifestHeader(ulong version, CacheGraphicsApi graphicsApi, CacheHashType hashType)
|
||||
{
|
||||
Version = version;
|
||||
GraphicsApi = graphicsApi;
|
||||
HashType = hashType;
|
||||
TableChecksum = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the checksum in the header.
|
||||
/// </summary>
|
||||
/// <param name="data">The data to perform the checksum on</param>
|
||||
public void UpdateChecksum(ReadOnlySpan<byte> data)
|
||||
{
|
||||
TableChecksum = CalculateCrc16(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate a CRC-16 over data.
|
||||
/// </summary>
|
||||
/// <param name="data">The data to perform the CRC-16 on</param>
|
||||
/// <returns>A CRC-16 over data</returns>
|
||||
private static ushort CalculateCrc16(ReadOnlySpan<byte> data)
|
||||
{
|
||||
int crc = 0;
|
||||
|
||||
const ushort poly = 0x1021;
|
||||
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
crc ^= data[i] << 8;
|
||||
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
crc <<= 1;
|
||||
|
||||
if ((crc & 0x10000) != 0)
|
||||
{
|
||||
crc = (crc ^ poly) & 0xFFFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (ushort)crc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check the validity of the header.
|
||||
/// </summary>
|
||||
/// <param name="version">The target version in use</param>
|
||||
/// <param name="graphicsApi">The target graphics api in use</param>
|
||||
/// <param name="hashType">The target hash type in use</param>
|
||||
/// <param name="data">The data after this header</param>
|
||||
/// <returns>True if the header is valid</returns>
|
||||
public bool IsValid(ulong version, CacheGraphicsApi graphicsApi, CacheHashType hashType, ReadOnlySpan<byte> data)
|
||||
{
|
||||
return Version == version && GraphicsApi == graphicsApi && HashType == hashType && TableChecksum == CalculateCrc16(data);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
using Ryujinx.Graphics.Shader;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Header of a cached guest gpu accessor.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = 1)]
|
||||
struct GuestGpuAccessorHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The count of texture descriptors.
|
||||
/// </summary>
|
||||
public int TextureDescriptorCount;
|
||||
|
||||
/// <summary>
|
||||
/// Local Size X for compute shaders.
|
||||
/// </summary>
|
||||
public int ComputeLocalSizeX;
|
||||
|
||||
/// <summary>
|
||||
/// Local Size Y for compute shaders.
|
||||
/// </summary>
|
||||
public int ComputeLocalSizeY;
|
||||
|
||||
/// <summary>
|
||||
/// Local Size Z for compute shaders.
|
||||
/// </summary>
|
||||
public int ComputeLocalSizeZ;
|
||||
|
||||
/// <summary>
|
||||
/// Local Memory size in bytes for compute shaders.
|
||||
/// </summary>
|
||||
public int ComputeLocalMemorySize;
|
||||
|
||||
/// <summary>
|
||||
/// Shared Memory size in bytes for compute shaders.
|
||||
/// </summary>
|
||||
public int ComputeSharedMemorySize;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public int Reserved1;
|
||||
|
||||
/// <summary>
|
||||
/// Current primitive topology for geometry shaders.
|
||||
/// </summary>
|
||||
public InputTopology PrimitiveTopology;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public ushort Reserved2;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public byte Reserved3;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Represent a cached shader entry in a guest shader program.
|
||||
/// </summary>
|
||||
class GuestShaderCacheEntry
|
||||
{
|
||||
/// <summary>
|
||||
/// The header of the cached shader entry.
|
||||
/// </summary>
|
||||
public GuestShaderCacheEntryHeader Header { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The code of this shader.
|
||||
/// </summary>
|
||||
/// <remarks>If a Vertex A is present, this also contains the code 2 section.</remarks>
|
||||
public byte[] Code { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The textures descriptors used for this shader.
|
||||
/// </summary>
|
||||
public Dictionary<int, GuestTextureDescriptor> TextureDescriptors { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of <see cref="GuestShaderCacheEntry"/>.
|
||||
/// </summary>
|
||||
/// <param name="header">The header of the cached shader entry</param>
|
||||
/// <param name="code">The code of this shader</param>
|
||||
private GuestShaderCacheEntry(GuestShaderCacheEntryHeader header, byte[] code)
|
||||
{
|
||||
Header = header;
|
||||
Code = code;
|
||||
TextureDescriptors = new Dictionary<int, GuestTextureDescriptor>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse a raw cached user shader program into an array of shader cache entry.
|
||||
/// </summary>
|
||||
/// <param name="data">The raw cached user shader program</param>
|
||||
/// <param name="fileHeader">The user shader program header</param>
|
||||
/// <returns>An array of shader cache entry</returns>
|
||||
public static GuestShaderCacheEntry[] Parse(ref ReadOnlySpan<byte> data, out GuestShaderCacheHeader fileHeader)
|
||||
{
|
||||
fileHeader = MemoryMarshal.Read<GuestShaderCacheHeader>(data);
|
||||
|
||||
data = data.Slice(Unsafe.SizeOf<GuestShaderCacheHeader>());
|
||||
|
||||
ReadOnlySpan<GuestShaderCacheEntryHeader> entryHeaders = MemoryMarshal.Cast<byte, GuestShaderCacheEntryHeader>(data.Slice(0, fileHeader.Count * Unsafe.SizeOf<GuestShaderCacheEntryHeader>()));
|
||||
|
||||
data = data.Slice(fileHeader.Count * Unsafe.SizeOf<GuestShaderCacheEntryHeader>());
|
||||
|
||||
GuestShaderCacheEntry[] result = new GuestShaderCacheEntry[fileHeader.Count];
|
||||
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
{
|
||||
GuestShaderCacheEntryHeader header = entryHeaders[i];
|
||||
|
||||
// Ignore empty entries
|
||||
if (header.Size == 0 && header.SizeA == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
byte[] code = data.Slice(0, header.Size + header.SizeA).ToArray();
|
||||
|
||||
data = data.Slice(header.Size + header.SizeA);
|
||||
|
||||
result[i] = new GuestShaderCacheEntry(header, code);
|
||||
|
||||
ReadOnlySpan<GuestTextureDescriptor> textureDescriptors = MemoryMarshal.Cast<byte, GuestTextureDescriptor>(data.Slice(0, header.GpuAccessorHeader.TextureDescriptorCount * Unsafe.SizeOf<GuestTextureDescriptor>()));
|
||||
|
||||
foreach (GuestTextureDescriptor textureDescriptor in textureDescriptors)
|
||||
{
|
||||
result[i].TextureDescriptors.Add((int)textureDescriptor.Handle, textureDescriptor);
|
||||
}
|
||||
|
||||
data = data.Slice(header.GpuAccessorHeader.TextureDescriptorCount * Unsafe.SizeOf<GuestTextureDescriptor>());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
using Ryujinx.Graphics.Shader;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// The header of a guest shader entry in a guest shader program.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0x1, Size = 0x30)]
|
||||
struct GuestShaderCacheEntryHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The stage of this shader.
|
||||
/// </summary>
|
||||
public ShaderStage Stage;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public byte Reserved1;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public byte Reserved2;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public byte Reserved3;
|
||||
|
||||
/// <summary>
|
||||
/// The size of the code section.
|
||||
/// </summary>
|
||||
public int Size;
|
||||
|
||||
/// <summary>
|
||||
/// The size of the code2 section if present. (Vertex A)
|
||||
/// </summary>
|
||||
public int SizeA;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public int Reserved4;
|
||||
|
||||
/// <summary>
|
||||
/// The header of the cached gpu accessor.
|
||||
/// </summary>
|
||||
public GuestGpuAccessorHeader GpuAccessorHeader;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new guest shader entry header.
|
||||
/// </summary>
|
||||
/// <param name="stage">The stage of this shader</param>
|
||||
/// <param name="size">The size of the code section</param>
|
||||
/// <param name="sizeA">The size of the code2 section if present (Vertex A)</param>
|
||||
/// <param name="gpuAccessorHeader">The header of the cached gpu accessor</param>
|
||||
public GuestShaderCacheEntryHeader(ShaderStage stage, int size, int sizeA, GuestGpuAccessorHeader gpuAccessorHeader) : this()
|
||||
{
|
||||
Stage = stage;
|
||||
Size = size;
|
||||
SizeA = sizeA;
|
||||
GpuAccessorHeader = gpuAccessorHeader;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// The header of a shader program in the guest cache.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0x1, Size = 0x10)]
|
||||
struct GuestShaderCacheHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The count of shaders defining this program.
|
||||
/// </summary>
|
||||
public byte Count;
|
||||
|
||||
/// <summary>
|
||||
/// The count of transform feedback data used in this program.
|
||||
/// </summary>
|
||||
public byte TransformFeedbackCount;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public ushort Reserved1;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public ulong Reserved2;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new guest shader cache header.
|
||||
/// </summary>
|
||||
/// <param name="count">The count of shaders defining this program</param>
|
||||
/// <param name="transformFeedbackCount">The count of transform feedback data used in this program</param>
|
||||
public GuestShaderCacheHeader(byte count, byte transformFeedbackCount) : this()
|
||||
{
|
||||
Count = count;
|
||||
TransformFeedbackCount = transformFeedbackCount;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Header for transform feedback.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x10)]
|
||||
struct GuestShaderCacheTransformFeedbackHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The buffer index of the transform feedback.
|
||||
/// </summary>
|
||||
public int BufferIndex;
|
||||
|
||||
/// <summary>
|
||||
/// The stride of the transform feedback.
|
||||
/// </summary>
|
||||
public int Stride;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the varying location buffer of the transform feedback.
|
||||
/// </summary>
|
||||
public int VaryingLocationsLength;
|
||||
|
||||
/// <summary>
|
||||
/// Reserved/unused.
|
||||
/// </summary>
|
||||
public int Reserved1;
|
||||
|
||||
public GuestShaderCacheTransformFeedbackHeader(int bufferIndex, int stride, int varyingLocationsLength) : this()
|
||||
{
|
||||
BufferIndex = bufferIndex;
|
||||
Stride = stride;
|
||||
VaryingLocationsLength = varyingLocationsLength;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
using Ryujinx.Graphics.Gpu.Image;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Mostly identical to TextureDescriptor from <see cref="Image"/> but we don't store the address of the texture and store its handle instead.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = 1)]
|
||||
struct GuestTextureDescriptor
|
||||
{
|
||||
public uint Handle;
|
||||
internal TextureDescriptor Descriptor;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,210 @@
|
|||
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);
|
||||
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, Header.UsesInstanceId);
|
||||
}
|
||||
|
||||
/// <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, ShaderCodeHolder[] 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using Ryujinx.Graphics.Shader;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Host shader entry header used for binding information.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x14)]
|
||||
struct HostShaderCacheEntryHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// Count of constant buffer descriptors.
|
||||
/// </summary>
|
||||
public int CBuffersCount;
|
||||
|
||||
/// <summary>
|
||||
/// Count of storage buffer descriptors.
|
||||
/// </summary>
|
||||
public int SBuffersCount;
|
||||
|
||||
/// <summary>
|
||||
/// Count of texture descriptors.
|
||||
/// </summary>
|
||||
public int TexturesCount;
|
||||
|
||||
/// <summary>
|
||||
/// Count of image descriptors.
|
||||
/// </summary>
|
||||
public int ImagesCount;
|
||||
|
||||
/// <summary>
|
||||
/// Set to true if the shader uses instance id.
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool UsesInstanceId;
|
||||
|
||||
/// <summary>
|
||||
/// Set to true if this entry is in use.
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool InUse;
|
||||
|
||||
/// <summary>
|
||||
/// Reserved / unused.
|
||||
/// </summary>
|
||||
public short Reserved;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new host shader cache entry header.
|
||||
/// </summary>
|
||||
/// <param name="cBuffersCount">Count of constant buffer descriptors</param>
|
||||
/// <param name="sBuffersCount">Count of storage buffer descriptors</param>
|
||||
/// <param name="texturesCount">Count of texture descriptors</param>
|
||||
/// <param name="imagesCount">Count of image descriptors</param>
|
||||
/// <param name="usesInstanceId">Set to true if the shader uses instance id</param>
|
||||
public HostShaderCacheEntryHeader(int cBuffersCount, int sBuffersCount, int texturesCount, int imagesCount, bool usesInstanceId) : this()
|
||||
{
|
||||
CBuffersCount = cBuffersCount;
|
||||
SBuffersCount = sBuffersCount;
|
||||
TexturesCount = texturesCount;
|
||||
ImagesCount = imagesCount;
|
||||
UsesInstanceId = usesInstanceId;
|
||||
InUse = true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// The header of a shader program in the guest cache.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0x1, Size = 0x10)]
|
||||
struct HostShaderCacheHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The count of shaders defining this program.
|
||||
/// </summary>
|
||||
public byte Count;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public byte Reserved1;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public ushort Reserved2;
|
||||
|
||||
/// <summary>
|
||||
/// Size of the shader binary.
|
||||
/// </summary>
|
||||
public int CodeSize;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new host shader cache header.
|
||||
/// </summary>
|
||||
/// <param name="count">The count of shaders defining this program</param>
|
||||
/// <param name="codeSize">The size of the shader binary</param>
|
||||
public HostShaderCacheHeader(byte count, int codeSize) : this()
|
||||
{
|
||||
Count = count;
|
||||
CodeSize = codeSize;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue