ParallelDiskCacheLoader.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. using Ryujinx.Common.Configuration;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.GAL;
  4. using Ryujinx.Graphics.Shader;
  5. using Ryujinx.Graphics.Shader.Translation;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Threading;
  11. using static Ryujinx.Graphics.Gpu.Shader.ShaderCache;
  12. namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
  13. {
  14. class ParallelDiskCacheLoader
  15. {
  16. private const int ThreadCount = 8;
  17. private readonly GpuContext _context;
  18. private readonly ShaderCacheHashTable _graphicsCache;
  19. private readonly ComputeShaderCacheHashTable _computeCache;
  20. private readonly DiskCacheHostStorage _hostStorage;
  21. private readonly CancellationToken _cancellationToken;
  22. private readonly Action<ShaderCacheState, int, int> _stateChangeCallback;
  23. /// <summary>
  24. /// Indicates if the cache should be loaded.
  25. /// </summary>
  26. public bool Active => !_cancellationToken.IsCancellationRequested;
  27. private bool _needsHostRegen;
  28. /// <summary>
  29. /// Number of shaders that failed to compile from the cache.
  30. /// </summary>
  31. public int ErrorCount { get; private set; }
  32. /// <summary>
  33. /// Program validation entry.
  34. /// </summary>
  35. private readonly struct ProgramEntry
  36. {
  37. /// <summary>
  38. /// Cached shader program.
  39. /// </summary>
  40. public readonly CachedShaderProgram CachedProgram;
  41. /// <summary>
  42. /// Optional binary code. If not null, it is used instead of the backend host binary.
  43. /// </summary>
  44. public readonly byte[] BinaryCode;
  45. /// <summary>
  46. /// Program index.
  47. /// </summary>
  48. public readonly int ProgramIndex;
  49. /// <summary>
  50. /// Indicates if the program is a compute shader.
  51. /// </summary>
  52. public readonly bool IsCompute;
  53. /// <summary>
  54. /// Indicates if the program is a host binary shader.
  55. /// </summary>
  56. public readonly bool IsBinary;
  57. /// <summary>
  58. /// Creates a new program validation entry.
  59. /// </summary>
  60. /// <param name="cachedProgram">Cached shader program</param>
  61. /// <param name="binaryCode">Optional binary code. If not null, it is used instead of the backend host binary</param>
  62. /// <param name="programIndex">Program index</param>
  63. /// <param name="isCompute">Indicates if the program is a compute shader</param>
  64. /// <param name="isBinary">Indicates if the program is a host binary shader</param>
  65. public ProgramEntry(
  66. CachedShaderProgram cachedProgram,
  67. byte[] binaryCode,
  68. int programIndex,
  69. bool isCompute,
  70. bool isBinary)
  71. {
  72. CachedProgram = cachedProgram;
  73. BinaryCode = binaryCode;
  74. ProgramIndex = programIndex;
  75. IsCompute = isCompute;
  76. IsBinary = isBinary;
  77. }
  78. }
  79. /// <summary>
  80. /// Translated shader compilation entry.
  81. /// </summary>
  82. private readonly struct ProgramCompilation
  83. {
  84. /// <summary>
  85. /// Translated shader stages.
  86. /// </summary>
  87. public readonly ShaderProgram[] TranslatedStages;
  88. /// <summary>
  89. /// Cached shaders.
  90. /// </summary>
  91. public readonly CachedShaderStage[] Shaders;
  92. /// <summary>
  93. /// Specialization state.
  94. /// </summary>
  95. public readonly ShaderSpecializationState SpecializationState;
  96. /// <summary>
  97. /// Program index.
  98. /// </summary>
  99. public readonly int ProgramIndex;
  100. /// <summary>
  101. /// Indicates if the program is a compute shader.
  102. /// </summary>
  103. public readonly bool IsCompute;
  104. /// <summary>
  105. /// Creates a new translated shader compilation entry.
  106. /// </summary>
  107. /// <param name="translatedStages">Translated shader stages</param>
  108. /// <param name="shaders">Cached shaders</param>
  109. /// <param name="specState">Specialization state</param>
  110. /// <param name="programIndex">Program index</param>
  111. /// <param name="isCompute">Indicates if the program is a compute shader</param>
  112. public ProgramCompilation(
  113. ShaderProgram[] translatedStages,
  114. CachedShaderStage[] shaders,
  115. ShaderSpecializationState specState,
  116. int programIndex,
  117. bool isCompute)
  118. {
  119. TranslatedStages = translatedStages;
  120. Shaders = shaders;
  121. SpecializationState = specState;
  122. ProgramIndex = programIndex;
  123. IsCompute = isCompute;
  124. }
  125. }
  126. /// <summary>
  127. /// Program translation entry.
  128. /// </summary>
  129. private readonly struct AsyncProgramTranslation
  130. {
  131. /// <summary>
  132. /// Guest code for each active stage.
  133. /// </summary>
  134. public readonly GuestCodeAndCbData?[] GuestShaders;
  135. /// <summary>
  136. /// Specialization state.
  137. /// </summary>
  138. public readonly ShaderSpecializationState SpecializationState;
  139. /// <summary>
  140. /// Program index.
  141. /// </summary>
  142. public readonly int ProgramIndex;
  143. /// <summary>
  144. /// Indicates if the program is a compute shader.
  145. /// </summary>
  146. public readonly bool IsCompute;
  147. /// <summary>
  148. /// Creates a new program translation entry.
  149. /// </summary>
  150. /// <param name="guestShaders">Guest code for each active stage</param>
  151. /// <param name="specState">Specialization state</param>
  152. /// <param name="programIndex">Program index</param>
  153. /// <param name="isCompute">Indicates if the program is a compute shader</param>
  154. public AsyncProgramTranslation(
  155. GuestCodeAndCbData?[] guestShaders,
  156. ShaderSpecializationState specState,
  157. int programIndex,
  158. bool isCompute)
  159. {
  160. GuestShaders = guestShaders;
  161. SpecializationState = specState;
  162. ProgramIndex = programIndex;
  163. IsCompute = isCompute;
  164. }
  165. }
  166. private readonly Queue<ProgramEntry> _validationQueue;
  167. private readonly ConcurrentQueue<ProgramCompilation> _compilationQueue;
  168. private readonly BlockingCollection<AsyncProgramTranslation> _asyncTranslationQueue;
  169. private readonly SortedList<int, (CachedShaderProgram, byte[])> _programList;
  170. private readonly int _backendParallelCompileThreads;
  171. private int _compiledCount;
  172. private int _totalCount;
  173. /// <summary>
  174. /// Creates a new parallel disk cache loader.
  175. /// </summary>
  176. /// <param name="context">GPU context</param>
  177. /// <param name="graphicsCache">Graphics shader cache</param>
  178. /// <param name="computeCache">Compute shader cache</param>
  179. /// <param name="hostStorage">Disk cache host storage</param>
  180. /// <param name="stateChangeCallback">Function to be called when there is a state change, reporting state, compiled and total shaders count</param>
  181. /// <param name="cancellationToken">Cancellation token</param>
  182. public ParallelDiskCacheLoader(GpuContext context,
  183. ShaderCacheHashTable graphicsCache,
  184. ComputeShaderCacheHashTable computeCache,
  185. DiskCacheHostStorage hostStorage,
  186. Action<ShaderCacheState, int, int> stateChangeCallback,
  187. CancellationToken cancellationToken)
  188. {
  189. _context = context;
  190. _graphicsCache = graphicsCache;
  191. _computeCache = computeCache;
  192. _hostStorage = hostStorage;
  193. _stateChangeCallback = stateChangeCallback;
  194. _cancellationToken = cancellationToken;
  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. if (_context.DirtyHacks.IsEnabled(DirtyHacks.ShaderCompilationThreadSleep))
  320. Thread.Sleep(_context.DirtyHacks[DirtyHacks.ShaderCompilationThreadSleep]);
  321. AsyncProgramTranslation asyncTranslation = new(guestShaders, specState, programIndex, isCompute);
  322. _asyncTranslationQueue.Add(asyncTranslation, _cancellationToken);
  323. }
  324. catch (OperationCanceledException)
  325. {
  326. }
  327. }
  328. /// <summary>
  329. /// Check the state of programs that have already been compiled,
  330. /// and add to the cache if the compilation was successful.
  331. /// </summary>
  332. public void CheckCompilation()
  333. {
  334. ProcessCompilationQueue();
  335. // Process programs that already finished compiling.
  336. // If not yet compiled, do nothing. This avoids blocking to wait for shader compilation.
  337. while (_validationQueue.TryPeek(out ProgramEntry entry))
  338. {
  339. ProgramLinkStatus result = entry.CachedProgram.HostProgram.CheckProgramLink(false);
  340. if (result != ProgramLinkStatus.Incomplete)
  341. {
  342. ProcessCompiledProgram(ref entry, result);
  343. _validationQueue.Dequeue();
  344. }
  345. else
  346. {
  347. break;
  348. }
  349. }
  350. }
  351. /// <summary>
  352. /// Waits until all programs finishes compiling, then adds the ones
  353. /// with successful compilation to the cache.
  354. /// </summary>
  355. private void CheckCompilationBlocking()
  356. {
  357. ProcessCompilationQueue();
  358. while (_validationQueue.TryDequeue(out ProgramEntry entry) && Active)
  359. {
  360. ProcessCompiledProgram(ref entry, entry.CachedProgram.HostProgram.CheckProgramLink(true), asyncCompile: false);
  361. }
  362. }
  363. /// <summary>
  364. /// Process a compiled program result.
  365. /// </summary>
  366. /// <param name="entry">Compiled program entry</param>
  367. /// <param name="result">Compilation result</param>
  368. /// <param name="asyncCompile">For failed host compilations, indicates if a guest compilation should be done asynchronously</param>
  369. private void ProcessCompiledProgram(ref ProgramEntry entry, ProgramLinkStatus result, bool asyncCompile = true)
  370. {
  371. if (result == ProgramLinkStatus.Success)
  372. {
  373. // Compilation successful, add to memory cache.
  374. if (entry.IsCompute)
  375. {
  376. _computeCache.Add(entry.CachedProgram);
  377. }
  378. else
  379. {
  380. _graphicsCache.Add(entry.CachedProgram);
  381. }
  382. if (!entry.IsBinary)
  383. {
  384. _needsHostRegen = true;
  385. }
  386. // Fetch the binary code from the backend if it isn't already present.
  387. byte[] binaryCode = entry.BinaryCode ?? entry.CachedProgram.HostProgram.GetBinary();
  388. _programList.Add(entry.ProgramIndex, (entry.CachedProgram, binaryCode));
  389. SignalCompiled();
  390. }
  391. else if (entry.IsBinary)
  392. {
  393. // If this is a host binary and compilation failed,
  394. // we still have a chance to recompile from the guest binary.
  395. CachedShaderProgram program = entry.CachedProgram;
  396. GuestCodeAndCbData?[] guestShaders = new GuestCodeAndCbData?[program.Shaders.Length];
  397. for (int index = 0; index < program.Shaders.Length; index++)
  398. {
  399. CachedShaderStage shader = program.Shaders[index];
  400. if (shader != null)
  401. {
  402. guestShaders[index] = new GuestCodeAndCbData(shader.Code, shader.Cb1Data);
  403. }
  404. }
  405. if (asyncCompile)
  406. {
  407. QueueGuestProgram(guestShaders, program.SpecializationState, entry.ProgramIndex, entry.IsCompute);
  408. }
  409. else
  410. {
  411. RecompileFromGuestCode(guestShaders, program.SpecializationState, entry.ProgramIndex, entry.IsCompute);
  412. ProcessCompilationQueue();
  413. }
  414. }
  415. else
  416. {
  417. // Failed to compile from both host and guest binary.
  418. ErrorCount++;
  419. SignalCompiled();
  420. }
  421. }
  422. /// <summary>
  423. /// Processes the queue of translated guest programs that should be compiled on the host.
  424. /// </summary>
  425. private void ProcessCompilationQueue()
  426. {
  427. while (_compilationQueue.TryDequeue(out ProgramCompilation compilation) && Active)
  428. {
  429. ShaderSource[] shaderSources = new ShaderSource[compilation.TranslatedStages.Length];
  430. ref GpuChannelComputeState computeState = ref compilation.SpecializationState.ComputeState;
  431. ShaderInfoBuilder shaderInfoBuilder = new(
  432. _context,
  433. compilation.SpecializationState.TransformFeedbackDescriptors != null,
  434. computeLocalSize: computeState.GetLocalSize());
  435. for (int index = 0; index < compilation.TranslatedStages.Length; index++)
  436. {
  437. ShaderProgram shader = compilation.TranslatedStages[index];
  438. shaderSources[index] = CreateShaderSource(shader);
  439. shaderInfoBuilder.AddStageInfo(shader.Info);
  440. }
  441. ShaderInfo shaderInfo = shaderInfoBuilder.Build(compilation.SpecializationState.PipelineState, fromCache: true);
  442. IProgram hostProgram = _context.Renderer.CreateProgram(shaderSources, shaderInfo);
  443. CachedShaderProgram program = new(hostProgram, compilation.SpecializationState, compilation.Shaders);
  444. // Vulkan's binary code is the SPIR-V used for compilation, so it is ready immediately. Other APIs get this after compilation.
  445. byte[] binaryCode = _context.Capabilities.Api == TargetApi.Vulkan ? ShaderBinarySerializer.Pack(shaderSources) : null;
  446. EnqueueForValidation(new ProgramEntry(program, binaryCode, compilation.ProgramIndex, compilation.IsCompute, isBinary: false));
  447. }
  448. }
  449. /// <summary>
  450. /// Enqueues a program for validation, which will check if the program was compiled successfully.
  451. /// </summary>
  452. /// <param name="newEntry">Program entry to be validated</param>
  453. private void EnqueueForValidation(ProgramEntry newEntry)
  454. {
  455. _validationQueue.Enqueue(newEntry);
  456. // Do not allow more than N shader compilation in-flight, where N is the maximum number of threads
  457. // the driver will be using for parallel compilation.
  458. // Submitting more seems to cause NVIDIA OpenGL driver to crash.
  459. if (_validationQueue.Count >= _backendParallelCompileThreads && _validationQueue.TryDequeue(out ProgramEntry entry))
  460. {
  461. ProcessCompiledProgram(ref entry, entry.CachedProgram.HostProgram.CheckProgramLink(true), asyncCompile: false);
  462. }
  463. }
  464. /// <summary>
  465. /// Processses the queue of programs that should be translated from guest code.
  466. /// </summary>
  467. /// <param name="state">Cancellation token</param>
  468. private void ProcessAsyncQueue(object state)
  469. {
  470. CancellationToken ct = (CancellationToken)state;
  471. try
  472. {
  473. foreach (AsyncProgramTranslation asyncCompilation in _asyncTranslationQueue.GetConsumingEnumerable(ct))
  474. {
  475. RecompileFromGuestCode(
  476. asyncCompilation.GuestShaders,
  477. asyncCompilation.SpecializationState,
  478. asyncCompilation.ProgramIndex,
  479. asyncCompilation.IsCompute);
  480. }
  481. }
  482. catch (OperationCanceledException)
  483. {
  484. }
  485. }
  486. /// <summary>
  487. /// Recompiles a program from guest code.
  488. /// </summary>
  489. /// <param name="guestShaders">Guest code for each active stage</param>
  490. /// <param name="specState">Specialization state</param>
  491. /// <param name="programIndex">Program index</param>
  492. /// <param name="isCompute">Indicates if the program is a compute shader</param>
  493. private void RecompileFromGuestCode(GuestCodeAndCbData?[] guestShaders, ShaderSpecializationState specState, int programIndex, bool isCompute)
  494. {
  495. try
  496. {
  497. if (isCompute)
  498. {
  499. RecompileComputeFromGuestCode(guestShaders, specState, programIndex);
  500. }
  501. else
  502. {
  503. RecompileGraphicsFromGuestCode(guestShaders, specState, programIndex);
  504. }
  505. }
  506. catch (Exception exception)
  507. {
  508. Logger.Error?.Print(LogClass.Gpu, $"Error translating guest shader. {exception.Message}");
  509. ErrorCount++;
  510. SignalCompiled();
  511. }
  512. }
  513. /// <summary>
  514. /// Recompiles a graphics program from guest code.
  515. /// </summary>
  516. /// <param name="guestShaders">Guest code for each active stage</param>
  517. /// <param name="specState">Specialization state</param>
  518. /// <param name="programIndex">Program index</param>
  519. private void RecompileGraphicsFromGuestCode(GuestCodeAndCbData?[] guestShaders, ShaderSpecializationState specState, int programIndex)
  520. {
  521. ShaderSpecializationState newSpecState = new(
  522. ref specState.GraphicsState,
  523. specState.PipelineState,
  524. specState.TransformFeedbackDescriptors);
  525. ResourceCounts counts = new();
  526. DiskCacheGpuAccessor[] gpuAccessors = new DiskCacheGpuAccessor[Constants.ShaderStages];
  527. TranslatorContext[] translatorContexts = new TranslatorContext[Constants.ShaderStages + 1];
  528. TranslatorContext nextStage = null;
  529. TargetApi api = _context.Capabilities.Api;
  530. bool hasCachedGs = guestShaders[4].HasValue;
  531. for (int stageIndex = Constants.ShaderStages - 1; stageIndex >= 0; stageIndex--)
  532. {
  533. if (guestShaders[stageIndex + 1].HasValue)
  534. {
  535. GuestCodeAndCbData shader = guestShaders[stageIndex + 1].Value;
  536. byte[] guestCode = shader.Code;
  537. byte[] cb1Data = shader.Cb1Data;
  538. DiskCacheGpuAccessor gpuAccessor = new(_context, guestCode, cb1Data, specState, newSpecState, counts, stageIndex, hasCachedGs);
  539. TranslatorContext currentStage = DecodeGraphicsShader(gpuAccessor, api, DefaultFlags, 0);
  540. if (nextStage != null)
  541. {
  542. currentStage.SetNextStage(nextStage);
  543. }
  544. if (stageIndex == 0 && guestShaders[0].HasValue)
  545. {
  546. byte[] guestCodeA = guestShaders[0].Value.Code;
  547. byte[] cb1DataA = guestShaders[0].Value.Cb1Data;
  548. DiskCacheGpuAccessor gpuAccessorA = new(_context, guestCodeA, cb1DataA, specState, newSpecState, counts, 0, hasCachedGs);
  549. translatorContexts[0] = DecodeGraphicsShader(gpuAccessorA, api, DefaultFlags | TranslationFlags.VertexA, 0);
  550. }
  551. gpuAccessors[stageIndex] = gpuAccessor;
  552. translatorContexts[stageIndex + 1] = currentStage;
  553. nextStage = currentStage;
  554. }
  555. }
  556. bool hasGeometryShader = translatorContexts[4] != null;
  557. bool vertexHasStore = translatorContexts[1] != null && translatorContexts[1].HasStore;
  558. bool geometryHasStore = hasGeometryShader && translatorContexts[4].HasStore;
  559. bool vertexToCompute = ShouldConvertVertexToCompute(_context, vertexHasStore, geometryHasStore, hasGeometryShader);
  560. // We don't support caching shader stages that have been converted to compute currently,
  561. // so just eliminate them if they exist in the cache.
  562. if (vertexToCompute)
  563. {
  564. return;
  565. }
  566. CachedShaderStage[] shaders = new CachedShaderStage[guestShaders.Length];
  567. List<ShaderProgram> translatedStages = new();
  568. TranslatorContext previousStage = null;
  569. for (int stageIndex = 0; stageIndex < Constants.ShaderStages; stageIndex++)
  570. {
  571. TranslatorContext currentStage = translatorContexts[stageIndex + 1];
  572. if (currentStage != null)
  573. {
  574. gpuAccessors[stageIndex].InitializeReservedCounts(specState.TransformFeedbackDescriptors != null, vertexToCompute);
  575. ShaderProgram program;
  576. byte[] guestCode = guestShaders[stageIndex + 1].Value.Code;
  577. byte[] cb1Data = guestShaders[stageIndex + 1].Value.Cb1Data;
  578. if (stageIndex == 0 && guestShaders[0].HasValue)
  579. {
  580. program = currentStage.Translate(translatorContexts[0]);
  581. byte[] guestCodeA = guestShaders[0].Value.Code;
  582. byte[] cb1DataA = guestShaders[0].Value.Cb1Data;
  583. shaders[0] = new CachedShaderStage(null, guestCodeA, cb1DataA);
  584. shaders[1] = new CachedShaderStage(program.Info, guestCode, cb1Data);
  585. }
  586. else
  587. {
  588. program = currentStage.Translate();
  589. shaders[stageIndex + 1] = new CachedShaderStage(program.Info, guestCode, cb1Data);
  590. }
  591. if (program != null)
  592. {
  593. translatedStages.Add(program);
  594. }
  595. previousStage = currentStage;
  596. }
  597. else if (
  598. previousStage != null &&
  599. previousStage.LayerOutputWritten &&
  600. stageIndex == 3 &&
  601. !_context.Capabilities.SupportsLayerVertexTessellation)
  602. {
  603. translatedStages.Add(previousStage.GenerateGeometryPassthrough());
  604. }
  605. }
  606. _compilationQueue.Enqueue(new ProgramCompilation(translatedStages.ToArray(), shaders, newSpecState, programIndex, isCompute: false));
  607. }
  608. /// <summary>
  609. /// Recompiles a compute program from guest code.
  610. /// </summary>
  611. /// <param name="guestShaders">Guest code for each active stage</param>
  612. /// <param name="specState">Specialization state</param>
  613. /// <param name="programIndex">Program index</param>
  614. private void RecompileComputeFromGuestCode(GuestCodeAndCbData?[] guestShaders, ShaderSpecializationState specState, int programIndex)
  615. {
  616. GuestCodeAndCbData shader = guestShaders[0].Value;
  617. ResourceCounts counts = new();
  618. ShaderSpecializationState newSpecState = new(ref specState.ComputeState);
  619. DiskCacheGpuAccessor gpuAccessor = new(_context, shader.Code, shader.Cb1Data, specState, newSpecState, counts, 0, false);
  620. gpuAccessor.InitializeReservedCounts(tfEnabled: false, vertexAsCompute: false);
  621. TranslatorContext translatorContext = DecodeComputeShader(gpuAccessor, _context.Capabilities.Api, 0);
  622. ShaderProgram program = translatorContext.Translate();
  623. CachedShaderStage[] shaders = new[] { new CachedShaderStage(program.Info, shader.Code, shader.Cb1Data) };
  624. _compilationQueue.Enqueue(new ProgramCompilation(new[] { program }, shaders, newSpecState, programIndex, isCompute: true));
  625. }
  626. /// <summary>
  627. /// Signals that compilation of a program has been finished successfully,
  628. /// or that it failed and guest recompilation has also been attempted.
  629. /// </summary>
  630. private void SignalCompiled()
  631. {
  632. _stateChangeCallback(ShaderCacheState.Loading, ++_compiledCount, _totalCount);
  633. }
  634. }
  635. }