ParallelDiskCacheLoader.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 readonly struct ProgramEntry
  35. {
  36. /// <summary>
  37. /// Cached shader program.
  38. /// </summary>
  39. public readonly CachedShaderProgram CachedProgram;
  40. /// <summary>
  41. /// Optional binary code. If not null, it is used instead of the backend host binary.
  42. /// </summary>
  43. public readonly byte[] BinaryCode;
  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="binaryCode">Optional binary code. If not null, it is used instead of the backend host binary</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. byte[] binaryCode,
  67. int programIndex,
  68. bool isCompute,
  69. bool isBinary)
  70. {
  71. CachedProgram = cachedProgram;
  72. BinaryCode = binaryCode;
  73. ProgramIndex = programIndex;
  74. IsCompute = isCompute;
  75. IsBinary = isBinary;
  76. }
  77. }
  78. /// <summary>
  79. /// Translated shader compilation entry.
  80. /// </summary>
  81. private readonly 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 readonly struct AsyncProgramTranslation
  129. {
  130. /// <summary>
  131. /// Guest code for each active stage.
  132. /// </summary>
  133. public readonly GuestCodeAndCbData?[] GuestShaders;
  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="guestShaders">Guest code for each active stage</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. GuestCodeAndCbData?[] guestShaders,
  155. ShaderSpecializationState specState,
  156. int programIndex,
  157. bool isCompute)
  158. {
  159. GuestShaders = guestShaders;
  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, byte[])> _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, byte[])>();
  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 && Active)
  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, byte[] binaryCode) = kv.Value;
  272. _hostStorage.AddShader(_context, program, binaryCode, 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="binaryCode">Host binary code</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, byte[] binaryCode, int programIndex, bool isCompute)
  302. {
  303. EnqueueForValidation(new ProgramEntry(cachedProgram, binaryCode, programIndex, isCompute, isBinary: true));
  304. }
  305. /// <summary>
  306. /// Enqueues a guest program for compilation.
  307. /// </summary>
  308. /// <param name="guestShaders">Guest code for each active stage</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(GuestCodeAndCbData?[] guestShaders, ShaderSpecializationState specState, int programIndex, bool isCompute)
  313. {
  314. try
  315. {
  316. AsyncProgramTranslation asyncTranslation = new AsyncProgramTranslation(guestShaders, specState, programIndex, isCompute);
  317. _asyncTranslationQueue.Add(asyncTranslation, _cancellationToken);
  318. }
  319. catch (OperationCanceledException)
  320. {
  321. }
  322. }
  323. /// <summary>
  324. /// Check the state of programs that have already been compiled,
  325. /// and add to the cache if the compilation was successful.
  326. /// </summary>
  327. public void CheckCompilation()
  328. {
  329. ProcessCompilationQueue();
  330. // Process programs that already finished compiling.
  331. // If not yet compiled, do nothing. This avoids blocking to wait for shader compilation.
  332. while (_validationQueue.TryPeek(out ProgramEntry entry))
  333. {
  334. ProgramLinkStatus result = entry.CachedProgram.HostProgram.CheckProgramLink(false);
  335. if (result != ProgramLinkStatus.Incomplete)
  336. {
  337. ProcessCompiledProgram(ref entry, result);
  338. _validationQueue.Dequeue();
  339. }
  340. else
  341. {
  342. break;
  343. }
  344. }
  345. }
  346. /// <summary>
  347. /// Waits until all programs finishes compiling, then adds the ones
  348. /// with successful compilation to the cache.
  349. /// </summary>
  350. private void CheckCompilationBlocking()
  351. {
  352. ProcessCompilationQueue();
  353. while (_validationQueue.TryDequeue(out ProgramEntry entry) && Active)
  354. {
  355. ProcessCompiledProgram(ref entry, entry.CachedProgram.HostProgram.CheckProgramLink(true), asyncCompile: false);
  356. }
  357. }
  358. /// <summary>
  359. /// Process a compiled program result.
  360. /// </summary>
  361. /// <param name="entry">Compiled program entry</param>
  362. /// <param name="result">Compilation result</param>
  363. /// <param name="asyncCompile">For failed host compilations, indicates if a guest compilation should be done asynchronously</param>
  364. private void ProcessCompiledProgram(ref ProgramEntry entry, ProgramLinkStatus result, bool asyncCompile = true)
  365. {
  366. if (result == ProgramLinkStatus.Success)
  367. {
  368. // Compilation successful, add to memory cache.
  369. if (entry.IsCompute)
  370. {
  371. _computeCache.Add(entry.CachedProgram);
  372. }
  373. else
  374. {
  375. _graphicsCache.Add(entry.CachedProgram);
  376. }
  377. if (!entry.IsBinary)
  378. {
  379. _needsHostRegen = true;
  380. }
  381. // Fetch the binary code from the backend if it isn't already present.
  382. byte[] binaryCode = entry.BinaryCode ?? entry.CachedProgram.HostProgram.GetBinary();
  383. _programList.Add(entry.ProgramIndex, (entry.CachedProgram, binaryCode));
  384. SignalCompiled();
  385. }
  386. else if (entry.IsBinary)
  387. {
  388. // If this is a host binary and compilation failed,
  389. // we still have a chance to recompile from the guest binary.
  390. CachedShaderProgram program = entry.CachedProgram;
  391. GuestCodeAndCbData?[] guestShaders = new GuestCodeAndCbData?[program.Shaders.Length];
  392. for (int index = 0; index < program.Shaders.Length; index++)
  393. {
  394. CachedShaderStage shader = program.Shaders[index];
  395. if (shader != null)
  396. {
  397. guestShaders[index] = new GuestCodeAndCbData(shader.Code, shader.Cb1Data);
  398. }
  399. }
  400. if (asyncCompile)
  401. {
  402. QueueGuestProgram(guestShaders, program.SpecializationState, entry.ProgramIndex, entry.IsCompute);
  403. }
  404. else
  405. {
  406. RecompileFromGuestCode(guestShaders, program.SpecializationState, entry.ProgramIndex, entry.IsCompute);
  407. ProcessCompilationQueue();
  408. }
  409. }
  410. else
  411. {
  412. // Failed to compile from both host and guest binary.
  413. ErrorCount++;
  414. SignalCompiled();
  415. }
  416. }
  417. /// <summary>
  418. /// Processes the queue of translated guest programs that should be compiled on the host.
  419. /// </summary>
  420. private void ProcessCompilationQueue()
  421. {
  422. while (_compilationQueue.TryDequeue(out ProgramCompilation compilation) && Active)
  423. {
  424. ShaderSource[] shaderSources = new ShaderSource[compilation.TranslatedStages.Length];
  425. int fragmentOutputMap = -1;
  426. for (int index = 0; index < compilation.TranslatedStages.Length; index++)
  427. {
  428. ShaderProgram shader = compilation.TranslatedStages[index];
  429. shaderSources[index] = CreateShaderSource(shader);
  430. if (shader.Info.Stage == ShaderStage.Fragment)
  431. {
  432. fragmentOutputMap = shader.Info.FragmentOutputMap;
  433. }
  434. }
  435. ShaderInfo shaderInfo = compilation.SpecializationState.PipelineState.HasValue
  436. ? new ShaderInfo(fragmentOutputMap, compilation.SpecializationState.PipelineState.Value, fromCache: true)
  437. : new ShaderInfo(fragmentOutputMap, fromCache: true);
  438. IProgram hostProgram = _context.Renderer.CreateProgram(shaderSources, shaderInfo);
  439. CachedShaderProgram program = new CachedShaderProgram(hostProgram, compilation.SpecializationState, compilation.Shaders);
  440. // Vulkan's binary code is the SPIR-V used for compilation, so it is ready immediately. Other APIs get this after compilation.
  441. byte[] binaryCode = _context.Capabilities.Api == TargetApi.Vulkan ? ShaderBinarySerializer.Pack(shaderSources) : null;
  442. EnqueueForValidation(new ProgramEntry(program, binaryCode, compilation.ProgramIndex, compilation.IsCompute, isBinary: false));
  443. }
  444. }
  445. /// <summary>
  446. /// Enqueues a program for validation, which will check if the program was compiled successfully.
  447. /// </summary>
  448. /// <param name="newEntry">Program entry to be validated</param>
  449. private void EnqueueForValidation(ProgramEntry newEntry)
  450. {
  451. _validationQueue.Enqueue(newEntry);
  452. // Do not allow more than N shader compilation in-flight, where N is the maximum number of threads
  453. // the driver will be using for parallel compilation.
  454. // Submitting more seems to cause NVIDIA OpenGL driver to crash.
  455. if (_validationQueue.Count >= _backendParallelCompileThreads && _validationQueue.TryDequeue(out ProgramEntry entry))
  456. {
  457. ProcessCompiledProgram(ref entry, entry.CachedProgram.HostProgram.CheckProgramLink(true), asyncCompile: false);
  458. }
  459. }
  460. /// <summary>
  461. /// Processses the queue of programs that should be translated from guest code.
  462. /// </summary>
  463. /// <param name="state">Cancellation token</param>
  464. private void ProcessAsyncQueue(object state)
  465. {
  466. CancellationToken ct = (CancellationToken)state;
  467. try
  468. {
  469. foreach (AsyncProgramTranslation asyncCompilation in _asyncTranslationQueue.GetConsumingEnumerable(ct))
  470. {
  471. RecompileFromGuestCode(
  472. asyncCompilation.GuestShaders,
  473. asyncCompilation.SpecializationState,
  474. asyncCompilation.ProgramIndex,
  475. asyncCompilation.IsCompute);
  476. }
  477. }
  478. catch (OperationCanceledException)
  479. {
  480. }
  481. }
  482. /// <summary>
  483. /// Recompiles a program from guest code.
  484. /// </summary>
  485. /// <param name="guestShaders">Guest code for each active stage</param>
  486. /// <param name="specState">Specialization state</param>
  487. /// <param name="programIndex">Program index</param>
  488. /// <param name="isCompute">Indicates if the program is a compute shader</param>
  489. private void RecompileFromGuestCode(GuestCodeAndCbData?[] guestShaders, ShaderSpecializationState specState, int programIndex, bool isCompute)
  490. {
  491. try
  492. {
  493. if (isCompute)
  494. {
  495. RecompileComputeFromGuestCode(guestShaders, specState, programIndex);
  496. }
  497. else
  498. {
  499. RecompileGraphicsFromGuestCode(guestShaders, specState, programIndex);
  500. }
  501. }
  502. catch (Exception exception)
  503. {
  504. Logger.Error?.Print(LogClass.Gpu, $"Error translating guest shader. {exception.Message}");
  505. ErrorCount++;
  506. SignalCompiled();
  507. }
  508. }
  509. /// <summary>
  510. /// Recompiles a graphics program from guest code.
  511. /// </summary>
  512. /// <param name="guestShaders">Guest code for each active stage</param>
  513. /// <param name="specState">Specialization state</param>
  514. /// <param name="programIndex">Program index</param>
  515. private void RecompileGraphicsFromGuestCode(GuestCodeAndCbData?[] guestShaders, ShaderSpecializationState specState, int programIndex)
  516. {
  517. ShaderSpecializationState newSpecState = new ShaderSpecializationState(
  518. ref specState.GraphicsState,
  519. specState.PipelineState,
  520. specState.TransformFeedbackDescriptors);
  521. ResourceCounts counts = new ResourceCounts();
  522. TranslatorContext[] translatorContexts = new TranslatorContext[Constants.ShaderStages + 1];
  523. TranslatorContext nextStage = null;
  524. TargetApi api = _context.Capabilities.Api;
  525. for (int stageIndex = Constants.ShaderStages - 1; stageIndex >= 0; stageIndex--)
  526. {
  527. if (guestShaders[stageIndex + 1].HasValue)
  528. {
  529. GuestCodeAndCbData shader = guestShaders[stageIndex + 1].Value;
  530. byte[] guestCode = shader.Code;
  531. byte[] cb1Data = shader.Cb1Data;
  532. DiskCacheGpuAccessor gpuAccessor = new DiskCacheGpuAccessor(_context, guestCode, cb1Data, specState, newSpecState, counts, stageIndex);
  533. TranslatorContext currentStage = DecodeGraphicsShader(gpuAccessor, api, DefaultFlags, 0);
  534. if (nextStage != null)
  535. {
  536. currentStage.SetNextStage(nextStage);
  537. }
  538. if (stageIndex == 0 && guestShaders[0].HasValue)
  539. {
  540. byte[] guestCodeA = guestShaders[0].Value.Code;
  541. byte[] cb1DataA = guestShaders[0].Value.Cb1Data;
  542. DiskCacheGpuAccessor gpuAccessorA = new DiskCacheGpuAccessor(_context, guestCodeA, cb1DataA, specState, newSpecState, counts, 0);
  543. translatorContexts[0] = DecodeGraphicsShader(gpuAccessorA, api, DefaultFlags | TranslationFlags.VertexA, 0);
  544. }
  545. translatorContexts[stageIndex + 1] = currentStage;
  546. nextStage = currentStage;
  547. }
  548. }
  549. CachedShaderStage[] shaders = new CachedShaderStage[guestShaders.Length];
  550. List<ShaderProgram> translatedStages = new List<ShaderProgram>();
  551. TranslatorContext previousStage = null;
  552. for (int stageIndex = 0; stageIndex < Constants.ShaderStages; stageIndex++)
  553. {
  554. TranslatorContext currentStage = translatorContexts[stageIndex + 1];
  555. if (currentStage != null)
  556. {
  557. ShaderProgram program;
  558. byte[] guestCode = guestShaders[stageIndex + 1].Value.Code;
  559. byte[] cb1Data = guestShaders[stageIndex + 1].Value.Cb1Data;
  560. if (stageIndex == 0 && guestShaders[0].HasValue)
  561. {
  562. program = currentStage.Translate(translatorContexts[0]);
  563. byte[] guestCodeA = guestShaders[0].Value.Code;
  564. byte[] cb1DataA = guestShaders[0].Value.Cb1Data;
  565. shaders[0] = new CachedShaderStage(null, guestCodeA, cb1DataA);
  566. shaders[1] = new CachedShaderStage(program.Info, guestCode, cb1Data);
  567. }
  568. else
  569. {
  570. program = currentStage.Translate();
  571. shaders[stageIndex + 1] = new CachedShaderStage(program.Info, guestCode, cb1Data);
  572. }
  573. if (program != null)
  574. {
  575. translatedStages.Add(program);
  576. }
  577. previousStage = currentStage;
  578. }
  579. else if (
  580. previousStage != null &&
  581. previousStage.LayerOutputWritten &&
  582. stageIndex == 3 &&
  583. !_context.Capabilities.SupportsLayerVertexTessellation)
  584. {
  585. translatedStages.Add(previousStage.GenerateGeometryPassthrough());
  586. }
  587. }
  588. _compilationQueue.Enqueue(new ProgramCompilation(translatedStages.ToArray(), shaders, newSpecState, programIndex, isCompute: false));
  589. }
  590. /// <summary>
  591. /// Recompiles a compute program from guest code.
  592. /// </summary>
  593. /// <param name="guestShaders">Guest code for each active stage</param>
  594. /// <param name="specState">Specialization state</param>
  595. /// <param name="programIndex">Program index</param>
  596. private void RecompileComputeFromGuestCode(GuestCodeAndCbData?[] guestShaders, ShaderSpecializationState specState, int programIndex)
  597. {
  598. GuestCodeAndCbData shader = guestShaders[0].Value;
  599. ResourceCounts counts = new ResourceCounts();
  600. ShaderSpecializationState newSpecState = new ShaderSpecializationState(ref specState.ComputeState);
  601. DiskCacheGpuAccessor gpuAccessor = new DiskCacheGpuAccessor(_context, shader.Code, shader.Cb1Data, specState, newSpecState, counts, 0);
  602. TranslatorContext translatorContext = DecodeComputeShader(gpuAccessor, _context.Capabilities.Api, 0);
  603. ShaderProgram program = translatorContext.Translate();
  604. CachedShaderStage[] shaders = new[] { new CachedShaderStage(program.Info, shader.Code, shader.Cb1Data) };
  605. _compilationQueue.Enqueue(new ProgramCompilation(new[] { program }, shaders, newSpecState, programIndex, isCompute: true));
  606. }
  607. /// <summary>
  608. /// Signals that compilation of a program has been finished successfully,
  609. /// or that it failed and guest recompilation has also been attempted.
  610. /// </summary>
  611. private void SignalCompiled()
  612. {
  613. _stateChangeCallback(ShaderCacheState.Loading, ++_compiledCount, _totalCount);
  614. }
  615. }
  616. }