CacheManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.Gpu.Shader.Cache
  6. {
  7. /// <summary>
  8. /// Global Manager of the shader cache.
  9. /// </summary>
  10. class CacheManager : IDisposable
  11. {
  12. private CacheGraphicsApi _graphicsApi;
  13. private CacheHashType _hashType;
  14. private string _shaderProvider;
  15. /// <summary>
  16. /// Cache storing raw Maxwell shaders as programs.
  17. /// </summary>
  18. private CacheCollection _guestProgramCache;
  19. /// <summary>
  20. /// Cache storing raw host programs.
  21. /// </summary>
  22. private CacheCollection _hostProgramCache;
  23. /// <summary>
  24. /// Version of the guest cache shader (to increment when guest cache structure change).
  25. /// </summary>
  26. private const ulong GuestCacheVersion = 1759;
  27. public bool IsReadOnly => _guestProgramCache.IsReadOnly || _hostProgramCache.IsReadOnly;
  28. /// <summary>
  29. /// Create a new cache manager instance
  30. /// </summary>
  31. /// <param name="graphicsApi">The graphics api in use</param>
  32. /// <param name="hashType">The hash type in use for the cache</param>
  33. /// <param name="shaderProvider">The name of the codegen provider</param>
  34. /// <param name="titleId">The guest application title ID</param>
  35. /// <param name="shaderCodeGenVersion">Version of the codegen</param>
  36. public CacheManager(CacheGraphicsApi graphicsApi, CacheHashType hashType, string shaderProvider, string titleId, ulong shaderCodeGenVersion)
  37. {
  38. _graphicsApi = graphicsApi;
  39. _hashType = hashType;
  40. _shaderProvider = shaderProvider;
  41. string baseCacheDirectory = CacheHelper.GetBaseCacheDirectory(titleId);
  42. CacheMigration.Run(baseCacheDirectory, graphicsApi, hashType, shaderProvider);
  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. /// Save a shader program not present in the program cache.
  73. /// </summary>
  74. /// <param name="programCodeHash">Target program code hash</param>
  75. /// <param name="guestProgram">Guest program raw data</param>
  76. /// <param name="hostProgram">Host program raw data</param>
  77. public void SaveProgram(ref Hash128 programCodeHash, byte[] guestProgram, byte[] hostProgram)
  78. {
  79. _guestProgramCache.AddValue(ref programCodeHash, guestProgram);
  80. _hostProgramCache.AddValue(ref programCodeHash, hostProgram);
  81. }
  82. /// <summary>
  83. /// Add a host shader program not present in the program cache.
  84. /// </summary>
  85. /// <param name="programCodeHash">Target program code hash</param>
  86. /// <param name="data">Host program raw data</param>
  87. public void AddHostProgram(ref Hash128 programCodeHash, byte[] data)
  88. {
  89. _hostProgramCache.AddValue(ref programCodeHash, data);
  90. }
  91. /// <summary>
  92. /// Replace a host shader program 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 ReplaceHostProgram(ref Hash128 programCodeHash, byte[] data)
  97. {
  98. _hostProgramCache.ReplaceValue(ref programCodeHash, data);
  99. }
  100. /// <summary>
  101. /// Get all guest program hashes.
  102. /// </summary>
  103. /// <returns>All guest program hashes</returns>
  104. public ReadOnlySpan<Hash128> GetGuestProgramList()
  105. {
  106. return _guestProgramCache.HashTable;
  107. }
  108. /// <summary>
  109. /// Get a host program by hash.
  110. /// </summary>
  111. /// <param name="hash">The given hash</param>
  112. /// <returns>The host program if present or null</returns>
  113. public byte[] GetHostProgramByHash(ref Hash128 hash)
  114. {
  115. return _hostProgramCache.GetValueRaw(ref hash);
  116. }
  117. /// <summary>
  118. /// Get a guest program by hash.
  119. /// </summary>
  120. /// <param name="hash">The given hash</param>
  121. /// <returns>The guest program if present or null</returns>
  122. public byte[] GetGuestProgramByHash(ref Hash128 hash)
  123. {
  124. return _guestProgramCache.GetValueRaw(ref hash);
  125. }
  126. public void Dispose()
  127. {
  128. Dispose(true);
  129. }
  130. protected virtual void Dispose(bool disposing)
  131. {
  132. if (disposing)
  133. {
  134. _guestProgramCache.Dispose();
  135. _hostProgramCache.Dispose();
  136. }
  137. }
  138. }
  139. }