ShaderCache.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Configuration;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.Graphics.GAL;
  5. using Ryujinx.Graphics.Gpu.Engine.Threed;
  6. using Ryujinx.Graphics.Gpu.Engine.Types;
  7. using Ryujinx.Graphics.Gpu.Image;
  8. using Ryujinx.Graphics.Gpu.Memory;
  9. using Ryujinx.Graphics.Gpu.Shader.DiskCache;
  10. using Ryujinx.Graphics.Shader;
  11. using Ryujinx.Graphics.Shader.Translation;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Threading;
  16. namespace Ryujinx.Graphics.Gpu.Shader
  17. {
  18. /// <summary>
  19. /// Memory cache of shader code.
  20. /// </summary>
  21. class ShaderCache : IDisposable
  22. {
  23. /// <summary>
  24. /// Default flags used on the shader translation process.
  25. /// </summary>
  26. public const TranslationFlags DefaultFlags = TranslationFlags.DebugMode;
  27. private readonly struct TranslatedShader
  28. {
  29. public readonly CachedShaderStage Shader;
  30. public readonly ShaderProgram Program;
  31. public TranslatedShader(CachedShaderStage shader, ShaderProgram program)
  32. {
  33. Shader = shader;
  34. Program = program;
  35. }
  36. }
  37. private readonly struct TranslatedShaderVertexPair
  38. {
  39. public readonly CachedShaderStage VertexA;
  40. public readonly CachedShaderStage VertexB;
  41. public readonly ShaderProgram Program;
  42. public TranslatedShaderVertexPair(CachedShaderStage vertexA, CachedShaderStage vertexB, ShaderProgram program)
  43. {
  44. VertexA = vertexA;
  45. VertexB = vertexB;
  46. Program = program;
  47. }
  48. }
  49. private readonly GpuContext _context;
  50. private readonly ShaderDumper _dumper;
  51. private readonly Dictionary<ulong, CachedShaderProgram> _cpPrograms;
  52. private readonly Dictionary<ShaderAddresses, CachedShaderProgram> _gpPrograms;
  53. private readonly struct ProgramToSave
  54. {
  55. public readonly CachedShaderProgram CachedProgram;
  56. public readonly IProgram HostProgram;
  57. public readonly byte[] BinaryCode;
  58. public ProgramToSave(CachedShaderProgram cachedProgram, IProgram hostProgram, byte[] binaryCode)
  59. {
  60. CachedProgram = cachedProgram;
  61. HostProgram = hostProgram;
  62. BinaryCode = binaryCode;
  63. }
  64. }
  65. private readonly Queue<ProgramToSave> _programsToSaveQueue;
  66. private readonly ComputeShaderCacheHashTable _computeShaderCache;
  67. private readonly ShaderCacheHashTable _graphicsShaderCache;
  68. private readonly DiskCacheHostStorage _diskCacheHostStorage;
  69. private readonly BackgroundDiskCacheWriter _cacheWriter;
  70. /// <summary>
  71. /// Event for signalling shader cache loading progress.
  72. /// </summary>
  73. public event Action<ShaderCacheState, int, int> ShaderCacheStateChanged;
  74. /// <summary>
  75. /// Creates a new instance of the shader cache.
  76. /// </summary>
  77. /// <param name="context">GPU context that the shader cache belongs to</param>
  78. public ShaderCache(GpuContext context)
  79. {
  80. _context = context;
  81. _dumper = new ShaderDumper();
  82. _cpPrograms = new Dictionary<ulong, CachedShaderProgram>();
  83. _gpPrograms = new Dictionary<ShaderAddresses, CachedShaderProgram>();
  84. _programsToSaveQueue = new Queue<ProgramToSave>();
  85. string diskCacheTitleId = GetDiskCachePath();
  86. _computeShaderCache = new ComputeShaderCacheHashTable();
  87. _graphicsShaderCache = new ShaderCacheHashTable();
  88. _diskCacheHostStorage = new DiskCacheHostStorage(diskCacheTitleId);
  89. if (_diskCacheHostStorage.CacheEnabled)
  90. {
  91. _cacheWriter = new BackgroundDiskCacheWriter(context, _diskCacheHostStorage);
  92. }
  93. }
  94. /// <summary>
  95. /// Gets the path where the disk cache for the current application is stored.
  96. /// </summary>
  97. private static string GetDiskCachePath()
  98. {
  99. return GraphicsConfig.EnableShaderCache && GraphicsConfig.TitleId != null
  100. ? Path.Combine(AppDataManager.GamesDirPath, GraphicsConfig.TitleId.ToLower(), "cache", "shader")
  101. : null;
  102. }
  103. /// <summary>
  104. /// Processes the queue of shaders that must save their binaries to the disk cache.
  105. /// </summary>
  106. public void ProcessShaderCacheQueue()
  107. {
  108. // Check to see if the binaries for previously compiled shaders are ready, and save them out.
  109. while (_programsToSaveQueue.TryPeek(out ProgramToSave programToSave))
  110. {
  111. ProgramLinkStatus result = programToSave.HostProgram.CheckProgramLink(false);
  112. if (result != ProgramLinkStatus.Incomplete)
  113. {
  114. if (result == ProgramLinkStatus.Success)
  115. {
  116. _cacheWriter.AddShader(programToSave.CachedProgram, programToSave.BinaryCode ?? programToSave.HostProgram.GetBinary());
  117. }
  118. _programsToSaveQueue.Dequeue();
  119. }
  120. else
  121. {
  122. break;
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// Initialize the cache.
  128. /// </summary>
  129. /// <param name="cancellationToken">Cancellation token to cancel the shader cache initialization process</param>
  130. internal void Initialize(CancellationToken cancellationToken)
  131. {
  132. if (_diskCacheHostStorage.CacheEnabled)
  133. {
  134. ParallelDiskCacheLoader loader = new(
  135. _context,
  136. _graphicsShaderCache,
  137. _computeShaderCache,
  138. _diskCacheHostStorage,
  139. ShaderCacheStateUpdate,
  140. cancellationToken);
  141. loader.LoadShaders();
  142. int errorCount = loader.ErrorCount;
  143. if (errorCount != 0)
  144. {
  145. Logger.Warning?.Print(LogClass.Gpu, $"Failed to load {errorCount} shaders from the disk cache.");
  146. }
  147. }
  148. }
  149. /// <summary>
  150. /// Shader cache state update handler.
  151. /// </summary>
  152. /// <param name="state">Current state of the shader cache load process</param>
  153. /// <param name="current">Number of the current shader being processed</param>
  154. /// <param name="total">Total number of shaders to process</param>
  155. private void ShaderCacheStateUpdate(ShaderCacheState state, int current, int total)
  156. {
  157. ShaderCacheStateChanged?.Invoke(state, current, total);
  158. }
  159. /// <summary>
  160. /// Gets a compute shader from the cache.
  161. /// </summary>
  162. /// <remarks>
  163. /// This automatically translates, compiles and adds the code to the cache if not present.
  164. /// </remarks>
  165. /// <param name="channel">GPU channel</param>
  166. /// <param name="samplerPoolMaximumId">Maximum ID that an entry in the sampler pool may have</param>
  167. /// <param name="poolState">Texture pool state</param>
  168. /// <param name="computeState">Compute engine state</param>
  169. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  170. /// <returns>Compiled compute shader code</returns>
  171. public CachedShaderProgram GetComputeShader(
  172. GpuChannel channel,
  173. int samplerPoolMaximumId,
  174. GpuChannelPoolState poolState,
  175. GpuChannelComputeState computeState,
  176. ulong gpuVa)
  177. {
  178. if (_cpPrograms.TryGetValue(gpuVa, out var cpShader) && IsShaderEqual(channel, poolState, computeState, cpShader, gpuVa))
  179. {
  180. return cpShader;
  181. }
  182. if (_computeShaderCache.TryFind(channel, poolState, computeState, gpuVa, out cpShader, out byte[] cachedGuestCode))
  183. {
  184. _cpPrograms[gpuVa] = cpShader;
  185. return cpShader;
  186. }
  187. ShaderSpecializationState specState = new(ref computeState);
  188. GpuAccessorState gpuAccessorState = new(samplerPoolMaximumId, poolState, computeState, default, specState);
  189. GpuAccessor gpuAccessor = new(_context, channel, gpuAccessorState);
  190. gpuAccessor.InitializeReservedCounts(tfEnabled: false, vertexAsCompute: false);
  191. TranslatorContext translatorContext = DecodeComputeShader(gpuAccessor, _context.Capabilities.Api, gpuVa);
  192. TranslatedShader translatedShader = TranslateShader(_dumper, channel, translatorContext, cachedGuestCode, asCompute: false);
  193. ShaderSource[] shaderSourcesArray = new ShaderSource[] { CreateShaderSource(translatedShader.Program) };
  194. ShaderInfo info = ShaderInfoBuilder.BuildForCompute(
  195. _context,
  196. translatedShader.Program.Info,
  197. computeState.GetLocalSize());
  198. IProgram hostProgram = _context.Renderer.CreateProgram(shaderSourcesArray, info);
  199. cpShader = new CachedShaderProgram(hostProgram, specState, translatedShader.Shader);
  200. _computeShaderCache.Add(cpShader);
  201. EnqueueProgramToSave(cpShader, hostProgram, shaderSourcesArray);
  202. _cpPrograms[gpuVa] = cpShader;
  203. return cpShader;
  204. }
  205. /// <summary>
  206. /// Updates the shader pipeline state based on the current GPU state.
  207. /// </summary>
  208. /// <param name="state">Current GPU 3D engine state</param>
  209. /// <param name="pipeline">Shader pipeline state to be updated</param>
  210. /// <param name="graphicsState">Current graphics state</param>
  211. /// <param name="channel">Current GPU channel</param>
  212. private static void UpdatePipelineInfo(
  213. ref ThreedClassState state,
  214. ref ProgramPipelineState pipeline,
  215. GpuChannelGraphicsState graphicsState,
  216. GpuChannel channel)
  217. {
  218. channel.TextureManager.UpdateRenderTargets();
  219. var rtControl = state.RtControl;
  220. var msaaMode = state.RtMsaaMode;
  221. pipeline.SamplesCount = msaaMode.SamplesInX() * msaaMode.SamplesInY();
  222. int count = rtControl.UnpackCount();
  223. for (int index = 0; index < Constants.TotalRenderTargets; index++)
  224. {
  225. int rtIndex = rtControl.UnpackPermutationIndex(index);
  226. var colorState = state.RtColorState[rtIndex];
  227. if (index >= count || colorState.Format == 0 || colorState.WidthOrStride == 0)
  228. {
  229. pipeline.AttachmentEnable[index] = false;
  230. pipeline.AttachmentFormats[index] = Format.R8G8B8A8Unorm;
  231. }
  232. else
  233. {
  234. pipeline.AttachmentEnable[index] = true;
  235. pipeline.AttachmentFormats[index] = colorState.Format.Convert().Format;
  236. }
  237. }
  238. pipeline.DepthStencilEnable = state.RtDepthStencilEnable;
  239. pipeline.DepthStencilFormat = pipeline.DepthStencilEnable ? state.RtDepthStencilState.Format.Convert().Format : Format.D24UnormS8Uint;
  240. pipeline.VertexBufferCount = Constants.TotalVertexBuffers;
  241. pipeline.Topology = graphicsState.Topology;
  242. }
  243. /// <summary>
  244. /// Gets a graphics shader program from the shader cache.
  245. /// This includes all the specified shader stages.
  246. /// </summary>
  247. /// <remarks>
  248. /// This automatically translates, compiles and adds the code to the cache if not present.
  249. /// </remarks>
  250. /// <param name="state">GPU state</param>
  251. /// <param name="pipeline">Pipeline state</param>
  252. /// <param name="channel">GPU channel</param>
  253. /// <param name="samplerPoolMaximumId">Maximum ID that an entry in the sampler pool may have</param>
  254. /// <param name="poolState">Texture pool state</param>
  255. /// <param name="graphicsState">3D engine state</param>
  256. /// <param name="addresses">Addresses of the shaders for each stage</param>
  257. /// <returns>Compiled graphics shader code</returns>
  258. public CachedShaderProgram GetGraphicsShader(
  259. ref ThreedClassState state,
  260. ref ProgramPipelineState pipeline,
  261. GpuChannel channel,
  262. int samplerPoolMaximumId,
  263. ref GpuChannelPoolState poolState,
  264. ref GpuChannelGraphicsState graphicsState,
  265. ShaderAddresses addresses)
  266. {
  267. if (_gpPrograms.TryGetValue(addresses, out var gpShaders) && IsShaderEqual(channel, ref poolState, ref graphicsState, gpShaders, addresses))
  268. {
  269. return gpShaders;
  270. }
  271. if (_graphicsShaderCache.TryFind(channel, ref poolState, ref graphicsState, addresses, out gpShaders, out var cachedGuestCode))
  272. {
  273. _gpPrograms[addresses] = gpShaders;
  274. return gpShaders;
  275. }
  276. TransformFeedbackDescriptor[] transformFeedbackDescriptors = GetTransformFeedbackDescriptors(ref state);
  277. UpdatePipelineInfo(ref state, ref pipeline, graphicsState, channel);
  278. ShaderSpecializationState specState = new(ref graphicsState, ref pipeline, transformFeedbackDescriptors);
  279. GpuAccessorState gpuAccessorState = new(samplerPoolMaximumId, poolState, default, graphicsState, specState, transformFeedbackDescriptors);
  280. ReadOnlySpan<ulong> addressesSpan = addresses.AsSpan();
  281. GpuAccessor[] gpuAccessors = new GpuAccessor[Constants.ShaderStages];
  282. TranslatorContext[] translatorContexts = new TranslatorContext[Constants.ShaderStages + 1];
  283. TranslatorContext nextStage = null;
  284. TargetApi api = _context.Capabilities.Api;
  285. for (int stageIndex = Constants.ShaderStages - 1; stageIndex >= 0; stageIndex--)
  286. {
  287. ulong gpuVa = addressesSpan[stageIndex + 1];
  288. if (gpuVa != 0)
  289. {
  290. GpuAccessor gpuAccessor = new(_context, channel, gpuAccessorState, stageIndex, addresses.Geometry != 0);
  291. TranslatorContext currentStage = DecodeGraphicsShader(gpuAccessor, api, DefaultFlags, gpuVa);
  292. if (nextStage != null)
  293. {
  294. currentStage.SetNextStage(nextStage);
  295. }
  296. if (stageIndex == 0 && addresses.VertexA != 0)
  297. {
  298. translatorContexts[0] = DecodeGraphicsShader(gpuAccessor, api, DefaultFlags | TranslationFlags.VertexA, addresses.VertexA);
  299. }
  300. gpuAccessors[stageIndex] = gpuAccessor;
  301. translatorContexts[stageIndex + 1] = currentStage;
  302. nextStage = currentStage;
  303. }
  304. }
  305. bool hasGeometryShader = translatorContexts[4] != null;
  306. bool vertexHasStore = translatorContexts[1] != null && translatorContexts[1].HasStore;
  307. bool geometryHasStore = hasGeometryShader && translatorContexts[4].HasStore;
  308. bool vertexToCompute = ShouldConvertVertexToCompute(_context, vertexHasStore, geometryHasStore, hasGeometryShader);
  309. bool geometryToCompute = ShouldConvertGeometryToCompute(_context, geometryHasStore);
  310. CachedShaderStage[] shaders = new CachedShaderStage[Constants.ShaderStages + 1];
  311. List<ShaderSource> shaderSources = new();
  312. TranslatorContext previousStage = null;
  313. ShaderInfoBuilder infoBuilder = new(_context, transformFeedbackDescriptors != null, vertexToCompute);
  314. if (geometryToCompute && translatorContexts[4] != null)
  315. {
  316. translatorContexts[4].SetVertexOutputMapForGeometryAsCompute(translatorContexts[1]);
  317. }
  318. ShaderAsCompute vertexAsCompute = null;
  319. ShaderAsCompute geometryAsCompute = null;
  320. for (int stageIndex = 0; stageIndex < Constants.ShaderStages; stageIndex++)
  321. {
  322. TranslatorContext currentStage = translatorContexts[stageIndex + 1];
  323. if (currentStage != null)
  324. {
  325. gpuAccessors[stageIndex].InitializeReservedCounts(transformFeedbackDescriptors != null, vertexToCompute);
  326. ShaderProgram program;
  327. bool asCompute = (stageIndex == 0 && vertexToCompute) || (stageIndex == 3 && geometryToCompute);
  328. if (stageIndex == 0 && translatorContexts[0] != null)
  329. {
  330. TranslatedShaderVertexPair translatedShader = TranslateShader(
  331. _dumper,
  332. channel,
  333. currentStage,
  334. translatorContexts[0],
  335. cachedGuestCode.VertexACode,
  336. cachedGuestCode.VertexBCode,
  337. asCompute);
  338. shaders[0] = translatedShader.VertexA;
  339. shaders[1] = translatedShader.VertexB;
  340. program = translatedShader.Program;
  341. }
  342. else
  343. {
  344. byte[] code = cachedGuestCode.GetByIndex(stageIndex);
  345. TranslatedShader translatedShader = TranslateShader(_dumper, channel, currentStage, code, asCompute);
  346. shaders[stageIndex + 1] = translatedShader.Shader;
  347. program = translatedShader.Program;
  348. }
  349. if (asCompute)
  350. {
  351. bool tfEnabled = transformFeedbackDescriptors != null;
  352. if (stageIndex == 0)
  353. {
  354. vertexAsCompute = CreateHostVertexAsComputeProgram(program, currentStage, tfEnabled);
  355. TranslatorContext lastInVertexPipeline = geometryToCompute ? translatorContexts[4] ?? currentStage : currentStage;
  356. (program, ShaderProgramInfo vacInfo) = lastInVertexPipeline.GenerateVertexPassthroughForCompute();
  357. infoBuilder.AddStageInfoVac(vacInfo);
  358. }
  359. else
  360. {
  361. geometryAsCompute = CreateHostVertexAsComputeProgram(program, currentStage, tfEnabled);
  362. program = null;
  363. }
  364. }
  365. if (program != null)
  366. {
  367. shaderSources.Add(CreateShaderSource(program));
  368. infoBuilder.AddStageInfo(program.Info);
  369. }
  370. previousStage = currentStage;
  371. }
  372. else if (
  373. previousStage != null &&
  374. previousStage.LayerOutputWritten &&
  375. stageIndex == 3 &&
  376. !_context.Capabilities.SupportsLayerVertexTessellation)
  377. {
  378. shaderSources.Add(CreateShaderSource(previousStage.GenerateGeometryPassthrough()));
  379. }
  380. }
  381. ShaderSource[] shaderSourcesArray = shaderSources.ToArray();
  382. ShaderInfo info = infoBuilder.Build(pipeline);
  383. IProgram hostProgram = _context.Renderer.CreateProgram(shaderSourcesArray, info);
  384. gpShaders = new(hostProgram, vertexAsCompute, geometryAsCompute, specState, shaders);
  385. _graphicsShaderCache.Add(gpShaders);
  386. // We don't currently support caching shaders that have been converted to compute.
  387. if (vertexAsCompute == null)
  388. {
  389. EnqueueProgramToSave(gpShaders, hostProgram, shaderSourcesArray);
  390. }
  391. _gpPrograms[addresses] = gpShaders;
  392. return gpShaders;
  393. }
  394. /// <summary>
  395. /// Checks if a vertex shader should be converted to a compute shader due to it making use of
  396. /// features that are not supported on the host.
  397. /// </summary>
  398. /// <param name="context">GPU context of the shader</param>
  399. /// <param name="vertexHasStore">Whether the vertex shader has image or storage buffer store operations</param>
  400. /// <param name="geometryHasStore">Whether the geometry shader has image or storage buffer store operations, if one exists</param>
  401. /// <param name="hasGeometryShader">Whether a geometry shader exists</param>
  402. /// <returns>True if the vertex shader should be converted to compute, false otherwise</returns>
  403. public static bool ShouldConvertVertexToCompute(GpuContext context, bool vertexHasStore, bool geometryHasStore, bool hasGeometryShader)
  404. {
  405. // If the host does not support store operations on vertex,
  406. // we need to emulate it on a compute shader.
  407. if (!context.Capabilities.SupportsVertexStoreAndAtomics && vertexHasStore)
  408. {
  409. return true;
  410. }
  411. // If any stage after the vertex stage is converted to compute,
  412. // we need to convert vertex to compute too.
  413. return hasGeometryShader && ShouldConvertGeometryToCompute(context, geometryHasStore);
  414. }
  415. /// <summary>
  416. /// Checks if a geometry shader should be converted to a compute shader due to it making use of
  417. /// features that are not supported on the host.
  418. /// </summary>
  419. /// <param name="context">GPU context of the shader</param>
  420. /// <param name="geometryHasStore">Whether the geometry shader has image or storage buffer store operations, if one exists</param>
  421. /// <returns>True if the geometry shader should be converted to compute, false otherwise</returns>
  422. public static bool ShouldConvertGeometryToCompute(GpuContext context, bool geometryHasStore)
  423. {
  424. return (!context.Capabilities.SupportsVertexStoreAndAtomics && geometryHasStore) ||
  425. !context.Capabilities.SupportsGeometryShader;
  426. }
  427. /// <summary>
  428. /// Checks if it might be necessary for any vertex, tessellation or geometry shader to be converted to compute,
  429. /// based on the supported host features.
  430. /// </summary>
  431. /// <param name="capabilities">Host capabilities</param>
  432. /// <returns>True if the possibility of a shader being converted to compute exists, false otherwise</returns>
  433. public static bool MayConvertVtgToCompute(ref Capabilities capabilities)
  434. {
  435. return !capabilities.SupportsVertexStoreAndAtomics || !capabilities.SupportsGeometryShader;
  436. }
  437. /// <summary>
  438. /// Creates a compute shader from a vertex, tessellation or geometry shader that has been converted to compute.
  439. /// </summary>
  440. /// <param name="program">Shader program</param>
  441. /// <param name="context">Translation context of the shader</param>
  442. /// <param name="tfEnabled">Whether transform feedback is enabled</param>
  443. /// <returns>Compute shader</returns>
  444. private ShaderAsCompute CreateHostVertexAsComputeProgram(ShaderProgram program, TranslatorContext context, bool tfEnabled)
  445. {
  446. ShaderSource source = new(program.Code, program.BinaryCode, ShaderStage.Compute, program.Language);
  447. ShaderInfo info = ShaderInfoBuilder.BuildForVertexAsCompute(_context, program.Info, context.GetVertexAsComputeInfo(), tfEnabled);
  448. return new(_context.Renderer.CreateProgram(new[] { source }, info), program.Info, context.GetResourceReservations());
  449. }
  450. /// <summary>
  451. /// Creates a shader source for use with the backend from a translated shader program.
  452. /// </summary>
  453. /// <param name="program">Translated shader program</param>
  454. /// <returns>Shader source</returns>
  455. public static ShaderSource CreateShaderSource(ShaderProgram program)
  456. {
  457. return new ShaderSource(program.Code, program.BinaryCode, program.Info.Stage, program.Language);
  458. }
  459. /// <summary>
  460. /// Puts a program on the queue of programs to be saved on the disk cache.
  461. /// </summary>
  462. /// <remarks>
  463. /// This will not do anything if disk shader cache is disabled.
  464. /// </remarks>
  465. /// <param name="program">Cached shader program</param>
  466. /// <param name="hostProgram">Host program</param>
  467. /// <param name="sources">Source for each shader stage</param>
  468. private void EnqueueProgramToSave(CachedShaderProgram program, IProgram hostProgram, ShaderSource[] sources)
  469. {
  470. if (_diskCacheHostStorage.CacheEnabled)
  471. {
  472. byte[] binaryCode = _context.Capabilities.Api == TargetApi.Vulkan ? ShaderBinarySerializer.Pack(sources) : null;
  473. ProgramToSave programToSave = new(program, hostProgram, binaryCode);
  474. _programsToSaveQueue.Enqueue(programToSave);
  475. }
  476. }
  477. /// <summary>
  478. /// Gets transform feedback state from the current GPU state.
  479. /// </summary>
  480. /// <param name="state">Current GPU state</param>
  481. /// <returns>Four transform feedback descriptors for the enabled TFBs, or null if TFB is disabled</returns>
  482. private static TransformFeedbackDescriptor[] GetTransformFeedbackDescriptors(ref ThreedClassState state)
  483. {
  484. bool tfEnable = state.TfEnable;
  485. if (!tfEnable)
  486. {
  487. return null;
  488. }
  489. TransformFeedbackDescriptor[] descs = new TransformFeedbackDescriptor[Constants.TotalTransformFeedbackBuffers];
  490. for (int i = 0; i < Constants.TotalTransformFeedbackBuffers; i++)
  491. {
  492. var tf = state.TfState[i];
  493. descs[i] = new TransformFeedbackDescriptor(
  494. tf.BufferIndex,
  495. tf.Stride,
  496. tf.VaryingsCount,
  497. ref state.TfVaryingLocations[i]);
  498. }
  499. return descs;
  500. }
  501. /// <summary>
  502. /// Checks if compute shader code in memory is equal to the cached shader.
  503. /// </summary>
  504. /// <param name="channel">GPU channel using the shader</param>
  505. /// <param name="poolState">GPU channel state to verify shader compatibility</param>
  506. /// <param name="computeState">GPU channel compute state to verify shader compatibility</param>
  507. /// <param name="cpShader">Cached compute shader</param>
  508. /// <param name="gpuVa">GPU virtual address of the shader code in memory</param>
  509. /// <returns>True if the code is different, false otherwise</returns>
  510. private static bool IsShaderEqual(
  511. GpuChannel channel,
  512. GpuChannelPoolState poolState,
  513. GpuChannelComputeState computeState,
  514. CachedShaderProgram cpShader,
  515. ulong gpuVa)
  516. {
  517. if (IsShaderEqual(channel.MemoryManager, cpShader.Shaders[0], gpuVa))
  518. {
  519. return cpShader.SpecializationState.MatchesCompute(channel, ref poolState, computeState, true);
  520. }
  521. return false;
  522. }
  523. /// <summary>
  524. /// Checks if graphics shader code from all stages in memory are equal to the cached shaders.
  525. /// </summary>
  526. /// <param name="channel">GPU channel using the shader</param>
  527. /// <param name="poolState">GPU channel state to verify shader compatibility</param>
  528. /// <param name="graphicsState">GPU channel graphics state to verify shader compatibility</param>
  529. /// <param name="gpShaders">Cached graphics shaders</param>
  530. /// <param name="addresses">GPU virtual addresses of all enabled shader stages</param>
  531. /// <returns>True if the code is different, false otherwise</returns>
  532. private static bool IsShaderEqual(
  533. GpuChannel channel,
  534. ref GpuChannelPoolState poolState,
  535. ref GpuChannelGraphicsState graphicsState,
  536. CachedShaderProgram gpShaders,
  537. ShaderAddresses addresses)
  538. {
  539. ReadOnlySpan<ulong> addressesSpan = addresses.AsSpan();
  540. for (int stageIndex = 0; stageIndex < gpShaders.Shaders.Length; stageIndex++)
  541. {
  542. CachedShaderStage shader = gpShaders.Shaders[stageIndex];
  543. ulong gpuVa = addressesSpan[stageIndex];
  544. if (!IsShaderEqual(channel.MemoryManager, shader, gpuVa))
  545. {
  546. return false;
  547. }
  548. }
  549. bool vertexAsCompute = gpShaders.VertexAsCompute != null;
  550. bool usesDrawParameters = gpShaders.Shaders[1]?.Info.UsesDrawParameters ?? false;
  551. return gpShaders.SpecializationState.MatchesGraphics(
  552. channel,
  553. ref poolState,
  554. ref graphicsState,
  555. vertexAsCompute,
  556. usesDrawParameters,
  557. checkTextures: true);
  558. }
  559. /// <summary>
  560. /// Checks if the code of the specified cached shader is different from the code in memory.
  561. /// </summary>
  562. /// <param name="memoryManager">Memory manager used to access the GPU memory where the shader is located</param>
  563. /// <param name="shader">Cached shader to compare with</param>
  564. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  565. /// <returns>True if the code is different, false otherwise</returns>
  566. private static bool IsShaderEqual(MemoryManager memoryManager, CachedShaderStage shader, ulong gpuVa)
  567. {
  568. if (shader == null)
  569. {
  570. return true;
  571. }
  572. ReadOnlySpan<byte> memoryCode = memoryManager.GetSpanMapped(gpuVa, shader.Code.Length);
  573. return memoryCode.SequenceEqual(shader.Code);
  574. }
  575. /// <summary>
  576. /// Decode the binary Maxwell shader code to a translator context.
  577. /// </summary>
  578. /// <param name="gpuAccessor">GPU state accessor</param>
  579. /// <param name="api">Graphics API that will be used with the shader</param>
  580. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  581. /// <returns>The generated translator context</returns>
  582. public static TranslatorContext DecodeComputeShader(IGpuAccessor gpuAccessor, TargetApi api, ulong gpuVa)
  583. {
  584. var options = CreateTranslationOptions(api, DefaultFlags | TranslationFlags.Compute);
  585. return Translator.CreateContext(gpuVa, gpuAccessor, options);
  586. }
  587. /// <summary>
  588. /// Decode the binary Maxwell shader code to a translator context.
  589. /// </summary>
  590. /// <remarks>
  591. /// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
  592. /// </remarks>
  593. /// <param name="gpuAccessor">GPU state accessor</param>
  594. /// <param name="api">Graphics API that will be used with the shader</param>
  595. /// <param name="flags">Flags that controls shader translation</param>
  596. /// <param name="gpuVa">GPU virtual address of the shader code</param>
  597. /// <returns>The generated translator context</returns>
  598. public static TranslatorContext DecodeGraphicsShader(IGpuAccessor gpuAccessor, TargetApi api, TranslationFlags flags, ulong gpuVa)
  599. {
  600. var options = CreateTranslationOptions(api, flags);
  601. return Translator.CreateContext(gpuVa, gpuAccessor, options);
  602. }
  603. /// <summary>
  604. /// Translates a previously generated translator context to something that the host API accepts.
  605. /// </summary>
  606. /// <param name="dumper">Optional shader code dumper</param>
  607. /// <param name="channel">GPU channel using the shader</param>
  608. /// <param name="currentStage">Translator context of the stage to be translated</param>
  609. /// <param name="vertexA">Optional translator context of the shader that should be combined</param>
  610. /// <param name="codeA">Optional Maxwell binary code of the Vertex A shader, if present</param>
  611. /// <param name="codeB">Optional Maxwell binary code of the Vertex B or current stage shader, if present on cache</param>
  612. /// <param name="asCompute">Indicates that the vertex shader should be converted to a compute shader</param>
  613. /// <returns>Compiled graphics shader code</returns>
  614. private static TranslatedShaderVertexPair TranslateShader(
  615. ShaderDumper dumper,
  616. GpuChannel channel,
  617. TranslatorContext currentStage,
  618. TranslatorContext vertexA,
  619. byte[] codeA,
  620. byte[] codeB,
  621. bool asCompute)
  622. {
  623. ulong cb1DataAddress = channel.BufferManager.GetGraphicsUniformBufferAddress(0, 1);
  624. var memoryManager = channel.MemoryManager;
  625. codeA ??= memoryManager.GetSpan(vertexA.Address, vertexA.Size).ToArray();
  626. codeB ??= memoryManager.GetSpan(currentStage.Address, currentStage.Size).ToArray();
  627. byte[] cb1DataA = ReadArray(memoryManager, cb1DataAddress, vertexA.Cb1DataSize);
  628. byte[] cb1DataB = ReadArray(memoryManager, cb1DataAddress, currentStage.Cb1DataSize);
  629. ShaderDumpPaths pathsA = default;
  630. ShaderDumpPaths pathsB = default;
  631. if (dumper != null)
  632. {
  633. pathsA = dumper.Dump(codeA, compute: false);
  634. pathsB = dumper.Dump(codeB, compute: false);
  635. }
  636. ShaderProgram program = currentStage.Translate(vertexA, asCompute);
  637. pathsB.Prepend(program);
  638. pathsA.Prepend(program);
  639. CachedShaderStage vertexAStage = new(null, codeA, cb1DataA);
  640. CachedShaderStage vertexBStage = new(program.Info, codeB, cb1DataB);
  641. return new TranslatedShaderVertexPair(vertexAStage, vertexBStage, program);
  642. }
  643. /// <summary>
  644. /// Translates a previously generated translator context to something that the host API accepts.
  645. /// </summary>
  646. /// <param name="dumper">Optional shader code dumper</param>
  647. /// <param name="channel">GPU channel using the shader</param>
  648. /// <param name="context">Translator context of the stage to be translated</param>
  649. /// <param name="code">Optional Maxwell binary code of the current stage shader, if present on cache</param>
  650. /// <param name="asCompute">Indicates that the vertex shader should be converted to a compute shader</param>
  651. /// <returns>Compiled graphics shader code</returns>
  652. private static TranslatedShader TranslateShader(ShaderDumper dumper, GpuChannel channel, TranslatorContext context, byte[] code, bool asCompute)
  653. {
  654. var memoryManager = channel.MemoryManager;
  655. ulong cb1DataAddress = context.Stage == ShaderStage.Compute
  656. ? channel.BufferManager.GetComputeUniformBufferAddress(1)
  657. : channel.BufferManager.GetGraphicsUniformBufferAddress(StageToStageIndex(context.Stage), 1);
  658. byte[] cb1Data = ReadArray(memoryManager, cb1DataAddress, context.Cb1DataSize);
  659. code ??= memoryManager.GetSpan(context.Address, context.Size).ToArray();
  660. ShaderDumpPaths paths = dumper?.Dump(code, context.Stage == ShaderStage.Compute) ?? default;
  661. ShaderProgram program = context.Translate(asCompute);
  662. paths.Prepend(program);
  663. return new TranslatedShader(new CachedShaderStage(program.Info, code, cb1Data), program);
  664. }
  665. /// <summary>
  666. /// Reads data from physical memory, returns an empty array if the memory is unmapped or size is 0.
  667. /// </summary>
  668. /// <param name="memoryManager">Memory manager with the physical memory to read from</param>
  669. /// <param name="address">Physical address of the region to read</param>
  670. /// <param name="size">Size in bytes of the data</param>
  671. /// <returns>An array with the data at the specified memory location</returns>
  672. private static byte[] ReadArray(MemoryManager memoryManager, ulong address, int size)
  673. {
  674. if (address == MemoryManager.PteUnmapped || size == 0)
  675. {
  676. return Array.Empty<byte>();
  677. }
  678. return memoryManager.Physical.GetSpan(address, size).ToArray();
  679. }
  680. /// <summary>
  681. /// Gets the index of a stage from a <see cref="ShaderStage"/>.
  682. /// </summary>
  683. /// <param name="stage">Stage to get the index from</param>
  684. /// <returns>Stage index</returns>
  685. private static int StageToStageIndex(ShaderStage stage)
  686. {
  687. return stage switch
  688. {
  689. ShaderStage.TessellationControl => 1,
  690. ShaderStage.TessellationEvaluation => 2,
  691. ShaderStage.Geometry => 3,
  692. ShaderStage.Fragment => 4,
  693. _ => 0,
  694. };
  695. }
  696. /// <summary>
  697. /// Creates shader translation options with the requested graphics API and flags.
  698. /// The shader language is chosen based on the current configuration and graphics API.
  699. /// </summary>
  700. /// <param name="api">Target graphics API</param>
  701. /// <param name="flags">Translation flags</param>
  702. /// <returns>Translation options</returns>
  703. private static TranslationOptions CreateTranslationOptions(TargetApi api, TranslationFlags flags)
  704. {
  705. TargetLanguage lang = api switch
  706. {
  707. TargetApi.OpenGL => TargetLanguage.Glsl,
  708. TargetApi.Vulkan => GraphicsConfig.EnableSpirvCompilationOnVulkan ? TargetLanguage.Spirv : TargetLanguage.Glsl,
  709. TargetApi.Metal => TargetLanguage.Msl,
  710. _ => throw new NotImplementedException()
  711. };
  712. return new TranslationOptions(lang, api, flags);
  713. }
  714. /// <summary>
  715. /// Disposes the shader cache, deleting all the cached shaders.
  716. /// It's an error to use the shader cache after disposal.
  717. /// </summary>
  718. public void Dispose()
  719. {
  720. foreach (CachedShaderProgram program in _graphicsShaderCache.GetPrograms())
  721. {
  722. program.Dispose();
  723. }
  724. foreach (CachedShaderProgram program in _computeShaderCache.GetPrograms())
  725. {
  726. program.Dispose();
  727. }
  728. _cacheWriter?.Dispose();
  729. }
  730. }
  731. }