BackgroundDiskCacheWriter.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using System;
  4. using System.IO;
  5. namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
  6. {
  7. /// <summary>
  8. /// Represents a background disk cache writer.
  9. /// </summary>
  10. class BackgroundDiskCacheWriter : IDisposable
  11. {
  12. /// <summary>
  13. /// Possible operation to do on the <see cref="_fileWriterWorkerQueue"/>.
  14. /// </summary>
  15. private enum CacheFileOperation
  16. {
  17. /// <summary>
  18. /// Operation to add a shader to the cache.
  19. /// </summary>
  20. AddShader
  21. }
  22. /// <summary>
  23. /// Represents an operation to perform on the <see cref="_fileWriterWorkerQueue"/>.
  24. /// </summary>
  25. private readonly struct CacheFileOperationTask
  26. {
  27. /// <summary>
  28. /// The type of operation to perform.
  29. /// </summary>
  30. public readonly CacheFileOperation Type;
  31. /// <summary>
  32. /// The data associated to this operation or null.
  33. /// </summary>
  34. public readonly object Data;
  35. public CacheFileOperationTask(CacheFileOperation type, object data)
  36. {
  37. Type = type;
  38. Data = data;
  39. }
  40. }
  41. /// <summary>
  42. /// Background shader cache write information.
  43. /// </summary>
  44. private readonly struct AddShaderData
  45. {
  46. /// <summary>
  47. /// Cached shader program.
  48. /// </summary>
  49. public readonly CachedShaderProgram Program;
  50. /// <summary>
  51. /// Binary host code.
  52. /// </summary>
  53. public readonly byte[] HostCode;
  54. /// <summary>
  55. /// Creates a new background shader cache write information.
  56. /// </summary>
  57. /// <param name="program">Cached shader program</param>
  58. /// <param name="hostCode">Binary host code</param>
  59. public AddShaderData(CachedShaderProgram program, byte[] hostCode)
  60. {
  61. Program = program;
  62. HostCode = hostCode;
  63. }
  64. }
  65. private readonly GpuContext _context;
  66. private readonly DiskCacheHostStorage _hostStorage;
  67. private readonly AsyncWorkQueue<CacheFileOperationTask> _fileWriterWorkerQueue;
  68. /// <summary>
  69. /// Creates a new background disk cache writer.
  70. /// </summary>
  71. /// <param name="context">GPU context</param>
  72. /// <param name="hostStorage">Disk cache host storage</param>
  73. public BackgroundDiskCacheWriter(GpuContext context, DiskCacheHostStorage hostStorage)
  74. {
  75. _context = context;
  76. _hostStorage = hostStorage;
  77. _fileWriterWorkerQueue = new AsyncWorkQueue<CacheFileOperationTask>(ProcessTask, "GPU.BackgroundDiskCacheWriter");
  78. }
  79. /// <summary>
  80. /// Processes a shader cache background operation.
  81. /// </summary>
  82. /// <param name="task">Task to process</param>
  83. private void ProcessTask(CacheFileOperationTask task)
  84. {
  85. switch (task.Type)
  86. {
  87. case CacheFileOperation.AddShader:
  88. AddShaderData data = (AddShaderData)task.Data;
  89. try
  90. {
  91. _hostStorage.AddShader(_context, data.Program, data.HostCode);
  92. }
  93. catch (DiskCacheLoadException diskCacheLoadException)
  94. {
  95. Logger.Error?.Print(LogClass.Gpu, $"Error writing shader to disk cache. {diskCacheLoadException.Message}");
  96. }
  97. catch (IOException ioException)
  98. {
  99. Logger.Error?.Print(LogClass.Gpu, $"Error writing shader to disk cache. {ioException.Message}");
  100. }
  101. break;
  102. }
  103. }
  104. /// <summary>
  105. /// Adds a shader program to be cached in the background.
  106. /// </summary>
  107. /// <param name="program">Shader program to cache</param>
  108. /// <param name="hostCode">Host binary code of the program</param>
  109. public void AddShader(CachedShaderProgram program, byte[] hostCode)
  110. {
  111. _fileWriterWorkerQueue.Add(new CacheFileOperationTask(CacheFileOperation.AddShader, new AddShaderData(program, hostCode)));
  112. }
  113. public void Dispose()
  114. {
  115. Dispose(true);
  116. }
  117. protected virtual void Dispose(bool disposing)
  118. {
  119. if (disposing)
  120. {
  121. _fileWriterWorkerQueue.Dispose();
  122. }
  123. }
  124. }
  125. }