CacheManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Configuration;
  3. using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. namespace Ryujinx.Graphics.Gpu.Shader.Cache
  8. {
  9. /// <summary>
  10. /// Global Manager of the shader cache.
  11. /// </summary>
  12. class CacheManager : IDisposable
  13. {
  14. private CacheGraphicsApi _graphicsApi;
  15. private CacheHashType _hashType;
  16. private string _shaderProvider;
  17. /// <summary>
  18. /// Cache storing raw Maxwell shaders as programs.
  19. /// </summary>
  20. private CacheCollection _guestProgramCache;
  21. /// <summary>
  22. /// Cache storing raw host programs.
  23. /// </summary>
  24. private CacheCollection _hostProgramCache;
  25. /// <summary>
  26. /// Version of the guest cache shader (to increment when guest cache structure change).
  27. /// </summary>
  28. private const ulong GuestCacheVersion = 1717;
  29. /// <summary>
  30. /// Create a new cache manager instance
  31. /// </summary>
  32. /// <param name="graphicsApi">The graphics api in use</param>
  33. /// <param name="hashType">The hash type in use for the cache</param>
  34. /// <param name="shaderProvider">The name of the codegen provider</param>
  35. /// <param name="titleId">The guest application title ID</param>
  36. /// <param name="shaderCodeGenVersion">Version of the codegen</param>
  37. public CacheManager(CacheGraphicsApi graphicsApi, CacheHashType hashType, string shaderProvider, string titleId, ulong shaderCodeGenVersion)
  38. {
  39. _graphicsApi = graphicsApi;
  40. _hashType = hashType;
  41. _shaderProvider = shaderProvider;
  42. string baseCacheDirectory = Path.Combine(AppDataManager.GamesDirPath, titleId, "cache", "shader");
  43. _guestProgramCache = new CacheCollection(baseCacheDirectory, _hashType, CacheGraphicsApi.Guest, "", "program", GuestCacheVersion);
  44. _hostProgramCache = new CacheCollection(baseCacheDirectory, _hashType, _graphicsApi, _shaderProvider, "host", shaderCodeGenVersion);
  45. }
  46. /// <summary>
  47. /// Entries to remove from the manifest.
  48. /// </summary>
  49. /// <param name="entries">Entries to remove from the manifest of all caches</param>
  50. public void RemoveManifestEntries(HashSet<Hash128> entries)
  51. {
  52. _guestProgramCache.RemoveManifestEntriesAsync(entries);
  53. _hostProgramCache.RemoveManifestEntriesAsync(entries);
  54. }
  55. /// <summary>
  56. /// Queue a task to flush temporary files to the archives.
  57. /// </summary>
  58. public void FlushToArchive()
  59. {
  60. _guestProgramCache.FlushToArchiveAsync();
  61. _hostProgramCache.FlushToArchiveAsync();
  62. }
  63. /// <summary>
  64. /// Wait for all tasks before this given point to be done.
  65. /// </summary>
  66. public void Synchronize()
  67. {
  68. _guestProgramCache.Synchronize();
  69. _hostProgramCache.Synchronize();
  70. }
  71. /// <summary>
  72. /// Computes the hash of some data using the current cache hashing algorithm.
  73. /// </summary>
  74. /// <param name="data">Some data to generate a hash for.</param>
  75. /// <returns>The hash of some data using the current hashing algorithm of the cache</returns>
  76. public Hash128 ComputeHash(ReadOnlySpan<byte> data)
  77. {
  78. return XXHash128.ComputeHash(data);
  79. }
  80. /// <summary>
  81. /// Save a shader program not present in the program cache.
  82. /// </summary>
  83. /// <param name="programCodeHash">Target program code hash</param>
  84. /// <param name="guestProgram">Guest program raw data</param>
  85. /// <param name="hostProgram">Host program raw data</param>
  86. public void SaveProgram(ref Hash128 programCodeHash, byte[] guestProgram, byte[] hostProgram)
  87. {
  88. _guestProgramCache.AddValue(ref programCodeHash, guestProgram);
  89. _hostProgramCache.AddValue(ref programCodeHash, hostProgram);
  90. }
  91. /// <summary>
  92. /// Add a host shader program not present in the program cache.
  93. /// </summary>
  94. /// <param name="programCodeHash">Target program code hash</param>
  95. /// <param name="data">Host program raw data</param>
  96. public void AddHostProgram(ref Hash128 programCodeHash, byte[] data)
  97. {
  98. _hostProgramCache.AddValue(ref programCodeHash, data);
  99. }
  100. /// <summary>
  101. /// Replace a host shader program present in the program cache.
  102. /// </summary>
  103. /// <param name="programCodeHash">Target program code hash</param>
  104. /// <param name="data">Host program raw data</param>
  105. public void ReplaceHostProgram(ref Hash128 programCodeHash, byte[] data)
  106. {
  107. _hostProgramCache.ReplaceValue(ref programCodeHash, data);
  108. }
  109. /// <summary>
  110. /// Get all guest program hashes.
  111. /// </summary>
  112. /// <returns>All guest program hashes</returns>
  113. public ReadOnlySpan<Hash128> GetGuestProgramList()
  114. {
  115. return _guestProgramCache.HashTable;
  116. }
  117. /// <summary>
  118. /// Get a host program by hash.
  119. /// </summary>
  120. /// <param name="hash">The given hash</param>
  121. /// <returns>The host program if present or null</returns>
  122. public byte[] GetHostProgramByHash(ref Hash128 hash)
  123. {
  124. return _hostProgramCache.GetValueRaw(ref hash);
  125. }
  126. /// <summary>
  127. /// Get a guest program by hash.
  128. /// </summary>
  129. /// <param name="hash">The given hash</param>
  130. /// <returns>The guest program if present or null</returns>
  131. public byte[] GetGuestProgramByHash(ref Hash128 hash)
  132. {
  133. return _guestProgramCache.GetValueRaw(ref hash);
  134. }
  135. public void Dispose()
  136. {
  137. Dispose(true);
  138. }
  139. protected virtual void Dispose(bool disposing)
  140. {
  141. if (disposing)
  142. {
  143. _guestProgramCache.Dispose();
  144. _hostProgramCache.Dispose();
  145. }
  146. }
  147. }
  148. }