diff options
| author | Mary <me@thog.eu> | 2020-11-13 00:15:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-13 00:15:34 +0100 |
| commit | 48f6570557fc76496936514d94e3ccddf55ec633 (patch) | |
| tree | ce455833899cb33a312e5853a7a3d191bb5d18d9 /Ryujinx.Graphics.Gpu/Shader/Cache | |
| parent | 7166e82c3cf1fd8cf2fce3281017ee88122684d8 (diff) | |
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.
Diffstat (limited to 'Ryujinx.Graphics.Gpu/Shader/Cache')
14 files changed, 1542 insertions, 0 deletions
diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs new file mode 100644 index 00000000..effd893a --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs @@ -0,0 +1,595 @@ +using Ryujinx.Common; +using Ryujinx.Common.Logging; +using Ryujinx.Graphics.Gpu.Shader.Cache.Definition; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Threading; + +namespace Ryujinx.Graphics.Gpu.Shader.Cache +{ + /// <summary> + /// Represent a cache collection handling one shader cache. + /// </summary> + class CacheCollection : IDisposable + { + /// <summary> + /// Possible operation to do on the <see cref="_fileWriterWorkerQueue"/>. + /// </summary> + private enum CacheFileOperation + { + /// <summary> + /// Save a new entry in the temp cache. + /// </summary> + SaveTempEntry, + + /// <summary> + /// Save the hash manifest. + /// </summary> + SaveManifest, + + /// <summary> + /// Flush temporary cache to archive. + /// </summary> + FlushToArchive, + + /// <summary> + /// Signal when hitting this point. This is useful to know if all previous operations were performed. + /// </summary> + Synchronize + } + + /// <summary> + /// Represent an operation to perform on the <see cref="_fileWriterWorkerQueue"/>. + /// </summary> + private class CacheFileOperationTask + { + /// <summary> + /// The type of operation to perform. + /// </summary> + public CacheFileOperation Type; + + /// <summary> + /// The data associated to this operation or null. + /// </summary> + public object Data; + } + + /// <summary> + /// Data associated to the <see cref="CacheFileOperation.SaveTempEntry"/> operation. + /// </summary> + private class CacheFileSaveEntryTaskData + { + /// <summary> + /// The key of the entry to cache. + /// </summary> + public Hash128 Key; + + /// <summary> + /// The value of the entry to cache. + /// </summary> + public byte[] Value; + } + + /// <summary> + /// The directory of the shader cache. + /// </summary> + private readonly string _cacheDirectory; + + /// <summary> + /// The version of the cache. + /// </summary> + private readonly ulong _version; + + /// <summary> + /// The hash type of the cache. + /// </summary> + private readonly CacheHashType _hashType; + + /// <summary> + /// The graphics API of the cache. + /// </summary> + private readonly CacheGraphicsApi _graphicsApi; + + /// <summary> + /// The table of all the hash registered in the cache. + /// </summary> + private HashSet<Hash128> _hashTable; + + /// <summary> + /// The queue of operations to be performed by the file writer worker. + /// </summary> + private AsyncWorkQueue<CacheFileOperationTask> _fileWriterWorkerQueue; + + /// <summary> + /// Main storage of the cache collection. + /// </summary> + private ZipArchive _cacheArchive; + + /// <summary> + /// Immutable copy of the hash table. + /// </summary> + public ReadOnlySpan<Hash128> HashTable => _hashTable.ToArray(); + + /// <summary> + /// Get the temp path to the cache data directory. + /// </summary> + /// <returns>The temp path to the cache data directory</returns> + private string GetCacheTempDataPath() => Path.Combine(_cacheDirectory, "temp"); + + /// <summary> + /// The path to the cache archive file. + /// </summary> + /// <returns>The path to the cache archive file</returns> + private string GetArchivePath() => Path.Combine(_cacheDirectory, "cache.zip"); + + /// <summary> + /// The path to the cache manifest file. + /// </summary> + /// <returns>The path to the cache manifest file</returns> + private string GetManifestPath() => Path.Combine(_cacheDirectory, "cache.info"); + + /// <summary> + /// Create a new temp path to the given cached file via its hash. + /// </summary> + /// <param name="key">The hash of the cached data</param> + /// <returns>New path to the given cached file</returns> + private string GenCacheTempFilePath(Hash128 key) => Path.Combine(GetCacheTempDataPath(), key.ToString()); + + /// <summary> + /// Create a new cache collection. + /// </summary> + /// <param name="baseCacheDirectory">The directory of the shader cache</param> + /// <param name="hashType">The hash type of the shader cache</param> + /// <param name="graphicsApi">The graphics api of the shader cache</param> + /// <param name="shaderProvider">The shader provider name of the shader cache</param> + /// <param name="cacheName">The name of the cache</param> + /// <param name="version">The version of the cache</param> + public CacheCollection(string baseCacheDirectory, CacheHashType hashType, CacheGraphicsApi graphicsApi, string shaderProvider, string cacheName, ulong version) + { + if (hashType != CacheHashType.XxHash128) + { + throw new NotImplementedException($"{hashType}"); + } + + _cacheDirectory = GenerateCachePath(baseCacheDirectory, graphicsApi, shaderProvider, cacheName); + _graphicsApi = graphicsApi; + _hashType = hashType; + _version = version; + _hashTable = new HashSet<Hash128>(); + + Load(); + + _fileWriterWorkerQueue = new AsyncWorkQueue<CacheFileOperationTask>(HandleCacheTask, $"CacheCollection.Worker.{cacheName}"); + } + + /// <summary> + /// Load the cache manifest file and recreate it if invalid. + /// </summary> + private void Load() + { + bool isInvalid = false; + + if (!Directory.Exists(_cacheDirectory)) + { + isInvalid = true; + } + else + { + string manifestPath = GetManifestPath(); + + if (File.Exists(manifestPath)) + { + Memory<byte> rawManifest = File.ReadAllBytes(manifestPath); + + if (MemoryMarshal.TryRead(rawManifest.Span, out CacheManifestHeader manifestHeader)) + { + Memory<byte> hashTableRaw = rawManifest.Slice(Unsafe.SizeOf<CacheManifestHeader>()); + + isInvalid = !manifestHeader.IsValid(_version, _graphicsApi, _hashType, hashTableRaw.Span); + + if (!isInvalid) + { + ReadOnlySpan<Hash128> hashTable = MemoryMarshal.Cast<byte, Hash128>(hashTableRaw.Span); + + foreach (Hash128 hash in hashTable) + { + _hashTable.Add(hash); + } + } + } + } + else + { + isInvalid = true; + } + } + + if (isInvalid) + { + Logger.Warning?.Print(LogClass.Gpu, $"Shader collection \"{_cacheDirectory}\" got invalidated, cache will need to be rebuilt."); + + if (Directory.Exists(_cacheDirectory)) + { + Directory.Delete(_cacheDirectory, true); + } + + Directory.CreateDirectory(_cacheDirectory); + + SaveManifest(); + } + + FlushToArchive(); + } + + /// <summary> + /// Remove given entries from the manifest. + /// </summary> + /// <param name="entries">Entries to remove from the manifest</param> + public void RemoveManifestEntries(HashSet<Hash128> entries) + { + lock (_hashTable) + { + foreach (Hash128 entry in entries) + { + _hashTable.Remove(entry); + } + + SaveManifest(); + } + } + + /// <summary> + /// Queue a task to flush temporary files to the archive on the worker. + /// </summary> + public void FlushToArchiveAsync() + { + _fileWriterWorkerQueue.Add(new CacheFileOperationTask + { + Type = CacheFileOperation.FlushToArchive + }); + } + + /// <summary> + /// Wait for all tasks before this given point to be done. + /// </summary> + public void Synchronize() + { + using (ManualResetEvent evnt = new ManualResetEvent(false)) + { + _fileWriterWorkerQueue.Add(new CacheFileOperationTask + { + Type = CacheFileOperation.Synchronize, + Data = evnt + }); + + evnt.WaitOne(); + } + } + + /// <summary> + /// Flush temporary files to the archive. + /// </summary> + /// <remarks>This dispose <see cref="_cacheArchive"/> if not null and reinstantiate it.</remarks> + private void FlushToArchive() + { + EnsureArchiveUpToDate(); + + // Open the zip in readonly to avoid anyone modifying/corrupting it during normal operations. + _cacheArchive = ZipFile.Open(GetArchivePath(), ZipArchiveMode.Read); + } + + /// <summary> + /// Save temporary files not in archive. + /// </summary> + /// <remarks>This dispose <see cref="_cacheArchive"/> if not null.</remarks> + public void EnsureArchiveUpToDate() + { + // First close previous opened instance if found. + if (_cacheArchive != null) + { + _cacheArchive.Dispose(); + } + + string archivePath = GetArchivePath(); + + // Open the zip in read/write. + _cacheArchive = ZipFile.Open(archivePath, ZipArchiveMode.Update); + + Logger.Info?.Print(LogClass.Gpu, $"Updating cache collection archive {archivePath}..."); + + // Update the content of the zip. + lock (_hashTable) + { + foreach (Hash128 hash in _hashTable) + { + string cacheTempFilePath = GenCacheTempFilePath(hash); + + if (File.Exists(cacheTempFilePath)) + { + string cacheHash = $"{hash}"; + + ZipArchiveEntry entry = _cacheArchive.GetEntry(cacheHash); + + entry?.Delete(); + + _cacheArchive.CreateEntryFromFile(cacheTempFilePath, cacheHash); + File.Delete(cacheTempFilePath); + } + } + + // Close the instance to force a flush. + _cacheArchive.Dispose(); + _cacheArchive = null; + + string cacheTempDataPath = GetCacheTempDataPath(); + + // Create the cache data path if missing. + if (!Directory.Exists(cacheTempDataPath)) + { + Directory.CreateDirectory(cacheTempDataPath); + } + } + + Logger.Info?.Print(LogClass.Gpu, $"Updated cache collection archive {archivePath}."); + } + + /// <summary> + /// Save the manifest file. + /// </summary> + private void SaveManifest() + { + CacheManifestHeader manifestHeader = new CacheManifestHeader(_version, _graphicsApi, _hashType); + + byte[] data; + + lock (_hashTable) + { + data = new byte[Unsafe.SizeOf<CacheManifestHeader>() + _hashTable.Count * Unsafe.SizeOf<Hash128>()]; + + // CacheManifestHeader has the same size as a Hash128. + Span<Hash128> dataSpan = MemoryMarshal.Cast<byte, Hash128>(data.AsSpan()).Slice(1); + + int i = 0; + + foreach (Hash128 hash in _hashTable) + { + dataSpan[i++] = hash; + } + } + + manifestHeader.UpdateChecksum(data.AsSpan().Slice(Unsafe.SizeOf<CacheManifestHeader>())); + + MemoryMarshal.Write(data, ref manifestHeader); + + File.WriteAllBytes(GetManifestPath(), data); + } + + /// <summary> + /// Generate the path to the cache directory. + /// </summary> + /// <param name="baseCacheDirectory">The base of the cache directory</param> + /// <param name="graphicsApi">The graphics api in use</param> + /// <param name="shaderProvider">The name of the shader provider in use</param> + /// <param name="cacheName">The name of the cache</param> + /// <returns>The path to the cache directory</returns> + private static string GenerateCachePath(string baseCacheDirectory, CacheGraphicsApi graphicsApi, string shaderProvider, string cacheName) + { + string graphicsApiName = graphicsApi switch + { + CacheGraphicsApi.OpenGL => "opengl", + CacheGraphicsApi.OpenGLES => "opengles", + CacheGraphicsApi.Vulkan => "vulkan", + CacheGraphicsApi.DirectX => "directx", + CacheGraphicsApi.Metal => "metal", + CacheGraphicsApi.Guest => "guest", + _ => throw new NotImplementedException(graphicsApi.ToString()), + }; + + return Path.Combine(baseCacheDirectory, graphicsApiName, shaderProvider, cacheName); + } + + /// <summary> + /// Get a cached file with the given hash. + /// </summary> + /// <param name="keyHash">The given hash</param> + /// <returns>The cached file if present or null</returns> + public byte[] GetValueRaw(ref Hash128 keyHash) + { + return GetValueRawFromArchive(ref keyHash) ?? GetValueRawFromFile(ref keyHash); + } + + /// <summary> + /// Get a cached file with the given hash that is present in the archive. + /// </summary> + /// <param name="keyHash">The given hash</param> + /// <returns>The cached file if present or null</returns> + private byte[] GetValueRawFromArchive(ref Hash128 keyHash) + { + bool found; + + lock (_hashTable) + { + found = _hashTable.Contains(keyHash); + } + + if (found) + { + ZipArchiveEntry archiveEntry = _cacheArchive.GetEntry($"{keyHash}"); + + if (archiveEntry != null) + { + try + { + byte[] result = new byte[archiveEntry.Length]; + + using (Stream archiveStream = archiveEntry.Open()) + { + archiveStream.Read(result); + + return result; + } + } + catch (Exception e) + { + Logger.Error?.Print(LogClass.Gpu, $"Cannot load cache file {keyHash} from archive"); + Logger.Error?.Print(LogClass.Gpu, e.ToString()); + } + } + } + + return null; + } + + /// <summary> + /// Get a cached file with the given hash that is not present in the archive. + /// </summary> + /// <param name="keyHash">The given hash</param> + /// <returns>The cached file if present or null</returns> + private byte[] GetValueRawFromFile(ref Hash128 keyHash) + { + bool found; + + lock (_hashTable) + { + found = _hashTable.Contains(keyHash); + } + + if (found) + { + string cacheTempFilePath = GenCacheTempFilePath(keyHash); + + try + { + return File.ReadAllBytes(GenCacheTempFilePath(keyHash)); + } + catch (Exception e) + { + Logger.Error?.Print(LogClass.Gpu, $"Cannot load cache file at {cacheTempFilePath}"); + Logger.Error?.Print(LogClass.Gpu, e.ToString()); + } + } + + return null; + } + + private void HandleCacheTask(CacheFileOperationTask task) + { + switch (task.Type) + { + case CacheFileOperation.SaveTempEntry: + SaveTempEntry((CacheFileSaveEntryTaskData)task.Data); + break; + case CacheFileOperation.SaveManifest: + SaveManifest(); + break; + case CacheFileOperation.FlushToArchive: + FlushToArchive(); + break; + case CacheFileOperation.Synchronize: + ((ManualResetEvent)task.Data).Set(); + break; + default: + throw new NotImplementedException($"{task.Type}"); + } + + } + + /// <summary> + /// Save a new entry in the temp cache. + /// </summary> + /// <param name="entry">The entry to save in the temp cache</param> + private void SaveTempEntry(CacheFileSaveEntryTaskData entry) + { + string tempPath = GenCacheTempFilePath(entry.Key); + + File.WriteAllBytes(tempPath, entry.Value); + } + + /// <summary> + /// Add a new value in the cache with a given hash. + /// </summary> + /// <param name="keyHash">The hash to use for the value in the cache</param> + /// <param name="value">The value to cache</param> + public void AddValue(ref Hash128 keyHash, byte[] value) + { + Debug.Assert(value != null); + Debug.Assert(GetValueRaw(ref keyHash) != null); + + bool isAlreadyPresent; + + lock (_hashTable) + { + isAlreadyPresent = !_hashTable.Add(keyHash); + } + + if (isAlreadyPresent) + { + // NOTE: Used for debug + File.WriteAllBytes(GenCacheTempFilePath(new Hash128()), value); + + throw new InvalidOperationException($"Cache collision found on {GenCacheTempFilePath(keyHash)}"); + } + + // Queue file change operations + _fileWriterWorkerQueue.Add(new CacheFileOperationTask + { + Type = CacheFileOperation.SaveTempEntry, + Data = new CacheFileSaveEntryTaskData + { + Key = keyHash, + Value = value + } + }); + + // Save the manifest changes + _fileWriterWorkerQueue.Add(new CacheFileOperationTask + { + Type = CacheFileOperation.SaveManifest, + }); + } + + /// <summary> + /// Replace a value at the given hash in the cache. + /// </summary> + /// <param name="keyHash">The hash to use for the value in the cache</param> + /// <param name="value">The value to cache</param> + public void ReplaceValue(ref Hash128 keyHash, byte[] value) + { + Debug.Assert(value != null); + + // Only queue file change operations + _fileWriterWorkerQueue.Add(new CacheFileOperationTask + { + Type = CacheFileOperation.SaveTempEntry, + Data = new CacheFileSaveEntryTaskData + { + Key = keyHash, + Value = value + } + }); + } + + public void Dispose() + { + Dispose(true); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + // Make sure all operations on _fileWriterWorkerQueue are done. + Synchronize(); + + _fileWriterWorkerQueue.Dispose(); + EnsureArchiveUpToDate(); + } + } + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs new file mode 100644 index 00000000..d241eb01 --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs @@ -0,0 +1,168 @@ +using Ryujinx.Common; +using Ryujinx.Common.Configuration; +using Ryujinx.Graphics.Gpu.Shader.Cache.Definition; +using System; +using System.Collections.Generic; +using System.IO; + +namespace Ryujinx.Graphics.Gpu.Shader.Cache +{ + /// <summary> + /// Global Manager of the shader cache. + /// </summary> + class CacheManager : IDisposable + { + private CacheGraphicsApi _graphicsApi; + private CacheHashType _hashType; + private string _shaderProvider; + + /// <summary> + /// Cache storing raw Maxwell shaders as programs. + /// </summary> + private CacheCollection _guestProgramCache; + + /// <summary> + /// Cache storing raw host programs. + /// </summary> + private CacheCollection _hostProgramCache; + + /// <summary> + /// Version of the guest cache shader (to increment when guest cache structure change). + /// </summary> + private const ulong GuestCacheVersion = 1; + + /// <summary> + /// Create a new cache manager instance + /// </summary> + /// <param name="graphicsApi">The graphics api in use</param> + /// <param name="hashType">The hash type in use for the cache</param> + /// <param name="shaderProvider">The name of the codegen provider</param> + /// <param name="titleId">The guest application title ID</param> + /// <param name="shaderCodeGenVersion">Version of the codegen</param> + public CacheManager(CacheGraphicsApi graphicsApi, CacheHashType hashType, string shaderProvider, string titleId, ulong shaderCodeGenVersion) + { + _graphicsApi = graphicsApi; + _hashType = hashType; + _shaderProvider = shaderProvider; + + string baseCacheDirectory = Path.Combine(AppDataManager.GamesDirPath, titleId, "cache", "shader"); + + _guestProgramCache = new CacheCollection(baseCacheDirectory, _hashType, CacheGraphicsApi.Guest, "", "program", GuestCacheVersion); + _hostProgramCache = new CacheCollection(baseCacheDirectory, _hashType, _graphicsApi, _shaderProvider, "host", shaderCodeGenVersion); + } + + + /// <summary> + /// Entries to remove from the manifest. + /// </summary> + /// <param name="entries">Entries to remove from the manifest of all caches</param> + public void RemoveManifestEntries(HashSet<Hash128> entries) + { + _guestProgramCache.RemoveManifestEntries(entries); + _hostProgramCache.RemoveManifestEntries(entries); + } + + /// <summary> + /// Queue a task to flush temporary files to the archives. + /// </summary> + public void FlushToArchive() + { + _guestProgramCache.FlushToArchiveAsync(); + _hostProgramCache.FlushToArchiveAsync(); + } + + /// <summary> + /// Wait for all tasks before this given point to be done. + /// </summary> + public void Synchronize() + { + _guestProgramCache.Synchronize(); + _hostProgramCache.Synchronize(); + } + + /// <summary> + /// Computes the hash of some data using the current cache hashing algorithm. + /// </summary> + /// <param name="data">Some data to generate a hash for.</param> + /// <returns>The hash of some data using the current hashing algorithm of the cache</returns> + public Hash128 ComputeHash(ReadOnlySpan<byte> data) + { + return XXHash128.ComputeHash(data); + } + + /// <summary> + /// Save a shader program not present in the program cache. + /// </summary> + /// <param name="programCodeHash">Target program code hash</param> + /// <param name="guestProgram">Guest program raw data</param> + /// <param name="hostProgram">Host program raw data</param> + public void SaveProgram(ref Hash128 programCodeHash, byte[] guestProgram, byte[] hostProgram) + { + _guestProgramCache.AddValue(ref programCodeHash, guestProgram); + _hostProgramCache.AddValue(ref programCodeHash, hostProgram); + } + + /// <summary> + /// Add a host shader program not present in the program cache. + /// </summary> + /// <param name="programCodeHash">Target program code hash</param> + /// <param name="data">Host program raw data</param> + public void AddHostProgram(ref Hash128 programCodeHash, byte[] data) + { + _hostProgramCache.AddValue(ref programCodeHash, data); + } + + /// <summary> + /// Replace a host shader program present in the program cache. + /// </summary> + /// <param name="programCodeHash">Target program code hash</param> + /// <param name="data">Host program raw data</param> + public void ReplaceHostProgram(ref Hash128 programCodeHash, byte[] data) + { + _hostProgramCache.ReplaceValue(ref programCodeHash, data); + } + + /// <summary> + /// Get all guest program hashes. + /// </summary> + /// <returns>All guest program hashes</returns> + public ReadOnlySpan<Hash128> GetGuestProgramList() + { + return _guestProgramCache.HashTable; + } + + /// <summary> + /// Get a host program by hash. + /// </summary> + /// <param name="hash">The given hash</param> + /// <returns>The host program if present or null</returns> + public byte[] GetHostProgramByHash(ref Hash128 hash) + { + return _hostProgramCache.GetValueRaw(ref hash); + } + + /// <summary> + /// Get a guest program by hash. + /// </summary> + /// <param name="hash">The given hash</param> + /// <returns>The guest program if present or null</returns> + public byte[] GetGuestProgramByHash(ref Hash128 hash) + { + return _guestProgramCache.GetValueRaw(ref hash); + } + + public void Dispose() + { + Dispose(true); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + _guestProgramCache.Dispose(); + _hostProgramCache.Dispose(); + } + } + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/CacheGraphicsApi.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/CacheGraphicsApi.cs new file mode 100644 index 00000000..9f8b5c39 --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/CacheGraphicsApi.cs @@ -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 + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/CacheHashType.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/CacheHashType.cs new file mode 100644 index 00000000..e4ebe416 --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/CacheHashType.cs @@ -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 + } +} 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 +{ + /// <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); + } + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestGpuAccessorHeader.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestGpuAccessorHeader.cs new file mode 100644 index 00000000..396b0443 --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestGpuAccessorHeader.cs @@ -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; + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheEntry.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheEntry.cs new file mode 100644 index 00000000..45a442e2 --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheEntry.cs @@ -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; + } + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheEntryHeader.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheEntryHeader.cs new file mode 100644 index 00000000..6d5bb28d --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheEntryHeader.cs @@ -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; + } + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheHeader.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheHeader.cs new file mode 100644 index 00000000..700be47d --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheHeader.cs @@ -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; + } + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheTransformFeedbackHeader.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheTransformFeedbackHeader.cs new file mode 100644 index 00000000..18cfdf55 --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheTransformFeedbackHeader.cs @@ -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; + } + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestTextureDescriptor.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestTextureDescriptor.cs new file mode 100644 index 00000000..7c73ef7b --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestTextureDescriptor.cs @@ -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; + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/HostShaderCacheEntry.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/HostShaderCacheEntry.cs new file mode 100644 index 00000000..f592919f --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/HostShaderCacheEntry.cs @@ -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(); + } + } + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/HostShaderCacheEntryHeader.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/HostShaderCacheEntryHeader.cs new file mode 100644 index 00000000..9b1af8fb --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/HostShaderCacheEntryHeader.cs @@ -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; + } + } +} diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/HostShaderCacheHeader.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/HostShaderCacheHeader.cs new file mode 100644 index 00000000..27f216cc --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/HostShaderCacheHeader.cs @@ -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; + } + } +} |
