ParallelDiskCacheLoader.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Shader;
  4. using Ryujinx.Graphics.Shader.Translation;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Threading;
  10. using static Ryujinx.Graphics.Gpu.Shader.ShaderCache;
  11. namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
  12. {
  13. class ParallelDiskCacheLoader
  14. {
  15. private const int ThreadCount = 8;
  16. private readonly GpuContext _context;
  17. private readonly ShaderCacheHashTable _graphicsCache;
  18. private readonly ComputeShaderCacheHashTable _computeCache;
  19. private readonly DiskCacheHostStorage _hostStorage;
  20. private readonly CancellationToken _cancellationToken;
  21. private readonly Action<ShaderCacheState, int, int> _stateChangeCallback;
  22. /// <summary>
  23. /// Indicates if the cache should be loaded.
  24. /// </summary>
  25. public bool Active => !_cancellationToken.IsCancellationRequested;
  26. private bool _needsHostRegen;
  27. /// <summary>
  28. /// Number of shaders that failed to compile from the cache.
  29. /// </summary>
  30. public int ErrorCount { get; private set; }
  31. /// <summary>
  32. /// Program validation entry.
  33. /// </summary>
  34. private struct ProgramEntry
  35. {
  36. /// <summary>
  37. /// Cached shader program.
  38. /// </summary>
  39. public readonly CachedShaderProgram CachedProgram;
  40. /// <summary>
  41. /// Host program.
  42. /// </summary>
  43. public readonly IProgram HostProgram;
  44. /// <summary>
  45. /// Program index.
  46. /// </summary>
  47. public readonly int ProgramIndex;
  48. /// <summary>
  49. /// Indicates if the program is a compute shader.
  50. /// </summary>
  51. public readonly bool IsCompute;
  52. /// <summary>
  53. /// Indicates if the program is a host binary shader.
  54. /// </summary>
  55. public readonly bool IsBinary;
  56. /// <summary>
  57. /// Creates a new program validation entry.
  58. /// </summary>
  59. /// <param name="cachedProgram">Cached shader program</param>
  60. /// <param name="hostProgram">Host program</param>
  61. /// <param name="programIndex">Program index</param>
  62. /// <param name="isCompute">Indicates if the program is a compute shader</param>
  63. /// <param name="isBinary">Indicates if the program is a host binary shader</param>
  64. public ProgramEntry(
  65. CachedShaderProgram cachedProgram,
  66. IProgram hostProgram,
  67. int programIndex,
  68. bool isCompute,
  69. bool isBinary)
  70. {
  71. CachedProgram = cachedProgram;
  72. HostProgram = hostProgram;
  73. ProgramIndex = programIndex;
  74. IsCompute = isCompute;
  75. IsBinary = isBinary;
  76. }
  77. }
  78. /// <summary>
  79. /// Translated shader compilation entry.
  80. /// </summary>
  81. private struct ProgramCompilation
  82. {
  83. /// <summary>
  84. /// Translated shader stages.
  85. /// </summary>
  86. public readonly ShaderProgram[] TranslatedStages;
  87. /// <summary>
  88. /// Cached shaders.
  89. /// </summary>
  90. public readonly CachedShaderStage[] Shaders;
  91. /// <summary>
  92. /// Specialization state.
  93. /// </summary>
  94. public readonly ShaderSpecializationState SpecializationState;
  95. /// <summary>
  96. /// Program index.
  97. /// </summary>
  98. public readonly int ProgramIndex;
  99. /// <summary>
  100. /// Indicates if the program is a compute shader.
  101. /// </summary>
  102. public readonly bool IsCompute;
  103. /// <summary>
  104. /// Creates a new translated shader compilation entry.
  105. /// </summary>
  106. /// <param name="translatedStages">Translated shader stages</param>
  107. /// <param name="shaders">Cached shaders</param>
  108. /// <param name="specState">Specialization state</param>
  109. /// <param name="programIndex">Program index</param>
  110. /// <param name="isCompute">Indicates if the program is a compute shader</param>
  111. public ProgramCompilation(
  112. ShaderProgram[] translatedStages,
  113. CachedShaderStage[] shaders,
  114. ShaderSpecializationState specState,
  115. int programIndex,
  116. bool isCompute)
  117. {
  118. TranslatedStages = translatedStages;
  119. Shaders = shaders;
  120. SpecializationState = specState;
  121. ProgramIndex = programIndex;
  122. IsCompute = isCompute;
  123. }
  124. }
  125. /// <summary>
  126. /// Program translation entry.
  127. /// </summary>
  128. private struct AsyncProgramTranslation
  129. {
  130. /// <summary>
  131. /// Cached shader stages.
  132. /// </summary>
  133. public readonly CachedShaderStage[] Shaders;
  134. /// <summary>
  135. /// Specialization state.
  136. /// </summary>
  137. public readonly ShaderSpecializationState SpecializationState;
  138. /// <summary>
  139. /// Program index.
  140. /// </summary>
  141. public readonly int ProgramIndex;
  142. /// <summary>
  143. /// Indicates if the program is a compute shader.
  144. /// </summary>
  145. public readonly bool IsCompute;
  146. /// <summary>
  147. /// Creates a new program translation entry.
  148. /// </summary>
  149. /// <param name="shaders">Cached shader stages</param>
  150. /// <param name="specState">Specialization state</param>
  151. /// <param name="programIndex">Program index</param>
  152. /// <param name="isCompute">Indicates if the program is a compute shader</param>
  153. public AsyncProgramTranslation(
  154. CachedShaderStage[] shaders,
  155. ShaderSpecializationState specState,
  156. int programIndex,
  157. bool isCompute)
  158. {
  159. Shaders = shaders;
  160. SpecializationState = specState;
  161. ProgramIndex = programIndex;
  162. IsCompute = isCompute;
  163. }
  164. }
  165. private readonly Queue<ProgramEntry> _validationQueue;
  166. private readonly ConcurrentQueue<ProgramCompilation> _compilationQueue;
  167. private readonly BlockingCollection<AsyncProgramTranslation> _asyncTranslationQueue;
  168. private readonly SortedList<int, CachedShaderProgram> _programList;
  169. private int _backendParallelCompileThreads;
  170. private int _compiledCount;
  171. private int _totalCount;
  172. /// <summary>
  173. /// Creates a new parallel disk cache loader.
  174. /// </summary>
  175. /// <param name="context">GPU context</param>
  176. /// <param name="graphicsCache">Graphics shader cache</param>
  177. /// <param name="computeCache">Compute shader cache</param>
  178. /// <param name="hostStorage">Disk cache host storage</param>
  179. /// <param name="cancellationToken">Cancellation token</param>
  180. /// <param name="stateChangeCallback">Function to be called when there is a state change, reporting state, compiled and total shaders count</param>
  181. public ParallelDiskCacheLoader(
  182. GpuContext context,
  183. ShaderCacheHashTable graphicsCache,
  184. ComputeShaderCacheHashTable computeCache,
  185. DiskCacheHostStorage hostStorage,
  186. CancellationToken cancellationToken,
  187. Action<ShaderCacheState, int, int> stateChangeCallback)
  188. {
  189. _context = context;
  190. _graphicsCache = graphicsCache;
  191. _computeCache = computeCache;
  192. _hostStorage = hostStorage;
  193. _cancellationToken = cancellationToken;
  194. _stateChangeCallback = stateChangeCallback;
  195. _validationQueue = new Queue<ProgramEntry>();
  196. _compilationQueue = new ConcurrentQueue<ProgramCompilation>();
  197. _asyncTranslationQueue = new BlockingCollection<AsyncProgramTranslation>(ThreadCount);
  198. _programList = new SortedList<int, CachedShaderProgram>();
  199. _backendParallelCompileThreads = Math.Min(Environment.ProcessorCount, 8); // Must be kept in sync with the backend code.
  200. }
  201. /// <summary>
  202. /// Loads all shaders from the cache.
  203. /// </summary>
  204. public void LoadShaders()
  205. {
  206. Thread[] workThreads = new Thread[ThreadCount];
  207. for (int index = 0; index < ThreadCount; index++)
  208. {
  209. workThreads[index] = new Thread(ProcessAsyncQueue)
  210. {
  211. Name = $"Gpu.AsyncTranslationThread.{index}"
  212. };
  213. }
  214. int programCount = _hostStorage.GetProgramCount();
  215. _compiledCount = 0;
  216. _totalCount = programCount;
  217. _stateChangeCallback(ShaderCacheState.Start, 0, programCount);
  218. Logger.Info?.Print(LogClass.Gpu, $"Loading {programCount} shaders from the cache...");
  219. for (int index = 0; index < ThreadCount; index++)
  220. {
  221. workThreads[index].Start(_cancellationToken);
  222. }
  223. try
  224. {
  225. _hostStorage.LoadShaders(_context, this);
  226. }
  227. catch (DiskCacheLoadException diskCacheLoadException)
  228. {
  229. Logger.Warning?.Print(LogClass.Gpu, $"Error loading the shader cache. {diskCacheLoadException.Message}");
  230. // If we can't even access the file, then we also can't rebuild.
  231. if (diskCacheLoadException.Result != DiskCacheLoadResult.NoAccess)
  232. {
  233. _needsHostRegen = true;
  234. }
  235. }
  236. catch (InvalidDataException invalidDataException)
  237. {
  238. Logger.Warning?.Print(LogClass.Gpu, $"Error decompressing the shader cache file. {invalidDataException.Message}");
  239. _needsHostRegen = true;
  240. }
  241. catch (IOException ioException)
  242. {
  243. Logger.Warning?.Print(LogClass.Gpu, $"Error reading the shader cache file. {ioException.Message}");
  244. _needsHostRegen = true;
  245. }
  246. _asyncTranslationQueue.CompleteAdding();
  247. for (int index = 0; index < ThreadCount; index++)
  248. {
  249. workThreads[index].Join();
  250. }
  251. CheckCompilationBlocking();
  252. if (_needsHostRegen)
  253. {
  254. // Rebuild both shared and host cache files.
  255. // Rebuilding shared is required because the shader information returned by the translator
  256. // might have changed, and so we have to reconstruct the file with the new information.
  257. try
  258. {
  259. _hostStorage.ClearSharedCache();
  260. _hostStorage.ClearHostCache(_context);
  261. if (_programList.Count != 0)
  262. {
  263. Logger.Info?.Print(LogClass.Gpu, $"Rebuilding {_programList.Count} shaders...");
  264. using var streams = _hostStorage.GetOutputStreams(_context);
  265. foreach (var kv in _programList)
  266. {
  267. if (!Active)
  268. {
  269. break;
  270. }
  271. CachedShaderProgram program = kv.Value;
  272. _hostStorage.AddShader(_context, program, program.HostProgram.GetBinary(), streams);
  273. }
  274. Logger.Info?.Print(LogClass.Gpu, $"Rebuilt {_programList.Count} shaders successfully.");
  275. }
  276. else
  277. {
  278. _hostStorage.ClearGuestCache();
  279. Logger.Info?.Print(LogClass.Gpu, "Shader cache deleted due to corruption.");
  280. }
  281. }
  282. catch (DiskCacheLoadException diskCacheLoadException)
  283. {
  284. Logger.Warning?.Print(LogClass.Gpu, $"Error deleting the shader cache. {diskCacheLoadException.Message}");
  285. }
  286. catch (IOException ioException)
  287. {
  288. Logger.Warning?.Print(LogClass.Gpu, $"Error deleting the shader cache file. {ioException.Message}");
  289. }
  290. }
  291. Logger.Info?.Print(LogClass.Gpu, "Shader cache loaded.");
  292. _stateChangeCallback(ShaderCacheState.Loaded, programCount, programCount);
  293. }
  294. /// <summary>
  295. /// Enqueues a host program for compilation.
  296. /// </summary>
  297. /// <param name="cachedProgram">Cached program</param>
  298. /// <param name="hostProgram">Host program to be compiled</param>
  299. /// <param name="programIndex">Program index</param>
  300. /// <param name="isCompute">Indicates if the program is a compute shader</param>
  301. public void QueueHostProgram(CachedShaderProgram cachedProgram, IProgram hostProgram, int programIndex, bool isCompute)
  302. {
  303. EnqueueForValidation(new ProgramEntry(cachedProgram, hostProgram, programIndex, isCompute, isBinary: true));
  304. }
  305. /// <summary>
  306. /// Enqueues a guest program for compilation.
  307. /// </summary>
  308. /// <param name="shaders">Cached shader stages</param>
  309. /// <param name="specState">Specialization state</param>
  310. /// <param name="programIndex">Program index</param>
  311. /// <param name="isCompute">Indicates if the program is a compute shader</param>
  312. public void QueueGuestProgram(CachedShaderStage[] shaders, ShaderSpecializationState specState, int programIndex, bool isCompute)
  313. {
  314. _asyncTranslationQueue.Add(new AsyncProgramTranslation(shaders, specState, programIndex, isCompute));
  315. }
  316. /// <summary>
  317. /// Check the state of programs that have already been compiled,
  318. /// and add to the cache if the compilation was successful.
  319. /// </summary>
  320. public void CheckCompilation()
  321. {
  322. ProcessCompilationQueue();
  323. // Process programs that already finished compiling.
  324. // If not yet compiled, do nothing. This avoids blocking to wait for shader compilation.
  325. while (_validationQueue.TryPeek(out ProgramEntry entry))
  326. {
  327. ProgramLinkStatus result = entry.HostProgram.CheckProgramLink(false);
  328. if (result != ProgramLinkStatus.Incomplete)
  329. {
  330. ProcessCompiledProgram(ref entry, result);
  331. _validationQueue.Dequeue();
  332. }
  333. else
  334. {
  335. break;
  336. }
  337. }
  338. }
  339. /// <summary>
  340. /// Waits until all programs finishes compiling, then adds the ones
  341. /// with successful compilation to the cache.
  342. /// </summary>
  343. private void CheckCompilationBlocking()
  344. {
  345. ProcessCompilationQueue();
  346. while (_validationQueue.TryDequeue(out ProgramEntry entry) && Active)
  347. {
  348. ProcessCompiledProgram(ref entry, entry.HostProgram.CheckProgramLink(true), asyncCompile: false);
  349. }
  350. }
  351. /// <summary>
  352. /// Process a compiled program result.
  353. /// </summary>
  354. /// <param name="entry">Compiled program entry</param>
  355. /// <param name="result">Compilation result</param>
  356. /// <param name="asyncCompile">For failed host compilations, indicates if a guest compilation should be done asynchronously</param>
  357. private void ProcessCompiledProgram(ref ProgramEntry entry, ProgramLinkStatus result, bool asyncCompile = true)
  358. {
  359. if (result == ProgramLinkStatus.Success)
  360. {
  361. // Compilation successful, add to memory cache.
  362. if (entry.IsCompute)
  363. {
  364. _computeCache.Add(entry.CachedProgram);
  365. }
  366. else
  367. {
  368. _graphicsCache.Add(entry.CachedProgram);
  369. }
  370. if (!entry.IsBinary)
  371. {
  372. _needsHostRegen = true;
  373. }
  374. _programList.Add(entry.ProgramIndex, entry.CachedProgram);
  375. SignalCompiled();
  376. }
  377. else if (entry.IsBinary)
  378. {
  379. // If this is a host binary and compilation failed,
  380. // we still have a chance to recompile from the guest binary.
  381. CachedShaderProgram program = entry.CachedProgram;
  382. if (asyncCompile)
  383. {
  384. QueueGuestProgram(program.Shaders, program.SpecializationState, entry.ProgramIndex, entry.IsCompute);
  385. }
  386. else
  387. {
  388. RecompileFromGuestCode(program.Shaders, program.SpecializationState, entry.ProgramIndex, entry.IsCompute);
  389. ProcessCompilationQueue();
  390. }
  391. }
  392. else
  393. {
  394. // Failed to compile from both host and guest binary.
  395. ErrorCount++;
  396. SignalCompiled();
  397. }
  398. }
  399. /// <summary>
  400. /// Processes the queue of translated guest programs that should be compiled on the host.
  401. /// </summary>
  402. private void ProcessCompilationQueue()
  403. {
  404. while (_compilationQueue.TryDequeue(out ProgramCompilation compilation) && Active)
  405. {
  406. ShaderSource[] shaderSources = new ShaderSource[compilation.TranslatedStages.Length];
  407. int fragmentOutputMap = -1;
  408. for (int index = 0; index < compilation.TranslatedStages.Length; index++)
  409. {
  410. ShaderProgram shader = compilation.TranslatedStages[index];
  411. shaderSources[index] = CreateShaderSource(shader);
  412. if (shader.Info.Stage == ShaderStage.Fragment)
  413. {
  414. fragmentOutputMap = shader.Info.FragmentOutputMap;
  415. }
  416. }
  417. IProgram hostProgram = _context.Renderer.CreateProgram(shaderSources, new ShaderInfo(fragmentOutputMap));
  418. CachedShaderProgram program = new CachedShaderProgram(hostProgram, compilation.SpecializationState, compilation.Shaders);
  419. EnqueueForValidation(new ProgramEntry(program, hostProgram, compilation.ProgramIndex, compilation.IsCompute, isBinary: false));
  420. }
  421. }
  422. /// <summary>
  423. /// Enqueues a program for validation, which will check if the program was compiled successfully.
  424. /// </summary>
  425. /// <param name="newEntry">Program entry to be validated</param>
  426. private void EnqueueForValidation(ProgramEntry newEntry)
  427. {
  428. _validationQueue.Enqueue(newEntry);
  429. // Do not allow more than N shader compilation in-flight, where N is the maximum number of threads
  430. // the driver will be using for parallel compilation.
  431. // Submitting more seems to cause NVIDIA OpenGL driver to crash.
  432. if (_validationQueue.Count >= _backendParallelCompileThreads && _validationQueue.TryDequeue(out ProgramEntry entry))
  433. {
  434. ProcessCompiledProgram(ref entry, entry.HostProgram.CheckProgramLink(true), asyncCompile: false);
  435. }
  436. }
  437. /// <summary>
  438. /// Processses the queue of programs that should be translated from guest code.
  439. /// </summary>
  440. /// <param name="state">Cancellation token</param>
  441. private void ProcessAsyncQueue(object state)
  442. {
  443. CancellationToken ct = (CancellationToken)state;
  444. try
  445. {
  446. foreach (AsyncProgramTranslation asyncCompilation in _asyncTranslationQueue.GetConsumingEnumerable(ct))
  447. {
  448. RecompileFromGuestCode(
  449. asyncCompilation.Shaders,
  450. asyncCompilation.SpecializationState,
  451. asyncCompilation.ProgramIndex,
  452. asyncCompilation.IsCompute);
  453. }
  454. }
  455. catch (OperationCanceledException)
  456. {
  457. }
  458. }
  459. /// <summary>
  460. /// Recompiles a program from guest code.
  461. /// </summary>
  462. /// <param name="shaders">Shader stages</param>
  463. /// <param name="specState">Specialization state</param>
  464. /// <param name="programIndex">Program index</param>
  465. /// <param name="isCompute">Indicates if the program is a compute shader</param>
  466. private void RecompileFromGuestCode(CachedShaderStage[] shaders, ShaderSpecializationState specState, int programIndex, bool isCompute)
  467. {
  468. try
  469. {
  470. if (isCompute)
  471. {
  472. RecompileComputeFromGuestCode(shaders, specState, programIndex);
  473. }
  474. else
  475. {
  476. RecompileGraphicsFromGuestCode(shaders, specState, programIndex);
  477. }
  478. }
  479. catch (DiskCacheLoadException diskCacheLoadException)
  480. {
  481. Logger.Error?.Print(LogClass.Gpu, $"Error translating guest shader. {diskCacheLoadException.Message}");
  482. ErrorCount++;
  483. SignalCompiled();
  484. }
  485. }
  486. /// <summary>
  487. /// Recompiles a graphics program from guest code.
  488. /// </summary>
  489. /// <param name="shaders">Shader stages</param>
  490. /// <param name="specState">Specialization state</param>
  491. /// <param name="programIndex">Program index</param>
  492. private void RecompileGraphicsFromGuestCode(CachedShaderStage[] shaders, ShaderSpecializationState specState, int programIndex)
  493. {
  494. ShaderSpecializationState newSpecState = new ShaderSpecializationState(specState.GraphicsState, specState.TransformFeedbackDescriptors);
  495. ResourceCounts counts = new ResourceCounts();
  496. TranslatorContext[] translatorContexts = new TranslatorContext[Constants.ShaderStages + 1];
  497. TranslatorContext nextStage = null;
  498. for (int stageIndex = Constants.ShaderStages - 1; stageIndex >= 0; stageIndex--)
  499. {
  500. CachedShaderStage shader = shaders[stageIndex + 1];
  501. if (shader != null)
  502. {
  503. byte[] guestCode = shader.Code;
  504. byte[] cb1Data = shader.Cb1Data;
  505. DiskCacheGpuAccessor gpuAccessor = new DiskCacheGpuAccessor(_context, guestCode, cb1Data, specState, newSpecState, counts, stageIndex);
  506. TranslatorContext currentStage = DecodeGraphicsShader(gpuAccessor, DefaultFlags, 0);
  507. if (nextStage != null)
  508. {
  509. currentStage.SetNextStage(nextStage);
  510. }
  511. if (stageIndex == 0 && shaders[0] != null)
  512. {
  513. byte[] guestCodeA = shaders[0].Code;
  514. byte[] cb1DataA = shaders[0].Cb1Data;
  515. DiskCacheGpuAccessor gpuAccessorA = new DiskCacheGpuAccessor(_context, guestCodeA, cb1DataA, specState, newSpecState, counts, 0);
  516. translatorContexts[0] = DecodeGraphicsShader(gpuAccessorA, DefaultFlags | TranslationFlags.VertexA, 0);
  517. }
  518. translatorContexts[stageIndex + 1] = currentStage;
  519. nextStage = currentStage;
  520. }
  521. }
  522. List<ShaderProgram> translatedStages = new List<ShaderProgram>();
  523. for (int stageIndex = 0; stageIndex < Constants.ShaderStages; stageIndex++)
  524. {
  525. TranslatorContext currentStage = translatorContexts[stageIndex + 1];
  526. if (currentStage != null)
  527. {
  528. ShaderProgram program;
  529. byte[] guestCode = shaders[stageIndex + 1].Code;
  530. byte[] cb1Data = shaders[stageIndex + 1].Cb1Data;
  531. if (stageIndex == 0 && shaders[0] != null)
  532. {
  533. program = currentStage.Translate(translatorContexts[0]);
  534. byte[] guestCodeA = shaders[0].Code;
  535. byte[] cb1DataA = shaders[0].Cb1Data;
  536. shaders[0] = new CachedShaderStage(null, guestCodeA, cb1DataA);
  537. shaders[1] = new CachedShaderStage(program.Info, guestCode, cb1Data);
  538. }
  539. else
  540. {
  541. program = currentStage.Translate();
  542. shaders[stageIndex + 1] = new CachedShaderStage(program.Info, guestCode, cb1Data);
  543. }
  544. if (program != null)
  545. {
  546. translatedStages.Add(program);
  547. }
  548. }
  549. }
  550. _compilationQueue.Enqueue(new ProgramCompilation(translatedStages.ToArray(), shaders, newSpecState, programIndex, isCompute: false));
  551. }
  552. /// <summary>
  553. /// Recompiles a compute program from guest code.
  554. /// </summary>
  555. /// <param name="shaders">Shader stages</param>
  556. /// <param name="specState">Specialization state</param>
  557. /// <param name="programIndex">Program index</param>
  558. private void RecompileComputeFromGuestCode(CachedShaderStage[] shaders, ShaderSpecializationState specState, int programIndex)
  559. {
  560. CachedShaderStage shader = shaders[0];
  561. ResourceCounts counts = new ResourceCounts();
  562. ShaderSpecializationState newSpecState = new ShaderSpecializationState(specState.ComputeState);
  563. DiskCacheGpuAccessor gpuAccessor = new DiskCacheGpuAccessor(_context, shader.Code, shader.Cb1Data, specState, newSpecState, counts, 0);
  564. TranslatorContext translatorContext = DecodeComputeShader(gpuAccessor, 0);
  565. ShaderProgram program = translatorContext.Translate();
  566. shaders[0] = new CachedShaderStage(program.Info, shader.Code, shader.Cb1Data);
  567. _compilationQueue.Enqueue(new ProgramCompilation(new[] { program }, shaders, newSpecState, programIndex, isCompute: true));
  568. }
  569. /// <summary>
  570. /// Signals that compilation of a program has been finished successfully,
  571. /// or that it failed and guest recompilation has also been attempted.
  572. /// </summary>
  573. private void SignalCompiled()
  574. {
  575. _stateChangeCallback(ShaderCacheState.Loading, ++_compiledCount, _totalCount);
  576. }
  577. }
  578. }