CacheManager.cs 6.1 KB

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