ParallelDiskCacheLoader.cs 29 KB

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