ShaderCache.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.GAL;
  4. using Ryujinx.Graphics.Gpu.Shader.Cache;
  5. using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
  6. using Ryujinx.Graphics.Gpu.State;
  7. using Ryujinx.Graphics.Shader;
  8. using Ryujinx.Graphics.Shader.Translation;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Runtime.CompilerServices;
  14. using System.Runtime.InteropServices;
  15. namespace Ryujinx.Graphics.Gpu.Shader
  16. {
  17. /// <summary>
  18. /// Memory cache of shader code.
  19. /// </summary>
  20. class ShaderCache : IDisposable
  21. {
  22. private const TranslationFlags DefaultFlags = TranslationFlags.DebugMode;
  23. private readonly GpuContext _context;
  24. private readonly ShaderDumper _dumper;
  25. private readonly Dictionary<ulong, List<ShaderBundle>> _cpPrograms;
  26. private readonly Dictionary<ShaderAddresses, List<ShaderBundle>> _gpPrograms;
  27. private CacheManager _cacheManager;
  28. private Dictionary<Hash128, ShaderBundle> _gpProgramsDiskCache;
  29. private Dictionary<Hash128, ShaderBundle> _cpProgramsDiskCache;
  30. /// <summary>
  31. /// Version of the codegen (to be incremented when codegen changes).
  32. /// </summary>
  33. private const ulong ShaderCodeGenVersion = 1;
  34. /// <summary>
  35. /// Creates a new instance of the shader cache.
  36. /// </summary>
  37. /// <param name="context">GPU context that the shader cache belongs to</param>
  38. public ShaderCache(GpuContext context)
  39. {
  40. _context = context;
  41. _dumper = new ShaderDumper();
  42. _cpPrograms = new Dictionary<ulong, List<ShaderBundle>>();
  43. _gpPrograms = new Dictionary<ShaderAddresses, List<ShaderBundle>>();
  44. _gpProgramsDiskCache = new Dictionary<Hash128, ShaderBundle>();
  45. _cpProgramsDiskCache = new Dictionary<Hash128, ShaderBundle>();
  46. }
  47. /// <summary>
  48. /// Initialize the cache.
  49. /// </summary>
  50. internal void Initialize()
  51. {
  52. if (GraphicsConfig.EnableShaderCache && GraphicsConfig.TitleId != null)
  53. {
  54. _cacheManager = new CacheManager(CacheGraphicsApi.OpenGL, CacheHashType.XxHash128, "glsl", GraphicsConfig.TitleId, ShaderCodeGenVersion);
  55. HashSet<Hash128> invalidEntries = new HashSet<Hash128>();
  56. ReadOnlySpan<Hash128> guestProgramList = _cacheManager.GetGuestProgramList();
  57. for (int programIndex = 0; programIndex < guestProgramList.Length; programIndex++)
  58. {
  59. Hash128 key = guestProgramList[programIndex];
  60. Logger.Info?.Print(LogClass.Gpu, $"Compiling shader {key} ({programIndex + 1} / {guestProgramList.Length})");
  61. byte[] hostProgramBinary = _cacheManager.GetHostProgramByHash(ref key);
  62. bool hasHostCache = hostProgramBinary != null;
  63. IProgram hostProgram = null;
  64. // If the program sources aren't in the cache, compile from saved guest program.
  65. byte[] guestProgram = _cacheManager.GetGuestProgramByHash(ref key);
  66. if (guestProgram == null)
  67. {
  68. Logger.Error?.Print(LogClass.Gpu, $"Ignoring orphan shader hash {key} in cache (is the cache incomplete?)");
  69. // Should not happen, but if someone messed with the cache it's better to catch it.
  70. invalidEntries.Add(key);
  71. continue;
  72. }
  73. ReadOnlySpan<byte> guestProgramReadOnlySpan = guestProgram;
  74. ReadOnlySpan<GuestShaderCacheEntry> cachedShaderEntries = GuestShaderCacheEntry.Parse(ref guestProgramReadOnlySpan, out GuestShaderCacheHeader fileHeader);
  75. if (cachedShaderEntries[0].Header.Stage == ShaderStage.Compute)
  76. {
  77. Debug.Assert(cachedShaderEntries.Length == 1);
  78. GuestShaderCacheEntry entry = cachedShaderEntries[0];
  79. HostShaderCacheEntry[] hostShaderEntries = null;
  80. // Try loading host shader binary.
  81. if (hasHostCache)
  82. {
  83. hostShaderEntries = HostShaderCacheEntry.Parse(hostProgramBinary, out ReadOnlySpan<byte> hostProgramBinarySpan);
  84. hostProgramBinary = hostProgramBinarySpan.ToArray();
  85. hostProgram = _context.Renderer.LoadProgramBinary(hostProgramBinary);
  86. }
  87. bool isHostProgramValid = hostProgram != null;
  88. ShaderProgram program;
  89. ShaderProgramInfo shaderProgramInfo;
  90. // Reconstruct code holder.
  91. if (isHostProgramValid)
  92. {
  93. program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
  94. shaderProgramInfo = hostShaderEntries[0].ToShaderProgramInfo();
  95. }
  96. else
  97. {
  98. IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors);
  99. program = Translator.CreateContext(0, gpuAccessor, DefaultFlags | TranslationFlags.Compute).Translate(out shaderProgramInfo);
  100. }
  101. ShaderCodeHolder shader = new ShaderCodeHolder(program, shaderProgramInfo, entry.Code);
  102. // If the host program was rejected by the gpu driver or isn't in cache, try to build from program sources again.
  103. if (hostProgram == null)
  104. {
  105. Logger.Info?.Print(LogClass.Gpu, $"Host shader {key} got invalidated, rebuilding from guest...");
  106. // Compile shader and create program as the shader program binary got invalidated.
  107. shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, shader.Program.Code);
  108. hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }, null);
  109. // As the host program was invalidated, save the new entry in the cache.
  110. hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), new ShaderCodeHolder[] { shader });
  111. if (hasHostCache)
  112. {
  113. _cacheManager.ReplaceHostProgram(ref key, hostProgramBinary);
  114. }
  115. else
  116. {
  117. Logger.Warning?.Print(LogClass.Gpu, $"Add missing host shader {key} in cache (is the cache incomplete?)");
  118. _cacheManager.AddHostProgram(ref key, hostProgramBinary);
  119. }
  120. }
  121. _cpProgramsDiskCache.Add(key, new ShaderBundle(hostProgram, shader));
  122. }
  123. else
  124. {
  125. Debug.Assert(cachedShaderEntries.Length == Constants.ShaderStages);
  126. ShaderCodeHolder[] shaders = new ShaderCodeHolder[cachedShaderEntries.Length];
  127. List<ShaderProgram> shaderPrograms = new List<ShaderProgram>();
  128. TransformFeedbackDescriptor[] tfd = ReadTransformationFeedbackInformations(ref guestProgramReadOnlySpan, fileHeader);
  129. TranslationFlags flags = DefaultFlags;
  130. if (tfd != null)
  131. {
  132. flags = TranslationFlags.Feedback;
  133. }
  134. TranslationCounts counts = new TranslationCounts();
  135. HostShaderCacheEntry[] hostShaderEntries = null;
  136. // Try loading host shader binary.
  137. if (hasHostCache)
  138. {
  139. hostShaderEntries = HostShaderCacheEntry.Parse(hostProgramBinary, out ReadOnlySpan<byte> hostProgramBinarySpan);
  140. hostProgramBinary = hostProgramBinarySpan.ToArray();
  141. hostProgram = _context.Renderer.LoadProgramBinary(hostProgramBinary);
  142. }
  143. bool isHostProgramValid = hostProgram != null;
  144. // Reconstruct code holder.
  145. for (int i = 0; i < cachedShaderEntries.Length; i++)
  146. {
  147. GuestShaderCacheEntry entry = cachedShaderEntries[i];
  148. if (entry == null)
  149. {
  150. continue;
  151. }
  152. ShaderProgram program;
  153. if (entry.Header.SizeA != 0)
  154. {
  155. ShaderProgramInfo shaderProgramInfo;
  156. if (isHostProgramValid)
  157. {
  158. program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
  159. shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
  160. }
  161. else
  162. {
  163. IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors);
  164. program = Translator.CreateContext((ulong)entry.Header.Size, 0, gpuAccessor, flags, counts).Translate(out shaderProgramInfo);
  165. }
  166. // NOTE: Vertex B comes first in the shader cache.
  167. byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size).ToArray();
  168. byte[] code2 = entry.Code.AsSpan().Slice(entry.Header.Size, entry.Header.SizeA).ToArray();
  169. shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, code, code2);
  170. }
  171. else
  172. {
  173. ShaderProgramInfo shaderProgramInfo;
  174. if (isHostProgramValid)
  175. {
  176. program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
  177. shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
  178. }
  179. else
  180. {
  181. IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors);
  182. program = Translator.CreateContext(0, gpuAccessor, flags, counts).Translate(out shaderProgramInfo);
  183. }
  184. shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, entry.Code);
  185. }
  186. shaderPrograms.Add(program);
  187. }
  188. // If the host program was rejected by the gpu driver or isn't in cache, try to build from program sources again.
  189. if (!isHostProgramValid)
  190. {
  191. Logger.Info?.Print(LogClass.Gpu, $"Host shader {key} got invalidated, rebuilding from guest...");
  192. List<IShader> hostShaders = new List<IShader>();
  193. // Compile shaders and create program as the shader program binary got invalidated.
  194. for (int stage = 0; stage < Constants.ShaderStages; stage++)
  195. {
  196. ShaderProgram program = shaders[stage]?.Program;
  197. if (program == null)
  198. {
  199. continue;
  200. }
  201. IShader hostShader = _context.Renderer.CompileShader(program.Stage, program.Code);
  202. shaders[stage].HostShader = hostShader;
  203. hostShaders.Add(hostShader);
  204. }
  205. hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray(), tfd);
  206. // As the host program was invalidated, save the new entry in the cache.
  207. hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), shaders);
  208. if (hasHostCache)
  209. {
  210. _cacheManager.ReplaceHostProgram(ref key, hostProgramBinary);
  211. }
  212. else
  213. {
  214. Logger.Warning?.Print(LogClass.Gpu, $"Add missing host shader {key} in cache (is the cache incomplete?)");
  215. _cacheManager.AddHostProgram(ref key, hostProgramBinary);
  216. }
  217. }
  218. _gpProgramsDiskCache.Add(key, new ShaderBundle(hostProgram, shaders));
  219. }
  220. }
  221. // Remove entries that are broken in the cache
  222. _cacheManager.RemoveManifestEntries(invalidEntries);
  223. _cacheManager.FlushToArchive();
  224. _cacheManager.Synchronize();
  225. Logger.Info?.Print(LogClass.Gpu, "Shader cache loaded.");
  226. }
  227. }
  228. /// <summary>
  229. /// Gets a compute shader from the cache.
  230. /// </summary>
  231. /// <remarks>
  232. /// This automatically translates, compiles and adds the code to the cache if not present.
  233. /// </remarks>
  234. /// <param name="state">Current GPU state</param>
  235. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  236. /// <param name="localSizeX">Local group size X of the computer shader</param>
  237. /// <param name="localSizeY">Local group size Y of the computer shader</param>
  238. /// <param name="localSizeZ">Local group size Z of the computer shader</param>
  239. /// <param name="localMemorySize">Local memory size of the compute shader</param>
  240. /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
  241. /// <returns>Compiled compute shader code</returns>
  242. public ShaderBundle GetComputeShader(
  243. GpuState state,
  244. ulong gpuVa,
  245. int localSizeX,
  246. int localSizeY,
  247. int localSizeZ,
  248. int localMemorySize,
  249. int sharedMemorySize)
  250. {
  251. bool isCached = _cpPrograms.TryGetValue(gpuVa, out List<ShaderBundle> list);
  252. if (isCached)
  253. {
  254. foreach (ShaderBundle cachedCpShader in list)
  255. {
  256. if (IsShaderEqual(cachedCpShader, gpuVa))
  257. {
  258. return cachedCpShader;
  259. }
  260. }
  261. }
  262. TranslatorContext[] shaderContexts = new TranslatorContext[1];
  263. shaderContexts[0] = DecodeComputeShader(
  264. state,
  265. gpuVa,
  266. localSizeX,
  267. localSizeY,
  268. localSizeZ,
  269. localMemorySize,
  270. sharedMemorySize);
  271. bool isShaderCacheEnabled = _cacheManager != null;
  272. byte[] programCode = null;
  273. Hash128 programCodeHash = default;
  274. GuestShaderCacheEntryHeader[] shaderCacheEntries = null;
  275. if (isShaderCacheEnabled)
  276. {
  277. // Compute hash and prepare data for shader disk cache comparison.
  278. GetProgramInformations(null, shaderContexts, out programCode, out programCodeHash, out shaderCacheEntries);
  279. }
  280. ShaderBundle cpShader;
  281. // Search for the program hash in loaded shaders.
  282. if (!isShaderCacheEnabled || !_cpProgramsDiskCache.TryGetValue(programCodeHash, out cpShader))
  283. {
  284. if (isShaderCacheEnabled)
  285. {
  286. Logger.Debug?.Print(LogClass.Gpu, $"Shader {programCodeHash} not in cache, compiling!");
  287. }
  288. // The shader isn't currently cached, translate it and compile it.
  289. ShaderCodeHolder shader = TranslateShader(shaderContexts[0]);
  290. shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, shader.Program.Code);
  291. IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }, null);
  292. byte[] hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), new ShaderCodeHolder[] { shader });
  293. cpShader = new ShaderBundle(hostProgram, shader);
  294. if (isShaderCacheEnabled)
  295. {
  296. _cpProgramsDiskCache.Add(programCodeHash, cpShader);
  297. _cacheManager.SaveProgram(ref programCodeHash, CreateGuestProgramDump(programCode, shaderCacheEntries, null), hostProgramBinary);
  298. }
  299. }
  300. if (!isCached)
  301. {
  302. list = new List<ShaderBundle>();
  303. _cpPrograms.Add(gpuVa, list);
  304. }
  305. list.Add(cpShader);
  306. return cpShader;
  307. }
  308. /// <summary>
  309. /// Gets a graphics shader program from the shader cache.
  310. /// This includes all the specified shader stages.
  311. /// </summary>
  312. /// <remarks>
  313. /// This automatically translates, compiles and adds the code to the cache if not present.
  314. /// </remarks>
  315. /// <param name="state">Current GPU state</param>
  316. /// <param name="addresses">Addresses of the shaders for each stage</param>
  317. /// <returns>Compiled graphics shader code</returns>
  318. public ShaderBundle GetGraphicsShader(GpuState state, ShaderAddresses addresses)
  319. {
  320. bool isCached = _gpPrograms.TryGetValue(addresses, out List<ShaderBundle> list);
  321. if (isCached)
  322. {
  323. foreach (ShaderBundle cachedGpShaders in list)
  324. {
  325. if (IsShaderEqual(cachedGpShaders, addresses))
  326. {
  327. return cachedGpShaders;
  328. }
  329. }
  330. }
  331. TranslatorContext[] shaderContexts = new TranslatorContext[Constants.ShaderStages];
  332. TransformFeedbackDescriptor[] tfd = GetTransformFeedbackDescriptors(state);
  333. TranslationFlags flags = DefaultFlags;
  334. if (tfd != null)
  335. {
  336. flags |= TranslationFlags.Feedback;
  337. }
  338. TranslationCounts counts = new TranslationCounts();
  339. if (addresses.VertexA != 0)
  340. {
  341. shaderContexts[0] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA);
  342. }
  343. else
  344. {
  345. shaderContexts[0] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex);
  346. }
  347. shaderContexts[1] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationControl, addresses.TessControl);
  348. shaderContexts[2] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
  349. shaderContexts[3] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Geometry, addresses.Geometry);
  350. shaderContexts[4] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Fragment, addresses.Fragment);
  351. bool isShaderCacheEnabled = _cacheManager != null;
  352. byte[] programCode = null;
  353. Hash128 programCodeHash = default;
  354. GuestShaderCacheEntryHeader[] shaderCacheEntries = null;
  355. if (isShaderCacheEnabled)
  356. {
  357. // Compute hash and prepare data for shader disk cache comparison.
  358. GetProgramInformations(tfd, shaderContexts, out programCode, out programCodeHash, out shaderCacheEntries);
  359. }
  360. ShaderBundle gpShaders;
  361. // Search for the program hash in loaded shaders.
  362. if (!isShaderCacheEnabled || !_gpProgramsDiskCache.TryGetValue(programCodeHash, out gpShaders))
  363. {
  364. if (isShaderCacheEnabled)
  365. {
  366. Logger.Debug?.Print(LogClass.Gpu, $"Shader {programCodeHash} not in cache, compiling!");
  367. }
  368. // The shader isn't currently cached, translate it and compile it.
  369. ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages];
  370. shaders[0] = TranslateShader(shaderContexts[0]);
  371. shaders[1] = TranslateShader(shaderContexts[1]);
  372. shaders[2] = TranslateShader(shaderContexts[2]);
  373. shaders[3] = TranslateShader(shaderContexts[3]);
  374. shaders[4] = TranslateShader(shaderContexts[4]);
  375. List<IShader> hostShaders = new List<IShader>();
  376. for (int stage = 0; stage < Constants.ShaderStages; stage++)
  377. {
  378. ShaderProgram program = shaders[stage]?.Program;
  379. if (program == null)
  380. {
  381. continue;
  382. }
  383. IShader hostShader = _context.Renderer.CompileShader(program.Stage, program.Code);
  384. shaders[stage].HostShader = hostShader;
  385. hostShaders.Add(hostShader);
  386. }
  387. IProgram hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray(), tfd);
  388. byte[] hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), shaders);
  389. gpShaders = new ShaderBundle(hostProgram, shaders);
  390. if (isShaderCacheEnabled)
  391. {
  392. _gpProgramsDiskCache.Add(programCodeHash, gpShaders);
  393. _cacheManager.SaveProgram(ref programCodeHash, CreateGuestProgramDump(programCode, shaderCacheEntries, tfd), hostProgramBinary);
  394. }
  395. }
  396. if (!isCached)
  397. {
  398. list = new List<ShaderBundle>();
  399. _gpPrograms.Add(addresses, list);
  400. }
  401. list.Add(gpShaders);
  402. return gpShaders;
  403. }
  404. /// <summary>
  405. /// Gets transform feedback state from the current GPU state.
  406. /// </summary>
  407. /// <param name="state">Current GPU state</param>
  408. /// <returns>Four transform feedback descriptors for the enabled TFBs, or null if TFB is disabled</returns>
  409. private TransformFeedbackDescriptor[] GetTransformFeedbackDescriptors(GpuState state)
  410. {
  411. bool tfEnable = state.Get<Boolean32>(MethodOffset.TfEnable);
  412. if (!tfEnable)
  413. {
  414. return null;
  415. }
  416. TransformFeedbackDescriptor[] descs = new TransformFeedbackDescriptor[Constants.TotalTransformFeedbackBuffers];
  417. for (int i = 0; i < Constants.TotalTransformFeedbackBuffers; i++)
  418. {
  419. var tf = state.Get<TfState>(MethodOffset.TfState, i);
  420. int length = (int)Math.Min((uint)tf.VaryingsCount, 0x80);
  421. var varyingLocations = state.GetSpan(MethodOffset.TfVaryingLocations + i * 0x80, length).ToArray();
  422. descs[i] = new TransformFeedbackDescriptor(tf.BufferIndex, tf.Stride, varyingLocations);
  423. }
  424. return descs;
  425. }
  426. /// <summary>
  427. /// Checks if compute shader code in memory is equal to the cached shader.
  428. /// </summary>
  429. /// <param name="cpShader">Cached compute shader</param>
  430. /// <param name="gpuVa">GPU virtual address of the shader code in memory</param>
  431. /// <returns>True if the code is different, false otherwise</returns>
  432. private bool IsShaderEqual(ShaderBundle cpShader, ulong gpuVa)
  433. {
  434. return IsShaderEqual(cpShader.Shaders[0], gpuVa);
  435. }
  436. /// <summary>
  437. /// Checks if graphics shader code from all stages in memory are equal to the cached shaders.
  438. /// </summary>
  439. /// <param name="gpShaders">Cached graphics shaders</param>
  440. /// <param name="addresses">GPU virtual addresses of all enabled shader stages</param>
  441. /// <returns>True if the code is different, false otherwise</returns>
  442. private bool IsShaderEqual(ShaderBundle gpShaders, ShaderAddresses addresses)
  443. {
  444. for (int stage = 0; stage < gpShaders.Shaders.Length; stage++)
  445. {
  446. ShaderCodeHolder shader = gpShaders.Shaders[stage];
  447. ulong gpuVa = 0;
  448. switch (stage)
  449. {
  450. case 0: gpuVa = addresses.Vertex; break;
  451. case 1: gpuVa = addresses.TessControl; break;
  452. case 2: gpuVa = addresses.TessEvaluation; break;
  453. case 3: gpuVa = addresses.Geometry; break;
  454. case 4: gpuVa = addresses.Fragment; break;
  455. }
  456. if (!IsShaderEqual(shader, gpuVa, addresses.VertexA))
  457. {
  458. return false;
  459. }
  460. }
  461. return true;
  462. }
  463. /// <summary>
  464. /// Checks if the code of the specified cached shader is different from the code in memory.
  465. /// </summary>
  466. /// <param name="shader">Cached shader to compare with</param>
  467. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  468. /// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" binary shader code</param>
  469. /// <returns>True if the code is different, false otherwise</returns>
  470. private bool IsShaderEqual(ShaderCodeHolder shader, ulong gpuVa, ulong gpuVaA = 0)
  471. {
  472. if (shader == null)
  473. {
  474. return true;
  475. }
  476. ReadOnlySpan<byte> memoryCode = _context.MemoryManager.GetSpan(gpuVa, shader.Code.Length);
  477. bool equals = memoryCode.SequenceEqual(shader.Code);
  478. if (equals && shader.Code2 != null)
  479. {
  480. memoryCode = _context.MemoryManager.GetSpan(gpuVaA, shader.Code2.Length);
  481. equals = memoryCode.SequenceEqual(shader.Code2);
  482. }
  483. return equals;
  484. }
  485. /// <summary>
  486. /// Decode the binary Maxwell shader code to a translator context.
  487. /// </summary>
  488. /// <param name="state">Current GPU state</param>
  489. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  490. /// <param name="localSizeX">Local group size X of the computer shader</param>
  491. /// <param name="localSizeY">Local group size Y of the computer shader</param>
  492. /// <param name="localSizeZ">Local group size Z of the computer shader</param>
  493. /// <param name="localMemorySize">Local memory size of the compute shader</param>
  494. /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
  495. /// <returns>The generated translator context</returns>
  496. private TranslatorContext DecodeComputeShader(
  497. GpuState state,
  498. ulong gpuVa,
  499. int localSizeX,
  500. int localSizeY,
  501. int localSizeZ,
  502. int localMemorySize,
  503. int sharedMemorySize)
  504. {
  505. if (gpuVa == 0)
  506. {
  507. return null;
  508. }
  509. GpuAccessor gpuAccessor = new GpuAccessor(_context, state, localSizeX, localSizeY, localSizeZ, localMemorySize, sharedMemorySize);
  510. return Translator.CreateContext(gpuVa, gpuAccessor, DefaultFlags | TranslationFlags.Compute);
  511. }
  512. /// <summary>
  513. /// Decode the binary Maxwell shader code to a translator context.
  514. /// </summary>
  515. /// <remarks>
  516. /// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
  517. /// </remarks>
  518. /// <param name="state">Current GPU state</param>
  519. /// <param name="counts">Cumulative shader resource counts</param>
  520. /// <param name="flags">Flags that controls shader translation</param>
  521. /// <param name="stage">Shader stage</param>
  522. /// <param name="gpuVa">GPU virtual address of the shader code</param>
  523. /// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" shader code</param>
  524. /// <returns>The generated translator context</returns>
  525. private TranslatorContext DecodeGraphicsShader(
  526. GpuState state,
  527. TranslationCounts counts,
  528. TranslationFlags flags,
  529. ShaderStage stage,
  530. ulong gpuVa,
  531. ulong gpuVaA = 0)
  532. {
  533. if (gpuVa == 0)
  534. {
  535. return null;
  536. }
  537. GpuAccessor gpuAccessor = new GpuAccessor(_context, state, (int)stage - 1);
  538. if (gpuVaA != 0)
  539. {
  540. return Translator.CreateContext(gpuVaA, gpuVa, gpuAccessor, flags, counts);
  541. }
  542. else
  543. {
  544. return Translator.CreateContext(gpuVa, gpuAccessor, flags, counts);
  545. }
  546. }
  547. /// <summary>
  548. /// Translates a previously generated translator context to something that the host API accepts.
  549. /// </summary>
  550. /// <param name="translatorContext">Current translator context to translate</param>
  551. /// <returns>Compiled graphics shader code</returns>
  552. private ShaderCodeHolder TranslateShader(TranslatorContext translatorContext)
  553. {
  554. if (translatorContext == null)
  555. {
  556. return null;
  557. }
  558. if (translatorContext.AddressA != 0)
  559. {
  560. byte[] codeA = _context.MemoryManager.GetSpan(translatorContext.AddressA, translatorContext.SizeA).ToArray();
  561. byte[] codeB = _context.MemoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray();
  562. _dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
  563. _dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB);
  564. ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo);
  565. if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
  566. {
  567. program.Prepend("// " + codePathB);
  568. program.Prepend("// " + fullPathB);
  569. program.Prepend("// " + codePathA);
  570. program.Prepend("// " + fullPathA);
  571. }
  572. return new ShaderCodeHolder(program, shaderProgramInfo, codeB, codeA);
  573. }
  574. else
  575. {
  576. byte[] code = _context.MemoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray();
  577. _dumper.Dump(code, compute: false, out string fullPath, out string codePath);
  578. ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo);
  579. if (fullPath != null && codePath != null)
  580. {
  581. program.Prepend("// " + codePath);
  582. program.Prepend("// " + fullPath);
  583. }
  584. return new ShaderCodeHolder(program, shaderProgramInfo, code);
  585. }
  586. }
  587. /// <summary>
  588. /// Disposes the shader cache, deleting all the cached shaders.
  589. /// It's an error to use the shader cache after disposal.
  590. /// </summary>
  591. public void Dispose()
  592. {
  593. foreach (List<ShaderBundle> list in _cpPrograms.Values)
  594. {
  595. foreach (ShaderBundle bundle in list)
  596. {
  597. bundle.Dispose();
  598. }
  599. }
  600. foreach (List<ShaderBundle> list in _gpPrograms.Values)
  601. {
  602. foreach (ShaderBundle bundle in list)
  603. {
  604. bundle.Dispose();
  605. }
  606. }
  607. _cacheManager?.Dispose();
  608. }
  609. /// <summary>
  610. /// Create a guest shader program.
  611. /// </summary>
  612. /// <param name="programCode">The program code of the shader code</param>
  613. /// <param name="shaderCacheEntries">The resulting guest shader entries header</param>
  614. /// <param name="tfd">The transform feedback descriptors in use</param>
  615. /// <returns>The resulting guest shader program</returns>
  616. private static byte[] CreateGuestProgramDump(ReadOnlySpan<byte> programCode, GuestShaderCacheEntryHeader[] shaderCacheEntries, TransformFeedbackDescriptor[] tfd)
  617. {
  618. using (MemoryStream resultStream = new MemoryStream())
  619. {
  620. BinaryWriter resultStreamWriter = new BinaryWriter(resultStream);
  621. byte transformFeedbackCount = 0;
  622. if (tfd != null)
  623. {
  624. transformFeedbackCount = (byte)tfd.Length;
  625. }
  626. // Header
  627. resultStreamWriter.WriteStruct(new GuestShaderCacheHeader((byte)shaderCacheEntries.Length, transformFeedbackCount));
  628. // Write all entries header
  629. foreach (GuestShaderCacheEntryHeader entry in shaderCacheEntries)
  630. {
  631. resultStreamWriter.WriteStruct(entry);
  632. }
  633. // Finally, write all program code and all transform feedback information.
  634. resultStreamWriter.Write(programCode);
  635. return resultStream.ToArray();
  636. }
  637. }
  638. /// <summary>
  639. /// Write transform feedback guest information to the given stream.
  640. /// </summary>
  641. /// <param name="stream">The stream to write data to</param>
  642. /// <param name="tfd">The current transform feedback descriptors used</param>
  643. private static void WriteTransformationFeedbackInformation(Stream stream, TransformFeedbackDescriptor[] tfd)
  644. {
  645. if (tfd != null)
  646. {
  647. BinaryWriter writer = new BinaryWriter(stream);
  648. foreach (TransformFeedbackDescriptor transform in tfd)
  649. {
  650. writer.WriteStruct(new GuestShaderCacheTransformFeedbackHeader(transform.BufferIndex, transform.Stride, transform.VaryingLocations.Length));
  651. writer.Write(transform.VaryingLocations);
  652. }
  653. }
  654. }
  655. /// <summary>
  656. /// Read transform feedback descriptors from guest.
  657. /// </summary>
  658. /// <param name="data">The raw guest transform feedback descriptors</param>
  659. /// <param name="header">The guest shader program header</param>
  660. /// <returns>The transform feedback descriptors read from guest</returns>
  661. private static TransformFeedbackDescriptor[] ReadTransformationFeedbackInformations(ref ReadOnlySpan<byte> data, GuestShaderCacheHeader header)
  662. {
  663. if (header.TransformFeedbackCount != 0)
  664. {
  665. TransformFeedbackDescriptor[] result = new TransformFeedbackDescriptor[header.TransformFeedbackCount];
  666. for (int i = 0; i < result.Length; i++)
  667. {
  668. GuestShaderCacheTransformFeedbackHeader feedbackHeader = MemoryMarshal.Read<GuestShaderCacheTransformFeedbackHeader>(data);
  669. result[i] = new TransformFeedbackDescriptor(feedbackHeader.BufferIndex, feedbackHeader.Stride, data.Slice(Unsafe.SizeOf<GuestShaderCacheTransformFeedbackHeader>(), feedbackHeader.VaryingLocationsLength).ToArray());
  670. data = data.Slice(Unsafe.SizeOf<GuestShaderCacheTransformFeedbackHeader>() + feedbackHeader.VaryingLocationsLength);
  671. }
  672. return result;
  673. }
  674. return null;
  675. }
  676. /// <summary>
  677. /// Create a new instance of <see cref="GuestGpuAccessorHeader"/> from an gpu accessor.
  678. /// </summary>
  679. /// <param name="gpuAccessor">The gpu accessor</param>
  680. /// <returns>a new instance of <see cref="GuestGpuAccessorHeader"/></returns>
  681. private static GuestGpuAccessorHeader CreateGuestGpuAccessorCache(IGpuAccessor gpuAccessor)
  682. {
  683. return new GuestGpuAccessorHeader
  684. {
  685. ComputeLocalSizeX = gpuAccessor.QueryComputeLocalSizeX(),
  686. ComputeLocalSizeY = gpuAccessor.QueryComputeLocalSizeY(),
  687. ComputeLocalSizeZ = gpuAccessor.QueryComputeLocalSizeZ(),
  688. ComputeLocalMemorySize = gpuAccessor.QueryComputeLocalMemorySize(),
  689. ComputeSharedMemorySize = gpuAccessor.QueryComputeSharedMemorySize(),
  690. PrimitiveTopology = gpuAccessor.QueryPrimitiveTopology(),
  691. };
  692. }
  693. /// <summary>
  694. /// Write the guest GpuAccessor informations to the given stream.
  695. /// </summary>
  696. /// <param name="stream">The stream to write the guest GpuAcessor</param>
  697. /// <param name="shaderContext">The shader tranlator context in use</param>
  698. /// <returns>The guest gpu accessor header</returns>
  699. private static GuestGpuAccessorHeader WriteGuestGpuAccessorCache(Stream stream, TranslatorContext shaderContext)
  700. {
  701. BinaryWriter writer = new BinaryWriter(stream);
  702. GuestGpuAccessorHeader header = CreateGuestGpuAccessorCache(shaderContext.GpuAccessor);
  703. // If we have a full gpu accessor, cache textures descriptors
  704. if (shaderContext.GpuAccessor is GpuAccessor gpuAccessor)
  705. {
  706. HashSet<int> textureHandlesInUse = shaderContext.TextureHandlesForCache;
  707. header.TextureDescriptorCount = textureHandlesInUse.Count;
  708. foreach (int textureHandle in textureHandlesInUse)
  709. {
  710. GuestTextureDescriptor textureDescriptor = gpuAccessor.GetTextureDescriptor(textureHandle).ToCache();
  711. textureDescriptor.Handle = (uint)textureHandle;
  712. writer.WriteStruct(textureDescriptor);
  713. }
  714. }
  715. return header;
  716. }
  717. /// <summary>
  718. /// Get the shader program information for use on the shader cache.
  719. /// </summary>
  720. /// <param name="tfd">The current transform feedback descriptors used</param>
  721. /// <param name="shaderContexts">The shader translators context in use</param>
  722. /// <param name="programCode">The resulting raw shader program code</param>
  723. /// <param name="programCodeHash">The resulting raw shader program code hash</param>
  724. /// <param name="entries">The resulting guest shader entries header</param>
  725. private void GetProgramInformations(TransformFeedbackDescriptor[] tfd, ReadOnlySpan<TranslatorContext> shaderContexts, out byte[] programCode, out Hash128 programCodeHash, out GuestShaderCacheEntryHeader[] entries)
  726. {
  727. GuestShaderCacheEntryHeader ComputeStage(Stream stream, TranslatorContext context)
  728. {
  729. if (context == null)
  730. {
  731. return new GuestShaderCacheEntryHeader();
  732. }
  733. ReadOnlySpan<byte> data = _context.MemoryManager.GetSpan(context.Address, context.Size);
  734. stream.Write(data);
  735. int size = data.Length;
  736. int sizeA = 0;
  737. if (context.AddressA != 0)
  738. {
  739. data = _context.MemoryManager.GetSpan(context.AddressA, context.SizeA);
  740. sizeA = data.Length;
  741. stream.Write(data);
  742. }
  743. GuestGpuAccessorHeader gpuAccessorHeader = WriteGuestGpuAccessorCache(stream, context);
  744. return new GuestShaderCacheEntryHeader(context.Stage, size, sizeA, gpuAccessorHeader);
  745. }
  746. entries = new GuestShaderCacheEntryHeader[shaderContexts.Length];
  747. using (MemoryStream stream = new MemoryStream())
  748. {
  749. for (int i = 0; i < shaderContexts.Length; i++)
  750. {
  751. entries[i] = ComputeStage(stream, shaderContexts[i]);
  752. }
  753. WriteTransformationFeedbackInformation(stream, tfd);
  754. programCode = stream.ToArray();
  755. programCodeHash = _cacheManager.ComputeHash(programCode);
  756. }
  757. }
  758. }
  759. }