From 48f6570557fc76496936514d94e3ccddf55ec633 Mon Sep 17 00:00:00 2001 From: Mary Date: Fri, 13 Nov 2020 00:15:34 +0100 Subject: 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. --- .../Shader/Cache/Definition/CacheManifestHeader.cs | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Ryujinx.Graphics.Gpu/Shader/Cache/Definition/CacheManifestHeader.cs (limited to 'Ryujinx.Graphics.Gpu/Shader/Cache/Definition/CacheManifestHeader.cs') diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/CacheManifestHeader.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/CacheManifestHeader.cs new file mode 100644 index 00000000..3f198dca --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/CacheManifestHeader.cs @@ -0,0 +1,97 @@ +using System; +using System.Runtime.InteropServices; + +namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition +{ + /// + /// Header of the shader cache manifest. + /// + [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x10)] + struct CacheManifestHeader + { + /// + /// The version of the cache. + /// + public ulong Version; + + /// + /// The graphics api used for this cache. + /// + public CacheGraphicsApi GraphicsApi; + + /// + /// The hash type used for this cache. + /// + public CacheHashType HashType; + + /// + /// CRC-16 checksum over the data in the file. + /// + public ushort TableChecksum; + + /// + /// Construct a new cache manifest header. + /// + /// The version of the cache + /// The graphics api used for this cache + /// The hash type used for this cache + public CacheManifestHeader(ulong version, CacheGraphicsApi graphicsApi, CacheHashType hashType) + { + Version = version; + GraphicsApi = graphicsApi; + HashType = hashType; + TableChecksum = 0; + } + + /// + /// Update the checksum in the header. + /// + /// The data to perform the checksum on + public void UpdateChecksum(ReadOnlySpan data) + { + TableChecksum = CalculateCrc16(data); + } + + /// + /// Calculate a CRC-16 over data. + /// + /// The data to perform the CRC-16 on + /// A CRC-16 over data + private static ushort CalculateCrc16(ReadOnlySpan 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; + } + + /// + /// Check the validity of the header. + /// + /// The target version in use + /// The target graphics api in use + /// The target hash type in use + /// The data after this header + /// True if the header is valid + public bool IsValid(ulong version, CacheGraphicsApi graphicsApi, CacheHashType hashType, ReadOnlySpan data) + { + return Version == version && GraphicsApi == graphicsApi && HashType == hashType && TableChecksum == CalculateCrc16(data); + } + } +} -- cgit v1.2.3