CacheManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 = 1759;
  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 = CacheHelper.GetBaseCacheDirectory(titleId);
  43. CacheMigration.Run(baseCacheDirectory, graphicsApi, hashType, shaderProvider);
  44. _guestProgramCache = new CacheCollection(baseCacheDirectory, _hashType, CacheGraphicsApi.Guest, "", "program", GuestCacheVersion);
  45. _hostProgramCache = new CacheCollection(baseCacheDirectory, _hashType, _graphicsApi, _shaderProvider, "host", shaderCodeGenVersion);
  46. }
  47. /// <summary>
  48. /// Entries to remove from the manifest.
  49. /// </summary>
  50. /// <param name="entries">Entries to remove from the manifest of all caches</param>
  51. public void RemoveManifestEntries(HashSet<Hash128> entries)
  52. {
  53. _guestProgramCache.RemoveManifestEntriesAsync(entries);
  54. _hostProgramCache.RemoveManifestEntriesAsync(entries);
  55. }
  56. /// <summary>
  57. /// Queue a task to flush temporary files to the archives.
  58. /// </summary>
  59. public void FlushToArchive()
  60. {
  61. _guestProgramCache.FlushToArchiveAsync();
  62. _hostProgramCache.FlushToArchiveAsync();
  63. }
  64. /// <summary>
  65. /// Wait for all tasks before this given point to be done.
  66. /// </summary>
  67. public void Synchronize()
  68. {
  69. _guestProgramCache.Synchronize();
  70. _hostProgramCache.Synchronize();
  71. }
  72. /// <summary>
  73. /// Save a shader program not present in the program cache.
  74. /// </summary>
  75. /// <param name="programCodeHash">Target program code hash</param>
  76. /// <param name="guestProgram">Guest program raw data</param>
  77. /// <param name="hostProgram">Host program raw data</param>
  78. public void SaveProgram(ref Hash128 programCodeHash, byte[] guestProgram, byte[] hostProgram)
  79. {
  80. _guestProgramCache.AddValue(ref programCodeHash, guestProgram);
  81. _hostProgramCache.AddValue(ref programCodeHash, hostProgram);
  82. }
  83. /// <summary>
  84. /// Add a host shader program not present in the program cache.
  85. /// </summary>
  86. /// <param name="programCodeHash">Target program code hash</param>
  87. /// <param name="data">Host program raw data</param>
  88. public void AddHostProgram(ref Hash128 programCodeHash, byte[] data)
  89. {
  90. _hostProgramCache.AddValue(ref programCodeHash, data);
  91. }
  92. /// <summary>
  93. /// Replace a host shader program present in the program cache.
  94. /// </summary>
  95. /// <param name="programCodeHash">Target program code hash</param>
  96. /// <param name="data">Host program raw data</param>
  97. public void ReplaceHostProgram(ref Hash128 programCodeHash, byte[] data)
  98. {
  99. _hostProgramCache.ReplaceValue(ref programCodeHash, data);
  100. }
  101. /// <summary>
  102. /// Get all guest program hashes.
  103. /// </summary>
  104. /// <returns>All guest program hashes</returns>
  105. public ReadOnlySpan<Hash128> GetGuestProgramList()
  106. {
  107. return _guestProgramCache.HashTable;
  108. }
  109. /// <summary>
  110. /// Get a host program by hash.
  111. /// </summary>
  112. /// <param name="hash">The given hash</param>
  113. /// <returns>The host program if present or null</returns>
  114. public byte[] GetHostProgramByHash(ref Hash128 hash)
  115. {
  116. return _hostProgramCache.GetValueRaw(ref hash);
  117. }
  118. /// <summary>
  119. /// Get a guest program by hash.
  120. /// </summary>
  121. /// <param name="hash">The given hash</param>
  122. /// <returns>The guest program if present or null</returns>
  123. public byte[] GetGuestProgramByHash(ref Hash128 hash)
  124. {
  125. return _guestProgramCache.GetValueRaw(ref hash);
  126. }
  127. public void Dispose()
  128. {
  129. Dispose(true);
  130. }
  131. protected virtual void Dispose(bool disposing)
  132. {
  133. if (disposing)
  134. {
  135. _guestProgramCache.Dispose();
  136. _hostProgramCache.Dispose();
  137. }
  138. }
  139. }
  140. }